Debian Codegraph @

Nagstamon/qui/widgets/server_vbox.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 from Nagstamon.config import (conf,
18 OS,
19 OS_MACOS)
20 from Nagstamon.qui.constants import (HEADERS,
21 HEADERS_KEYS_COLUMNS,
22 HEADERS_HEADERS_COLUMNS,
23 SORT_ORDER,
24 SPACE)
25 from Nagstamon.qui.qt import (QPushButton,
26 QSizePolicy,
27 Qt,
28 QVBoxLayout,
29 Signal,
30 Slot)
31 from Nagstamon.qui.widgets.buttons import (Button,
32 PushButtonBrowserURL)
33 from Nagstamon.qui.widgets.labels import (ClosingLabel,
34 ServerStatusLabel)
35 from Nagstamon.qui.widgets.layout import HBoxLayout
36 from Nagstamon.qui.widgets.treeview import TreeView
37
38
39 class ServerVBox(QVBoxLayout):
40 """
41 one VBox per server containing buttons and hosts/services listview
42 """
43 # used to update status label text like 'Connected-'
44 change_label_status = Signal(str, str)
45
46 # signal to submit server to authentication dialog for credentials
47 authenticate_credentials = Signal(str)
48
49 # signal to submit server to weblogin dialog as browser
50 authenticate_weblogin = Signal(str)
51
52 # handle TLS error button
53 button_fix_tls_error_show = Signal()
54 button_fix_tls_error_hide = Signal()
55
56 # buttons pressed, which need dialogs
57 button_edit_pressed = Signal(str)
58 button_fix_tls_error_pressed = Signal(str, bool)
59
60 # open dialog - may need closing the statuswindow
61 open_dialog = Signal()
62
63 def __init__(self, server, parent=None):
64 QVBoxLayout.__init__(self, parent)
65
66 self.parent_statuswindow = parent
67
68 # no space around
69 self.setSpacing(0)
70 self.setContentsMargins(0, 0, 0, 0)
71
72 # server the vbox belongs to
73 self.server = server
74
75 # header containing monitor name, buttons and status
76 self.header = HBoxLayout(spacing=SPACE, parent=parent)
77 self.addLayout(self.header)
78 # top and bottom should be kept by padding
79 self.header.setContentsMargins(0, 0, SPACE, 0)
80
81 self.label = ClosingLabel(parent=parent)
82 self.label.mouse_released.connect(self.parent_statuswindow.hide_window)
83 self.update_label()
84 self.button_monitor = PushButtonBrowserURL(text='Monitor',
85 parent=parent,
86 server=self.server,
87 url_type='monitor')
88 self.button_monitor.webbrowser_opened.connect(self.parent_statuswindow.hide_window)
89 self.button_hosts = PushButtonBrowserURL(text='Hosts',
90 parent=parent,
91 server=self.server,
92 url_type='hosts')
93 self.button_hosts.webbrowser_opened.connect(self.parent_statuswindow.hide_window)
94 self.button_services = PushButtonBrowserURL(text='Services',
95 parent=parent,
96 server=self.server,
97 url_type='services')
98 self.button_services.webbrowser_opened.connect(self.parent_statuswindow.hide_window)
99 self.button_history = PushButtonBrowserURL(text='History',
100 parent=parent,
101 server=self.server,
102 url_type='history')
103 self.button_history.webbrowser_opened.connect(self.parent_statuswindow.hide_window)
104 self.button_edit = Button('Edit',
105 parent=parent)
106
107 # use label instead of spacer to be clickable
108 self.label_stretcher = ClosingLabel('', parent=parent)
109 self.label_stretcher.setSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.Expanding)
110 self.label_stretcher.mouse_released.connect(self.parent_statuswindow.hide_window)
111
112 self.label_status = ServerStatusLabel(parent=parent)
113 self.label_status.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
114
115 self.button_authenticate = QPushButton('Authenticate', parent=parent)
116
117 self.button_fix_tls_error = QPushButton('Fix error', parent=parent)
118
119 # avoid useless spaces in macOS when server has nothing to show
120 # see https://bugreports.qt.io/browse/QTBUG-2699
121 self.button_monitor.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
122 self.button_history.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
123 self.button_services.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
124 self.button_hosts.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
125 self.button_edit.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
126 self.button_authenticate.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
127 self.button_fix_tls_error.setAttribute(Qt.WidgetAttribute.WA_LayoutUsesWidgetRect)
128
129 self.button_monitor.clicked.connect(self.button_monitor.open_url)
130 self.button_hosts.clicked.connect(self.button_hosts.open_url)
131 self.button_services.clicked.connect(self.button_services.open_url)
132 self.button_history.clicked.connect(self.button_history.open_url)
133 self.button_edit.clicked.connect(self.edit_server)
134
135 self.header.addWidget(self.label)
136 self.header.addWidget(self.button_monitor)
137 self.header.addWidget(self.button_hosts)
138 self.header.addWidget(self.button_services)
139 self.header.addWidget(self.button_history)
140 self.header.addWidget(self.button_edit)
141
142 self.header.addWidget(self.label_stretcher)
143
144 self.header.addWidget(self.label_status)
145 self.header.addWidget(self.button_authenticate)
146 self.header.addWidget(self.button_fix_tls_error)
147
148 self.open_dialog.connect(self.parent_statuswindow.hide_window)
149
150 # attempt to get header strings
151 try:
152 # when stored as simple lowercase keys
153 sort_column = HEADERS_KEYS_COLUMNS[conf.default_sort_field]
154 except Exception:
155 # when as legacy stored as presentation string
156 sort_column = HEADERS_HEADERS_COLUMNS[conf.default_sort_field]
157
158 # convert sort order to number as used in Qt.SortOrder
159 sort_order = SORT_ORDER[conf.default_sort_order.lower()]
160
161 self.table = TreeView(len(HEADERS) + 1, 0, sort_column, sort_order, self.server, parent=parent)
162
163 # delete vbox if thread quits
164 self.table.worker_thread.finished.connect(self.delete)
165
166 # connect worker to status label to reflect connectivity
167 self.table.worker.change_label_status.connect(self.label_status.change)
168 self.table.worker.restore_label_status.connect(self.label_status.restore)
169 self.table.worker.authentication_needed.connect(self.parent_statuswindow.show_window_for_authentication)
170
171 # care about authentications
172 self.button_authenticate.clicked.connect(self.authenticate_server)
173 # somehow a long way to connect the signal with the slot but works
174 self.authenticate_credentials.connect(self.parent_statuswindow.injected_dialogs.authentication.show_auth_dialog)
175 self.authenticate_weblogin.connect(self.parent_statuswindow.injected_dialogs.weblogin.show_browser)
176 self.parent_statuswindow.injected_dialogs.authentication.update.connect(self.update_label)
177
178 # start ignoring TLS trouble when button clicked
179 self.button_fix_tls_error.clicked.connect(self.fix_tls_error)
180
181 # connect button signals to dialogs
182 self.button_edit_pressed.connect(self.parent_statuswindow.injected_dialogs.server.edit)
183 self.button_fix_tls_error_pressed.connect(self.parent_statuswindow.injected_dialogs.server.edit)
184
185 self.addWidget(self.table, 1)
186
187 # as default do not show anything
188 self.show_only_header()
189
190 def get_real_height(self):
191 """
192 return summarized real height of hbox items and table
193 """
194 height = self.table.get_real_height()
195 if self.label.isVisible() and self.button_monitor.isVisible():
196 # compare item heights, decide to take the largest and add 2 time the MARGIN (top and bottom)
197 if self.label.sizeHint().height() > self.button_monitor.sizeHint().height():
198 height += self.label.sizeHint().height() + 2
199 else:
200 height += self.button_monitor.sizeHint().height() + 2
201 return height
202
203 @Slot()
204 def show_all(self):
205 """
206 show all items in server vbox
207 """
208 self.button_authenticate.hide()
209 self.button_edit.show()
210 self.button_fix_tls_error.hide()
211 self.button_history.show()
212 self.button_hosts.show()
213 self.button_monitor.show()
214 self.button_services.show()
215 self.label.show()
216 self.label_status.show()
217 self.label_stretcher.show()
218 # special table treatment
219 self.table.show()
220 # self.table.is_shown = True
221
222 @Slot()
223 def show_only_header(self):
224 """
225 show all items in server vbox except the table - not needed if empty or major connection problem
226 """
227 self.button_authenticate.hide()
228 self.button_edit.show()
229 self.button_history.show()
230 self.button_hosts.show()
231 self.button_fix_tls_error.hide()
232 self.button_monitor.show()
233 self.button_services.show()
234 self.label.show()
235 self.label_status.show()
236 self.label_stretcher.show()
237 # special table treatment
238 self.table.hide()
239
240 @Slot()
241 def hide_all(self):
242 """
243 hide all items in server vbox
244 """
245 self.button_authenticate.hide()
246 self.button_edit.hide()
247 self.button_fix_tls_error.hide()
248 self.button_history.hide()
249 self.button_hosts.hide()
250 self.button_monitor.hide()
251 self.button_services.hide()
252 self.label.hide()
253 self.label_status.hide()
254 self.label_stretcher.hide()
255 # special table treatment
256 self.table.hide()
257
258 @Slot()
259 def delete(self):
260 """
261 delete VBox and its children
262 """
263 for widget in (self.label,
264 self.button_monitor,
265 self.button_hosts,
266 self.button_services,
267 self.button_history,
268 self.button_edit,
269 self.label_status,
270 self.label_stretcher,
271 self.button_authenticate,
272 self.button_fix_tls_error):
273 widget.hide()
274 widget.deleteLater()
275 self.removeItem(self.header)
276 self.header.deleteLater()
277 self.table.hide()
278 self.table.deleteLater()
279 self.deleteLater()
280
281 def edit_server(self):
282 """
283 call dialogs.server.edit() with server name
284 """
285 if not conf.fullscreen and not conf.windowed:
286 self.open_dialog.emit()
287 self.button_edit_pressed.emit(self.server.name)
288
289 def authenticate_server(self):
290 """
291 send signal to open authentication dialog with self.server.name
292 """
293 if self.server.authentication != 'web':
294 self.authenticate_credentials.emit(self.server.name)
295 else:
296 self.authenticate_weblogin.emit(self.server.name)
297
298 @Slot()
299 def update_label(self):
300 if self.server.authentication != 'web':
301 self.label.setText(f'<big><b>&nbsp;{self.server.username}@{self.server.name}</b></big>')
302 else:
303 self.label.setText(f'<big><b>&nbsp;{self.server.name}</b></big>')
304 # let label padding keep top and bottom space - apparently not necessary on OSX
305 if OS != OS_MACOS:
306 self.label.setStyleSheet('''padding-top: {0}px;
307 padding-bottom: {0}px;'''.format(SPACE))
308
309 @Slot()
310 def fix_tls_error(self):
311 """
312 call dialogs.server.edit() with server name and showing extra options
313 """
314 if not conf.fullscreen and not conf.windowed:
315 self.open_dialog.emit()
316 self.button_fix_tls_error_pressed.emit(self.server.name, True)