Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (153 loc) · 5.57 KB
/
Copy pathmain.py
File metadata and controls
175 lines (153 loc) · 5.57 KB
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
"""MailProcessor – entry point."""
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import Qt
import config as cfg_module
from i18n import set_language, tr
def _run_bundle_self_test() -> int:
"""Exercise packaged assets and the primary Qt UI without user state."""
from PySide6.QtCore import QEvent, QEventLoop, QObject, QTimer
from PySide6.QtGui import QIcon
from config import AppConfig
from settings_dialog import SettingsDialog
from tray import MailProcessorTray, _resource_path
app = QApplication.instance()
owns_app = app is None
tray = None
dialog = None
menu = None
timer = None
result = 0
try:
if app is None:
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
app = QApplication([sys.argv[0]])
app.setQuitOnLastWindowClosed(False)
app.setApplicationName("MailProcessor")
app.setOrganizationName("lukisch")
# This flag is for a frozen test bundle only. It never loads or saves
# the real profile; all UI objects receive an empty synthetic config.
if not getattr(sys, "frozen", False) or not hasattr(sys, "_MEIPASS"):
result = 10
else:
icon_path = _resource_path("resources/icon.ico")
if not icon_path.is_file():
result = 11
else:
icon = QIcon(str(icon_path))
if icon.isNull():
result = 12
else:
app.setWindowIcon(icon)
cfg = AppConfig(
language="en", first_run=False, start_with_windows=False
)
set_language(cfg.language)
tray = MailProcessorTray(cfg)
menu = tray.contextMenu()
if tray.icon().isNull() or menu is None or len(menu.actions()) < 4:
result = 13
else:
dialog = SettingsDialog(cfg)
loop = QEventLoop()
timer = QTimer(dialog)
timer.setSingleShot(True)
painted = False
class PaintProbe(QObject):
def eventFilter(self, watched, event):
nonlocal painted
if watched is dialog and event.type() == QEvent.Type.Paint:
painted = True
loop.quit()
return False
paint_probe = PaintProbe(dialog)
dialog.installEventFilter(paint_probe)
dialog.show()
app.processEvents()
if not painted:
timer.timeout.connect(loop.quit)
timer.start(1500)
loop.exec()
timer.stop()
if not dialog.isVisible() or not painted:
result = 14
except Exception:
result = 15
finally:
if timer is not None:
try:
timer.stop()
except Exception:
if result == 0:
result = 16
if dialog is not None:
try:
dialog.close()
except Exception:
if result == 0:
result = 16
if menu is not None:
try:
menu.close()
except Exception:
if result == 0:
result = 16
if tray is not None:
try:
tray.hide()
except Exception:
if result == 0:
result = 16
if app is not None:
try:
app.processEvents()
except Exception:
if result == 0:
result = 16
if owns_app:
try:
app.quit()
except Exception:
if result == 0:
result = 16
return result
def main():
if sys.argv[1:] == ["--self-test"]:
return _run_bundle_self_test()
# High-DPI
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
app.setApplicationName("MailProcessor")
app.setOrganizationName("lukisch")
from tray import get_app_icon
app.setWindowIcon(get_app_icon())
cfg = cfg_module.load()
set_language(cfg.language)
if cfg.first_run:
from installer import InstallerWizard
wizard = InstallerWizard(cfg)
result = wizard.exec()
if result != InstallerWizard.DialogCode.Accepted:
sys.exit(0)
# Reload config (wizard saved it)
cfg = cfg_module.load()
set_language(cfg.language)
from settings_dialog import ensure_autostart_entry
ensure_autostart_entry(cfg.start_with_windows)
from tray import MailProcessorTray
tray = MailProcessorTray(cfg)
if not tray.isSystemTrayAvailable():
from PySide6.QtWidgets import QMessageBox
QMessageBox.critical(None, tr("app_name"), tr("tray_unavailable"))
sys.exit(1)
tray.show()
sys.exit(app.exec())
if __name__ == "__main__":
result = main()
if result is not None:
sys.exit(result)