OpenSecurity/bin/opensecurity_tray.pyw
author om
Wed, 11 Dec 2013 14:42:55 +0100
changeset 42 e10a08095ccc
parent 38 560882d3d3c0
child 44 1d4afdfca7a9
permissions -rwxr-xr-x
more details on GUI parts
     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 import urllib2
    40 
    41 from PyQt4 import QtCore
    42 from PyQt4 import QtGui
    43 
    44 # local
    45 from about import About
    46 from environment import Environment
    47 
    48 
    49 # ------------------------------------------------------------
    50 # code
    51 
    52 
    53 class OpenSecurityWait(QtGui.QDialog):
    54 
    55     """OpenSecurity: please wait ..."""
    56     
    57     def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    58         super(OpenSecurityWait, self).__init__(parent, flags)
    59         self.setWindowTitle('OpenSecurity')
    60         self.setup_ui()
    61         
    62         
    63     def setup_ui(self):
    64         """Create the widgets."""
    65         
    66         lyMain = QtGui.QVBoxLayout(self)
    67         lyMain.setContentsMargins(8, 8, 8, 8)
    68         
    69         # content area: left pixmap, right text
    70         lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
    71         lyMain.addWidget(lbTitle)
    72         
    73         self.setMinimumSize(400, 50)
    74         self.resize(lyMain.minimumSize())
    75 
    76 
    77 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
    78     
    79     """This is the OpenSecuirty Tray Icon"""
    80 
    81     def __init__(self, icon, parent=None):
    82         
    83         super(OpenSecurityTrayIcon, self).__init__(icon, parent)
    84         self.setup_ui()
    85         
    86         
    87     def clicked_about(self):
    88         """clicked about"""
    89         dlgAbout = About()
    90         dlgAbout.exec_()
    91     
    92 
    93     def clicked_browser(self):
    94         """wish for safe internet browsing"""
    95         
    96         # TODO: HARDCODED ADDRESS OF OPENSECURITYD
    97         
    98         # tell the user to wait
    99         dlg = OpenSecurityWait()
   100         dlg.show()
   101         QtGui.QApplication.instance().processEvents()
   102         
   103         try:
   104         
   105             # get a proper browsing VM
   106             browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
   107             dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
   108             process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
   109             print(process_command)
   110             process = subprocess.Popen(process_command, shell = False)
   111             process.communicate()
   112             
   113         except:
   114             dlg.hide()
   115             QtGui.QApplication.instance().processEvents()
   116             QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
   117             
   118         dlg.hide()
   119         QtGui.QApplication.instance().processEvents()
   120             
   121             
   122     def clicked_exit(self):
   123         """clicked exit"""
   124         sys.exit(0)
   125     
   126 
   127     def clicked_launch_application(self):
   128         """clicked the launch an application"""
   129         dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
   130         process_command = [sys.executable, dlg_launch_image]
   131         print(process_command)
   132         process = subprocess.Popen(process_command, shell = False)
   133         process.communicate()
   134             
   135             
   136     def clicked_refresh(self):
   137         """clicked refresh"""
   138         self.setup_ui()
   139 
   140     
   141     def setup_ui(self):
   142         """create the user interface
   143         As for the system tray this is 'just' the context menu.
   144         """
   145     
   146         # define the tray icon menu
   147         menu = QtGui.QMenu(self.parent())
   148         self.setContextMenu(menu)
   149         
   150         # add known apps
   151         cAcBrowser = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Safe Internet Browsing')
   152         menu.addSeparator()
   153         
   154         # add standard menu items
   155         cAcLaunch = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Lauch Application')
   156         menu.addSeparator()
   157         cAcRefresh = menu.addAction('Refresh')
   158         cAcAbout = menu.addAction("About")
   159         cAcExit = menu.addAction("Exit")
   160         
   161         cAcBrowser.triggered.connect(self.clicked_browser)
   162         cAcLaunch.triggered.connect(self.clicked_launch_application)
   163         cAcRefresh.triggered.connect(self.clicked_refresh)
   164         cAcAbout.triggered.connect(self.clicked_about)
   165         cAcExit.triggered.connect(self.clicked_exit)
   166         
   167         
   168 def main():
   169     
   170     app = QtGui.QApplication(sys.argv)
   171 
   172     # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   173     image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
   174     for file in os.listdir(image_path):
   175         if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   176             QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   177 
   178     w = QtGui.QWidget()
   179     trayIcon = OpenSecurityTrayIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), w)
   180 
   181     trayIcon.show()
   182     app.setQuitOnLastWindowClosed(False)
   183     sys.exit(app.exec_())
   184    
   185 
   186 # start
   187 if __name__ == "__main__":
   188     main()
   189