Skip to content

Commit ca0b7f6

Browse files
elpasonyalldawson
authored andcommittedNov 11, 2019
Option to ignore the connection temporarily of for the session
1 parent df7d05e commit ca0b7f6

File tree

3 files changed

+123
-67
lines changed

3 files changed

+123
-67
lines changed
 

‎src/gui/qgscredentialdialog.cpp

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
#include "qgsapplication.h"
2424

2525
#include <QPushButton>
26+
#include <QMenu>
27+
#include <QToolButton>
2628
#include <QThread>
2729
#include <QTimer>
30+
#include <QGlobalStatic>
2831

29-
QSet<QString> QgsCredentialDialog::sIgnoredConnectionsCache;
3032
QMutex QgsCredentialDialog::sIgnoredConnectionsCacheMutex;
33+
typedef QSet<QString> IgnoredConnectionsSet;
34+
35+
//! Temporary cache for ignored connections, to avoid GUI freezing by multiple credentials requests to the same connection
36+
Q_GLOBAL_STATIC( IgnoredConnectionsSet, sIgnoredConnectionsCache );
3137

3238

3339
static QString invalidStyle_( const QString &selector = QStringLiteral( "QLineEdit" ) )
@@ -50,25 +56,55 @@ QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WindowFlags fl )
5056
connect( this, &QgsCredentialDialog::credentialsRequestedMasterPassword,
5157
this, &QgsCredentialDialog::requestCredentialsMasterPassword,
5258
Qt::BlockingQueuedConnection );
53-
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
54-
QPushButton *ignoreButton { buttonBox->button( QDialogButtonBox::StandardButton::Ignore ) };
55-
ignoreButton->setToolTip( tr( "All requests for this connection will be automatically rejected for the next 60 seconds." ) );
5659

57-
// Keep a cache of ignored connections, and ignore them for 60 seconds.
58-
connect( ignoreButton, &QPushButton::clicked, [ = ]( bool )
60+
// Setup ignore button
61+
mIgnoreButton->setToolTip( tr( "All requests for this connection will be automatically rejected" ) );
62+
QMenu *menu = new QMenu( mIgnoreButton );
63+
QAction *ignoreTemporarily = new QAction( tr( "Ignore for 10 seconds" ), menu );
64+
ignoreTemporarily->setToolTip( tr( "All requests for this connection will be automatically rejected for 10 seconds" ) );
65+
QAction *ignoreForSession = new QAction( tr( "Ignore for session" ), menu );
66+
ignoreForSession->setToolTip( tr( "All requests for this connection will be automatically rejected for the duration of the current session" ) );
67+
menu->addAction( ignoreTemporarily );
68+
menu->addAction( ignoreForSession );
69+
connect( ignoreTemporarily, &QAction::triggered, [ = ]
70+
{
71+
mIgnoreMode = IgnoreTemporarily;
72+
//mIgnoreButton->setText( ignoreTemporarily->text() );
73+
mIgnoreButton->setToolTip( ignoreTemporarily->toolTip() );
74+
} );
75+
connect( ignoreForSession, &QAction::triggered, [ = ]
76+
{
77+
mIgnoreMode = IgnoreForSession;
78+
//mIgnoreButton->setText( ignoreForSession->text() );
79+
mIgnoreButton->setToolTip( ignoreForSession->toolTip() );
80+
} );
81+
mIgnoreButton->setText( mIgnoreMode == IgnoreTemporarily ? ignoreTemporarily->text() : ignoreForSession->text() );
82+
mIgnoreButton->setToolTip( mIgnoreMode == IgnoreTemporarily ? ignoreTemporarily->toolTip() : ignoreForSession->toolTip() );
83+
mIgnoreButton->setMenu( menu );
84+
mIgnoreButton->setMaximumHeight( mOkButton->sizeHint().height() );
85+
86+
// Connect ok and cancel buttons
87+
connect( mOkButton, &QPushButton::clicked, this, &QgsCredentialDialog::accept );
88+
connect( mCancelButton, &QPushButton::clicked, this, &QgsCredentialDialog::reject );
89+
90+
// Keep a cache of ignored connections, and ignore them for 10 seconds.
91+
connect( mIgnoreButton, &QPushButton::clicked, [ = ]( bool )
5992
{
6093
const QString realm { labelRealm->text() };
6194
{
6295
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
6396
// Insert the realm in the cache of ignored connections
64-
sIgnoredConnectionsCache.insert( realm );
97+
sIgnoredConnectionsCache->insert( realm );
6598
}
66-
QTimer::singleShot( 60000, [ = ]()
99+
if ( mIgnoreMode == IgnoreTemporarily )
67100
{
68-
QgsDebugMsgLevel( QStringLiteral( "Removing ignored connection from cache: %1" ).arg( realm ), 4 );
69-
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
70-
sIgnoredConnectionsCache.remove( realm );
71-
} );
101+
QTimer::singleShot( 10000, [ = ]()
102+
{
103+
QgsDebugMsgLevel( QStringLiteral( "Removing ignored connection from cache: %1" ).arg( realm ), 4 );
104+
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
105+
sIgnoredConnectionsCache->remove( realm );
106+
} );
107+
}
72108
accept( );
73109
} );
74110

@@ -100,15 +136,15 @@ void QgsCredentialDialog::requestCredentials( const QString &realm, QString *use
100136
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
101137
{
102138
QMutexLocker locker( &sIgnoredConnectionsCacheMutex );
103-
if ( sIgnoredConnectionsCache.contains( realm ) )
139+
if ( sIgnoredConnectionsCache->contains( realm ) )
104140
{
105141
QgsDebugMsg( QStringLiteral( "Skipping ignored connection: " ) + realm );
106142
*ok = false;
107143
return;
108144
}
109145
}
110146
stackedWidget->setCurrentIndex( 0 );
111-
buttonBox->button( QDialogButtonBox::StandardButton::Ignore )->show();
147+
mIgnoreButton->show();
112148
chkbxPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
113149
labelRealm->setText( realm );
114150
leUsername->setText( *username );
@@ -161,7 +197,7 @@ void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password, b
161197
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
162198
stackedWidget->setCurrentIndex( 1 );
163199

164-
buttonBox->button( QDialogButtonBox::StandardButton::Ignore )->hide();
200+
mIgnoreButton->hide();
165201
leMasterPass->setFocus();
166202

167203
QString titletxt( stored ? tr( "Enter CURRENT master authentication password" ) : tr( "Set NEW master authentication password" ) );

‎src/gui/qgscredentialdialog.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,21 @@ class GUI_EXPORT QgsCredentialDialog : public QDialog, public QgsCredentials, pr
6363
bool requestMasterPassword( QString &password SIP_INOUT, bool stored = false ) override;
6464

6565
private:
66-
QPushButton *mOkButton = nullptr;
67-
//! Temporary cache for ignored connections, to avoid GUI freezing by multiple credentials requests to the same connection
68-
static QSet<QString> sIgnoredConnectionsCache;
66+
67+
/**
68+
* The ConnectionsIgnoreMode enum represent the modes a connection can be ignored
69+
*/
70+
enum ConnectionsIgnoreMode
71+
{
72+
IgnoreTemporarily, //!< Ignore for a certain amount of time
73+
IgnoreForSession, //!< Ignore for the whole QGIS session
74+
};
75+
76+
//! mutex for the static ignored connections cache
6977
static QMutex sIgnoredConnectionsCacheMutex;
7078

79+
ConnectionsIgnoreMode mIgnoreMode = ConnectionsIgnoreMode::IgnoreTemporarily;
80+
7181
};
7282

7383
#endif

‎src/ui/qgscredentialdialog.ui

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,15 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>396</width>
9+
<width>358</width>
1010
<height>293</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Enter Credentials</string>
1515
</property>
1616
<layout class="QFormLayout" name="formLayout">
17-
<property name="fieldGrowthPolicy">
18-
<enum>QFormLayout::ExpandingFieldsGrow</enum>
19-
</property>
20-
<item row="3" column="0" colspan="2">
21-
<widget class="QDialogButtonBox" name="buttonBox">
22-
<property name="orientation">
23-
<enum>Qt::Horizontal</enum>
24-
</property>
25-
<property name="standardButtons">
26-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ignore|QDialogButtonBox::Ok</set>
27-
</property>
28-
</widget>
29-
</item>
30-
<item row="1" column="0" colspan="2">
17+
<item row="0" column="0">
3118
<widget class="QStackedWidget" name="stackedWidget">
3219
<property name="currentIndex">
3320
<number>1</number>
@@ -212,6 +199,62 @@ font-style: italic;
212199
</widget>
213200
</widget>
214201
</item>
202+
<item row="1" column="0" colspan="2">
203+
<layout class="QHBoxLayout" name="horizontalLayout">
204+
<item>
205+
<spacer name="horizontalSpacer">
206+
<property name="orientation">
207+
<enum>Qt::Horizontal</enum>
208+
</property>
209+
<property name="sizeHint" stdset="0">
210+
<size>
211+
<width>40</width>
212+
<height>20</height>
213+
</size>
214+
</property>
215+
</spacer>
216+
</item>
217+
<item>
218+
<widget class="QPushButton" name="mOkButton">
219+
<property name="text">
220+
<string>Ok</string>
221+
</property>
222+
</widget>
223+
</item>
224+
<item>
225+
<widget class="QToolButton" name="mIgnoreButton">
226+
<property name="sizePolicy">
227+
<sizepolicy hsizetype="Minimum" vsizetype="Maximum">
228+
<horstretch>0</horstretch>
229+
<verstretch>0</verstretch>
230+
</sizepolicy>
231+
</property>
232+
<property name="focusPolicy">
233+
<enum>Qt::StrongFocus</enum>
234+
</property>
235+
<property name="toolTip">
236+
<string>All requests for this connection will be automatically rejected for the next 60 seconds</string>
237+
</property>
238+
<property name="text">
239+
<string>Ignore</string>
240+
</property>
241+
<property name="popupMode">
242+
<enum>QToolButton::MenuButtonPopup</enum>
243+
</property>
244+
<property name="toolButtonStyle">
245+
<enum>Qt::ToolButtonTextOnly</enum>
246+
</property>
247+
</widget>
248+
</item>
249+
<item>
250+
<widget class="QPushButton" name="mCancelButton">
251+
<property name="text">
252+
<string>Cancel</string>
253+
</property>
254+
</widget>
255+
</item>
256+
</layout>
257+
</item>
215258
</layout>
216259
</widget>
217260
<customwidgets>
@@ -229,38 +272,5 @@ font-style: italic;
229272
<tabstop>chkbxEraseAuthDb</tabstop>
230273
</tabstops>
231274
<resources/>
232-
<connections>
233-
<connection>
234-
<sender>buttonBox</sender>
235-
<signal>accepted()</signal>
236-
<receiver>QgsCredentialDialog</receiver>
237-
<slot>accept()</slot>
238-
<hints>
239-
<hint type="sourcelabel">
240-
<x>248</x>
241-
<y>254</y>
242-
</hint>
243-
<hint type="destinationlabel">
244-
<x>157</x>
245-
<y>274</y>
246-
</hint>
247-
</hints>
248-
</connection>
249-
<connection>
250-
<sender>buttonBox</sender>
251-
<signal>rejected()</signal>
252-
<receiver>QgsCredentialDialog</receiver>
253-
<slot>reject()</slot>
254-
<hints>
255-
<hint type="sourcelabel">
256-
<x>316</x>
257-
<y>260</y>
258-
</hint>
259-
<hint type="destinationlabel">
260-
<x>286</x>
261-
<y>274</y>
262-
</hint>
263-
</hints>
264-
</connection>
265-
</connections>
275+
<connections/>
266276
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.