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