]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounter.cpp
bd204fe8e24a353a16a0dccdc6ff821b668c8474
[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 <QThread>
28
29 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObject* parent) :
30 QObject(parent),
31 m_model(model),
32 m_queue(),
33 m_worker(nullptr),
34 m_workerIsBusy(false),
35 m_dirWatcher(nullptr),
36 m_watchedDirs()
37 {
38 connect(m_model, &KFileItemModel::itemsRemoved,
39 this, &KDirectoryContentsCounter::slotItemsRemoved);
40
41 if (!m_workerThread) {
42 m_workerThread = new QThread();
43 m_workerThread->start();
44 }
45
46 m_worker = new KDirectoryContentsCounterWorker();
47 m_worker->moveToThread(m_workerThread);
48 ++m_workersCount;
49
50 connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount,
51 m_worker, &KDirectoryContentsCounterWorker::countDirectoryContents);
52 connect(m_worker, &KDirectoryContentsCounterWorker::result,
53 this, &KDirectoryContentsCounter::slotResult);
54
55 m_dirWatcher = new KDirWatch(this);
56 connect(m_dirWatcher, &KDirWatch::dirty, this, &KDirectoryContentsCounter::slotDirWatchDirty);
57 }
58
59 KDirectoryContentsCounter::~KDirectoryContentsCounter()
60 {
61 --m_workersCount;
62
63 if (m_workersCount > 0) {
64 // The worker thread will continue running. It could even be running
65 // a method of m_worker at the moment, so we delete it using
66 // deleteLater() to prevent a crash.
67 m_worker->deleteLater();
68 } else {
69 // There are no remaining workers -> stop the worker thread.
70 m_workerThread->quit();
71 m_workerThread->wait();
72 delete m_workerThread;
73 m_workerThread = nullptr;
74
75 // The worker thread has finished running now, so it's safe to delete
76 // m_worker. deleteLater() would not work at all because the event loop
77 // which would deliver the event to m_worker is not running any more.
78 delete m_worker;
79 }
80 }
81
82 void KDirectoryContentsCounter::addDirectory(const QString& path)
83 {
84 startWorker(path);
85 }
86
87 int KDirectoryContentsCounter::countDirectoryContentsSynchronously(const QString& path)
88 {
89 const QString resolvedPath = QFileInfo(path).canonicalFilePath();
90
91 if (!m_dirWatcher->contains(resolvedPath)) {
92 m_dirWatcher->addDir(resolvedPath);
93 m_watchedDirs.insert(resolvedPath);
94 }
95
96 KDirectoryContentsCounterWorker::Options options;
97
98 if (m_model->showHiddenFiles()) {
99 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
100 }
101
102 if (m_model->showDirectoriesOnly()) {
103 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
104 }
105
106 return KDirectoryContentsCounterWorker::subItemsCount(path, options);
107 }
108
109 void KDirectoryContentsCounter::slotResult(const QString& path, int count)
110 {
111 m_workerIsBusy = false;
112
113 const QString resolvedPath = QFileInfo(path).canonicalFilePath();
114
115 if (!m_dirWatcher->contains(resolvedPath)) {
116 m_dirWatcher->addDir(resolvedPath);
117 m_watchedDirs.insert(resolvedPath);
118 }
119
120 if (!m_queue.isEmpty()) {
121 startWorker(m_queue.dequeue());
122 }
123
124 emit result(path, count);
125 }
126
127 void KDirectoryContentsCounter::slotDirWatchDirty(const QString& path)
128 {
129 const int index = m_model->index(QUrl::fromLocalFile(path));
130 if (index >= 0) {
131 if (!m_model->fileItem(index).isDir()) {
132 // If INotify is used, KDirWatch issues the dirty() signal
133 // also for changed files inside the directory, even if we
134 // don't enable this behavior explicitly (see bug 309740).
135 return;
136 }
137
138 startWorker(path);
139 }
140 }
141
142 void KDirectoryContentsCounter::slotItemsRemoved()
143 {
144 const bool allItemsRemoved = (m_model->count() == 0);
145
146 if (!m_watchedDirs.isEmpty()) {
147 // Don't let KDirWatch watch for removed items
148 if (allItemsRemoved) {
149 foreach (const QString& path, m_watchedDirs) {
150 m_dirWatcher->removeDir(path);
151 }
152 m_watchedDirs.clear();
153 m_queue.clear();
154 } else {
155 QMutableSetIterator<QString> it(m_watchedDirs);
156 while (it.hasNext()) {
157 const QString& path = it.next();
158 if (m_model->index(QUrl::fromLocalFile(path)) < 0) {
159 m_dirWatcher->removeDir(path);
160 it.remove();
161 }
162 }
163 }
164 }
165 }
166
167 void KDirectoryContentsCounter::startWorker(const QString& path)
168 {
169 if (m_workerIsBusy) {
170 m_queue.enqueue(path);
171 } else {
172 KDirectoryContentsCounterWorker::Options options;
173
174 if (m_model->showHiddenFiles()) {
175 options |= KDirectoryContentsCounterWorker::CountHiddenFiles;
176 }
177
178 if (m_model->showDirectoriesOnly()) {
179 options |= KDirectoryContentsCounterWorker::CountDirectoriesOnly;
180 }
181
182 emit requestDirectoryContentsCount(path, options);
183 m_workerIsBusy = true;
184 }
185 }
186
187 QThread* KDirectoryContentsCounter::m_workerThread = nullptr;
188 int KDirectoryContentsCounter::m_workersCount = 0;