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