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