oliver@134
|
1 |
#!/bin/env python
|
oliver@134
|
2 |
# -*- coding: utf-8 -*-
|
oliver@134
|
3 |
|
oliver@134
|
4 |
# ------------------------------------------------------------
|
oliver@134
|
5 |
# keyfile_dialog.pyw
|
oliver@134
|
6 |
#
|
oliver@134
|
7 |
# the user should give us a keyfile
|
oliver@134
|
8 |
#
|
oliver@134
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
oliver@134
|
10 |
#
|
oliver@240
|
11 |
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
|
oliver@134
|
12 |
#
|
oliver@134
|
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@134
|
38 |
# ------------------------------------------------------------
|
oliver@134
|
39 |
|
oliver@134
|
40 |
|
oliver@134
|
41 |
# ------------------------------------------------------------
|
oliver@134
|
42 |
# imports
|
oliver@134
|
43 |
|
oliver@134
|
44 |
import base64
|
oliver@134
|
45 |
import sys
|
oliver@134
|
46 |
|
oliver@232
|
47 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
48 |
import win32api
|
oliver@232
|
49 |
import win32con
|
oliver@232
|
50 |
import win32gui
|
oliver@232
|
51 |
|
oliver@134
|
52 |
from PyQt4 import QtCore
|
oliver@134
|
53 |
from PyQt4 import QtGui
|
oliver@134
|
54 |
|
oliver@134
|
55 |
from ui_KeyfileDialog import Ui_KeyfileDialog
|
oliver@134
|
56 |
from about_dialog import AboutDialog
|
oliver@134
|
57 |
|
oliver@134
|
58 |
|
oliver@134
|
59 |
# ------------------------------------------------------------
|
oliver@134
|
60 |
# code
|
oliver@134
|
61 |
|
oliver@134
|
62 |
|
oliver@134
|
63 |
class KeyfileDialog(QtGui.QDialog):
|
oliver@134
|
64 |
|
oliver@134
|
65 |
"""A dialog for letting the user pass on a password/keyfile combo"""
|
oliver@134
|
66 |
|
oliver@134
|
67 |
def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
|
oliver@134
|
68 |
|
oliver@134
|
69 |
QtGui.QDialog.__init__(self)
|
oliver@134
|
70 |
|
oliver@134
|
71 |
# setup the user interface
|
oliver@134
|
72 |
self.ui = Ui_KeyfileDialog()
|
oliver@134
|
73 |
self.ui.setupUi(self)
|
oliver@134
|
74 |
|
oliver@134
|
75 |
# local members
|
oliver@134
|
76 |
self._about_dialog = AboutDialog()
|
oliver@134
|
77 |
self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
|
oliver@134
|
78 |
self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
|
oliver@134
|
79 |
self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
|
oliver@134
|
80 |
|
oliver@134
|
81 |
# connectors
|
oliver@134
|
82 |
self.ui.lblText.setText(user_text)
|
oliver@134
|
83 |
self.ui.btnAbout.clicked.connect(self.clicked_about)
|
oliver@134
|
84 |
self.ui.btnCancel.clicked.connect(self.reject)
|
oliver@134
|
85 |
self.ui.btnOk.clicked.connect(self.clicked_ok)
|
oliver@134
|
86 |
self.ui.btnBrowse.clicked.connect(self.clicked_browse)
|
oliver@134
|
87 |
|
oliver@134
|
88 |
# positionate ourself central
|
oliver@134
|
89 |
screen = QtGui.QDesktopWidget().screenGeometry()
|
oliver@134
|
90 |
size = self.geometry()
|
oliver@134
|
91 |
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
|
oliver@134
|
92 |
|
oliver@232
|
93 |
t = QtCore.QTimer(self)
|
oliver@232
|
94 |
t.timeout.connect(self.force_foreground)
|
oliver@232
|
95 |
t.setInterval(0)
|
oliver@232
|
96 |
t.setSingleShot(True)
|
oliver@232
|
97 |
t.start()
|
oliver@232
|
98 |
|
oliver@134
|
99 |
|
oliver@134
|
100 |
def clicked_about(self):
|
oliver@134
|
101 |
|
oliver@134
|
102 |
"""About button has been clicked."""
|
oliver@134
|
103 |
self._about_dialog.show()
|
oliver@134
|
104 |
|
oliver@134
|
105 |
|
oliver@134
|
106 |
def clicked_browse(self):
|
oliver@134
|
107 |
|
oliver@134
|
108 |
"""Browse button has been clicked."""
|
oliver@134
|
109 |
if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
|
oliver@134
|
110 |
self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
|
oliver@134
|
111 |
|
oliver@134
|
112 |
|
oliver@134
|
113 |
def clicked_ok(self):
|
oliver@134
|
114 |
|
oliver@134
|
115 |
"""Ok button has been clicked."""
|
oliver@134
|
116 |
|
oliver@134
|
117 |
# read the content of the keyfile
|
oliver@134
|
118 |
keyfile_content = ''
|
oliver@134
|
119 |
try:
|
oliver@134
|
120 |
keyfile = open(self.ui.edtKeyfile.text(), 'r')
|
oliver@134
|
121 |
keyfile_content = keyfile.read()
|
oliver@134
|
122 |
except Exception as e:
|
oliver@134
|
123 |
sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
|
oliver@134
|
124 |
QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
|
oliver@134
|
125 |
return
|
oliver@134
|
126 |
|
oliver@134
|
127 |
# turn into Base64
|
oliver@134
|
128 |
keyfile_content_base64 = base64.b64encode(keyfile_content)
|
oliver@134
|
129 |
|
oliver@134
|
130 |
sys.stdout.write('{ ')
|
oliver@134
|
131 |
sys.stdout.write('"password": "')
|
oliver@134
|
132 |
sys.stdout.write(self.ui.edtPassword.text())
|
oliver@134
|
133 |
sys.stdout.write('", ')
|
oliver@134
|
134 |
sys.stdout.write('"keyfile": "')
|
oliver@134
|
135 |
sys.stdout.write(keyfile_content_base64)
|
oliver@134
|
136 |
sys.stdout.write('" ')
|
oliver@134
|
137 |
sys.stdout.write('}\n')
|
oliver@134
|
138 |
self.accept()
|
oliver@134
|
139 |
|
oliver@134
|
140 |
|
oliver@232
|
141 |
def force_foreground(self):
|
oliver@232
|
142 |
|
oliver@232
|
143 |
"""Force ourselves into foreground"""
|
oliver@232
|
144 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
145 |
w = self
|
oliver@232
|
146 |
while not w.nativeParentWidget() is None:
|
oliver@232
|
147 |
w = w.nativeParentWidget()
|
oliver@232
|
148 |
win32gui.BringWindowToTop(int(w.effectiveWinId()))
|
oliver@232
|
149 |
win32gui.SetForegroundWindow(int(w.effectiveWinId()))
|
oliver@232
|
150 |
|
oliver@232
|
151 |
|
oliver@134
|
152 |
def set_user_text(user_text):
|
oliver@134
|
153 |
|
oliver@134
|
154 |
"""Set a text to explain which password we need."""
|
oliver@134
|
155 |
self.ui.lblText.setText(user_text)
|
oliver@134
|
156 |
|
oliver@134
|
157 |
|
oliver@134
|
158 |
if __name__ == "__main__":
|
oliver@232
|
159 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@232
|
160 |
win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
|
oliver@134
|
161 |
a = QtGui.QApplication(sys.argv)
|
oliver@134
|
162 |
d = KeyfileDialog()
|
oliver@134
|
163 |
d.show()
|
oliver@134
|
164 |
sys.exit(a.exec_())
|
oliver@134
|
165 |
|