File indexing completed on 2025-01-26 04:53:20

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "folderarchivecache.h"
0007 #include "folderarchiveaccountinfo.h"
0008 #include "kmail_debug.h"
0009 
0010 FolderArchiveCache::FolderArchiveCache(QObject *parent)
0011     : QObject(parent)
0012 {
0013 }
0014 
0015 FolderArchiveCache::~FolderArchiveCache() = default;
0016 
0017 void FolderArchiveCache::clearCache()
0018 {
0019     mCache.clear();
0020 }
0021 
0022 void FolderArchiveCache::clearCacheWithContainsCollection(Akonadi::Collection::Id id)
0023 {
0024     QHash<QString, ArchiveCache>::iterator i = mCache.begin();
0025     while (i != mCache.end()) {
0026         if (i.value().colId == id) {
0027             i = mCache.erase(i);
0028         } else {
0029             ++i;
0030         }
0031     }
0032 }
0033 
0034 Akonadi::Collection::Id FolderArchiveCache::collectionId(FolderArchiveAccountInfo *info)
0035 {
0036     // qCDebug(KMAIL_LOG)<<" Look at Cache ";
0037     if (mCache.contains(info->instanceName())) {
0038         // qCDebug(KMAIL_LOG)<<"instance name : "<<info->instanceName();
0039         switch (info->folderArchiveType()) {
0040         case FolderArchiveAccountInfo::UniqueFolder:
0041             qCDebug(KMAIL_LOG) << "FolderArchiveAccountInfo::UniqueFolder has cache " << mCache.value(info->instanceName()).colId;
0042             return mCache.value(info->instanceName()).colId;
0043         case FolderArchiveAccountInfo::FolderByMonths:
0044             // qCDebug(KMAIL_LOG)<<"FolderArchiveAccountInfo::ByMonths has cache ?";
0045             if (mCache.value(info->instanceName()).date.month() != QDate::currentDate().month()) {
0046                 // qCDebug(KMAIL_LOG)<<"need to remove current cache month is not good";
0047                 mCache.remove(info->instanceName());
0048                 return -1;
0049             } else {
0050                 return mCache.value(info->instanceName()).colId;
0051             }
0052         case FolderArchiveAccountInfo::FolderByYears:
0053             // qCDebug(KMAIL_LOG)<<"FolderArchiveAccountInfo::ByYears has cache ?";
0054             if (mCache.value(info->instanceName()).date.year() != QDate::currentDate().year()) {
0055                 // qCDebug(KMAIL_LOG)<<"need to remove current cache year is not good";
0056                 mCache.remove(info->instanceName());
0057                 return -1;
0058             } else {
0059                 return mCache.value(info->instanceName()).colId;
0060             }
0061         }
0062         return mCache.value(info->instanceName()).colId;
0063     }
0064     // qCDebug(KMAIL_LOG)<<" Don't have cache for this instancename "<<info->instanceName();
0065     return -1;
0066 }
0067 
0068 void FolderArchiveCache::addToCache(const QString &resourceName, Akonadi::Collection::Id id)
0069 {
0070     if (mCache.contains(resourceName)) {
0071         ArchiveCache cache = mCache.value(resourceName);
0072         cache.colId = id;
0073         mCache.insert(resourceName, cache);
0074     } else {
0075         ArchiveCache cache;
0076         cache.colId = id;
0077         mCache.insert(resourceName, cache);
0078     }
0079 }
0080 
0081 #include "moc_folderarchivecache.cpp"