OpenSecurity/bin/opensecurity_tray.pyw
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Tue, 01 Apr 2014 13:43:59 +0200
changeset 106 c5101320b46c
parent 105 9cc91074deb8
child 111 a2c7f29d3683
permissions -rwxr-xr-x
more work on user interface
     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 if sys.platform == 'win32' or sys.platform == 'cygwin':
    46     from cygwin import Cygwin
    47 
    48 from ui import AboutDialog
    49 from ui import ConfigureDialog
    50 from ui import opensecurity_rc
    51 
    52 
    53 # ------------------------------------------------------------
    54 # code
    55 
    56 
    57 class OpenSecurityWait(QtGui.QDialog):
    58 
    59     """OpenSecurity: please wait ..."""
    60     
    61     def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    62         super(OpenSecurityWait, self).__init__(parent, flags)
    63         self.setWindowTitle('OpenSecurity')
    64         self.setup_ui()
    65         
    66         
    67     def setup_ui(self):
    68         """Create the widgets."""
    69         
    70         lyMain = QtGui.QVBoxLayout(self)
    71         lyMain.setContentsMargins(8, 8, 8, 8)
    72         
    73         # content area: left pixmap, right text
    74         lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
    75         lyMain.addWidget(lbTitle)
    76         
    77         self.setMinimumSize(400, 50)
    78         self.resize(lyMain.minimumSize())
    79 
    80 
    81 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
    82     
    83     """This is the OpenSecuirty Tray Icon"""
    84 
    85     def __init__(self, icon, parent=None):
    86         
    87         super(OpenSecurityTrayIcon, self).__init__(icon, parent)
    88         self.setup_ui()
    89         
    90         
    91     def clicked_about(self):
    92         """clicked about"""
    93         d = AboutDialog()
    94         d.exec_()
    95     
    96 
    97     def clicked_browser(self):
    98         """wish for safe internet browsing"""
    99         
   100         if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
   101             QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
   102             return
   103 
   104         # TODO: HARDCODED ADDRESS OF OPENSECURITYD
   105         
   106         # tell the user to wait
   107         d = OpenSecurityWait()
   108         d.show()
   109         QtGui.QApplication.instance().processEvents()
   110         
   111         try:
   112         
   113             # get a proper browsing VM
   114             browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
   115             #dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
   116             #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
   117             #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
   118             #print(process_command)
   119             #process = subprocess.Popen(process_command, shell = False)
   120             
   121         except:
   122             d.hide()
   123             QtGui.QApplication.instance().processEvents()
   124             QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
   125             
   126         d.hide()
   127         QtGui.QApplication.instance().processEvents()
   128             
   129             
   130     def clicked_configure(self):
   131         """clicked configure"""
   132         d = ConfigureDialog()
   133         d.exec_()
   134     
   135 
   136     def clicked_exit(self):
   137         """clicked exit"""
   138         sys.exit(0)
   139     
   140 
   141     def clicked_launch_application(self):
   142         """clicked the launch an application"""
   143         dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
   144         process_command = [sys.executable, dlg_launch_image]
   145         print(process_command)
   146         process = subprocess.Popen(process_command, shell = False)
   147             
   148             
   149     def setup_ui(self):
   150         """create the user interface
   151         As for the system tray this is 'just' the context menu.
   152         """
   153     
   154         # define the tray icon menu
   155         menu = QtGui.QMenu(self.parent())
   156         self.setContextMenu(menu)
   157         
   158         # add known apps
   159         self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
   160         icon = QtGui.QIcon()
   161         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   162         self.acBrowser.setIcon(icon)
   163         menu.addAction(self.acBrowser)
   164         menu.addSeparator()
   165         
   166         # add standard menu items
   167         self.acLaunch = QtGui.QAction('Launch Application', self.parent())
   168         icon = QtGui.QIcon()
   169         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   170         self.acLaunch.setIcon(icon)
   171         menu.addAction(self.acLaunch)
   172         self.acConfigure = QtGui.QAction('Configuration', self.parent())
   173         icon = QtGui.QIcon()
   174         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   175         self.acConfigure.setIcon(icon)
   176         menu.addAction(self.acConfigure)
   177         menu.addSeparator()
   178 
   179         self.acAbout = menu.addAction('About')
   180         self.acExit = menu.addAction('Exit')
   181         
   182         self.acBrowser.triggered.connect(self.clicked_browser)
   183         self.acLaunch.triggered.connect(self.clicked_launch_application)
   184         self.acConfigure.triggered.connect(self.clicked_configure)
   185         self.acAbout.triggered.connect(self.clicked_about)
   186         self.acExit.triggered.connect(self.clicked_exit)
   187         
   188         
   189 def main():
   190     
   191     a = QtGui.QApplication(sys.argv)
   192 
   193     w = QtGui.QWidget()
   194     icon = QtGui.QIcon()
   195     icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   196     trayIcon = OpenSecurityTrayIcon(icon)
   197 
   198     trayIcon.show()
   199     a.setQuitOnLastWindowClosed(False)
   200     sys.exit(a.exec_())
   201    
   202 
   203 # start
   204 if __name__ == "__main__":
   205     main()
   206