┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews/kfileitemmodelrolesupdater.cpp
diff options
context:
space:
mode:
authorMéven Car <[email protected]>2023-05-26 15:10:09 +0000
committerMéven Car <[email protected]>2023-05-26 15:10:09 +0000
commita1c5c5cf81b5d1f6b7a0aa10b8a981cb70c5b26c (patch)
tree0fcfce01dcef32b1b70b084e9719cf7d9c26418d /src/kitemviews/kfileitemmodelrolesupdater.cpp
parentab13c8881dafbe5beefef0db5891ce9f731ef489 (diff)
KDirectoryContentsCounter: show intermediate dir size counting results, improve stopping, improve data caching
Two user visible changes: * we can see the dir size changing as it is updated. * This makes the file counting a lot more reactive, when changing directories for instance. `KDirectoryContentsCounterWorker::walkDir` is not recursive anymore. The cache is now shared and only a single thread is used to count size recursively.
Diffstat (limited to 'src/kitemviews/kfileitemmodelrolesupdater.cpp')
-rw-r--r--src/kitemviews/kfileitemmodelrolesupdater.cpp88
1 files changed, 66 insertions, 22 deletions
diff --git a/src/kitemviews/kfileitemmodelrolesupdater.cpp b/src/kitemviews/kfileitemmodelrolesupdater.cpp
index c488fc0cf..6838d0861 100644
--- a/src/kitemviews/kfileitemmodelrolesupdater.cpp
+++ b/src/kitemviews/kfileitemmodelrolesupdater.cpp
@@ -10,6 +10,7 @@
#include "kfileitemmodel.h"
#include "private/kdirectorycontentscounter.h"
#include "private/kpixmapmodifier.h"
+#include "qdir.h"
#include <KConfig>
#include <KConfigGroup>
@@ -20,6 +21,8 @@
#include <KPluginMetaData>
#include <KSharedConfig>
+#include "dolphin_detailsmodesettings.h"
+
#if HAVE_BALOO
#include "private/kbaloorolesprovider.h"
#include <Baloo/File>
@@ -72,7 +75,6 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel *model, QO
, m_resolvableRoles()
, m_enabledPlugins()
, m_localFileSizePreviewLimit(0)
- , m_scanDirectories(true)
, m_pendingSortRoleItems()
, m_pendingIndexes()
, m_pendingPreviewItems()
@@ -321,16 +323,6 @@ qlonglong KFileItemModelRolesUpdater::localFileSizePreviewLimit() const
return m_localFileSizePreviewLimit;
}
-void KFileItemModelRolesUpdater::setScanDirectories(bool enabled)
-{
- m_scanDirectories = enabled;
-}
-
-bool KFileItemModelRolesUpdater::scanDirectories() const
-{
- return m_scanDirectories;
-}
-
void KFileItemModelRolesUpdater::setHoverSequenceState(const QUrl &itemUrl, int seqIdx)
{
const KFileItem item = m_model->fileItem(itemUrl);
@@ -423,8 +415,7 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList &itemRang
m_hoverSequenceLoadedItems.clear();
killPreviewJob();
-
- if (m_scanDirectories) {
+ if (!m_model->showDirectoriesOnly()) {
m_directoryContentsCounter->stopWorker();
}
} else {
@@ -856,10 +847,10 @@ void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem &
#endif
}
-void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString &path, int count, long size)
+void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString &path, int count, long long size)
{
- const bool getSizeRole = m_roles.contains("size");
const bool getIsExpandableRole = m_roles.contains("isExpandable");
+ const bool getSizeRole = m_roles.contains("size");
if (getSizeRole || getIsExpandableRole) {
const int index = m_model->index(QUrl::fromLocalFile(path));
@@ -1278,18 +1269,71 @@ bool KFileItemModelRolesUpdater::applyResolvedRoles(int index, ResolveHint hint)
void KFileItemModelRolesUpdater::startDirectorySizeCounting(const KFileItem &item, int index)
{
- if (item.isSlow()) {
+ if (DetailsModeSettings::directorySizeCount() || item.isSlow() || !item.isLocalFile()) {
+ // fastpath no recursion necessary
+
+ auto data = m_model->data(index);
+ if (data.value("size") == -2) {
+ // means job already started
+ return;
+ }
+
+ auto url = item.url();
+ if (!item.localPath().isEmpty()) {
+ // optimization for desktop:/, trash:/
+ url = QUrl::fromLocalFile(item.localPath());
+ }
+
+ data.insert("isExpandable", false);
+ data.insert("count", 0);
+ data.insert("size", -2); // invalid size, -1 means size unknown
+
+ disconnect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ m_model->setData(index, data);
+ connect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+
+ auto listJob = KIO::listDir(url);
+ QObject::connect(listJob, &KIO::ListJob::entries, this, [this, index](const KJob * /*job*/, const KIO::UDSEntryList &list) {
+ auto data = m_model->data(index);
+ int origCount = data.value("count").toInt();
+ int entryCount = origCount;
+
+ for (const KIO::UDSEntry &entry : list) {
+ const auto name = entry.stringValue(KIO::UDSEntry::UDS_NAME);
+
+ if (name == QStringLiteral("..") || name == QStringLiteral(".")) {
+ continue;
+ }
+ if (!m_model->showHiddenFiles() && name.startsWith(QLatin1Char('.'))) {
+ continue;
+ }
+ if (m_model->showDirectoriesOnly() && !entry.isDir()) {
+ continue;
+ }
+ ++entryCount;
+ }
+
+ // count has changed
+ if (origCount < entryCount) {
+ QHash<QByteArray, QVariant> data;
+ data.insert("isExpandable", entryCount > 0);
+ data.insert("count", entryCount);
+
+ disconnect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ m_model->setData(index, data);
+ connect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ }
+ });
return;
}
// Tell m_directoryContentsCounter that we want to count the items
// inside the directory. The result will be received in slotDirectoryContentsCountReceived.
- if (m_scanDirectories && item.isLocalFile()) {
- const QString path = item.localPath();
- const auto priority = index >= m_firstVisibleIndex && index <= m_lastVisibleIndex ? KDirectoryContentsCounter::PathCountPriority::High
- : KDirectoryContentsCounter::PathCountPriority::Normal;
- m_directoryContentsCounter->scanDirectory(path, priority);
- }
+ const QString path = item.localPath();
+ const auto priority = index >= m_firstVisibleIndex && index <= m_lastVisibleIndex ? KDirectoryContentsCounter::PathCountPriority::High
+ : KDirectoryContentsCounter::PathCountPriority::Normal;
+
+ m_directoryContentsCounter->scanDirectory(path, priority);
}
QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileItem &item, int index)