]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/private/kdirectorycontentscounter.cpp
Merge remote-tracking branch 'origin/release/21.08'
[dolphin.git] / src / kitemviews / private / kdirectorycontentscounter.cpp
index bd204fe8e24a353a16a0dccdc6ff821b668c8474..a2a27ad28707717931119b7387f5f86ff74c3668 100644 (file)
@@ -1,22 +1,9 @@
-/***************************************************************************
- *   Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com>             *
- *   Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com>    *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
+ * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "kdirectorycontentscounter.h"
 #include "kitemviews/kfileitemmodel.h"
 #include <KDirWatch>
 
 #include <QFileInfo>
+#include <QDir>
 #include <QThread>
 
+namespace  {
+    /// cache of directory counting result
+    static QHash<QString, QPair<int, long>> *s_cache;
+}
+
 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) :
     QObject(parent),
     m_model(model),
@@ -43,9 +36,12 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj
         m_workerThread->start();
     }
 
+    if (s_cache == nullptr) {
+        s_cache = new QHash<QString, QPair<int, long>>();
+    }
+
     m_worker = new KDirectoryContentsCounterWorker();
     m_worker->moveToThread(m_workerThread);
-    ++m_workersCount;
 
     connect(this,     &KDirectoryContentsCounter::requestDirectoryContentsCount,
             m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents);
@@ -58,9 +54,7 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj
 
 KDirectoryContentsCounter::~KDirectoryContentsCounter()
 {
-    --m_workersCount;
-
-    if (m_workersCount > 0) {
+    if (m_workerThread->isRunning()) {
         // The worker thread will continue running. It could even be running
         // a method of m_worker at the moment, so we delete it using
         // deleteLater() to prevent a crash.
@@ -79,49 +73,49 @@ KDirectoryContentsCounter::~KDirectoryContentsCounter()
     }
 }
 
-void KDirectoryContentsCounter::addDirectory(const QString& path)
+void KDirectoryContentsCounter::scanDirectory(const QString& path)
 {
     startWorker(path);
 }
 
-int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path)
+void KDirectoryContentsCounter::slotResult(const QString& path, int count, long size)
 {
-    const QString resolvedPath = QFileInfo(path).canonicalFilePath();
+    m_workerIsBusy = false;
+
+    const QFileInfo info = QFileInfo(path);
+    const QString resolvedPath = info.canonicalFilePath();
 
     if (!m_dirWatcher->contains(resolvedPath)) {
         m_dirWatcher->addDir(resolvedPath);
         m_watchedDirs.insert(resolvedPath);
     }
 
-    KDirectoryContentsCounterWorker::Options options;
-
-    if (m_model->showHiddenFiles()) {
-        options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
-    }
-
-    if (m_model->showDirectoriesOnly()) {
-        options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
+    if (!m_priorityQueue.empty()) {
+        const QString firstPath = m_priorityQueue.front();
+        m_priorityQueue.pop_front();
+        startWorker(firstPath);
+    } else if (!m_queue.empty()) {
+        const QString firstPath = m_queue.front();
+        m_queue.pop_front();
+        startWorker(firstPath);
     }
 
-    return KDirectoryContentsCounterWorker::subItemsCount(path, options);
-}
-
-void KDirectoryContentsCounter::slotResult(const QString& path, int count)
-{
-    m_workerIsBusy = false;
-
-    const QString resolvedPath = QFileInfo(path).canonicalFilePath();
-
-    if (!m_dirWatcher->contains(resolvedPath)) {
-        m_dirWatcher->addDir(resolvedPath);
-        m_watchedDirs.insert(resolvedPath);
+    if (s_cache->contains(resolvedPath)) {
+        const auto pair = s_cache->value(resolvedPath);
+        if (pair.first == count && pair.second == size) {
+            // no change no need to send another result event
+            return;
+        }
     }
 
-    if (!m_queue.isEmpty()) {
-        startWorker(m_queue.dequeue());
+    if (info.dir().path() == m_model->rootItem().url().path()) {
+        // update cache or overwrite value
+        // when path is a direct children of the current model root
+        s_cache->insert(resolvedPath, QPair<int, long>(count, size));
     }
 
-    emit result(path, count);
+    // sends the results
+    Q_EMIT result(path, count, size);
 }
 
 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
@@ -146,7 +140,7 @@ void KDirectoryContentsCounter::slotItemsRemoved()
     if (!m_watchedDirs.isEmpty()) {
         // Don't let KDirWatch watch for removed items
         if (allItemsRemoved) {
-            foreach (const QString& path, m_watchedDirs) {
+            for (const QString& path : qAsConst(m_watchedDirs)) {
                 m_dirWatcher->removeDir(path);
             }
             m_watchedDirs.clear();
@@ -166,8 +160,25 @@ void KDirectoryContentsCounter::slotItemsRemoved()
 
 void KDirectoryContentsCounter::startWorker(const QString& path)
 {
+    const QString resolvedPath = QFileInfo(path).canonicalFilePath();
+    const bool alreadyInCache = s_cache->contains(resolvedPath);
+    if (alreadyInCache) {
+        // fast path when in cache
+        // will be updated later if result has changed
+        const auto pair = s_cache->value(resolvedPath);
+        Q_EMIT result(path, pair.first, pair.second);
+    }
+
     if (m_workerIsBusy) {
-        m_queue.enqueue(path);
+        if (std::find(m_queue.begin(), m_queue.end(), path) == m_queue.end() &&
+            std::find(m_priorityQueue.begin(), m_priorityQueue.end(), path) == m_priorityQueue.end()) {
+            if (alreadyInCache) {
+                m_queue.push_back(path);
+            } else {
+                // append to priority queue
+                m_priorityQueue.push_back(path);
+            }
+        }
     } else {
         KDirectoryContentsCounterWorker::Options options;
 
@@ -179,10 +190,9 @@ void KDirectoryContentsCounter::startWorker(const QString& path)
             options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
         }
 
-        emit requestDirectoryContentsCount(path, options);
+        Q_EMIT requestDirectoryContentsCount(path, options);
         m_workerIsBusy = true;
     }
 }
 
 QThread* KDirectoryContentsCounter::m_workerThread = nullptr;
-int KDirectoryContentsCounter::m_workersCount = 0;