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 from PyQt4 import QtCore
39 from PyQt4 import QtGui
41 from ui_KeyfileDialog import Ui_KeyfileDialog
42 from about_dialog import AboutDialog
45 # ------------------------------------------------------------
49 class KeyfileDialog(QtGui.QDialog):
51 """A dialog for letting the user pass on a password/keyfile combo"""
53 def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
55 QtGui.QDialog.__init__(self)
57 # setup the user interface
58 self.ui = Ui_KeyfileDialog()
62 self._about_dialog = AboutDialog()
63 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
64 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
65 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
68 self.ui.lblText.setText(user_text)
69 self.ui.btnAbout.clicked.connect(self.clicked_about)
70 self.ui.btnCancel.clicked.connect(self.reject)
71 self.ui.btnOk.clicked.connect(self.clicked_ok)
72 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
74 # positionate ourself central
75 screen = QtGui.QDesktopWidget().screenGeometry()
76 size = self.geometry()
77 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
80 def clicked_about(self):
82 """About button has been clicked."""
83 self._about_dialog.show()
86 def clicked_browse(self):
88 """Browse button has been clicked."""
89 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
90 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
95 """Ok button has been clicked."""
97 # read the content of the keyfile
100 keyfile = open(self.ui.edtKeyfile.text(), 'r')
101 keyfile_content = keyfile.read()
102 except Exception as e:
103 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
104 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
108 keyfile_content_base64 = base64.b64encode(keyfile_content)
110 sys.stdout.write('{ ')
111 sys.stdout.write('"password": "')
112 sys.stdout.write(self.ui.edtPassword.text())
113 sys.stdout.write('", ')
114 sys.stdout.write('"keyfile": "')
115 sys.stdout.write(keyfile_content_base64)
116 sys.stdout.write('" ')
117 sys.stdout.write('}\n')
121 def set_user_text(user_text):
123 """Set a text to explain which password we need."""
124 self.ui.lblText.setText(user_text)
127 if __name__ == "__main__":
128 a = QtGui.QApplication(sys.argv)