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