OpenSecurity/bin/notification.py
changeset 29 3f564e1673bb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/notification.py	Mon Dec 09 14:44:41 2013 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# notification-dialog
     1.9 +# 
    1.10 +# show the user an opensecurity specific message box
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import sys
    1.39 +
    1.40 +from PyQt4 import QtCore
    1.41 +from PyQt4 import QtGui
    1.42 +
    1.43 +# local
    1.44 +from about import About
    1.45 +
    1.46 +
    1.47 +# ------------------------------------------------------------
    1.48 +# code
    1.49 +
    1.50 +class Notification(QtGui.QDialog):
    1.51 +    
    1.52 +    """Show the user an OpenSecurity specific notification."""
    1.53 +    
    1.54 +    TYPES = ['information', 'warning', 'critical']
    1.55 +        
    1.56 +    def __init__(self, msgtype, text, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    1.57 +        
    1.58 +        # super call and widget init
    1.59 +        super(Notification, self).__init__(parent, flags)
    1.60 +        self.setWindowTitle('OpenSecuirty Notification')
    1.61 +        self.setup_ui(msgtype)
    1.62 +        
    1.63 +        # positionate ourself central
    1.64 +        screen = QtGui.QDesktopWidget().screenGeometry()
    1.65 +        self.resize(self.geometry().width() * 1.25, self.geometry().height())
    1.66 +        size = self.geometry()
    1.67 +        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    1.68 +        
    1.69 +        # fix up text
    1.70 +        if msgtype in Notification.TYPES:
    1.71 +            
    1.72 +            typetext = { 
    1.73 +                'information': 'OpenSecurity information', 
    1.74 +                'warning': '<b>OpenSecurity warning</b>', 
    1.75 +                'critical': '<b>OpenSecurity critical error</b>'
    1.76 +            }
    1.77 +            self.lbMsgType.setText(typetext[msgtype])
    1.78 +            
    1.79 +            captiontext = { 
    1.80 +                'information': 'OpenSecurity: Information', 
    1.81 +                'warning': 'OpenSecurity: Warning', 
    1.82 +                'critical': 'OpenSecurity: Critical'
    1.83 +            }
    1.84 +            self.setWindowTitle(captiontext[msgtype])
    1.85 +            
    1.86 +        else:
    1.87 +            raise ValueError('unknown msgtype')
    1.88 +        
    1.89 +        self.lbText.setText(text)
    1.90 +        
    1.91 +
    1.92 +    def clicked_about(self):
    1.93 +        """clicked the about button"""
    1.94 +        dlgAbout = About()
    1.95 +        dlgAbout.exec_()
    1.96 +    
    1.97 +
    1.98 +    def clicked_ok(self):
    1.99 +        """clicked the ok button"""
   1.100 +        self.accept()
   1.101 +    
   1.102 +
   1.103 +    def setup_ui(self, msgtype):
   1.104 +        
   1.105 +        """Create the widgets."""
   1.106 +        
   1.107 +        lyMain = QtGui.QVBoxLayout(self)
   1.108 +        lyMain.setContentsMargins(8, 8, 8, 8)
   1.109 +        
   1.110 +        # content area: left pixmap, right text
   1.111 +        lyContent = QtGui.QHBoxLayout()
   1.112 +        lyMain.addLayout(lyContent)
   1.113 +        
   1.114 +        # pixmap
   1.115 +        lbPix = QtGui.QLabel()
   1.116 +        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   1.117 +        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   1.118 +        lyContent.addSpacing(16)
   1.119 +        
   1.120 +        # text ...
   1.121 +        lyText = QtGui.QVBoxLayout()
   1.122 +        lyContent.addLayout(lyText, 1)
   1.123 +        
   1.124 +        self.lbMsgType = QtGui.QLabel()
   1.125 +        lyText.addWidget(self.lbMsgType)
   1.126 +        self.lbText = QtGui.QLabel()
   1.127 +        lyText.addWidget(self.lbText)
   1.128 +        lyText.addStretch(1)
   1.129 +        
   1.130 +        lyMain.addStretch(1)
   1.131 +        
   1.132 +        # buttons
   1.133 +        lyButton = QtGui.QHBoxLayout()
   1.134 +        lyMain.addLayout(lyButton)
   1.135 +        
   1.136 +        lyButton.addStretch(1)
   1.137 +        btnOk = QtGui.QPushButton('&Ok', self)
   1.138 +        btnOk.setDefault(True)
   1.139 +        btnOk.setMinimumWidth(100)
   1.140 +        lyButton.addWidget(btnOk)
   1.141 +        btnAbout = QtGui.QPushButton('&About', self)
   1.142 +        btnAbout.setMinimumWidth(100)
   1.143 +        lyButton.addWidget(btnAbout)
   1.144 +        
   1.145 +        button_width = max(btnOk.width(), btnAbout.width())
   1.146 +        btnOk.setMinimumWidth(button_width)
   1.147 +        btnAbout.setMinimumWidth(button_width)
   1.148 +        
   1.149 +        # reduce to the max
   1.150 +        self.resize(lyMain.minimumSize())
   1.151 +        
   1.152 +        # connectors
   1.153 +        btnOk.clicked.connect(self.clicked_ok)
   1.154 +        btnAbout.clicked.connect(self.clicked_about)