-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
344 lines (285 loc) · 11.1 KB
/
app.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
import os
import sys
import webbrowser
from PySide6.QtCore import Slot, QSize, QUrl, Qt, QTimer
from PySide6.QtGui import QIcon, QGuiApplication
from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineDownloadRequest, QWebEngineProfile
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, QHBoxLayout, QLabel, QMainWindow,
QPushButton, QSizePolicy, QSystemTrayIcon, QVBoxLayout, QWidget)
def asset_path(asset_name):
"""
Construct correct path for the asset file
"""
return os.path.join(os.path.dirname(__file__), "assets", asset_name)
def show_notification(title, msg):
"""
Show custom notification in the system tray
"""
tray_icon = QSystemTrayIcon(QIcon(asset_path('logo.png')), parent=app)
tray_icon.show()
tray_icon.showMessage(
title, "Your file has been successfully downloaded.", QSystemTrayIcon.Information, 5000)
class TempPage(QWebEnginePage):
"""
Temporary page to facilitate capturing the URLs of any clicked links
"""
def __init__(self, webview, profile, parent=None):
super().__init__(profile, parent)
self.web = webview
self.urlChanged.connect(self.handle_url_changed)
self.allowlist = [
'mail.proton.me', 'calendar.proton.me', 'drive.proton.me', 'account.proton.me']
@Slot(QUrl)
def handle_url_changed(self, url: QUrl):
if url.host() not in self.allowlist:
webbrowser.open(url.toString())
else:
self.web.load(url)
self.deleteLater()
class ProtonWebPage(QWebEnginePage):
"""
Custom web page for loading Proton services
"""
def __init__(self, profile, parent=None):
super().__init__(profile, parent)
def createWindow(self, _):
return TempPage(self.parent(), self.profile(), self)
class ProtonWebView(QWebEngineView):
"""
Custom QWebEngineView for Protodesk
"""
def __init__(self, profile, parent=None):
super().__init__(profile, parent)
self.setPage(ProtonWebPage(profile, self))
profile = self.page().profile()
profile.downloadRequested.connect(self.handle_download)
# load initial page: Proton Mail
self.page().setUrl(QUrl('https://mail.proton.me'))
def handle_download(self, download: QWebEngineDownloadRequest):
"""
Handle a file download request
"""
suggested_filename = download.downloadFileName()
save_path, _ = QFileDialog.getSaveFileName(self, 'Save File', suggested_filename)
if save_path:
download.setDownloadDirectory(os.path.dirname(save_path))
download.accept()
self.check_download_status(download)
else:
download.cancel()
def check_download_status(self, download: QWebEngineDownloadRequest):
"""
Periodically check if a download is finished and show a notification if it is.
"""
if download.isFinished():
if download.state() == QWebEngineDownloadRequest.DownloadState.DownloadCompleted:
show_notification('Download Complete', 'File has been downloaded successfully')
else:
show_notification('Download Failed', download.errorString())
else:
QTimer.singleShot(500, lambda: self.check_download_status(download))
class DonateDialog(QDialog):
"""
Dialog for donating to the project
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Donate')
self.setFixedSize(300, 300)
self.setWindowFlags(Qt.Popup)
self.setStyleSheet("""
QDialog {
background-color: #2D2D2D;
border-radius: 10px;
}
QLabel {
font-family: 'Arial';
font-size: 12pt;
color: white;
}
QLabel#title_label {
font-size: 16pt;
font-weight: bold;
color: #0076D1;
}
QPushButton {
font-family: 'Arial';
font-size: 12pt;
background-color: white;
color: #000000;
border-radius: 5px;
}
""")
layout = QVBoxLayout()
title_label = QLabel('Donate')
title_label.setObjectName("title_label")
title_label.setAlignment(Qt.AlignCenter)
layout.addWidget(title_label)
info_label = QLabel("Support the development of \nProtodesk.")
info_label.setAlignment(Qt.AlignCenter)
layout.addWidget(info_label)
paypal_btn = QPushButton()
paypal_btn.setIcon(QIcon(asset_path('paypal.svg')))
paypal_btn.setIconSize(QSize(150, 80))
paypal_btn.clicked.connect(self.open_paypal)
layout.addWidget(paypal_btn)
kofi_btn = QPushButton()
kofi_btn.setIcon(QIcon(asset_path('kofi.svg')))
kofi_btn.setIconSize(QSize(150, 80))
kofi_btn.clicked.connect(self.open_kofi)
layout.addWidget(kofi_btn)
self.setLayout(layout)
def open_paypal(self):
paypal_url = 'https://www.paypal.com/donate/?hosted_button_id=8KU8MDWA86SNJ'
webbrowser.open(paypal_url)
def open_kofi(self):
kofi_url = 'https://ko-fi.com/nemuelw'
webbrowser.open(kofi_url)
class AboutDialog(QDialog):
"""
Dialog for info on the application: version, description, author, and contact information.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('About Protodesk')
self.setFixedSize(300, 230)
self.setWindowFlags(Qt.Popup)
self.setStyleSheet("""
QDialog {
background-color: #2D2D2D;
border-radius: 10px;
padding: 10px;
}
QLabel {
font-family: 'Arial';
font-size: 12pt;
color: white;
}
QLabel#title_label {
font-size: 14pt;
font-weight: bold;
color: #0076D1;
}
QPushButton {
font-family: 'Arial';
font-size: 12pt;
background-color: #0076D1;
color: white;
padding: 10px;
border: none;
border-radius: 10px;
width: 100%;
}
QPushButton:hover {
background-color: #005BB5;
}
""")
layout = QVBoxLayout()
title_label = QLabel('Protodesk')
title_label.setObjectName("title_label")
title_label.setAlignment(Qt.AlignCenter)
layout.addWidget(title_label)
info_label = QLabel('Version 1.3.0\nUnofficial desktop app for Proton.')
info_label.setAlignment(Qt.AlignCenter)
layout.addWidget(info_label)
dev_label = QLabel('Author: Nemuel Wainaina\nContact: [email protected]')
dev_label.setAlignment(Qt.AlignCenter)
layout.addWidget(dev_label)
ok_button = QPushButton("Ok")
ok_button.clicked.connect(self.accept)
layout.addWidget(ok_button)
self.setLayout(layout)
class ProtonDesktopApp(QMainWindow):
"""
Main application window for Protodesk
"""
def __init__(self):
super().__init__()
self.proton_services = {
'mail': 'https://mail.proton.me',
'calendar': 'https://calendar.proton.me',
'drive': 'https://drive.proton.me'
}
self.setWindowTitle('Protodesk')
# window size and position
screen_geometry = QGuiApplication.primaryScreen().availableGeometry()
screen_width, screen_height = screen_geometry.width(), screen_geometry.width()
self.setGeometry(0, 0, screen_width, screen_height)
self.central_widget = QWidget(self)
self.main_layout = QHBoxLayout(self.central_widget)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.setSpacing(0)
self.setCentralWidget(self.central_widget)
# styles for tooltips
self.setStyleSheet('''
QToolTip {
background-color: #333333;
color: #ffffff;
font: 14px;
border: 1px solid #888888;
padding: 5px;
opacity: 200;
}
''')
# sidebar
self.sidebar = QWidget(self)
self.sidebar_layout = QVBoxLayout(self.sidebar)
self.sidebar.setFixedWidth(60)
self.sidebar.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
self.sidebar.setStyleSheet('background-color: #505264;')
# sidebar buttons: mail, calendar, drive, donate, about
self.add_button('mail', 'Mail', 'mail.svg')
self.add_button('calendar', 'Calendar', 'calendar.svg')
self.add_button('drive', 'Drive', 'drive.svg')
self.sidebar_layout.addStretch()
self.add_button('donate', 'Donate', 'donate.svg', self.show_donate_dialog)
self.add_button('about', 'About', 'about.svg', self.show_about_dialog)
self.main_layout.addWidget(self.sidebar, alignment=Qt.AlignLeft)
# enable on-disk persistence for session data
profile = QWebEngineProfile('protodesk')
# webview
self.web = ProtonWebView(profile, self)
self.main_layout.addWidget(self.web)
self.main_layout.setStretchFactor(self.web, 1)
def add_button(self, service_name, tooltip, icon_path, on_clicked=None):
"""
Add a button to the sidebar with the specified service name, tooltip, and icon.
Args:
service_name (str): The name of the Proton service being represented by the button.
tooltip (str): The tooltip for the button.
icon_path (str): The path to the icon file to use for the button.
on_clicked (function): An optional callback function to be executed when the button is clicked.
"""
btn = QPushButton(self)
btn.setIcon(QIcon(asset_path(icon_path)))
btn.setIconSize(QSize(32, 32))
btn.setStyleSheet("border: none; margin: 2px;")
btn.setToolTip(tooltip)
btn.clicked.connect(lambda:
on_clicked() if on_clicked else self.load_proton_service(service_name))
self.sidebar_layout.addWidget(btn)
def load_proton_service(self, service_name):
"""
Load the page for the specified Proton service inside the webview.
Args:
service_name (str): The name of the Proton service to load.
"""
destination_url = self.proton_services.get(service_name, 'mail')
self.web.load(QUrl(destination_url))
def show_donate_dialog(self):
"""
Show the 'Donate' dialog
"""
DonateDialog(self).show()
def show_about_dialog(self):
"""
Show the 'About' dialog
"""
AboutDialog(self).show()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon(asset_path('logo.png')))
window = ProtonDesktopApp()
window.show()
sys.exit(app.exec())