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