OpenSecurity/bin/ui/format_drive_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 25 Jun 2014 21:49:09 +0200
changeset 205 b16c57614eee
child 207 ae931a692b54
permissions -rwxr-xr-x
added format drive option
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # keyfile_dialog.pyw
     6 # 
     7 # the user should give us a keyfile
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    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
    15 #
    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.
    19 # 
    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.
    24 # 
    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 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import base64
    36 import sys
    37 
    38 from PyQt4 import QtCore
    39 from PyQt4 import QtGui
    40 
    41 from ui_KeyfileDialog import Ui_KeyfileDialog 
    42 from about_dialog import AboutDialog
    43 
    44 
    45 # ------------------------------------------------------------
    46 # code
    47 
    48 
    49 class KeyfileDialog(QtGui.QDialog):
    50 
    51     """A dialog for letting the user pass on a password/keyfile combo"""
    52 
    53     def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
    54 
    55         QtGui.QDialog.__init__(self)
    56 
    57         # setup the user interface
    58         self.ui = Ui_KeyfileDialog()
    59         self.ui.setupUi(self)
    60     
    61         # local members
    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)
    66 
    67         # connectors
    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)
    73 
    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)
    78  
    79 
    80     def clicked_about(self):
    81 
    82         """About button has been clicked."""
    83         self._about_dialog.show()
    84 
    85 
    86     def clicked_browse(self):
    87 
    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])
    91 
    92 
    93     def clicked_ok(self):
    94         
    95         """Ok button has been clicked."""
    96 
    97         # read the content of the keyfile
    98         keyfile_content = ''
    99         try:
   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))
   105             return
   106 
   107         # turn into Base64
   108         keyfile_content_base64 = base64.b64encode(keyfile_content)
   109 
   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')
   118         self.accept()
   119 
   120 
   121     def set_user_text(user_text):
   122 
   123         """Set a text to explain which password we need."""
   124         self.ui.lblText.setText(user_text)
   125 
   126 
   127 if __name__ == "__main__":
   128     a = QtGui.QApplication(sys.argv)
   129     d = KeyfileDialog()
   130     d.show()
   131     sys.exit(a.exec_())     
   132