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