OpenSecurity/bin/ui/format_drive_dialog.py
changeset 205 b16c57614eee
child 207 ae931a692b54
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/ui/format_drive_dialog.py	Wed Jun 25 21:49:09 2014 +0200
     1.3 @@ -0,0 +1,132 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# keyfile_dialog.pyw
     1.9 +# 
    1.10 +# the user should give us a keyfile
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2014 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import base64
    1.39 +import sys
    1.40 +
    1.41 +from PyQt4 import QtCore
    1.42 +from PyQt4 import QtGui
    1.43 +
    1.44 +from ui_KeyfileDialog import Ui_KeyfileDialog 
    1.45 +from about_dialog import AboutDialog
    1.46 +
    1.47 +
    1.48 +# ------------------------------------------------------------
    1.49 +# code
    1.50 +
    1.51 +
    1.52 +class KeyfileDialog(QtGui.QDialog):
    1.53 +
    1.54 +    """A dialog for letting the user pass on a password/keyfile combo"""
    1.55 +
    1.56 +    def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
    1.57 +
    1.58 +        QtGui.QDialog.__init__(self)
    1.59 +
    1.60 +        # setup the user interface
    1.61 +        self.ui = Ui_KeyfileDialog()
    1.62 +        self.ui.setupUi(self)
    1.63 +    
    1.64 +        # local members
    1.65 +        self._about_dialog = AboutDialog()
    1.66 +        self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
    1.67 +        self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
    1.68 +        self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
    1.69 +
    1.70 +        # connectors
    1.71 +        self.ui.lblText.setText(user_text)
    1.72 +        self.ui.btnAbout.clicked.connect(self.clicked_about)
    1.73 +        self.ui.btnCancel.clicked.connect(self.reject)
    1.74 +        self.ui.btnOk.clicked.connect(self.clicked_ok)
    1.75 +        self.ui.btnBrowse.clicked.connect(self.clicked_browse)
    1.76 +
    1.77 +        # positionate ourself central
    1.78 +        screen = QtGui.QDesktopWidget().screenGeometry()
    1.79 +        size = self.geometry()
    1.80 +        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    1.81 + 
    1.82 +
    1.83 +    def clicked_about(self):
    1.84 +
    1.85 +        """About button has been clicked."""
    1.86 +        self._about_dialog.show()
    1.87 +
    1.88 +
    1.89 +    def clicked_browse(self):
    1.90 +
    1.91 +        """Browse button has been clicked."""
    1.92 +        if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
    1.93 +            self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
    1.94 +
    1.95 +
    1.96 +    def clicked_ok(self):
    1.97 +        
    1.98 +        """Ok button has been clicked."""
    1.99 +
   1.100 +        # read the content of the keyfile
   1.101 +        keyfile_content = ''
   1.102 +        try:
   1.103 +            keyfile = open(self.ui.edtKeyfile.text(), 'r')
   1.104 +            keyfile_content = keyfile.read()
   1.105 +        except Exception as e:
   1.106 +            sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
   1.107 +            QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
   1.108 +            return
   1.109 +
   1.110 +        # turn into Base64
   1.111 +        keyfile_content_base64 = base64.b64encode(keyfile_content)
   1.112 +
   1.113 +        sys.stdout.write('{ ')
   1.114 +        sys.stdout.write('"password": "')
   1.115 +        sys.stdout.write(self.ui.edtPassword.text())
   1.116 +        sys.stdout.write('", ')
   1.117 +        sys.stdout.write('"keyfile": "')
   1.118 +        sys.stdout.write(keyfile_content_base64)
   1.119 +        sys.stdout.write('" ')
   1.120 +        sys.stdout.write('}\n')
   1.121 +        self.accept()
   1.122 +
   1.123 +
   1.124 +    def set_user_text(user_text):
   1.125 +
   1.126 +        """Set a text to explain which password we need."""
   1.127 +        self.ui.lblText.setText(user_text)
   1.128 +
   1.129 +
   1.130 +if __name__ == "__main__":
   1.131 +    a = QtGui.QApplication(sys.argv)
   1.132 +    d = KeyfileDialog()
   1.133 +    d.show()
   1.134 +    sys.exit(a.exec_())     
   1.135 +