OpenSecurity/bin/ui/format_drive_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 213 2e0b94e12bfc
permissions -rwxr-xr-x
lizenz fixed in all files
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # format_drive_dialog.pyw
     6 # 
     7 # letting the user format a drive attached to a certain VM
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
    12 # 
    13 # 
    14 #     X-Net Services GmbH
    15 #     Elisabethstrasse 1
    16 #     4020 Linz
    17 #     AUSTRIA
    18 #     https://www.x-net.at
    19 # 
    20 #     AIT Austrian Institute of Technology
    21 #     Donau City Strasse 1
    22 #     1220 Wien
    23 #     AUSTRIA
    24 #     http://www.ait.ac.at
    25 # 
    26 # 
    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
    30 # 
    31 #    http://www.apache.org/licenses/LICENSE-2.0
    32 # 
    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 # ------------------------------------------------------------
    39 
    40 
    41 # ------------------------------------------------------------
    42 # imports
    43 
    44 import base64
    45 import sys
    46 
    47 import urllib
    48 import urllib2
    49 
    50 from PyQt4 import QtCore
    51 from PyQt4 import QtGui
    52 
    53 from ui_FormatDriveDialog import Ui_FormatDriveDialog 
    54 from about_dialog import AboutDialog
    55 
    56 # ------------------------------------------------------------
    57 # code
    58 
    59 
    60 class FormatDriveDialog(QtGui.QDialog):
    61 
    62     """A dialog for letting the user pass on a password/keyfile combo"""
    63 
    64     def __init__(self, ip):
    65 
    66         QtGui.QDialog.__init__(self)
    67 
    68         user_text = """
    69 <b>Attention!</b><br/>
    70 You are going to wipe all data stored<br/>
    71 at device attached to IP <b>%s</b>.<br/>
    72 <br/>
    73 <b>This is irreversible.</b><br/>
    74 <br/>
    75 Please provide an appropriate password or keyfile to proceed:
    76 """ % ip
    77 
    78         # setup the user interface
    79         self.ui = Ui_FormatDriveDialog()
    80         self.ui.setupUi(self)
    81     
    82         # local members
    83         self._about_dialog = AboutDialog()
    84         self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
    85         self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
    86         self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
    87 
    88         # connectors
    89         self.ui.lblText.setText(user_text)
    90         self.ui.btnAbout.clicked.connect(self.clicked_about)
    91         self.ui.btnCancel.clicked.connect(self.reject)
    92         self.ui.btnOk.clicked.connect(self.clicked_ok)
    93         self.ui.btnBrowse.clicked.connect(self.clicked_browse)
    94 
    95         # positionate ourself central
    96         screen = QtGui.QDesktopWidget().screenGeometry()
    97         size = self.geometry()
    98         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    99  
   100 
   101     def clicked_about(self):
   102 
   103         """About button has been clicked."""
   104         self._about_dialog.show()
   105 
   106 
   107     def clicked_browse(self):
   108 
   109         """Browse button has been clicked."""
   110         if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
   111             self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
   112 
   113 
   114     def clicked_ok(self):
   115         
   116         """Ok button has been clicked."""
   117 
   118         init_data = dict()
   119         
   120         # pick the password
   121         init_data['password'] = str(self.ui.edtPassword.text())
   122         if len(init_data['password']) == 0:
   123             QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
   124             return
   125 
   126         # read the content of the keyfile
   127         keyfile_content = ''
   128         try:
   129             if len(self.ui.edtKeyfile.text()) > 0:
   130                 keyfile = open(self.ui.edtKeyfile.text(), 'r')
   131                 keyfile_content = keyfile.read()
   132         except Exception as e:
   133             sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
   134             QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
   135             return
   136 
   137         # turn into Base64
   138         if len(keyfile_content) > 0:
   139             keyfile_content_base64 = base64.b64encode(keyfile_content)
   140             init_data['keyfile'] = keyfile_content_base64
   141 
   142         res = ""
   143         try:
   144             req_data = urllib.urlencode(init_data)
   145             req = urllib2.Request('http://' + ip + ':58081/init', req_data)
   146             res = urllib2.urlopen(req)
   147         except:
   148             print('EXCEPTION ' + res)
   149             pass
   150 
   151         self.accept()
   152 
   153 
   154     def set_user_text(self, user_text):
   155 
   156         """Set a text to explain which password we need."""
   157         self.ui.lblText.setText(user_text)
   158 
   159 
   160 if __name__ == "__main__":
   161     
   162     print(sys.argv)
   163     
   164     ip = None
   165     try:
   166         ip = sys.argv[-1:][0]
   167     except:
   168         pass
   169     
   170     a = QtGui.QApplication(sys.argv)
   171     d = FormatDriveDialog(ip)
   172     d.show()
   173     sys.exit(a.exec_())     
   174