OpenSecurity/bin/opensecurity_tray.pyw
author mb
Wed, 02 Apr 2014 10:45:58 +0100
changeset 111 a2c7f29d3683
parent 110 490a78181935
parent 106 c5101320b46c
child 136 ac117cd7bab1
permissions -rwxr-xr-x
merge
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurity-dialog
om@13
     6
# 
om@13
     7
# an opensecurity dialog
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import argparse
om@13
    36
import os
om@13
    37
import subprocess
om@13
    38
import sys
om@42
    39
import urllib2
om@13
    40
om@13
    41
from PyQt4 import QtCore
om@13
    42
from PyQt4 import QtGui
om@13
    43
om@13
    44
# local
oliver@104
    45
if sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@104
    46
    from cygwin import Cygwin
oliver@104
    47
oliver@104
    48
from ui import AboutDialog
oliver@106
    49
from ui import ConfigureDialog
oliver@104
    50
from ui import opensecurity_rc
om@13
    51
oliver@106
    52
om@13
    53
# ------------------------------------------------------------
om@13
    54
# code
om@13
    55
om@13
    56
om@42
    57
class OpenSecurityWait(QtGui.QDialog):
om@42
    58
om@42
    59
    """OpenSecurity: please wait ..."""
om@42
    60
    
om@42
    61
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@42
    62
        super(OpenSecurityWait, self).__init__(parent, flags)
om@42
    63
        self.setWindowTitle('OpenSecurity')
om@42
    64
        self.setup_ui()
om@42
    65
        
om@42
    66
        
om@42
    67
    def setup_ui(self):
om@42
    68
        """Create the widgets."""
om@42
    69
        
om@42
    70
        lyMain = QtGui.QVBoxLayout(self)
om@42
    71
        lyMain.setContentsMargins(8, 8, 8, 8)
om@42
    72
        
om@42
    73
        # content area: left pixmap, right text
om@42
    74
        lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
om@42
    75
        lyMain.addWidget(lbTitle)
om@42
    76
        
om@42
    77
        self.setMinimumSize(400, 50)
om@42
    78
        self.resize(lyMain.minimumSize())
om@42
    79
om@42
    80
om@13
    81
class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
om@13
    82
    
om@13
    83
    """This is the OpenSecuirty Tray Icon"""
om@13
    84
om@13
    85
    def __init__(self, icon, parent=None):
om@13
    86
        
om@13
    87
        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
om@13
    88
        self.setup_ui()
om@13
    89
        
om@13
    90
        
om@13
    91
    def clicked_about(self):
om@13
    92
        """clicked about"""
oliver@104
    93
        d = AboutDialog()
oliver@104
    94
        d.exec_()
om@13
    95
    
om@13
    96
om@37
    97
    def clicked_browser(self):
om@37
    98
        """wish for safe internet browsing"""
om@42
    99
        
oliver@104
   100
        if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
oliver@104
   101
            QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
oliver@104
   102
            return
oliver@104
   103
om@42
   104
        # TODO: HARDCODED ADDRESS OF OPENSECURITYD
om@42
   105
        
om@42
   106
        # tell the user to wait
oliver@104
   107
        d = OpenSecurityWait()
oliver@104
   108
        d.show()
om@42
   109
        QtGui.QApplication.instance().processEvents()
om@42
   110
        
om@42
   111
        try:
om@42
   112
        
om@42
   113
            # get a proper browsing VM
mb@110
   114
            Cygwin.start_X11()
om@42
   115
            browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
mb@90
   116
            #dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
mb@60
   117
            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
mb@90
   118
            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
mb@90
   119
            #print(process_command)
mb@90
   120
            #process = subprocess.Popen(process_command, shell = False)
om@42
   121
            
om@42
   122
        except:
oliver@104
   123
            d.hide()
om@42
   124
            QtGui.QApplication.instance().processEvents()
om@42
   125
            QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
om@42
   126
            
oliver@104
   127
        d.hide()
om@42
   128
        QtGui.QApplication.instance().processEvents()
om@37
   129
            
om@37
   130
            
oliver@106
   131
    def clicked_configure(self):
oliver@106
   132
        """clicked configure"""
oliver@106
   133
        d = ConfigureDialog()
oliver@106
   134
        d.exec_()
oliver@106
   135
    
oliver@106
   136
om@13
   137
    def clicked_exit(self):
om@13
   138
        """clicked exit"""
om@13
   139
        sys.exit(0)
om@13
   140
    
om@13
   141
om@13
   142
    def clicked_launch_application(self):
om@13
   143
        """clicked the launch an application"""
oliver@104
   144
        dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
om@13
   145
        process_command = [sys.executable, dlg_launch_image]
om@13
   146
        print(process_command)
om@13
   147
        process = subprocess.Popen(process_command, shell = False)
om@13
   148
            
om@13
   149
            
om@13
   150
    def setup_ui(self):
om@13
   151
        """create the user interface
om@13
   152
        As for the system tray this is 'just' the context menu.
om@13
   153
        """
om@13
   154
    
om@13
   155
        # define the tray icon menu
om@13
   156
        menu = QtGui.QMenu(self.parent())
om@13
   157
        self.setContextMenu(menu)
om@13
   158
        
om@13
   159
        # add known apps
oliver@104
   160
        self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
oliver@104
   161
        icon = QtGui.QIcon()
oliver@106
   162
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   163
        self.acBrowser.setIcon(icon)
oliver@104
   164
        menu.addAction(self.acBrowser)
om@37
   165
        menu.addSeparator()
om@13
   166
        
om@13
   167
        # add standard menu items
oliver@104
   168
        self.acLaunch = QtGui.QAction('Launch Application', self.parent())
oliver@104
   169
        icon = QtGui.QIcon()
oliver@106
   170
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   171
        self.acLaunch.setIcon(icon)
oliver@104
   172
        menu.addAction(self.acLaunch)
oliver@106
   173
        self.acConfigure = QtGui.QAction('Configuration', self.parent())
oliver@106
   174
        icon = QtGui.QIcon()
oliver@106
   175
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@106
   176
        self.acConfigure.setIcon(icon)
oliver@106
   177
        menu.addAction(self.acConfigure)
om@13
   178
        menu.addSeparator()
oliver@104
   179
oliver@104
   180
        self.acAbout = menu.addAction('About')
oliver@104
   181
        self.acExit = menu.addAction('Exit')
om@13
   182
        
oliver@104
   183
        self.acBrowser.triggered.connect(self.clicked_browser)
oliver@104
   184
        self.acLaunch.triggered.connect(self.clicked_launch_application)
oliver@106
   185
        self.acConfigure.triggered.connect(self.clicked_configure)
oliver@104
   186
        self.acAbout.triggered.connect(self.clicked_about)
oliver@104
   187
        self.acExit.triggered.connect(self.clicked_exit)
om@13
   188
        
om@13
   189
        
om@13
   190
def main():
om@13
   191
    
oliver@104
   192
    a = QtGui.QApplication(sys.argv)
om@13
   193
om@13
   194
    w = QtGui.QWidget()
oliver@104
   195
    icon = QtGui.QIcon()
oliver@104
   196
    icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   197
    trayIcon = OpenSecurityTrayIcon(icon)
om@13
   198
om@13
   199
    trayIcon.show()
oliver@104
   200
    a.setQuitOnLastWindowClosed(False)
oliver@104
   201
    sys.exit(a.exec_())
om@13
   202
   
om@13
   203
om@13
   204
# start
om@13
   205
if __name__ == "__main__":
om@13
   206
    main()
om@13
   207