OpenSecurity/bin/opensecurity_tray.py
author om
Fri, 06 Dec 2013 12:24:24 +0100
changeset 16 e16d64b5e008
parent 14 c187aaceca32
parent 13 4457d7071a23
permissions -rwxr-xr-x
working on client/server code merge
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurity-dialog
     6 # 
     7 # an opensecurity dialog
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import argparse
    36 import os
    37 import subprocess
    38 import sys
    39 
    40 from PyQt4 import QtCore
    41 from PyQt4 import QtGui
    42 
    43 # local
    44 from about import About
    45 from environment import Environment
    46 
    47 
    48 # ------------------------------------------------------------
    49 # code
    50 
    51 
    52 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
    53     
    54     """This is the OpenSecuirty Tray Icon"""
    55 
    56     def __init__(self, icon, parent=None):
    57         
    58         super(OpenSecurityTrayIcon, self).__init__(icon, parent)
    59         self.setup_ui()
    60         
    61         
    62     def clicked_about(self):
    63         """clicked about"""
    64         dlgAbout = About()
    65         dlgAbout.exec_()
    66     
    67 
    68     def clicked_exit(self):
    69         """clicked exit"""
    70         sys.exit(0)
    71     
    72 
    73     def clicked_launch_application(self):
    74         """clicked the launch an application"""
    75         dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
    76         process_command = [sys.executable, dlg_launch_image]
    77         print(process_command)
    78         process = subprocess.Popen(process_command, shell = False)
    79         process.communicate()
    80             
    81             
    82     def clicked_refresh(self):
    83         """clicked refresh"""
    84         self.setup_ui()
    85 
    86     
    87     def setup_ui(self):
    88         """create the user interface
    89         As for the system tray this is 'just' the context menu.
    90         """
    91     
    92         # define the tray icon menu
    93         menu = QtGui.QMenu(self.parent())
    94         self.setContextMenu(menu)
    95         
    96         # add known apps
    97         
    98         # add standard menu items
    99         cAcLaunch = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Lauch Application')
   100         menu.addSeparator()
   101         cAcRefresh = menu.addAction('Refresh')
   102         cAcAbout = menu.addAction("About")
   103         cAcExit = menu.addAction("Exit")
   104         
   105         cAcLaunch.triggered.connect(self.clicked_launch_application)
   106         cAcRefresh.triggered.connect(self.clicked_refresh)
   107         cAcAbout.triggered.connect(self.clicked_about)
   108         cAcExit.triggered.connect(self.clicked_exit)
   109         
   110         
   111 def main():
   112     
   113     app = QtGui.QApplication(sys.argv)
   114 
   115     # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   116     image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
   117     for file in os.listdir(image_path):
   118         if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   119             QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   120 
   121     w = QtGui.QWidget()
   122     trayIcon = OpenSecurityTrayIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), w)
   123 
   124     trayIcon.show()
   125     sys.exit(app.exec_())
   126    
   127 
   128 # start
   129 if __name__ == "__main__":
   130     main()
   131