2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-FileCopyrightText: 2013 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "kfileitemmodel.h"
11 #include "dolphin_contentdisplaysettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphindebug.h"
14 #include "private/kfileitemmodelsortalgorithm.h"
18 #include <KLocalizedString>
19 #include <KUrlMimeData>
20 #include <kio_version.h>
22 #include <QElapsedTimer>
25 #include <QMimeDatabase>
26 #include <QRecursiveMutex>
30 #include <klazylocalizedstring.h>
32 Q_GLOBAL_STATIC(QRecursiveMutex
, s_collatorMutex
)
34 // #define KFILEITEMMODEL_DEBUG
36 KFileItemModel::KFileItemModel(QObject
*parent
)
37 : KItemModelBase("text", parent
)
38 , m_dirLister(nullptr)
39 , m_sortDirsFirst(true)
40 , m_sortHiddenLast(false)
41 , m_sortRole(NameRole
)
42 , m_sortingProgressPercent(-1)
49 , m_maximumUpdateIntervalTimer(nullptr)
50 , m_resortAllItemsTimer(nullptr)
51 , m_pendingItemsToInsert()
56 m_collator
.setNumericMode(true);
58 loadSortingSettings();
60 m_dirLister
= new KDirLister(this);
61 m_dirLister
->setAutoErrorHandlingEnabled(false);
62 m_dirLister
->setDelayedMimeTypes(true);
64 const QWidget
*parentWidget
= qobject_cast
<QWidget
*>(parent
);
66 m_dirLister
->setMainWindow(parentWidget
->window());
69 connect(m_dirLister
, &KCoreDirLister::started
, this, &KFileItemModel::directoryLoadingStarted
);
70 connect(m_dirLister
, &KCoreDirLister::canceled
, this, &KFileItemModel::slotCanceled
);
71 connect(m_dirLister
, &KCoreDirLister::itemsAdded
, this, &KFileItemModel::slotItemsAdded
);
72 connect(m_dirLister
, &KCoreDirLister::itemsDeleted
, this, &KFileItemModel::slotItemsDeleted
);
73 connect(m_dirLister
, &KCoreDirLister::refreshItems
, this, &KFileItemModel::slotRefreshItems
);
74 connect(m_dirLister
, &KCoreDirLister::clear
, this, &KFileItemModel::slotClear
);
75 connect(m_dirLister
, &KCoreDirLister::infoMessage
, this, &KFileItemModel::infoMessage
);
76 connect(m_dirLister
, &KCoreDirLister::jobError
, this, &KFileItemModel::slotListerError
);
77 connect(m_dirLister
, &KCoreDirLister::percent
, this, &KFileItemModel::directoryLoadingProgress
);
78 connect(m_dirLister
, &KCoreDirLister::redirection
, this, &KFileItemModel::directoryRedirection
);
79 connect(m_dirLister
, &KCoreDirLister::listingDirCompleted
, this, &KFileItemModel::slotCompleted
);
81 // Apply default roles that should be determined
83 m_requestRole
[NameRole
] = true;
84 m_requestRole
[IsDirRole
] = true;
85 m_requestRole
[IsLinkRole
] = true;
86 m_roles
.insert("text");
87 m_roles
.insert("isDir");
88 m_roles
.insert("isLink");
89 m_roles
.insert("isHidden");
91 // For slow KIO-slaves like used for searching it makes sense to show results periodically even
92 // before the completed() or canceled() signal has been emitted.
93 m_maximumUpdateIntervalTimer
= new QTimer(this);
94 m_maximumUpdateIntervalTimer
->setInterval(2000);
95 m_maximumUpdateIntervalTimer
->setSingleShot(true);
96 connect(m_maximumUpdateIntervalTimer
, &QTimer::timeout
, this, &KFileItemModel::dispatchPendingItemsToInsert
);
98 // When changing the value of an item which represents the sort-role a resorting must be
99 // triggered. Especially in combination with KFileItemModelRolesUpdater this might be done
100 // for a lot of items within a quite small timeslot. To prevent expensive resortings the
101 // resorting is postponed until the timer has been exceeded.
102 m_resortAllItemsTimer
= new QTimer(this);
103 m_resortAllItemsTimer
->setInterval(500);
104 m_resortAllItemsTimer
->setSingleShot(true);
105 connect(m_resortAllItemsTimer
, &QTimer::timeout
, this, &KFileItemModel::resortAllItems
);
107 connect(GeneralSettings::self(), &GeneralSettings::sortingChoiceChanged
, this, &KFileItemModel::slotSortingChoiceChanged
);
109 setShowTrashMime(m_dirLister
->showHiddenFiles());
112 KFileItemModel::~KFileItemModel()
114 qDeleteAll(m_itemData
);
115 qDeleteAll(m_filteredItems
);
116 qDeleteAll(m_pendingItemsToInsert
);
119 void KFileItemModel::loadDirectory(const QUrl
&url
)
121 m_dirLister
->openUrl(url
);
124 void KFileItemModel::refreshDirectory(const QUrl
&url
)
126 // Refresh all expanded directories first (Bug 295300)
127 QHashIterator
<QUrl
, QUrl
> expandedDirs(m_expandedDirs
);
128 while (expandedDirs
.hasNext()) {
130 m_dirLister
->openUrl(expandedDirs
.value(), KDirLister::Reload
);
133 m_dirLister
->openUrl(url
, KDirLister::Reload
);
135 Q_EMIT
directoryRefreshing();
138 QUrl
KFileItemModel::directory() const
140 return m_dirLister
->url();
143 void KFileItemModel::cancelDirectoryLoading()
148 int KFileItemModel::count() const
150 return m_itemData
.count();
153 QHash
<QByteArray
, QVariant
> KFileItemModel::data(int index
) const
155 if (index
>= 0 && index
< count()) {
156 ItemData
*data
= m_itemData
.at(index
);
157 if (data
->values
.isEmpty()) {
158 data
->values
= retrieveData(data
->item
, data
->parent
);
159 } else if (data
->values
.count() <= 2 && data
->values
.value("isExpanded").toBool()) {
160 // Special case dealt by slotRefreshItems(), avoid losing the "isExpanded" and "expandedParentsCount" state when refreshing
161 // slotRefreshItems() makes sure folders keep the "isExpanded" and "expandedParentsCount" while clearing the remaining values
162 // so this special request of different behavior can be identified here.
163 bool hasExpandedParentsCount
= false;
164 const int expandedParentsCount
= data
->values
.value("expandedParentsCount").toInt(&hasExpandedParentsCount
);
166 data
->values
= retrieveData(data
->item
, data
->parent
);
167 data
->values
.insert("isExpanded", true);
168 if (hasExpandedParentsCount
) {
169 data
->values
.insert("expandedParentsCount", expandedParentsCount
);
175 return QHash
<QByteArray
, QVariant
>();
178 bool KFileItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
> &values
)
180 if (index
< 0 || index
>= count()) {
184 QHash
<QByteArray
, QVariant
> currentValues
= data(index
);
186 // Determine which roles have been changed
187 QSet
<QByteArray
> changedRoles
;
188 QHashIterator
<QByteArray
, QVariant
> it(values
);
189 while (it
.hasNext()) {
191 const QByteArray role
= sharedValue(it
.key());
192 const QVariant value
= it
.value();
194 if (currentValues
[role
] != value
) {
195 currentValues
[role
] = value
;
196 changedRoles
.insert(role
);
200 if (changedRoles
.isEmpty()) {
204 m_itemData
[index
]->values
= currentValues
;
205 if (changedRoles
.contains("text")) {
206 QUrl url
= m_itemData
[index
]->item
.url();
207 url
= url
.adjusted(QUrl::RemoveFilename
);
208 url
.setPath(url
.path() + currentValues
["text"].toString());
209 m_itemData
[index
]->item
.setUrl(url
);
212 emitItemsChangedAndTriggerResorting(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
217 void KFileItemModel::setSortDirectoriesFirst(bool dirsFirst
)
219 if (dirsFirst
!= m_sortDirsFirst
) {
220 m_sortDirsFirst
= dirsFirst
;
225 bool KFileItemModel::sortDirectoriesFirst() const
227 return m_sortDirsFirst
;
230 void KFileItemModel::setSortHiddenLast(bool hiddenLast
)
232 if (hiddenLast
!= m_sortHiddenLast
) {
233 m_sortHiddenLast
= hiddenLast
;
238 bool KFileItemModel::sortHiddenLast() const
240 return m_sortHiddenLast
;
243 void KFileItemModel::setShowTrashMime(bool show
)
245 const auto trashMime
= QStringLiteral("application/x-trash");
246 QStringList excludeFilter
= m_filter
.excludeMimeTypes();
247 bool wasShown
= !excludeFilter
.contains(trashMime
);
251 excludeFilter
.removeAll(trashMime
);
255 excludeFilter
.append(trashMime
);
259 if (wasShown
!= show
) {
260 setExcludeMimeTypeFilter(excludeFilter
);
264 void KFileItemModel::setShowHiddenFiles(bool show
)
266 #if KIO_VERSION < QT_VERSION_CHECK(5, 100, 0)
267 m_dirLister
->setShowingDotFiles(show
);
269 m_dirLister
->setShowHiddenFiles(show
);
271 setShowTrashMime(show
);
272 m_dirLister
->emitChanges();
274 dispatchPendingItemsToInsert();
278 bool KFileItemModel::showHiddenFiles() const
280 #if KIO_VERSION < QT_VERSION_CHECK(5, 100, 0)
281 return m_dirLister
->showingDotFiles();
283 return m_dirLister
->showHiddenFiles();
287 void KFileItemModel::setShowDirectoriesOnly(bool enabled
)
289 m_dirLister
->setDirOnlyMode(enabled
);
292 bool KFileItemModel::showDirectoriesOnly() const
294 return m_dirLister
->dirOnlyMode();
297 QMimeData
*KFileItemModel::createMimeData(const KItemSet
&indexes
) const
299 QMimeData
*data
= new QMimeData();
301 // The following code has been taken from KDirModel::mimeData()
302 // (kdelibs/kio/kio/kdirmodel.cpp)
303 // SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
305 QList
<QUrl
> mostLocalUrls
;
306 const ItemData
*lastAddedItem
= nullptr;
308 for (int index
: indexes
) {
309 const ItemData
*itemData
= m_itemData
.at(index
);
310 const ItemData
*parent
= itemData
->parent
;
312 while (parent
&& parent
!= lastAddedItem
) {
313 parent
= parent
->parent
;
316 if (parent
&& parent
== lastAddedItem
) {
317 // A parent of 'itemData' has been added already.
321 lastAddedItem
= itemData
;
322 const KFileItem
&item
= itemData
->item
;
323 if (!item
.isNull()) {
327 mostLocalUrls
<< item
.mostLocalUrl(&isLocal
);
331 KUrlMimeData::setUrls(urls
, mostLocalUrls
, data
);
335 int KFileItemModel::indexForKeyboardSearch(const QString
&text
, int startFromIndex
) const
337 startFromIndex
= qMax(0, startFromIndex
);
338 for (int i
= startFromIndex
; i
< count(); ++i
) {
339 if (fileItem(i
).text().startsWith(text
, Qt::CaseInsensitive
)) {
343 for (int i
= 0; i
< startFromIndex
; ++i
) {
344 if (fileItem(i
).text().startsWith(text
, Qt::CaseInsensitive
)) {
351 bool KFileItemModel::supportsDropping(int index
) const
357 item
= fileItem(index
);
359 return !item
.isNull() && ((item
.isDir() && item
.isWritable()) || item
.isDesktopFile());
362 QString
KFileItemModel::roleDescription(const QByteArray
&role
) const
364 static QHash
<QByteArray
, QString
> description
;
365 if (description
.isEmpty()) {
367 const RoleInfoMap
*map
= rolesInfoMap(count
);
368 for (int i
= 0; i
< count
; ++i
) {
369 if (map
[i
].roleTranslation
.isEmpty()) {
372 description
.insert(map
[i
].role
, map
[i
].roleTranslation
.toString());
376 return description
.value(role
);
379 QList
<QPair
<int, QVariant
>> KFileItemModel::groups() const
381 if (!m_itemData
.isEmpty() && m_groups
.isEmpty()) {
382 #ifdef KFILEITEMMODEL_DEBUG
386 switch (typeForRole(sortRole())) {
388 m_groups
= nameRoleGroups();
391 m_groups
= sizeRoleGroups();
393 case ModificationTimeRole
:
394 m_groups
= timeRoleGroups([](const ItemData
*item
) {
395 return item
->item
.time(KFileItem::ModificationTime
);
398 case CreationTimeRole
:
399 m_groups
= timeRoleGroups([](const ItemData
*item
) {
400 return item
->item
.time(KFileItem::CreationTime
);
404 m_groups
= timeRoleGroups([](const ItemData
*item
) {
405 return item
->item
.time(KFileItem::AccessTime
);
408 case DeletionTimeRole
:
409 m_groups
= timeRoleGroups([](const ItemData
*item
) {
410 return item
->values
.value("deletiontime").toDateTime();
413 case PermissionsRole
:
414 m_groups
= permissionRoleGroups();
417 m_groups
= ratingRoleGroups();
420 m_groups
= genericStringRoleGroups(sortRole());
424 #ifdef KFILEITEMMODEL_DEBUG
425 qCDebug(DolphinDebug
) << "[TIME] Calculating groups for" << count() << "items:" << timer
.elapsed();
432 KFileItem
KFileItemModel::fileItem(int index
) const
434 if (index
>= 0 && index
< count()) {
435 return m_itemData
.at(index
)->item
;
441 KFileItem
KFileItemModel::fileItem(const QUrl
&url
) const
443 const int indexForUrl
= index(url
);
444 if (indexForUrl
>= 0) {
445 return m_itemData
.at(indexForUrl
)->item
;
450 int KFileItemModel::index(const KFileItem
&item
) const
452 return index(item
.url());
455 int KFileItemModel::index(const QUrl
&url
) const
457 const QUrl urlToFind
= url
.adjusted(QUrl::StripTrailingSlash
);
459 const int itemCount
= m_itemData
.count();
460 int itemsInHash
= m_items
.count();
462 int index
= m_items
.value(urlToFind
, -1);
463 while (index
< 0 && itemsInHash
< itemCount
) {
464 // Not all URLs are stored yet in m_items. We grow m_items until either
465 // urlToFind is found, or all URLs have been stored in m_items.
466 // Note that we do not add the URLs to m_items one by one, but in
467 // larger blocks. After each block, we check if urlToFind is in
468 // m_items. We could in principle compare urlToFind with each URL while
469 // we are going through m_itemData, but comparing two QUrls will,
470 // unlike calling qHash for the URLs, trigger a parsing of the URLs
471 // which costs both CPU cycles and memory.
472 const int blockSize
= 1000;
473 const int currentBlockEnd
= qMin(itemsInHash
+ blockSize
, itemCount
);
474 for (int i
= itemsInHash
; i
< currentBlockEnd
; ++i
) {
475 const QUrl nextUrl
= m_itemData
.at(i
)->item
.url();
476 m_items
.insert(nextUrl
, i
);
479 itemsInHash
= currentBlockEnd
;
480 index
= m_items
.value(urlToFind
, -1);
484 // The item could not be found, even though all items from m_itemData
485 // should be in m_items now. We print some diagnostic information which
486 // might help to find the cause of the problem, but only once. This
487 // prevents that obtaining and printing the debugging information
488 // wastes CPU cycles and floods the shell or .xsession-errors.
489 static bool printDebugInfo
= true;
491 if (m_items
.count() != m_itemData
.count() && printDebugInfo
) {
492 printDebugInfo
= false;
494 qCWarning(DolphinDebug
) << "The model is in an inconsistent state.";
495 qCWarning(DolphinDebug
) << "m_items.count() ==" << m_items
.count();
496 qCWarning(DolphinDebug
) << "m_itemData.count() ==" << m_itemData
.count();
498 // Check if there are multiple items with the same URL.
499 QMultiHash
<QUrl
, int> indexesForUrl
;
500 for (int i
= 0; i
< m_itemData
.count(); ++i
) {
501 indexesForUrl
.insert(m_itemData
.at(i
)->item
.url(), i
);
504 const auto uniqueKeys
= indexesForUrl
.uniqueKeys();
505 for (const QUrl
&url
: uniqueKeys
) {
506 if (indexesForUrl
.count(url
) > 1) {
507 qCWarning(DolphinDebug
) << "Multiple items found with the URL" << url
;
509 auto it
= indexesForUrl
.find(url
);
510 while (it
!= indexesForUrl
.end() && it
.key() == url
) {
511 const ItemData
*data
= m_itemData
.at(it
.value());
512 qCWarning(DolphinDebug
) << "index" << it
.value() << ":" << data
->item
;
514 qCWarning(DolphinDebug
) << "parent" << data
->parent
->item
;
526 KFileItem
KFileItemModel::rootItem() const
528 return m_dirLister
->rootItem();
531 void KFileItemModel::clear()
536 void KFileItemModel::setRoles(const QSet
<QByteArray
> &roles
)
538 if (m_roles
== roles
) {
542 const QSet
<QByteArray
> changedRoles
= (roles
- m_roles
) + (m_roles
- roles
);
546 const bool supportedExpanding
= m_requestRole
[ExpandedParentsCountRole
];
547 const bool willSupportExpanding
= roles
.contains("expandedParentsCount");
548 if (supportedExpanding
&& !willSupportExpanding
) {
549 // No expanding is supported anymore. Take care to delete all items that have an expansion level
550 // that is not 0 (and hence are part of an expanded item).
551 removeExpandedItems();
558 QSetIterator
<QByteArray
> it(roles
);
559 while (it
.hasNext()) {
560 const QByteArray
&role
= it
.next();
561 m_requestRole
[typeForRole(role
)] = true;
565 // Update m_data with the changed requested roles
566 const int maxIndex
= count() - 1;
567 for (int i
= 0; i
<= maxIndex
; ++i
) {
568 m_itemData
[i
]->values
= retrieveData(m_itemData
.at(i
)->item
, m_itemData
.at(i
)->parent
);
571 Q_EMIT
itemsChanged(KItemRangeList() << KItemRange(0, count()), changedRoles
);
574 // Clear the 'values' of all filtered items. They will be re-populated with the
575 // correct roles the next time 'values' will be accessed via data(int).
576 QHash
<KFileItem
, ItemData
*>::iterator filteredIt
= m_filteredItems
.begin();
577 const QHash
<KFileItem
, ItemData
*>::iterator filteredEnd
= m_filteredItems
.end();
578 while (filteredIt
!= filteredEnd
) {
579 (*filteredIt
)->values
.clear();
584 QSet
<QByteArray
> KFileItemModel::roles() const
589 bool KFileItemModel::setExpanded(int index
, bool expanded
)
591 if (!isExpandable(index
) || isExpanded(index
) == expanded
) {
595 QHash
<QByteArray
, QVariant
> values
;
596 values
.insert(sharedValue("isExpanded"), expanded
);
597 if (!setData(index
, values
)) {
601 const KFileItem item
= m_itemData
.at(index
)->item
;
602 const QUrl url
= item
.url();
603 const QUrl targetUrl
= item
.targetUrl();
605 m_expandedDirs
.insert(targetUrl
, url
);
606 m_dirLister
->openUrl(url
, KDirLister::Keep
);
608 const QVariantList previouslyExpandedChildren
= m_itemData
.at(index
)->values
.value("previouslyExpandedChildren").value
<QVariantList
>();
609 for (const QVariant
&var
: previouslyExpandedChildren
) {
610 m_urlsToExpand
.insert(var
.toUrl());
613 // Note that there might be (indirect) children of the folder which is to be collapsed in
614 // m_pendingItemsToInsert. To prevent that they will be inserted into the model later,
615 // possibly without a parent, which might result in a crash, we insert all pending items
616 // right now. All new items which would be without a parent will then be removed.
617 dispatchPendingItemsToInsert();
619 // Check if the index of the collapsed folder has changed. If that is the case, then items
620 // were inserted before the collapsed folder, and its index needs to be updated.
621 if (m_itemData
.at(index
)->item
!= item
) {
622 index
= this->index(item
);
625 m_expandedDirs
.remove(targetUrl
);
626 m_dirLister
->stop(url
);
627 #if KIO_VERSION >= QT_VERSION_CHECK(5, 92, 0)
628 m_dirLister
->forgetDirs(url
);
631 const int parentLevel
= expandedParentsCount(index
);
632 const int itemCount
= m_itemData
.count();
633 const int firstChildIndex
= index
+ 1;
635 QVariantList expandedChildren
;
637 int childIndex
= firstChildIndex
;
638 while (childIndex
< itemCount
&& expandedParentsCount(childIndex
) > parentLevel
) {
639 ItemData
*itemData
= m_itemData
.at(childIndex
);
640 if (itemData
->values
.value("isExpanded").toBool()) {
641 const QUrl targetUrl
= itemData
->item
.targetUrl();
642 const QUrl url
= itemData
->item
.url();
643 m_expandedDirs
.remove(targetUrl
);
644 m_dirLister
->stop(url
); // TODO: try to unit-test this, see https://bugs.kde.org/show_bug.cgi?id=332102#c11
645 #if KIO_VERSION >= QT_VERSION_CHECK(5, 92, 0)
646 m_dirLister
->forgetDirs(url
);
648 expandedChildren
.append(targetUrl
);
652 const int childrenCount
= childIndex
- firstChildIndex
;
654 removeFilteredChildren(KItemRangeList() << KItemRange(index
, 1 + childrenCount
));
655 removeItems(KItemRangeList() << KItemRange(firstChildIndex
, childrenCount
), DeleteItemData
);
657 m_itemData
.at(index
)->values
.insert("previouslyExpandedChildren", expandedChildren
);
663 bool KFileItemModel::isExpanded(int index
) const
665 if (index
>= 0 && index
< count()) {
666 return m_itemData
.at(index
)->values
.value("isExpanded").toBool();
671 bool KFileItemModel::isExpandable(int index
) const
673 if (index
>= 0 && index
< count()) {
674 // Call data (instead of accessing m_itemData directly)
675 // to ensure that the value is initialized.
676 return data(index
).value("isExpandable").toBool();
681 int KFileItemModel::expandedParentsCount(int index
) const
683 if (index
>= 0 && index
< count()) {
684 return expandedParentsCount(m_itemData
.at(index
));
689 QSet
<QUrl
> KFileItemModel::expandedDirectories() const
692 const auto dirs
= m_expandedDirs
;
693 for (const auto &dir
: dirs
) {
699 void KFileItemModel::restoreExpandedDirectories(const QSet
<QUrl
> &urls
)
701 m_urlsToExpand
= urls
;
704 void KFileItemModel::expandParentDirectories(const QUrl
&url
)
706 // Assure that each sub-path of the URL that should be
707 // expanded is added to m_urlsToExpand. KDirLister
708 // does not care whether the parent-URL has already been
710 QUrl urlToExpand
= m_dirLister
->url();
711 const int pos
= urlToExpand
.path().length();
713 // first subdir can be empty, if m_dirLister->url().path() does not end with '/'
714 // this happens if baseUrl is not root but a home directory, see FoldersPanel,
715 // so using QString::SkipEmptyParts
716 const QStringList subDirs
= url
.path().mid(pos
).split(QDir::separator(), Qt::SkipEmptyParts
);
717 for (int i
= 0; i
< subDirs
.count() - 1; ++i
) {
718 QString path
= urlToExpand
.path();
719 if (!path
.endsWith(QLatin1Char('/'))) {
720 path
.append(QLatin1Char('/'));
722 urlToExpand
.setPath(path
+ subDirs
.at(i
));
723 m_urlsToExpand
.insert(urlToExpand
);
726 // KDirLister::open() must called at least once to trigger an initial
727 // loading. The pending URLs that must be restored are handled
728 // in slotCompleted().
729 QSetIterator
<QUrl
> it2(m_urlsToExpand
);
730 while (it2
.hasNext()) {
731 const int idx
= index(it2
.next());
732 if (idx
>= 0 && !isExpanded(idx
)) {
733 setExpanded(idx
, true);
739 void KFileItemModel::setNameFilter(const QString
&nameFilter
)
741 if (m_filter
.pattern() != nameFilter
) {
742 dispatchPendingItemsToInsert();
743 m_filter
.setPattern(nameFilter
);
748 QString
KFileItemModel::nameFilter() const
750 return m_filter
.pattern();
753 void KFileItemModel::setMimeTypeFilters(const QStringList
&filters
)
755 if (m_filter
.mimeTypes() != filters
) {
756 dispatchPendingItemsToInsert();
757 m_filter
.setMimeTypes(filters
);
762 QStringList
KFileItemModel::mimeTypeFilters() const
764 return m_filter
.mimeTypes();
767 void KFileItemModel::setExcludeMimeTypeFilter(const QStringList
&filters
)
769 if (m_filter
.excludeMimeTypes() != filters
) {
770 dispatchPendingItemsToInsert();
771 m_filter
.setExcludeMimeTypes(filters
);
776 QStringList
KFileItemModel::excludeMimeTypeFilter() const
778 return m_filter
.excludeMimeTypes();
781 void KFileItemModel::applyFilters()
784 // Check which previously shown items from m_itemData must now get
785 // hidden and hence moved from m_itemData into m_filteredItems.
787 QList
<int> newFilteredIndexes
; // This structure is good for prepending. We will want an ascending sorted Container at the end, this will do fine.
789 // This pointer will refer to the next confirmed shown item from the point of
790 // view of the current "itemData" in the upcoming "for" loop.
791 ItemData
*itemShownBelow
= nullptr;
793 // We will iterate backwards because it's convenient to know beforehand if the item just below is its child or not.
794 for (int index
= m_itemData
.count() - 1; index
>= 0; --index
) {
795 ItemData
*itemData
= m_itemData
.at(index
);
797 if (m_filter
.matches(itemData
->item
) || (itemShownBelow
&& itemShownBelow
->parent
== itemData
)) {
798 // We could've entered here for two reasons:
799 // 1. This item passes the filter itself
800 // 2. This is an expanded folder that doesn't pass the filter but sees a filter-passing child just below
802 // So this item must remain shown.
803 // Lets register this item as the next shown item from the point of view of the next iteration of this for loop
804 itemShownBelow
= itemData
;
806 // We hide this item for now, however, for expanded folders this is not final:
807 // if after the next "for" loop we discover that its children must now be shown with the newly applied fliter, we shall re-insert it
808 newFilteredIndexes
.prepend(index
);
809 m_filteredItems
.insert(itemData
->item
, itemData
);
810 // indexShownBelow doesn't get updated since this item will be hidden
814 // This will remove the newly filtered items from m_itemData
815 removeItems(KItemRangeList::fromSortedContainer(newFilteredIndexes
), KeepItemData
);
818 // Check which hidden items from m_filteredItems should
819 // become visible again and hence moved from m_filteredItems back into m_itemData.
821 QList
<ItemData
*> newVisibleItems
;
823 QHash
<KFileItem
, ItemData
*> ancestorsOfNewVisibleItems
; // We will make sure these also become visible in step 3.
825 QHash
<KFileItem
, ItemData
*>::iterator it
= m_filteredItems
.begin();
826 while (it
!= m_filteredItems
.end()) {
827 if (m_filter
.matches(it
.key())) {
828 newVisibleItems
.append(it
.value());
830 // If this is a child of an expanded folder, we must make sure that its whole parental chain will also be shown.
831 // We will go up through its parental chain until we either:
832 // 1 - reach the "root item" of the current view, i.e the currently opened folder on Dolphin. Their children have their ItemData::parent set to
833 // nullptr. or 2 - we reach an unfiltered parent or a previously discovered ancestor.
834 for (ItemData
*parent
= it
.value()->parent
; parent
&& !ancestorsOfNewVisibleItems
.contains(parent
->item
) && m_filteredItems
.contains(parent
->item
);
835 parent
= parent
->parent
) {
836 // We wish we could remove this parent from m_filteredItems right now, but we are iterating over it
837 // and it would mess up the iteration. We will mark it to be removed in step 3.
838 ancestorsOfNewVisibleItems
.insert(parent
->item
, parent
);
841 it
= m_filteredItems
.erase(it
);
843 // Item remains filtered for now
844 // However, for expanded folders this is not final, we may discover later that it has unfiltered descendants.
850 // Handles the ancestorsOfNewVisibleItems.
851 // Now that we are done iterating through m_filteredItems we can safely move the ancestorsOfNewVisibleItems from m_filteredItems to newVisibleItems.
852 for (it
= ancestorsOfNewVisibleItems
.begin(); it
!= ancestorsOfNewVisibleItems
.end(); it
++) {
853 if (m_filteredItems
.remove(it
.key())) {
854 // m_filteredItems still contained this ancestor until now so we can be sure that we aren't adding a duplicate ancestor to newVisibleItems.
855 newVisibleItems
.append(it
.value());
859 // This will insert the newly discovered unfiltered items into m_itemData
860 insertItems(newVisibleItems
);
863 void KFileItemModel::removeFilteredChildren(const KItemRangeList
&itemRanges
)
865 if (m_filteredItems
.isEmpty() || !m_requestRole
[ExpandedParentsCountRole
]) {
866 // There are either no filtered items, or it is not possible to expand
867 // folders -> there cannot be any filtered children.
871 QSet
<ItemData
*> parents
;
872 for (const KItemRange
&range
: itemRanges
) {
873 for (int index
= range
.index
; index
< range
.index
+ range
.count
; ++index
) {
874 parents
.insert(m_itemData
.at(index
));
878 QHash
<KFileItem
, ItemData
*>::iterator it
= m_filteredItems
.begin();
879 while (it
!= m_filteredItems
.end()) {
880 if (parents
.contains(it
.value()->parent
)) {
882 it
= m_filteredItems
.erase(it
);
889 QList
<KFileItemModel::RoleInfo
> KFileItemModel::rolesInformation()
891 static QList
<RoleInfo
> rolesInfo
;
892 if (rolesInfo
.isEmpty()) {
894 const RoleInfoMap
*map
= rolesInfoMap(count
);
895 for (int i
= 0; i
< count
; ++i
) {
896 if (map
[i
].roleType
!= NoRole
) {
898 info
.role
= map
[i
].role
;
899 info
.translation
= map
[i
].roleTranslation
.toString();
900 if (!map
[i
].groupTranslation
.isEmpty()) {
901 info
.group
= map
[i
].groupTranslation
.toString();
903 // For top level roles, groupTranslation is 0. We must make sure that
904 // info.group is an empty string then because the code that generates
905 // menus tries to put the actions into sub menus otherwise.
906 info
.group
= QString();
908 info
.requiresBaloo
= map
[i
].requiresBaloo
;
909 info
.requiresIndexer
= map
[i
].requiresIndexer
;
910 if (!map
[i
].tooltipTranslation
.isEmpty()) {
911 info
.tooltip
= map
[i
].tooltipTranslation
.toString();
913 info
.tooltip
= QString();
915 rolesInfo
.append(info
);
923 void KFileItemModel::onGroupedSortingChanged(bool current
)
929 void KFileItemModel::onSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
, bool resortItems
)
932 m_sortRole
= typeForRole(current
);
934 if (!m_requestRole
[m_sortRole
]) {
935 QSet
<QByteArray
> newRoles
= m_roles
;
945 void KFileItemModel::onSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
)
952 void KFileItemModel::loadSortingSettings()
954 using Choice
= GeneralSettings::EnumSortingChoice
;
955 switch (GeneralSettings::sortingChoice()) {
956 case Choice::NaturalSorting
:
957 m_naturalSorting
= true;
958 m_collator
.setCaseSensitivity(Qt::CaseInsensitive
);
960 case Choice::CaseSensitiveSorting
:
961 m_naturalSorting
= false;
962 m_collator
.setCaseSensitivity(Qt::CaseSensitive
);
964 case Choice::CaseInsensitiveSorting
:
965 m_naturalSorting
= false;
966 m_collator
.setCaseSensitivity(Qt::CaseInsensitive
);
971 // Workaround for bug https://bugreports.qt.io/browse/QTBUG-69361
972 // Force the clean state of QCollator in single thread to avoid thread safety problems in sort
973 m_collator
.compare(QString(), QString());
976 void KFileItemModel::resortAllItems()
978 m_resortAllItemsTimer
->stop();
980 const int itemCount
= count();
981 if (itemCount
<= 0) {
985 #ifdef KFILEITEMMODEL_DEBUG
988 qCDebug(DolphinDebug
) << "===========================================================";
989 qCDebug(DolphinDebug
) << "Resorting" << itemCount
<< "items";
992 // Remember the order of the current URLs so
993 // that it can be determined which indexes have
994 // been moved because of the resorting.
996 oldUrls
.reserve(itemCount
);
997 for (const ItemData
*itemData
: qAsConst(m_itemData
)) {
998 oldUrls
.append(itemData
->item
.url());
1002 m_items
.reserve(itemCount
);
1005 sort(m_itemData
.begin(), m_itemData
.end());
1006 for (int i
= 0; i
< itemCount
; ++i
) {
1007 m_items
.insert(m_itemData
.at(i
)->item
.url(), i
);
1010 // Determine the first index that has been moved.
1011 int firstMovedIndex
= 0;
1012 while (firstMovedIndex
< itemCount
&& firstMovedIndex
== m_items
.value(oldUrls
.at(firstMovedIndex
))) {
1016 const bool itemsHaveMoved
= firstMovedIndex
< itemCount
;
1017 if (itemsHaveMoved
) {
1020 int lastMovedIndex
= itemCount
- 1;
1021 while (lastMovedIndex
> firstMovedIndex
&& lastMovedIndex
== m_items
.value(oldUrls
.at(lastMovedIndex
))) {
1025 Q_ASSERT(firstMovedIndex
<= lastMovedIndex
);
1027 // Create a list movedToIndexes, which has the property that
1028 // movedToIndexes[i] is the new index of the item with the old index
1029 // firstMovedIndex + i.
1030 const int movedItemsCount
= lastMovedIndex
- firstMovedIndex
+ 1;
1031 QList
<int> movedToIndexes
;
1032 movedToIndexes
.reserve(movedItemsCount
);
1033 for (int i
= firstMovedIndex
; i
<= lastMovedIndex
; ++i
) {
1034 const int newIndex
= m_items
.value(oldUrls
.at(i
));
1035 movedToIndexes
.append(newIndex
);
1038 Q_EMIT
itemsMoved(KItemRange(firstMovedIndex
, movedItemsCount
), movedToIndexes
);
1039 } else if (groupedSorting()) {
1040 // The groups might have changed even if the order of the items has not.
1041 const QList
<QPair
<int, QVariant
>> oldGroups
= m_groups
;
1043 if (groups() != oldGroups
) {
1044 Q_EMIT
groupsChanged();
1048 #ifdef KFILEITEMMODEL_DEBUG
1049 qCDebug(DolphinDebug
) << "[TIME] Resorting of" << itemCount
<< "items:" << timer
.elapsed();
1053 void KFileItemModel::slotCompleted()
1055 m_maximumUpdateIntervalTimer
->stop();
1056 dispatchPendingItemsToInsert();
1058 if (!m_urlsToExpand
.isEmpty()) {
1059 // Try to find a URL that can be expanded.
1060 // Note that the parent folder must be expanded before any of its subfolders become visible.
1061 // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
1062 // -> we expand the first visible URL we find in m_restoredExpandedUrls.
1063 // Iterate over a const copy because items are deleted and inserted within the loop
1064 const auto urlsToExpand
= m_urlsToExpand
;
1065 for (const QUrl
&url
: urlsToExpand
) {
1066 const int indexForUrl
= index(url
);
1067 if (indexForUrl
>= 0) {
1068 m_urlsToExpand
.remove(url
);
1069 if (setExpanded(indexForUrl
, true)) {
1070 // The dir lister has been triggered. This slot will be called
1071 // again after the directory has been expanded.
1077 // None of the URLs in m_restoredExpandedUrls could be found in the model. This can happen
1078 // if these URLs have been deleted in the meantime.
1079 m_urlsToExpand
.clear();
1082 Q_EMIT
directoryLoadingCompleted();
1085 void KFileItemModel::slotCanceled()
1087 m_maximumUpdateIntervalTimer
->stop();
1088 dispatchPendingItemsToInsert();
1090 Q_EMIT
directoryLoadingCanceled();
1093 void KFileItemModel::slotItemsAdded(const QUrl
&directoryUrl
, const KFileItemList
&items
)
1095 Q_ASSERT(!items
.isEmpty());
1097 const QUrl parentUrl
= m_expandedDirs
.value(directoryUrl
, directoryUrl
.adjusted(QUrl::StripTrailingSlash
));
1099 if (m_requestRole
[ExpandedParentsCountRole
]) {
1100 // If the expanding of items is enabled, the call
1101 // dirLister->openUrl(url, KDirLister::Keep) in KFileItemModel::setExpanded()
1102 // might result in emitting the same items twice due to the Keep-parameter.
1103 // This case happens if an item gets expanded, collapsed and expanded again
1104 // before the items could be loaded for the first expansion.
1105 if (index(items
.first().url()) >= 0) {
1106 // The items are already part of the model.
1110 if (directoryUrl
!= directory()) {
1111 // To be able to compare whether the new items may be inserted as children
1112 // of a parent item the pending items must be added to the model first.
1113 dispatchPendingItemsToInsert();
1116 // KDirLister keeps the children of items that got expanded once even if
1117 // they got collapsed again with KFileItemModel::setExpanded(false). So it must be
1118 // checked whether the parent for new items is still expanded.
1119 const int parentIndex
= index(parentUrl
);
1120 if (parentIndex
>= 0 && !m_itemData
[parentIndex
]->values
.value("isExpanded").toBool()) {
1121 // The parent is not expanded.
1126 const QList
<ItemData
*> itemDataList
= createItemDataList(parentUrl
, items
);
1128 if (!m_filter
.hasSetFilters()) {
1129 m_pendingItemsToInsert
.append(itemDataList
);
1131 QSet
<ItemData
*> parentsToEnsureVisible
;
1133 // The name or type filter is active. Hide filtered items
1134 // before inserting them into the model and remember
1135 // the filtered items in m_filteredItems.
1136 for (ItemData
*itemData
: itemDataList
) {
1137 if (m_filter
.matches(itemData
->item
)) {
1138 m_pendingItemsToInsert
.append(itemData
);
1139 if (itemData
->parent
) {
1140 parentsToEnsureVisible
.insert(itemData
->parent
);
1143 m_filteredItems
.insert(itemData
->item
, itemData
);
1147 // Entire parental chains must be shown
1148 for (ItemData
*parent
: parentsToEnsureVisible
) {
1149 for (; parent
&& m_filteredItems
.remove(parent
->item
); parent
= parent
->parent
) {
1150 m_pendingItemsToInsert
.append(parent
);
1155 if (!m_maximumUpdateIntervalTimer
->isActive()) {
1156 // Assure that items get dispatched if no completed() or canceled() signal is
1157 // emitted during the maximum update interval.
1158 m_maximumUpdateIntervalTimer
->start();
1161 Q_EMIT
fileItemsChanged({KFileItem(directoryUrl
)});
1164 int KFileItemModel::filterChildlessParents(KItemRangeList
&removedItemRanges
, const QSet
<ItemData
*> &parentsToEnsureVisible
)
1166 int filteredParentsCount
= 0;
1167 // The childless parents not yet removed will always be right above the start of a removed range.
1168 // We iterate backwards to ensure the deepest folders are processed before their parents
1169 for (int i
= removedItemRanges
.size() - 1; i
>= 0; i
--) {
1170 KItemRange itemRange
= removedItemRanges
.at(i
);
1171 const ItemData
*const firstInRange
= m_itemData
.at(itemRange
.index
);
1172 ItemData
*itemAbove
= itemRange
.index
- 1 >= 0 ? m_itemData
.at(itemRange
.index
- 1) : nullptr;
1173 const ItemData
*const itemBelow
= itemRange
.index
+ itemRange
.count
< m_itemData
.count() ? m_itemData
.at(itemRange
.index
+ itemRange
.count
) : nullptr;
1175 if (itemAbove
&& firstInRange
->parent
== itemAbove
&& !m_filter
.matches(itemAbove
->item
) && (!itemBelow
|| itemBelow
->parent
!= itemAbove
)
1176 && !parentsToEnsureVisible
.contains(itemAbove
)) {
1177 // The item above exists, is the parent, doesn't pass the filter, does not belong to parentsToEnsureVisible
1178 // and this deleted range covers all of its descendents, so none will be left.
1179 m_filteredItems
.insert(itemAbove
->item
, itemAbove
);
1180 // This range's starting index will be extended to include the parent above:
1183 ++filteredParentsCount
;
1184 KItemRange previousRange
= i
> 0 ? removedItemRanges
.at(i
- 1) : KItemRange();
1185 // We must check if this caused the range to touch the previous range, if that's the case they shall be merged
1186 if (i
> 0 && previousRange
.index
+ previousRange
.count
== itemRange
.index
) {
1187 previousRange
.count
+= itemRange
.count
;
1188 removedItemRanges
.replace(i
- 1, previousRange
);
1189 removedItemRanges
.removeAt(i
);
1191 removedItemRanges
.replace(i
, itemRange
);
1192 // We must revisit this range in the next iteration since its starting index changed
1197 return filteredParentsCount
;
1200 void KFileItemModel::slotItemsDeleted(const KFileItemList
&items
)
1202 dispatchPendingItemsToInsert();
1204 QVector
<int> indexesToRemove
;
1205 indexesToRemove
.reserve(items
.count());
1206 KFileItemList dirsChanged
;
1208 const auto currentDir
= directory();
1210 for (const KFileItem
&item
: items
) {
1211 if (item
.url() == currentDir
) {
1212 Q_EMIT
currentDirectoryRemoved();
1216 const int indexForItem
= index(item
);
1217 if (indexForItem
>= 0) {
1218 indexesToRemove
.append(indexForItem
);
1220 // Probably the item has been filtered.
1221 QHash
<KFileItem
, ItemData
*>::iterator it
= m_filteredItems
.find(item
);
1222 if (it
!= m_filteredItems
.end()) {
1224 m_filteredItems
.erase(it
);
1228 QUrl parentUrl
= item
.url().adjusted(QUrl::RemoveFilename
| QUrl::StripTrailingSlash
);
1229 if (dirsChanged
.findByUrl(parentUrl
).isNull()) {
1230 dirsChanged
<< KFileItem(parentUrl
);
1234 std::sort(indexesToRemove
.begin(), indexesToRemove
.end());
1236 if (m_requestRole
[ExpandedParentsCountRole
] && !m_expandedDirs
.isEmpty()) {
1237 // Assure that removing a parent item also results in removing all children
1238 QVector
<int> indexesToRemoveWithChildren
;
1239 indexesToRemoveWithChildren
.reserve(m_itemData
.count());
1241 const int itemCount
= m_itemData
.count();
1242 for (int index
: qAsConst(indexesToRemove
)) {
1243 indexesToRemoveWithChildren
.append(index
);
1245 const int parentLevel
= expandedParentsCount(index
);
1246 int childIndex
= index
+ 1;
1247 while (childIndex
< itemCount
&& expandedParentsCount(childIndex
) > parentLevel
) {
1248 indexesToRemoveWithChildren
.append(childIndex
);
1253 indexesToRemove
= indexesToRemoveWithChildren
;
1256 KItemRangeList itemRanges
= KItemRangeList::fromSortedContainer(indexesToRemove
);
1257 removeFilteredChildren(itemRanges
);
1259 // This call will update itemRanges to include the childless parents that have been filtered.
1260 const int filteredParentsCount
= filterChildlessParents(itemRanges
);
1262 // If any childless parents were filtered, then itemRanges got updated and now contains items that were really deleted
1263 // mixed with expanded folders that are just being filtered out.
1264 // If that's the case, we pass 'DeleteItemDataIfUnfiltered' as a hint
1265 // so removeItems() will check m_filteredItems to differentiate which is which.
1266 removeItems(itemRanges
, filteredParentsCount
> 0 ? DeleteItemDataIfUnfiltered
: DeleteItemData
);
1268 Q_EMIT
fileItemsChanged(dirsChanged
);
1271 void KFileItemModel::slotRefreshItems(const QList
<QPair
<KFileItem
, KFileItem
>> &items
)
1273 Q_ASSERT(!items
.isEmpty());
1274 #ifdef KFILEITEMMODEL_DEBUG
1275 qCDebug(DolphinDebug
) << "Refreshing" << items
.count() << "items";
1278 // Get the indexes of all items that have been refreshed
1280 indexes
.reserve(items
.count());
1282 QSet
<QByteArray
> changedRoles
;
1283 KFileItemList changedFiles
;
1285 // Contains the indexes of the currently visible items
1286 // that should get hidden and hence moved to m_filteredItems.
1287 QVector
<int> newFilteredIndexes
;
1289 // Contains currently hidden items that should
1290 // get visible and hence removed from m_filteredItems
1291 QList
<ItemData
*> newVisibleItems
;
1293 QListIterator
<QPair
<KFileItem
, KFileItem
>> it(items
);
1295 while (it
.hasNext()) {
1296 const QPair
<KFileItem
, KFileItem
> &itemPair
= it
.next();
1297 const KFileItem
&oldItem
= itemPair
.first
;
1298 const KFileItem
&newItem
= itemPair
.second
;
1299 const int indexForItem
= index(oldItem
);
1300 const bool newItemMatchesFilter
= m_filter
.matches(newItem
);
1301 if (indexForItem
>= 0) {
1302 m_itemData
[indexForItem
]->item
= newItem
;
1304 // Keep old values as long as possible if they could not retrieved synchronously yet.
1305 // The update of the values will be done asynchronously by KFileItemModelRolesUpdater.
1306 ItemData
*const itemData
= m_itemData
.at(indexForItem
);
1307 QHashIterator
<QByteArray
, QVariant
> it(retrieveData(newItem
, itemData
->parent
));
1308 while (it
.hasNext()) {
1310 const QByteArray
&role
= it
.key();
1311 if (itemData
->values
.value(role
) != it
.value()) {
1312 itemData
->values
.insert(role
, it
.value());
1313 changedRoles
.insert(role
);
1317 m_items
.remove(oldItem
.url());
1318 // We must maintain m_items consistent with m_itemData for now, this very loop is using it.
1319 // We leave it to be cleared by removeItems() later, when m_itemData actually gets updated.
1320 m_items
.insert(newItem
.url(), indexForItem
);
1321 if (newItemMatchesFilter
1322 || (itemData
->values
.value("isExpanded").toBool()
1323 && (indexForItem
+ 1 < m_itemData
.count() && m_itemData
.at(indexForItem
+ 1)->parent
== itemData
))) {
1324 // We are lenient with expanded folders that originally had visible children.
1325 // If they become childless now they will be caught by filterChildlessParents()
1326 changedFiles
.append(newItem
);
1327 indexes
.append(indexForItem
);
1329 newFilteredIndexes
.append(indexForItem
);
1330 m_filteredItems
.insert(newItem
, itemData
);
1333 // Check if 'oldItem' is one of the filtered items.
1334 QHash
<KFileItem
, ItemData
*>::iterator it
= m_filteredItems
.find(oldItem
);
1335 if (it
!= m_filteredItems
.end()) {
1336 ItemData
*const itemData
= it
.value();
1337 itemData
->item
= newItem
;
1339 // The data stored in 'values' might have changed. Therefore, we clear
1340 // 'values' and re-populate it the next time it is requested via data(int).
1341 // Before clearing, we must remember if it was expanded and the expanded parents count,
1342 // otherwise these states would be lost. The data() method will deal with this special case.
1343 const bool isExpanded
= itemData
->values
.value("isExpanded").toBool();
1344 bool hasExpandedParentsCount
= false;
1345 const int expandedParentsCount
= itemData
->values
.value("expandedParentsCount").toInt(&hasExpandedParentsCount
);
1346 itemData
->values
.clear();
1348 itemData
->values
.insert("isExpanded", true);
1349 if (hasExpandedParentsCount
) {
1350 itemData
->values
.insert("expandedParentsCount", expandedParentsCount
);
1354 m_filteredItems
.erase(it
);
1355 if (newItemMatchesFilter
) {
1356 newVisibleItems
.append(itemData
);
1358 m_filteredItems
.insert(newItem
, itemData
);
1364 std::sort(newFilteredIndexes
.begin(), newFilteredIndexes
.end());
1366 // We must keep track of parents of new visible items since they must be shown no matter what
1367 // They will be considered "immune" to filterChildlessParents()
1368 QSet
<ItemData
*> parentsToEnsureVisible
;
1370 for (ItemData
*item
: newVisibleItems
) {
1371 for (ItemData
*parent
= item
->parent
; parent
&& !parentsToEnsureVisible
.contains(parent
); parent
= parent
->parent
) {
1372 parentsToEnsureVisible
.insert(parent
);
1375 for (ItemData
*parent
: parentsToEnsureVisible
) {
1376 // We make sure they are all unfiltered.
1377 if (m_filteredItems
.remove(parent
->item
)) {
1378 // If it is being unfiltered now, we mark it to be inserted by appending it to newVisibleItems
1379 newVisibleItems
.append(parent
);
1380 // It could be in newFilteredIndexes, we must remove it if it's there:
1381 const int parentIndex
= index(parent
->item
);
1382 if (parentIndex
>= 0) {
1383 QVector
<int>::iterator it
= std::lower_bound(newFilteredIndexes
.begin(), newFilteredIndexes
.end(), parentIndex
);
1384 if (it
!= newFilteredIndexes
.end() && *it
== parentIndex
) {
1385 newFilteredIndexes
.erase(it
);
1391 KItemRangeList removedRanges
= KItemRangeList::fromSortedContainer(newFilteredIndexes
);
1393 // This call will update itemRanges to include the childless parents that have been filtered.
1394 filterChildlessParents(removedRanges
, parentsToEnsureVisible
);
1396 removeItems(removedRanges
, KeepItemData
);
1398 // Show previously hidden items that should get visible
1399 insertItems(newVisibleItems
);
1401 // Final step: we will emit 'itemsChanged' and 'fileItemsChanged' signals and trigger the asynchronous re-sorting logic.
1403 // If the changed items have been created recently, they might not be in m_items yet.
1404 // In that case, the list 'indexes' might be empty.
1405 if (indexes
.isEmpty()) {
1409 if (newVisibleItems
.count() > 0 || removedRanges
.count() > 0) {
1410 // The original indexes have changed and are now worthless since items were removed and/or inserted.
1412 // m_items is not yet rebuilt at this point, so we use our own means to resolve the new indexes.
1413 const QSet
<const KFileItem
> changedFilesSet(changedFiles
.cbegin(), changedFiles
.cend());
1414 for (int i
= 0; i
< m_itemData
.count(); i
++) {
1415 if (changedFilesSet
.contains(m_itemData
.at(i
)->item
)) {
1420 std::sort(indexes
.begin(), indexes
.end());
1423 // Extract the item-ranges out of the changed indexes
1424 const KItemRangeList itemRangeList
= KItemRangeList::fromSortedContainer(indexes
);
1425 emitItemsChangedAndTriggerResorting(itemRangeList
, changedRoles
);
1427 Q_EMIT
fileItemsChanged(changedFiles
);
1430 void KFileItemModel::slotClear()
1432 #ifdef KFILEITEMMODEL_DEBUG
1433 qCDebug(DolphinDebug
) << "Clearing all items";
1436 qDeleteAll(m_filteredItems
);
1437 m_filteredItems
.clear();
1440 m_maximumUpdateIntervalTimer
->stop();
1441 m_resortAllItemsTimer
->stop();
1443 qDeleteAll(m_pendingItemsToInsert
);
1444 m_pendingItemsToInsert
.clear();
1446 const int removedCount
= m_itemData
.count();
1447 if (removedCount
> 0) {
1448 qDeleteAll(m_itemData
);
1451 Q_EMIT
itemsRemoved(KItemRangeList() << KItemRange(0, removedCount
));
1454 m_expandedDirs
.clear();
1457 void KFileItemModel::slotSortingChoiceChanged()
1459 loadSortingSettings();
1463 void KFileItemModel::dispatchPendingItemsToInsert()
1465 if (!m_pendingItemsToInsert
.isEmpty()) {
1466 insertItems(m_pendingItemsToInsert
);
1467 m_pendingItemsToInsert
.clear();
1471 void KFileItemModel::insertItems(QList
<ItemData
*> &newItems
)
1473 if (newItems
.isEmpty()) {
1477 #ifdef KFILEITEMMODEL_DEBUG
1478 QElapsedTimer timer
;
1480 qCDebug(DolphinDebug
) << "===========================================================";
1481 qCDebug(DolphinDebug
) << "Inserting" << newItems
.count() << "items";
1485 prepareItemsForSorting(newItems
);
1487 // Natural sorting of items can be very slow. However, it becomes much faster
1488 // if the input sequence is already mostly sorted. Therefore, we first sort
1489 // 'newItems' according to the QStrings using QString::operator<(), which is quite fast.
1490 if (m_naturalSorting
) {
1491 if (m_sortRole
== NameRole
) {
1492 parallelMergeSort(newItems
.begin(), newItems
.end(), nameLessThan
, QThread::idealThreadCount());
1493 } else if (isRoleValueNatural(m_sortRole
)) {
1494 auto lambdaLessThan
= [&](const KFileItemModel::ItemData
*a
, const KFileItemModel::ItemData
*b
) {
1495 const QByteArray role
= roleForType(m_sortRole
);
1496 return a
->values
.value(role
).toString() < b
->values
.value(role
).toString();
1498 parallelMergeSort(newItems
.begin(), newItems
.end(), lambdaLessThan
, QThread::idealThreadCount());
1502 sort(newItems
.begin(), newItems
.end());
1504 #ifdef KFILEITEMMODEL_DEBUG
1505 qCDebug(DolphinDebug
) << "[TIME] Sorting:" << timer
.elapsed();
1508 KItemRangeList itemRanges
;
1509 const int existingItemCount
= m_itemData
.count();
1510 const int newItemCount
= newItems
.count();
1511 const int totalItemCount
= existingItemCount
+ newItemCount
;
1513 if (existingItemCount
== 0) {
1514 // Optimization for the common special case that there are no
1515 // items in the model yet. Happens, e.g., when entering a folder.
1516 m_itemData
= newItems
;
1517 itemRanges
<< KItemRange(0, newItemCount
);
1519 m_itemData
.reserve(totalItemCount
);
1520 for (int i
= existingItemCount
; i
< totalItemCount
; ++i
) {
1521 m_itemData
.append(nullptr);
1524 // We build the new list m_itemData in reverse order to minimize
1525 // the number of moves and guarantee O(N) complexity.
1526 int targetIndex
= totalItemCount
- 1;
1527 int sourceIndexExistingItems
= existingItemCount
- 1;
1528 int sourceIndexNewItems
= newItemCount
- 1;
1532 while (sourceIndexNewItems
>= 0) {
1533 ItemData
*newItem
= newItems
.at(sourceIndexNewItems
);
1534 if (sourceIndexExistingItems
>= 0 && lessThan(newItem
, m_itemData
.at(sourceIndexExistingItems
), m_collator
)) {
1535 // Move an existing item to its new position. If any new items
1536 // are behind it, push the item range to itemRanges.
1537 if (rangeCount
> 0) {
1538 itemRanges
<< KItemRange(sourceIndexExistingItems
+ 1, rangeCount
);
1542 m_itemData
[targetIndex
] = m_itemData
.at(sourceIndexExistingItems
);
1543 --sourceIndexExistingItems
;
1545 // Insert a new item into the list.
1547 m_itemData
[targetIndex
] = newItem
;
1548 --sourceIndexNewItems
;
1553 // Push the final item range to itemRanges.
1554 if (rangeCount
> 0) {
1555 itemRanges
<< KItemRange(sourceIndexExistingItems
+ 1, rangeCount
);
1558 // Note that itemRanges is still sorted in reverse order.
1559 std::reverse(itemRanges
.begin(), itemRanges
.end());
1562 // The indexes in m_items are not correct anymore. Therefore, we clear m_items.
1563 // It will be re-populated with the updated indices if index(const QUrl&) is called.
1566 Q_EMIT
itemsInserted(itemRanges
);
1568 #ifdef KFILEITEMMODEL_DEBUG
1569 qCDebug(DolphinDebug
) << "[TIME] Inserting of" << newItems
.count() << "items:" << timer
.elapsed();
1573 void KFileItemModel::removeItems(const KItemRangeList
&itemRanges
, RemoveItemsBehavior behavior
)
1575 if (itemRanges
.isEmpty()) {
1581 // Step 1: Remove the items from m_itemData, and free the ItemData.
1582 int removedItemsCount
= 0;
1583 for (const KItemRange
&range
: itemRanges
) {
1584 removedItemsCount
+= range
.count
;
1586 for (int index
= range
.index
; index
< range
.index
+ range
.count
; ++index
) {
1587 if (behavior
== DeleteItemData
|| (behavior
== DeleteItemDataIfUnfiltered
&& !m_filteredItems
.contains(m_itemData
.at(index
)->item
))) {
1588 delete m_itemData
.at(index
);
1591 m_itemData
[index
] = nullptr;
1595 // Step 2: Remove the ItemData pointers from the list m_itemData.
1596 int target
= itemRanges
.at(0).index
;
1597 int source
= itemRanges
.at(0).index
+ itemRanges
.at(0).count
;
1600 const int oldItemDataCount
= m_itemData
.count();
1601 while (source
< oldItemDataCount
) {
1602 m_itemData
[target
] = m_itemData
[source
];
1606 if (nextRange
< itemRanges
.count() && source
== itemRanges
.at(nextRange
).index
) {
1607 // Skip the items in the next removed range.
1608 source
+= itemRanges
.at(nextRange
).count
;
1613 m_itemData
.erase(m_itemData
.end() - removedItemsCount
, m_itemData
.end());
1615 // The indexes in m_items are not correct anymore. Therefore, we clear m_items.
1616 // It will be re-populated with the updated indices if index(const QUrl&) is called.
1619 Q_EMIT
itemsRemoved(itemRanges
);
1622 QList
<KFileItemModel::ItemData
*> KFileItemModel::createItemDataList(const QUrl
&parentUrl
, const KFileItemList
&items
) const
1624 if (m_sortRole
== TypeRole
) {
1625 // Try to resolve the MIME-types synchronously to prevent a reordering of
1626 // the items when sorting by type (per default MIME-types are resolved
1627 // asynchronously by KFileItemModelRolesUpdater).
1628 determineMimeTypes(items
, 200);
1631 // We search for the parent in m_itemData and then in m_filteredItems if necessary
1632 const int parentIndex
= index(parentUrl
);
1633 ItemData
*parentItem
= parentIndex
< 0 ? m_filteredItems
.value(KFileItem(parentUrl
), nullptr) : m_itemData
.at(parentIndex
);
1635 QList
<ItemData
*> itemDataList
;
1636 itemDataList
.reserve(items
.count());
1638 for (const KFileItem
&item
: items
) {
1639 ItemData
*itemData
= new ItemData();
1640 itemData
->item
= item
;
1641 itemData
->parent
= parentItem
;
1642 itemDataList
.append(itemData
);
1645 return itemDataList
;
1648 void KFileItemModel::prepareItemsForSorting(QList
<ItemData
*> &itemDataList
)
1650 switch (m_sortRole
) {
1652 case PermissionsRole
:
1655 case DestinationRole
:
1657 case DeletionTimeRole
:
1658 // These roles can be determined with retrieveData, and they have to be stored
1659 // in the QHash "values" for the sorting.
1660 for (ItemData
*itemData
: qAsConst(itemDataList
)) {
1661 if (itemData
->values
.isEmpty()) {
1662 itemData
->values
= retrieveData(itemData
->item
, itemData
->parent
);
1668 // At least store the data including the file type for items with known MIME type.
1669 for (ItemData
*itemData
: qAsConst(itemDataList
)) {
1670 if (itemData
->values
.isEmpty()) {
1671 const KFileItem item
= itemData
->item
;
1672 if (item
.isDir() || item
.isMimeTypeKnown()) {
1673 itemData
->values
= retrieveData(itemData
->item
, itemData
->parent
);
1680 // The other roles are either resolved by KFileItemModelRolesUpdater
1681 // (this includes the SizeRole for directories), or they do not need
1682 // to be stored in the QHash "values" for sorting because the data can
1683 // be retrieved directly from the KFileItem (NameRole, SizeRole for files,
1689 int KFileItemModel::expandedParentsCount(const ItemData
*data
)
1691 // The hash 'values' is only guaranteed to contain the key "expandedParentsCount"
1692 // if the corresponding item is expanded, and it is not a top-level item.
1693 const ItemData
*parent
= data
->parent
;
1695 if (parent
->parent
) {
1696 Q_ASSERT(parent
->values
.contains("expandedParentsCount"));
1697 return parent
->values
.value("expandedParentsCount").toInt() + 1;
1706 void KFileItemModel::removeExpandedItems()
1708 QVector
<int> indexesToRemove
;
1710 const int maxIndex
= m_itemData
.count() - 1;
1711 for (int i
= 0; i
<= maxIndex
; ++i
) {
1712 const ItemData
*itemData
= m_itemData
.at(i
);
1713 if (itemData
->parent
) {
1714 indexesToRemove
.append(i
);
1718 removeItems(KItemRangeList::fromSortedContainer(indexesToRemove
), DeleteItemData
);
1719 m_expandedDirs
.clear();
1721 // Also remove all filtered items which have a parent.
1722 QHash
<KFileItem
, ItemData
*>::iterator it
= m_filteredItems
.begin();
1723 const QHash
<KFileItem
, ItemData
*>::iterator end
= m_filteredItems
.end();
1726 if (it
.value()->parent
) {
1728 it
= m_filteredItems
.erase(it
);
1735 void KFileItemModel::emitItemsChangedAndTriggerResorting(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &changedRoles
)
1737 Q_EMIT
itemsChanged(itemRanges
, changedRoles
);
1739 // Trigger a resorting if necessary. Note that this can happen even if the sort
1740 // role has not changed at all because the file name can be used as a fallback.
1741 if (changedRoles
.contains(sortRole()) || changedRoles
.contains(roleForType(NameRole
))) {
1742 for (const KItemRange
&range
: itemRanges
) {
1743 bool needsResorting
= false;
1745 const int first
= range
.index
;
1746 const int last
= range
.index
+ range
.count
- 1;
1748 // Resorting the model is necessary if
1749 // (a) The first item in the range is "lessThan" its predecessor,
1750 // (b) the successor of the last item is "lessThan" the last item, or
1751 // (c) the internal order of the items in the range is incorrect.
1752 if (first
> 0 && lessThan(m_itemData
.at(first
), m_itemData
.at(first
- 1), m_collator
)) {
1753 needsResorting
= true;
1754 } else if (last
< count() - 1 && lessThan(m_itemData
.at(last
+ 1), m_itemData
.at(last
), m_collator
)) {
1755 needsResorting
= true;
1757 for (int index
= first
; index
< last
; ++index
) {
1758 if (lessThan(m_itemData
.at(index
+ 1), m_itemData
.at(index
), m_collator
)) {
1759 needsResorting
= true;
1765 if (needsResorting
) {
1766 m_resortAllItemsTimer
->start();
1772 if (groupedSorting() && changedRoles
.contains(sortRole())) {
1773 // The position is still correct, but the groups might have changed
1774 // if the changed item is either the first or the last item in a
1776 // In principle, we could try to find out if the item really is the
1777 // first or last one in its group and then update the groups
1778 // (possibly with a delayed timer to make sure that we don't
1779 // re-calculate the groups very often if items are updated one by
1780 // one), but starting m_resortAllItemsTimer is easier.
1781 m_resortAllItemsTimer
->start();
1785 void KFileItemModel::resetRoles()
1787 for (int i
= 0; i
< RolesCount
; ++i
) {
1788 m_requestRole
[i
] = false;
1792 KFileItemModel::RoleType
KFileItemModel::typeForRole(const QByteArray
&role
) const
1794 static QHash
<QByteArray
, RoleType
> roles
;
1795 if (roles
.isEmpty()) {
1796 // Insert user visible roles that can be accessed with
1797 // KFileItemModel::roleInformation()
1799 const RoleInfoMap
*map
= rolesInfoMap(count
);
1800 for (int i
= 0; i
< count
; ++i
) {
1801 roles
.insert(map
[i
].role
, map
[i
].roleType
);
1804 // Insert internal roles (take care to synchronize the implementation
1805 // with KFileItemModel::roleForType() in case if a change is done).
1806 roles
.insert("isDir", IsDirRole
);
1807 roles
.insert("isLink", IsLinkRole
);
1808 roles
.insert("isHidden", IsHiddenRole
);
1809 roles
.insert("isExpanded", IsExpandedRole
);
1810 roles
.insert("isExpandable", IsExpandableRole
);
1811 roles
.insert("expandedParentsCount", ExpandedParentsCountRole
);
1813 Q_ASSERT(roles
.count() == RolesCount
);
1816 return roles
.value(role
, NoRole
);
1819 QByteArray
KFileItemModel::roleForType(RoleType roleType
) const
1821 static QHash
<RoleType
, QByteArray
> roles
;
1822 if (roles
.isEmpty()) {
1823 // Insert user visible roles that can be accessed with
1824 // KFileItemModel::roleInformation()
1826 const RoleInfoMap
*map
= rolesInfoMap(count
);
1827 for (int i
= 0; i
< count
; ++i
) {
1828 roles
.insert(map
[i
].roleType
, map
[i
].role
);
1831 // Insert internal roles (take care to synchronize the implementation
1832 // with KFileItemModel::typeForRole() in case if a change is done).
1833 roles
.insert(IsDirRole
, "isDir");
1834 roles
.insert(IsLinkRole
, "isLink");
1835 roles
.insert(IsHiddenRole
, "isHidden");
1836 roles
.insert(IsExpandedRole
, "isExpanded");
1837 roles
.insert(IsExpandableRole
, "isExpandable");
1838 roles
.insert(ExpandedParentsCountRole
, "expandedParentsCount");
1840 Q_ASSERT(roles
.count() == RolesCount
);
1843 return roles
.value(roleType
);
1846 QHash
<QByteArray
, QVariant
> KFileItemModel::retrieveData(const KFileItem
&item
, const ItemData
*parent
) const
1848 // It is important to insert only roles that are fast to retrieve. E.g.
1849 // KFileItem::iconName() can be very expensive if the MIME-type is unknown
1850 // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater.
1851 QHash
<QByteArray
, QVariant
> data
;
1852 data
.insert(sharedValue("url"), item
.url());
1854 const bool isDir
= item
.isDir();
1855 if (m_requestRole
[IsDirRole
] && isDir
) {
1856 data
.insert(sharedValue("isDir"), true);
1859 if (m_requestRole
[IsLinkRole
] && item
.isLink()) {
1860 data
.insert(sharedValue("isLink"), true);
1863 if (m_requestRole
[IsHiddenRole
]) {
1864 data
.insert(sharedValue("isHidden"), item
.isHidden() || item
.mimetype() == QStringLiteral("application/x-trash"));
1867 if (m_requestRole
[NameRole
]) {
1868 data
.insert(sharedValue("text"), item
.text());
1871 if (m_requestRole
[ExtensionRole
] && !isDir
) {
1872 // TODO KF6 use KFileItem::suffix 464722
1873 data
.insert(sharedValue("extension"), QFileInfo(item
.name()).suffix());
1876 if (m_requestRole
[SizeRole
] && !isDir
) {
1877 data
.insert(sharedValue("size"), item
.size());
1880 if (m_requestRole
[ModificationTimeRole
]) {
1881 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1882 // having several thousands of items. Instead read the raw number from UDSEntry directly
1883 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1884 const long long dateTime
= item
.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME
, -1);
1885 data
.insert(sharedValue("modificationtime"), dateTime
);
1888 if (m_requestRole
[CreationTimeRole
]) {
1889 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1890 // having several thousands of items. Instead read the raw number from UDSEntry directly
1891 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1892 const long long dateTime
= item
.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME
, -1);
1893 data
.insert(sharedValue("creationtime"), dateTime
);
1896 if (m_requestRole
[AccessTimeRole
]) {
1897 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1898 // having several thousands of items. Instead read the raw number from UDSEntry directly
1899 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1900 const long long dateTime
= item
.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME
, -1);
1901 data
.insert(sharedValue("accesstime"), dateTime
);
1904 if (m_requestRole
[PermissionsRole
]) {
1905 data
.insert(sharedValue("permissions"), QVariantList() << item
.permissionsString() << item
.permissions());
1908 if (m_requestRole
[OwnerRole
]) {
1909 data
.insert(sharedValue("owner"), item
.user());
1912 if (m_requestRole
[GroupRole
]) {
1913 data
.insert(sharedValue("group"), item
.group());
1916 if (m_requestRole
[DestinationRole
]) {
1917 QString destination
= item
.linkDest();
1918 if (destination
.isEmpty()) {
1919 destination
= QLatin1Char('-');
1921 data
.insert(sharedValue("destination"), destination
);
1924 if (m_requestRole
[PathRole
]) {
1926 if (item
.url().scheme() == QLatin1String("trash")) {
1927 path
= item
.entry().stringValue(KIO::UDSEntry::UDS_EXTRA
);
1929 // For performance reasons cache the home-path in a static QString
1930 // (see QDir::homePath() for more details)
1931 static QString homePath
;
1932 if (homePath
.isEmpty()) {
1933 homePath
= QDir::homePath();
1936 path
= item
.localPath();
1937 if (path
.startsWith(homePath
)) {
1938 path
.replace(0, homePath
.length(), QLatin1Char('~'));
1942 const int index
= path
.lastIndexOf(item
.text());
1943 path
= path
.mid(0, index
- 1);
1944 data
.insert(sharedValue("path"), path
);
1947 if (m_requestRole
[DeletionTimeRole
]) {
1948 QDateTime deletionTime
;
1949 if (item
.url().scheme() == QLatin1String("trash")) {
1950 deletionTime
= QDateTime::fromString(item
.entry().stringValue(KIO::UDSEntry::UDS_EXTRA
+ 1), Qt::ISODate
);
1952 data
.insert(sharedValue("deletiontime"), deletionTime
);
1955 if (m_requestRole
[IsExpandableRole
] && isDir
) {
1956 data
.insert(sharedValue("isExpandable"), true);
1959 if (m_requestRole
[ExpandedParentsCountRole
]) {
1961 const int level
= expandedParentsCount(parent
) + 1;
1962 data
.insert(sharedValue("expandedParentsCount"), level
);
1966 if (item
.isMimeTypeKnown()) {
1967 QString iconName
= item
.iconName();
1968 if (!QIcon::hasThemeIcon(iconName
)) {
1969 QMimeType mimeType
= QMimeDatabase().mimeTypeForName(item
.mimetype());
1970 iconName
= mimeType
.genericIconName();
1973 data
.insert(sharedValue("iconName"), iconName
);
1975 if (m_requestRole
[TypeRole
]) {
1976 data
.insert(sharedValue("type"), item
.mimeComment());
1978 } else if (m_requestRole
[TypeRole
] && isDir
) {
1979 static const QString folderMimeType
= item
.mimeComment();
1980 data
.insert(sharedValue("type"), folderMimeType
);
1986 bool KFileItemModel::lessThan(const ItemData
*a
, const ItemData
*b
, const QCollator
&collator
) const
1990 if (a
->parent
!= b
->parent
) {
1991 const int expansionLevelA
= expandedParentsCount(a
);
1992 const int expansionLevelB
= expandedParentsCount(b
);
1994 // If b has a higher expansion level than a, check if a is a parent
1995 // of b, and make sure that both expansion levels are equal otherwise.
1996 for (int i
= expansionLevelB
; i
> expansionLevelA
; --i
) {
1997 if (b
->parent
== a
) {
2003 // If a has a higher expansion level than a, check if b is a parent
2004 // of a, and make sure that both expansion levels are equal otherwise.
2005 for (int i
= expansionLevelA
; i
> expansionLevelB
; --i
) {
2006 if (a
->parent
== b
) {
2012 Q_ASSERT(expandedParentsCount(a
) == expandedParentsCount(b
));
2014 // Compare the last parents of a and b which are different.
2015 while (a
->parent
!= b
->parent
) {
2021 // Show hidden files and folders last
2022 if (m_sortHiddenLast
) {
2023 const bool isHiddenA
= a
->item
.isHidden();
2024 const bool isHiddenB
= b
->item
.isHidden();
2025 if (isHiddenA
&& !isHiddenB
) {
2027 } else if (!isHiddenA
&& isHiddenB
) {
2032 if (m_sortDirsFirst
|| (ContentDisplaySettings::directorySizeCount() && m_sortRole
== SizeRole
)) {
2033 const bool isDirA
= a
->item
.isDir();
2034 const bool isDirB
= b
->item
.isDir();
2035 if (isDirA
&& !isDirB
) {
2037 } else if (!isDirA
&& isDirB
) {
2042 result
= sortRoleCompare(a
, b
, collator
);
2044 return (sortOrder() == Qt::AscendingOrder
) ? result
< 0 : result
> 0;
2047 void KFileItemModel::sort(const QList
<KFileItemModel::ItemData
*>::iterator
&begin
, const QList
<KFileItemModel::ItemData
*>::iterator
&end
) const
2049 auto lambdaLessThan
= [&](const KFileItemModel::ItemData
*a
, const KFileItemModel::ItemData
*b
) {
2050 return lessThan(a
, b
, m_collator
);
2053 if (m_sortRole
== NameRole
|| isRoleValueNatural(m_sortRole
)) {
2054 // Sorting by string can be expensive, in particular if natural sorting is
2055 // enabled. Use all CPU cores to speed up the sorting process.
2056 static const int numberOfThreads
= QThread::idealThreadCount();
2057 parallelMergeSort(begin
, end
, lambdaLessThan
, numberOfThreads
);
2059 // Sorting by other roles is quite fast. Use only one thread to prevent
2060 // problems caused by non-reentrant comparison functions, see
2061 // https://bugs.kde.org/show_bug.cgi?id=312679
2062 mergeSort(begin
, end
, lambdaLessThan
);
2066 int KFileItemModel::sortRoleCompare(const ItemData
*a
, const ItemData
*b
, const QCollator
&collator
) const
2068 // This function must never return 0, because that would break stable
2069 // sorting, which leads to all kinds of bugs.
2070 // See: https://bugs.kde.org/show_bug.cgi?id=433247
2071 // If two items have equal sort values, let the fallbacks at the bottom of
2072 // the function handle it.
2073 const KFileItem
&itemA
= a
->item
;
2074 const KFileItem
&itemB
= b
->item
;
2078 switch (m_sortRole
) {
2080 // The name role is handled as default fallback after the switch
2084 if (ContentDisplaySettings::directorySizeCount() && itemA
.isDir()) {
2085 // folders first then
2086 // items A and B are folders thanks to lessThan checks
2087 auto valueA
= a
->values
.value("count");
2088 auto valueB
= b
->values
.value("count");
2089 if (valueA
.isNull()) {
2090 if (!valueB
.isNull()) {
2093 } else if (valueB
.isNull()) {
2096 if (valueA
.toLongLong() < valueB
.toLongLong()) {
2098 } else if (valueA
.toLongLong() > valueB
.toLongLong()) {
2105 KIO::filesize_t sizeA
= 0;
2106 if (itemA
.isDir()) {
2107 sizeA
= a
->values
.value("size").toULongLong();
2109 sizeA
= itemA
.size();
2111 KIO::filesize_t sizeB
= 0;
2112 if (itemB
.isDir()) {
2113 sizeB
= b
->values
.value("size").toULongLong();
2115 sizeB
= itemB
.size();
2117 if (sizeA
< sizeB
) {
2119 } else if (sizeA
> sizeB
) {
2125 case ModificationTimeRole
: {
2126 const long long dateTimeA
= itemA
.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME
, -1);
2127 const long long dateTimeB
= itemB
.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME
, -1);
2128 if (dateTimeA
< dateTimeB
) {
2130 } else if (dateTimeA
> dateTimeB
) {
2136 case AccessTimeRole
: {
2137 const long long dateTimeA
= itemA
.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME
, -1);
2138 const long long dateTimeB
= itemB
.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME
, -1);
2139 if (dateTimeA
< dateTimeB
) {
2141 } else if (dateTimeA
> dateTimeB
) {
2147 case CreationTimeRole
: {
2148 const long long dateTimeA
= itemA
.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME
, -1);
2149 const long long dateTimeB
= itemB
.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME
, -1);
2150 if (dateTimeA
< dateTimeB
) {
2152 } else if (dateTimeA
> dateTimeB
) {
2158 case DeletionTimeRole
: {
2159 const QDateTime dateTimeA
= a
->values
.value("deletiontime").toDateTime();
2160 const QDateTime dateTimeB
= b
->values
.value("deletiontime").toDateTime();
2161 if (dateTimeA
< dateTimeB
) {
2163 } else if (dateTimeA
> dateTimeB
) {
2177 case ReleaseYearRole
: {
2178 result
= a
->values
.value(roleForType(m_sortRole
)).toInt() - b
->values
.value(roleForType(m_sortRole
)).toInt();
2182 case DimensionsRole
: {
2183 const QByteArray role
= roleForType(m_sortRole
);
2184 const QSize dimensionsA
= a
->values
.value(role
).toSize();
2185 const QSize dimensionsB
= b
->values
.value(role
).toSize();
2187 if (dimensionsA
.width() == dimensionsB
.width()) {
2188 result
= dimensionsA
.height() - dimensionsB
.height();
2190 result
= dimensionsA
.width() - dimensionsB
.width();
2196 const QByteArray role
= roleForType(m_sortRole
);
2197 const QString roleValueA
= a
->values
.value(role
).toString();
2198 const QString roleValueB
= b
->values
.value(role
).toString();
2199 if (!roleValueA
.isEmpty() && roleValueB
.isEmpty()) {
2201 } else if (roleValueA
.isEmpty() && !roleValueB
.isEmpty()) {
2203 } else if (isRoleValueNatural(m_sortRole
)) {
2204 result
= stringCompare(roleValueA
, roleValueB
, collator
);
2206 result
= QString::compare(roleValueA
, roleValueB
);
2213 // The current sort role was sufficient to define an order
2217 // Fallback #1: Compare the text of the items
2218 result
= stringCompare(itemA
.text(), itemB
.text(), collator
);
2223 // Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
2224 result
= stringCompare(itemA
.name(), itemB
.name(), collator
);
2229 // Fallback #3: It must be assured that the sort order is always unique even if two values have been
2230 // equal. In this case a comparison of the URL is done which is unique in all cases
2231 // within KDirLister.
2232 return QString::compare(itemA
.url().url(), itemB
.url().url(), Qt::CaseSensitive
);
2235 int KFileItemModel::stringCompare(const QString
&a
, const QString
&b
, const QCollator
&collator
) const
2237 QMutexLocker
collatorLock(s_collatorMutex());
2239 if (m_naturalSorting
) {
2240 return collator
.compare(a
, b
);
2243 const int result
= QString::compare(a
, b
, collator
.caseSensitivity());
2244 if (result
!= 0 || collator
.caseSensitivity() == Qt::CaseSensitive
) {
2245 // Only return the result, if the strings are not equal. If they are equal by a case insensitive
2246 // comparison, still a deterministic sort order is required. A case sensitive
2247 // comparison is done as fallback.
2251 return QString::compare(a
, b
, Qt::CaseSensitive
);
2254 QList
<QPair
<int, QVariant
>> KFileItemModel::nameRoleGroups() const
2256 Q_ASSERT(!m_itemData
.isEmpty());
2258 const int maxIndex
= count() - 1;
2259 QList
<QPair
<int, QVariant
>> groups
;
2263 for (int i
= 0; i
<= maxIndex
; ++i
) {
2264 if (isChildItem(i
)) {
2268 const QString name
= m_itemData
.at(i
)->item
.text();
2270 // Use the first character of the name as group indication
2271 QChar newFirstChar
= name
.at(0).toUpper();
2272 if (newFirstChar
== QLatin1Char('~') && name
.length() > 1) {
2273 newFirstChar
= name
.at(1).toUpper();
2276 if (firstChar
!= newFirstChar
) {
2277 QString newGroupValue
;
2278 if (newFirstChar
.isLetter()) {
2279 if (m_collator
.compare(newFirstChar
, QChar(QLatin1Char('A'))) >= 0 && m_collator
.compare(newFirstChar
, QChar(QLatin1Char('Z'))) <= 0) {
2280 // WARNING! Symbols based on latin 'Z' like 'Z' with acute are treated wrong as non Latin and put in a new group.
2282 // Try to find a matching group in the range 'A' to 'Z'.
2283 static std::vector
<QChar
> lettersAtoZ
;
2284 lettersAtoZ
.reserve('Z' - 'A' + 1);
2285 if (lettersAtoZ
.empty()) {
2286 for (char c
= 'A'; c
<= 'Z'; ++c
) {
2287 lettersAtoZ
.push_back(QLatin1Char(c
));
2291 auto localeAwareLessThan
= [this](QChar c1
, QChar c2
) -> bool {
2292 return m_collator
.compare(c1
, c2
) < 0;
2295 std::vector
<QChar
>::iterator it
= std::lower_bound(lettersAtoZ
.begin(), lettersAtoZ
.end(), newFirstChar
, localeAwareLessThan
);
2296 if (it
!= lettersAtoZ
.end()) {
2297 if (localeAwareLessThan(newFirstChar
, *it
)) {
2298 // newFirstChar belongs to the group preceding *it.
2299 // Example: for an umlaut 'A' in the German locale, *it would be 'B' now.
2302 newGroupValue
= *it
;
2306 // Symbols from non Latin-based scripts
2307 newGroupValue
= newFirstChar
;
2309 } else if (newFirstChar
>= QLatin1Char('0') && newFirstChar
<= QLatin1Char('9')) {
2310 // Apply group '0 - 9' for any name that starts with a digit
2311 newGroupValue
= i18nc("@title:group Groups that start with a digit", "0 - 9");
2313 newGroupValue
= i18nc("@title:group", "Others");
2316 if (newGroupValue
!= groupValue
) {
2317 groupValue
= newGroupValue
;
2318 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2321 firstChar
= newFirstChar
;
2327 QList
<QPair
<int, QVariant
>> KFileItemModel::sizeRoleGroups() const
2329 Q_ASSERT(!m_itemData
.isEmpty());
2331 const int maxIndex
= count() - 1;
2332 QList
<QPair
<int, QVariant
>> groups
;
2335 for (int i
= 0; i
<= maxIndex
; ++i
) {
2336 if (isChildItem(i
)) {
2340 const KFileItem
&item
= m_itemData
.at(i
)->item
;
2341 KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
2342 QString newGroupValue
;
2343 if (!item
.isNull() && item
.isDir()) {
2344 if (ContentDisplaySettings::directorySizeCount() || m_sortDirsFirst
) {
2345 newGroupValue
= i18nc("@title:group Size", "Folders");
2347 fileSize
= m_itemData
.at(i
)->values
.value("size").toULongLong();
2351 if (newGroupValue
.isEmpty()) {
2352 if (fileSize
< 5 * 1024 * 1024) { // < 5 MB
2353 newGroupValue
= i18nc("@title:group Size", "Small");
2354 } else if (fileSize
< 10 * 1024 * 1024) { // < 10 MB
2355 newGroupValue
= i18nc("@title:group Size", "Medium");
2357 newGroupValue
= i18nc("@title:group Size", "Big");
2361 if (newGroupValue
!= groupValue
) {
2362 groupValue
= newGroupValue
;
2363 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2370 QList
<QPair
<int, QVariant
>> KFileItemModel::timeRoleGroups(const std::function
<QDateTime(const ItemData
*)> &fileTimeCb
) const
2372 Q_ASSERT(!m_itemData
.isEmpty());
2374 const int maxIndex
= count() - 1;
2375 QList
<QPair
<int, QVariant
>> groups
;
2377 const QDate currentDate
= QDate::currentDate();
2379 QDate previousFileDate
;
2381 for (int i
= 0; i
<= maxIndex
; ++i
) {
2382 if (isChildItem(i
)) {
2386 const QDateTime fileTime
= fileTimeCb(m_itemData
.at(i
));
2387 const QDate fileDate
= fileTime
.date();
2388 if (fileDate
== previousFileDate
) {
2389 // The current item is in the same group as the previous item
2392 previousFileDate
= fileDate
;
2394 const int daysDistance
= fileDate
.daysTo(currentDate
);
2396 QString newGroupValue
;
2397 if (currentDate
.year() == fileDate
.year() && currentDate
.month() == fileDate
.month()) {
2398 switch (daysDistance
/ 7) {
2400 switch (daysDistance
) {
2402 newGroupValue
= i18nc("@title:group Date", "Today");
2405 newGroupValue
= i18nc("@title:group Date", "Yesterday");
2408 newGroupValue
= fileTime
.toString(i18nc("@title:group Date: The week day name: dddd", "dddd"));
2409 newGroupValue
= i18nc(
2410 "Can be used to script translation of \"dddd\""
2411 "with context @title:group Date",
2417 newGroupValue
= i18nc("@title:group Date", "One Week Ago");
2420 newGroupValue
= i18nc("@title:group Date", "Two Weeks Ago");
2423 newGroupValue
= i18nc("@title:group Date", "Three Weeks Ago");
2427 newGroupValue
= i18nc("@title:group Date", "Earlier this Month");
2433 const QDate lastMonthDate
= currentDate
.addMonths(-1);
2434 if (lastMonthDate
.year() == fileDate
.year() && lastMonthDate
.month() == fileDate
.month()) {
2435 if (daysDistance
== 1) {
2436 const KLocalizedString format
= ki18nc(
2437 "@title:group Date: "
2438 "MMMM is full month name in current locale, and yyyy is "
2439 "full year number. You must keep the ' don't use any fancy \" or « or similar. The ' is not shown to the user, it's there to mark a "
2440 "part of the text that should not be formatted as a date",
2441 "'Yesterday' (MMMM, yyyy)");
2442 const QString translatedFormat
= format
.toString();
2443 if (translatedFormat
.count(QLatin1Char('\'')) == 2) {
2444 newGroupValue
= fileTime
.toString(translatedFormat
);
2445 newGroupValue
= i18nc(
2446 "Can be used to script translation of "
2447 "\"'Yesterday' (MMMM, yyyy)\" with context @title:group Date",
2451 qCWarning(DolphinDebug
).nospace()
2452 << "A wrong translation was found: " << translatedFormat
<< ". Please file a bug report at bugs.kde.org";
2453 const QString untranslatedFormat
= format
.toString({QLatin1String("en_US")});
2454 newGroupValue
= fileTime
.toString(untranslatedFormat
);
2456 } else if (daysDistance
<= 7) {
2458 fileTime
.toString(i18nc("@title:group Date: "
2459 "The week day name: dddd, MMMM is full month name "
2460 "in current locale, and yyyy is full year number.",
2461 "dddd (MMMM, yyyy)"));
2462 newGroupValue
= i18nc(
2463 "Can be used to script translation of "
2464 "\"dddd (MMMM, yyyy)\" with context @title:group Date",
2467 } else if (daysDistance
<= 7 * 2) {
2468 const KLocalizedString format
= ki18nc(
2469 "@title:group Date: "
2470 "MMMM is full month name in current locale, and yyyy is "
2471 "full year number. You must keep the ' don't use any fancy \" or « or similar. The ' is not shown to the user, it's there to mark a "
2472 "part of the text that should not be formatted as a date",
2473 "'One Week Ago' (MMMM, yyyy)");
2474 const QString translatedFormat
= format
.toString();
2475 if (translatedFormat
.count(QLatin1Char('\'')) == 2) {
2476 newGroupValue
= fileTime
.toString(translatedFormat
);
2477 newGroupValue
= i18nc(
2478 "Can be used to script translation of "
2479 "\"'One Week Ago' (MMMM, yyyy)\" with context @title:group Date",
2483 qCWarning(DolphinDebug
).nospace()
2484 << "A wrong translation was found: " << translatedFormat
<< ". Please file a bug report at bugs.kde.org";
2485 const QString untranslatedFormat
= format
.toString({QLatin1String("en_US")});
2486 newGroupValue
= fileTime
.toString(untranslatedFormat
);
2488 } else if (daysDistance
<= 7 * 3) {
2489 const KLocalizedString format
= ki18nc(
2490 "@title:group Date: "
2491 "MMMM is full month name in current locale, and yyyy is "
2492 "full year number. You must keep the ' don't use any fancy \" or « or similar. The ' is not shown to the user, it's there to mark a "
2493 "part of the text that should not be formatted as a date",
2494 "'Two Weeks Ago' (MMMM, yyyy)");
2495 const QString translatedFormat
= format
.toString();
2496 if (translatedFormat
.count(QLatin1Char('\'')) == 2) {
2497 newGroupValue
= fileTime
.toString(translatedFormat
);
2498 newGroupValue
= i18nc(
2499 "Can be used to script translation of "
2500 "\"'Two Weeks Ago' (MMMM, yyyy)\" with context @title:group Date",
2504 qCWarning(DolphinDebug
).nospace()
2505 << "A wrong translation was found: " << translatedFormat
<< ". Please file a bug report at bugs.kde.org";
2506 const QString untranslatedFormat
= format
.toString({QLatin1String("en_US")});
2507 newGroupValue
= fileTime
.toString(untranslatedFormat
);
2509 } else if (daysDistance
<= 7 * 4) {
2510 const KLocalizedString format
= ki18nc(
2511 "@title:group Date: "
2512 "MMMM is full month name in current locale, and yyyy is "
2513 "full year number. You must keep the ' don't use any fancy \" or « or similar. The ' is not shown to the user, it's there to mark a "
2514 "part of the text that should not be formatted as a date",
2515 "'Three Weeks Ago' (MMMM, yyyy)");
2516 const QString translatedFormat
= format
.toString();
2517 if (translatedFormat
.count(QLatin1Char('\'')) == 2) {
2518 newGroupValue
= fileTime
.toString(translatedFormat
);
2519 newGroupValue
= i18nc(
2520 "Can be used to script translation of "
2521 "\"'Three Weeks Ago' (MMMM, yyyy)\" with context @title:group Date",
2525 qCWarning(DolphinDebug
).nospace()
2526 << "A wrong translation was found: " << translatedFormat
<< ". Please file a bug report at bugs.kde.org";
2527 const QString untranslatedFormat
= format
.toString({QLatin1String("en_US")});
2528 newGroupValue
= fileTime
.toString(untranslatedFormat
);
2531 const KLocalizedString format
= ki18nc(
2532 "@title:group Date: "
2533 "MMMM is full month name in current locale, and yyyy is "
2534 "full year number. You must keep the ' don't use any fancy \" or « or similar. The ' is not shown to the user, it's there to mark a "
2535 "part of the text that should not be formatted as a date",
2536 "'Earlier on' MMMM, yyyy");
2537 const QString translatedFormat
= format
.toString();
2538 if (translatedFormat
.count(QLatin1Char('\'')) == 2) {
2539 newGroupValue
= fileTime
.toString(translatedFormat
);
2540 newGroupValue
= i18nc(
2541 "Can be used to script translation of "
2542 "\"'Earlier on' MMMM, yyyy\" with context @title:group Date",
2546 qCWarning(DolphinDebug
).nospace()
2547 << "A wrong translation was found: " << translatedFormat
<< ". Please file a bug report at bugs.kde.org";
2548 const QString untranslatedFormat
= format
.toString({QLatin1String("en_US")});
2549 newGroupValue
= fileTime
.toString(untranslatedFormat
);
2554 fileTime
.toString(i18nc("@title:group "
2555 "The month and year: MMMM is full month name in current locale, "
2556 "and yyyy is full year number",
2558 newGroupValue
= i18nc(
2559 "Can be used to script translation of "
2560 "\"MMMM, yyyy\" with context @title:group Date",
2566 if (newGroupValue
!= groupValue
) {
2567 groupValue
= newGroupValue
;
2568 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2575 QList
<QPair
<int, QVariant
>> KFileItemModel::permissionRoleGroups() const
2577 Q_ASSERT(!m_itemData
.isEmpty());
2579 const int maxIndex
= count() - 1;
2580 QList
<QPair
<int, QVariant
>> groups
;
2582 QString permissionsString
;
2584 for (int i
= 0; i
<= maxIndex
; ++i
) {
2585 if (isChildItem(i
)) {
2589 const ItemData
*itemData
= m_itemData
.at(i
);
2590 const QString newPermissionsString
= itemData
->values
.value("permissions").toString();
2591 if (newPermissionsString
== permissionsString
) {
2594 permissionsString
= newPermissionsString
;
2596 const QFileInfo
info(itemData
->item
.url().toLocalFile());
2600 if (info
.permission(QFile::ReadUser
)) {
2601 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
2603 if (info
.permission(QFile::WriteUser
)) {
2604 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
2606 if (info
.permission(QFile::ExeUser
)) {
2607 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
2609 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
2613 if (info
.permission(QFile::ReadGroup
)) {
2614 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
2616 if (info
.permission(QFile::WriteGroup
)) {
2617 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
2619 if (info
.permission(QFile::ExeGroup
)) {
2620 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
2622 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
2624 // Set others string
2626 if (info
.permission(QFile::ReadOther
)) {
2627 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
2629 if (info
.permission(QFile::WriteOther
)) {
2630 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
2632 if (info
.permission(QFile::ExeOther
)) {
2633 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
2635 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
2637 const QString newGroupValue
= i18nc("@title:group Files and folders by permissions", "User: %1 | Group: %2 | Others: %3", user
, group
, others
);
2638 if (newGroupValue
!= groupValue
) {
2639 groupValue
= newGroupValue
;
2640 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2647 QList
<QPair
<int, QVariant
>> KFileItemModel::ratingRoleGroups() const
2649 Q_ASSERT(!m_itemData
.isEmpty());
2651 const int maxIndex
= count() - 1;
2652 QList
<QPair
<int, QVariant
>> groups
;
2654 int groupValue
= -1;
2655 for (int i
= 0; i
<= maxIndex
; ++i
) {
2656 if (isChildItem(i
)) {
2659 const int newGroupValue
= m_itemData
.at(i
)->values
.value("rating", 0).toInt();
2660 if (newGroupValue
!= groupValue
) {
2661 groupValue
= newGroupValue
;
2662 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2669 QList
<QPair
<int, QVariant
>> KFileItemModel::genericStringRoleGroups(const QByteArray
&role
) const
2671 Q_ASSERT(!m_itemData
.isEmpty());
2673 const int maxIndex
= count() - 1;
2674 QList
<QPair
<int, QVariant
>> groups
;
2676 bool isFirstGroupValue
= true;
2678 for (int i
= 0; i
<= maxIndex
; ++i
) {
2679 if (isChildItem(i
)) {
2682 const QString newGroupValue
= m_itemData
.at(i
)->values
.value(role
).toString();
2683 if (newGroupValue
!= groupValue
|| isFirstGroupValue
) {
2684 groupValue
= newGroupValue
;
2685 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
2686 isFirstGroupValue
= false;
2693 void KFileItemModel::emitSortProgress(int resolvedCount
)
2695 // Be tolerant against a resolvedCount with a wrong range.
2696 // Although there should not be a case where KFileItemModelRolesUpdater
2697 // (= caller) provides a wrong range, it is important to emit
2698 // a useful progress information even if there is an unexpected
2699 // implementation issue.
2701 const int itemCount
= count();
2702 if (resolvedCount
>= itemCount
) {
2703 m_sortingProgressPercent
= -1;
2704 if (m_resortAllItemsTimer
->isActive()) {
2705 m_resortAllItemsTimer
->stop();
2709 Q_EMIT
directorySortingProgress(100);
2710 } else if (itemCount
> 0) {
2711 resolvedCount
= qBound(0, resolvedCount
, itemCount
);
2713 const int progress
= resolvedCount
* 100 / itemCount
;
2714 if (m_sortingProgressPercent
!= progress
) {
2715 m_sortingProgressPercent
= progress
;
2716 Q_EMIT
directorySortingProgress(progress
);
2721 const KFileItemModel::RoleInfoMap
*KFileItemModel::rolesInfoMap(int &count
)
2723 static const RoleInfoMap rolesInfoMap
[] = {
2725 // | role | roleType | role translation | group translation | requires Baloo | requires indexer
2726 { nullptr, NoRole
, KLazyLocalizedString(), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2727 { "text", NameRole
, kli18nc("@label", "Name"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2728 { "size", SizeRole
, kli18nc("@label", "Size"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2729 { "modificationtime", ModificationTimeRole
, kli18nc("@label", "Modified"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2730 { "creationtime", CreationTimeRole
, kli18nc("@label", "Created"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2731 { "accesstime", AccessTimeRole
, kli18nc("@label", "Accessed"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2732 { "type", TypeRole
, kli18nc("@label", "Type"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2733 { "rating", RatingRole
, kli18nc("@label", "Rating"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2734 { "tags", TagsRole
, kli18nc("@label", "Tags"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2735 { "comment", CommentRole
, kli18nc("@label", "Comment"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2736 { "title", TitleRole
, kli18nc("@label", "Title"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2737 { "author", AuthorRole
, kli18nc("@label", "Author"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2738 { "publisher", PublisherRole
, kli18nc("@label", "Publisher"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2739 { "pageCount", PageCountRole
, kli18nc("@label", "Page Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2740 { "wordCount", WordCountRole
, kli18nc("@label", "Word Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2741 { "lineCount", LineCountRole
, kli18nc("@label", "Line Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2742 { "imageDateTime", ImageDateTimeRole
, kli18nc("@label", "Date Photographed"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2743 { "dimensions", DimensionsRole
, kli18nc("@label width x height", "Dimensions"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2744 { "width", WidthRole
, kli18nc("@label", "Width"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2745 { "height", HeightRole
, kli18nc("@label", "Height"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2746 { "orientation", OrientationRole
, kli18nc("@label", "Orientation"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2747 { "artist", ArtistRole
, kli18nc("@label", "Artist"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2748 { "genre", GenreRole
, kli18nc("@label", "Genre"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2749 { "album", AlbumRole
, kli18nc("@label", "Album"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2750 { "duration", DurationRole
, kli18nc("@label", "Duration"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2751 { "bitrate", BitrateRole
, kli18nc("@label", "Bitrate"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2752 { "track", TrackRole
, kli18nc("@label", "Track"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2753 { "releaseYear", ReleaseYearRole
, kli18nc("@label", "Release Year"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2754 { "aspectRatio", AspectRatioRole
, kli18nc("@label", "Aspect Ratio"), kli18nc("@label", "Video"), KLazyLocalizedString(), true, true },
2755 { "frameRate", FrameRateRole
, kli18nc("@label", "Frame Rate"), kli18nc("@label", "Video"), KLazyLocalizedString(), true, true },
2756 { "path", PathRole
, kli18nc("@label", "Path"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2757 { "extension", ExtensionRole
, kli18nc("@label", "File Extension"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2758 { "deletiontime", DeletionTimeRole
, kli18nc("@label", "Deletion Time"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2759 { "destination", DestinationRole
, kli18nc("@label", "Link Destination"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2760 { "originUrl", OriginUrlRole
, kli18nc("@label", "Downloaded From"), kli18nc("@label", "Other"), KLazyLocalizedString(), true, false },
2761 { "permissions", PermissionsRole
, kli18nc("@label", "Permissions"), kli18nc("@label", "Other"), kli18nc("@tooltip", "The permission format can be changed in settings. Options are Symbolic, Numeric (Octal) or Combined formats"), false, false },
2762 { "owner", OwnerRole
, kli18nc("@label", "Owner"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2763 { "group", GroupRole
, kli18nc("@label", "User Group"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2767 count
= sizeof(rolesInfoMap
) / sizeof(RoleInfoMap
);
2768 return rolesInfoMap
;
2771 void KFileItemModel::determineMimeTypes(const KFileItemList
&items
, int timeout
)
2773 QElapsedTimer timer
;
2775 for (const KFileItem
&item
: items
) {
2776 // Only determine mime types for files here. For directories,
2777 // KFileItem::determineMimeType() reads the .directory file inside to
2778 // load the icon, but this is not necessary at all if we just need the
2779 // type. Some special code for setting the correct mime type for
2780 // directories is in retrieveData().
2781 if (!item
.isDir()) {
2782 item
.determineMimeType();
2785 if (timer
.elapsed() > timeout
) {
2786 // Don't block the user interface, let the remaining items
2787 // be resolved asynchronously.
2793 QByteArray
KFileItemModel::sharedValue(const QByteArray
&value
)
2795 static QSet
<QByteArray
> pool
;
2796 const QSet
<QByteArray
>::const_iterator it
= pool
.constFind(value
);
2798 if (it
!= pool
.constEnd()) {
2806 bool KFileItemModel::isConsistent() const
2808 // m_items may contain less items than m_itemData because m_items
2809 // is populated lazily, see KFileItemModel::index(const QUrl& url).
2810 if (m_items
.count() > m_itemData
.count()) {
2814 for (int i
= 0, iMax
= count(); i
< iMax
; ++i
) {
2815 // Check if m_items and m_itemData are consistent.
2816 const KFileItem item
= fileItem(i
);
2817 if (item
.isNull()) {
2818 qCWarning(DolphinDebug
) << "Item" << i
<< "is null";
2822 const int itemIndex
= index(item
);
2823 if (itemIndex
!= i
) {
2824 qCWarning(DolphinDebug
) << "Item" << i
<< "has a wrong index:" << itemIndex
;
2828 // Check if the items are sorted correctly.
2829 if (i
> 0 && !lessThan(m_itemData
.at(i
- 1), m_itemData
.at(i
), m_collator
)) {
2830 qCWarning(DolphinDebug
) << "The order of items" << i
- 1 << "and" << i
<< "is wrong:" << fileItem(i
- 1) << fileItem(i
);
2834 // Check if all parent-child relationships are consistent.
2835 const ItemData
*data
= m_itemData
.at(i
);
2836 const ItemData
*parent
= data
->parent
;
2838 if (expandedParentsCount(data
) != expandedParentsCount(parent
) + 1) {
2839 qCWarning(DolphinDebug
) << "expandedParentsCount is inconsistent for parent" << parent
->item
<< "and child" << data
->item
;
2843 const int parentIndex
= index(parent
->item
);
2844 if (parentIndex
>= i
) {
2845 qCWarning(DolphinDebug
) << "Index" << parentIndex
<< "of parent" << parent
->item
<< "is not smaller than index" << i
<< "of child"
2855 void KFileItemModel::slotListerError(KIO::Job
*job
)
2857 if (job
->error() == KIO::ERR_IS_FILE
) {
2858 if (auto *listJob
= qobject_cast
<KIO::ListJob
*>(job
)) {
2859 Q_EMIT
urlIsFileError(listJob
->url());
2862 const QString errorString
= job
->errorString();
2863 Q_EMIT
errorMessage(!errorString
.isEmpty() ? errorString
: i18nc("@info:status", "Unknown error."));
2867 #include "moc_kfileitemmodel.cpp"