File indexing completed on 2024-12-22 04:57:32

0001 /*
0002     SPDX-FileCopyrightText: 2012 Andras Mantia <amantia@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "keycache.h"
0008 #include <QDirIterator>
0009 
0010 KeyCache *KeyCache::mSelf = nullptr;
0011 
0012 void KeyCache::addKeys(const QString &dir)
0013 {
0014     if (!mNewKeys.contains(dir)) {
0015         mNewKeys.insert(dir, listNew(dir));
0016         // qCDebug(LIBMAILDIR_LOG) << "Added new keys for: " << dir;
0017     }
0018 
0019     if (!mCurKeys.contains(dir)) {
0020         mCurKeys.insert(dir, listCurrent(dir));
0021         // qCDebug(LIBMAILDIR_LOG) << "Added cur keys for: " << dir;
0022     }
0023 }
0024 
0025 void KeyCache::refreshKeys(const QString &dir)
0026 {
0027     mNewKeys.remove(dir);
0028     mCurKeys.remove(dir);
0029     addKeys(dir);
0030 }
0031 
0032 void KeyCache::addNewKey(const QString &dir, const QString &key)
0033 {
0034     mNewKeys[dir].insert(key);
0035     // qCDebug(LIBMAILDIR_LOG) << "Added new key for : " << dir << " key: " << key;
0036 }
0037 
0038 void KeyCache::addCurKey(const QString &dir, const QString &key)
0039 {
0040     mCurKeys[dir].insert(key);
0041     // qCDebug(LIBMAILDIR_LOG) << "Added cur key for : " << dir << " key:" << key;
0042 }
0043 
0044 void KeyCache::removeKey(const QString &dir, const QString &key)
0045 {
0046     // qCDebug(LIBMAILDIR_LOG) << "Removed new and cur key for: " << dir << " key:" << key;
0047     mNewKeys[dir].remove(key);
0048     mCurKeys[dir].remove(key);
0049 }
0050 
0051 bool KeyCache::isCurKey(const QString &dir, const QString &key) const
0052 {
0053     return mCurKeys.value(dir).contains(key);
0054 }
0055 
0056 bool KeyCache::isNewKey(const QString &dir, const QString &key) const
0057 {
0058     return mNewKeys.value(dir).contains(key);
0059 }
0060 
0061 QSet<QString> KeyCache::listNew(const QString &dir) const
0062 {
0063     QSet<QString> keys;
0064     QDirIterator d(dir + QLatin1StringView("/new"), QDir::Files);
0065     while (d.hasNext()) {
0066         d.next();
0067         keys.insert(d.fileName());
0068     }
0069     return keys;
0070 }
0071 
0072 QSet<QString> KeyCache::listCurrent(const QString &dir) const
0073 {
0074     QSet<QString> keys;
0075     QDirIterator d(dir + QLatin1StringView("/cur"), QDir::Files);
0076     while (d.hasNext()) {
0077         d.next();
0078         keys.insert(d.fileName());
0079     }
0080     return keys;
0081 }