Skip to content

Commit

Permalink
Move connections ignore cache to credentials dialog
Browse files Browse the repository at this point in the history
this way it is available for all providers
  • Loading branch information
elpaso committed Oct 31, 2019
1 parent 38fadd6 commit 670618a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
53 changes: 45 additions & 8 deletions src/gui/qgscredentialdialog.cpp
Expand Up @@ -24,6 +24,11 @@

#include <QPushButton>
#include <QThread>
#include <QTimer>

QSet<QString> QgsCredentialDialog::sIgnoredConnectionsCache;
QMutex QgsCredentialDialog::sIgnoredConnectionsCacheMutex;


static QString invalidStyle_( const QString &selector = QStringLiteral( "QLineEdit" ) )
{
Expand All @@ -46,6 +51,27 @@ QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WindowFlags fl )
this, &QgsCredentialDialog::requestCredentialsMasterPassword,
Qt::BlockingQueuedConnection );
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
QPushButton *ignoreButton { buttonBox->button( QDialogButtonBox::StandardButton::Ignore ) };
ignoreButton->setToolTip( tr( "All requests for this connection will be automatically rejected for the next 5 seconds." ) );

// Keep a cache of ignored connections, and ignore them for 5 seconds.
connect( ignoreButton, &QPushButton::clicked, [ = ]( bool )
{
const QString realm { labelRealm->text() };
{
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
// Insert the realm in the cache of ignored connections
sIgnoredConnectionsCache.insert( realm );
}
QTimer::singleShot( 5000, [ = ]()
{
QgsDebugMsgLevel( QStringLiteral( "Removing ignored connection from cache: %1" ).arg( realm ), 4 );
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
sIgnoredConnectionsCache.remove( realm );
} );
accept( );
} );

leMasterPass->setPlaceholderText( tr( "Required" ) );
chkbxPasswordHelperEnable->setText( tr( "Store/update the master password in your %1" )
.arg( QgsAuthManager::AUTH_PASSWORD_HELPER_DISPLAY_NAME ) );
Expand All @@ -71,9 +97,18 @@ bool QgsCredentialDialog::request( const QString &realm, QString &username, QStr
void QgsCredentialDialog::requestCredentials( const QString &realm, QString *username, QString *password, const QString &message, bool *ok )
{
Q_ASSERT( qApp->thread() == thread() && thread() == QThread::currentThread() );
QgsDebugMsg( QStringLiteral( "Entering." ) );
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
{
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
if ( sIgnoredConnectionsCache.contains( realm ) )
{
QgsDebugMsg( QStringLiteral( "Skipping ignored connection: " ) + realm );
*ok = false;
return;
}
}
stackedWidget->setCurrentIndex( 0 );

buttonBox->button( QDialogButtonBox::StandardButton::Ignore )->show();
chkbxPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
labelRealm->setText( realm );
leUsername->setText( *username );
Expand All @@ -90,9 +125,9 @@ void QgsCredentialDialog::requestCredentials( const QString &realm, QString *use

QApplication::setOverrideCursor( Qt::ArrowCursor );

QgsDebugMsg( QStringLiteral( "exec()" ) );
QgsDebugMsgLevel( QStringLiteral( "exec()" ), 4 );
*ok = exec() == QDialog::Accepted;
QgsDebugMsg( QStringLiteral( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
QgsDebugMsgLevel( QStringLiteral( "exec(): %1" ).arg( *ok ? "true" : "false" ), 4 );

QApplication::restoreOverrideCursor();

Expand All @@ -111,7 +146,7 @@ bool QgsCredentialDialog::requestMasterPassword( QString &password, bool stored
bool ok;
if ( qApp->thread() != QThread::currentThread() )
{
QgsDebugMsg( QStringLiteral( "emitting signal" ) );
QgsDebugMsgLevel( QStringLiteral( "emitting signal" ), 4 );
emit credentialsRequestedMasterPassword( &password, stored, &ok );
}
else
Expand All @@ -123,8 +158,10 @@ bool QgsCredentialDialog::requestMasterPassword( QString &password, bool stored

void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password, bool stored, bool *ok )
{
QgsDebugMsg( QStringLiteral( "Entering." ) );
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
stackedWidget->setCurrentIndex( 1 );

buttonBox->button( QDialogButtonBox::StandardButton::Ignore )->hide();
leMasterPass->setFocus();

QString titletxt( stored ? tr( "Enter CURRENT master authentication password" ) : tr( "Set NEW master authentication password" ) );
Expand Down Expand Up @@ -155,9 +192,9 @@ void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password, b
s.setWidth( width() );
resize( s );

QgsDebugMsg( QStringLiteral( "exec()" ) );
QgsDebugMsgLevel( QStringLiteral( "exec()" ), 4 );
*ok = exec() == QDialog::Accepted;
QgsDebugMsg( QStringLiteral( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
QgsDebugMsgLevel( QStringLiteral( "exec(): %1" ).arg( *ok ? "true" : "false" ), 4 );

if ( *ok )
{
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgscredentialdialog.h
Expand Up @@ -64,6 +64,10 @@ class GUI_EXPORT QgsCredentialDialog : public QDialog, public QgsCredentials, pr

private:
QPushButton *mOkButton = nullptr;
//! Temporary cache for ignored connections, to avoid GUI freezing by multiple credentials requests to the same connection
static QSet<QString> sIgnoredConnectionsCache;
static QMutex sIgnoredConnectionsCacheMutex;

};

#endif
27 changes: 1 addition & 26 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -144,8 +144,6 @@ Oid QgsPostgresResult::PQoidValue()

QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRO;
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRW;
QSet<QString> QgsPostgresConn::sBrokenConnectionsCache;
QMutex QgsPostgresConn::sBrokenConnectionsCacheMutex;

const int QgsPostgresConn::GEOM_TYPE_SELECT_LIMIT = 100;

Expand Down Expand Up @@ -222,19 +220,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
, mLock( QMutex::Recursive )
{

{
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
if ( sBrokenConnectionsCache.contains( conninfo ) )
{
QgsDebugMsg( QStringLiteral( "Skipping broken PostgreSQL connection: " ) + conninfo );
mRef = 0;
return;
}
else
{
QgsDebugMsg( QStringLiteral( "New PostgreSQL connection for " ) + conninfo );
}
}
QgsDebugMsg( QStringLiteral( "New PostgreSQL connection for " ) + conninfo );

// expand connectionInfo
QgsDataSourceUri uri( conninfo );
Expand Down Expand Up @@ -303,17 +289,6 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
bool ok = QgsCredentials::instance()->get( conninfo, username, password, PQerrorMessage() );
if ( !ok )
{
{
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
// Insert the connInfo in the cache of broken/ignored connections
QgsPostgresConn::sBrokenConnectionsCache.insert( conninfo );
}
QTimer::singleShot( 5000, [ = ]()
{
QgsDebugMsgLevel( QStringLiteral( "Removing broken connection from cache: %1" ).arg( conninfo ), 4 );
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
QgsPostgresConn::sBrokenConnectionsCache.remove( conninfo );
} );
break;
}

Expand Down
4 changes: 0 additions & 4 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -435,10 +435,6 @@ class QgsPostgresConn : public QObject
static QMap<QString, QgsPostgresConn *> sConnectionsRW;
static QMap<QString, QgsPostgresConn *> sConnectionsRO;

//! Temporary cache for broken connections, to avoid GUI freezing by multiple credentials requests
static QSet<QString> sBrokenConnectionsCache;
static QMutex sBrokenConnectionsCacheMutex;

//! Count number of spatial columns in a given relation
void addColumnInfo( QgsPostgresLayerProperty &layerProperty, const QString &schemaName, const QString &viewName, bool fetchPkCandidates );

Expand Down
4 changes: 2 additions & 2 deletions src/ui/qgscredentialdialog.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>396</width>
<height>289</height>
<height>293</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -23,7 +23,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ignore|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
Expand Down

0 comments on commit 670618a

Please sign in to comment.