From dcdc95b6d709616498de00389bd19926625b20f2 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 7 Jun 2016 09:16:04 +0200 Subject: An imap accounts plugin --- accounts/CMakeLists.txt | 1 + accounts/imap/CMakeLists.txt | 48 ++++ accounts/imap/imapaccountplugin.cpp | 29 +++ accounts/imap/imapaccountplugin.h | 31 +++ accounts/imap/imapsettings.cpp | 250 +++++++++++++++++++++ accounts/imap/imapsettings.h | 74 ++++++ .../package/contents/ui/ImapAccountSettings.qml | 164 ++++++++++++++ accounts/imap/package/metadata.desktop | 8 + accounts/imap/qmldir | 3 + accounts/imap/tests/CMakeLists.txt | 6 + accounts/imap/tests/settingstest.cpp | 87 +++++++ 11 files changed, 701 insertions(+) create mode 100644 accounts/imap/CMakeLists.txt create mode 100644 accounts/imap/imapaccountplugin.cpp create mode 100644 accounts/imap/imapaccountplugin.h create mode 100644 accounts/imap/imapsettings.cpp create mode 100644 accounts/imap/imapsettings.h create mode 100644 accounts/imap/package/contents/ui/ImapAccountSettings.qml create mode 100644 accounts/imap/package/metadata.desktop create mode 100644 accounts/imap/qmldir create mode 100644 accounts/imap/tests/CMakeLists.txt create mode 100644 accounts/imap/tests/settingstest.cpp (limited to 'accounts') diff --git a/accounts/CMakeLists.txt b/accounts/CMakeLists.txt index 25fa7f67..212a62ff 100644 --- a/accounts/CMakeLists.txt +++ b/accounts/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(maildir) +add_subdirectory(imap) diff --git a/accounts/imap/CMakeLists.txt b/accounts/imap/CMakeLists.txt new file mode 100644 index 00000000..afff7102 --- /dev/null +++ b/accounts/imap/CMakeLists.txt @@ -0,0 +1,48 @@ +project(kube-accounts-imap) + +cmake_minimum_required(VERSION 2.8.12) + +include(CPack) +include(FeatureSummary) +find_package(PkgConfig) + +################# set KDE specific information ################# + +find_package(ECM 0.0.8 REQUIRED NO_MODULE) + +# where to look first for cmake modules, before ${CMAKE_ROOT}/Modules/ is checked +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR}) + +include(KDEInstallDirs) +include(KDECMakeSettings) +include(KDECompilerSettings) + +find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Quick Test Gui) + +set (QT_MIN_VERSION "5.4.0") +find_package(Sink CONFIG REQUIRED) +find_package(KF5Async CONFIG REQUIRED) +find_package(KF5 REQUIRED COMPONENTS Package) + +include_directories(SYSTEM ${KDE_INSTALL_FULL_INCLUDEDIR}/KF5/) +#FIXME +include_directories(../../framework/) + +set(SRCS + imapsettings.cpp + imapaccountplugin.cpp +) + +add_library(imapaccountplugin SHARED ${SRCS}) +qt5_use_modules(imapaccountplugin Core Quick Qml) +target_link_libraries(imapaccountplugin sink settingsplugin) + +add_library(imapaccount_static STATIC ${SRCS}) +qt5_use_modules(imapaccount_static Core Quick Qml) +target_link_libraries(imapaccount_static sink settingsplugin) +add_subdirectory(tests) + +kpackage_install_package(package org.kube.accounts.imap "genericqml") + +install(TARGETS imapaccountplugin DESTINATION ${QML_INSTALL_DIR}/org/kube/accounts/imap) +install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kube/accounts/imap) diff --git a/accounts/imap/imapaccountplugin.cpp b/accounts/imap/imapaccountplugin.cpp new file mode 100644 index 00000000..781c9a5e --- /dev/null +++ b/accounts/imap/imapaccountplugin.cpp @@ -0,0 +1,29 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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. +*/ +#include "imapaccountplugin.h" + +#include "imapsettings.h" + +#include + +void ImapAccountPlugin::registerTypes (const char *uri) +{ + Q_ASSERT(uri == QLatin1String("org.kube.accounts.imap")); + qmlRegisterType(uri, 1, 0, "ImapSettings"); +} diff --git a/accounts/imap/imapaccountplugin.h b/accounts/imap/imapaccountplugin.h new file mode 100644 index 00000000..7977efeb --- /dev/null +++ b/accounts/imap/imapaccountplugin.h @@ -0,0 +1,31 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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. +*/ +#pragma once + +#include +#include + +class ImapAccountPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + virtual void registerTypes(const char *uri); +}; diff --git a/accounts/imap/imapsettings.cpp b/accounts/imap/imapsettings.cpp new file mode 100644 index 00000000..7d93606d --- /dev/null +++ b/accounts/imap/imapsettings.cpp @@ -0,0 +1,250 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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. +*/ +#include "imapsettings.h" + +#include + +#include +#include +#include +#include + +ImapSettings::ImapSettings(QObject *parent) + : QObject(parent) +{ +} + + +void ImapSettings::setAccountIdentifier(const QByteArray &id) +{ + if (id.isEmpty()) { + return; + } + mAccountIdentifier = id; + + //Clear + mIcon = QString(); + mName = QString(); + mImapServer = QString(); + mImapUsername = QString(); + mImapPassword = QString(); + mSmtpServer = QString(); + mSmtpUsername = QString(); + mSmtpPassword = QString(); + emit changed(); + emit imapResourceChanged(); + emit smtpResourceChanged(); + + Q_ASSERT(!id.isEmpty()); + Sink::Store::fetchOne(Sink::Query::IdentityFilter(id)) + .then([this](const Sink::ApplicationDomain::SinkAccount &account) { + mIcon = account.getProperty("icon").toString(); + mName = account.getProperty("name").toString(); + emit changed(); + }).exec(); + + Sink::Store::fetchOne(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter("storage")) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mIdentifier = resource.identifier(); + mImapServer = resource.getProperty("server").toString(); + mImapUsername = resource.getProperty("username").toString(); + mImapPassword = resource.getProperty("password").toString(); + emit imapResourceChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the imap resource: " << errorMessage; + }).exec(); + + Sink::Store::fetchOne(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter("transport")) + .then([this](const Sink::ApplicationDomain::SinkResource &resource) { + mMailtransportIdentifier = resource.identifier(); + mSmtpServer = resource.getProperty("server").toString(); + mSmtpUsername = resource.getProperty("username").toString(); + mSmtpPassword = resource.getProperty("password").toString(); + emit smtpResourceChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the smtp resource: " << errorMessage; + }).exec(); + + //FIXME this assumes that we only ever have one identity per account + Sink::Store::fetchOne(Sink::Query::AccountFilter(id)) + .then([this](const Sink::ApplicationDomain::Identity &identity) { + mIdentityIdentifier = identity.identifier(); + mUsername = identity.getProperty("username").toString(); + mEmailAddress = identity.getProperty("address").toString(); + emit identityChanged(); + }, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Failed to find the identity resource: " << errorMessage; + }).exec(); +} + +QByteArray ImapSettings::accountIdentifier() const +{ + return mAccountIdentifier; +} + +QValidator *ImapSettings::imapServerValidator() const +{ + class ImapServerValidator : public QValidator { + State validate(QString &input, int &pos) const { + Q_UNUSED(pos); + // imaps://mainserver.example.net:475 + const QUrl url(input); + static QSet validProtocols = QSet() << "imap" << "imaps"; + if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { + return Acceptable; + } else { + return Intermediate; + } + } + }; + static ImapServerValidator *validator = new ImapServerValidator; + return validator; +} + +QValidator *ImapSettings::smtpServerValidator() const +{ + class SmtpServerValidator : public QValidator { + State validate(QString &input, int &pos) const { + Q_UNUSED(pos); + // smtps://mainserver.example.net:475 + const QUrl url(input); + static QSet validProtocols = QSet() << "smtp" << "smtps"; + if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { + return Acceptable; + } else { + return Intermediate; + } + } + }; + static SmtpServerValidator *validator = new SmtpServerValidator; + return validator; +} + +void ImapSettings::save() +{ + qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; + Q_ASSERT(!mAccountIdentifier.isEmpty()); + Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); + account.setProperty("type", "imap"); + account.setProperty("name", mName); + account.setProperty("icon", mIcon); + Q_ASSERT(!account.identifier().isEmpty()); + Sink::Store::modify(account).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating account: " << errorMessage; + }) + .exec(); + + if (!mIdentifier.isEmpty()) { + Sink::ApplicationDomain::SinkResource resource(mIdentifier); + resource.setProperty("server", mImapServer); + resource.setProperty("username", mImapUsername); + resource.setProperty("password", mImapPassword); + Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while modifying resource: " << errorMessage; + }) + .exec(); + } else { + auto resource = Sink::ApplicationDomain::ImapResource::create(mAccountIdentifier); + mIdentifier = resource.identifier(); + resource.setProperty("server", mImapServer); + resource.setProperty("username", mImapUsername); + resource.setProperty("password", mImapPassword); + Sink::Store::create(resource).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating resource: " << errorMessage; + }) + .exec(); + } + + if (!mMailtransportIdentifier.isEmpty()) { + Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier); + resource.setProperty("server", mSmtpServer); + resource.setProperty("username", mSmtpUsername); + resource.setProperty("password", mSmtpPassword); + Sink::Store::modify(resource).then([](){}, [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while modifying resource: " << errorMessage; + }) + .exec(); + } else { + auto resource = Sink::ApplicationDomain::MailtransportResource::create(mAccountIdentifier); + mMailtransportIdentifier = resource.identifier(); + resource.setProperty("server", mSmtpServer); + resource.setProperty("username", mSmtpUsername); + resource.setProperty("password", mSmtpPassword); + Sink::Store::create(resource).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating resource: " << errorMessage; + }) + .exec(); + } + + if (!mIdentityIdentifier.isEmpty()) { + Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier); + identity.setProperty("username", mUsername); + identity.setProperty("address", mEmailAddress); + Sink::Store::modify(identity).then([](){}, [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while modifying identity: " << errorMessage; + }) + .exec(); + } else { + auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity(); + mIdentityIdentifier = identity.identifier(); + identity.setProperty("account", mAccountIdentifier); + identity.setProperty("username", mUsername); + identity.setProperty("address", mEmailAddress); + Sink::Store::create(identity).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while creating identity: " << errorMessage; + }) + .exec(); + } +} + +void ImapSettings::remove() +{ + if (mIdentifier.isEmpty()) { + qWarning() << "We're missing an identifier"; + } else { + Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer::create()); + Sink::Store::remove(mailTransportResource).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing resource: " << errorMessage; + }) + .exec(); + + Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer::create()); + Sink::Store::remove(resource).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing resource: " << errorMessage; + }) + .exec(); + + Sink::ApplicationDomain::SinkAccount account("", mAccountIdentifier, 0, QSharedPointer::create()); + Sink::Store::remove(account).then([]() {}, + [](int errorCode, const QString &errorMessage) { + qWarning() << "Error while removing account: " << errorMessage; + }) + .exec(); + } +} + diff --git a/accounts/imap/imapsettings.h b/accounts/imap/imapsettings.h new file mode 100644 index 00000000..30bd67d1 --- /dev/null +++ b/accounts/imap/imapsettings.h @@ -0,0 +1,74 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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. +*/ +#pragma once + +#include +#include + +class ImapSettings : public QObject +{ + Q_OBJECT + Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier) + Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed) + Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed) + Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged) + Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged) + Q_PROPERTY(QString imapServer MEMBER mImapServer NOTIFY imapResourceChanged) + Q_PROPERTY(QValidator* imapServerValidator READ imapServerValidator CONSTANT) + Q_PROPERTY(QString imapUsername MEMBER mImapUsername NOTIFY imapResourceChanged) + Q_PROPERTY(QString imapPassword MEMBER mImapPassword NOTIFY imapResourceChanged) + Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) + Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) + Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) + Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) + +public: + ImapSettings(QObject *parent = 0); + + void setAccountIdentifier(const QByteArray &); + QByteArray accountIdentifier() const; + + QValidator *imapServerValidator() const; + QValidator *smtpServerValidator() const; + + Q_INVOKABLE void save(); + Q_INVOKABLE void remove(); + +signals: + void imapResourceChanged(); + void smtpResourceChanged(); + void identityChanged(); + void changed(); + +private: + QByteArray mIdentifier; + QByteArray mAccountIdentifier; + QByteArray mMailtransportIdentifier; + QByteArray mIdentityIdentifier; + QString mIcon; + QString mName; + QString mUsername; + QString mEmailAddress; + QString mImapServer; + QString mImapUsername; + QString mImapPassword; + QString mSmtpServer; + QString mSmtpUsername; + QString mSmtpPassword; +}; diff --git a/accounts/imap/package/contents/ui/ImapAccountSettings.qml b/accounts/imap/package/contents/ui/ImapAccountSettings.qml new file mode 100644 index 00000000..4dfa3ba4 --- /dev/null +++ b/accounts/imap/package/contents/ui/ImapAccountSettings.qml @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2016 Michael Bohlender + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +import QtQuick 2.4 +import QtQuick.Controls 1.4 +import QtQuick.Layouts 1.1 +import QtQuick.Dialogs 1.0 + +import org.kube.framework.settings 1.0 as KubeSettings +import org.kube.framework.theme 1.0 +import org.kube.accounts.imap 1.0 as ImapAccount + + +Rectangle { + id: root + property string accountId + property string accountName + property string icon + + color: ColorPalette.background + + GridLayout { + id: gridLayout + columns: 2 + Layout.fillWidth: true + + Text { + Layout.columnSpan: 2 + Layout.fillWidth: true + text: "General:" + } + + Label { text: "Account Name" } + TextField { + id: name + placeholderText: accountName + Layout.fillWidth: true + text: imapSettings.accountName + onTextChanged: { imapSettings.accountName = text; root.accountName = text; } + } + + Label { text: "User Name" } + TextField { + placeholderText: "Your Name" + Layout.fillWidth: true + text: imapSettings.userName + onTextChanged: { imapSettings.userName = text; } + } + + Label { text: "Email Address" } + TextField { + placeholderText: "Your EMail Address" + Layout.fillWidth: true + text: imapSettings.emailAddress + onTextChanged: { imapSettings.emailAddress = text; } + } + + Text { + Layout.columnSpan: 2 + Layout.fillWidth: true + text: "Imap:" + } + + Label { text: "Username" } + TextField { + placeholderText: "Username" + Layout.fillWidth: true + text: imapSettings.imapUsername + onTextChanged: { imapSettings.imapUsername = text; } + } + + Label { text: "Password" } + TextField { + placeholderText: "Password" + Layout.fillWidth: true + text: imapSettings.imapPassword + onTextChanged: { imapSettings.imapPassword = text; } + } + + Label { text: "Server" } + TextField { + id: server + placeholderText: "imaps://mainserver.example.net:993" + Layout.fillWidth: true + text: imapSettings.imapServer + onTextChanged: { imapSettings.imapServer = text; } + validator: imapSettings.imapServerValidator + Rectangle { + anchors.fill: parent + opacity: 0.2 + color: server.acceptableInput ? "green" : "yellow" + } + } + + Text { + Layout.columnSpan: 2 + Layout.fillWidth: true + text: "Smtp:" + } + + Label { text: "Username" } + TextField { + placeholderText: "Username" + Layout.fillWidth: true + text: imapSettings.smtpUsername + onTextChanged: { imapSettings.smtpUsername = text; } + } + + Label { text: "Password" } + TextField { + placeholderText: "Password" + Layout.fillWidth: true + text: imapSettings.smtpPassword + onTextChanged: { imapSettings.smtpPassword = text; } + } + + Label { text: "Server" } + TextField { + id: server + placeholderText: "smtps://mainserver.example.net:465" + Layout.fillWidth: true + text: imapSettings.smtpServer + onTextChanged: { imapSettings.smtpServer = text; } + validator: imapSettings.smtpServerValidator + Rectangle { + anchors.fill: parent + opacity: 0.2 + color: server.acceptableInput ? "green" : "yellow" + } + } + + ImapAccount.ImapSettings { + id: imapSettings + accountIdentifier: accountId + } + + Button { + text: "Save" + onClicked: { + imapSettings.save(); + } + } + Button { + text: "Remove" + onClicked: { + imapSettings.remove(); + } + } + } +} diff --git a/accounts/imap/package/metadata.desktop b/accounts/imap/package/metadata.desktop new file mode 100644 index 00000000..7c4320c3 --- /dev/null +++ b/accounts/imap/package/metadata.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Name=Kube Imap Accounts Plugin +X-KDE-PluginInfo-Name=org.kube.accounts.imap +Exec=kpackagelauncherqml -a org.kube.accounts.imap +X-Plasma-MainScript=ui/ImapAccountSettings.qml +X-KDE-ServiceTypes=KPackage/GenericQML +Icon=folder +Type=Service diff --git a/accounts/imap/qmldir b/accounts/imap/qmldir new file mode 100644 index 00000000..5e4861c0 --- /dev/null +++ b/accounts/imap/qmldir @@ -0,0 +1,3 @@ +module org.kube.accounts.maildir + +plugin maildiraccountplugin diff --git a/accounts/imap/tests/CMakeLists.txt b/accounts/imap/tests/CMakeLists.txt new file mode 100644 index 00000000..9c914370 --- /dev/null +++ b/accounts/imap/tests/CMakeLists.txt @@ -0,0 +1,6 @@ +include_directories(../) +cmake_policy(SET CMP0063 NEW) +add_executable(imapsettingstest settingstest.cpp) +add_test(imapsettingstest settingstest) +qt5_use_modules(imapsettingstest Core Test Concurrent) +target_link_libraries(imapsettingstest sink imapaccount_static) diff --git a/accounts/imap/tests/settingstest.cpp b/accounts/imap/tests/settingstest.cpp new file mode 100644 index 00000000..923f7e14 --- /dev/null +++ b/accounts/imap/tests/settingstest.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "imapsettings.h" + +class SettingsTest : public QObject +{ + Q_OBJECT +private slots: + + void initTestCase() + { + Sink::Test::initTest(); + } + + void testLoad() + { + auto accountId = "accountid"; + auto imapServer = QString("imapserver"); + auto imapUsername = QString("username"); + auto imapPassword = QString("password"); + auto smtpServer = QString("smtpserver"); + auto smtpUsername = QString("username"); + auto smtpPassword = QString("password"); + auto username = QString("username"); + auto emailAddress = QString("emailAddress"); + + ImapSettings settings; + settings.setAccountIdentifier(accountId); + settings.setProperty("imapServer", imapServer); + settings.setProperty("imapUsername", imapUsername); + settings.setProperty("imapPassword", imapPassword); + settings.setProperty("smtpServer", smtpServer); + settings.setProperty("smtpUsername", smtpUsername); + settings.setProperty("smtpPassword", smtpPassword); + settings.setProperty("userName", username); + settings.setProperty("emailAddress", emailAddress); + settings.save(); + + Sink::Store::fetchAll(Sink::Query()).then>([](const QList &resources) { + QCOMPARE(resources.size(), 2); + }) + .exec().waitForFinished(); + + //Ensure we can read back all the information using the accountid + { + ImapSettings readSettings; + QSignalSpy spy(&readSettings, &ImapSettings::imapResourceChanged); + QSignalSpy spy1(&readSettings, &ImapSettings::smtpResourceChanged); + readSettings.setAccountIdentifier(accountId); + //Once for clear and once for the new setting + QTRY_COMPARE(spy.count(), 2); + QTRY_COMPARE(spy1.count(), 2); + QVERIFY(!readSettings.accountIdentifier().isEmpty()); + QCOMPARE(readSettings.property("imapServer").toString(), imapServer); + QCOMPARE(readSettings.property("imapUsername").toString(), imapUsername); + QCOMPARE(readSettings.property("imapPassword").toString(), imapPassword); + QCOMPARE(readSettings.property("smtpServer").toString(), smtpServer); + QCOMPARE(readSettings.property("smtpUsername").toString(), smtpUsername); + QCOMPARE(readSettings.property("smtpPassword").toString(), smtpPassword); + QCOMPARE(readSettings.property("userName").toString(), smtpUsername); + QCOMPARE(readSettings.property("emailAddress").toString(), emailAddress); + } + + { + ImapSettings settings; + QSignalSpy spy(&settings, &ImapSettings::imapResourceChanged); + settings.setAccountIdentifier(accountId); + QTRY_COMPARE(spy.count(), 2); + settings.remove(); + } + + Sink::Store::fetchAll(Sink::Query()).then>([](const QList &resources) { + QCOMPARE(resources.size(), 0); + }) + .exec().waitForFinished(); + } +}; + +QTEST_GUILESS_MAIN(SettingsTest) +#include "settingstest.moc" -- cgit v1.2.3