OpenSecurity/bin/opensecurity_dialog.py
author om
Mon, 09 Dec 2013 14:44:41 +0100
changeset 29 3f564e1673bb
parent 16 e16d64b5e008
child 92 bc1255abd544
permissions -rwxr-xr-x
added notifcation and password callback url
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 sys
om@13
    38
om@13
    39
from PyQt4 import QtCore
om@13
    40
from PyQt4 import QtGui
om@13
    41
om@13
    42
# local
om@13
    43
from credentials import Credentials
om@13
    44
from environment import Environment
om@29
    45
from notification import Notification
om@13
    46
from password import Password
om@13
    47
om@13
    48
om@13
    49
# ------------------------------------------------------------
om@13
    50
# code
om@13
    51
om@13
    52
om@13
    53
def main():
om@13
    54
    
om@13
    55
    # parse command line
om@13
    56
    parser = argparse.ArgumentParser(description = 'OpenSecurity Dialog.')
om@29
    57
    parser.add_argument('mode', metavar='MODE', help='dialog mode: \'password\', \'credentials\', \'notification-information\', \'notification-warning\' or \'notification-critical\'')
om@13
    58
    parser.add_argument('text', metavar='TEXT', help='text to show')
om@13
    59
    args = parser.parse_args()
om@13
    60
    
om@13
    61
    app = QtGui.QApplication(sys.argv)
om@13
    62
    
om@13
    63
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
om@13
    64
    data_path = Environment("OpenSecurity").data_path
om@13
    65
    image_path = os.path.join(data_path, '..', 'gfx')
om@13
    66
    for file in os.listdir(image_path):
om@13
    67
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@13
    68
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
om@13
    69
            
om@13
    70
    # we should have now our application icon
om@13
    71
    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
om@13
    72
    
om@13
    73
    if args.mode == 'password':
om@13
    74
        dlg = Password(args.text)
om@13
    75
    
om@13
    76
    if args.mode == 'credentials':
om@13
    77
        dlg = Credentials(args.text)
om@13
    78
    
om@29
    79
    if args.mode == 'notification-information':
om@29
    80
        dlg = Notification('information', args.text)
om@29
    81
    
om@29
    82
    if args.mode == 'notification-warning':
om@29
    83
        dlg = Notification('warning', args.text)
om@29
    84
    
om@29
    85
    if args.mode == 'notification-critical':
om@29
    86
        dlg = Notification('critical', args.text)
om@29
    87
        
om@29
    88
    if not 'dlg' in locals():
om@29
    89
        raise ValueError('unknown mode. type --help for help')
om@29
    90
    
om@13
    91
    # pop up the dialog
om@13
    92
    dlg.show()
om@13
    93
    app.exec_()
om@13
    94
    
om@13
    95
    # give proper result code
om@13
    96
    if dlg.result() == QtGui.QDialog.Accepted:
om@13
    97
        res = 0
om@13
    98
    else:
om@13
    99
        res = 1
om@13
   100
    sys.exit(res)
om@13
   101
    
om@13
   102
om@13
   103
# start
om@13
   104
if __name__ == "__main__":
om@13
   105
    main()
om@13
   106