-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
432 lines (381 loc) · 18.6 KB
/
main.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File: main.py
import sys
import os
import platform
import socket
import ctypes
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import *
from PySide6.QtCore import Qt, QFile, QIODevice, QCoreApplication
from PySide6.QtGui import QIcon
import util
from beamit import *
from infoErrorDialog import InfoErrorDialog
beamitsender = beamit_send()
beamitreceiver = beamit_receive()
serverurl = "<-serverurl->"
autoOpen = False
windowstatus = 3
filePath = ""
if platform.system() == "Windows":
filePath = os.path.expandvars(r'C:/Users/%USERNAME%/Documents/BeamIT')
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('beamit.de')
else:
filePath = os.path.expandvars(r'/home/$USER/BeamIT')
if socket.gethostname().find('.')>=0:
devicename=socket.gethostname()
else:
devicename=socket.gethostbyaddr(socket.gethostname())[0]
returnvalue = None
class SignupPage(QWidget):
"""
Method to load the UI.
"""
def load_ui(self):
ui_file_name = "Signup-page.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
loader.load(ui_file, self)
ui_file.close()
def __init__(self):
super(SignupPage, self).__init__()
self.load_ui()
self.setWindowTitle("BeamIT sign up")
self.setWindowIcon(QIcon('icon.ico'))
self.image = None
self.te_username: QTextEdit = self.findChild(QTextEdit, "te_username")
self.le_password: QLineEdit= self.findChild(QLineEdit, "le_password")
self.le_repeat_password: QLineEdit= self.findChild(QLineEdit, "le_repeat_password")
self.te_connection: QTextEdit = self.findChild(QTextEdit, "te_connection")
self.te_connection.setText(serverurl)
self.b_confirm: QPushButton = self.findChild(QPushButton, "b_confirm")
self.b_confirm.clicked.connect(self.b_confirm_handler)
self.b_confirm_and_sign: QPushButton = self.findChild(QPushButton, "b_confirm_and_sign")
self.b_confirm_and_sign.clicked.connect(self.b_confirm_and_sign_handler)
self.b_redirect_login: QPushButton = self.findChild(QPushButton, "b_redirect_login")
self.b_redirect_login.clicked.connect(self.b_redirect_login_handler)
"""
Method to check the password and password repeat input. Then confirm the SignUp.
"""
def b_confirm_handler(self):
if self.le_password.text() == self.le_repeat_password.text():
beamitsender.server_url = self.te_connection.toPlainText()
response = beamitsender.register(self.te_username.toPlainText(), self.le_password.text())
if response['successfull'] != True:
self.error = InfoErrorDialog(response['message'])
self.error.exec()
app.exit(2)
else:
self.info = InfoErrorDialog(response['message'], showRedErrorLabel=False)
self.info.exec()
else:
self.error = InfoErrorDialog("Passwords are not identical")
self.error.exec()
"""
Method to check the password and password repeat input. Then confirm the SignUp and LogIn.
"""
def b_confirm_and_sign_handler(self):
if self.le_password.text() == self.le_repeat_password.text():
beamitsender.server_url = self.te_connection.toPlainText()
response = beamitsender.register(self.te_username.toPlainText(), self.le_password.text())
if response['successfull'] != True:
self.error = InfoErrorDialog(response['message'])
self.error.exec()
app.exit(2)
else:
self.info = InfoErrorDialog(response['message'], showRedErrorLabel=False)
self.info.exec()
try:
response = beamitsender.login(url=self.te_connection.toPlainText(), username=self.te_username.toPlainText(), password=self.le_password.text(), devicename=devicename)
if response['successfull'] != True:
self.error = InfoErrorDialog(response['message'])
self.error.exec()
else:
accessToken = response['message']
util.storeConfig(serverurl=self.te_connection.toPlainText(), username=accessToken['username'], devicename=accessToken['devicename'], devicetoken=accessToken['token'], filepath=filePath, autoOpen=False)
app.exit(3)
except Exception as e:
print(e)
self.error = InfoErrorDialog("Connection could not be established")
self.error.exec()
else:
self.error = InfoErrorDialog("Passwords are not identical")
self.error.show()
def b_redirect_login_handler(self):
app.exit(1)
class LoginPage(QWidget):
"""
Method to load the UI.
"""
def load_ui(self):
ui_file_name = "LoginPage.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
loader.load(ui_file, self)
ui_file.close()
def __init__(self):
super(LoginPage, self).__init__()
self.load_ui()
self.setWindowTitle("BeamIT Login")
self.setWindowIcon(QIcon('icon.ico'))
self.image = None
self.b_confirm: QPushButton = self.findChild(QPushButton, "b_confirm")
self.b_confirm.clicked.connect(self.b_confirm_handler)
self.b_redirect_signup: QPushButton = self.findChild(QPushButton, "b_redirect_signup")
self.b_redirect_signup.clicked.connect(self.b_redirect_signup_handler)
self.te_connection_input: QTextEdit = self.findChild(QTextEdit, "te_connection_input")
self.te_connection_input.setText(serverurl)
self.te_username: QTextEdit = self.findChild(QTextEdit, "te_username")
self.le_password: QLineEdit= self.findChild(QLineEdit, "le_password")
"""
Method to check the username and password.
"""
def b_confirm_handler(self):
try:
response = beamitsender.login(url=self.te_connection_input.toPlainText(), username=self.te_username.toPlainText(), password=self.le_password.text(), devicename=devicename)
if response['successfull'] != True:
self.error = InfoErrorDialog(response['message'])
self.error.exec()
app.exit(1)
else:
accessToken = response['message']
util.storeConfig(serverurl=self.te_connection_input.toPlainText(), username=accessToken['username'], devicename=accessToken['devicename'], devicetoken=accessToken['token'], filepath=filePath, autoOpen=False)
app.exit(3)
except Exception as e:
print(e)
self.error = InfoErrorDialog("Connection could not be established")
self.error.exec()
def b_redirect_signup_handler(self):
app.exit(2)
class MainWindow(QWidget):
"""
Method to load the UI.
"""
def load_ui(self):
ui_file_name = "mainwindow_v2.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
loader.load(ui_file, self)
ui_file.close()
def __init__(self):
super().__init__()
self.load_ui()
self.setWindowTitle("BeamIT Main")
self.setWindowIcon(QIcon('icon.ico'))
self.getGuiElements()
self.setAcceptDrops(True)
"""
Method to get the other GUI elements.
"""
def getGuiElements(self):
self.b_refresh: QPushButton = self.findChild(QPushButton, "b_refresh")
self.b_refresh.clicked.connect(self.b_refresh_handler)
self.lw_devices: QListWidget = self.findChild(QListWidget, "lw_devices")
self.b_delete_device: QPushButton = self.findChild(QPushButton, "b_delete_device")
self.b_delete_device.clicked.connect(self.b_delete_device_handler)
self.b_rename_current_device: QPushButton = self.findChild(QPushButton, "b_rename_current_device")
self.b_rename_current_device.clicked.connect(self.b_rename_current_device_handler)
self.b_locate_files: QPushButton = self.findChild(QPushButton, "b_locate_files")
self.b_locate_files.clicked.connect(self.b_locate_files_handler)
self.l_dragdrop: QLabel = self.findChild(QLabel, "l_dragdrop")
self.cb_auto_open: QCheckBox = self.findChild(QCheckBox, "cb_auto_open")
if config['autoOpen'] == "True":
self.cb_auto_open.setChecked(True)
elif config['autoOpen'] == "False":
self.cb_auto_open.setChecked(False)
self.cb_auto_open.stateChanged.connect(self.cb_auto_open_changed)
self.b_set_path: QPushButton = self.findChild(QPushButton, "b_set_path")
self.b_set_path.clicked.connect(self.b_set_path_handler)
self.b_delete_account: QPushButton = self.findChild(QPushButton, "b_delete_account")
self.b_delete_account.clicked.connect(self.b_delete_account_handler)
self.b_logout: QPushButton = self.findChild(QPushButton, "b_logout")
self.b_logout.clicked.connect(self.b_logout_handler)
"""
Method to load the devices from the associated user.
"""
def loadDevices(self):
self.lw_devices.clear()
response = beamitsender.listDevices()
if response['successfull'] == True:
for device in response['message']:
self.lw_devices.addItem(QListWidgetItem(device))
else:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=True)
def b_refresh_handler(self):
self.loadDevices()
"""
Method to delete the devices from the associated user.
"""
def b_delete_device_handler(self):
if self.lw_devices.currentItem():
if not self.lw_devices.currentItem().text() == config['devicename']:
response = beamitsender.removeDevice(self.lw_devices.currentItem().text())
if response['successfull']:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=False)
else:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=True)
self.loadDevices()
else:
self.error = InfoErrorDialog("Can not delete own device. Use logout instead")
self.error.exec()
else:
self.error = InfoErrorDialog("No target device selected")
self.error.exec()
"""
Method to rename the device with which you are logged in.
"""
def b_rename_current_device_handler(self):
self.info = InfoErrorDialog(message="Enter new device name", showRedErrorLabel=False, showUserInputFile=True)
self.info.exec()
newDeviceName = self.info.input
response = beamitsender.renameDevices(newDeviceName)
if response['successfull']:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=False)
util.storeConfig(serverurl=serverurl, username=config['username'], devicename=newDeviceName, devicetoken=config['devicetoken'], filepath=filePath, autoOpen=config['autoOpen'])
beamitsender.devicename=newDeviceName
else:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=True)
self.loadDevices()
#TODO wird nicht im richtigen ordner geöffnet
def b_locate_files_handler(self):
print(filePath)
util.startfile(path=filePath, filename="", args="explore")
"""
Methods to get the Drag&Drop-Functionality.
"""
def dragEnterEvent(self, event):
if event.answerRect().intersects(self.l_dragdrop.geometry()):
event.acceptProposedAction()
def dropEvent(self, event):
if self.lw_devices.currentItem():
if event.mimeData().text().startswith(("https://", "http://", "www.")):
response = beamitsender.beamitShare(targetDevices=("{" + self.lw_devices.currentItem().text() + "}"), autoOpen=False, encrypted=False, datatype="url", senddata=event.mimeData().text())
if not response['successfull']:
self.error = InfoErrorDialog(message=response['message'])
self.error.show()
elif event.mimeData().text().startswith(("file:///", '/')):
filename, filecontent = util.getFile(event.mimeData().text())
response = beamitsender.beamitShare(targetDevices=("{" + self.lw_devices.currentItem().text() + "}"), autoOpen=False, encrypted=False, datatype="files", senddata=(filename, filecontent))
if not response['successfull']:
self.error = InfoErrorDialog(message=response['message'])
self.error.show()
else:
response = beamitsender.beamitShare(targetDevices=("{" + self.lw_devices.currentItem().text() + "}"), autoOpen=False, encrypted=False, datatype="text", senddata=event.mimeData().text())
if not response['successfull']:
self.error = InfoErrorDialog(message=response['message'])
self.error.show()
else:
self.error = InfoErrorDialog("No target device selected")
self.error.exec()
def cb_auto_open_changed(self):
if self.cb_auto_open.checkState() == self.cb_auto_open.checkState().Checked:
beamitreceiver.autoOpen = True
util.storeConfig(serverurl=serverurl, username=config['username'], devicename=config['devicename'], devicetoken=config['devicetoken'], filepath=filePath, autoOpen=True)
else:
beamitreceiver.autoOpen = False
util.storeConfig(serverurl=serverurl, username=config['username'], devicename=config['devicename'], devicetoken=config['devicetoken'], filepath=filePath, autoOpen=False)
#TODO how detect if closed or confirmed path?
def b_set_path_handler(self):
self.pathdialog = QFileDialog()
self.pathdialog.setFileMode(QFileDialog.FileMode.Directory)
self.pathdialog.exec()
filePath = self.pathdialog.selectedFiles()[0]
beamitreceiver.filePath = filePath
util.storeConfig(serverurl=serverurl, username=config['username'], devicename=config['devicename'], devicetoken=config['devicetoken'], filepath=filePath)
self.info = InfoErrorDialog("Path changed to " + filePath, showRedErrorLabel=False)
self.info.exec()
"""
Method to delete the account with which you are logged in.
"""
def b_delete_account_handler(self):
self.delete_account_dialog = QMessageBox()
self.delete_account_dialog.setWindowTitle("Delete Account?")
self.delete_account_dialog.setText("Do you really want to delete your Account from Server?")
self.delete_account_dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
button = self.delete_account_dialog.exec()
if button == QMessageBox.Yes:
response = beamitsender.unregister()
if response['successfull']:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=False)
self.info.exec()
app.exit(1)
else:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=True)
self.info.exec()
"""
Method to logout from the device.
"""
def b_logout_handler(self):
response = beamitsender.removeDevice(devicename)
if response['successfull']:
self.info = InfoErrorDialog("User logged out successfully", showRedErrorLabel=False)
self.info.exec()
util.removeConfig()
app.exit(1)
else:
self.info = InfoErrorDialog(message=response['message'], showRedErrorLabel=True)
self.info.exec()
if __name__ == "__main__":
if sys.argv[1]:
os.chdir(sys.argv[1])
QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
app = QApplication()
while (1):
if windowstatus == 0: # Exit Application
sys.exit()
if windowstatus == 1: # Login screen
loginpage = LoginPage()
loginpage.show()
windowstatus = app.exec()
loginpage.close()
if windowstatus == 2: # Signup screen
signuppage = SignupPage()
signuppage.show()
windowstatus = app.exec()
signuppage.close()
if windowstatus == 3: # Main screen
config = util.getConfig()
if config:
mainWindow = MainWindow()
beamitsender.init(config['serverurl'], config['username'], config['devicename'], config['devicetoken'])
filePath = config['filepath']
devicename = config['devicename']
serverurl = config['serverurl']
try:
response = beamitsender.listDevices()
if response['successfull']:
util.createFolder(filePath)
mainWindow.show()
mainWindow.loadDevices()
beamitreceiver.init(config['serverurl'], config['username'], config['devicename'], config['devicetoken'], filePath, autoOpen=config['autoOpen'])
beamitreceiver.start()
windowstatus = app.exec()
mainWindow.close()
beamitreceiver.stop()
else:
error = InfoErrorDialog(message=response['message'])
error.show()
error.exec()
windowstatus = 1
except Exception as e:
print(e)
windowstatus = 1
delete_account_dialog = QMessageBox()
delete_account_dialog.setWindowTitle("Server not reachable")
delete_account_dialog.setText("The Server " + serverurl + " is not reachable. Do you want to logout?")
delete_account_dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
button = delete_account_dialog.exec()
if button == QMessageBox.Yes:
util.removeConfig()
windowstatus=1
else:
windowstatus = 1