Changeset View
Changeset View
Standalone View
Standalone View
src/akonadi/akonadicachingstorage.cpp
Show All 22 Lines | |||||
23 | */ | 23 | */ | ||
24 | 24 | | |||
25 | 25 | | |||
26 | #include "akonadicachingstorage.h" | 26 | #include "akonadicachingstorage.h" | ||
27 | #include "akonadistorage.h" | 27 | #include "akonadistorage.h" | ||
28 | 28 | | |||
29 | #include "akonadicollectionfetchjobinterface.h" | 29 | #include "akonadicollectionfetchjobinterface.h" | ||
30 | #include "akonadiitemfetchjobinterface.h" | 30 | #include "akonadiitemfetchjobinterface.h" | ||
31 | #include "akonaditagfetchjobinterface.h" | ||||
31 | 32 | | |||
32 | #include <QTimer> | 33 | #include <QTimer> | ||
33 | 34 | | |||
34 | using namespace Akonadi; | 35 | using namespace Akonadi; | ||
35 | 36 | | |||
36 | class CachingCollectionFetchJob : public KCompositeJob, public CollectionFetchJobInterface | 37 | class CachingCollectionFetchJob : public KCompositeJob, public CollectionFetchJobInterface | ||
37 | { | 38 | { | ||
38 | Q_OBJECT | 39 | Q_OBJECT | ||
▲ Show 20 Lines • Show All 258 Lines • ▼ Show 20 Line(s) | 278 | private: | |||
297 | bool m_started; | 298 | bool m_started; | ||
298 | StorageInterface::Ptr m_storage; | 299 | StorageInterface::Ptr m_storage; | ||
299 | Cache::Ptr m_cache; | 300 | Cache::Ptr m_cache; | ||
300 | Item m_item; | 301 | Item m_item; | ||
301 | Collection m_collection; | 302 | Collection m_collection; | ||
302 | Item::List m_items; | 303 | Item::List m_items; | ||
303 | }; | 304 | }; | ||
304 | 305 | | |||
306 | class CachingTagFetchJob : public KCompositeJob, public TagFetchJobInterface | ||||
307 | { | ||||
308 | Q_OBJECT | ||||
309 | public: | ||||
310 | CachingTagFetchJob(const StorageInterface::Ptr &storage, | ||||
311 | const Cache::Ptr &cache, | ||||
312 | QObject *parent = nullptr) | ||||
313 | : KCompositeJob(parent), | ||||
314 | m_started(false), | ||||
315 | m_storage(storage), | ||||
316 | m_cache(cache) | ||||
317 | { | ||||
318 | QTimer::singleShot(0, this, &CachingTagFetchJob::start); | ||||
319 | } | ||||
320 | | ||||
321 | void start() override | ||||
322 | { | ||||
323 | if (m_started) | ||||
324 | return; | ||||
325 | | ||||
326 | if (m_cache->isTagListPopulated()) { | ||||
327 | QTimer::singleShot(0, this, &CachingTagFetchJob::retrieveFromCache); | ||||
328 | } else { | ||||
329 | auto job = m_storage->fetchTags(); | ||||
330 | addSubjob(job->kjob()); | ||||
331 | } | ||||
332 | | ||||
333 | m_started = true; | ||||
334 | } | ||||
335 | | ||||
336 | Tag::List tags() const override | ||||
337 | { | ||||
338 | return m_tags; | ||||
339 | } | ||||
340 | | ||||
341 | private: | ||||
342 | void slotResult(KJob *kjob) override | ||||
343 | { | ||||
344 | if (kjob->error()) { | ||||
345 | KCompositeJob::slotResult(kjob); | ||||
346 | return; | ||||
347 | } | ||||
348 | | ||||
349 | auto job = dynamic_cast<TagFetchJobInterface*>(kjob); | ||||
350 | Q_ASSERT(job); | ||||
351 | m_tags = job->tags(); | ||||
352 | m_cache->setTags(m_tags); | ||||
353 | emitResult(); | ||||
354 | } | ||||
355 | | ||||
356 | void retrieveFromCache() | ||||
357 | { | ||||
358 | m_tags = m_cache->tags(); | ||||
359 | emitResult(); | ||||
360 | } | ||||
361 | | ||||
362 | bool m_started; | ||||
363 | StorageInterface::Ptr m_storage; | ||||
364 | Cache::Ptr m_cache; | ||||
365 | Tag::List m_tags; | ||||
366 | }; | ||||
305 | 367 | | |||
306 | CachingStorage::CachingStorage(const Cache::Ptr &cache, const StorageInterface::Ptr &storage) | 368 | CachingStorage::CachingStorage(const Cache::Ptr &cache, const StorageInterface::Ptr &storage) | ||
307 | : m_cache(cache), | 369 | : m_cache(cache), | ||
308 | m_storage(storage) | 370 | m_storage(storage) | ||
309 | { | 371 | { | ||
310 | } | 372 | } | ||
311 | 373 | | |||
312 | CachingStorage::~CachingStorage() | 374 | CachingStorage::~CachingStorage() | ||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | |||||
405 | 467 | | |||
406 | ItemFetchJobInterface *CachingStorage::fetchTagItems(Tag tag) | 468 | ItemFetchJobInterface *CachingStorage::fetchTagItems(Tag tag) | ||
407 | { | 469 | { | ||
408 | return m_storage->fetchTagItems(tag); | 470 | return m_storage->fetchTagItems(tag); | ||
409 | } | 471 | } | ||
410 | 472 | | |||
411 | TagFetchJobInterface *CachingStorage::fetchTags() | 473 | TagFetchJobInterface *CachingStorage::fetchTags() | ||
412 | { | 474 | { | ||
413 | return m_storage->fetchTags(); | 475 | return new CachingTagFetchJob(m_storage, m_cache); | ||
414 | } | 476 | } | ||
415 | 477 | | |||
416 | #include "akonadicachingstorage.moc" | 478 | #include "akonadicachingstorage.moc" |