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