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