2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the user should give us a keyfile
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 # ------------------------------------------------------------
38 if sys.platform == 'win32' or sys.platform == 'cygwin':
43 from PyQt4 import QtCore
44 from PyQt4 import QtGui
46 from ui_KeyfileDialog import Ui_KeyfileDialog
47 from about_dialog import AboutDialog
50 # ------------------------------------------------------------
54 class KeyfileDialog(QtGui.QDialog):
56 """A dialog for letting the user pass on a password/keyfile combo"""
58 def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
60 QtGui.QDialog.__init__(self)
62 # setup the user interface
63 self.ui = Ui_KeyfileDialog()
67 self._about_dialog = AboutDialog()
68 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
69 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
70 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
73 self.ui.lblText.setText(user_text)
74 self.ui.btnAbout.clicked.connect(self.clicked_about)
75 self.ui.btnCancel.clicked.connect(self.reject)
76 self.ui.btnOk.clicked.connect(self.clicked_ok)
77 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
79 # positionate ourself central
80 screen = QtGui.QDesktopWidget().screenGeometry()
81 size = self.geometry()
82 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
84 t = QtCore.QTimer(self)
85 t.timeout.connect(self.force_foreground)
91 def clicked_about(self):
93 """About button has been clicked."""
94 self._about_dialog.show()
97 def clicked_browse(self):
99 """Browse button has been clicked."""
100 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
101 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
104 def clicked_ok(self):
106 """Ok button has been clicked."""
108 # read the content of the keyfile
111 keyfile = open(self.ui.edtKeyfile.text(), 'r')
112 keyfile_content = keyfile.read()
113 except Exception as e:
114 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
115 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
119 keyfile_content_base64 = base64.b64encode(keyfile_content)
121 sys.stdout.write('{ ')
122 sys.stdout.write('"password": "')
123 sys.stdout.write(self.ui.edtPassword.text())
124 sys.stdout.write('", ')
125 sys.stdout.write('"keyfile": "')
126 sys.stdout.write(keyfile_content_base64)
127 sys.stdout.write('" ')
128 sys.stdout.write('}\n')
132 def force_foreground(self):
134 """Force ourselves into foreground"""
135 if sys.platform == 'win32' or sys.platform == 'cygwin':
137 while not w.nativeParentWidget() is None:
138 w = w.nativeParentWidget()
139 win32gui.BringWindowToTop(int(w.effectiveWinId()))
140 win32gui.SetForegroundWindow(int(w.effectiveWinId()))
143 def set_user_text(user_text):
145 """Set a text to explain which password we need."""
146 self.ui.lblText.setText(user_text)
149 if __name__ == "__main__":
150 if sys.platform == 'win32' or sys.platform == 'cygwin':
151 win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
152 a = QtGui.QApplication(sys.argv)