-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
177 lines (151 loc) · 5.45 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
import logging
import os
import sys
import time
from http.server import HTTPServer
from threading import Thread as PyThread
import darkdetect
from PySide6.QtCore import QThreadPool
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication
from qfluentwidgets import InfoBar, setTheme, Theme
from data import resource
from nlightreader import ParentWindow
from nlightreader.consts.app import APP_BRANCH, APP_NAME, APP_VERSION
from nlightreader.consts.files import Icons
from nlightreader.consts.paths import APP_DATA_PATH
from nlightreader.consts.urls import GITHUB_REPO
from nlightreader.utils.config import cfg
from nlightreader.utils.kodik_server import KodikHTTPRequestHandler
from nlightreader.utils.threads import Thread
from nlightreader.utils.translator import NlightTranslator, translate
from nlightreader.utils.utils import get_html
class App(QApplication):
def __init__(self, argv):
super().__init__(argv)
self.setApplicationDisplayName(APP_NAME)
self.setApplicationVersion(APP_VERSION)
self.setWindowIcon(QIcon(Icons.App))
self.translator = NlightTranslator()
self.load_translator()
self.update_style()
def load_translator(self):
locale = cfg.get(cfg.language).value
self.translator.load(locale)
self.installTranslator(self.translator)
def update_style(self):
if (theme_mode := cfg.get(cfg.theme_mode)) == "Auto":
setTheme(Theme.DARK if darkdetect.isDark() else Theme.LIGHT)
else:
setTheme(Theme.DARK if theme_mode == "Dark" else Theme.LIGHT)
class MainWindow(ParentWindow):
def __init__(self):
super().__init__()
self.set_min_size_by_screen()
self.setWindowTitle(APP_NAME)
self.setWindowIcon(QIcon(Icons.App))
self._theme_updater = Thread(
target=self.theme_listener,
callback=self.update_style,
)
self._update_checker = Thread(
target=self.check_for_updates,
callback=self.show_update_info,
error_callback=lambda: self.show_update_info(None),
)
self.settings_interface.check_for_updates_signal.connect(
self.start_check_for_updates_thread,
)
self.settings_interface.theme_changed.connect(
app.update_style,
)
self._theme_updater.start()
if cfg.get(cfg.check_updates_at_startup):
self.start_check_for_updates_thread()
def start_check_for_updates_thread(self):
self._update_checker.terminate()
self._update_checker.wait()
self._update_checker.start()
def check_for_updates(self) -> str | None:
response = get_html(
f"{GITHUB_REPO}/releases",
params={"per_page": 2},
content_type="json",
)
if not response:
return None
latest_version = None
for release in response:
version = release["tag_name"]
if APP_BRANCH in version:
latest_version = version
return latest_version
def show_update_info(self, result):
info_bar_title = translate(
"Message",
"Check for updates.",
)
info_bar_duration = 3500
if result is None:
InfoBar.error(
title=info_bar_title,
content=translate(
"Message",
"Error checking for updates.",
),
duration=info_bar_duration,
parent=self,
)
elif result != APP_VERSION:
InfoBar.info(
title=info_bar_title,
content=translate(
"Message",
"New version {result} is available! "
"You are currently on version {APP_VERSION}.",
).format(result=result, APP_VERSION=APP_VERSION),
duration=info_bar_duration,
parent=self,
)
else:
InfoBar.success(
title=info_bar_title,
content=translate(
"Message",
"No updates available. You are using the latest version.",
),
duration=info_bar_duration,
parent=self,
)
@staticmethod
def theme_listener():
theme = darkdetect.theme()
while darkdetect.theme() == theme or cfg.get(cfg.theme_mode) != "Auto":
time.sleep(1)
def update_style(self):
app.update_style()
self._theme_updater.start()
def closeEvent(self, event):
super().closeEvent(event)
self._theme_updater.terminate()
self._theme_updater.wait()
app.closeAllWindows()
if __name__ == "__main__":
if "debug" in sys.argv:
logging.basicConfig(
level=logging.WARNING,
filename="latest.log",
filemode="w",
)
QThreadPool.globalInstance().setMaxThreadCount(32)
if cfg.get(cfg.dpi_scale) != "Auto":
os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpi_scale))
app = App(sys.argv)
APP_DATA_PATH.mkdir(parents=True, exist_ok=True)
if cfg.get(cfg.enable_kodik_server):
httpd = HTTPServer(("localhost", 8000), KodikHTTPRequestHandler)
PyThread(target=httpd.serve_forever, daemon=True).start()
window = MainWindow()
window.show()
sys.exit(app.exec())