]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
KDirectoryContentsCounter: don't schedule scanning a folder already in the Queue
[dolphin.git] / src / kitemviews / private / kdirectorycontentscounter.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "kdirectorycontentscounter.h"
22 #include "kitemviews/kfileitemmodel.h"
23
24 #include <KDirWatch>
25
26 #include <QFileInfo>
27 #include <QDir>
28 #include <QThread>
29
30 namespace {
31 /// cache of directory counting result
32 static QHash<QString, QPair<int, long>> *s_cache;
33 }
34
35 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) :
36 QObject(parent),
37 m_model(model),
38 m_queue(),
39 m_worker(nullptr),
40 m_workerIsBusy(false),
41 m_dirWatcher(nullptr),
42 m_watchedDirs()
43 {
44 connect(m_model, &KFileItemModel::itemsRemoved,
45 this, &KDirectoryContentsCounter::slotItemsRemoved);
46
47 if (!m_workerThread) {
48 m_workerThread = new QThread();
49 m_workerThread->start();
50 }
51
52 if (s_cache == nullptr) {
53 s_cache = new QHash<QString, QPair<int, long>>();
54 }
55
56 m_worker = new KDirectoryContentsCounterWorker();
57 m_worker->moveToThread(m_workerThread);
58
59 connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount,
60 m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents);
61 connect(m_worker, &KDirectoryContentsCounterWorker::result,
62 this, &KDirectoryContentsCounter::slotResult);
63
64 m_dirWatcher = new KDirWatch(this);
65 connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty);
66 }
67
68 KDirectoryContentsCounter::~KDirectoryContentsCounter()
69 {
70 if (m_workerThread->isRunning()) {
71 // The worker thread will continue running. It could even be running
72 // a method of m_worker at the moment, so we delete it using
73 // deleteLater() to prevent a crash.
74 m_worker->deleteLater();
75 } else {
76 // There are no remaining workers -> stop the worker thread.
77 m_workerThread->quit();
78 m_workerThread->wait();
79 delete m_workerThread;
80 m_workerThread = nullptr;
81
82 // The worker thread has finished running now, so it's safe to delete
83 // m_worker. deleteLater() would not work at all because the event loop
84 // which would deliver the event to m_worker is not running any more.
85 delete m_worker;
86 }
87 }
88
89 void KDirectoryContentsCounter::scanDirectory(const QString& path)
90 {
91 startWorker(path);
92 }
93
94 void KDirectoryContentsCounter::slotResult(const QString& path, int count, long size)
95 {
96 m_workerIsBusy = false;
97
98 const QFileInfo info = QFileInfo(path);
99 const QString resolvedPath = info.canonicalFilePath();
100
101 if (!m_dirWatcher->contains(resolvedPath)) {
102 m_dirWatcher->addDir(resolvedPath);
103 m_watchedDirs.insert(resolvedPath);
104 }
105
106 if (!m_queue.isEmpty()) {
107 startWorker(m_queue.takeFirst());
108 }
109
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;
115 }
116 }
117
118 if (info.dir().path() == m_model->rootItem().url().path()) {
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 }
123
124 // sends the results
125 emit result(resolvedPath, count, size);
126 }
127
128 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
129 {
130 const int index = m_model->index(QUrl::fromLocalFile(path));
131 if (index >= 0) {
132 if (!m_model->fileItem(index).isDir()) {
133 // If INotify is used, KDirWatch issues the dirty() signal
134 // also for changed files inside the directory, even if we
135 // don't enable this behavior explicitly (see bug 309740).
136 return;
137 }
138
139 startWorker(path);
140 }
141 }
142
143 void KDirectoryContentsCounter::slotItemsRemoved()
144 {
145 const bool allItemsRemoved = (m_model->count() == 0);
146
147 if (!m_watchedDirs.isEmpty()) {
148 // Don't let KDirWatch watch for removed items
149 if (allItemsRemoved) {
150 for (const QString& path : qAsConst(m_watchedDirs)) {
151 m_dirWatcher->removeDir(path);
152 }
153 m_watchedDirs.clear();
154 m_queue.clear();
155 } else {
156 QMutableSetIterator<QString> it(m_watchedDirs);
157 while (it.hasNext()) {
158 const QString& path = it.next();
159 if (m_model->index(QUrl::fromLocalFile(path)) < 0) {
160 m_dirWatcher->removeDir(path);
161 it.remove();
162 }
163 }
164 }
165 }
166 }
167
168 void KDirectoryContentsCounter::startWorker(const QString& path)
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
177 if (m_workerIsBusy) {
178 if (!m_queue.contains(path)) {
179 m_queue.append(path);
180 }
181 } else {
182 KDirectoryContentsCounterWorker::Options options;
183
184 if (m_model->showHiddenFiles()) {
185 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
186 }
187
188 if (m_model->showDirectoriesOnly()) {
189 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
190 }
191
192 emit requestDirectoryContentsCount(path, options);
193 m_workerIsBusy = true;
194 }
195 }
196
197 QThread* KDirectoryContentsCounter::m_workerThread = nullptr;