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
om@29
     1
#!/bin/env python
om@29
     2
# -*- coding: utf-8 -*-
om@29
     3
om@29
     4
# ------------------------------------------------------------
om@29
     5
# notification-dialog
om@29
     6
# 
om@29
     7
# show the user an opensecurity specific message box
om@29
     8
#
om@29
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@29
    10
#
om@29
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@29
    12
# AIT Austrian Institute of Technology GmbH
om@29
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@29
    14
# http://www.ait.ac.at
om@29
    15
#
om@29
    16
# This program is free software; you can redistribute it and/or
om@29
    17
# modify it under the terms of the GNU General Public License
om@29
    18
# as published by the Free Software Foundation version 2.
om@29
    19
# 
om@29
    20
# This program is distributed in the hope that it will be useful,
om@29
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@29
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@29
    23
# GNU General Public License for more details.
om@29
    24
# 
om@29
    25
# You should have received a copy of the GNU General Public License
om@29
    26
# along with this program; if not, write to the Free Software
om@29
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@29
    28
# Boston, MA  02110-1301, USA.
om@29
    29
# ------------------------------------------------------------
om@29
    30
om@29
    31
om@29
    32
# ------------------------------------------------------------
om@29
    33
# imports
om@29
    34
om@29
    35
import sys
om@29
    36
om@29
    37
from PyQt4 import QtCore
om@29
    38
from PyQt4 import QtGui
om@29
    39
om@29
    40
# local
om@29
    41
from about import About
om@29
    42
om@29
    43
om@29
    44
# ------------------------------------------------------------
om@29
    45
# code
om@29
    46
om@29
    47
class Notification(QtGui.QDialog):
om@29
    48
    
om@29
    49
    """Show the user an OpenSecurity specific notification."""
om@29
    50
    
om@29
    51
    TYPES = ['information', 'warning', 'critical']
om@29
    52
        
om@29
    53
    def __init__(self, msgtype, text, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@29
    54
        
om@29
    55
        # super call and widget init
om@29
    56
        super(Notification, self).__init__(parent, flags)
om@29
    57
        self.setWindowTitle('OpenSecuirty Notification')
om@29
    58
        self.setup_ui(msgtype)
om@29
    59
        
om@29
    60
        # positionate ourself central
om@29
    61
        screen = QtGui.QDesktopWidget().screenGeometry()
om@29
    62
        self.resize(self.geometry().width() * 1.25, self.geometry().height())
om@29
    63
        size = self.geometry()
om@29
    64
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
om@29
    65
        
om@29
    66
        # fix up text
om@29
    67
        if msgtype in Notification.TYPES:
om@29
    68
            
om@29
    69
            typetext = { 
om@29
    70
                'information': 'OpenSecurity information', 
om@29
    71
                'warning': '<b>OpenSecurity warning</b>', 
om@29
    72
                'critical': '<b>OpenSecurity critical error</b>'
om@29
    73
            }
om@29
    74
            self.lbMsgType.setText(typetext[msgtype])
om@29
    75
            
om@29
    76
            captiontext = { 
om@29
    77
                'information': 'OpenSecurity: Information', 
om@29
    78
                'warning': 'OpenSecurity: Warning', 
om@29
    79
                'critical': 'OpenSecurity: Critical'
om@29
    80
            }
om@29
    81
            self.setWindowTitle(captiontext[msgtype])
om@29
    82
            
om@29
    83
        else:
om@29
    84
            raise ValueError('unknown msgtype')
om@29
    85
        
om@29
    86
        self.lbText.setText(text)
om@29
    87
        
om@29
    88
om@29
    89
    def clicked_about(self):
om@29
    90
        """clicked the about button"""
om@29
    91
        dlgAbout = About()
om@29
    92
        dlgAbout.exec_()
om@29
    93
    
om@29
    94
om@29
    95
    def clicked_ok(self):
om@29
    96
        """clicked the ok button"""
om@29
    97
        self.accept()
om@29
    98
    
om@29
    99
om@29
   100
    def setup_ui(self, msgtype):
om@29
   101
        
om@29
   102
        """Create the widgets."""
om@29
   103
        
om@29
   104
        lyMain = QtGui.QVBoxLayout(self)
om@29
   105
        lyMain.setContentsMargins(8, 8, 8, 8)
om@29
   106
        
om@29
   107
        # content area: left pixmap, right text
om@29
   108
        lyContent = QtGui.QHBoxLayout()
om@29
   109
        lyMain.addLayout(lyContent)
om@29
   110
        
om@29
   111
        # pixmap
om@29
   112
        lbPix = QtGui.QLabel()
om@29
   113
        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
om@29
   114
        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
om@29
   115
        lyContent.addSpacing(16)
om@29
   116
        
om@29
   117
        # text ...
om@29
   118
        lyText = QtGui.QVBoxLayout()
om@29
   119
        lyContent.addLayout(lyText, 1)
om@29
   120
        
om@29
   121
        self.lbMsgType = QtGui.QLabel()
om@29
   122
        lyText.addWidget(self.lbMsgType)
om@29
   123
        self.lbText = QtGui.QLabel()
om@29
   124
        lyText.addWidget(self.lbText)
om@29
   125
        lyText.addStretch(1)
om@29
   126
        
om@29
   127
        lyMain.addStretch(1)
om@29
   128
        
om@29
   129
        # buttons
om@29
   130
        lyButton = QtGui.QHBoxLayout()
om@29
   131
        lyMain.addLayout(lyButton)
om@29
   132
        
om@29
   133
        lyButton.addStretch(1)
om@29
   134
        btnOk = QtGui.QPushButton('&Ok', self)
om@29
   135
        btnOk.setDefault(True)
om@29
   136
        btnOk.setMinimumWidth(100)
om@29
   137
        lyButton.addWidget(btnOk)
om@29
   138
        btnAbout = QtGui.QPushButton('&About', self)
om@29
   139
        btnAbout.setMinimumWidth(100)
om@29
   140
        lyButton.addWidget(btnAbout)
om@29
   141
        
om@29
   142
        button_width = max(btnOk.width(), btnAbout.width())
om@29
   143
        btnOk.setMinimumWidth(button_width)
om@29
   144
        btnAbout.setMinimumWidth(button_width)
om@29
   145
        
om@29
   146
        # reduce to the max
om@29
   147
        self.resize(lyMain.minimumSize())
om@29
   148
        
om@29
   149
        # connectors
om@29
   150
        btnOk.clicked.connect(self.clicked_ok)
om@29
   151
        btnAbout.clicked.connect(self.clicked_about)