]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
Fix FIFO usage
[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_priorityQueue.isEmpty()) {
107 startWorker(m_priorityQueue.takeFirst());
108 } else if (!m_queue.isEmpty()) {
109 startWorker(m_queue.takeFirst());
110 }
111
112 if (s_cache->contains(resolvedPath)) {
113 const auto pair = s_cache->value(resolvedPath);
114 if (pair.first == count && pair.second == size) {
115 // no change no need to send another result event
116 return;
117 }
118 }
119
120 if (info.dir().path() == m_model->rootItem().url().path()) {
121 // update cache or overwrite value
122 // when path is a direct children of the current model root
123 s_cache->insert(resolvedPath, QPair<int, long>(count, size));
124 }
125
126 // sends the results
127 emit result(resolvedPath, count, size);
128 }
129
130 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
131 {
132 const int index = m_model->index(QUrl::fromLocalFile(path));
133 if (index >= 0) {
134 if (!m_model->fileItem(index).isDir()) {
135 // If INotify is used, KDirWatch issues the dirty() signal
136 // also for changed files inside the directory, even if we
137 // don't enable this behavior explicitly (see bug 309740).
138 return;
139 }
140
141 startWorker(path);
142 }
143 }
144
145 void KDirectoryContentsCounter::slotItemsRemoved()
146 {
147 const bool allItemsRemoved = (m_model->count() == 0);
148
149 if (!m_watchedDirs.isEmpty()) {
150 // Don't let KDirWatch watch for removed items
151 if (allItemsRemoved) {
152 for (const QString& path : qAsConst(m_watchedDirs)) {
153 m_dirWatcher->removeDir(path);
154 }
155 m_watchedDirs.clear();
156 m_queue.clear();
157 } else {
158 QMutableSetIterator<QString> it(m_watchedDirs);
159 while (it.hasNext()) {
160 const QString& path = it.next();
161 if (m_model->index(QUrl::fromLocalFile(path)) < 0) {
162 m_dirWatcher->removeDir(path);
163 it.remove();
164 }
165 }
166 }
167 }
168 }
169
170 void KDirectoryContentsCounter::startWorker(const QString& path)
171 {
172 const bool alreadyInCache = s_cache->contains(path);
173 if (alreadyInCache) {
174 // fast path when in cache
175 // will be updated later if result has changed
176 const auto pair = s_cache->value(path);
177 emit result(path, pair.first, pair.second);
178 }
179
180 if (m_workerIsBusy) {
181 if (!m_queue.contains(path) && !m_priorityQueue.contains(path)) {
182 if (alreadyInCache) {
183 m_queue.append(path);
184 } else {
185 // append to priority queue
186 m_priorityQueue.append(path);
187 }
188 }
189 } else {
190 KDirectoryContentsCounterWorker::Options options;
191
192 if (m_model->showHiddenFiles()) {
193 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
194 }
195
196 if (m_model->showDirectoriesOnly()) {
197 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
198 }
199
200 emit requestDirectoryContentsCount(path, options);
201 m_workerIsBusy = true;
202 }
203 }
204
205 QThread* KDirectoryContentsCounter::m_workerThread = nullptr;