diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-03-15 14:44:06 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-03-15 15:19:41 +0100 |
commit | d838e58a3fd94a04d9db82560f7004433300b77e (patch) | |
tree | e464e72ba3e207c20bf9c5628f2513bf6170a552 /common/resourcefacade.cpp | |
parent | 3978a0128925081dac0d16c1956328b694796ce6 (diff) | |
download | sink-d838e58a3fd94a04d9db82560f7004433300b77e.tar.gz sink-d838e58a3fd94a04d9db82560f7004433300b77e.zip |
Added support for accounts
Diffstat (limited to 'common/resourcefacade.cpp')
-rw-r--r-- | common/resourcefacade.cpp | 99 |
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 | |||
131 | AccountFacade::AccountFacade(const QByteArray &) : Sink::StoreFacade<Sink::ApplicationDomain::SinkAccount>() | ||
132 | { | ||
133 | } | ||
134 | |||
135 | AccountFacade::~AccountFacade() | ||
136 | { | ||
137 | } | ||
138 | |||
139 | KAsync::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 | |||
161 | KAsync::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 | |||
183 | KAsync::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 | |||
195 | QPair<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 | } | ||