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