File indexing completed on 2024-05-26 05:14:36

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvraitl@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "intervalcheck.h"
0009 #include "storage/datastore.h"
0010 #include "storage/entity.h"
0011 #include "storage/itemretrievalmanager.h"
0012 
0013 using namespace Akonadi::Server;
0014 
0015 static const int MINIMUM_AUTOSYNC_INTERVAL = 5; // minutes
0016 static const int MINIMUM_COLTREESYNC_INTERVAL = 5; // minutes
0017 
0018 IntervalCheck::IntervalCheck(ItemRetrievalManager &itemRetrievalManager)
0019     : CollectionScheduler(QStringLiteral("IntervalCheck"), QThread::IdlePriority)
0020     , mItemRetrievalManager(itemRetrievalManager)
0021 {
0022 }
0023 
0024 IntervalCheck::~IntervalCheck()
0025 {
0026     quitThread();
0027 }
0028 
0029 void IntervalCheck::requestCollectionSync(const Collection &collection)
0030 {
0031     QMetaObject::invokeMethod(
0032         this,
0033         [this, collection]() {
0034             collectionExpired(collection);
0035         },
0036         Qt::QueuedConnection);
0037 }
0038 
0039 int IntervalCheck::collectionScheduleInterval(const Collection &collection)
0040 {
0041     return collection.cachePolicyCheckInterval();
0042 }
0043 
0044 bool IntervalCheck::hasChanged(const Collection &collection, const Collection &changed)
0045 {
0046     return collection.cachePolicyCheckInterval() != changed.cachePolicyCheckInterval() || collection.enabled() != changed.enabled()
0047         || collection.syncPref() != changed.syncPref();
0048 }
0049 
0050 bool IntervalCheck::shouldScheduleCollection(const Collection &collection)
0051 {
0052     return collection.cachePolicyCheckInterval() > 0
0053         && ((collection.syncPref() == Collection::True) || ((collection.syncPref() == Collection::Undefined) && collection.enabled()));
0054 }
0055 
0056 void IntervalCheck::collectionExpired(const Collection &collection)
0057 {
0058     const QDateTime now(QDateTime::currentDateTime());
0059 
0060     if (collection.parentId() == 0) {
0061         const QString resourceName = collection.resource().name();
0062 
0063         const int interval = qMax(MINIMUM_COLTREESYNC_INTERVAL, collection.cachePolicyCheckInterval());
0064 
0065         const QDateTime lastExpectedCheck = now.addSecs(interval * -60);
0066         if (!mLastCollectionTreeSyncs.contains(resourceName) || mLastCollectionTreeSyncs.value(resourceName) < lastExpectedCheck) {
0067             mLastCollectionTreeSyncs.insert(resourceName, now);
0068             mItemRetrievalManager.triggerCollectionTreeSync(resourceName);
0069         }
0070     }
0071 
0072     // now on to the actual collection syncing
0073     const int interval = qMax(MINIMUM_AUTOSYNC_INTERVAL, collection.cachePolicyCheckInterval());
0074 
0075     const QDateTime lastExpectedCheck = now.addSecs(interval * -60);
0076     if (mLastChecks.contains(collection.id()) && mLastChecks.value(collection.id()) > lastExpectedCheck) {
0077         return;
0078     }
0079     mLastChecks.insert(collection.id(), now);
0080     mItemRetrievalManager.triggerCollectionSync(collection.resource().name(), collection.id());
0081 }
0082 
0083 #include "moc_intervalcheck.cpp"