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