X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/c16f777b289fb617217c8c19aa320fbe90f38209..38c34eeca:/src/kitemviews/private/kdirectorycontentscounter.cpp diff --git a/src/kitemviews/private/kdirectorycontentscounter.cpp b/src/kitemviews/private/kdirectorycontentscounter.cpp index 4d6a4861c..039b79b6e 100644 --- a/src/kitemviews/private/kdirectorycontentscounter.cpp +++ b/src/kitemviews/private/kdirectorycontentscounter.cpp @@ -1,48 +1,35 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * Copyright (C) 2013 by Frank Reininghaus * - * * - * 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 + * SPDX-FileCopyrightText: 2013 Frank Reininghaus + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "kdirectorycontentscounter.h" #include "kitemviews/kfileitemmodel.h" #include -#include #include +#include #include -namespace { - /// cache of directory counting result - static QHash> *s_cache; +namespace +{ +/// cache of directory counting result +static QHash> *s_cache; } -KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) : - QObject(parent), - m_model(model), - m_queue(), - m_worker(nullptr), - m_workerIsBusy(false), - m_dirWatcher(nullptr), - m_watchedDirs() +KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel *model, QObject *parent) + : QObject(parent) + , m_model(model) + , m_queue() + , m_worker(nullptr) + , m_workerIsBusy(false) + , m_dirWatcher(nullptr) + , m_watchedDirs() { - connect(m_model, &KFileItemModel::itemsRemoved, - this, &KDirectoryContentsCounter::slotItemsRemoved); + connect(m_model, &KFileItemModel::itemsRemoved, this, &KDirectoryContentsCounter::slotItemsRemoved); if (!m_workerThread) { m_workerThread = new QThread(); @@ -56,10 +43,8 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj m_worker = new KDirectoryContentsCounterWorker(); m_worker->moveToThread(m_workerThread); - connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount, - m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents); - connect(m_worker, &KDirectoryContentsCounterWorker::result, - this, &KDirectoryContentsCounter::slotResult); + connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount, m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents); + connect(m_worker, &KDirectoryContentsCounterWorker::result, this, &KDirectoryContentsCounter::slotResult); m_dirWatcher = new KDirWatch(this); connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty); @@ -86,12 +71,12 @@ KDirectoryContentsCounter::~KDirectoryContentsCounter() } } -void KDirectoryContentsCounter::scanDirectory(const QString& path) +void KDirectoryContentsCounter::scanDirectory(const QString &path) { startWorker(path); } -void KDirectoryContentsCounter::slotResult(const QString& path, int count, long size) +void KDirectoryContentsCounter::slotResult(const QString &path, int count, long size) { m_workerIsBusy = false; @@ -103,10 +88,14 @@ void KDirectoryContentsCounter::slotResult(const QString& path, int count, long m_watchedDirs.insert(resolvedPath); } - if (!m_priorityQueue.isEmpty()) { - startWorker(m_priorityQueue.takeFirst()); - } else if (!m_queue.isEmpty()) { - startWorker(m_queue.takeFirst()); + 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); } if (s_cache->contains(resolvedPath)) { @@ -124,10 +113,10 @@ void KDirectoryContentsCounter::slotResult(const QString& path, int count, long } // sends the results - emit result(resolvedPath, count, size); + Q_EMIT result(path, count, size); } -void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path) +void KDirectoryContentsCounter::slotDirWatchDirty(const QString &path) { const int index = m_model->index(QUrl::fromLocalFile(path)); if (index >= 0) { @@ -149,7 +138,7 @@ void KDirectoryContentsCounter::slotItemsRemoved() if (!m_watchedDirs.isEmpty()) { // Don't let KDirWatch watch for removed items if (allItemsRemoved) { - for (const QString& path : qAsConst(m_watchedDirs)) { + for (const QString &path : qAsConst(m_watchedDirs)) { m_dirWatcher->removeDir(path); } m_watchedDirs.clear(); @@ -157,7 +146,7 @@ void KDirectoryContentsCounter::slotItemsRemoved() } else { QMutableSetIterator it(m_watchedDirs); while (it.hasNext()) { - const QString& path = it.next(); + const QString &path = it.next(); if (m_model->index(QUrl::fromLocalFile(path)) < 0) { m_dirWatcher->removeDir(path); it.remove(); @@ -167,23 +156,25 @@ void KDirectoryContentsCounter::slotItemsRemoved() } } -void KDirectoryContentsCounter::startWorker(const QString& path) +void KDirectoryContentsCounter::startWorker(const QString &path) { - const bool alreadyInCache = s_cache->contains(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(path); - emit result(path, pair.first, pair.second); + const auto pair = s_cache->value(resolvedPath); + Q_EMIT result(path, pair.first, pair.second); } if (m_workerIsBusy) { - if (!m_queue.contains(path) && !m_priorityQueue.contains(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.append(path); + m_queue.push_back(path); } else { // append to priority queue - m_priorityQueue.append(path); + m_priorityQueue.push_back(path); } } } else { @@ -197,9 +188,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; +QThread *KDirectoryContentsCounter::m_workerThread = nullptr;