From 0a19d42799307a3822b6d04f3c1c5d6f547e753f Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 13 Nov 2021 14:57:24 +0100 Subject: [PATCH] Improve handling of closed keychain --- qml/main.qml | 2 +- src/controller.cpp | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/qml/main.qml b/qml/main.qml index e79fdbb6e..10d8cb9d7 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -335,7 +335,7 @@ Kirigami.ApplicationWindow { } function onGlobalErrorOccured(error, detail) { - showPassiveNotification(i18nc("%1: %2", error, detail)); + showPassiveNotification(i18n("%1: %2", error, detail)); } function onShowWindow() { diff --git a/src/controller.cpp b/src/controller.cpp index d769e5d7c..46e77d506 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -311,23 +311,30 @@ QByteArray Controller::loadAccessTokenFromFile(const AccountSettings &account) QByteArray Controller::loadAccessTokenFromKeyChain(const AccountSettings &account) { - qDebug() << "Read the access token from the keychain for " << account.userId(); - QKeychain::ReadPasswordJob job(qAppName()); - job.setAutoDelete(false); - job.setKey(account.userId()); - QEventLoop loop; - QKeychain::ReadPasswordJob::connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit); - job.start(); - loop.exec(); + QKeychain::Error error; + QString errorString; + do { + qDebug() << "Reading access token from the keychain for" << account.userId(); + QKeychain::ReadPasswordJob job(qAppName()); + job.setAutoDelete(false); + job.setKey(account.userId()); + QEventLoop loop; + QKeychain::ReadPasswordJob::connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit); + job.start(); + loop.exec(); - if (job.error() == QKeychain::Error::NoError) { - return job.binaryData(); - } + if (job.error() == QKeychain::Error::NoError) { + return job.binaryData(); + } + Q_EMIT globalErrorOccured(i18n("Unable to read access token"), i18n("Please make sure that the keychain is opened.")); + error = job.error(); + errorString = job.errorString(); + } while (error == QKeychain::Error::OtherError); - qWarning() << "Could not read the access token from the keychain: " << qPrintable(job.errorString()); + qWarning() << "Could not read the access token from the keychain:" << errorString; // no access token from the keychain, try token file auto accessToken = loadAccessTokenFromFile(account); - if (job.error() == QKeychain::Error::EntryNotFound) { + if (error == QKeychain::Error::EntryNotFound) { if (!accessToken.isEmpty()) { qDebug() << "Migrating the access token from file to the keychain for " << account.userId(); bool removed = false;