┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews/private/kdirectorycontentscounter.cpp
diff options
context:
space:
mode:
authorFrank Reininghaus <[email protected]>2013-09-04 21:49:01 +0200
committerFrank Reininghaus <[email protected]>2013-09-04 21:50:16 +0200
commit4bfc28cb4bceb55996eec853da383efb44aaeeba (patch)
tree2ff8dc01d3692db9fe05f238ceae80e0da8307f6 /src/kitemviews/private/kdirectorycontentscounter.cpp
parent8f7c3619ec734bbf31ae81315a6548db9cbd19aa (diff)
Count the items inside directories in another thread
This prevents that the GUI freezes if there are many files inside the directory, or if the access to the directory is slow for some other reason. BUG: 318518 REVIEW: 111920 FIXED-IN: 4.12.0
Diffstat (limited to 'src/kitemviews/private/kdirectorycontentscounter.cpp')
-rw-r--r--src/kitemviews/private/kdirectorycontentscounter.cpp164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/kitemviews/private/kdirectorycontentscounter.cpp b/src/kitemviews/private/kdirectorycontentscounter.cpp
new file mode 100644
index 000000000..fd8479feb
--- /dev/null
+++ b/src/kitemviews/private/kdirectorycontentscounter.cpp
@@ -0,0 +1,164 @@
+/***************************************************************************
+ * Copyright (C) 2011 by Peter Penz <[email protected]> *
+ * Copyright (C) 2013 by Frank Reininghaus <[email protected]> *
+ * *
+ * 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 *
+ ***************************************************************************/
+
+#include "kdirectorycontentscounter.h"
+
+#include "kdirectorycontentscounterworker.h"
+#include <kitemviews/kfileitemmodel.h>
+
+#include <KDirWatch>
+#include <QThread>
+
+KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) :
+ QObject(parent),
+ m_model(model),
+ m_queue(),
+ m_workerThread(0),
+ m_worker(0),
+ m_workerIsBusy(false),
+ m_dirWatcher(0),
+ m_watchedDirs()
+{
+ connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
+ this, SLOT(slotItemsRemoved()));
+
+ m_workerThread = new QThread(this);
+ m_worker = new KDirectoryContentsCounterWorker();
+ m_worker->moveToThread(m_workerThread);
+
+ connect(this, SIGNAL(requestDirectoryContentsCount(QString,KDirectoryContentsCounterWorker::Options)),
+ m_worker, SLOT(countDirectoryContents(QString,KDirectoryContentsCounterWorker::Options)));
+ connect(m_worker, SIGNAL(result(QString,int)),
+ this, SLOT(slotResult(QString,int)));
+
+ m_workerThread->start();
+
+ m_dirWatcher = new KDirWatch(this);
+ connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
+}
+
+KDirectoryContentsCounter::~KDirectoryContentsCounter()
+{
+ m_workerThread->quit();
+ m_workerThread->wait();
+
+ delete m_worker;
+}
+
+void KDirectoryContentsCounter::addDirectory(const QString& path)
+{
+ startWorker(path);
+}
+
+int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path)
+{
+ if (!m_dirWatcher->contains(path)) {
+ m_dirWatcher->addDir(path);
+ m_watchedDirs.insert(path);
+ }
+
+ KDirectoryContentsCounterWorker::Options options;
+
+ if (m_model->showHiddenFiles()) {
+ options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
+ }
+
+ if (m_model->showDirectoriesOnly()) {
+ options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
+ }
+
+ return KDirectoryContentsCounterWorker::subItemsCount(path, options);
+}
+
+void KDirectoryContentsCounter::slotResult(const QString& path, int count)
+{
+ m_workerIsBusy = false;
+
+ if (!m_dirWatcher->contains(path)) {
+ m_dirWatcher->addDir(path);
+ m_watchedDirs.insert(path);
+ }
+
+ if (!m_queue.isEmpty()) {
+ startWorker(m_queue.dequeue());
+ }
+
+ emit result(path, count);
+}
+
+void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
+{
+ const int index = m_model->index(KUrl(path));
+ if (index >= 0) {
+ if (!m_model->fileItem(index).isDir()) {
+ // If INotify is used, KDirWatch issues the dirty() signal
+ // also for changed files inside the directory, even if we
+ // don't enable this behavior explicitly (see bug 309740).
+ return;
+ }
+
+ startWorker(path);
+ }
+}
+
+void KDirectoryContentsCounter::slotItemsRemoved()
+{
+ const bool allItemsRemoved = (m_model->count() == 0);
+
+ if (!m_watchedDirs.isEmpty()) {
+ // Don't let KDirWatch watch for removed items
+ if (allItemsRemoved) {
+ foreach (const QString& path, m_watchedDirs) {
+ m_dirWatcher->removeDir(path);
+ }
+ m_watchedDirs.clear();
+ m_queue.clear();
+ } else {
+ QMutableSetIterator<QString> it(m_watchedDirs);
+ while (it.hasNext()) {
+ const QString& path = it.next();
+ if (m_model->index(KUrl(path)) < 0) {
+ m_dirWatcher->removeDir(path);
+ it.remove();
+ }
+ }
+ }
+ }
+}
+
+void KDirectoryContentsCounter::startWorker(const QString& path)
+{
+ if (m_workerIsBusy) {
+ m_queue.enqueue(path);
+ } else {
+ KDirectoryContentsCounterWorker::Options options;
+
+ if (m_model->showHiddenFiles()) {
+ options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
+ }
+
+ if (m_model->showDirectoriesOnly()) {
+ options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
+ }
+
+ emit requestDirectoryContentsCount(path, options);
+ m_workerIsBusy = true;
+ }
+}