From 68fcd3e123e9c0e345d95728d0c8742e53be940a Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 11 Apr 2016 08:39:43 +0200 Subject: Use ConfigStore for accounts --- common/CMakeLists.txt | 1 + common/configstore.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++ common/configstore.h | 68 +++++++++++++++++++++++++++++++++ common/resourceconfig.cpp | 70 ---------------------------------- common/resourceconfig.h | 12 ------ common/resourcefacade.cpp | 18 ++++----- common/resourcefacade.h | 3 ++ tests/accountstest.cpp | 6 +++ 8 files changed, 183 insertions(+), 91 deletions(-) create mode 100644 common/configstore.cpp create mode 100644 common/configstore.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a47d259..54d86f3 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -58,6 +58,7 @@ set(command_SRCS typeindex.cpp resourcefacade.cpp resourceconfig.cpp + configstore.cpp resultset.cpp domain/applicationdomaintype.cpp domain/event.cpp diff --git a/common/configstore.cpp b/common/configstore.cpp new file mode 100644 index 0000000..a8469ba --- /dev/null +++ b/common/configstore.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * 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 2 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, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "configstore.h" + +#include +#include +#include +#include +#include + +static QSharedPointer getConfig(const QByteArray &identifier) +{ + return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/" + identifier + ".ini", QSettings::IniFormat); +} + +ConfigStore::ConfigStore(const QByteArray &identifier) + : mIdentifier(identifier), + mConfig(getConfig(identifier)) +{ + +} + +QMap ConfigStore::getEntries() +{ + QMap resources; + for (const auto &identifier : mConfig->childGroups()) { + mConfig->beginGroup(identifier); + const auto type = mConfig->value("type").toByteArray(); + resources.insert(identifier.toLatin1(), type); + mConfig->endGroup(); + } + return resources; +} + +void ConfigStore::add(const QByteArray &identifier, const QByteArray &type) +{ + Trace() << "Adding " << identifier; + mConfig->beginGroup(QString::fromLatin1(identifier)); + mConfig->setValue("type", type); + mConfig->endGroup(); + mConfig->sync(); +} + +void ConfigStore::remove(const QByteArray &identifier) +{ + Trace() << "Removing " << identifier; + mConfig->beginGroup(QString::fromLatin1(identifier)); + mConfig->remove(""); + mConfig->endGroup(); + mConfig->sync(); + QFile::remove(getConfig(identifier)->fileName()); +} + +void ConfigStore::clear() +{ + mConfig->clear(); + mConfig->sync(); +} + +void ConfigStore::modify(const QByteArray &identifier, const QMap &configuration) +{ + Trace() << "Modifying " << identifier; + auto config = getConfig(identifier); + config->clear(); + for (const auto &key : configuration.keys()) { + config->setValue(key, configuration.value(key)); + } + config->sync(); +} + +QMap ConfigStore::get(const QByteArray &identifier) +{ + QMap configuration; + auto config = getConfig(identifier); + for (const auto &key : config->allKeys()) { + configuration.insert(key.toLatin1(), config->value(key)); + } + return configuration; +} + diff --git a/common/configstore.h b/common/configstore.h new file mode 100644 index 0000000..2f51f63 --- /dev/null +++ b/common/configstore.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * 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 2 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, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include "sink_export.h" +#include +#include +#include +#include +#include +#include + +class SINK_EXPORT ConfigStore +{ +public: + ConfigStore(const QByteArray &identifier); + + /** + * Returns all entries with their type. + */ + QMap getEntries(); + + /** + * Create an entry with a type. + */ + void add(const QByteArray &identifier, const QByteArray &type); + + /** + * Remove an entry. + */ + void remove(const QByteArray &identifier); + + /** + * Remove all entries + */ + void clear(); + + /** + * Modify the configuration of an entry. + */ + void modify(const QByteArray &identifier, const QMap &configuration); + + /** + * Get the configuration of an entry. + */ + QMap get(const QByteArray &identifier); + +private: + QByteArray mIdentifier; + QSharedPointer mConfig; +}; diff --git a/common/resourceconfig.cpp b/common/resourceconfig.cpp index fa94f3c..cfde4e9 100644 --- a/common/resourceconfig.cpp +++ b/common/resourceconfig.cpp @@ -97,73 +97,3 @@ QMap ResourceConfig::getConfiguration(const QByteArray &id } return configuration; } - - -QByteArray AccountConfig::newIdentifier(const QByteArray &type) -{ - auto settings = getConfig("accounts"); - const auto counter = settings->value("instanceCounter", 0).toInt() + 1; - const QByteArray identifier = type + ".instance" + QByteArray::number(counter); - settings->setValue("instanceCounter", counter); - settings->sync(); - return identifier; -} - -void AccountConfig::addAccount(const QByteArray &identifier, const QByteArray &type) -{ - auto settings = getConfig("accounts"); - settings->beginGroup(QString::fromLatin1(identifier)); - settings->setValue("type", type); - settings->endGroup(); - settings->sync(); -} - -void AccountConfig::removeAccount(const QByteArray &identifier) -{ - auto settings = getConfig("accounts"); - settings->beginGroup(QString::fromLatin1(identifier)); - settings->remove(""); - settings->endGroup(); - settings->sync(); - QFile::remove(getConfig(identifier)->fileName()); -} - -QMap AccountConfig::getAccounts() -{ - QMap accounts; - auto settings = getConfig("accounts"); - for (const auto &identifier : settings->childGroups()) { - settings->beginGroup(identifier); - const auto type = settings->value("type").toByteArray(); - accounts.insert(identifier.toLatin1(), type); - settings->endGroup(); - } - return accounts; -} - -void AccountConfig::clear() -{ - auto settings = getConfig("accounts"); - settings->clear(); - settings->sync(); -} - -void AccountConfig::configureAccount(const QByteArray &identifier, const QMap &configuration) -{ - auto config = getConfig(identifier); - config->clear(); - for (const auto &key : configuration.keys()) { - config->setValue(key, configuration.value(key)); - } - config->sync(); -} - -QMap AccountConfig::getConfiguration(const QByteArray &identifier) -{ - QMap configuration; - auto config = getConfig(identifier); - for (const auto &key : config->allKeys()) { - configuration.insert(key.toLatin1(), config->value(key)); - } - return configuration; -} diff --git a/common/resourceconfig.h b/common/resourceconfig.h index 505b5ec..2108caa 100644 --- a/common/resourceconfig.h +++ b/common/resourceconfig.h @@ -36,15 +36,3 @@ public: static void configureResource(const QByteArray &identifier, const QMap &configuration); static QMap getConfiguration(const QByteArray &identifier); }; - -class SINK_EXPORT AccountConfig -{ -public: - static QMap getAccounts(); - static QByteArray newIdentifier(const QByteArray &type); - static void addAccount(const QByteArray &identifier, const QByteArray &type); - static void removeAccount(const QByteArray &identifier); - static void clear(); - static void configureAccount(const QByteArray &identifier, const QMap &configuration); - static QMap getConfiguration(const QByteArray &identifier); -}; diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 4bf4962..2969c44 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp @@ -152,7 +152,7 @@ static Sink::ApplicationDomain::SinkAccount::Ptr readAccountFromConfig(const QBy auto account = Sink::ApplicationDomain::SinkAccount::Ptr::create(id); account->setProperty("type", type); - const auto configurationValues = AccountConfig::getConfiguration(id); + const auto configurationValues = ConfigStore("accounts").get(id); for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) { account->setProperty(it.key(), it.value()); } @@ -160,7 +160,7 @@ static Sink::ApplicationDomain::SinkAccount::Ptr readAccountFromConfig(const QBy } -AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade() +AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade(), mConfigStore("accounts") { } @@ -174,8 +174,8 @@ KAsync::Job AccountFacade::create(const Sink::ApplicationDomain::SinkAccou const QByteArray type = account.getProperty("type").toByteArray(); const QByteArray providedIdentifier = account.getProperty("identifier").toByteArray(); // It is currently a requirement that the account starts with the type - const QByteArray identifier = providedIdentifier.isEmpty() ? AccountConfig::newIdentifier(type) : providedIdentifier; - AccountConfig::addAccount(identifier, type); + const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier; + mConfigStore.add(identifier, type); auto changedProperties = account.changedProperties(); changedProperties.removeOne("identifier"); changedProperties.removeOne("type"); @@ -185,7 +185,7 @@ KAsync::Job AccountFacade::create(const Sink::ApplicationDomain::SinkAccou for (const auto &property : changedProperties) { configurationValues.insert(property, account.getProperty(property)); } - AccountConfig::configureAccount(identifier, configurationValues); + mConfigStore.modify(identifier, configurationValues); } sConfig->add(readAccountFromConfig(identifier, type)); }); @@ -208,10 +208,10 @@ KAsync::Job AccountFacade::modify(const Sink::ApplicationDomain::SinkAccou for (const auto &property : changedProperties) { configurationValues.insert(property, account.getProperty(property)); } - AccountConfig::configureAccount(identifier, configurationValues); + mConfigStore.modify(identifier, configurationValues); } - const auto type = AccountConfig::getAccounts().value(identifier); + const auto type = mConfigStore.getEntries().value(identifier); sConfig->modify(readAccountFromConfig(identifier, type)); }); } @@ -224,7 +224,7 @@ KAsync::Job AccountFacade::remove(const Sink::ApplicationDomain::SinkAccou Warning() << "We need an \"identifier\" property to identify the account to configure"; return; } - AccountConfig::removeAccount(identifier); + mConfigStore.remove(identifier); sConfig->remove(Sink::ApplicationDomain::SinkAccount::Ptr::create(account)); }); } @@ -237,7 +237,7 @@ QPair, typename Sink::ResultEmittersetFetcher([](const QSharedPointer &) {}); resultProvider->onDone([=]() { delete resultProvider; delete guard; }); auto job = KAsync::start([=]() { - const auto configuredAccounts = AccountConfig::getAccounts(); + const auto configuredAccounts = mConfigStore.getEntries(); for (const auto &res : configuredAccounts.keys()) { const auto type = configuredAccounts.value(res); if (!query.ids.isEmpty() && !query.ids.contains(res)) { diff --git a/common/resourcefacade.h b/common/resourcefacade.h index 4d0e597..0deb017 100644 --- a/common/resourcefacade.h +++ b/common/resourcefacade.h @@ -24,6 +24,7 @@ #include #include "common/resultprovider.h" #include "common/domain/applicationdomaintype.h" +#include "common/configstore.h" namespace Sink { class Query; @@ -50,6 +51,8 @@ public: KAsync::Job modify(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE; KAsync::Job remove(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE; QPair, typename Sink::ResultEmitter::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; +private: + ConfigStore mConfigStore; }; class ConfigNotifier : public QObject diff --git a/tests/accountstest.cpp b/tests/accountstest.cpp index 0ae10ec..dc74d15 100644 --- a/tests/accountstest.cpp +++ b/tests/accountstest.cpp @@ -5,6 +5,7 @@ #include #include +#include class AccountsTest : public QObject { @@ -14,6 +15,7 @@ private slots: void initTestCase() { Sink::Test::initTest(); + Sink::Log::setDebugOutputLevel(Sink::Log::Trace); } void testLoad() @@ -24,6 +26,8 @@ private slots: QString accountName("name"); QString accountIcon("icon"); auto account = ApplicationDomainType::createEntity(); + //FIXME Get rid of this line + account.setProperty("identifier", account.identifier()); account.setProperty("type", "maildir"); account.setProperty("name", accountName); account.setProperty("icon", accountIcon); @@ -38,6 +42,8 @@ private slots: QString smtpUsername("smtpUsername"); QString smtpPassword("smtpPassword"); auto resource = ApplicationDomainType::createEntity(); + //FIXME Get rid of this line + resource.setProperty("identifier", resource.identifier()); resource.setProperty("type", "org.kde.mailtransport"); resource.setProperty("account", account.identifier()); resource.setProperty("server", smtpServer); -- cgit v1.2.3