QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscredentialdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscredentialdialog.cpp - description
3  -------------------
4  begin : February 2010
5  copyright : (C) 2010 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscredentialdialog.h"
19 #include "qgslogger.h"
20 
21 #include <QSettings>
22 #include <QThread>
23 
24 QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WFlags fl )
25  : QDialog( parent, fl )
26 {
27  setupUi( this );
28  setInstance( this );
29  connect( this, SIGNAL( credentialsRequested( QString, QString *, QString *, QString, bool * ) ),
30  this, SLOT( requestCredentials( QString, QString *, QString *, QString, bool * ) ),
31  Qt::BlockingQueuedConnection );
32 }
33 
35 {
36 }
37 
38 bool QgsCredentialDialog::request( QString realm, QString &username, QString &password, QString message )
39 {
40  bool ok;
41  if ( qApp->thread() != QThread::currentThread() )
42  {
43  QgsDebugMsg( "emitting signal" );
44  emit credentialsRequested( realm, &username, &password, message, &ok );
45  QgsDebugMsg( QString( "signal returned %1 (username=%2, password=%3)" ).arg( ok ? "true" : "false" ).arg( username ).arg( password ) );
46  }
47  else
48  {
49  requestCredentials( realm, &username, &password, message, &ok );
50  }
51  return ok;
52 }
53 
54 void QgsCredentialDialog::requestCredentials( QString realm, QString *username, QString *password, QString message, bool *ok )
55 {
56  QgsDebugMsg( "Entering." );
57  labelRealm->setText( realm );
58  leUsername->setText( *username );
59  lePassword->setText( *password );
60  labelMessage->setText( message );
61  labelMessage->setHidden( message.isEmpty() );
62 
63  if ( !leUsername->text().isEmpty() )
64  lePassword->setFocus();
65 
66  QApplication::setOverrideCursor( Qt::ArrowCursor );
67 
68  QgsDebugMsg( "exec()" );
69  *ok = exec() == QDialog::Accepted;
70  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
71 
72  QApplication::restoreOverrideCursor();
73 
74  if ( *ok )
75  {
76  *username = leUsername->text();
77  *password = lePassword->text();
78  }
79 }