Changeset View
Changeset View
Standalone View
Standalone View
src/kitemviews/private/kdirectorycontentscounter.cpp
| Show All 18 Lines | |||||
| 19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
| 20 | 20 | | |||
| 21 | #include "kdirectorycontentscounter.h" | 21 | #include "kdirectorycontentscounter.h" | ||
| 22 | #include "kitemviews/kfileitemmodel.h" | 22 | #include "kitemviews/kfileitemmodel.h" | ||
| 23 | 23 | | |||
| 24 | #include <KDirWatch> | 24 | #include <KDirWatch> | ||
| 25 | 25 | | |||
| 26 | #include <QFileInfo> | 26 | #include <QFileInfo> | ||
| 27 | #include <QDir> | ||||
| 27 | #include <QThread> | 28 | #include <QThread> | ||
| 28 | 29 | | |||
| 30 | namespace { | ||||
| 31 | /// cache of directory counting result | ||||
| 32 | static QHash<QString, QPair<int, long>> *s_cache; | ||||
meven: It would be great to be able to share this cache with baloo-widgets, so that those data can be… | |||||
meven: The Status bar info could be updated to get this data. | |||||
| 33 | } | ||||
| 34 | | ||||
| 29 | KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) : | 35 | KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) : | ||
| 30 | QObject(parent), | 36 | QObject(parent), | ||
| 31 | m_model(model), | 37 | m_model(model), | ||
| 32 | m_queue(), | 38 | m_queue(), | ||
| 33 | m_worker(nullptr), | 39 | m_worker(nullptr), | ||
| 34 | m_workerIsBusy(false), | 40 | m_workerIsBusy(false), | ||
| 35 | m_dirWatcher(nullptr), | 41 | m_dirWatcher(nullptr), | ||
| 36 | m_watchedDirs() | 42 | m_watchedDirs() | ||
| 37 | { | 43 | { | ||
| 38 | connect(m_model, &KFileItemModel::itemsRemoved, | 44 | connect(m_model, &KFileItemModel::itemsRemoved, | ||
| 39 | this, &KDirectoryContentsCounter::slotItemsRemoved); | 45 | this, &KDirectoryContentsCounter::slotItemsRemoved); | ||
| 40 | 46 | | |||
| 41 | if (!m_workerThread) { | 47 | if (!m_workerThread) { | ||
| 42 | m_workerThread = new QThread(); | 48 | m_workerThread = new QThread(); | ||
| 43 | m_workerThread->start(); | 49 | m_workerThread->start(); | ||
| 44 | } | 50 | } | ||
| 45 | 51 | | |||
| 52 | if (s_cache == nullptr) { | ||||
| 53 | s_cache = new QHash<QString, QPair<int, long>>(); | ||||
| 54 | } | ||||
| 55 | | ||||
| 46 | m_worker = new KDirectoryContentsCounterWorker(); | 56 | m_worker = new KDirectoryContentsCounterWorker(); | ||
| 47 | m_worker->moveToThread(m_workerThread); | 57 | m_worker->moveToThread(m_workerThread); | ||
| 48 | ++m_workersCount; | | |||
| 49 | 58 | | |||
| 50 | connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount, | 59 | connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount, | ||
| 51 | m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents); | 60 | m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents); | ||
| 52 | connect(m_worker, &KDirectoryContentsCounterWorker::result, | 61 | connect(m_worker, &KDirectoryContentsCounterWorker::result, | ||
| 53 | this, &KDirectoryContentsCounter::slotResult); | 62 | this, &KDirectoryContentsCounter::slotResult); | ||
| 54 | 63 | | |||
| 55 | m_dirWatcher = new KDirWatch(this); | 64 | m_dirWatcher = new KDirWatch(this); | ||
| 56 | connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); | 65 | connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); | ||
| 57 | } | 66 | } | ||
| 58 | 67 | | |||
| 59 | KDirectoryContentsCounter::~KDirectoryContentsCounter() | 68 | KDirectoryContentsCounter::~KDirectoryContentsCounter() | ||
| 60 | { | 69 | { | ||
| 61 | --m_workersCount; | 70 | if (m_workerThread->isRunning()) { | ||
elvisangelaccio: Why this change? What's wrong with `m_workersCount` ? | |||||
I intend to have a single worker KDirectoryContentsCounter, so why bother reimplementing a refCount ? meven: I intend to have a single worker KDirectoryContentsCounter, so why bother reimplementing a… | |||||
I see. This is valuable information, please put it in the commit message :) elvisangelaccio: I see. This is valuable information, please put it in the commit message :) | |||||
| 62 | | ||||
| 63 | if (m_workersCount > 0) { | | |||
| 64 | // The worker thread will continue running. It could even be running | 71 | // The worker thread will continue running. It could even be running | ||
| 65 | // a method of m_worker at the moment, so we delete it using | 72 | // a method of m_worker at the moment, so we delete it using | ||
| 66 | // deleteLater() to prevent a crash. | 73 | // deleteLater() to prevent a crash. | ||
| 67 | m_worker->deleteLater(); | 74 | m_worker->deleteLater(); | ||
| 68 | } else { | 75 | } else { | ||
| 69 | // There are no remaining workers -> stop the worker thread. | 76 | // There are no remaining workers -> stop the worker thread. | ||
| 70 | m_workerThread->quit(); | 77 | m_workerThread->quit(); | ||
| 71 | m_workerThread->wait(); | 78 | m_workerThread->wait(); | ||
| 72 | delete m_workerThread; | 79 | delete m_workerThread; | ||
| 73 | m_workerThread = nullptr; | 80 | m_workerThread = nullptr; | ||
| 74 | 81 | | |||
| 75 | // The worker thread has finished running now, so it's safe to delete | 82 | // The worker thread has finished running now, so it's safe to delete | ||
| 76 | // m_worker. deleteLater() would not work at all because the event loop | 83 | // m_worker. deleteLater() would not work at all because the event loop | ||
| 77 | // which would deliver the event to m_worker is not running any more. | 84 | // which would deliver the event to m_worker is not running any more. | ||
| 78 | delete m_worker; | 85 | delete m_worker; | ||
| 79 | } | 86 | } | ||
| 80 | } | 87 | } | ||
| 81 | 88 | | |||
| 82 | void KDirectoryContentsCounter::addDirectory(const QString& path) | 89 | void KDirectoryContentsCounter::scanDirectory(const QString& path) | ||
| 83 | { | 90 | { | ||
| 84 | startWorker(path); | 91 | startWorker(path); | ||
| 85 | } | 92 | } | ||
| 86 | 93 | | |||
| 87 | int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path) | 94 | void KDirectoryContentsCounter::slotResult(const QString& path, int count, long size) | ||
| 88 | { | 95 | { | ||
| 89 | const QString resolvedPath = QFileInfo(path).canonicalFilePath(); | 96 | m_workerIsBusy = false; | ||
| 97 | | ||||
| 98 | const QFileInfo info = QFileInfo(path); | ||||
| 99 | const QString resolvedPath = info.canonicalFilePath(); | ||||
| 90 | 100 | | |||
| 91 | if (!m_dirWatcher->contains(resolvedPath)) { | 101 | if (!m_dirWatcher->contains(resolvedPath)) { | ||
| 92 | m_dirWatcher->addDir(resolvedPath); | 102 | m_dirWatcher->addDir(resolvedPath); | ||
| 93 | m_watchedDirs.insert(resolvedPath); | 103 | m_watchedDirs.insert(resolvedPath); | ||
| 94 | } | 104 | } | ||
| 95 | 105 | | |||
| 96 | KDirectoryContentsCounterWorker::Options options; | 106 | if (!m_queue.isEmpty()) { | ||
| 97 | 107 | startWorker(m_queue.dequeue()); | |||
| 98 | if (m_model->showHiddenFiles()) { | | |||
| 99 | options |= KDirectoryContentsCounterWorker::CountHiddenFiles; | | |||
| 100 | } | | |||
| 101 | | ||||
| 102 | if (m_model->showDirectoriesOnly()) { | | |||
| 103 | options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly; | | |||
| 104 | } | 108 | } | ||
| 105 | 109 | | |||
| 106 | return KDirectoryContentsCounterWorker::subItemsCount(path, options); | 110 | if (s_cache->contains(resolvedPath)) { | ||
| 111 | const auto pair = s_cache->value(resolvedPath); | ||||
| 112 | if (pair.first == count && pair.second == size) { | ||||
| 113 | // no change no need to send another result event | ||||
| 114 | return; | ||||
| 107 | } | 115 | } | ||
| 108 | | ||||
| 109 | void KDirectoryContentsCounter::slotResult(const QString& path, int count) | | |||
| 110 | { | | |||
| 111 | m_workerIsBusy = false; | | |||
| 112 | | ||||
| 113 | const QString resolvedPath = QFileInfo(path).canonicalFilePath(); | | |||
| 114 | | ||||
| 115 | if (!m_dirWatcher->contains(resolvedPath)) { | | |||
| 116 | m_dirWatcher->addDir(resolvedPath); | | |||
| 117 | m_watchedDirs.insert(resolvedPath); | | |||
| 118 | } | 116 | } | ||
| 119 | 117 | | |||
| 120 | if (!m_queue.isEmpty()) { | 118 | if (info.dir().path() == m_model->rootItem().url().path()) { | ||
| 121 | startWorker(m_queue.dequeue()); | 119 | // update cache or overwrite value | ||
| 120 | // when path is a direct children of the current model root | ||||
| 121 | s_cache->insert(resolvedPath, QPair<int, long>(count, size)); | ||||
| 122 | } | 122 | } | ||
| 123 | 123 | | |||
| 124 | emit result(path, count); | 124 | // sends the results | ||
| 125 | emit result(resolvedPath, count, size); | ||||
| 125 | } | 126 | } | ||
| 126 | 127 | | |||
| 127 | void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) | 128 | void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) | ||
| 128 | { | 129 | { | ||
| 129 | const int index = m_model->index(QUrl::fromLocalFile(path)); | 130 | const int index = m_model->index(QUrl::fromLocalFile(path)); | ||
| 130 | if (index >= 0) { | 131 | if (index >= 0) { | ||
| 131 | if (!m_model->fileItem(index).isDir()) { | 132 | if (!m_model->fileItem(index).isDir()) { | ||
| 132 | // If INotify is used, KDirWatch issues the dirty() signal | 133 | // If INotify is used, KDirWatch issues the dirty() signal | ||
| 133 | // also for changed files inside the directory, even if we | 134 | // also for changed files inside the directory, even if we | ||
| 134 | // don't enable this behavior explicitly (see bug 309740). | 135 | // don't enable this behavior explicitly (see bug 309740). | ||
| 135 | return; | 136 | return; | ||
| 136 | } | 137 | } | ||
| 137 | 138 | | |||
| 138 | startWorker(path); | 139 | startWorker(path); | ||
| 139 | } | 140 | } | ||
| 140 | } | 141 | } | ||
| 141 | 142 | | |||
| 142 | void KDirectoryContentsCounter::slotItemsRemoved() | 143 | void KDirectoryContentsCounter::slotItemsRemoved() | ||
| 143 | { | 144 | { | ||
| 144 | const bool allItemsRemoved = (m_model->count() == 0); | 145 | const bool allItemsRemoved = (m_model->count() == 0); | ||
| 145 | 146 | | |||
| 146 | if (!m_watchedDirs.isEmpty()) { | 147 | if (!m_watchedDirs.isEmpty()) { | ||
| 147 | // Don't let KDirWatch watch for removed items | 148 | // Don't let KDirWatch watch for removed items | ||
| 148 | if (allItemsRemoved) { | 149 | if (allItemsRemoved) { | ||
| 149 | foreach (const QString& path, m_watchedDirs) { | 150 | for (const QString& path : qAsConst(m_watchedDirs)) { | ||
| 150 | m_dirWatcher->removeDir(path); | 151 | m_dirWatcher->removeDir(path); | ||
| 151 | } | 152 | } | ||
| 152 | m_watchedDirs.clear(); | 153 | m_watchedDirs.clear(); | ||
| 153 | m_queue.clear(); | 154 | m_queue.clear(); | ||
| 154 | } else { | 155 | } else { | ||
| 155 | QMutableSetIterator<QString> it(m_watchedDirs); | 156 | QMutableSetIterator<QString> it(m_watchedDirs); | ||
| 156 | while (it.hasNext()) { | 157 | while (it.hasNext()) { | ||
| 157 | const QString& path = it.next(); | 158 | const QString& path = it.next(); | ||
| 158 | if (m_model->index(QUrl::fromLocalFile(path)) < 0) { | 159 | if (m_model->index(QUrl::fromLocalFile(path)) < 0) { | ||
| 159 | m_dirWatcher->removeDir(path); | 160 | m_dirWatcher->removeDir(path); | ||
| 160 | it.remove(); | 161 | it.remove(); | ||
| 161 | } | 162 | } | ||
| 162 | } | 163 | } | ||
| 163 | } | 164 | } | ||
| 164 | } | 165 | } | ||
| 165 | } | 166 | } | ||
| 166 | 167 | | |||
| 167 | void KDirectoryContentsCounter::startWorker(const QString& path) | 168 | void KDirectoryContentsCounter::startWorker(const QString& path) | ||
| 168 | { | 169 | { | ||
| 170 | if (s_cache->contains(path)) { | ||||
| 171 | // fast path when in cache | ||||
| 172 | // will be updated later if result has changed | ||||
| 173 | const auto pair = s_cache->value(path); | ||||
| 174 | emit result(path, pair.first, pair.second); | ||||
| 175 | } | ||||
| 176 | | ||||
| 169 | if (m_workerIsBusy) { | 177 | if (m_workerIsBusy) { | ||
| 170 | m_queue.enqueue(path); | 178 | m_queue.enqueue(path); | ||
| 171 | } else { | 179 | } else { | ||
| 172 | KDirectoryContentsCounterWorker::Options options; | 180 | KDirectoryContentsCounterWorker::Options options; | ||
| 173 | 181 | | |||
| 174 | if (m_model->showHiddenFiles()) { | 182 | if (m_model->showHiddenFiles()) { | ||
| 175 | options |= KDirectoryContentsCounterWorker::CountHiddenFiles; | 183 | options |= KDirectoryContentsCounterWorker::CountHiddenFiles; | ||
| 176 | } | 184 | } | ||
| 177 | 185 | | |||
| 178 | if (m_model->showDirectoriesOnly()) { | 186 | if (m_model->showDirectoriesOnly()) { | ||
| 179 | options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly; | 187 | options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly; | ||
| 180 | } | 188 | } | ||
| 181 | 189 | | |||
| 182 | emit requestDirectoryContentsCount(path, options); | 190 | emit requestDirectoryContentsCount(path, options); | ||
| 183 | m_workerIsBusy = true; | 191 | m_workerIsBusy = true; | ||
| 184 | } | 192 | } | ||
| 185 | } | 193 | } | ||
| 186 | 194 | | |||
| 187 | QThread* KDirectoryContentsCounter::m_workerThread = nullptr; | 195 | QThread* KDirectoryContentsCounter::m_workerThread = nullptr; | ||
| 188 | int KDirectoryContentsCounter::m_workersCount = 0; | | |||
It would be great to be able to share this cache with baloo-widgets, so that those data can be used in the metadata widget