OpenSecurity/bin/opensecurity_tray.pyw
author mb
Wed, 02 Apr 2014 10:41:00 +0100
changeset 110 490a78181935
parent 96 630b62946c9e
child 111 a2c7f29d3683
permissions -rwxr-xr-x
XWin - needs dbus
Fixed: Not terminating ssh session (due to debus-launcher not closing channels)
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
om@13
    45
from about import About
om@13
    46
from environment import Environment
mb@110
    47
from cygwin import Cygwin
mb@110
    48
import threading
mb@110
    49
import time
om@13
    50
# ------------------------------------------------------------
om@13
    51
# code
om@13
    52
om@13
    53
om@42
    54
class OpenSecurityWait(QtGui.QDialog):
om@42
    55
om@42
    56
    """OpenSecurity: please wait ..."""
om@42
    57
    
om@42
    58
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@42
    59
        super(OpenSecurityWait, self).__init__(parent, flags)
om@42
    60
        self.setWindowTitle('OpenSecurity')
om@42
    61
        self.setup_ui()
om@42
    62
        
om@42
    63
        
om@42
    64
    def setup_ui(self):
om@42
    65
        """Create the widgets."""
om@42
    66
        
om@42
    67
        lyMain = QtGui.QVBoxLayout(self)
om@42
    68
        lyMain.setContentsMargins(8, 8, 8, 8)
om@42
    69
        
om@42
    70
        # content area: left pixmap, right text
om@42
    71
        lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
om@42
    72
        lyMain.addWidget(lbTitle)
om@42
    73
        
om@42
    74
        self.setMinimumSize(400, 50)
om@42
    75
        self.resize(lyMain.minimumSize())
om@42
    76
om@42
    77
om@13
    78
class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
om@13
    79
    
om@13
    80
    """This is the OpenSecuirty Tray Icon"""
om@13
    81
om@13
    82
    def __init__(self, icon, parent=None):
om@13
    83
        
om@13
    84
        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
om@13
    85
        self.setup_ui()
om@13
    86
        
om@13
    87
        
om@13
    88
    def clicked_about(self):
om@13
    89
        """clicked about"""
om@13
    90
        dlgAbout = About()
om@13
    91
        dlgAbout.exec_()
om@13
    92
    
om@13
    93
om@37
    94
    def clicked_browser(self):
om@37
    95
        """wish for safe internet browsing"""
om@42
    96
        
om@42
    97
        # TODO: HARDCODED ADDRESS OF OPENSECURITYD
om@42
    98
        
om@42
    99
        # tell the user to wait
om@42
   100
        dlg = OpenSecurityWait()
om@42
   101
        dlg.show()
om@42
   102
        QtGui.QApplication.instance().processEvents()
om@42
   103
        
om@42
   104
        try:
om@42
   105
        
om@42
   106
            # get a proper browsing VM
mb@110
   107
            Cygwin.start_X11()
om@42
   108
            browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
mb@90
   109
            #dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
mb@60
   110
            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
mb@90
   111
            #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
mb@90
   112
            #print(process_command)
mb@90
   113
            #process = subprocess.Popen(process_command, shell = False)
om@42
   114
            
om@42
   115
        except:
om@42
   116
            dlg.hide()
om@42
   117
            QtGui.QApplication.instance().processEvents()
om@42
   118
            QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
om@42
   119
            
om@42
   120
        dlg.hide()
om@42
   121
        QtGui.QApplication.instance().processEvents()
om@37
   122
            
om@37
   123
            
om@13
   124
    def clicked_exit(self):
om@13
   125
        """clicked exit"""
om@13
   126
        sys.exit(0)
om@13
   127
    
om@13
   128
om@13
   129
    def clicked_launch_application(self):
om@13
   130
        """clicked the launch an application"""
om@13
   131
        dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
om@13
   132
        process_command = [sys.executable, dlg_launch_image]
om@13
   133
        print(process_command)
om@13
   134
        process = subprocess.Popen(process_command, shell = False)
om@13
   135
            
om@13
   136
            
om@13
   137
    def setup_ui(self):
om@13
   138
        """create the user interface
om@13
   139
        As for the system tray this is 'just' the context menu.
om@13
   140
        """
om@13
   141
    
om@13
   142
        # define the tray icon menu
om@13
   143
        menu = QtGui.QMenu(self.parent())
om@13
   144
        self.setContextMenu(menu)
om@13
   145
        
om@13
   146
        # add known apps
om@37
   147
        cAcBrowser = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Safe Internet Browsing')
om@37
   148
        menu.addSeparator()
om@13
   149
        
om@13
   150
        # add standard menu items
om@13
   151
        cAcLaunch = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Lauch Application')
om@13
   152
        menu.addSeparator()
om@13
   153
        cAcAbout = menu.addAction("About")
om@13
   154
        cAcExit = menu.addAction("Exit")
om@13
   155
        
om@37
   156
        cAcBrowser.triggered.connect(self.clicked_browser)
om@13
   157
        cAcLaunch.triggered.connect(self.clicked_launch_application)
om@13
   158
        cAcAbout.triggered.connect(self.clicked_about)
om@13
   159
        cAcExit.triggered.connect(self.clicked_exit)
mb@110
   160
                           
om@13
   161
def main():
om@13
   162
    app = QtGui.QApplication(sys.argv)
om@13
   163
om@13
   164
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
oliver@92
   165
    image_path = os.path.join(Environment("OpenSecurity").data_path, 'gfx')
om@13
   166
    for file in os.listdir(image_path):
om@13
   167
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@13
   168
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
om@13
   169
om@13
   170
    w = QtGui.QWidget()
om@13
   171
    trayIcon = OpenSecurityTrayIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), w)
om@13
   172
om@13
   173
    trayIcon.show()
om@38
   174
    app.setQuitOnLastWindowClosed(False)
om@13
   175
    sys.exit(app.exec_())
om@13
   176
   
om@13
   177
om@13
   178
# start
om@13
   179
if __name__ == "__main__":
om@13
   180
    main()
om@13
   181