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