2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the user should give us a password
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
18 # https://www.x-net.at
20 # AIT Austrian Institute of Technology
21 # Donau City Strasse 1
24 # http://www.ait.ac.at
27 # Licensed under the Apache License, Version 2.0 (the "License");
28 # you may not use this file except in compliance with the License.
29 # You may obtain a copy of the License at
31 # http://www.apache.org/licenses/LICENSE-2.0
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS,
35 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 # See the License for the specific language governing permissions and
37 # limitations under the License.
38 # ------------------------------------------------------------
41 # ------------------------------------------------------------
46 if sys.platform == 'win32' or sys.platform == 'cygwin':
51 from PyQt4 import QtCore
52 from PyQt4 import QtGui
54 from ui_PasswordDialog import Ui_PasswordDialog
55 from about_dialog import AboutDialog
58 # ------------------------------------------------------------
62 class PasswordDialog(QtGui.QDialog):
64 """A dialog for letting the user type in a password"""
66 def __init__(self, user_text = 'Please provide an approbitate password to proceed:'):
68 QtGui.QDialog.__init__(self)
70 # setup the user interface
71 self.ui = Ui_PasswordDialog()
75 self._about_dialog = AboutDialog()
78 self.ui.lblText.setText(user_text)
79 self.ui.btnAbout.clicked.connect(self.clicked_about)
80 self.ui.btnCancel.clicked.connect(self.reject)
81 self.ui.btnOk.clicked.connect(self.clicked_ok)
83 # positionate ourself central
84 screen = QtGui.QDesktopWidget().screenGeometry()
85 size = self.geometry()
86 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
88 t = QtCore.QTimer(self)
89 t.timeout.connect(self.force_foreground)
95 def clicked_about(self):
97 """About button has been clicked."""
98 self._about_dialog.show()
101 def clicked_ok(self):
103 """Ok button has been clicked."""
104 sys.stdout.write('{ ')
105 sys.stdout.write('"password": "')
106 sys.stdout.write(self.ui.edtPassword.text())
107 sys.stdout.write('" ')
108 sys.stdout.write('}\n')
112 def force_foreground(self):
114 """Force ourselves into foreground"""
115 if sys.platform == 'win32' or sys.platform == 'cygwin':
117 while not w.nativeParentWidget() is None:
118 w = w.nativeParentWidget()
119 win32gui.BringWindowToTop(int(w.effectiveWinId()))
120 win32gui.SetForegroundWindow(int(w.effectiveWinId()))
123 def set_user_text(user_text):
125 """Set a text to explain which password we need."""
126 self.ui.lblText.setText(user_text)
129 if __name__ == "__main__":
130 if sys.platform == 'win32' or sys.platform == 'cygwin':
131 win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
132 a = QtGui.QApplication(sys.argv)