OpenSecurity/bin/ui/credentials_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 # credentials_dialog.pyw
     6 # 
     7 # the user should give us some user/pass info
     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 sys
    45 
    46 if sys.platform == 'win32' or sys.platform == 'cygwin':
    47     import win32api
    48     import win32con
    49     import win32gui
    50 
    51 from PyQt4 import QtCore
    52 from PyQt4 import QtGui
    53 
    54 from ui_CredentialsDialog import Ui_CredentialsDialog 
    55 from about_dialog import AboutDialog
    56 
    57 
    58 # ------------------------------------------------------------
    59 # code
    60 
    61 
    62 class CredentialsDialog(QtGui.QDialog):
    63 
    64     """A dialog for letting the user pass on a user/password combo"""
    65 
    66     def __init__(self, user_text = 'Please provide approbitate login information:'):
    67 
    68         QtGui.QDialog.__init__(self)
    69 
    70         # setup the user interface
    71         self.ui = Ui_CredentialsDialog()
    72         self.ui.setupUi(self)
    73     
    74         # local members
    75         self._about_dialog = AboutDialog()
    76 
    77         # connectors
    78         self.ui.lblText.setText(user_text)
    79         self.ui.btnAbout.clicked.connect(self.clicked_about)
    80         self.ui.btnCancel.clicked.connect(self.reject)
    81         self.ui.btnOk.clicked.connect(self.clicked_ok)
    82 
    83         # positionate ourself central
    84         screen = QtGui.QDesktopWidget().screenGeometry()
    85         size = self.geometry()
    86         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    87  
    88         t = QtCore.QTimer(self)
    89         t.timeout.connect(self.force_foreground)
    90         t.setInterval(0)
    91         t.setSingleShot(True)
    92         t.start()
    93  
    94 
    95     def clicked_about(self):
    96 
    97         """About button has been clicked."""
    98         self._about_dialog.show()
    99 
   100 
   101     def clicked_ok(self):
   102         
   103         """Ok button has been clicked."""
   104         sys.stdout.write('{ ')
   105         sys.stdout.write('"user": "')
   106         sys.stdout.write(self.ui.edtUser.text())
   107         sys.stdout.write('", ')
   108         sys.stdout.write('"password": "')
   109         sys.stdout.write(self.ui.edtPassword.text())
   110         sys.stdout.write('" ')
   111         sys.stdout.write('}\n')
   112         self.accept()
   113 
   114 
   115     def force_foreground(self):
   116 
   117         """Force ourselves into foreground"""
   118         if sys.platform == 'win32' or sys.platform == 'cygwin':
   119             w = self
   120             while not w.nativeParentWidget() is None:
   121                 w = w.nativeParentWidget()
   122             win32gui.BringWindowToTop(int(w.effectiveWinId()))
   123             win32gui.SetForegroundWindow(int(w.effectiveWinId()))
   124 
   125 
   126     def set_user_text(user_text):
   127 
   128         """Set a text to explain which credentials we need."""
   129         self.ui.lblText.setText(user_text)
   130 
   131 
   132 if __name__ == "__main__":
   133     if sys.platform == 'win32' or sys.platform == 'cygwin':
   134         win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
   135     a = QtGui.QApplication(sys.argv)
   136     d = CredentialsDialog()
   137     d.show()
   138     sys.exit(a.exec_())     
   139