OpenSecurity/bin/ui/password_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Thu, 02 Oct 2014 13:18:22 +0200
changeset 232 56120b285fc8
parent 134 f1c1c06c947d
child 236 8bfcfd4dd6ad
permissions -rwxr-xr-x
Bring Password, KeyFile and Credentials-Dialog to Windows front
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # password_dialog.pyw
     6 # 
     7 # the user should give us a password
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright (C) 2014 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 if sys.platform == 'win32' or sys.platform == 'cygwin':
    38     import win32api
    39     import win32con
    40     import win32gui
    41 
    42 from PyQt4 import QtCore
    43 from PyQt4 import QtGui
    44 
    45 from ui_PasswordDialog import Ui_PasswordDialog 
    46 from about_dialog import AboutDialog
    47 
    48 
    49 # ------------------------------------------------------------
    50 # code
    51 
    52 
    53 class PasswordDialog(QtGui.QDialog):
    54 
    55     """A dialog for letting the user type in a password"""
    56 
    57     def __init__(self, user_text = 'Please provide an approbitate password to proceed:'):
    58 
    59         QtGui.QDialog.__init__(self)
    60 
    61         # setup the user interface
    62         self.ui = Ui_PasswordDialog()
    63         self.ui.setupUi(self)
    64     
    65         # local members
    66         self._about_dialog = AboutDialog()
    67 
    68         # connectors
    69         self.ui.lblText.setText(user_text)
    70         self.ui.btnAbout.clicked.connect(self.clicked_about)
    71         self.ui.btnCancel.clicked.connect(self.reject)
    72         self.ui.btnOk.clicked.connect(self.clicked_ok)
    73 
    74         # positionate ourself central
    75         screen = QtGui.QDesktopWidget().screenGeometry()
    76         size = self.geometry()
    77         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    78 
    79         t = QtCore.QTimer(self)
    80         t.timeout.connect(self.force_foreground)
    81         t.setInterval(0)
    82         t.setSingleShot(True)
    83         t.start()
    84  
    85 
    86     def clicked_about(self):
    87 
    88         """About button has been clicked."""
    89         self._about_dialog.show()
    90 
    91 
    92     def clicked_ok(self):
    93         
    94         """Ok button has been clicked."""
    95         sys.stdout.write('{ ')
    96         sys.stdout.write('"password": "')
    97         sys.stdout.write(self.ui.edtPassword.text())
    98         sys.stdout.write('" ')
    99         sys.stdout.write('}\n')
   100         self.accept()
   101 
   102 
   103     def force_foreground(self):
   104 
   105         """Force ourselves into foreground"""
   106         if sys.platform == 'win32' or sys.platform == 'cygwin':
   107             w = self
   108             while not w.nativeParentWidget() is None:
   109                 w = w.nativeParentWidget()
   110             win32gui.BringWindowToTop(int(w.effectiveWinId()))
   111             win32gui.SetForegroundWindow(int(w.effectiveWinId()))
   112 
   113 
   114     def set_user_text(user_text):
   115 
   116         """Set a text to explain which password we need."""
   117         self.ui.lblText.setText(user_text)
   118 
   119 
   120 if __name__ == "__main__":
   121     if sys.platform == 'win32' or sys.platform == 'cygwin':
   122         win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
   123     a = QtGui.QApplication(sys.argv)
   124     d = PasswordDialog()
   125     d.setWindowModality(QtCore.Qt.ApplicationModal)
   126     d.show()
   127     sys.exit(a.exec_())     
   128