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
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurity-dialog
     6 # 
     7 # an opensecurity dialog
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import argparse
    36 import os
    37 import sys
    38 
    39 from PyQt4 import QtCore
    40 from PyQt4 import QtGui
    41 
    42 # local
    43 from credentials import Credentials
    44 from environment import Environment
    45 from notification import Notification
    46 from password import Password
    47 
    48 
    49 # ------------------------------------------------------------
    50 # code
    51 
    52 
    53 def main():
    54     
    55     # parse command line
    56     parser = argparse.ArgumentParser(description = 'OpenSecurity Dialog.')
    57     parser.add_argument('mode', metavar='MODE', help='dialog mode: \'password\', \'credentials\', \'notification-information\', \'notification-warning\' or \'notification-critical\'')
    58     parser.add_argument('text', metavar='TEXT', help='text to show')
    59     args = parser.parse_args()
    60     
    61     app = QtGui.QApplication(sys.argv)
    62     
    63     # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
    64     data_path = Environment("OpenSecurity").data_path
    65     image_path = os.path.join(data_path, '..', 'gfx')
    66     for file in os.listdir(image_path):
    67         if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
    68             QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
    69             
    70     # we should have now our application icon
    71     app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
    72     
    73     if args.mode == 'password':
    74         dlg = Password(args.text)
    75     
    76     if args.mode == 'credentials':
    77         dlg = Credentials(args.text)
    78     
    79     if args.mode == 'notification-information':
    80         dlg = Notification('information', args.text)
    81     
    82     if args.mode == 'notification-warning':
    83         dlg = Notification('warning', args.text)
    84     
    85     if args.mode == 'notification-critical':
    86         dlg = Notification('critical', args.text)
    87         
    88     if not 'dlg' in locals():
    89         raise ValueError('unknown mode. type --help for help')
    90     
    91     # pop up the dialog
    92     dlg.show()
    93     app.exec_()
    94     
    95     # give proper result code
    96     if dlg.result() == QtGui.QDialog.Accepted:
    97         res = 0
    98     else:
    99         res = 1
   100     sys.exit(res)
   101     
   102 
   103 # start
   104 if __name__ == "__main__":
   105     main()
   106