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