Debian Codegraph @

Nagstamon/qui/dialogs/check_version.py

raw
1 # Nagstamon - Nagios status monitor for your desktop
2 # Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
18 from Nagstamon.config import AppInfo
19 from Nagstamon.qui.qt import (QMessageBox,
20 QObject,
21 Qt,
22 QThread,
23 QWidget,
24 Signal,
25 Slot)
26 from Nagstamon.servers import get_enabled_servers
27
28
29 class CheckVersion(QObject):
30 """
31 checking for updates
32 """
33
34 is_checking = False
35
36 version_info_retrieved = Signal()
37
38 @Slot(bool, QWidget)
39 def check(self, start_mode=False, parent=None):
40
41 if not self.is_checking:
42
43 # lock checking thread
44 self.is_checking = True
45
46 # list of enabled servers which connections outside should be used to check
47 self.enabled_servers = get_enabled_servers()
48
49 # set mode to be evaluated by worker
50 self.start_mode = start_mode
51
52 # store caller of dialog window - not if at start because this will disturb EWMH
53 if start_mode:
54 self.parent = None
55 else:
56 self.parent = parent
57
58 # thread for worker to avoid
59 self.worker_thread = QThread(parent=self)
60 self.worker = self.Worker(start_mode)
61
62 # if update check is ready it sends the message to GUI thread
63 self.worker.ready.connect(self.show_message)
64
65 # stop thread if worker has finished
66 self.worker.finished.connect(self.worker_thread.quit)
67 # reset checking lock if finished
68 self.worker.finished.connect(self.reset_checking)
69
70 self.worker.moveToThread(self.worker_thread)
71 # run check when thread starts
72 self.worker_thread.started.connect(self.worker.check)
73 self.worker_thread.start(QThread.Priority.LowestPriority)
74
75 @Slot()
76 def reset_checking(self):
77 """
78 reset checking the flag to avoid QThread crashes
79 """
80 self.is_checking = False
81
82 @Slot(str)
83 def show_message(self, message):
84 """
85 message dialog must be shown from GUI thread
86 """
87 self.version_info_retrieved.emit()
88
89 # attempt to solve https://github.com/HenriWahl/Nagstamon/issues/303
90 # might be working this time
91 parent = self.parent
92
93 messagebox = QMessageBox(QMessageBox.Icon.Information,
94 'Nagstamon version check',
95 message,
96 QMessageBox.StandardButton.Ok,
97 parent,
98 Qt.WindowType.Dialog | Qt.WindowType.MSWindowsFixedSizeDialogHint)
99 messagebox.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
100 messagebox.setWindowModality(Qt.WindowModality.NonModal)
101 messagebox.open()
102
103 class Worker(QObject):
104
105 """
106 check for a new version in the background
107 """
108 # send signal if some version information is available
109 ready = Signal(str)
110
111 finished = Signal()
112
113 def __init__(self, start_mode=False):
114 QObject.__init__(self)
115 self.start_mode = start_mode
116
117 def check(self):
118 """
119 check for update using server connection
120 """
121 # get servers to be used for checking the version
122 enabled_servers = get_enabled_servers()
123
124 # default latest version is 'unavailable' and message empty
125 latest_version = 'unavailable'
126 message = ''
127
128 # find at least one server which allows getting version information
129 for server in enabled_servers:
130 for download_server, download_url in AppInfo.DOWNLOAD_SERVERS.items():
131 # dummy message just in case version check does not work
132 message = 'Cannot reach version check at <a href={0}>{0}</<a>.'.format(
133 f'https://{download_server}{AppInfo.VERSION_PATH}')
134
135 # retrieve VERSION_URL without auth information
136 response = server.fetch_url(f'https://{download_server}{AppInfo.VERSION_PATH}',
137 giveback='raw',
138 no_auth=True)
139
140 # stop searching the available download URLs
141 if response.error == '' and \
142 not response.result.startswith('<') and \
143 not '\n' in response.result and \
144 5 < len(response.result) < 15 and \
145 response.result[0].isdigit():
146 latest_version = response.result.strip()
147 break
148
149 # ignore TLS error in case it was caused by requesting the latest version - not important for monitoring
150 server.tls_error = False
151
152 # stop searching via enabled servers
153 if response.error == '' and not response.result.startswith('<'):
154 latest_version = response.result.strip()
155 break
156
157 # compose a message according to version information
158 if latest_version != 'unavailable':
159 if latest_version == AppInfo.VERSION:
160 message = f'You are using the latest version <b>Nagstamon {AppInfo.VERSION}</b>.'
161 # avoid GitHub HTML being evaluated as version number -> checking for length
162 elif latest_version > AppInfo.VERSION and not len(latest_version) > 20:
163 message = f'The new version <b>Nagstamon {latest_version}</b> is available.<p>' \
164 f'Get it at <a href={AppInfo.WEBSITE}/download>{AppInfo.WEBSITE}/download</a>.'
165 elif latest_version < AppInfo.VERSION:
166 # for some reason, the local version is newer than that remote one - just ignore
167 message = ''
168
169 # check if there is anything to tell
170 if message != '':
171 # if run from startup do not cry if any error occurred or nothing new is available
172 if self.start_mode is False or \
173 (self.start_mode is True and latest_version not in ('unavailable', AppInfo.VERSION)):
174 self.ready.emit(message)
175
176 # tell thread to finish
177 self.finished.emit()
178
179
180 # initialized an object to be used in other modules
181 check_version = CheckVersion()