ProxyAuthHandler: Add template member function execAwait to avoid code duplication
authorMichael Schuster <michael@schuster.ms>
Tue, 30 Jun 2020 04:02:58 +0000 (06:02 +0200)
committerMichael Schuster <michael@schuster.ms>
Mon, 6 Jul 2020 19:51:40 +0000 (21:51 +0200)
Signed-off-by: Michael Schuster <michael@schuster.ms>
src/gui/proxyauthhandler.cpp
src/gui/proxyauthhandler.h

index 010093b54c38da3b911944c67f19ddc966420e46..0506572fbadee1d8c1c1143809bca6656c53db4a 100644 (file)
@@ -24,6 +24,7 @@
 #include <keychain.h>
 
 using namespace OCC;
+using namespace QKeychain;
 
 Q_LOGGING_CATEGORY(lcProxy, "nextcloud.gui.credentials.proxy", QtInfoMsg)
 
@@ -139,24 +140,22 @@ void ProxyAuthHandler::slotSenderDestroyed(QObject *obj)
 
 bool ProxyAuthHandler::getCredsFromDialog()
 {
-    QEventLoop waitLoop;
-
     // Open the credentials dialog
     if (!_waitingForDialog) {
         _dialog->reset();
         _dialog->setProxyAddress(_proxy);
-        connect(_dialog, &QDialog::finished, &waitLoop, &QEventLoop::quit);
         _dialog->open();
     }
 
     // This function can be reentered while the dialog is open.
     // If that's the case, continue processing the dialog until
     // it's done.
-    ++_waitingForDialog;
-    if (_dialog) {
-        waitLoop.exec(QEventLoop::ExcludeSocketNotifiers);
+    if(_dialog) {
+        execAwait(_dialog.data(),
+                  &QDialog::finished,
+                  _waitingForDialog,
+                  QEventLoop::ExcludeSocketNotifiers);
     }
-    --_waitingForDialog;
 
     if (_dialog && _dialog->result() == QDialog::Accepted) {
         qCInfo(lcProxy) << "got creds for" << _proxy << "from dialog";
@@ -167,11 +166,25 @@ bool ProxyAuthHandler::getCredsFromDialog()
     return false;
 }
 
-bool ProxyAuthHandler::getCredsFromKeychain()
+template<class T, typename PointerToMemberFunction>
+void ProxyAuthHandler::execAwait(const T *sender,
+                                 PointerToMemberFunction signal,
+                                 int &counter,
+                                 const QEventLoop::ProcessEventsFlags flags)
 {
-    using namespace QKeychain;
+    if(!sender)
+        return;
+
     QEventLoop waitLoop;
+    connect(sender, signal, &waitLoop, &QEventLoop::quit);
 
+    ++counter;
+    waitLoop.exec(flags);
+    --counter;
+}
+
+bool ProxyAuthHandler::getCredsFromKeychain()
+{
     if (_waitingForDialog) {
         return false;
     }
@@ -189,7 +202,6 @@ bool ProxyAuthHandler::getCredsFromKeychain()
         _readPasswordJob->setInsecureFallback(false);
         _readPasswordJob->setKey(keychainPasswordKey());
         _readPasswordJob->setAutoDelete(false);
-        connect(_readPasswordJob.data(), &QKeychain::Job::finished, &waitLoop, &QEventLoop::quit);
         _readPasswordJob->start();
     }
 
@@ -197,9 +209,9 @@ bool ProxyAuthHandler::getCredsFromKeychain()
     // This really needs the counter and the flag here, because otherwise we get
     // bad behavior when we reenter this code after the flag has been switched
     // but before the while loop has finished.
-    ++_waitingForKeychain;
-    waitLoop.exec();
-    --_waitingForKeychain;
+    execAwait(_readPasswordJob.data(),
+              &QKeychain::Job::finished,
+              _waitingForKeychain);
 
     if (_readPasswordJob->error() == NoError) {
         qCInfo(lcProxy) << "got creds for" << _proxy << "from keychain";
@@ -216,9 +228,6 @@ bool ProxyAuthHandler::getCredsFromKeychain()
 
 void ProxyAuthHandler::storeCredsInKeychain()
 {
-    using namespace QKeychain;
-    QEventLoop waitLoop;
-
     if (_waitingForKeychain) {
         return;
     }
@@ -233,12 +242,11 @@ void ProxyAuthHandler::storeCredsInKeychain()
     job->setKey(keychainPasswordKey());
     job->setTextData(_password);
     job->setAutoDelete(false);
-    connect(job, &QKeychain::Job::finished, &waitLoop, &QEventLoop::quit);
     job->start();
 
-    ++_waitingForKeychain;
-    waitLoop.exec();
-    --_waitingForKeychain;
+    execAwait(job,
+              &QKeychain::Job::finished,
+              _waitingForKeychain);
 
     job->deleteLater();
     if (job->error() != NoError) {
index ccc6d824b2ce17f6d90d4b260e5fa94b9e9fd268..e1a29141115aeb050f15698d107f47a88e0be181 100644 (file)
@@ -71,6 +71,12 @@ private:
     /// Stores the current credentials in the keychain.
     void storeCredsInKeychain();
 
+    template<class T, typename PointerToMemberFunction>
+    void execAwait(const T *sender,
+                   PointerToMemberFunction signal,
+                   int &counter,
+                   const QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
+
     QString keychainUsernameKey() const;
     QString keychainPasswordKey() const;