Commit 8f9f19a8 authored by Albert Astals Cid's avatar Albert Astals Cid

Introduce the password provider class

Instead of using the dbus interface of resuces to get the passwords

Differential Revision: https://phabricator.kde.org/D6207
parent 8e8d4316
......@@ -268,6 +268,7 @@ ecm_generate_headers(ksieveui_Camelcaseutil_HEADERS
HEADER_NAMES
Util
SieveImapAccountSettings
SieveImapPasswordProvider
REQUIRED_HEADERS ksieveui_util_HEADERS
PREFIX KSieveUi
RELATIVE util
......
......@@ -32,9 +32,10 @@
using namespace KSieveUi;
SieveDebugDialog::SieveDebugDialog(QWidget *parent)
SieveDebugDialog::SieveDebugDialog(SieveImapPasswordProvider* passwordProvider, QWidget *parent)
: QDialog(parent)
, mSieveJob(nullptr)
, mPasswordProvider(passwordProvider)
, mShutDownJob(nullptr)
{
setWindowTitle(i18n("Sieve Diagnostics"));
......@@ -121,7 +122,7 @@ void SieveDebugDialog::slotDiagNextAccount()
mEdit->editor()->appendPlainText(i18n("------------------------------------------------------------\n"));
// Detect URL for this IMAP account
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(ident);
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(ident, mPasswordProvider);
const QUrl url = info.sieveUrl;
if (!url.isValid()) {
mEdit->editor()->appendPlainText(i18n("(Account does not support Sieve)\n\n"));
......@@ -158,7 +159,7 @@ void SieveDebugDialog::slotDiagNextScript()
mEdit->editor()->appendPlainText(i18n("Contents of script '%1':\n", scriptFile));
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(mResourceIdentifier.first());
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(mResourceIdentifier.first(), mPasswordProvider);
mUrl = info.sieveUrl;
mUrl = mUrl.adjusted(QUrl::RemoveFilename);
......
......@@ -29,6 +29,9 @@ namespace KPIMTextEdit {
class PlainTextEditorWidget;
}
namespace KSieveUi {
class SieveImapPasswordProvider;
/**
* Diagnostic info for Sieve. Only compiled when debug is enabled, it is
* not useful enough for non-developers to have this in releases.
......@@ -37,7 +40,7 @@ class KSIEVEUI_EXPORT SieveDebugDialog : public QDialog
{
Q_OBJECT
public:
explicit SieveDebugDialog(QWidget *parent = nullptr);
explicit SieveDebugDialog(SieveImapPasswordProvider* passwordProvider, QWidget *parent = nullptr);
~SieveDebugDialog();
Q_SIGNALS:
......@@ -58,6 +61,7 @@ private:
QUrl mUrl;
KSyntaxHighlighting::Repository mRepo;
SieveImapPasswordProvider* mPasswordProvider;
KPIMTextEdit::PlainTextEditorWidget *mEdit;
QStringList mResourceIdentifier;
QStringList mScriptList;
......
......@@ -76,7 +76,7 @@ public:
bool mWasActive : 1;
};
ManageSieveScriptsDialog::ManageSieveScriptsDialog(QWidget *parent)
ManageSieveScriptsDialog::ManageSieveScriptsDialog(SieveImapPasswordProvider *passwordProvider, QWidget *parent)
: QDialog(parent)
, d(new KSieveUi::ManageSieveScriptsDialogPrivate)
{
......@@ -93,7 +93,7 @@ ManageSieveScriptsDialog::ManageSieveScriptsDialog(QWidget *parent)
vlay->setSpacing(0);
vlay->setMargin(0);
d->mTreeView = new CustomManageSieveWidget(frame);
d->mTreeView = new CustomManageSieveWidget(passwordProvider, frame);
connect(d->mTreeView, &CustomManageSieveWidget::editScript, this, &ManageSieveScriptsDialog::slotEditScript);
connect(d->mTreeView, &CustomManageSieveWidget::newScript, this, &ManageSieveScriptsDialog::slotNewScript);
connect(d->mTreeView, &CustomManageSieveWidget::updateButtons, this, &ManageSieveScriptsDialog::slotUpdateButtons);
......
......@@ -33,12 +33,13 @@ class SieveJob;
namespace KSieveUi {
class SieveEditor;
class ManageSieveScriptsDialogPrivate;
class SieveImapPasswordProvider;
class KSIEVEUI_EXPORT ManageSieveScriptsDialog : public QDialog
{
Q_OBJECT
public:
explicit ManageSieveScriptsDialog(QWidget *parent = nullptr);
explicit ManageSieveScriptsDialog(SieveImapPasswordProvider* passwordProvider, QWidget *parent = nullptr);
~ManageSieveScriptsDialog();
Q_SIGNALS:
......
......@@ -84,18 +84,6 @@ QString AbstractAkonadiImapSettingInterface::sieveAlternateUrl() const
return {};
}
QString AbstractAkonadiImapSettingInterface::password(const QString &identifier) const
{
Q_UNUSED(identifier);
return {};
}
QString AbstractAkonadiImapSettingInterface::sieveCustomPassword(const QString &identifier) const
{
Q_UNUSED(identifier);
return {};
}
int AbstractAkonadiImapSettingInterface::imapPort() const
{
return -1;
......
......@@ -40,8 +40,6 @@ public:
virtual int alternateAuthentication() const;
virtual int authentication() const;
virtual QString sieveAlternateUrl() const;
virtual QString password(const QString &identifier) const;
virtual QString sieveCustomPassword(const QString &identifier) const;
virtual int imapPort() const;
};
}
......
......@@ -96,29 +96,6 @@ QString KSieveUi::AkonadiImapSettingInterface::sieveAlternateUrl() const
return mInterface->sieveAlternateUrl();
}
QString AkonadiImapSettingInterface::sieveCustomPassword(const QString &identifier) const
{
QString pwd;
QDBusInterface resourceSettings(QLatin1String("org.freedesktop.Akonadi.Resource.") + identifier, QStringLiteral("/Settings"), QStringLiteral("org.kde.Akonadi.Imap.Wallet"));
QDBusReply<QString> replyPass = resourceSettings.call(QStringLiteral("sieveCustomPassword"));
if (replyPass.isValid()) {
pwd = replyPass;
}
return pwd;
}
QString AkonadiImapSettingInterface::password(const QString &identifier) const
{
QDBusInterface resourceSettings(QLatin1String("org.freedesktop.Akonadi.Resource.") + identifier, QStringLiteral("/Settings"), QStringLiteral("org.kde.Akonadi.Imap.Wallet"));
QString pwd;
QDBusReply<QString> replyPass = resourceSettings.call(QStringLiteral("password"));
if (replyPass.isValid()) {
pwd = replyPass;
}
return pwd;
}
int KSieveUi::AkonadiImapSettingInterface::imapPort() const
{
return mInterface->imapPort();
......
......@@ -43,8 +43,6 @@ public:
int alternateAuthentication() const override;
int authentication() const override;
QString sieveAlternateUrl() const override;
QString password(const QString &identifier) const override;
QString sieveCustomPassword(const QString &identifier) const override;
int imapPort() const override;
private:
......
......@@ -89,18 +89,6 @@ int AkonadiImapSettingInterfaceTest::alternateAuthentication() const
return mData.alternateAuthentication;
}
QString AkonadiImapSettingInterfaceTest::password(const QString &identifier) const
{
Q_UNUSED(identifier);
return mData.password;
}
QString AkonadiImapSettingInterfaceTest::sieveCustomPassword(const QString &identifier) const
{
Q_UNUSED(identifier);
return mData.sieveCustomPassword;
}
int AkonadiImapSettingInterfaceTest::imapPort() const
{
return mData.imapPort;
......
......@@ -58,8 +58,6 @@ public:
int alternateAuthentication() const override;
int authentication() const override;
QString sieveAlternateUrl() const override;
QString password(const QString &identifier) const override;
QString sieveCustomPassword(const QString &identifier) const override;
int imapPort() const override;
private:
AkonadiImapSettingInterfaceDataTest mData;
......
......@@ -22,9 +22,31 @@
#include "imapresourcesettings.h"
#include "akonadiimapsettinginterfacetest.h"
#include "../abstractakonadiimapsettinginterface.h"
#include "../sieveimappasswordprovider.h"
#include <KSieveUi/SieveImapAccountSettings>
#include <QTest>
class DataImapPasswordProvider : public KSieveUi::SieveImapPasswordProvider
{
public:
DataImapPasswordProvider(AkonadiImapSettingInterfaceDataTest *data)
: mData(data)
{
}
QString password(const QString &/*identifier*/) override
{
return mData->password;
}
QString sieveCustomPassword(const QString &/*identifier*/) override
{
return mData->sieveCustomPassword;
}
AkonadiImapSettingInterfaceDataTest *mData;
};
UtilMethodTest::UtilMethodTest(QObject *parent)
: QObject(parent)
{
......@@ -33,7 +55,7 @@ UtilMethodTest::UtilMethodTest(QObject *parent)
void UtilMethodTest::shouldReturnEmptyInfo()
{
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AbstractAkonadiImapSettingInterface);
KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("foo"), false, interface);
KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("dummy"), {}, false, interface);
QVERIFY(!info.sieveImapAccountSettings.isValid());
}
......@@ -521,7 +543,8 @@ void UtilMethodTest::shouldAssignValue()
QFETCH(bool, useVacationFile);
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new AkonadiImapSettingInterfaceTest(data));
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("foo"), useVacationFile, interface);
std::unique_ptr<DataImapPasswordProvider> provider(new DataImapPasswordProvider(&data));
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("foo"), provider.get(), useVacationFile, interface);
QCOMPARE(info.sieveImapAccountSettings.isValid(), sieveImapAccountValid);
QCOMPARE(info, accountInfo);
}
......
/*
Copyright (C) 2017 Albert Astals Cid <aacid@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef SIEVEIMAPPASSWORDPROVIDER_H
#define SIEVEIMAPPASSWORDPROVIDER_H
#include "ksieveui_export.h"
class QString;
namespace KSieveUi
{
class KSIEVEUI_EXPORT SieveImapPasswordProvider
{
public:
virtual ~SieveImapPasswordProvider() = default;
virtual QString password(const QString &identifier) = 0;
virtual QString sieveCustomPassword(const QString &identifier) = 0;
};
}
#endif
......@@ -41,6 +41,7 @@
#include "libksieve_debug.h"
#include <PimCommon/PimUtil>
#include "imapresourcesettings.h"
#include "sieveimappasswordprovider.h"
#include "sieve-vacation.h"
#include "sieveimapinstance/sieveimapinstanceinterfacemanager.h"
#include "sieveimapinstance/sieveimapinstance.h"
......@@ -52,15 +53,15 @@
using namespace KSieveUi;
KSieveUi::Util::AccountInfo KSieveUi::Util::fullAccountInfo(const QString &identifier, bool withVacationFileName)
KSieveUi::Util::AccountInfo KSieveUi::Util::fullAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName)
{
std::unique_ptr<OrgKdeAkonadiImapSettingsInterface> interfaceImap(PimCommon::Util::createImapSettingsInterface(identifier));
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AkonadiImapSettingInterface(interfaceImap));
KSieveUi::Util::AccountInfo accountInfo = KSieveUi::Util::findAccountInfo(identifier, withVacationFileName, interface);
KSieveUi::Util::AccountInfo accountInfo = KSieveUi::Util::findAccountInfo(identifier, provider, withVacationFileName, interface);
return accountInfo;
}
KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &identifier, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface)
KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface)
{
KSieveUi::Util::AccountInfo accountInfo;
if (!interface) {
......@@ -90,7 +91,7 @@ KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &ident
u.setHost(server);
u.setUserName(userName);
const QString pwd = interface->password(identifier);
const QString pwd = provider->password(identifier);
u.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
......@@ -155,7 +156,7 @@ KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &ident
accountInfo.sieveImapAccountSettings.setServerName(server);
accountInfo.sieveImapAccountSettings.setUserName(userName);
accountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)interface->authentication()));
const QString pwd = interface->password(identifier);
const QString pwd = provider->password(identifier);
accountInfo.sieveImapAccountSettings.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
......@@ -208,10 +209,10 @@ KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &ident
const QString resultCustomAuthentication = interface->sieveCustomAuthentification();
if (resultCustomAuthentication == QLatin1String("ImapUserPassword")) {
u.setUserName(interface->userName());
const QString pwd = interface->password(identifier);
const QString pwd = provider->password(identifier);
u.setPassword(pwd);
} else if (resultCustomAuthentication == QLatin1String("CustomUserPassword")) {
const QString pwd = interface->sieveCustomPassword(identifier);
const QString pwd = provider->sieveCustomPassword(identifier);
u.setPassword(pwd);
u.setUserName(interface->sieveCustomUsername());
} else {
......
......@@ -50,6 +50,7 @@ class QStringList;
namespace KSieveUi {
class AbstractAkonadiImapSettingInterface;
class SieveImapPasswordProvider;
/**
* The Util namespace contains a collection of helper functions use in
* various places.
......@@ -79,11 +80,11 @@ bool hasKep14Support(const QStringList &sieveCapabilities, const QStringList &av
*/
bool isKep14ProtectedName(const QString &scriptName);
KSIEVEUI_TESTS_EXPORT KSieveUi::Util::AccountInfo findAccountInfo(const QString &identifier, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface);
KSIEVEUI_TESTS_EXPORT KSieveUi::Util::AccountInfo findAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface);
/**
* Returns all sieve account info with the given @p identifier.
*/
KSIEVEUI_TESTS_EXPORT KSieveUi::Util::AccountInfo fullAccountInfo(const QString &identifier, bool withVacationFileName = true);
KSIEVEUI_TESTS_EXPORT KSieveUi::Util::AccountInfo fullAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName = true);
}
}
......
......@@ -28,8 +28,9 @@
#include "libksieve_debug.h"
using namespace KSieveUi;
MultiImapVacationManager::MultiImapVacationManager(QObject *parent)
MultiImapVacationManager::MultiImapVacationManager(SieveImapPasswordProvider* passwordProvider, QObject *parent)
: QObject(parent)
, mPasswordProvider(passwordProvider)
, mNumberOfJobs(0)
, mCheckInProgress(false)
{
......@@ -48,7 +49,7 @@ QMap <QString, QUrl> MultiImapVacationManager::serverList() const
continue;
}
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(instance.identifier());
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(instance.identifier(), mPasswordProvider);
const QUrl url = info.sieveUrl;
if (!url.isEmpty()) {
list.insert(instance.name(), url);
......
......@@ -30,11 +30,12 @@ class QUrl;
namespace KSieveUi {
class CheckKolabKep14SupportJob;
class VacationCheckJob;
class SieveImapPasswordProvider;
class KSIEVEUI_EXPORT MultiImapVacationManager : public QObject
{
Q_OBJECT
public:
explicit MultiImapVacationManager(QObject *parent = nullptr);
explicit MultiImapVacationManager(SieveImapPasswordProvider* passwordProvider, QObject *parent = nullptr);
~MultiImapVacationManager();
void checkVacation();
......@@ -51,6 +52,7 @@ private Q_SLOTS:
void slotCheckKep14Ended(KSieveUi::CheckKolabKep14SupportJob *job, bool success);
private:
SieveImapPasswordProvider* mPasswordProvider;
int mNumberOfJobs;
bool mCheckInProgress;
......
......@@ -27,8 +27,9 @@
using namespace KSieveUi;
Vacation::Vacation(QObject *parent, bool checkOnly, const QUrl &url)
Vacation::Vacation(SieveImapPasswordProvider* passwordProvider, QObject *parent, bool checkOnly, const QUrl &url)
: QObject(parent)
, mPasswordProvider(passwordProvider)
, mSieveJob(nullptr)
, mDialog(nullptr)
, mWasActive(false)
......@@ -76,7 +77,7 @@ QUrl Vacation::findURL(QString &serverName) const
continue;
}
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(instance.identifier());
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(instance.identifier(), mPasswordProvider);
const QUrl url = info.sieveUrl;
if (!url.isEmpty()) {
serverName = instance.name();
......
......@@ -26,13 +26,14 @@ class SieveJob;
namespace KSieveUi {
class VacationDialog;
class SieveImapPasswordProvider;
class Vacation : public QObject
{
Q_OBJECT
public:
explicit Vacation(QObject *parent = nullptr, bool checkonly = false, const QUrl &url = QUrl());
explicit Vacation(SieveImapPasswordProvider* passwordProvider, QObject *parent = nullptr, bool checkonly = false, const QUrl &url = QUrl());
virtual ~Vacation();
bool isUsable() const;
......@@ -58,6 +59,7 @@ protected Q_SLOTS:
void slotPutInactiveResult(KManageSieve::SieveJob *, bool);
protected:
// IO:
SieveImapPasswordProvider* mPasswordProvider;
KManageSieve::SieveJob *mSieveJob;
QUrl mUrl;
QString mServerName;
......
......@@ -44,11 +44,11 @@ public:
bool mQuestionAsked;
};
VacationManager::VacationManager(QWidget *parent)
VacationManager::VacationManager(SieveImapPasswordProvider* passwordProvider, QWidget *parent)
: QObject(parent)
, d(new KSieveUi::VacationManagerPrivate(parent))
{
d->mCheckVacation = new KSieveUi::MultiImapVacationManager(this);
d->mCheckVacation = new KSieveUi::MultiImapVacationManager(passwordProvider, this);
connect(d->mCheckVacation.data(), &KSieveUi::MultiImapVacationManager::scriptActive, this, &VacationManager::updateVacationScriptStatus);
connect(d->mCheckVacation.data(), &KSieveUi::MultiImapVacationManager::scriptActive, this, &VacationManager::slotUpdateVacationScriptStatus);
}
......
......@@ -27,11 +27,12 @@ class QWidget;
namespace KSieveUi {
class VacationManagerPrivate;
class SieveImapInstanceInterface;
class SieveImapPasswordProvider;
class KSIEVEUI_EXPORT VacationManager : public QObject
{
Q_OBJECT
public:
explicit VacationManager(QWidget *parent);
explicit VacationManager(SieveImapPasswordProvider* passwordProvider, QWidget *parent);
~VacationManager();
void checkVacation();
......
......@@ -27,8 +27,9 @@
using namespace KSieveUi;
CustomManageSieveWidget::CustomManageSieveWidget(QWidget *parent)
CustomManageSieveWidget::CustomManageSieveWidget(SieveImapPasswordProvider* passwordProvider, QWidget *parent)
: KSieveUi::ManageSieveWidget(parent)
, mPasswordProvider(passwordProvider)
{
}
......@@ -50,7 +51,7 @@ bool CustomManageSieveWidget::refreshList()
last = new SieveTreeWidgetItem(treeView(), last);
last->setIcon(0, QIcon::fromTheme(QStringLiteral("network-server")));
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(type.identifier(), false);
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(type.identifier(), mPasswordProvider, false);
const QUrl u = info.sieveUrl;
if (u.isEmpty()) {
QTreeWidgetItem *item = new QTreeWidgetItem(last);
......
......@@ -21,15 +21,19 @@
#include <widgets/managesievewidget.h>
namespace KSieveUi {
class SieveImapPasswordProvider;
class CustomManageSieveWidget : public KSieveUi::ManageSieveWidget
{
Q_OBJECT
public:
explicit CustomManageSieveWidget(QWidget *parent = nullptr);
explicit CustomManageSieveWidget(SieveImapPasswordProvider* passwordProvider, QWidget *parent = nullptr);
virtual ~CustomManageSieveWidget();
protected:
bool refreshList() override;
private:
SieveImapPasswordProvider* mPasswordProvider;
};
}
......
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