ait/os/bin/opensecurityd/password.py
author om
Tue, 12 Nov 2013 11:31:34 +0100
branchom
changeset 2 c9bf2537109a
permissions -rwxr-xr-x
added C/C++ and Python sources
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # password-dialog
     6 # 
     7 # ask the user a password
     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 # code
    45 
    46 
    47 class Password(QtGui.QDialog):
    48     
    49     """Ask the user for a password."""
    50     
    51     def __init__(self, text, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    52         
    53         # super call and widget init
    54         super(Password, self).__init__(parent, flags)
    55         self.setWindowTitle('OpenSecuirty Password Request')
    56         self.setup_ui()
    57         
    58         # positionate ourself central
    59         screen = QtGui.QDesktopWidget().screenGeometry()
    60         self.resize(self.geometry().width() * 1.25, self.geometry().height())
    61         size = self.geometry()
    62         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    63         
    64         # fix up text
    65         self.lbText.setText(text)
    66         
    67 
    68     def clicked_about(self):
    69         """clicked the about button"""
    70         dlgAbout = About()
    71         dlgAbout.exec_()
    72     
    73 
    74     def clicked_cancel(self):
    75         """clicked the cancel button"""
    76         self.reject()
    77     
    78 
    79     def clicked_ok(self):
    80         """clicked the ok button"""
    81         sys.stdout.write('{ ')
    82         sys.stdout.write('\'password\': \'')
    83         sys.stdout.write(self.edPassword.text())
    84         sys.stdout.write('\' ')
    85         sys.stdout.write('}\n')
    86         self.accept()
    87     
    88 
    89     def setup_ui(self):
    90         
    91         """Create the widgets."""
    92         
    93         lyMain = QtGui.QVBoxLayout(self)
    94         lyMain.setContentsMargins(8, 8, 8, 8)
    95         
    96         # content area: left pixmap, right text
    97         lyContent = QtGui.QHBoxLayout()
    98         lyMain.addLayout(lyContent)
    99         
   100         # pixmap
   101         lbPix = QtGui.QLabel()
   102         lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   103         lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   104         lyContent.addSpacing(16)
   105         
   106         # text ...
   107         lyText = QtGui.QVBoxLayout()
   108         lyContent.addLayout(lyText)
   109         self.lbText = QtGui.QLabel()
   110         lyText.addWidget(self.lbText)
   111         lyPassword = QtGui.QHBoxLayout()
   112         lyText.addLayout(lyPassword)
   113         lbPassword = QtGui.QLabel('&Password:')
   114         lyPassword.addWidget(lbPassword)
   115         self.edPassword = QtGui.QLineEdit()
   116         self.edPassword.setEchoMode(QtGui.QLineEdit.Password)
   117         lyPassword.addWidget(self.edPassword)
   118         lbPassword.setBuddy(self.edPassword)
   119         lyText.addStretch(1)
   120         
   121         lyMain.addStretch(1)
   122         
   123         # buttons
   124         lyButton = QtGui.QHBoxLayout()
   125         lyMain.addLayout(lyButton)
   126         
   127         lyButton.addStretch(1)
   128         btnOk = QtGui.QPushButton('&Ok', self)
   129         btnOk.setDefault(True)
   130         btnOk.setMinimumWidth(100)
   131         lyButton.addWidget(btnOk)
   132         btnCancel = QtGui.QPushButton('&Cancel', self)
   133         btnCancel.setMinimumWidth(100)
   134         lyButton.addWidget(btnCancel)
   135         btnAbout = QtGui.QPushButton('&About', self)
   136         btnAbout.setMinimumWidth(100)
   137         lyButton.addWidget(btnAbout)
   138         
   139         button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
   140         btnOk.setMinimumWidth(button_width)
   141         btnCancel.setMinimumWidth(button_width)
   142         btnAbout.setMinimumWidth(button_width)
   143         
   144         # reduce to the max
   145         self.resize(lyMain.minimumSize())
   146         
   147         # connectors
   148         btnOk.clicked.connect(self.clicked_ok)
   149         btnCancel.clicked.connect(self.clicked_cancel)
   150         btnAbout.clicked.connect(self.clicked_about)