OpenSecurity/bin/opensecurity_tray.pyw
author mb
Wed, 02 Apr 2014 10:45:58 +0100
changeset 111 a2c7f29d3683
parent 110 490a78181935
parent 106 c5101320b46c
child 136 ac117cd7bab1
permissions -rwxr-xr-x
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 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             Cygwin.start_X11()
   115             browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
   116             #dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
   117             #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
   118             #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
   119             #print(process_command)
   120             #process = subprocess.Popen(process_command, shell = False)
   121             
   122         except:
   123             d.hide()
   124             QtGui.QApplication.instance().processEvents()
   125             QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
   126             
   127         d.hide()
   128         QtGui.QApplication.instance().processEvents()
   129             
   130             
   131     def clicked_configure(self):
   132         """clicked configure"""
   133         d = ConfigureDialog()
   134         d.exec_()
   135     
   136 
   137     def clicked_exit(self):
   138         """clicked exit"""
   139         sys.exit(0)
   140     
   141 
   142     def clicked_launch_application(self):
   143         """clicked the launch an application"""
   144         dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
   145         process_command = [sys.executable, dlg_launch_image]
   146         print(process_command)
   147         process = subprocess.Popen(process_command, shell = False)
   148             
   149             
   150     def setup_ui(self):
   151         """create the user interface
   152         As for the system tray this is 'just' the context menu.
   153         """
   154     
   155         # define the tray icon menu
   156         menu = QtGui.QMenu(self.parent())
   157         self.setContextMenu(menu)
   158         
   159         # add known apps
   160         self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
   161         icon = QtGui.QIcon()
   162         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   163         self.acBrowser.setIcon(icon)
   164         menu.addAction(self.acBrowser)
   165         menu.addSeparator()
   166         
   167         # add standard menu items
   168         self.acLaunch = QtGui.QAction('Launch Application', self.parent())
   169         icon = QtGui.QIcon()
   170         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   171         self.acLaunch.setIcon(icon)
   172         menu.addAction(self.acLaunch)
   173         self.acConfigure = QtGui.QAction('Configuration', self.parent())
   174         icon = QtGui.QIcon()
   175         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   176         self.acConfigure.setIcon(icon)
   177         menu.addAction(self.acConfigure)
   178         menu.addSeparator()
   179 
   180         self.acAbout = menu.addAction('About')
   181         self.acExit = menu.addAction('Exit')
   182         
   183         self.acBrowser.triggered.connect(self.clicked_browser)
   184         self.acLaunch.triggered.connect(self.clicked_launch_application)
   185         self.acConfigure.triggered.connect(self.clicked_configure)
   186         self.acAbout.triggered.connect(self.clicked_about)
   187         self.acExit.triggered.connect(self.clicked_exit)
   188         
   189         
   190 def main():
   191     
   192     a = QtGui.QApplication(sys.argv)
   193 
   194     w = QtGui.QWidget()
   195     icon = QtGui.QIcon()
   196     icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   197     trayIcon = OpenSecurityTrayIcon(icon)
   198 
   199     trayIcon.show()
   200     a.setQuitOnLastWindowClosed(False)
   201     sys.exit(a.exec_())
   202    
   203 
   204 # start
   205 if __name__ == "__main__":
   206     main()
   207