Skip to content

Commit

Permalink
allow credential requests from threads
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed May 12, 2013
1 parent fa0a9b7 commit 82e78ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/gui/qgscredentialdialog.cpp
Expand Up @@ -16,13 +16,19 @@
***************************************************************************/

#include "qgscredentialdialog.h"
#include "qgslogger.h"

#include <QSettings>
#include <QThread>

QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
setInstance( this );
connect( this, SIGNAL( credentialsRequested( QString, QString *, QString *, QString, bool * ) ),
this, SLOT( requestCredentials( QString, QString *, QString *, QString, bool * ) ),
Qt::BlockingQueuedConnection );
}

QgsCredentialDialog::~QgsCredentialDialog()
Expand All @@ -31,26 +37,40 @@ QgsCredentialDialog::~QgsCredentialDialog()

bool QgsCredentialDialog::request( QString realm, QString &username, QString &password, QString message )
{
bool ok;
if ( qApp->thread() != QThread::currentThread() )
{
QgsDebugMsg( "emitting signal" );
emit credentialsRequested( realm, &username, &password, message, &ok );
QgsDebugMsg( QString( "signal returned %1 (username=%2, password=%3)" ).arg( ok ? "true" : "false" ).arg( username ).arg( password ) );
}
else
{
requestCredentials( realm, &username, &password, message, &ok );
}
return ok;
}

void QgsCredentialDialog::requestCredentials( QString realm, QString *username, QString *password, QString message, bool *ok )
{
QgsDebugMsg( "Entering." );
labelRealm->setText( realm );
leUsername->setText( username );
lePassword->setText( password );
leUsername->setText( *username );
lePassword->setText( *password );
labelMessage->setText( message );
labelMessage->setHidden( message.isEmpty() );

QApplication::setOverrideCursor( Qt::ArrowCursor );

int res = exec();
QgsDebugMsg( "exec()" );
*ok = exec() == QDialog::Accepted;
QgsDebugMsg( QString( "exec(): %1" ).arg( ok ? "true" : "false" ) );

QApplication::restoreOverrideCursor();

if ( res == QDialog::Accepted )
{
username = leUsername->text();
password = lePassword->text();
return true;
}
else
if ( *ok )
{
return false;
*username = leUsername->text();
*password = lePassword->text();
}
}
6 changes: 6 additions & 0 deletions src/gui/qgscredentialdialog.h
Expand Up @@ -34,6 +34,12 @@ class GUI_EXPORT QgsCredentialDialog : public QDialog, public QgsCredentials, pr
QgsCredentialDialog( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
~QgsCredentialDialog();

signals:
void credentialsRequested( QString, QString *, QString *, QString, bool * );

private slots:
void requestCredentials( QString, QString *, QString *, QString, bool * );

protected:
virtual bool request( QString realm, QString &username, QString &password, QString message = QString::null );
};
Expand Down

0 comments on commit 82e78ff

Please sign in to comment.