OpenSecurity/bin/opensecurity_tray.py
author om
Fri, 06 Dec 2013 12:10:30 +0100
changeset 14 c187aaceca32
parent 3 OpenSecurity/client/opensecurity_tray.py@65432e6c6042
child 16 e16d64b5e008
permissions -rwxr-xr-x
renamed "client" to "bin"
om@3
     1
#!/bin/env python
om@3
     2
# -*- coding: utf-8 -*-
om@3
     3
om@3
     4
# ------------------------------------------------------------
om@3
     5
# opensecurity-dialog
om@3
     6
# 
om@3
     7
# an opensecurity dialog
om@3
     8
#
om@3
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@3
    10
#
om@3
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@3
    12
# AIT Austrian Institute of Technology GmbH
om@3
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@3
    14
# http://www.ait.ac.at
om@3
    15
#
om@3
    16
# This program is free software; you can redistribute it and/or
om@3
    17
# modify it under the terms of the GNU General Public License
om@3
    18
# as published by the Free Software Foundation version 2.
om@3
    19
# 
om@3
    20
# This program is distributed in the hope that it will be useful,
om@3
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@3
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@3
    23
# GNU General Public License for more details.
om@3
    24
# 
om@3
    25
# You should have received a copy of the GNU General Public License
om@3
    26
# along with this program; if not, write to the Free Software
om@3
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@3
    28
# Boston, MA  02110-1301, USA.
om@3
    29
# ------------------------------------------------------------
om@3
    30
om@3
    31
om@3
    32
# ------------------------------------------------------------
om@3
    33
# imports
om@3
    34
om@3
    35
import argparse
om@3
    36
import os
om@3
    37
import subprocess
om@3
    38
import sys
om@3
    39
om@3
    40
from PyQt4 import QtCore
om@3
    41
from PyQt4 import QtGui
om@3
    42
om@3
    43
# local
om@3
    44
from about import About
om@3
    45
from environment import Environment
om@3
    46
om@3
    47
om@3
    48
# ------------------------------------------------------------
om@3
    49
# code
om@3
    50
om@3
    51
om@3
    52
class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
om@3
    53
    
om@3
    54
    """This is the OpenSecuirty Tray Icon"""
om@3
    55
om@3
    56
    def __init__(self, icon, parent=None):
om@3
    57
        
om@3
    58
        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
om@3
    59
        self.setup_ui()
om@3
    60
        
om@3
    61
        
om@3
    62
    def clicked_about(self):
om@3
    63
        """clicked about"""
om@3
    64
        dlgAbout = About()
om@3
    65
        dlgAbout.exec_()
om@3
    66
    
om@3
    67
om@3
    68
    def clicked_exit(self):
om@3
    69
        """clicked exit"""
om@3
    70
        sys.exit(0)
om@3
    71
    
om@3
    72
om@3
    73
    def clicked_launch_application(self):
om@3
    74
        """clicked the launch an application"""
om@3
    75
        dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
om@3
    76
        process_command = [sys.executable, dlg_launch_image]
om@3
    77
        print(process_command)
om@3
    78
        process = subprocess.Popen(process_command, shell = False)
om@3
    79
        process.communicate()
om@3
    80
            
om@3
    81
            
om@3
    82
    def clicked_refresh(self):
om@3
    83
        """clicked refresh"""
om@3
    84
        self.setup_ui()
om@3
    85
om@3
    86
    
om@3
    87
    def setup_ui(self):
om@3
    88
        """create the user interface
om@3
    89
        As for the system tray this is 'just' the context menu.
om@3
    90
        """
om@3
    91
    
om@3
    92
        # define the tray icon menu
om@3
    93
        menu = QtGui.QMenu(self.parent())
om@3
    94
        self.setContextMenu(menu)
om@3
    95
        
om@3
    96
        # add known apps
om@3
    97
        
om@3
    98
        # add standard menu items
om@3
    99
        cAcLaunch = menu.addAction(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), 'Lauch Application')
om@3
   100
        menu.addSeparator()
om@3
   101
        cAcRefresh = menu.addAction('Refresh')
om@3
   102
        cAcAbout = menu.addAction("About")
om@3
   103
        cAcExit = menu.addAction("Exit")
om@3
   104
        
om@3
   105
        cAcLaunch.triggered.connect(self.clicked_launch_application)
om@3
   106
        cAcRefresh.triggered.connect(self.clicked_refresh)
om@3
   107
        cAcAbout.triggered.connect(self.clicked_about)
om@3
   108
        cAcExit.triggered.connect(self.clicked_exit)
om@3
   109
        
om@3
   110
        
om@3
   111
def main():
om@3
   112
    
om@3
   113
    app = QtGui.QApplication(sys.argv)
om@3
   114
om@3
   115
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
om@3
   116
    image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
om@3
   117
    for file in os.listdir(image_path):
om@3
   118
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@3
   119
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
om@3
   120
om@3
   121
    w = QtGui.QWidget()
om@3
   122
    trayIcon = OpenSecurityTrayIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')), w)
om@3
   123
om@3
   124
    trayIcon.show()
om@3
   125
    sys.exit(app.exec_())
om@3
   126
   
om@3
   127
om@3
   128
# start
om@3
   129
if __name__ == "__main__":
om@3
   130
    main()
om@3
   131