1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
21 #include "kdirectorycontentscounter.h"
22 #include "kitemviews/kfileitemmodel.h"
31 /// cache of directory counting result
32 static QHash
<QString
, QPair
<int, long>> *s_cache
;
35 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel
* model
, QObject
* parent
) :
40 m_workerIsBusy(false),
41 m_dirWatcher(nullptr),
44 connect(m_model
, &KFileItemModel::itemsRemoved
,
45 this, &KDirectoryContentsCounter::slotItemsRemoved
);
47 if (!m_workerThread
) {
48 m_workerThread
= new QThread();
49 m_workerThread
->start();
52 if (s_cache
== nullptr) {
53 s_cache
= new QHash
<QString
, QPair
<int, long>>();
56 m_worker
= new KDirectoryContentsCounterWorker();
57 m_worker
->moveToThread(m_workerThread
);
59 connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount
,
60 m_worker
, &KDirectoryContentsCounterWorker::countDirectoryContents
);
61 connect(m_worker
, &KDirectoryContentsCounterWorker::result
,
62 this, &KDirectoryContentsCounter::slotResult
);
64 m_dirWatcher
= new KDirWatch(this);
65 connect(m_dirWatcher
, &KDirWatch::dirty
, this, &KDirectoryContentsCounter::slotDirWatchDirty
);
68 KDirectoryContentsCounter::~KDirectoryContentsCounter()
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();
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;
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.
89 void KDirectoryContentsCounter::scanDirectory(const QString
& path
)
94 void KDirectoryContentsCounter::slotResult(const QString
& path
, int count
, long size
)
96 m_workerIsBusy
= false;
98 const QFileInfo info
= QFileInfo(path
);
99 const QString resolvedPath
= info
.canonicalFilePath();
101 if (!m_dirWatcher
->contains(resolvedPath
)) {
102 m_dirWatcher
->addDir(resolvedPath
);
103 m_watchedDirs
.insert(resolvedPath
);
106 if (!m_queue
.isEmpty()) {
107 startWorker(m_queue
.takeFirst());
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
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
));
125 emit
result(resolvedPath
, count
, size
);
128 void KDirectoryContentsCounter::slotDirWatchDirty(const QString
& path
)
130 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
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).
143 void KDirectoryContentsCounter::slotItemsRemoved()
145 const bool allItemsRemoved
= (m_model
->count() == 0);
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
);
153 m_watchedDirs
.clear();
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
);
168 void KDirectoryContentsCounter::startWorker(const QString
& path
)
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
);
177 if (m_workerIsBusy
) {
178 if (!m_queue
.contains(path
)) {
179 m_queue
.append(path
);
182 KDirectoryContentsCounterWorker::Options options
;
184 if (m_model
->showHiddenFiles()) {
185 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
188 if (m_model
->showDirectoriesOnly()) {
189 options
|= KDirectoryContentsCounterWorker::CountDirectoriesOnly
;
192 emit
requestDirectoryContentsCount(path
, options
);
193 m_workerIsBusy
= true;
197 QThread
* KDirectoryContentsCounter::m_workerThread
= nullptr;