2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the user should give us a keyfile
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 # ------------------------------------------------------------
47 if sys.platform == 'win32' or sys.platform == 'cygwin':
52 from PyQt4 import QtCore
53 from PyQt4 import QtGui
55 from ui_KeyfileDialog import Ui_KeyfileDialog
56 from about_dialog import AboutDialog
59 # ------------------------------------------------------------
63 class KeyfileDialog(QtGui.QDialog):
65 """A dialog for letting the user pass on a password/keyfile combo"""
67 def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
69 QtGui.QDialog.__init__(self)
71 # setup the user interface
72 self.ui = Ui_KeyfileDialog()
76 self._about_dialog = AboutDialog()
77 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
78 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
79 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
82 self.ui.lblText.setText(user_text)
83 self.ui.btnAbout.clicked.connect(self.clicked_about)
84 self.ui.btnCancel.clicked.connect(self.reject)
85 self.ui.btnOk.clicked.connect(self.clicked_ok)
86 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
88 # positionate ourself central
89 screen = QtGui.QDesktopWidget().screenGeometry()
90 size = self.geometry()
91 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
93 t = QtCore.QTimer(self)
94 t.timeout.connect(self.force_foreground)
100 def clicked_about(self):
102 """About button has been clicked."""
103 self._about_dialog.show()
106 def clicked_browse(self):
108 """Browse button has been clicked."""
109 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
110 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
113 def clicked_ok(self):
115 """Ok button has been clicked."""
117 # read the content of the keyfile
120 keyfile = open(self.ui.edtKeyfile.text(), 'r')
121 keyfile_content = keyfile.read()
122 except Exception as e:
123 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
124 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
128 keyfile_content_base64 = base64.b64encode(keyfile_content)
130 sys.stdout.write('{ ')
131 sys.stdout.write('"password": "')
132 sys.stdout.write(self.ui.edtPassword.text())
133 sys.stdout.write('", ')
134 sys.stdout.write('"keyfile": "')
135 sys.stdout.write(keyfile_content_base64)
136 sys.stdout.write('" ')
137 sys.stdout.write('}\n')
141 def force_foreground(self):
143 """Force ourselves into foreground"""
144 if sys.platform == 'win32' or sys.platform == 'cygwin':
146 while not w.nativeParentWidget() is None:
147 w = w.nativeParentWidget()
148 win32gui.BringWindowToTop(int(w.effectiveWinId()))
149 win32gui.SetForegroundWindow(int(w.effectiveWinId()))
152 def set_user_text(user_text):
154 """Set a text to explain which password we need."""
155 self.ui.lblText.setText(user_text)
158 if __name__ == "__main__":
159 if sys.platform == 'win32' or sys.platform == 'cygwin':
160 win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
161 a = QtGui.QApplication(sys.argv)