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 "dolphin_contentdisplaysettings.h"
10 #include "kitemviews/kfileitemmodel.h"
30 inline operator bool() const
32 return timestamp
!= 0 && count
!= -1;
41 cacheData
insert(const QString
&key
, cacheData data
, bool inserted
)
43 data
.timestamp
= QDateTime::currentMSecsSinceEpoch();
47 m_cache
.insert(key
, data
);
51 cacheData
value(const QString
&key
) const
53 return m_cache
.value(key
);
56 void unRefAll(const QSet
<QString
> &keys
)
58 for (const auto &key
: keys
) {
59 auto entry
= m_cache
[key
];
61 if (entry
.refCount
== 0) {
67 void removeAll(const QSet
<QString
> &keys
)
69 for (const auto &key
: keys
) {
75 QHash
<QString
, cacheData
> m_cache
;
78 /// cache of directory counting result
79 static LocalCache
*s_cache
;
80 static QThread
*s_workerThread
;
81 static KDirectoryContentsCounterWorker
*s_worker
;
84 KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel
*model
, QObject
*parent
)
89 , m_workerIsBusy(false)
90 , m_dirWatcher(nullptr)
93 if (s_cache
== nullptr) {
94 s_cache
= new LocalCache();
97 if (!s_workerThread
) {
98 s_workerThread
= new QThread();
99 s_workerThread
->setObjectName(QStringLiteral("KDirectoryContentsCounterThread"));
100 s_workerThread
->start();
104 s_worker
= new KDirectoryContentsCounterWorker();
105 s_worker
->moveToThread(s_workerThread
);
108 connect(m_model
, &KFileItemModel::itemsRemoved
, this, &KDirectoryContentsCounter::slotItemsRemoved
);
109 connect(m_model
, &KFileItemModel::directoryRefreshing
, this, &KDirectoryContentsCounter::slotDirectoryRefreshing
);
111 connect(this, &KDirectoryContentsCounter::requestDirectoryContentsCount
, s_worker
, &KDirectoryContentsCounterWorker::countDirectoryContents
);
113 connect(s_worker
, &KDirectoryContentsCounterWorker::result
, this, &KDirectoryContentsCounter::slotResult
);
114 connect(s_worker
, &KDirectoryContentsCounterWorker::intermediateResult
, this, &KDirectoryContentsCounter::result
);
115 connect(s_worker
, &KDirectoryContentsCounterWorker::finished
, this, &KDirectoryContentsCounter::scheduleNext
);
117 m_dirWatcher
= new KDirWatch(this);
118 connect(m_dirWatcher
, &KDirWatch::dirty
, this, &KDirectoryContentsCounter::slotDirWatchDirty
);
121 KDirectoryContentsCounter::~KDirectoryContentsCounter()
123 s_cache
->unRefAll(m_watchedDirs
);
126 void KDirectoryContentsCounter::slotResult(const QString
&path
, int count
, long long size
)
128 const auto fileInfo
= QFileInfo(path
);
129 const QString resolvedPath
= fileInfo
.canonicalFilePath();
130 if (fileInfo
.isReadable() && !m_watchedDirs
.contains(resolvedPath
)) {
131 m_dirWatcher
->addDir(resolvedPath
);
133 bool inserted
= m_watchedDirs
.insert(resolvedPath
) == m_watchedDirs
.end();
135 // update cache or overwrite value
136 s_cache
->insert(resolvedPath
, {count
, size
, true}, inserted
);
139 Q_EMIT
result(path
, count
, size
);
142 void KDirectoryContentsCounter::slotDirWatchDirty(const QString
&path
)
144 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
146 if (!m_model
->fileItem(index
).isDir()) {
147 // If INotify is used, KDirWatch issues the dirty() signal
148 // also for changed files inside the directory, even if we
149 // don't enable this behavior explicitly (see bug 309740).
153 scanDirectory(path
, PathCountPriority::High
);
157 void KDirectoryContentsCounter::slotItemsRemoved()
159 const bool allItemsRemoved
= (m_model
->count() == 0);
161 if (allItemsRemoved
) {
162 s_cache
->removeAll(m_watchedDirs
);
166 if (!m_watchedDirs
.isEmpty()) {
167 // Don't let KDirWatch watch for removed items
168 if (allItemsRemoved
) {
169 for (const QString
&path
: std::as_const(m_watchedDirs
)) {
170 m_dirWatcher
->removeDir(path
);
172 m_watchedDirs
.clear();
174 QMutableSetIterator
<QString
> it(m_watchedDirs
);
175 while (it
.hasNext()) {
176 const QString
&path
= it
.next();
177 if (m_model
->index(QUrl::fromLocalFile(path
)) < 0) {
178 m_dirWatcher
->removeDir(path
);
186 void KDirectoryContentsCounter::slotDirectoryRefreshing()
188 s_cache
->removeAll(m_watchedDirs
);
191 void KDirectoryContentsCounter::scheduleNext()
193 if (!m_priorityQueue
.empty()) {
194 m_currentPath
= m_priorityQueue
.front();
195 m_priorityQueue
.pop_front();
196 } else if (!m_queue
.empty()) {
197 m_currentPath
= m_queue
.front();
200 m_currentPath
.clear();
201 m_workerIsBusy
= false;
205 const auto fileInfo
= QFileInfo(m_currentPath
);
206 const QString resolvedPath
= fileInfo
.canonicalFilePath();
207 const auto pair
= s_cache
->value(resolvedPath
);
209 // fast path when in cache
210 // will be updated later if result has changed
211 Q_EMIT
result(m_currentPath
, pair
.count
, pair
.size
);
214 // if scanned fully recently, skip rescan
215 if (pair
&& pair
.timestamp
>= fileInfo
.fileTime(QFile::FileModificationTime
).toMSecsSinceEpoch()) {
220 KDirectoryContentsCounterWorker::Options options
;
222 if (m_model
->showHiddenFiles()) {
223 options
|= KDirectoryContentsCounterWorker::CountHiddenFiles
;
226 m_workerIsBusy
= true;
227 Q_EMIT
requestDirectoryContentsCount(m_currentPath
, options
, ContentDisplaySettings::recursiveDirectorySizeLimit());
230 void KDirectoryContentsCounter::enqueuePathScanning(const QString
&path
, bool alreadyInCache
, PathCountPriority priority
)
232 // ensure to update the entry in the queue
233 auto it
= std::find(m_queue
.begin(), m_queue
.end(), path
);
234 if (it
!= m_queue
.end()) {
237 it
= std::find(m_priorityQueue
.begin(), m_priorityQueue
.end(), path
);
238 if (it
!= m_priorityQueue
.end()) {
239 m_priorityQueue
.erase(it
);
243 if (priority
== PathCountPriority::Normal
) {
244 if (alreadyInCache
) {
245 // we already knew the dir size
246 // otherwise it gets lower priority
247 m_queue
.push_back(path
);
249 m_queue
.push_front(path
);
252 // append to priority queue
253 m_priorityQueue
.push_front(path
);
257 void KDirectoryContentsCounter::scanDirectory(const QString
&path
, PathCountPriority priority
)
259 if (m_workerIsBusy
&& m_currentPath
== path
) {
264 const auto fileInfo
= QFileInfo(path
);
265 const QString resolvedPath
= fileInfo
.canonicalFilePath();
266 const auto pair
= s_cache
->value(resolvedPath
);
268 // fast path when in cache
269 // will be updated later if result has changed
270 Q_EMIT
result(path
, pair
.count
, pair
.size
);
273 // if scanned fully recently, skip rescan
274 if (pair
&& pair
.timestamp
>= fileInfo
.fileTime(QFile::FileModificationTime
).toMSecsSinceEpoch()) {
278 enqueuePathScanning(path
, pair
, priority
);
280 if (!m_workerIsBusy
&& !s_worker
->stopping()) {
285 void KDirectoryContentsCounter::stopWorker()
288 m_priorityQueue
.clear();
290 if (m_workerIsBusy
&& m_currentPath
== s_worker
->scannedPath()) {
293 m_currentPath
.clear();
296 #include "moc_kdirectorycontentscounter.cpp"