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
oliver@205
     1
#!/bin/env python
oliver@205
     2
# -*- coding: utf-8 -*-
oliver@205
     3
oliver@205
     4
# ------------------------------------------------------------
oliver@205
     5
# keyfile_dialog.pyw
oliver@205
     6
# 
oliver@205
     7
# the user should give us a keyfile
oliver@205
     8
#
oliver@205
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
oliver@205
    10
#
oliver@205
    11
# Copyright (C) 2014 AIT Austrian Institute of Technology
oliver@205
    12
# AIT Austrian Institute of Technology GmbH
oliver@205
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
oliver@205
    14
# http://www.ait.ac.at
oliver@205
    15
#
oliver@205
    16
# This program is free software; you can redistribute it and/or
oliver@205
    17
# modify it under the terms of the GNU General Public License
oliver@205
    18
# as published by the Free Software Foundation version 2.
oliver@205
    19
# 
oliver@205
    20
# This program is distributed in the hope that it will be useful,
oliver@205
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
oliver@205
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
oliver@205
    23
# GNU General Public License for more details.
oliver@205
    24
# 
oliver@205
    25
# You should have received a copy of the GNU General Public License
oliver@205
    26
# along with this program; if not, write to the Free Software
oliver@205
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
oliver@205
    28
# Boston, MA  02110-1301, USA.
oliver@205
    29
# ------------------------------------------------------------
oliver@205
    30
oliver@205
    31
oliver@205
    32
# ------------------------------------------------------------
oliver@205
    33
# imports
oliver@205
    34
oliver@205
    35
import base64
oliver@205
    36
import sys
oliver@205
    37
oliver@205
    38
from PyQt4 import QtCore
oliver@205
    39
from PyQt4 import QtGui
oliver@205
    40
oliver@205
    41
from ui_KeyfileDialog import Ui_KeyfileDialog 
oliver@205
    42
from about_dialog import AboutDialog
oliver@205
    43
oliver@205
    44
oliver@205
    45
# ------------------------------------------------------------
oliver@205
    46
# code
oliver@205
    47
oliver@205
    48
oliver@205
    49
class KeyfileDialog(QtGui.QDialog):
oliver@205
    50
oliver@205
    51
    """A dialog for letting the user pass on a password/keyfile combo"""
oliver@205
    52
oliver@205
    53
    def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
oliver@205
    54
oliver@205
    55
        QtGui.QDialog.__init__(self)
oliver@205
    56
oliver@205
    57
        # setup the user interface
oliver@205
    58
        self.ui = Ui_KeyfileDialog()
oliver@205
    59
        self.ui.setupUi(self)
oliver@205
    60
    
oliver@205
    61
        # local members
oliver@205
    62
        self._about_dialog = AboutDialog()
oliver@205
    63
        self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
oliver@205
    64
        self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
oliver@205
    65
        self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
oliver@205
    66
oliver@205
    67
        # connectors
oliver@205
    68
        self.ui.lblText.setText(user_text)
oliver@205
    69
        self.ui.btnAbout.clicked.connect(self.clicked_about)
oliver@205
    70
        self.ui.btnCancel.clicked.connect(self.reject)
oliver@205
    71
        self.ui.btnOk.clicked.connect(self.clicked_ok)
oliver@205
    72
        self.ui.btnBrowse.clicked.connect(self.clicked_browse)
oliver@205
    73
oliver@205
    74
        # positionate ourself central
oliver@205
    75
        screen = QtGui.QDesktopWidget().screenGeometry()
oliver@205
    76
        size = self.geometry()
oliver@205
    77
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
oliver@205
    78
 
oliver@205
    79
oliver@205
    80
    def clicked_about(self):
oliver@205
    81
oliver@205
    82
        """About button has been clicked."""
oliver@205
    83
        self._about_dialog.show()
oliver@205
    84
oliver@205
    85
oliver@205
    86
    def clicked_browse(self):
oliver@205
    87
oliver@205
    88
        """Browse button has been clicked."""
oliver@205
    89
        if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
oliver@205
    90
            self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
oliver@205
    91
oliver@205
    92
oliver@205
    93
    def clicked_ok(self):
oliver@205
    94
        
oliver@205
    95
        """Ok button has been clicked."""
oliver@205
    96
oliver@205
    97
        # read the content of the keyfile
oliver@205
    98
        keyfile_content = ''
oliver@205
    99
        try:
oliver@205
   100
            keyfile = open(self.ui.edtKeyfile.text(), 'r')
oliver@205
   101
            keyfile_content = keyfile.read()
oliver@205
   102
        except Exception as e:
oliver@205
   103
            sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
oliver@205
   104
            QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
oliver@205
   105
            return
oliver@205
   106
oliver@205
   107
        # turn into Base64
oliver@205
   108
        keyfile_content_base64 = base64.b64encode(keyfile_content)
oliver@205
   109
oliver@205
   110
        sys.stdout.write('{ ')
oliver@205
   111
        sys.stdout.write('"password": "')
oliver@205
   112
        sys.stdout.write(self.ui.edtPassword.text())
oliver@205
   113
        sys.stdout.write('", ')
oliver@205
   114
        sys.stdout.write('"keyfile": "')
oliver@205
   115
        sys.stdout.write(keyfile_content_base64)
oliver@205
   116
        sys.stdout.write('" ')
oliver@205
   117
        sys.stdout.write('}\n')
oliver@205
   118
        self.accept()
oliver@205
   119
oliver@205
   120
oliver@205
   121
    def set_user_text(user_text):
oliver@205
   122
oliver@205
   123
        """Set a text to explain which password we need."""
oliver@205
   124
        self.ui.lblText.setText(user_text)
oliver@205
   125
oliver@205
   126
oliver@205
   127
if __name__ == "__main__":
oliver@205
   128
    a = QtGui.QApplication(sys.argv)
oliver@205
   129
    d = KeyfileDialog()
oliver@205
   130
    d.show()
oliver@205
   131
    sys.exit(a.exec_())     
oliver@205
   132