summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-11 08:39:43 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-11 08:39:43 +0200
commit68fcd3e123e9c0e345d95728d0c8742e53be940a (patch)
treee4e79abf7f3f2262871675a12f37d38f60fad71e
parent9f5e4a488360c2c0232a12b65e9d1c8366c0bc8b (diff)
downloadsink-68fcd3e123e9c0e345d95728d0c8742e53be940a.tar.gz
sink-68fcd3e123e9c0e345d95728d0c8742e53be940a.zip
Use ConfigStore for accounts
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/configstore.cpp96
-rw-r--r--common/configstore.h68
-rw-r--r--common/resourceconfig.cpp70
-rw-r--r--common/resourceconfig.h12
-rw-r--r--common/resourcefacade.cpp18
-rw-r--r--common/resourcefacade.h3
-rw-r--r--tests/accountstest.cpp6
8 files changed, 183 insertions, 91 deletions
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
58 typeindex.cpp 58 typeindex.cpp
59 resourcefacade.cpp 59 resourcefacade.cpp
60 resourceconfig.cpp 60 resourceconfig.cpp
61 configstore.cpp
61 resultset.cpp 62 resultset.cpp
62 domain/applicationdomaintype.cpp 63 domain/applicationdomaintype.cpp
63 domain/event.cpp 64 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 @@
1/*
2 * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#include "configstore.h"
20
21#include <QSettings>
22#include <QSharedPointer>
23#include <QStandardPaths>
24#include <QFile>
25#include <log.h>
26
27static QSharedPointer<QSettings> getConfig(const QByteArray &identifier)
28{
29 return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/" + identifier + ".ini", QSettings::IniFormat);
30}
31
32ConfigStore::ConfigStore(const QByteArray &identifier)
33 : mIdentifier(identifier),
34 mConfig(getConfig(identifier))
35{
36
37}
38
39QMap<QByteArray, QByteArray> ConfigStore::getEntries()
40{
41 QMap<QByteArray, QByteArray> resources;
42 for (const auto &identifier : mConfig->childGroups()) {
43 mConfig->beginGroup(identifier);
44 const auto type = mConfig->value("type").toByteArray();
45 resources.insert(identifier.toLatin1(), type);
46 mConfig->endGroup();
47 }
48 return resources;
49}
50
51void ConfigStore::add(const QByteArray &identifier, const QByteArray &type)
52{
53 Trace() << "Adding " << identifier;
54 mConfig->beginGroup(QString::fromLatin1(identifier));
55 mConfig->setValue("type", type);
56 mConfig->endGroup();
57 mConfig->sync();
58}
59
60void ConfigStore::remove(const QByteArray &identifier)
61{
62 Trace() << "Removing " << identifier;
63 mConfig->beginGroup(QString::fromLatin1(identifier));
64 mConfig->remove("");
65 mConfig->endGroup();
66 mConfig->sync();
67 QFile::remove(getConfig(identifier)->fileName());
68}
69
70void ConfigStore::clear()
71{
72 mConfig->clear();
73 mConfig->sync();
74}
75
76void ConfigStore::modify(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration)
77{
78 Trace() << "Modifying " << identifier;
79 auto config = getConfig(identifier);
80 config->clear();
81 for (const auto &key : configuration.keys()) {
82 config->setValue(key, configuration.value(key));
83 }
84 config->sync();
85}
86
87QMap<QByteArray, QVariant> ConfigStore::get(const QByteArray &identifier)
88{
89 QMap<QByteArray, QVariant> configuration;
90 auto config = getConfig(identifier);
91 for (const auto &key : config->allKeys()) {
92 configuration.insert(key.toLatin1(), config->value(key));
93 }
94 return configuration;
95}
96
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 @@
1/*
2 * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "sink_export.h"
23#include <QList>
24#include <QByteArray>
25#include <QVariant>
26#include <QMap>
27#include <QSettings>
28#include <QSharedPointer>
29
30class SINK_EXPORT ConfigStore
31{
32public:
33 ConfigStore(const QByteArray &identifier);
34
35 /**
36 * Returns all entries with their type.
37 */
38 QMap<QByteArray, QByteArray> getEntries();
39
40 /**
41 * Create an entry with a type.
42 */
43 void add(const QByteArray &identifier, const QByteArray &type);
44
45 /**
46 * Remove an entry.
47 */
48 void remove(const QByteArray &identifier);
49
50 /**
51 * Remove all entries
52 */
53 void clear();
54
55 /**
56 * Modify the configuration of an entry.
57 */
58 void modify(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration);
59
60 /**
61 * Get the configuration of an entry.
62 */
63 QMap<QByteArray, QVariant> get(const QByteArray &identifier);
64
65private:
66 QByteArray mIdentifier;
67 QSharedPointer<QSettings> mConfig;
68};
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<QByteArray, QVariant> ResourceConfig::getConfiguration(const QByteArray &id
97 } 97 }
98 return configuration; 98 return configuration;
99} 99}
100
101
102QByteArray AccountConfig::newIdentifier(const QByteArray &type)
103{
104 auto settings = getConfig("accounts");
105 const auto counter = settings->value("instanceCounter", 0).toInt() + 1;
106 const QByteArray identifier = type + ".instance" + QByteArray::number(counter);
107 settings->setValue("instanceCounter", counter);
108 settings->sync();
109 return identifier;
110}
111
112void AccountConfig::addAccount(const QByteArray &identifier, const QByteArray &type)
113{
114 auto settings = getConfig("accounts");
115 settings->beginGroup(QString::fromLatin1(identifier));
116 settings->setValue("type", type);
117 settings->endGroup();
118 settings->sync();
119}
120
121void AccountConfig::removeAccount(const QByteArray &identifier)
122{
123 auto settings = getConfig("accounts");
124 settings->beginGroup(QString::fromLatin1(identifier));
125 settings->remove("");
126 settings->endGroup();
127 settings->sync();
128 QFile::remove(getConfig(identifier)->fileName());
129}
130
131QMap<QByteArray, QByteArray> AccountConfig::getAccounts()
132{
133 QMap<QByteArray, QByteArray> accounts;
134 auto settings = getConfig("accounts");
135 for (const auto &identifier : settings->childGroups()) {
136 settings->beginGroup(identifier);
137 const auto type = settings->value("type").toByteArray();
138 accounts.insert(identifier.toLatin1(), type);
139 settings->endGroup();
140 }
141 return accounts;
142}
143
144void AccountConfig::clear()
145{
146 auto settings = getConfig("accounts");
147 settings->clear();
148 settings->sync();
149}
150
151void AccountConfig::configureAccount(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration)
152{
153 auto config = getConfig(identifier);
154 config->clear();
155 for (const auto &key : configuration.keys()) {
156 config->setValue(key, configuration.value(key));
157 }
158 config->sync();
159}
160
161QMap<QByteArray, QVariant> AccountConfig::getConfiguration(const QByteArray &identifier)
162{
163 QMap<QByteArray, QVariant> configuration;
164 auto config = getConfig(identifier);
165 for (const auto &key : config->allKeys()) {
166 configuration.insert(key.toLatin1(), config->value(key));
167 }
168 return configuration;
169}
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:
36 static void configureResource(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration); 36 static void configureResource(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration);
37 static QMap<QByteArray, QVariant> getConfiguration(const QByteArray &identifier); 37 static QMap<QByteArray, QVariant> getConfiguration(const QByteArray &identifier);
38}; 38};
39
40class SINK_EXPORT AccountConfig
41{
42public:
43 static QMap<QByteArray, QByteArray> getAccounts();
44 static QByteArray newIdentifier(const QByteArray &type);
45 static void addAccount(const QByteArray &identifier, const QByteArray &type);
46 static void removeAccount(const QByteArray &identifier);
47 static void clear();
48 static void configureAccount(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration);
49 static QMap<QByteArray, QVariant> getConfiguration(const QByteArray &identifier);
50};
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
152 auto account = Sink::ApplicationDomain::SinkAccount::Ptr::create(id); 152 auto account = Sink::ApplicationDomain::SinkAccount::Ptr::create(id);
153 153
154 account->setProperty("type", type); 154 account->setProperty("type", type);
155 const auto configurationValues = AccountConfig::getConfiguration(id); 155 const auto configurationValues = ConfigStore("accounts").get(id);
156 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) { 156 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) {
157 account->setProperty(it.key(), it.value()); 157 account->setProperty(it.key(), it.value());
158 } 158 }
@@ -160,7 +160,7 @@ static Sink::ApplicationDomain::SinkAccount::Ptr readAccountFromConfig(const QBy
160} 160}
161 161
162 162
163AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade<Sink::ApplicationDomain::SinkAccount>() 163AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade<Sink::ApplicationDomain::SinkAccount>(), mConfigStore("accounts")
164{ 164{
165} 165}
166 166
@@ -174,8 +174,8 @@ KAsync::Job<void> AccountFacade::create(const Sink::ApplicationDomain::SinkAccou
174 const QByteArray type = account.getProperty("type").toByteArray(); 174 const QByteArray type = account.getProperty("type").toByteArray();
175 const QByteArray providedIdentifier = account.getProperty("identifier").toByteArray(); 175 const QByteArray providedIdentifier = account.getProperty("identifier").toByteArray();
176 // It is currently a requirement that the account starts with the type 176 // It is currently a requirement that the account starts with the type
177 const QByteArray identifier = providedIdentifier.isEmpty() ? AccountConfig::newIdentifier(type) : providedIdentifier; 177 const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier;
178 AccountConfig::addAccount(identifier, type); 178 mConfigStore.add(identifier, type);
179 auto changedProperties = account.changedProperties(); 179 auto changedProperties = account.changedProperties();
180 changedProperties.removeOne("identifier"); 180 changedProperties.removeOne("identifier");
181 changedProperties.removeOne("type"); 181 changedProperties.removeOne("type");
@@ -185,7 +185,7 @@ KAsync::Job<void> AccountFacade::create(const Sink::ApplicationDomain::SinkAccou
185 for (const auto &property : changedProperties) { 185 for (const auto &property : changedProperties) {
186 configurationValues.insert(property, account.getProperty(property)); 186 configurationValues.insert(property, account.getProperty(property));
187 } 187 }
188 AccountConfig::configureAccount(identifier, configurationValues); 188 mConfigStore.modify(identifier, configurationValues);
189 } 189 }
190 sConfig->add(readAccountFromConfig(identifier, type)); 190 sConfig->add(readAccountFromConfig(identifier, type));
191 }); 191 });
@@ -208,10 +208,10 @@ KAsync::Job<void> AccountFacade::modify(const Sink::ApplicationDomain::SinkAccou
208 for (const auto &property : changedProperties) { 208 for (const auto &property : changedProperties) {
209 configurationValues.insert(property, account.getProperty(property)); 209 configurationValues.insert(property, account.getProperty(property));
210 } 210 }
211 AccountConfig::configureAccount(identifier, configurationValues); 211 mConfigStore.modify(identifier, configurationValues);
212 } 212 }
213 213
214 const auto type = AccountConfig::getAccounts().value(identifier); 214 const auto type = mConfigStore.getEntries().value(identifier);
215 sConfig->modify(readAccountFromConfig(identifier, type)); 215 sConfig->modify(readAccountFromConfig(identifier, type));
216 }); 216 });
217} 217}
@@ -224,7 +224,7 @@ KAsync::Job<void> AccountFacade::remove(const Sink::ApplicationDomain::SinkAccou
224 Warning() << "We need an \"identifier\" property to identify the account to configure"; 224 Warning() << "We need an \"identifier\" property to identify the account to configure";
225 return; 225 return;
226 } 226 }
227 AccountConfig::removeAccount(identifier); 227 mConfigStore.remove(identifier);
228 sConfig->remove(Sink::ApplicationDomain::SinkAccount::Ptr::create(account)); 228 sConfig->remove(Sink::ApplicationDomain::SinkAccount::Ptr::create(account));
229 }); 229 });
230} 230}
@@ -237,7 +237,7 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::S
237 resultProvider->setFetcher([](const QSharedPointer<Sink::ApplicationDomain::SinkAccount> &) {}); 237 resultProvider->setFetcher([](const QSharedPointer<Sink::ApplicationDomain::SinkAccount> &) {});
238 resultProvider->onDone([=]() { delete resultProvider; delete guard; }); 238 resultProvider->onDone([=]() { delete resultProvider; delete guard; });
239 auto job = KAsync::start<void>([=]() { 239 auto job = KAsync::start<void>([=]() {
240 const auto configuredAccounts = AccountConfig::getAccounts(); 240 const auto configuredAccounts = mConfigStore.getEntries();
241 for (const auto &res : configuredAccounts.keys()) { 241 for (const auto &res : configuredAccounts.keys()) {
242 const auto type = configuredAccounts.value(res); 242 const auto type = configuredAccounts.value(res);
243 if (!query.ids.isEmpty() && !query.ids.contains(res)) { 243 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 @@
24#include <Async/Async> 24#include <Async/Async>
25#include "common/resultprovider.h" 25#include "common/resultprovider.h"
26#include "common/domain/applicationdomaintype.h" 26#include "common/domain/applicationdomaintype.h"
27#include "common/configstore.h"
27 28
28namespace Sink { 29namespace Sink {
29class Query; 30class Query;
@@ -50,6 +51,8 @@ public:
50 KAsync::Job<void> modify(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE; 51 KAsync::Job<void> modify(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE;
51 KAsync::Job<void> remove(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE; 52 KAsync::Job<void> remove(const Sink::ApplicationDomain::SinkAccount &resource) Q_DECL_OVERRIDE;
52 QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::SinkAccount::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; 53 QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::SinkAccount::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE;
54private:
55 ConfigStore mConfigStore;
53}; 56};
54 57
55class ConfigNotifier : public QObject 58class 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 @@
5 5
6#include <test.h> 6#include <test.h>
7#include <store.h> 7#include <store.h>
8#include <log.h>
8 9
9class AccountsTest : public QObject 10class AccountsTest : public QObject
10{ 11{
@@ -14,6 +15,7 @@ private slots:
14 void initTestCase() 15 void initTestCase()
15 { 16 {
16 Sink::Test::initTest(); 17 Sink::Test::initTest();
18 Sink::Log::setDebugOutputLevel(Sink::Log::Trace);
17 } 19 }
18 20
19 void testLoad() 21 void testLoad()
@@ -24,6 +26,8 @@ private slots:
24 QString accountName("name"); 26 QString accountName("name");
25 QString accountIcon("icon"); 27 QString accountIcon("icon");
26 auto account = ApplicationDomainType::createEntity<SinkAccount>(); 28 auto account = ApplicationDomainType::createEntity<SinkAccount>();
29 //FIXME Get rid of this line
30 account.setProperty("identifier", account.identifier());
27 account.setProperty("type", "maildir"); 31 account.setProperty("type", "maildir");
28 account.setProperty("name", accountName); 32 account.setProperty("name", accountName);
29 account.setProperty("icon", accountIcon); 33 account.setProperty("icon", accountIcon);
@@ -38,6 +42,8 @@ private slots:
38 QString smtpUsername("smtpUsername"); 42 QString smtpUsername("smtpUsername");
39 QString smtpPassword("smtpPassword"); 43 QString smtpPassword("smtpPassword");
40 auto resource = ApplicationDomainType::createEntity<SinkResource>(); 44 auto resource = ApplicationDomainType::createEntity<SinkResource>();
45 //FIXME Get rid of this line
46 resource.setProperty("identifier", resource.identifier());
41 resource.setProperty("type", "org.kde.mailtransport"); 47 resource.setProperty("type", "org.kde.mailtransport");
42 resource.setProperty("account", account.identifier()); 48 resource.setProperty("account", account.identifier());
43 resource.setProperty("server", smtpServer); 49 resource.setProperty("server", smtpServer);