OpenSecurity/bin/opensecurity_tray.pyw
changeset 104 e30b476d23a0
parent 100 1b0068ef8404
child 105 9cc91074deb8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/opensecurity_tray.pyw	Mon Mar 31 15:26:56 2014 +0200
     1.3 @@ -0,0 +1,200 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# opensecurity-dialog
     1.9 +# 
    1.10 +# an opensecurity dialog
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import argparse
    1.39 +import os
    1.40 +import subprocess
    1.41 +import sys
    1.42 +import urllib2
    1.43 +
    1.44 +from PyQt4 import QtCore
    1.45 +from PyQt4 import QtGui
    1.46 +
    1.47 +# local
    1.48 +if sys.platform == 'win32' or sys.platform == 'cygwin':
    1.49 +    from cygwin import Cygwin
    1.50 +
    1.51 +from ui import AboutDialog
    1.52 +from ui import opensecurity_rc
    1.53 +
    1.54 +
    1.55 +# ------------------------------------------------------------
    1.56 +# code
    1.57 +
    1.58 +
    1.59 +class OpenSecurityWait(QtGui.QDialog):
    1.60 +
    1.61 +    """OpenSecurity: please wait ..."""
    1.62 +    
    1.63 +    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    1.64 +        super(OpenSecurityWait, self).__init__(parent, flags)
    1.65 +        self.setWindowTitle('OpenSecurity')
    1.66 +        self.setup_ui()
    1.67 +        
    1.68 +        
    1.69 +    def setup_ui(self):
    1.70 +        """Create the widgets."""
    1.71 +        
    1.72 +        lyMain = QtGui.QVBoxLayout(self)
    1.73 +        lyMain.setContentsMargins(8, 8, 8, 8)
    1.74 +        
    1.75 +        # content area: left pixmap, right text
    1.76 +        lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
    1.77 +        lyMain.addWidget(lbTitle)
    1.78 +        
    1.79 +        self.setMinimumSize(400, 50)
    1.80 +        self.resize(lyMain.minimumSize())
    1.81 +
    1.82 +
    1.83 +class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
    1.84 +    
    1.85 +    """This is the OpenSecuirty Tray Icon"""
    1.86 +
    1.87 +    def __init__(self, icon, parent=None):
    1.88 +        
    1.89 +        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
    1.90 +        self.setup_ui()
    1.91 +        
    1.92 +        
    1.93 +    def clicked_about(self):
    1.94 +        """clicked about"""
    1.95 +        d = AboutDialog()
    1.96 +        d.exec_()
    1.97 +    
    1.98 +
    1.99 +    def clicked_browser(self):
   1.100 +        """wish for safe internet browsing"""
   1.101 +        
   1.102 +        if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
   1.103 +            QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
   1.104 +            return
   1.105 +
   1.106 +        # TODO: HARDCODED ADDRESS OF OPENSECURITYD
   1.107 +        
   1.108 +        # tell the user to wait
   1.109 +        d = OpenSecurityWait()
   1.110 +        d.show()
   1.111 +        QtGui.QApplication.instance().processEvents()
   1.112 +        
   1.113 +        try:
   1.114 +        
   1.115 +            # get a proper browsing VM
   1.116 +            Cygwin.start_X11()
   1.117 +            browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
   1.118 +            #dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
   1.119 +            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
   1.120 +            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
   1.121 +            #print(process_command)
   1.122 +            #process = subprocess.Popen(process_command, shell = False)
   1.123 +            
   1.124 +        except:
   1.125 +            d.hide()
   1.126 +            QtGui.QApplication.instance().processEvents()
   1.127 +            QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
   1.128 +            
   1.129 +        d.hide()
   1.130 +        QtGui.QApplication.instance().processEvents()
   1.131 +            
   1.132 +            
   1.133 +    def clicked_exit(self):
   1.134 +        """clicked exit"""
   1.135 +        sys.exit(0)
   1.136 +    
   1.137 +
   1.138 +    def clicked_launch_application(self):
   1.139 +        """clicked the launch an application"""
   1.140 +        dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
   1.141 +        process_command = [sys.executable, dlg_launch_image]
   1.142 +        print(process_command)
   1.143 +        process = subprocess.Popen(process_command, shell = False)
   1.144 +            
   1.145 +            
   1.146 +    def setup_ui(self):
   1.147 +        """create the user interface
   1.148 +        As for the system tray this is 'just' the context menu.
   1.149 +        """
   1.150 +    
   1.151 +        # define the tray icon menu
   1.152 +        menu = QtGui.QMenu(self.parent())
   1.153 +        self.setContextMenu(menu)
   1.154 +        
   1.155 +        # add known apps
   1.156 +        self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
   1.157 +        icon = QtGui.QIcon()
   1.158 +        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_icon_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   1.159 +        self.acBrowser.setIcon(icon)
   1.160 +        menu.addAction(self.acBrowser)
   1.161 +        menu.addSeparator()
   1.162 +        
   1.163 +        # add standard menu items
   1.164 +        self.acLaunch = QtGui.QAction('Launch Application', self.parent())
   1.165 +        icon = QtGui.QIcon()
   1.166 +        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_icon_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   1.167 +        self.acLaunch.setIcon(icon)
   1.168 +        menu.addAction(self.acLaunch)
   1.169 +        menu.addSeparator()
   1.170 +
   1.171 +        self.acAbout = menu.addAction('About')
   1.172 +        self.acExit = menu.addAction('Exit')
   1.173 +        
   1.174 +        self.acBrowser.triggered.connect(self.clicked_browser)
   1.175 +        self.acLaunch.triggered.connect(self.clicked_launch_application)
   1.176 +        self.acAbout.triggered.connect(self.clicked_about)
   1.177 +        self.acExit.triggered.connect(self.clicked_exit)
   1.178 +        
   1.179 +        
   1.180 +def main():
   1.181 +    
   1.182 +    a = QtGui.QApplication(sys.argv)
   1.183 +
   1.184 +#    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   1.185 +#    image_path = os.path.join(Environment("OpenSecurity").data_path, 'gfx')
   1.186 +#    for file in os.listdir(image_path):
   1.187 +#        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   1.188 +#            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   1.189 +
   1.190 +    w = QtGui.QWidget()
   1.191 +    icon = QtGui.QIcon()
   1.192 +    icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   1.193 +    trayIcon = OpenSecurityTrayIcon(icon)
   1.194 +
   1.195 +    trayIcon.show()
   1.196 +    a.setQuitOnLastWindowClosed(False)
   1.197 +    sys.exit(a.exec_())
   1.198 +   
   1.199 +
   1.200 +# start
   1.201 +if __name__ == "__main__":
   1.202 +    main()
   1.203 +