2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the user should give us a password
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
32 # ------------------------------------------------------------
37 if sys.platform == 'win32' or sys.platform == 'cygwin':
42 from PyQt4 import QtCore
43 from PyQt4 import QtGui
45 from ui_PasswordDialog import Ui_PasswordDialog
46 from about_dialog import AboutDialog
49 # ------------------------------------------------------------
53 class PasswordDialog(QtGui.QDialog):
55 """A dialog for letting the user type in a password"""
57 def __init__(self, user_text = 'Please provide an approbitate password to proceed:'):
59 QtGui.QDialog.__init__(self)
61 # setup the user interface
62 self.ui = Ui_PasswordDialog()
66 self._about_dialog = AboutDialog()
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)
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)
79 t = QtCore.QTimer(self)
80 t.timeout.connect(self.force_foreground)
86 def clicked_about(self):
88 """About button has been clicked."""
89 self._about_dialog.show()
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')
103 def force_foreground(self):
105 """Force ourselves into foreground"""
106 if sys.platform == 'win32' or sys.platform == 'cygwin':
108 while not w.nativeParentWidget() is None:
109 w = w.nativeParentWidget()
110 win32gui.BringWindowToTop(int(w.effectiveWinId()))
111 win32gui.SetForegroundWindow(int(w.effectiveWinId()))
114 def set_user_text(user_text):
116 """Set a text to explain which password we need."""
117 self.ui.lblText.setText(user_text)
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)