| 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 conf |
| 19 |
from Nagstamon.qui.dialogs.dialog import Dialog |
| 20 |
from Nagstamon.qui.qt import (Qt, |
| 21 |
Signal, |
| 22 |
Slot) |
| 23 |
from Nagstamon.servers import servers |
| 24 |
|
| 25 |
class DialogAuthentication(Dialog): |
| 26 |
""" |
| 27 |
dialog for authentication |
| 28 |
""" |
| 29 |
# signal for telling server_vbox label to update |
| 30 |
update = Signal(str) |
| 31 |
|
| 32 |
# signal to tell the world that the authentication dialog will show up |
| 33 |
show_up = Signal() |
| 34 |
|
| 35 |
def __init__(self): |
| 36 |
Dialog.__init__(self, 'dialog_authentication') |
| 37 |
# store server |
| 38 |
self.server = None |
| 39 |
|
| 40 |
def initialize(self): |
| 41 |
""" |
| 42 |
setup dialog fitting to server |
| 43 |
""" |
| 44 |
if self.server is not None: |
| 45 |
|
| 46 |
self.window.setWindowTitle(f'Authenticate {self.server.name}') |
| 47 |
if self.server.type in ['Centreon', 'Thruk']: |
| 48 |
self.window.input_checkbox_use_autologin.show() |
| 49 |
self.window.input_lineedit_autologin_key.show() |
| 50 |
self.window.input_lineedit_autologin_key.show() |
| 51 |
self.window.label_autologin_key.show() |
| 52 |
# enable switching autologin key and password |
| 53 |
self.window.input_checkbox_use_autologin.clicked.connect(self.toggle_autologin) |
| 54 |
self.window.input_checkbox_use_autologin.setChecked(self.server.use_autologin) |
| 55 |
self.window.input_lineedit_autologin_key.setText(self.server.autologin_key) |
| 56 |
# initialize autologin |
| 57 |
self.toggle_autologin() |
| 58 |
else: |
| 59 |
self.window.input_checkbox_use_autologin.hide() |
| 60 |
self.window.input_lineedit_autologin_key.hide() |
| 61 |
self.window.label_autologin_key.hide() |
| 62 |
|
| 63 |
# set existing values |
| 64 |
self.window.input_lineedit_username.setText(self.server.username) |
| 65 |
self.window.input_lineedit_password.setText(self.server.password) |
| 66 |
self.window.input_checkbox_save_password.setChecked(conf.servers[self.server.name].save_password) |
| 67 |
|
| 68 |
@Slot(str) |
| 69 |
def show_auth_dialog(self, server): |
| 70 |
""" |
| 71 |
initialize and show authentication dialog |
| 72 |
""" |
| 73 |
self.server = servers[server] |
| 74 |
self.initialize() |
| 75 |
self.show_up.emit() |
| 76 |
self.window.adjustSize() |
| 77 |
|
| 78 |
# the dock icon might be needed to be shown for a potential keyboard input |
| 79 |
self.check_macos_dock_icon_fix_show.emit() |
| 80 |
|
| 81 |
self.window.exec() |
| 82 |
|
| 83 |
# en reverse the dock icon might be hidden again after a potential keyboard input |
| 84 |
self.check_macos_dock_icon_fix_hide.emit() |
| 85 |
|
| 86 |
def ok(self): |
| 87 |
""" |
| 88 |
take username and password |
| 89 |
""" |
| 90 |
|
| 91 |
# close window fist to avoid lagging UI |
| 92 |
self.window.close() |
| 93 |
|
| 94 |
self.server.username = self.window.input_lineedit_username.text() |
| 95 |
self.server.password = self.window.input_lineedit_password.text() |
| 96 |
self.server.refresh_authentication = False |
| 97 |
|
| 98 |
# store password if it should be saved |
| 99 |
if self.window.input_checkbox_save_password.isChecked(): |
| 100 |
conf.servers[self.server.name].username = self.server.username |
| 101 |
conf.servers[self.server.name].password = self.server.password |
| 102 |
conf.servers[self.server.name].save_password = self.window.input_checkbox_save_password.isChecked() |
| 103 |
# store server settings |
| 104 |
conf.save_multiple_config('servers', 'server') |
| 105 |
|
| 106 |
# Centreon |
| 107 |
if self.server.type in ['Centreon', 'Thruk']: |
| 108 |
if self.window.input_checkbox_use_autologin: |
| 109 |
conf.servers[self.server.name].use_autologin = self.window.input_checkbox_use_autologin.isChecked() |
| 110 |
conf.servers[self.server.name].autologin_key = self.window.input_lineedit_autologin_key.text() |
| 111 |
# store server settings |
| 112 |
conf.save_multiple_config('servers', 'server') |
| 113 |
|
| 114 |
# reset server connection |
| 115 |
self.server.reset_http() |
| 116 |
|
| 117 |
# force server to recheck right now |
| 118 |
self.server.thread_counter = conf.update_interval_seconds |
| 119 |
|
| 120 |
# update server_vbox label |
| 121 |
self.update.emit(self.server.name) |
| 122 |
|
| 123 |
# call close and macOS dock icon treatment from ancestor |
| 124 |
super().ok() |
| 125 |
|
| 126 |
@Slot() |
| 127 |
def toggle_autologin(self): |
| 128 |
""" |
| 129 |
toggle autologin option for Centreon |
| 130 |
""" |
| 131 |
if self.window.input_checkbox_use_autologin.isChecked(): |
| 132 |
self.window.label_username.hide() |
| 133 |
self.window.label_password.hide() |
| 134 |
self.window.input_lineedit_username.hide() |
| 135 |
self.window.input_lineedit_password.hide() |
| 136 |
self.window.input_checkbox_save_password.hide() |
| 137 |
|
| 138 |
self.window.label_autologin_key.show() |
| 139 |
self.window.input_lineedit_autologin_key.show() |
| 140 |
else: |
| 141 |
self.window.label_username.show() |
| 142 |
self.window.label_password.show() |
| 143 |
self.window.input_lineedit_username.show() |
| 144 |
self.window.input_lineedit_password.show() |
| 145 |
self.window.input_checkbox_save_password.show() |
| 146 |
|
| 147 |
self.window.label_autologin_key.hide() |
| 148 |
self.window.input_lineedit_autologin_key.hide() |
| 149 |
|
| 150 |
# adjust dialog window size after UI changes |
| 151 |
self.window.adjustSize() |