File indexing completed on 2024-05-19 05:00:40

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "pathcache.h"
0009 #include "gdrivedebug.h"
0010 
0011 #include <QDateTime>
0012 
0013 PathCache::PathCache()
0014 {
0015 }
0016 
0017 PathCache::~PathCache()
0018 {
0019 }
0020 
0021 void PathCache::insertPath(const QString &path, const QString &fileId)
0022 {
0023     if (path.startsWith(QLatin1Char('/'))) {
0024         m_pathIdMap.insert(path.mid(1), fileId);
0025     } else {
0026         m_pathIdMap.insert(path, fileId);
0027     }
0028 }
0029 
0030 QString PathCache::idForPath(const QString &path) const
0031 {
0032     if (path.startsWith(QLatin1Char('/'))) {
0033         return m_pathIdMap[path.mid(1)];
0034     } else {
0035         return m_pathIdMap[path];
0036     }
0037 }
0038 
0039 QStringList PathCache::descendants(const QString &path) const
0040 {
0041     const QString fullPath = path.endsWith(QLatin1Char('/')) ? path : path + QLatin1Char('/');
0042 
0043     QStringList descendants;
0044     for (auto iter = m_pathIdMap.begin(); iter != m_pathIdMap.end(); ++iter) {
0045         if (!iter.key().startsWith(fullPath)) {
0046             // Not a descendant at all
0047             continue;
0048         }
0049 
0050         if (iter.key().lastIndexOf(QLatin1Char('/')) >= fullPath.size()) {
0051             // Not a direct descendant
0052             continue;
0053         }
0054 
0055         descendants.append(iter.key());
0056     }
0057 
0058     return descendants;
0059 }
0060 
0061 void PathCache::removePath(const QString &path)
0062 {
0063     m_pathIdMap.remove(path);
0064 }
0065 
0066 void PathCache::dump()
0067 {
0068     qCDebug(GDRIVE) << "==== DUMP ====";
0069     for (auto iter = m_pathIdMap.constBegin(); iter != m_pathIdMap.constEnd(); ++iter) {
0070         qCDebug(GDRIVE) << iter.key() << " => " << iter.value();
0071     }
0072     qCDebug(GDRIVE) << "==== DUMP ====";
0073 }