OpenSecurity/bin/notification.py
author om
Mon, 09 Dec 2013 14:44:41 +0100
changeset 29 3f564e1673bb
permissions -rw-r--r--
added notifcation and password callback url
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # notification-dialog
     6 # 
     7 # show the user an opensecurity specific message box
     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 sys
    36 
    37 from PyQt4 import QtCore
    38 from PyQt4 import QtGui
    39 
    40 # local
    41 from about import About
    42 
    43 
    44 # ------------------------------------------------------------
    45 # code
    46 
    47 class Notification(QtGui.QDialog):
    48     
    49     """Show the user an OpenSecurity specific notification."""
    50     
    51     TYPES = ['information', 'warning', 'critical']
    52         
    53     def __init__(self, msgtype, text, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    54         
    55         # super call and widget init
    56         super(Notification, self).__init__(parent, flags)
    57         self.setWindowTitle('OpenSecuirty Notification')
    58         self.setup_ui(msgtype)
    59         
    60         # positionate ourself central
    61         screen = QtGui.QDesktopWidget().screenGeometry()
    62         self.resize(self.geometry().width() * 1.25, self.geometry().height())
    63         size = self.geometry()
    64         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    65         
    66         # fix up text
    67         if msgtype in Notification.TYPES:
    68             
    69             typetext = { 
    70                 'information': 'OpenSecurity information', 
    71                 'warning': '<b>OpenSecurity warning</b>', 
    72                 'critical': '<b>OpenSecurity critical error</b>'
    73             }
    74             self.lbMsgType.setText(typetext[msgtype])
    75             
    76             captiontext = { 
    77                 'information': 'OpenSecurity: Information', 
    78                 'warning': 'OpenSecurity: Warning', 
    79                 'critical': 'OpenSecurity: Critical'
    80             }
    81             self.setWindowTitle(captiontext[msgtype])
    82             
    83         else:
    84             raise ValueError('unknown msgtype')
    85         
    86         self.lbText.setText(text)
    87         
    88 
    89     def clicked_about(self):
    90         """clicked the about button"""
    91         dlgAbout = About()
    92         dlgAbout.exec_()
    93     
    94 
    95     def clicked_ok(self):
    96         """clicked the ok button"""
    97         self.accept()
    98     
    99 
   100     def setup_ui(self, msgtype):
   101         
   102         """Create the widgets."""
   103         
   104         lyMain = QtGui.QVBoxLayout(self)
   105         lyMain.setContentsMargins(8, 8, 8, 8)
   106         
   107         # content area: left pixmap, right text
   108         lyContent = QtGui.QHBoxLayout()
   109         lyMain.addLayout(lyContent)
   110         
   111         # pixmap
   112         lbPix = QtGui.QLabel()
   113         lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   114         lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   115         lyContent.addSpacing(16)
   116         
   117         # text ...
   118         lyText = QtGui.QVBoxLayout()
   119         lyContent.addLayout(lyText, 1)
   120         
   121         self.lbMsgType = QtGui.QLabel()
   122         lyText.addWidget(self.lbMsgType)
   123         self.lbText = QtGui.QLabel()
   124         lyText.addWidget(self.lbText)
   125         lyText.addStretch(1)
   126         
   127         lyMain.addStretch(1)
   128         
   129         # buttons
   130         lyButton = QtGui.QHBoxLayout()
   131         lyMain.addLayout(lyButton)
   132         
   133         lyButton.addStretch(1)
   134         btnOk = QtGui.QPushButton('&Ok', self)
   135         btnOk.setDefault(True)
   136         btnOk.setMinimumWidth(100)
   137         lyButton.addWidget(btnOk)
   138         btnAbout = QtGui.QPushButton('&About', self)
   139         btnAbout.setMinimumWidth(100)
   140         lyButton.addWidget(btnAbout)
   141         
   142         button_width = max(btnOk.width(), btnAbout.width())
   143         btnOk.setMinimumWidth(button_width)
   144         btnAbout.setMinimumWidth(button_width)
   145         
   146         # reduce to the max
   147         self.resize(lyMain.minimumSize())
   148         
   149         # connectors
   150         btnOk.clicked.connect(self.clicked_ok)
   151         btnAbout.clicked.connect(self.clicked_about)