summaryrefslogtreecommitdiffstats
path: root/common/resourcefacade.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/resourcefacade.cpp')
-rw-r--r--common/resourcefacade.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp
index 3095c2e..d91c974 100644
--- a/common/resourcefacade.cpp
+++ b/common/resourcefacade.cpp
@@ -126,3 +126,102 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::S
126 }); 126 });
127 return qMakePair(job, emitter); 127 return qMakePair(job, emitter);
128} 128}
129
130
131AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade<Sink::ApplicationDomain::SinkAccount>()
132{
133}
134
135AccountFacade::~AccountFacade()
136{
137}
138
139KAsync::Job<void> AccountFacade::create(const Sink::ApplicationDomain::SinkAccount &account)
140{
141 return KAsync::start<void>([account, this]() {
142 const QByteArray type = account.getProperty("type").toByteArray();
143 const QByteArray providedIdentifier = account.getProperty("identifier").toByteArray();
144 // It is currently a requirement that the account starts with the type
145 const QByteArray identifier = providedIdentifier.isEmpty() ? AccountConfig::newIdentifier(type) : providedIdentifier;
146 AccountConfig::addAccount(identifier, type);
147 auto changedProperties = account.changedProperties();
148 changedProperties.removeOne("identifier");
149 changedProperties.removeOne("type");
150 if (!changedProperties.isEmpty()) {
151 // We have some configuration values
152 QMap<QByteArray, QVariant> configurationValues;
153 for (const auto &property : changedProperties) {
154 configurationValues.insert(property, account.getProperty(property));
155 }
156 AccountConfig::configureAccount(identifier, configurationValues);
157 }
158 });
159}
160
161KAsync::Job<void> AccountFacade::modify(const Sink::ApplicationDomain::SinkAccount &account)
162{
163 return KAsync::start<void>([account, this]() {
164 const QByteArray identifier = account.identifier();
165 if (identifier.isEmpty()) {
166 Warning() << "We need an \"identifier\" property to identify the account to configure.";
167 return;
168 }
169 auto changedProperties = account.changedProperties();
170 changedProperties.removeOne("identifier");
171 changedProperties.removeOne("type");
172 if (!changedProperties.isEmpty()) {
173 // We have some configuration values
174 QMap<QByteArray, QVariant> configurationValues;
175 for (const auto &property : changedProperties) {
176 configurationValues.insert(property, account.getProperty(property));
177 }
178 AccountConfig::configureAccount(identifier, configurationValues);
179 }
180 });
181}
182
183KAsync::Job<void> AccountFacade::remove(const Sink::ApplicationDomain::SinkAccount &account)
184{
185 return KAsync::start<void>([account, this]() {
186 const QByteArray identifier = account.identifier();
187 if (identifier.isEmpty()) {
188 Warning() << "We need an \"identifier\" property to identify the account to configure";
189 return;
190 }
191 AccountConfig::removeAccount(identifier);
192 });
193}
194
195QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::SinkAccount::Ptr>::Ptr> AccountFacade::load(const Sink::Query &query)
196{
197 auto resultProvider = new Sink::ResultProvider<typename Sink::ApplicationDomain::SinkAccount::Ptr>();
198 auto emitter = resultProvider->emitter();
199 resultProvider->setFetcher([](const QSharedPointer<Sink::ApplicationDomain::SinkAccount> &) {});
200 resultProvider->onDone([resultProvider]() { delete resultProvider; });
201 auto job = KAsync::start<void>([query, resultProvider]() {
202 const auto configuredAccounts = AccountConfig::getAccounts();
203 for (const auto &res : configuredAccounts.keys()) {
204 const auto type = configuredAccounts.value(res);
205 if (query.propertyFilter.contains("type") && query.propertyFilter.value("type").toByteArray() != type) {
206 continue;
207 }
208 if (!query.ids.isEmpty() && !query.ids.contains(res)) {
209 continue;
210 }
211
212 auto account = Sink::ApplicationDomain::SinkAccount::Ptr::create("", res, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
213 account->setProperty("type", type);
214
215 const auto configurationValues = AccountConfig::getConfiguration(res);
216 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) {
217 account->setProperty(it.key(), it.value());
218 }
219
220 resultProvider->add(account);
221 }
222 // TODO initialResultSetComplete should be implicit
223 resultProvider->initialResultSetComplete(Sink::ApplicationDomain::SinkAccount::Ptr());
224 resultProvider->complete();
225 });
226 return qMakePair(job, emitter);
227}