1 /*
2    SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 #include "folderarchivecache.h"
7 #include "folderarchiveaccountinfo.h"
8 #include "kmail_debug.h"
9 
FolderArchiveCache(QObject * parent)10 FolderArchiveCache::FolderArchiveCache(QObject *parent)
11     : QObject(parent)
12 {
13 }
14 
15 FolderArchiveCache::~FolderArchiveCache() = default;
16 
clearCache()17 void FolderArchiveCache::clearCache()
18 {
19     mCache.clear();
20 }
21 
clearCacheWithContainsCollection(Akonadi::Collection::Id id)22 void FolderArchiveCache::clearCacheWithContainsCollection(Akonadi::Collection::Id id)
23 {
24     QHash<QString, ArchiveCache>::iterator i = mCache.begin();
25     while (i != mCache.end()) {
26         if (i.value().colId == id) {
27             i = mCache.erase(i);
28         } else {
29             ++i;
30         }
31     }
32 }
33 
collectionId(FolderArchiveAccountInfo * info)34 Akonadi::Collection::Id FolderArchiveCache::collectionId(FolderArchiveAccountInfo *info)
35 {
36     // qCDebug(KMAIL_LOG)<<" Look at Cache ";
37     if (mCache.contains(info->instanceName())) {
38         // qCDebug(KMAIL_LOG)<<"instance name : "<<info->instanceName();
39         switch (info->folderArchiveType()) {
40         case FolderArchiveAccountInfo::UniqueFolder:
41             qCDebug(KMAIL_LOG) << "FolderArchiveAccountInfo::UniqueFolder has cache " << mCache.value(info->instanceName()).colId;
42             return mCache.value(info->instanceName()).colId;
43         case FolderArchiveAccountInfo::FolderByMonths:
44             // qCDebug(KMAIL_LOG)<<"FolderArchiveAccountInfo::ByMonths has cache ?";
45             if (mCache.value(info->instanceName()).date.month() != QDate::currentDate().month()) {
46                 // qCDebug(KMAIL_LOG)<<"need to remove current cache month is not good";
47                 mCache.remove(info->instanceName());
48                 return -1;
49             } else {
50                 return mCache.value(info->instanceName()).colId;
51             }
52         case FolderArchiveAccountInfo::FolderByYears:
53             // qCDebug(KMAIL_LOG)<<"FolderArchiveAccountInfo::ByYears has cache ?";
54             if (mCache.value(info->instanceName()).date.year() != QDate::currentDate().year()) {
55                 // qCDebug(KMAIL_LOG)<<"need to remove current cache year is not good";
56                 mCache.remove(info->instanceName());
57                 return -1;
58             } else {
59                 return mCache.value(info->instanceName()).colId;
60             }
61         }
62         return mCache.value(info->instanceName()).colId;
63     }
64     // qCDebug(KMAIL_LOG)<<" Don't have cache for this instancename "<<info->instanceName();
65     return -1;
66 }
67 
addToCache(const QString & resourceName,Akonadi::Collection::Id id)68 void FolderArchiveCache::addToCache(const QString &resourceName, Akonadi::Collection::Id id)
69 {
70     if (mCache.contains(resourceName)) {
71         ArchiveCache cache = mCache.value(resourceName);
72         cache.colId = id;
73         mCache.insert(resourceName, cache);
74     } else {
75         ArchiveCache cache;
76         cache.colId = id;
77         mCache.insert(resourceName, cache);
78     }
79 }
80