2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "kdirectorycontentscounter.h"
9 #include "kitemviews/kfileitemmodel.h"
19 /// cache of directory counting result
20 static QHash
<QString
, QPair
<int, long>> *s_cache
;
23 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel
*model
, QObject
*parent
)
28 , m_workerIsBusy(false)
29 , m_dirWatcher(nullptr)
32 connect(m_model
, &KFileItemModel::itemsRemoved
, this, &KDirectoryContentsCounter::slotItemsRemoved
);
34 if (!m_workerThread
) {
35 m_workerThread
= new QThread();
36 m_workerThread
->start();
39 if (s_cache
== nullptr) {
40 s_cache
= new QHash
<QString
, QPair
<int, long>>();
43 m_worker
= new KDirectoryContentsCounterWorker();
44 m_worker
->moveToThread(m_workerThread
);
46 connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount
, m_worker
, &KDirectoryContentsCounterWorker::countDirectoryContents
);
47 connect(m_worker
, &KDirectoryContentsCounterWorker::result
, this, &KDirectoryContentsCounter::slotResult
);
49 m_dirWatcher
= new KDirWatch(this);
50 connect(m_dirWatcher
, &KDirWatch::dirty
, this, &KDirectoryContentsCounter::slotDirWatchDirty
);
53 KDirectoryContentsCounter::~KDirectoryContentsCounter()
55 if (m_workerThread
->isRunning()) {
56 // The worker thread will continue running. It could even be running
57 // a method of m_worker at the moment, so we delete it using
58 // deleteLater() to prevent a crash.
59 m_worker
->deleteLater();
61 // There are no remaining workers -> stop the worker thread.
62 m_workerThread
->quit();
63 m_workerThread
->wait();
64 delete m_workerThread
;
65 m_workerThread
= nullptr;
67 // The worker thread has finished running now, so it's safe to delete
68 // m_worker. deleteLater() would not work at all because the event loop
69 // which would deliver the event to m_worker is not running any more.
74 void KDirectoryContentsCounter::scanDirectory(const QString
&path
)
79 void KDirectoryContentsCounter::slotResult(const QString
&path
, int count
, long size
)
81 m_workerIsBusy
= false;
83 const QFileInfo info
= QFileInfo(path
);
84 const QString resolvedPath
= info
.canonicalFilePath();
86 if (!m_dirWatcher
->contains(resolvedPath
)) {
87 m_dirWatcher
->addDir(resolvedPath
);
88 m_watchedDirs
.insert(resolvedPath
);
91 if (!m_priorityQueue
.empty()) {
92 const QString firstPath
= m_priorityQueue
.front();
93 m_priorityQueue
.pop_front();
94 startWorker(firstPath
);
95 } else if (!m_queue
.empty()) {
96 const QString firstPath
= m_queue
.front();
98 startWorker(firstPath
);
101 if (s_cache
->contains(resolvedPath
)) {
102 const auto pair
= s_cache
->value(resolvedPath
);
103 if (pair
.first
== count
&& pair
.second
== size
) {
104 // no change no need to send another result event
109 if (info
.dir().path() == m_model
->rootItem().url().path()) {
110 // update cache or overwrite value
111 // when path is a direct children of the current model root
112 s_cache
->insert(resolvedPath
, QPair
<int, long>(count
, size
));
116 Q_EMIT
result(path
, count
, size
);
119 void KDirectoryContentsCounter::slotDirWatchDirty(const QString
&path
)
121 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
123 if (!m_model
->fileItem(index
).isDir()) {
124 // If INotify is used, KDirWatch issues the dirty() signal
125 // also for changed files inside the directory, even if we
126 // don't enable this behavior explicitly (see bug 309740).
134 void KDirectoryContentsCounter::slotItemsRemoved()
136 const bool allItemsRemoved
= (m_model
->count() == 0);
138 if (!m_watchedDirs
.isEmpty()) {
139 // Don't let KDirWatch watch for removed items
140 if (allItemsRemoved
) {
141 for (const QString
&path
: qAsConst(m_watchedDirs
)) {
142 m_dirWatcher
->removeDir(path
);
144 m_watchedDirs
.clear();
147 QMutableSetIterator
<QString
> it(m_watchedDirs
);
148 while (it
.hasNext()) {
149 const QString
&path
= it
.next();
150 if (m_model
->index(QUrl::fromLocalFile(path
)) < 0) {
151 m_dirWatcher
->removeDir(path
);
159 void KDirectoryContentsCounter::startWorker(const QString
&path
)
161 const QString resolvedPath
= QFileInfo(path
).canonicalFilePath();
162 const bool alreadyInCache
= s_cache
->contains(resolvedPath
);
163 if (alreadyInCache
) {
164 // fast path when in cache
165 // will be updated later if result has changed
166 const auto pair
= s_cache
->value(resolvedPath
);
167 Q_EMIT
result(path
, pair
.first
, pair
.second
);
170 if (m_workerIsBusy
) {
171 if (std::find(m_queue
.begin(), m_queue
.end(), path
) == m_queue
.end()
172 && std::find(m_priorityQueue
.begin(), m_priorityQueue
.end(), path
) == m_priorityQueue
.end()) {
173 if (alreadyInCache
) {
174 m_queue
.push_back(path
);
176 // append to priority queue
177 m_priorityQueue
.push_back(path
);
181 KDirectoryContentsCounterWorker::Options options
;
183 if (m_model
->showHiddenFiles()) {
184 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
187 if (m_model
->showDirectoriesOnly()) {
188 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
191 Q_EMIT
requestDirectoryContentsCount(path
, options
);
192 m_workerIsBusy
= true;
196 QThread
*KDirectoryContentsCounter::m_workerThread
= nullptr;