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