oliver@101
|
1 |
#!/bin/env python
|
oliver@101
|
2 |
# -*- coding: utf-8 -*-
|
oliver@101
|
3 |
|
oliver@101
|
4 |
# ------------------------------------------------------------
|
oliver@101
|
5 |
# password_dialog.pyw
|
oliver@101
|
6 |
#
|
oliver@101
|
7 |
# the user should give us a password
|
oliver@101
|
8 |
#
|
oliver@101
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
oliver@101
|
10 |
#
|
oliver@101
|
11 |
# Copyright (C) 2014 AIT Austrian Institute of Technology
|
oliver@101
|
12 |
# AIT Austrian Institute of Technology GmbH
|
oliver@101
|
13 |
# Donau-City-Strasse 1 | 1220 Vienna | Austria
|
oliver@101
|
14 |
# http://www.ait.ac.at
|
oliver@101
|
15 |
#
|
oliver@101
|
16 |
# This program is free software; you can redistribute it and/or
|
oliver@101
|
17 |
# modify it under the terms of the GNU General Public License
|
oliver@101
|
18 |
# as published by the Free Software Foundation version 2.
|
oliver@101
|
19 |
#
|
oliver@101
|
20 |
# This program is distributed in the hope that it will be useful,
|
oliver@101
|
21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
oliver@101
|
22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
oliver@101
|
23 |
# GNU General Public License for more details.
|
oliver@101
|
24 |
#
|
oliver@101
|
25 |
# You should have received a copy of the GNU General Public License
|
oliver@101
|
26 |
# along with this program; if not, write to the Free Software
|
oliver@101
|
27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
oliver@101
|
28 |
# Boston, MA 02110-1301, USA.
|
oliver@101
|
29 |
# ------------------------------------------------------------
|
oliver@101
|
30 |
|
oliver@101
|
31 |
|
oliver@101
|
32 |
# ------------------------------------------------------------
|
oliver@101
|
33 |
# imports
|
oliver@101
|
34 |
|
oliver@101
|
35 |
import sys
|
oliver@101
|
36 |
|
oliver@232
|
37 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
38 |
import win32api
|
oliver@232
|
39 |
import win32con
|
oliver@232
|
40 |
import win32gui
|
oliver@232
|
41 |
|
oliver@101
|
42 |
from PyQt4 import QtCore
|
oliver@101
|
43 |
from PyQt4 import QtGui
|
oliver@101
|
44 |
|
oliver@101
|
45 |
from ui_PasswordDialog import Ui_PasswordDialog
|
oliver@101
|
46 |
from about_dialog import AboutDialog
|
oliver@101
|
47 |
|
oliver@101
|
48 |
|
oliver@101
|
49 |
# ------------------------------------------------------------
|
oliver@101
|
50 |
# code
|
oliver@101
|
51 |
|
oliver@101
|
52 |
|
oliver@101
|
53 |
class PasswordDialog(QtGui.QDialog):
|
oliver@101
|
54 |
|
oliver@134
|
55 |
"""A dialog for letting the user type in a password"""
|
oliver@101
|
56 |
|
oliver@101
|
57 |
def __init__(self, user_text = 'Please provide an approbitate password to proceed:'):
|
oliver@101
|
58 |
|
oliver@101
|
59 |
QtGui.QDialog.__init__(self)
|
oliver@101
|
60 |
|
oliver@101
|
61 |
# setup the user interface
|
oliver@101
|
62 |
self.ui = Ui_PasswordDialog()
|
oliver@101
|
63 |
self.ui.setupUi(self)
|
oliver@101
|
64 |
|
oliver@101
|
65 |
# local members
|
oliver@101
|
66 |
self._about_dialog = AboutDialog()
|
oliver@101
|
67 |
|
oliver@101
|
68 |
# connectors
|
oliver@101
|
69 |
self.ui.lblText.setText(user_text)
|
oliver@101
|
70 |
self.ui.btnAbout.clicked.connect(self.clicked_about)
|
oliver@101
|
71 |
self.ui.btnCancel.clicked.connect(self.reject)
|
oliver@101
|
72 |
self.ui.btnOk.clicked.connect(self.clicked_ok)
|
oliver@101
|
73 |
|
oliver@101
|
74 |
# positionate ourself central
|
oliver@101
|
75 |
screen = QtGui.QDesktopWidget().screenGeometry()
|
oliver@101
|
76 |
size = self.geometry()
|
oliver@101
|
77 |
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
|
oliver@232
|
78 |
|
oliver@232
|
79 |
t = QtCore.QTimer(self)
|
oliver@232
|
80 |
t.timeout.connect(self.force_foreground)
|
oliver@232
|
81 |
t.setInterval(0)
|
oliver@232
|
82 |
t.setSingleShot(True)
|
oliver@232
|
83 |
t.start()
|
oliver@101
|
84 |
|
oliver@101
|
85 |
|
oliver@101
|
86 |
def clicked_about(self):
|
oliver@101
|
87 |
|
oliver@101
|
88 |
"""About button has been clicked."""
|
oliver@101
|
89 |
self._about_dialog.show()
|
oliver@101
|
90 |
|
oliver@101
|
91 |
|
oliver@101
|
92 |
def clicked_ok(self):
|
oliver@101
|
93 |
|
oliver@101
|
94 |
"""Ok button has been clicked."""
|
oliver@101
|
95 |
sys.stdout.write('{ ')
|
oliver@134
|
96 |
sys.stdout.write('"password": "')
|
oliver@101
|
97 |
sys.stdout.write(self.ui.edtPassword.text())
|
oliver@134
|
98 |
sys.stdout.write('" ')
|
oliver@101
|
99 |
sys.stdout.write('}\n')
|
oliver@101
|
100 |
self.accept()
|
oliver@101
|
101 |
|
oliver@101
|
102 |
|
oliver@232
|
103 |
def force_foreground(self):
|
oliver@232
|
104 |
|
oliver@232
|
105 |
"""Force ourselves into foreground"""
|
oliver@232
|
106 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
107 |
w = self
|
oliver@232
|
108 |
while not w.nativeParentWidget() is None:
|
oliver@232
|
109 |
w = w.nativeParentWidget()
|
oliver@232
|
110 |
win32gui.BringWindowToTop(int(w.effectiveWinId()))
|
oliver@232
|
111 |
win32gui.SetForegroundWindow(int(w.effectiveWinId()))
|
oliver@232
|
112 |
|
oliver@232
|
113 |
|
oliver@101
|
114 |
def set_user_text(user_text):
|
oliver@101
|
115 |
|
oliver@101
|
116 |
"""Set a text to explain which password we need."""
|
oliver@101
|
117 |
self.ui.lblText.setText(user_text)
|
oliver@101
|
118 |
|
oliver@101
|
119 |
|
oliver@101
|
120 |
if __name__ == "__main__":
|
oliver@232
|
121 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
122 |
win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
|
oliver@113
|
123 |
a = QtGui.QApplication(sys.argv)
|
oliver@113
|
124 |
d = PasswordDialog()
|
oliver@232
|
125 |
d.setWindowModality(QtCore.Qt.ApplicationModal)
|
oliver@113
|
126 |
d.show()
|
oliver@113
|
127 |
sys.exit(a.exec_())
|
oliver@101
|
128 |
|