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