Debian Codegraph @

Nagstamon/qui/dialogs/action.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 copy import deepcopy
19 from functools import wraps
20 from urllib.parse import quote
21
22 from Nagstamon.servers import SERVER_TYPES
23 from Nagstamon.config import (Action,
24 conf)
25 from Nagstamon.qui.dialogs.dialog import Dialog
26 from Nagstamon.qui.qt import (QMessageBox,
27 Qt,
28 Signal,
29 Slot)
30
31
32 class DialogAction(Dialog):
33 """
34 Dialog used to set up one single action
35 """
36
37 # signal to emit when ok button is pressed - used to update the list of actions
38 edited_update_list = Signal(str, str, str)
39
40 # mapping between action types and combobox content
41 ACTION_TYPES = {'browser': 'Browser',
42 'command': 'Command',
43 'url': 'URL'}
44
45 def __init__(self):
46 Dialog.__init__(self, 'settings_action')
47 # initial values
48 self.action_conf = None
49 self.mode = None
50 self.previous_action_conf = None
51 # define checkbox-to-widgets dependencies which apply at initialization
52 # which widgets have to be hidden because of irrelevance
53 # dictionary holds checkbox/radiobutton as key and relevant widgets in a list
54 self.TOGGLE_DEPS = {
55 self.window.input_checkbox_re_host_enabled: [self.window.input_lineedit_re_host_pattern,
56 self.window.input_checkbox_re_host_reverse],
57 self.window.input_checkbox_re_service_enabled: [self.window.input_lineedit_re_service_pattern,
58 self.window.input_checkbox_re_service_reverse],
59 self.window.input_checkbox_re_status_information_enabled: [
60 self.window.input_lineedit_re_status_information_pattern,
61 self.window.input_checkbox_re_status_information_reverse],
62 self.window.input_checkbox_re_duration_enabled: [self.window.input_lineedit_re_duration_pattern,
63 self.window.input_checkbox_re_duration_reverse],
64 self.window.input_checkbox_re_attempt_enabled: [self.window.input_lineedit_re_attempt_pattern,
65 self.window.input_checkbox_re_attempt_reverse],
66 self.window.input_checkbox_re_groups_enabled: [self.window.input_lineedit_re_groups_pattern,
67 self.window.input_checkbox_re_groups_reverse]}
68
69 # fill action types into combobox
70 self.window.input_combobox_type.addItems(sorted(self.ACTION_TYPES.values()))
71
72 # fill default order fields combobox with monitor server types
73 self.window.input_combobox_monitor_type.addItem("All monitor servers")
74 self.window.input_combobox_monitor_type.addItems(sorted(SERVER_TYPES.keys(), key=str.lower))
75 # default to Nagios as it is the mostly used monitor server
76 self.window.input_combobox_monitor_type.setCurrentIndex(0)
77
78 def dialog_decoration(method, *args):
79 """
80 try with a decorator instead of repeated calls
81 """
82
83 # the function which decorates method
84 # wraps is used to keep the original method's name and docstring
85 @wraps(method)
86 def decoration_function(self, *decorated_args):
87 """
88 self.server_conf has to be set by decorated method
89 """
90
91 # previous action conf only useful when editing - defaults to None
92 self.previous_action_conf = None
93
94 # call decorated method
95 method(self, *decorated_args)
96
97 # run through all input widgets and apply defaults from config
98 for widget in self.window.__dict__:
99 if widget.startswith('input_'):
100 if widget.startswith('input_checkbox_'):
101 setting = widget.split('input_checkbox_')[1]
102 self.window.__dict__[widget].setChecked(self.action_conf.__dict__[setting])
103 elif widget.startswith('input_radiobutton_'):
104 setting = widget.split('input_radiobutton_')[1]
105 self.window.__dict__[widget].setChecked(self.action_conf.__dict__[setting])
106 elif widget.startswith('input_lineedit_'):
107 setting = widget.split('input_lineedit_')[1]
108 self.window.__dict__[widget].setText(self.action_conf.__dict__[setting])
109 elif widget.startswith('input_textedit_'):
110 setting = widget.split('input_textedit_')[1]
111 self.window.__dict__[widget].setText(self.action_conf.__dict__[setting])
112
113 # set comboboxes
114 self.window.input_combobox_type.setCurrentText(self.ACTION_TYPES[self.action_conf.type.lower()])
115 self.window.input_combobox_monitor_type.setCurrentText(self.action_conf.monitor_type)
116
117 # apply toggle-dependencies between checkboxes and certain widgets
118 self.toggle_toggles()
119
120 # important final size adjustment
121 self.window.adjustSize()
122
123 # if running on macOS with disabled dock icon, the dock icon might have to be made visible
124 # to make Nagstamon accept keyboard input
125 self.check_macos_dock_icon_fix_show.emit()
126
127 # force modality - using .open() still allows to switch to main settings dialog
128 self.window.exec()
129
130 # en reverse the dock icon might be hidden again after a potential keyboard input
131 self.check_macos_dock_icon_fix_hide.emit()
132
133 # give back decorated function
134 return decoration_function
135
136 @Slot()
137 @dialog_decoration
138 def new(self):
139 """
140 create new server
141 """
142 self.mode = 'new'
143
144 # create a new server config object
145 self.action_conf = Action()
146 # window title might be pretty simple
147 self.window.setWindowTitle('New action')
148
149 @Slot(str)
150 @dialog_decoration
151 def edit(self, name):
152 """
153 edit existing action
154 """
155 self.mode = 'edit'
156 # shorter action conf
157 self.action_conf = conf.actions[name]
158 # store action name in case it will be changed
159 self.previous_action_conf = deepcopy(self.action_conf)
160 # set window title
161 self.window.setWindowTitle(f'Edit {self.action_conf.name}')
162
163 @Slot(str)
164 @dialog_decoration
165 def copy(self, name):
166 """
167 copy existing action
168 """
169 self.mode = 'copy'
170 # shorter action conf
171 self.action_conf = deepcopy(conf.actions[name])
172 # set window title before name change to reflect copy
173 self.window.setWindowTitle(f'Copy {self.action_conf.name}')
174 # indicate copy of other action
175 self.action_conf.name = f'Copy of {self.action_conf.name}'
176
177 def ok(self):
178 """
179 evaluate the state of widgets to get new configuration
180 """
181 # check that no duplicate name exists
182 if self.window.input_lineedit_name.text() in conf.actions and \
183 (self.mode in ['new', 'copy'] or
184 self.mode == 'edit' and self.action_conf != conf.actions[self.window.input_lineedit_name.text()]):
185 # cry if duplicate name exists
186 QMessageBox.critical(self.window, 'Nagstamon',
187 f'The action name <b>{self.window.input_lineedit_name.text()}</b> is already used.',
188 QMessageBox.StandardButton.Ok)
189 else:
190 # get configuration from UI
191 for widget in self.window.__dict__:
192 if widget.startswith('input_'):
193 if widget.startswith('input_checkbox_'):
194 setting = widget.split('input_checkbox_')[1]
195 self.action_conf.__dict__[setting] = self.window.__dict__[widget].isChecked()
196 if widget.startswith('input_radiobutton_'):
197 setting = widget.split('input_radiobutton_')[1]
198 self.action_conf.__dict__[setting] = self.window.__dict__[widget].isChecked()
199 elif widget.startswith('input_combobox_'):
200 setting = widget.split('input_combobox_')[1]
201 self.action_conf.__dict__[setting] = self.window.__dict__[widget].currentText()
202 elif widget.startswith('input_lineedit_'):
203 setting = widget.split('input_lineedit_')[1]
204 self.action_conf.__dict__[setting] = self.window.__dict__[widget].text()
205 elif widget.startswith('input_textedit_'):
206 setting = widget.split('input_textedit_')[1]
207 self.action_conf.__dict__[setting] = self.window.__dict__[widget].toPlainText()
208
209 # edited action will be deleted and recreated with new configuration
210 if self.mode == 'edit':
211 # delete previous name
212 conf.actions.pop(self.previous_action_conf.name)
213
214 # Avoid the wrong monitor type which blocks display of action
215 if self.action_conf.monitor_type not in SERVER_TYPES:
216 self.action_conf.monitor_type = ''
217
218 # lower type to recognize action type on monitor
219 self.action_conf.type = self.action_conf.type.lower()
220
221 # add edited or new/copied action
222 conf.actions[self.action_conf.name] = self.action_conf
223
224 # refresh list of actions, give call the current action name to highlight it
225 self.edited_update_list.emit('list_actions', 'actions', self.action_conf.name)
226
227 # delete the old action .conf file to reflect name changes
228 # new one will be written soon
229 if self.previous_action_conf is not None:
230 conf.delete_file('actions', 'action_{0}.conf'.format(quote(self.previous_action_conf.name, safe='')))
231
232 # store server settings
233 conf.save_multiple_config('actions', 'action')
234
235 # call close and macOS dock icon treatment from ancestor
236 super().ok()