[53e7d45] | 1 | #!/usr/bin/env python3 |
---|
[11f7a07] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | # |
---|
| 4 | # Copyright (c) 2014 Virtual Cable S.L. |
---|
| 5 | # All rights reserved. |
---|
| 6 | # |
---|
| 7 | # Redistribution and use in source and binary forms, with or without modification, |
---|
| 8 | # are permitted provided that the following conditions are met: |
---|
| 9 | # |
---|
| 10 | # * Redistributions of source code must retain the above copyright notice, |
---|
| 11 | # this list of conditions and the following disclaimer. |
---|
| 12 | # * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 13 | # this list of conditions and the following disclaimer in the documentation |
---|
| 14 | # and/or other materials provided with the distribution. |
---|
| 15 | # * Neither the name of Virtual Cable S.L. nor the names of its contributors |
---|
| 16 | # may be used to endorse or promote products derived from this software |
---|
| 17 | # without specific prior written permission. |
---|
| 18 | # |
---|
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 20 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 21 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 22 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
| 23 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 24 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
| 25 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
| 26 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
| 27 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
[815ea50] | 29 | """ |
---|
[11f7a07] | 30 | @author: Adolfo Gómez, dkmaster at dkmon dot com |
---|
[815ea50] | 31 | """ |
---|
[53e7d45] | 32 | import atexit |
---|
| 33 | import base64 |
---|
[1528428] | 34 | import json |
---|
[11f7a07] | 35 | import sys |
---|
| 36 | import time |
---|
[10fab78] | 37 | import os |
---|
[b84ab33] | 38 | from PyQt6 import QtCore, QtGui, QtWidgets |
---|
[11f7a07] | 39 | |
---|
| 40 | from about_dialog_ui import Ui_OGAAboutDialog |
---|
| 41 | from message_dialog_ui import Ui_OGAMessageDialog |
---|
[53e7d45] | 42 | from opengnsys import VERSION, ipc, operations, utils |
---|
[11f7a07] | 43 | from opengnsys.config import readConfig |
---|
| 44 | from opengnsys.loader import loadModules |
---|
[53e7d45] | 45 | from opengnsys.log import logger |
---|
| 46 | from opengnsys.scriptThread import ScriptExecutorThread |
---|
| 47 | from opengnsys.service import IPC_PORT |
---|
[aac3fb9] | 48 | |
---|
[11f7a07] | 49 | trayIcon = None |
---|
| 50 | |
---|
[815ea50] | 51 | |
---|
[98fc98d] | 52 | def sigAtExit(): |
---|
[11f7a07] | 53 | if trayIcon: |
---|
| 54 | trayIcon.quit() |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | # About dialog |
---|
[53e7d45] | 58 | class OGAAboutDialog(QtWidgets.QDialog): |
---|
[11f7a07] | 59 | def __init__(self, parent=None): |
---|
[1528428] | 60 | QtWidgets.QDialog.__init__(self, parent) |
---|
[11f7a07] | 61 | self.ui = Ui_OGAAboutDialog() |
---|
| 62 | self.ui.setupUi(self) |
---|
| 63 | self.ui.VersionLabel.setText("Version " + VERSION) |
---|
| 64 | |
---|
| 65 | def closeDialog(self): |
---|
| 66 | self.hide() |
---|
| 67 | |
---|
| 68 | |
---|
[1528428] | 69 | class OGAMessageDialog(QtWidgets.QDialog): |
---|
[11f7a07] | 70 | def __init__(self, parent=None): |
---|
[1528428] | 71 | QtWidgets.QDialog.__init__(self, parent) |
---|
[11f7a07] | 72 | self.ui = Ui_OGAMessageDialog() |
---|
| 73 | self.ui.setupUi(self) |
---|
| 74 | |
---|
| 75 | def message(self, message): |
---|
| 76 | self.ui.message.setText(message) |
---|
| 77 | self.show() |
---|
| 78 | |
---|
| 79 | def closeDialog(self): |
---|
| 80 | self.hide() |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | class MessagesProcessor(QtCore.QThread): |
---|
| 84 | logoff = QtCore.pyqtSignal(name='logoff') |
---|
| 85 | message = QtCore.pyqtSignal(tuple, name='message') |
---|
[1528428] | 86 | script = QtCore.pyqtSignal(str, name='script') |
---|
[11f7a07] | 87 | exit = QtCore.pyqtSignal(name='exit') |
---|
| 88 | |
---|
| 89 | def __init__(self, port): |
---|
| 90 | super(self.__class__, self).__init__() |
---|
| 91 | # Retries connection for a while |
---|
| 92 | for _ in range(10): |
---|
| 93 | try: |
---|
| 94 | self.ipc = ipc.ClientIPC(port) |
---|
| 95 | self.ipc.start() |
---|
| 96 | break |
---|
| 97 | except Exception: |
---|
| 98 | logger.debug('IPC Server is not reachable') |
---|
| 99 | self.ipc = None |
---|
| 100 | time.sleep(2) |
---|
| 101 | |
---|
| 102 | self.running = False |
---|
| 103 | |
---|
| 104 | def stop(self): |
---|
| 105 | self.running = False |
---|
| 106 | if self.ipc: |
---|
| 107 | self.ipc.stop() |
---|
| 108 | |
---|
| 109 | def isAlive(self): |
---|
| 110 | return self.ipc is not None |
---|
| 111 | |
---|
[be263c6] | 112 | def sendLogin(self, user_data): |
---|
[11f7a07] | 113 | if self.ipc: |
---|
[be263c6] | 114 | self.ipc.sendLogin(user_data) |
---|
[11f7a07] | 115 | |
---|
[53e7d45] | 116 | def sendLogout(self, username): |
---|
[11f7a07] | 117 | if self.ipc: |
---|
[53e7d45] | 118 | self.ipc.sendLogout(username) |
---|
[11f7a07] | 119 | |
---|
| 120 | def run(self): |
---|
| 121 | if self.ipc is None: |
---|
| 122 | return |
---|
| 123 | self.running = True |
---|
| 124 | |
---|
| 125 | # Wait a bit so we ensure IPC thread is running... |
---|
| 126 | time.sleep(2) |
---|
| 127 | |
---|
| 128 | while self.running and self.ipc.running: |
---|
| 129 | try: |
---|
| 130 | msg = self.ipc.getMessage() |
---|
| 131 | if msg is None: |
---|
| 132 | break |
---|
[53e7d45] | 133 | msg_id, data = msg |
---|
[bb685d9] | 134 | logger.debug('Got Message on User Space: {}:{}'.format(msg_id, data)) |
---|
[53e7d45] | 135 | if msg_id == ipc.MSG_MESSAGE: |
---|
[64c933f] | 136 | module, message, data = data.decode('utf-8').split('\0') |
---|
[11f7a07] | 137 | self.message.emit((module, message, data)) |
---|
[53e7d45] | 138 | elif msg_id == ipc.MSG_LOGOFF: |
---|
[11f7a07] | 139 | self.logoff.emit() |
---|
[53e7d45] | 140 | elif msg_id == ipc.MSG_SCRIPT: |
---|
| 141 | self.script.emit(data.decode('utf-8')) |
---|
[11f7a07] | 142 | except Exception as e: |
---|
[64c933f] | 143 | logger.error('Got error on IPC thread {}'.format(utils.exceptionToMessage(e))) |
---|
[11f7a07] | 144 | |
---|
| 145 | if self.ipc.running is False and self.running is True: |
---|
| 146 | logger.warn('Lost connection with Service, closing program') |
---|
| 147 | |
---|
| 148 | self.exit.emit() |
---|
| 149 | |
---|
| 150 | |
---|
[1528428] | 151 | class OGASystemTray(QtWidgets.QSystemTrayIcon): |
---|
[11f7a07] | 152 | def __init__(self, app_, parent=None): |
---|
| 153 | self.app = app_ |
---|
| 154 | self.config = readConfig(client=True) |
---|
[53e7d45] | 155 | self.modules = None |
---|
[98fc98d] | 156 | # Get opengnsys section as dict |
---|
[11f7a07] | 157 | cfg = dict(self.config.items('opengnsys')) |
---|
| 158 | # Set up log level |
---|
| 159 | logger.setLevel(cfg.get('log', 'INFO')) |
---|
[98fc98d] | 160 | |
---|
[11f7a07] | 161 | self.ipcport = int(cfg.get('ipc_port', IPC_PORT)) |
---|
[98fc98d] | 162 | |
---|
[10fab78] | 163 | QtCore.QDir.addSearchPath('images', os.path.join(os.path.dirname(__file__), 'img')) |
---|
| 164 | icon = QtGui.QIcon('images:oga.png') |
---|
[11f7a07] | 165 | |
---|
[1528428] | 166 | QtWidgets.QSystemTrayIcon.__init__(self, icon, parent) |
---|
| 167 | self.menu = QtWidgets.QMenu(parent) |
---|
| 168 | exit_action = self.menu.addAction("About") |
---|
| 169 | exit_action.triggered.connect(self.about) |
---|
[11f7a07] | 170 | self.setContextMenu(self.menu) |
---|
| 171 | self.ipc = MessagesProcessor(self.ipcport) |
---|
[98fc98d] | 172 | |
---|
[11f7a07] | 173 | if self.ipc.isAlive() is False: |
---|
| 174 | raise Exception('No connection to service, exiting.') |
---|
[98fc98d] | 175 | |
---|
[11f7a07] | 176 | self.timer = QtCore.QTimer() |
---|
| 177 | self.timer.timeout.connect(self.timerFnc) |
---|
| 178 | |
---|
| 179 | self.stopped = False |
---|
| 180 | |
---|
| 181 | self.ipc.message.connect(self.message) |
---|
| 182 | self.ipc.exit.connect(self.quit) |
---|
| 183 | self.ipc.script.connect(self.executeScript) |
---|
| 184 | self.ipc.logoff.connect(self.logoff) |
---|
| 185 | |
---|
| 186 | self.aboutDlg = OGAAboutDialog() |
---|
| 187 | self.msgDlg = OGAMessageDialog() |
---|
| 188 | |
---|
| 189 | self.timer.start(1000) # Launch idle checking every 1 seconds |
---|
| 190 | |
---|
| 191 | self.ipc.start() |
---|
[98fc98d] | 192 | |
---|
[11f7a07] | 193 | def initialize(self): |
---|
| 194 | # Load modules and activate them |
---|
| 195 | # Also, sends "login" event to service |
---|
| 196 | self.modules = loadModules(self, client=True) |
---|
| 197 | logger.debug('Modules: {}'.format(list(v.name for v in self.modules))) |
---|
[98fc98d] | 198 | |
---|
[11f7a07] | 199 | # Send init to all modules |
---|
[53e7d45] | 200 | valid_mods = [] |
---|
[11f7a07] | 201 | for mod in self.modules: |
---|
| 202 | try: |
---|
| 203 | logger.debug('Activating module {}'.format(mod.name)) |
---|
| 204 | mod.activate() |
---|
[53e7d45] | 205 | valid_mods.append(mod) |
---|
[11f7a07] | 206 | except Exception as e: |
---|
| 207 | logger.exception() |
---|
| 208 | logger.error("Activation of {} failed: {}".format(mod.name, utils.exceptionToMessage(e))) |
---|
[53e7d45] | 209 | self.modules[:] = valid_mods # copy instead of assignment |
---|
[11f7a07] | 210 | # If this is running, it's because he have logged in, inform service of this fact |
---|
[be263c6] | 211 | self.ipc.sendLogin((operations.getCurrentUser(), operations.getSessionLanguage(), |
---|
| 212 | operations.get_session_type())) |
---|
[11f7a07] | 213 | |
---|
| 214 | def deinitialize(self): |
---|
| 215 | for mod in reversed(self.modules): # Deinitialize reversed of initialization |
---|
| 216 | try: |
---|
| 217 | logger.debug('Deactivating module {}'.format(mod.name)) |
---|
| 218 | mod.deactivate() |
---|
| 219 | except Exception as e: |
---|
| 220 | logger.exception() |
---|
| 221 | logger.error("Deactivation of {} failed: {}".format(mod.name, utils.exceptionToMessage(e))) |
---|
| 222 | |
---|
| 223 | def timerFnc(self): |
---|
| 224 | pass |
---|
| 225 | |
---|
| 226 | def message(self, msg): |
---|
[815ea50] | 227 | """ |
---|
[11f7a07] | 228 | Processes the message sent asynchronously, msg is an QString |
---|
[815ea50] | 229 | """ |
---|
[11f7a07] | 230 | try: |
---|
| 231 | logger.debug('msg: {}, {}'.format(type(msg), msg)) |
---|
| 232 | module, message, data = msg |
---|
| 233 | except Exception as e: |
---|
| 234 | logger.error('Got exception {} processing message {}'.format(e, msg)) |
---|
| 235 | return |
---|
| 236 | |
---|
| 237 | for v in self.modules: |
---|
| 238 | if v.name == module: # Case Sensitive!!!! |
---|
| 239 | try: |
---|
| 240 | logger.debug('Notifying message {} to module {} with json data {}'.format(message, v.name, data)) |
---|
| 241 | v.processMessage(message, json.loads(data)) |
---|
| 242 | return |
---|
| 243 | except Exception as e: |
---|
| 244 | logger.error('Got exception {} processing generic message on {}'.format(e, v.name)) |
---|
[98fc98d] | 245 | |
---|
[11f7a07] | 246 | logger.error('Module {} not found, messsage {} not sent'.format(module, message)) |
---|
| 247 | |
---|
| 248 | def executeScript(self, script): |
---|
| 249 | logger.debug('Executing script') |
---|
[53e7d45] | 250 | script = base64.b64decode(script.encode('ascii')) |
---|
[11f7a07] | 251 | th = ScriptExecutorThread(script) |
---|
| 252 | th.start() |
---|
| 253 | |
---|
| 254 | def logoff(self): |
---|
| 255 | logger.debug('Logoff invoked') |
---|
| 256 | operations.logoff() # Invoke log off |
---|
| 257 | |
---|
| 258 | def about(self): |
---|
[b84ab33] | 259 | self.aboutDlg.exec() |
---|
[11f7a07] | 260 | |
---|
[98fc98d] | 261 | def cleanup(self): |
---|
[11f7a07] | 262 | logger.debug('Quit invoked') |
---|
| 263 | if self.stopped is False: |
---|
| 264 | self.stopped = True |
---|
| 265 | try: |
---|
| 266 | self.deinitialize() |
---|
| 267 | except Exception: |
---|
| 268 | logger.exception() |
---|
| 269 | logger.error('Got exception deinitializing modules') |
---|
[98fc98d] | 270 | |
---|
[11f7a07] | 271 | try: |
---|
| 272 | # If we close Client, send Logoff to Broker |
---|
| 273 | self.ipc.sendLogout(operations.getCurrentUser()) |
---|
[0698264] | 274 | time.sleep(1) |
---|
[11f7a07] | 275 | self.timer.stop() |
---|
| 276 | self.ipc.stop() |
---|
| 277 | except Exception: |
---|
[0698264] | 278 | # May we have lost connection with server, simply log and exit in that case |
---|
[e21cba1] | 279 | logger.exception() |
---|
| 280 | logger.exception("Got an exception, processing quit") |
---|
[11f7a07] | 281 | |
---|
[98fc98d] | 282 | try: |
---|
| 283 | # operations.logoff() # Uncomment this after testing to logoff user |
---|
| 284 | pass |
---|
| 285 | except Exception: |
---|
| 286 | pass |
---|
| 287 | |
---|
| 288 | def quit(self): |
---|
[815ea50] | 289 | # logger.debug("Exec quit {}".format(self.stopped)) |
---|
[98fc98d] | 290 | if self.stopped is False: |
---|
| 291 | self.cleanup() |
---|
| 292 | self.app.quit() |
---|
[11f7a07] | 293 | |
---|
[aac3fb9] | 294 | def closeEvent(self, event): |
---|
[98fc98d] | 295 | logger.debug("Exec closeEvent") |
---|
| 296 | event.accept() |
---|
| 297 | self.quit() |
---|
[11f7a07] | 298 | |
---|
[815ea50] | 299 | |
---|
[11f7a07] | 300 | if __name__ == '__main__': |
---|
[1528428] | 301 | app = QtWidgets.QApplication(sys.argv) |
---|
[11f7a07] | 302 | |
---|
[1528428] | 303 | if not QtWidgets.QSystemTrayIcon.isSystemTrayAvailable(): |
---|
[11f7a07] | 304 | # QtGui.QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.") |
---|
| 305 | sys.exit(1) |
---|
| 306 | |
---|
| 307 | # This is important so our app won't close on messages windows (alerts, etc...) |
---|
[1528428] | 308 | QtWidgets.QApplication.setQuitOnLastWindowClosed(False) |
---|
[11f7a07] | 309 | |
---|
| 310 | try: |
---|
| 311 | trayIcon = OGASystemTray(app) |
---|
| 312 | except Exception as e: |
---|
| 313 | logger.exception() |
---|
[815ea50] | 314 | logger.error('OGA Service is not running, or it can\'t contact with OGA Server. User Tools stopped: {}'.format( |
---|
| 315 | utils.exceptionToMessage(e))) |
---|
[11f7a07] | 316 | sys.exit(1) |
---|
| 317 | |
---|
| 318 | try: |
---|
| 319 | trayIcon.initialize() # Initialize modules, etc.. |
---|
| 320 | except Exception as e: |
---|
| 321 | logger.exception() |
---|
| 322 | logger.error('Exception initializing OpenGnsys User Agent {}'.format(utils.exceptionToMessage(e))) |
---|
| 323 | trayIcon.quit() |
---|
| 324 | sys.exit(1) |
---|
| 325 | |
---|
[98fc98d] | 326 | app.aboutToQuit.connect(trayIcon.cleanup) |
---|
[11f7a07] | 327 | trayIcon.show() |
---|
| 328 | |
---|
| 329 | # Catch kill and logout user :) |
---|
[98fc98d] | 330 | atexit.register(sigAtExit) |
---|
[11f7a07] | 331 | |
---|
[b84ab33] | 332 | res = app.exec() |
---|
[11f7a07] | 333 | |
---|
| 334 | logger.debug('Exiting') |
---|
| 335 | trayIcon.quit() |
---|
| 336 | |
---|
| 337 | sys.exit(res) |
---|