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