forked from blawar/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nut_gui.py
358 lines (287 loc) · 9.94 KB
/
nut_gui.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import time
import server
import socket
import urllib3
import threading
import webbrowser
import nut
from nut import usb
from nut import nsps
from nut import users
from nut import config
from nut import status
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtWidgets import QProgressBar
from PyQt5.QtWidgets import QTableWidget
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDesktopWidget
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import pyqtSlot
def getIpAddress():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except OSError:
return None
def formatSpeed(n):
return str(round(n / 1000 / 1000, 1)) + 'MB/s'
class Header:
def __init__(self, app):
self.layout = QHBoxLayout()
self.textbox = QLineEdit(app)
self.textbox.setMinimumWidth(25)
self.textbox.setAlignment(Qt.AlignLeft)
self.textbox.setText(os.path.abspath(config.paths.scan[0]))
self.textbox.textChanged.connect(self.updatePath)
self.layout.addWidget(self.textbox)
self.scan = QPushButton('Scan', app)
self.scan.clicked.connect(app.on_scan)
self.layout.addWidget(self.scan)
self.gdrive = QPushButton('Setup GDrive OAuth', app)
self.gdrive.clicked.connect(app.on_gdrive)
self.layout.addWidget(self.gdrive)
ipAddr = getIpAddress()
if ipAddr:
self.serverInfo = QLabel(
f"<b>IP:</b> {ipAddr} <b>Port:</b> {config.server.port} " +
f"<b>User:</b> {users.first().id} <b>Password:</b> " +
f"{users.first().password}"
)
else:
self.serverInfo = QLabel("<b>Offline</b>")
self.serverInfo.setMinimumWidth(200)
self.serverInfo.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.serverInfo)
self.usbStatus = QLabel("<b>USB:</b> " + str(usb.status))
self.usbStatus.setMinimumWidth(50)
self.usbStatus.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.usbStatus)
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.tick)
self.timer.start()
def updatePath(self):
config.paths.scan[0] = self.textbox.text()
config.save()
def tick(self):
self.usbStatus.setText("<b>USB:</b> " + str(usb.status))
class Progress:
def __init__(self, app):
self.app = app
self.progress = QProgressBar(app)
self.text = QLabel()
self.speed = QLabel()
self.text.resize(100, 40)
self.speed.resize(100, 40)
self.layout = QHBoxLayout()
self.layout.addWidget(self.text)
self.layout.addWidget(self.progress)
self.layout.addWidget(self.speed)
self.timer = QTimer()
self.timer.setInterval(250)
self.timer.timeout.connect(self.tick)
self.timer.start()
def resetStatus(self):
self.progress.setValue(0)
self.text.setText('')
self.speed.setText('')
def tick(self):
for i in status.lst:
if i.isOpen():
try:
self.progress.setValue(i.i / i.size * 100)
self.text.setText(i.desc)
self.speed.setText(
formatSpeed(i.a / (time.process_time() - i.ats))
)
# TODO: Remove bare except
except:
self.resetStatus()
break
else:
self.resetStatus()
if len(status.lst) == 0:
self.resetStatus()
if self.app.needsRefresh:
self.app.needsRefresh = False
self.app.refreshTable()
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowIcon(QIcon('images/logo.jpg'))
screen = QDesktopWidget().screenGeometry()
self.title = 'NUT USB / Web Server v2.7'
self.left = int(screen.width() / 4)
self.top = int(screen.height() / 4)
self.width = int(screen.width() / 2)
self.height = int(screen.height() / 2)
self.needsRefresh = False
self.initUI()
def refresh(self):
self.needsRefresh = True
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
self.layout = QVBoxLayout()
self.header = Header(self)
self.layout.addLayout(self.header.layout)
self.layout.addWidget(self.tableWidget)
self.progress = Progress(self)
self.layout.addLayout(self.progress.layout)
self.setLayout(self.layout)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.tableWidget.setColumnCount(4)
headers = [
QTableWidgetItem("File"),
QTableWidgetItem("Title ID"),
QTableWidgetItem("Type"),
QTableWidgetItem("Size")
]
i = 0
for h in headers:
self.tableWidget.setHorizontalHeaderItem(i, h)
i = i + 1
header = self.tableWidget.horizontalHeader()
i = 0
for h in headers:
mode = QHeaderView.Stretch if i == 0 else \
QHeaderView.ResizeToContents
header.setSectionResizeMode(i, mode)
i = i + 1
self.tableWidget.setSortingEnabled(True)
self.refreshTable()
@pyqtSlot()
def on_scan(self):
self.tableWidget.setRowCount(0)
nut.scan()
self.refreshTable()
@pyqtSlot()
def on_gdrive(self):
if config.getGdriveCredentialsFile() is None:
webbrowser.open_new_tab(
'https://developers.google.com/drive/api/v3/quickstart/go',
)
QMessageBox.information(
self,
'Google Drive OAuth Setup',
"You require a credentials.json file to set up Google Drive " +
"OAuth. This file can be obtained from " +
"https://developers.google.com/drive/api/v3/quickstart/go , " +
"click on the blue button that says 'Enable the Drive API' " +
"and save the credentials.json to t his application's " +
"directory.",
)
else:
buttonReply = QMessageBox.question(
self,
'Google Drive OAuth Setup',
"Do you you want to setup GDrive OAuth?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No,
)
if buttonReply == QMessageBox.Yes:
try:
os.unlink('gdrive.token')
# TODO: Remove bare except
except:
pass
try:
os.unlink('token.pickle')
# TODO: Remove bare except
except:
pass
server.controller.api.getGdriveToken(None, None)
QMessageBox.information(
self,
'Google Drive OAuth Setup',
"OAuth has completed. Please copy gdrive.token and " +
"credentials.json to your Nintendo Switch's " +
"sdmc:/switch/tinfoil/ and/or sdmc:/switch/sx/ " +
"directories."
)
@pyqtSlot()
def refreshTable(self):
try:
self.tableWidget.setRowCount(0)
self.tableWidget.setRowCount(len(nsps.files))
i = 0
for k, f in nsps.files.items():
if f.path.endswith('.nsx'):
continue
titleType = "UPD" if f.isUpdate() else "DLC" if f.isDLC() \
else "BASE"
self.tableWidget.setItem(
i,
0,
QTableWidgetItem(f.fileName()),
)
self.tableWidget.setItem(
i,
1,
QTableWidgetItem(str(f.titleId)),
)
self.tableWidget.setItem(
i,
2,
QTableWidgetItem(titleType),
)
self.tableWidget.setItem(
i,
3,
QTableWidgetItem(str(f.fileSize)),
)
i = i + 1
self.tableWidget.setRowCount(i)
except BaseException as e:
print('exception: ' + str(e))
pass
threadRun = True
def usbThread():
usb.daemon()
def nutThread():
server.run()
def initThread(app):
nut.scan()
app.refresh()
def run():
urllib3.disable_warnings()
print(' ,;:;;,')
print(' ;;;;;')
print(' .=\', ;:;;:,')
print(' /_\', "=. \';:;:;')
print(' @=:__, \\,;:;:\'')
print(' _(\\.= ;:;;\'')
print(' `"_( _/="`')
print(' `"\'')
nut.initFiles()
app = QApplication(sys.argv)
ex = App()
threads = []
threads.append(threading.Thread(target=initThread, args=[ex]))
threads.append(threading.Thread(target=usbThread, args=[]))
threads.append(threading.Thread(target=nutThread, args=[]))
for t in threads:
t.start()
sys.exit(app.exec_())
print('fin')
if __name__ == '__main__':
run()