This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpcgui.py
316 lines (235 loc) · 10.4 KB
/
rpcgui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import logging
import random
import string
import sys
import time
from pathlib import Path
import sentry_sdk
from PyQt5 import QtGui as Qg
from PyQt5 import QtWidgets as Qw
import tabs
import util
# hardcoded to catch early errors, we'll set more metadata later on
# when our config files are available.
sentry_sdk.init("https://[email protected]/5338196")
# set up logging and add our custom GUI handler
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
formatter = logging.Formatter('[%(asctime)s] %(threadName)s %(levelname)s: %(message)s',
'%H:%M:%S')
logging.getLogger('requests').setLevel(logging.WARNING)
gui_handler = util.GUILoggerHandler()
gui_handler.setLevel(logging.INFO)
gui_handler.setFormatter(formatter)
file_handler = util.FileLoggerHandler()
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(gui_handler)
logger.addHandler(file_handler)
script_dir = Path(sys.argv[0]).parent
data_dir = script_dir / 'data'
# in case we crash prematurely, let's construct the bare minimum dir structure
(script_dir / 'logs' / 'errors').mkdir(parents=True, exist_ok=True)
def on_error(exc_type, exc_value, exc_traceback):
tb = exc_traceback.format()
log_path = file_handler.create_error_log(tb)
util.MsgBoxes.error('\n'.join(tb), path=log_path)
sys.__excepthook__(exc_type, exc_value, exc_traceback)
sys.excepthook = on_error
class SystemTrayIcon(Qw.QSystemTrayIcon):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.update()
self.quit_button = None
self.toggle_button = None
self.open_button = None
self.activated.connect(self._open_window)
self.menu = Qw.QMenu(parent)
self.populate_menu(self.menu)
self.setContextMenu(self.menu)
def _toggle_enabled(self):
curr_enabled = self.parent.wiimmfi_thread.run
if curr_enabled:
self.parent.wiimmfi_thread.run = False
self.toggle_button.setText('Enable game detection')
else:
self.parent.wiimmfi_thread.run = True
self.toggle_button.setText('Disable game detection')
self.update()
def _open_window(self, reason=None):
if reason and reason == Qw.QSystemTrayIcon.Context: # right-click
return
self.parent.setHidden(False)
self.parent.activateWindow()
def _quit(self):
self.parent.do_close = True
self.parent.close()
def populate_menu(self, menu):
version = self.parent.config.version_info['version']
header = menu.addAction(f'Wiimmfi-RPC v{version}')
header.setDisabled(True)
menu.addSeparator()
self.open_button = menu.addAction('Open Wiimmfi-RPC')
self.open_button.triggered.connect(self._open_window)
toggle_text = 'Disable' if self.parent.wiimmfi_thread.run else 'Enable'
self.toggle_button = menu.addAction(toggle_text + ' game detection')
self.toggle_button.triggered.connect(self._toggle_enabled)
menu.addSeparator()
self.quit_button = menu.addAction('Quit Wiimmfi-RPC')
self.quit_button.triggered.connect(self._quit)
def update(self):
online_player = self.parent.wiimmfi_thread.last_player
if not self.parent.wiimmfi_thread.run:
icon_path = script_dir / 'icons' / 'disabled.png'
self.setToolTip('Game detection has been disabled.')
elif online_player:
icon_path = script_dir / 'icons' / 'active.png'
self.setToolTip(f'Playing {online_player.game_name}.')
else:
icon_path = script_dir / 'icons' / 'inactive.png'
self.setToolTip('Not playing any games.')
icon = Qg.QIcon(str(icon_path))
self.setIcon(icon)
class TableWidget(Qw.QWidget):
TABS = (
tabs.OverviewTab,
tabs.FriendcodesTab,
tabs.OnlinePlayerTab,
tabs.SettingsTab,
tabs.LogsTab
)
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.config = self.parent.config
self.layout = Qw.QVBoxLayout(self)
# Initialize tab screen
self.tabs = Qw.QTabWidget()
self.tabs.resize(400, 400)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
logging.info('Initialized window tabs')
def add_tabs(self):
for tab in self.TABS:
name = tab.OPTIONS.pop('name')
debug = tab.OPTIONS.pop('debug')
if debug and not self.config.preferences['debug']:
# debug mode must be enabled for debug tabs
continue
params = {
'config': self.config,
'gui_handler': gui_handler
}
# initialize widget and add it to our tabs
tab_obj = tab(self.parent, **params)
self.tabs.addTab(tab_obj, name)
class Application(Qw.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.do_close = False
self.sys_tray: SystemTrayIcon = None # init now to prevent race conditions
self.setGeometry(0, 0, 400, 400)
# Set up the status bar + its widgets
self.status_bar = Qw.QStatusBar()
self.setStatusBar(self.status_bar)
self.thread_counter = Qw.QLabel()
self.thread_counter.setText('0/0 [p:0]')
self.progress_bar = Qw.QProgressBar()
self.progress_bar.setMaximum(100)
self.thread_status = Qw.QLabel()
self.thread_status.setText('No operations.')
self.status_bar.addWidget(self.thread_counter)
self.status_bar.addWidget(self.progress_bar)
self.status_bar.addWidget(self.thread_status)
self.thread_manager = util.ThreadManager(file_handler=file_handler,
thread_counter=self.thread_counter,
progress_bar=self.progress_bar,
thread_status=self.thread_status)
self.do_reload = util.full_check(self.thread_manager)
if self.do_reload:
while self.thread_manager.thread_queue:
# Ugly, but we need to block until all threads have finished here.
# Thread.wait() returns too early so we wait for all threads to
# get kicked out of the queue.
app.processEvents()
time.sleep(0.1)
logging.info('Successfully restored config files')
self.config = self.load_config()
logging.info('Loaded config files')
version = self.config.version_info['version']
self._init_sentry()
self.wiimmfi_thread = util.WiimmfiCheckThread(self.config, self._status_updated)
self.thread_manager.add_thread(self.wiimmfi_thread)
self.game_list_thread = util.WiimmfiGameListThread()
self.thread_manager.add_thread(self.game_list_thread)
self.updater = util.Updater(self.thread_manager, self.config)
self.updater.check_updates()
# Init the title and tabs.
self.setWindowTitle(f'Wiimmfi-RPC v{version}')
self.table_widget = TableWidget(self)
self.table_widget.add_tabs()
self.setCentralWidget(self.table_widget)
logging.info('---- Finished booting ----')
# We do this at the end to make sure it has
# access to all the resources it needs.
self.sys_tray = SystemTrayIcon(self)
self.sys_tray.show()
self.show()
def _init_sentry(self):
new_registered = False
user_id = self.config.preferences['config']['sentry']['user_id']
if not user_id: # generate a random new one, let's hope it's unique...
rand_id = ''.join([random.choice(string.ascii_letters
+ string.digits) for n in range(32)])
self.config.preferences['config']['sentry']['user_id'] = rand_id
self.config.preferences.flush()
new_registered = True
with sentry_sdk.configure_scope() as scope:
# noinspection PyUnresolvedReferences,PyDunderSlots
scope.user = {'id': self.config.preferences['config']['sentry']['user_id']}
scope.set_tag('version', self.config.version_info['version'])
scope.set_tag('thread_name', 'MainThread')
scope.set_tag('bundled', util.is_bundled())
if new_registered:
sentry_sdk.capture_message('newClient')
def _status_updated(self):
if self.sys_tray is None: # not yet initialized, we drop the update to prevent a race condition.
return
self.sys_tray.update()
def closeEvent(self, event: Qg.QCloseEvent):
self.setHidden(True)
# save all config files, in case something was pending
self.config.friend_codes.flush()
self.config.preferences.flush()
if not self.config.preferences['config']['tray']['minimize_on_exit']:
event.accept()
return
if self.do_close:
ok = util.MsgBoxes.promptyesno('Are you sure you want to quit?')
if ok:
event.accept()
else:
event.ignore()
if self.config.preferences['config']['tray']['show_notice']:
self.config.preferences['config']['tray']['show_notice'] = False
self.sys_tray.showMessage('Wiimmfi-RPC',
'Wiimmfi-RPC has been minimized to tray and will '
'keep running in the background. To quit the program, '
'right-click on this icon and select "Quit".')
def load_config(self):
config = util.Config(friend_codes=data_dir / 'friend_codes.json',
preferences=data_dir / 'preferences.json',
version_info=data_dir / 'version_info.json',
statuses=data_dir / 'statuses.json')
logging.info('Debug mode: '
+ ('ON' if config.preferences['debug'] else 'OFF'))
return config
if __name__ == '__main__':
logging.info('Starting...')
app = Qw.QApplication(sys.argv)
ex = Application()
if '--start-minimized' in sys.argv:
ex.setHidden(True)
sys.exit(app.exec_())