OpenSecurity/bin/ui/keyfile_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 232 56120b285fc8
permissions -rwxr-xr-x
lizenz fixed in all files
     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 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 if sys.platform == 'win32' or sys.platform == 'cygwin':
    48     import win32api
    49     import win32con
    50     import win32gui
    51 
    52 from PyQt4 import QtCore
    53 from PyQt4 import QtGui
    54 
    55 from ui_KeyfileDialog import Ui_KeyfileDialog 
    56 from about_dialog import AboutDialog
    57 
    58 
    59 # ------------------------------------------------------------
    60 # code
    61 
    62 
    63 class KeyfileDialog(QtGui.QDialog):
    64 
    65     """A dialog for letting the user pass on a password/keyfile combo"""
    66 
    67     def __init__(self, user_text = 'Please provide an approbitate password and keyfile to proceed:'):
    68 
    69         QtGui.QDialog.__init__(self)
    70 
    71         # setup the user interface
    72         self.ui = Ui_KeyfileDialog()
    73         self.ui.setupUi(self)
    74     
    75         # local members
    76         self._about_dialog = AboutDialog()
    77         self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
    78         self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
    79         self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
    80 
    81         # connectors
    82         self.ui.lblText.setText(user_text)
    83         self.ui.btnAbout.clicked.connect(self.clicked_about)
    84         self.ui.btnCancel.clicked.connect(self.reject)
    85         self.ui.btnOk.clicked.connect(self.clicked_ok)
    86         self.ui.btnBrowse.clicked.connect(self.clicked_browse)
    87 
    88         # positionate ourself central
    89         screen = QtGui.QDesktopWidget().screenGeometry()
    90         size = self.geometry()
    91         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    92  
    93         t = QtCore.QTimer(self)
    94         t.timeout.connect(self.force_foreground)
    95         t.setInterval(0)
    96         t.setSingleShot(True)
    97         t.start()
    98  
    99 
   100     def clicked_about(self):
   101 
   102         """About button has been clicked."""
   103         self._about_dialog.show()
   104 
   105 
   106     def clicked_browse(self):
   107 
   108         """Browse button has been clicked."""
   109         if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
   110             self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
   111 
   112 
   113     def clicked_ok(self):
   114         
   115         """Ok button has been clicked."""
   116 
   117         # read the content of the keyfile
   118         keyfile_content = ''
   119         try:
   120             keyfile = open(self.ui.edtKeyfile.text(), 'r')
   121             keyfile_content = keyfile.read()
   122         except Exception as e:
   123             sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
   124             QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
   125             return
   126 
   127         # turn into Base64
   128         keyfile_content_base64 = base64.b64encode(keyfile_content)
   129 
   130         sys.stdout.write('{ ')
   131         sys.stdout.write('"password": "')
   132         sys.stdout.write(self.ui.edtPassword.text())
   133         sys.stdout.write('", ')
   134         sys.stdout.write('"keyfile": "')
   135         sys.stdout.write(keyfile_content_base64)
   136         sys.stdout.write('" ')
   137         sys.stdout.write('}\n')
   138         self.accept()
   139 
   140 
   141     def force_foreground(self):
   142 
   143         """Force ourselves into foreground"""
   144         if sys.platform == 'win32' or sys.platform == 'cygwin':
   145             w = self
   146             while not w.nativeParentWidget() is None:
   147                 w = w.nativeParentWidget()
   148             win32gui.BringWindowToTop(int(w.effectiveWinId()))
   149             win32gui.SetForegroundWindow(int(w.effectiveWinId()))
   150 
   151 
   152     def set_user_text(user_text):
   153 
   154         """Set a text to explain which password we need."""
   155         self.ui.lblText.setText(user_text)
   156 
   157 
   158 if __name__ == "__main__":
   159     if sys.platform == 'win32' or sys.platform == 'cygwin':
   160         win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
   161     a = QtGui.QApplication(sys.argv)
   162     d = KeyfileDialog()
   163     d.show()
   164     sys.exit(a.exec_())     
   165