#include <keychain.h>
using namespace OCC;
+using namespace QKeychain;
Q_LOGGING_CATEGORY(lcProxy, "nextcloud.gui.credentials.proxy", QtInfoMsg)
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";
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;
}
_readPasswordJob->setInsecureFallback(false);
_readPasswordJob->setKey(keychainPasswordKey());
_readPasswordJob->setAutoDelete(false);
- connect(_readPasswordJob.data(), &QKeychain::Job::finished, &waitLoop, &QEventLoop::quit);
_readPasswordJob->start();
}
// 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";
void ProxyAuthHandler::storeCredsInKeychain()
{
- using namespace QKeychain;
- QEventLoop waitLoop;
-
if (_waitingForKeychain) {
return;
}
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) {