From: Michael Schuster Date: Tue, 30 Jun 2020 04:02:58 +0000 (+0200) Subject: ProxyAuthHandler: Add template member function execAwait to avoid code duplication X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~222^2^2~111^2~1 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=ff631e919f7a70c7b205919dd831a816a14897b9;p=nextcloud-desktop.git ProxyAuthHandler: Add template member function execAwait to avoid code duplication Signed-off-by: Michael Schuster --- diff --git a/src/gui/proxyauthhandler.cpp b/src/gui/proxyauthhandler.cpp index 010093b54..0506572fb 100644 --- a/src/gui/proxyauthhandler.cpp +++ b/src/gui/proxyauthhandler.cpp @@ -24,6 +24,7 @@ #include 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 +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) { diff --git a/src/gui/proxyauthhandler.h b/src/gui/proxyauthhandler.h index ccc6d824b..e1a291411 100644 --- a/src/gui/proxyauthhandler.h +++ b/src/gui/proxyauthhandler.h @@ -71,6 +71,12 @@ private: /// Stores the current credentials in the keychain. void storeCredsInKeychain(); + template + void execAwait(const T *sender, + PointerToMemberFunction signal, + int &counter, + const QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents); + QString keychainUsernameKey() const; QString keychainPasswordKey() const;