OpenSecurity/bin/opensecurity_tray.pyw
author om
Tue, 10 Dec 2013 17:24:12 +0100
changeset 37 6d7b4672414c
parent 16 OpenSecurity/bin/opensecurity_tray.py@e16d64b5e008
child 38 560882d3d3c0
permissions -rwxr-xr-x
added opensecurity-system tray starter
     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_browser(self):
    69         """wish for safe internet browsing"""
    70         dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
    71         process_command = [sys.executable, dlg_launch_image, '192.168.56.101', '/usr/bin/iceweasel']
    72         print(process_command)
    73         process = subprocess.Popen(process_command, shell = False)
    74         process.communicate()
    75             
    76             
    77     def clicked_exit(self):
    78         """clicked exit"""
    79         sys.exit(0)
    80     
    81 
    82     def clicked_launch_application(self):
    83         """clicked the launch an application"""
    84         dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
    85         process_command = [sys.executable, dlg_launch_image]
    86         print(process_command)
    87         process = subprocess.Popen(process_command, shell = False)
    88         process.communicate()
    89             
    90             
    91     def clicked_refresh(self):
    92         """clicked refresh"""
    93         self.setup_ui()
    94 
    95     
    96     def setup_ui(self):
    97         """create the user interface
    98         As for the system tray this is 'just' the context menu.
    99         """
   100     
   101         # define the tray icon menu
   102         menu = QtGui.QMenu(self.parent())
   103         self.setContextMenu(menu)
   104         
   105         # add known apps
   106         cAcBrowser = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Safe Internet Browsing')
   107         menu.addSeparator()
   108         
   109         # add standard menu items
   110         cAcLaunch = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Lauch Application')
   111         menu.addSeparator()
   112         cAcRefresh = menu.addAction('Refresh')
   113         cAcAbout = menu.addAction("About")
   114         cAcExit = menu.addAction("Exit")
   115         
   116         cAcBrowser.triggered.connect(self.clicked_browser)
   117         cAcLaunch.triggered.connect(self.clicked_launch_application)
   118         cAcRefresh.triggered.connect(self.clicked_refresh)
   119         cAcAbout.triggered.connect(self.clicked_about)
   120         cAcExit.triggered.connect(self.clicked_exit)
   121         
   122         
   123 def main():
   124     
   125     app = QtGui.QApplication(sys.argv)
   126 
   127     # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   128     image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
   129     for file in os.listdir(image_path):
   130         if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   131             QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   132 
   133     w = QtGui.QWidget()
   134     trayIcon = OpenSecurityTrayIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), w)
   135 
   136     trayIcon.show()
   137     sys.exit(app.exec_())
   138    
   139 
   140 # start
   141 if __name__ == "__main__":
   142     main()
   143