oliver@100
|
1 |
#!/bin/env python
|
oliver@100
|
2 |
# -*- coding: utf-8 -*-
|
oliver@100
|
3 |
|
oliver@100
|
4 |
# ------------------------------------------------------------
|
oliver@100
|
5 |
# credentials_dialog.pyw
|
oliver@100
|
6 |
#
|
oliver@100
|
7 |
# the user should give us some user/pass info
|
oliver@100
|
8 |
#
|
oliver@100
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
oliver@100
|
10 |
#
|
oliver@240
|
11 |
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
|
oliver@100
|
12 |
#
|
oliver@100
|
13 |
#
|
oliver@240
|
14 |
# X-Net Services GmbH
|
oliver@240
|
15 |
# Elisabethstrasse 1
|
oliver@240
|
16 |
# 4020 Linz
|
oliver@240
|
17 |
# AUSTRIA
|
oliver@240
|
18 |
# https://www.x-net.at
|
oliver@240
|
19 |
#
|
oliver@240
|
20 |
# AIT Austrian Institute of Technology
|
oliver@240
|
21 |
# Donau City Strasse 1
|
oliver@240
|
22 |
# 1220 Wien
|
oliver@240
|
23 |
# AUSTRIA
|
oliver@240
|
24 |
# http://www.ait.ac.at
|
oliver@240
|
25 |
#
|
oliver@240
|
26 |
#
|
oliver@240
|
27 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
oliver@240
|
28 |
# you may not use this file except in compliance with the License.
|
oliver@240
|
29 |
# You may obtain a copy of the License at
|
oliver@240
|
30 |
#
|
oliver@240
|
31 |
# http://www.apache.org/licenses/LICENSE-2.0
|
oliver@240
|
32 |
#
|
oliver@240
|
33 |
# Unless required by applicable law or agreed to in writing, software
|
oliver@240
|
34 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
oliver@240
|
35 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
oliver@240
|
36 |
# See the License for the specific language governing permissions and
|
oliver@240
|
37 |
# limitations under the License.
|
oliver@100
|
38 |
# ------------------------------------------------------------
|
oliver@100
|
39 |
|
oliver@100
|
40 |
|
oliver@100
|
41 |
# ------------------------------------------------------------
|
oliver@100
|
42 |
# imports
|
oliver@100
|
43 |
|
oliver@100
|
44 |
import sys
|
oliver@100
|
45 |
|
oliver@232
|
46 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
47 |
import win32api
|
oliver@232
|
48 |
import win32con
|
oliver@232
|
49 |
import win32gui
|
oliver@232
|
50 |
|
oliver@100
|
51 |
from PyQt4 import QtCore
|
oliver@100
|
52 |
from PyQt4 import QtGui
|
oliver@100
|
53 |
|
oliver@100
|
54 |
from ui_CredentialsDialog import Ui_CredentialsDialog
|
oliver@100
|
55 |
from about_dialog import AboutDialog
|
oliver@100
|
56 |
|
oliver@100
|
57 |
|
oliver@100
|
58 |
# ------------------------------------------------------------
|
oliver@100
|
59 |
# code
|
oliver@100
|
60 |
|
oliver@100
|
61 |
|
oliver@100
|
62 |
class CredentialsDialog(QtGui.QDialog):
|
oliver@100
|
63 |
|
oliver@100
|
64 |
"""A dialog for letting the user pass on a user/password combo"""
|
oliver@100
|
65 |
|
oliver@100
|
66 |
def __init__(self, user_text = 'Please provide approbitate login information:'):
|
oliver@100
|
67 |
|
oliver@100
|
68 |
QtGui.QDialog.__init__(self)
|
oliver@100
|
69 |
|
oliver@100
|
70 |
# setup the user interface
|
oliver@100
|
71 |
self.ui = Ui_CredentialsDialog()
|
oliver@100
|
72 |
self.ui.setupUi(self)
|
oliver@100
|
73 |
|
oliver@100
|
74 |
# local members
|
oliver@100
|
75 |
self._about_dialog = AboutDialog()
|
oliver@100
|
76 |
|
oliver@100
|
77 |
# connectors
|
oliver@100
|
78 |
self.ui.lblText.setText(user_text)
|
oliver@100
|
79 |
self.ui.btnAbout.clicked.connect(self.clicked_about)
|
oliver@100
|
80 |
self.ui.btnCancel.clicked.connect(self.reject)
|
oliver@100
|
81 |
self.ui.btnOk.clicked.connect(self.clicked_ok)
|
oliver@100
|
82 |
|
oliver@101
|
83 |
# positionate ourself central
|
oliver@101
|
84 |
screen = QtGui.QDesktopWidget().screenGeometry()
|
oliver@101
|
85 |
size = self.geometry()
|
oliver@101
|
86 |
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
|
oliver@101
|
87 |
|
oliver@232
|
88 |
t = QtCore.QTimer(self)
|
oliver@232
|
89 |
t.timeout.connect(self.force_foreground)
|
oliver@232
|
90 |
t.setInterval(0)
|
oliver@232
|
91 |
t.setSingleShot(True)
|
oliver@232
|
92 |
t.start()
|
oliver@232
|
93 |
|
oliver@100
|
94 |
|
oliver@100
|
95 |
def clicked_about(self):
|
oliver@100
|
96 |
|
oliver@100
|
97 |
"""About button has been clicked."""
|
oliver@100
|
98 |
self._about_dialog.show()
|
oliver@100
|
99 |
|
oliver@100
|
100 |
|
oliver@100
|
101 |
def clicked_ok(self):
|
oliver@100
|
102 |
|
oliver@100
|
103 |
"""Ok button has been clicked."""
|
oliver@100
|
104 |
sys.stdout.write('{ ')
|
oliver@134
|
105 |
sys.stdout.write('"user": "')
|
oliver@100
|
106 |
sys.stdout.write(self.ui.edtUser.text())
|
oliver@134
|
107 |
sys.stdout.write('", ')
|
oliver@134
|
108 |
sys.stdout.write('"password": "')
|
oliver@100
|
109 |
sys.stdout.write(self.ui.edtPassword.text())
|
oliver@134
|
110 |
sys.stdout.write('" ')
|
oliver@100
|
111 |
sys.stdout.write('}\n')
|
oliver@100
|
112 |
self.accept()
|
oliver@100
|
113 |
|
oliver@100
|
114 |
|
oliver@232
|
115 |
def force_foreground(self):
|
oliver@232
|
116 |
|
oliver@232
|
117 |
"""Force ourselves into foreground"""
|
oliver@232
|
118 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
119 |
w = self
|
oliver@232
|
120 |
while not w.nativeParentWidget() is None:
|
oliver@232
|
121 |
w = w.nativeParentWidget()
|
oliver@232
|
122 |
win32gui.BringWindowToTop(int(w.effectiveWinId()))
|
oliver@232
|
123 |
win32gui.SetForegroundWindow(int(w.effectiveWinId()))
|
oliver@232
|
124 |
|
oliver@232
|
125 |
|
oliver@100
|
126 |
def set_user_text(user_text):
|
oliver@100
|
127 |
|
oliver@100
|
128 |
"""Set a text to explain which credentials we need."""
|
oliver@100
|
129 |
self.ui.lblText.setText(user_text)
|
oliver@100
|
130 |
|
oliver@100
|
131 |
|
oliver@100
|
132 |
if __name__ == "__main__":
|
oliver@232
|
133 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
134 |
win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
|
oliver@113
|
135 |
a = QtGui.QApplication(sys.argv)
|
oliver@113
|
136 |
d = CredentialsDialog()
|
oliver@113
|
137 |
d.show()
|
oliver@113
|
138 |
sys.exit(a.exec_())
|
oliver@100
|
139 |
|