Commit c42d1e83 authored by Laurent Montel's avatar Laurent Montel 😁

Port to qt5keychain

parent bfeff212
Pipeline #41291 failed with stage
in 18 minutes and 6 seconds
......@@ -27,7 +27,8 @@
#include "pop3resource_debug.h"
#include <QTimer>
#include <qt5keychain/keychain.h>
using namespace QKeychain;
using namespace Akonadi;
using namespace MailTransport;
using namespace KWallet;
......@@ -60,8 +61,6 @@ POP3Resource::POP3Resource(const QString &id)
POP3Resource::~POP3Resource()
{
mSettings.save();
delete mWallet;
mWallet = nullptr;
}
void POP3Resource::configurationChanged()
......@@ -126,24 +125,18 @@ QString POP3Resource::buildLabelForPasswordDialog(const QString &detailedError)
return queryText;
}
void POP3Resource::walletOpenedForLoading(bool success)
void POP3Resource::walletOpenedForLoading(QKeychain::Job *baseJob)
{
bool passwordLoaded = success;
if (success) {
if (mWallet && mWallet->isOpen() && mWallet->hasFolder(QStringLiteral("pop3"))) {
mWallet->setFolder(QStringLiteral("pop3"));
if (mWallet->hasEntry(identifier())) {
mWallet->readPassword(identifier(), mPassword);
} else {
passwordLoaded = false;
}
} else {
passwordLoaded = false;
}
auto *job = qobject_cast<ReadPasswordJob *>(baseJob);
bool passwordLoaded = false;
Q_ASSERT(job);
if (!job->error()) {
mPassword = job->textData();
passwordLoaded = true;
} else {
passwordLoaded = false;
qCWarning(POP3RESOURCE_LOG) << "We have an error during reading password " << job->errorString();
}
delete mWallet;
mWallet = nullptr;
if (!passwordLoaded) {
const QString queryText = buildLabelForPasswordDialog(
i18n("You are asked here because the password could not be loaded from the wallet."));
......@@ -153,25 +146,6 @@ void POP3Resource::walletOpenedForLoading(bool success)
}
}
void POP3Resource::walletOpenedForSaving(bool success)
{
if (success) {
if (mWallet && mWallet->isOpen()) {
if (!mWallet->hasFolder(QStringLiteral("pop3"))) {
mWallet->createFolder(QStringLiteral("pop3"));
}
mWallet->setFolder(QStringLiteral("pop3"));
mWallet->writePassword(identifier(), mPassword);
}
} else {
qCWarning(POP3RESOURCE_LOG) << "Unable to write the password to the wallet.";
}
delete mWallet;
mWallet = nullptr;
finish();
}
void POP3Resource::showPasswordDialog(const QString &queryText)
{
QPointer<KPasswordDialog> dlg
......@@ -262,19 +236,16 @@ void POP3Resource::doStateStep()
const bool loadPasswordFromWallet = !mAskAgain && passwordNeeded && !mSettings.login().isEmpty()
&& mPassword.isEmpty();
if (loadPasswordFromWallet) {
mWallet = Wallet::openWallet(Wallet::NetworkWallet(), winIdForDialogs(),
Wallet::Asynchronous);
}
if (loadPasswordFromWallet && mWallet) {
connect(mWallet, &KWallet::Wallet::walletOpened, this, &POP3Resource::walletOpenedForLoading);
auto readJob = new ReadPasswordJob(QStringLiteral("pop3"), this);
connect(readJob, &QKeychain::Job::finished, this, &POP3Resource::walletOpenedForLoading);
readJob->setKey(identifier());
readJob->start();
} else if (passwordNeeded && (mPassword.isEmpty() || mAskAgain)) {
QString detail;
if (mAskAgain) {
detail = i18n("You are asked here because the previous login was not successful.");
} else if (mSettings.login().isEmpty()) {
detail = i18n("You are asked here because the username you supplied is empty.");
} else if (!mWallet) {
detail = i18n("You are asked here because the wallet password storage is disabled.");
}
showPasswordDialog(buildLabelForPasswordDialog(detail));
......@@ -384,13 +355,16 @@ void POP3Resource::doStateStep()
if (mSavePassword) {
qCDebug(POP3RESOURCE_LOG) << "Writing password back to the wallet.";
Q_EMIT status(Running, i18n("Saving password to the wallet."));
mWallet = Wallet::openWallet(Wallet::NetworkWallet(), winIdForDialogs(),
Wallet::Asynchronous);
if (mWallet) {
connect(mWallet, &KWallet::Wallet::walletOpened, this, &POP3Resource::walletOpenedForSaving);
} else {
auto writeJob = new WritePasswordJob(QStringLiteral("pop3"), this);
connect(writeJob, &QKeychain::Job::finished, this, [this](QKeychain::Job *baseJob) {
if (baseJob->error()) {
qCWarning(POP3RESOURCE_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
}
finish();
}
});
writeJob->setKey(identifier());
writeJob->setTextData(mPassword);
writeJob->start();
} else {
finish();
}
......@@ -969,8 +943,6 @@ void POP3Resource::resetState()
mIntervalCheckInProgress = false;
mSavePassword = false;
updateIntervalTimer();
delete mWallet;
mWallet = nullptr;
if (mPopSession) {
// Closing the POP session means the KIO slave will get disconnected, which
......@@ -1007,12 +979,9 @@ void POP3Resource::clearCachedPassword()
void POP3Resource::cleanup()
{
if (mWallet && mWallet->isOpen() && mWallet->hasFolder(QStringLiteral("pop3"))) {
mWallet->setFolder(QStringLiteral("pop3"));
if (mWallet->hasEntry(identifier())) {
mWallet->removeEntry(identifier());
}
}
auto deleteJob = new DeletePasswordJob(QStringLiteral("pop3"));
deleteJob->setKey(identifier());
deleteJob->start();
ResourceBase::cleanup();
}
......@@ -1026,8 +995,6 @@ void POP3Resource::doSetOnline(bool online)
cancelSync(i18n("Mail check aborted after going offline."), false /* no error */);
}
Q_EMIT status(Idle, i18n("Offline"));
delete mWallet;
mWallet = nullptr;
clearCachedPassword();
}
}
......
......@@ -19,13 +19,11 @@ class DeleteJob;
namespace Akonadi {
class ItemCreateJob;
}
namespace KWallet {
class Wallet;
}
class POPSession;
class QTimer;
namespace QKeychain {
class Job;
}
class POP3Resource : public Akonadi::ResourceBase, public Akonadi::AgentBase::Observer
{
Q_OBJECT
......@@ -68,7 +66,7 @@ private Q_SLOTS:
void precommandResult(KJob *job);
// For state RequestPassword
void walletOpenedForLoading(bool success);
void walletOpenedForLoading(QKeychain::Job *baseJob);
// For state Login
void loginJobResult(KJob *job);
......@@ -93,9 +91,6 @@ private Q_SLOTS:
// For state Quit
void quitJobResult(KJob *job);
// For state SavePassword
void walletOpenedForSaving(bool success);
private:
enum State {
......@@ -140,7 +135,6 @@ private:
QString mPassword;
bool mSavePassword = false;
bool mTestLocalInbox = false;
KWallet::Wallet *mWallet = nullptr;
// Maps IDs on the server to message sizes on the server
QMap<int, int> mIdsToSizeMap;
......
......@@ -38,7 +38,7 @@ void Settings::setPassword(const QString &password)
qCWarning(POP3RESOURCE_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
}
});
writeJob->setKey(mIdentifier);
writeJob->setKey(mResourceId);
writeJob->setTextData(password);
writeJob->start();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment