]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodel.cpp
Resort directory size count after refreshing
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "kfileitemmodel.h"
10
11 #include "dolphin_contentdisplaysettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphindebug.h"
14 #include "private/kfileitemmodelsortalgorithm.h"
15
16 #include <KDirLister>
17 #include <KIO/Job>
18 #include <KLocalizedString>
19 #include <KUrlMimeData>
20
21 #include <QElapsedTimer>
22 #include <QIcon>
23 #include <QMimeData>
24 #include <QMimeDatabase>
25 #include <QRecursiveMutex>
26 #include <QTimer>
27 #include <QWidget>
28 #include <algorithm>
29 #include <klazylocalizedstring.h>
30
31 Q_GLOBAL_STATIC(QRecursiveMutex, s_collatorMutex)
32
33 // #define KFILEITEMMODEL_DEBUG
34
35 KFileItemModel::KFileItemModel(QObject *parent)
36 : KItemModelBase("text", parent)
37 , m_dirLister(nullptr)
38 , m_sortDirsFirst(true)
39 , m_sortHiddenLast(false)
40 , m_sortRole(NameRole)
41 , m_sortingProgressPercent(-1)
42 , m_roles()
43 , m_itemData()
44 , m_items()
45 , m_filter()
46 , m_filteredItems()
47 , m_requestRole()
48 , m_maximumUpdateIntervalTimer(nullptr)
49 , m_resortAllItemsTimer(nullptr)
50 , m_pendingItemsToInsert()
51 , m_groups()
52 , m_expandedDirs()
53 , m_urlsToExpand()
54 {
55 m_collator.setNumericMode(true);
56
57 loadSortingSettings();
58
59 m_dirLister = new KDirLister(this);
60 m_dirLister->setAutoErrorHandlingEnabled(false);
61 m_dirLister->setDelayedMimeTypes(true);
62
63 const QWidget *parentWidget = qobject_cast<QWidget *>(parent);
64 if (parentWidget) {
65 m_dirLister->setMainWindow(parentWidget->window());
66 }
67
68 connect(m_dirLister, &KCoreDirLister::started, this, &KFileItemModel::directoryLoadingStarted);
69 connect(m_dirLister, &KCoreDirLister::canceled, this, &KFileItemModel::slotCanceled);
70 connect(m_dirLister, &KCoreDirLister::itemsAdded, this, &KFileItemModel::slotItemsAdded);
71 connect(m_dirLister, &KCoreDirLister::itemsDeleted, this, &KFileItemModel::slotItemsDeleted);
72 connect(m_dirLister, &KCoreDirLister::refreshItems, this, &KFileItemModel::slotRefreshItems);
73 connect(m_dirLister, &KCoreDirLister::clear, this, &KFileItemModel::slotClear);
74 connect(m_dirLister, &KCoreDirLister::infoMessage, this, &KFileItemModel::infoMessage);
75 connect(m_dirLister, &KCoreDirLister::jobError, this, &KFileItemModel::slotListerError);
76 connect(m_dirLister, &KCoreDirLister::percent, this, &KFileItemModel::directoryLoadingProgress);
77 connect(m_dirLister, &KCoreDirLister::redirection, this, &KFileItemModel::directoryRedirection);
78 connect(m_dirLister, &KCoreDirLister::listingDirCompleted, this, &KFileItemModel::slotCompleted);
79
80 // Apply default roles that should be determined
81 resetRoles();
82 m_requestRole[NameRole] = true;
83 m_requestRole[IsDirRole] = true;
84 m_requestRole[IsLinkRole] = true;
85 m_roles.insert("text");
86 m_roles.insert("isDir");
87 m_roles.insert("isLink");
88 m_roles.insert("isHidden");
89
90 // For slow KIO-slaves like used for searching it makes sense to show results periodically even
91 // before the completed() or canceled() signal has been emitted.
92 m_maximumUpdateIntervalTimer = new QTimer(this);
93 m_maximumUpdateIntervalTimer->setInterval(2000);
94 m_maximumUpdateIntervalTimer->setSingleShot(true);
95 connect(m_maximumUpdateIntervalTimer, &QTimer::timeout, this, &KFileItemModel::dispatchPendingItemsToInsert);
96
97 // When changing the value of an item which represents the sort-role a resorting must be
98 // triggered. Especially in combination with KFileItemModelRolesUpdater this might be done
99 // for a lot of items within a quite small timeslot. To prevent expensive resortings the
100 // resorting is postponed until the timer has been exceeded.
101 m_resortAllItemsTimer = new QTimer(this);
102 m_resortAllItemsTimer->setInterval(50);
103 m_resortAllItemsTimer->setSingleShot(true);
104 connect(m_resortAllItemsTimer, &QTimer::timeout, this, &KFileItemModel::resortAllItems);
105
106 connect(GeneralSettings::self(), &GeneralSettings::sortingChoiceChanged, this, &KFileItemModel::slotSortingChoiceChanged);
107
108 setShowTrashMime(m_dirLister->showHiddenFiles());
109 }
110
111 KFileItemModel::~KFileItemModel()
112 {
113 qDeleteAll(m_itemData);
114 qDeleteAll(m_filteredItems);
115 qDeleteAll(m_pendingItemsToInsert);
116 }
117
118 void KFileItemModel::loadDirectory(const QUrl &url)
119 {
120 m_dirLister->openUrl(url);
121 }
122
123 void KFileItemModel::refreshDirectory(const QUrl &url)
124 {
125 // Refresh all expanded directories first (Bug 295300)
126 QHashIterator<QUrl, QUrl> expandedDirs(m_expandedDirs);
127 while (expandedDirs.hasNext()) {
128 expandedDirs.next();
129 m_dirLister->openUrl(expandedDirs.value(), KDirLister::Reload);
130 }
131
132 m_dirLister->openUrl(url, KDirLister::Reload);
133
134 Q_EMIT directoryRefreshing();
135 }
136
137 QUrl KFileItemModel::directory() const
138 {
139 return m_dirLister->url();
140 }
141
142 void KFileItemModel::cancelDirectoryLoading()
143 {
144 m_dirLister->stop();
145 }
146
147 int KFileItemModel::count() const
148 {
149 return m_itemData.count();
150 }
151
152 QHash<QByteArray, QVariant> KFileItemModel::data(int index) const
153 {
154 if (index >= 0 && index < count()) {
155 ItemData *data = m_itemData.at(index);
156 if (data->values.isEmpty()) {
157 data->values = retrieveData(data->item, data->parent);
158 } else if (data->values.count() <= 2 && data->values.value("isExpanded").toBool()) {
159 // Special case dealt by slotRefreshItems(), avoid losing the "isExpanded" and "expandedParentsCount" state when refreshing
160 // slotRefreshItems() makes sure folders keep the "isExpanded" and "expandedParentsCount" while clearing the remaining values
161 // so this special request of different behavior can be identified here.
162 bool hasExpandedParentsCount = false;
163 const int expandedParentsCount = data->values.value("expandedParentsCount").toInt(&hasExpandedParentsCount);
164
165 data->values = retrieveData(data->item, data->parent);
166 data->values.insert("isExpanded", true);
167 if (hasExpandedParentsCount) {
168 data->values.insert("expandedParentsCount", expandedParentsCount);
169 }
170 }
171
172 return data->values;
173 }
174 return QHash<QByteArray, QVariant>();
175 }
176
177 bool KFileItemModel::setData(int index, const QHash<QByteArray, QVariant> &values)
178 {
179 if (index < 0 || index >= count()) {
180 return false;
181 }
182
183 QHash<QByteArray, QVariant> currentValues = data(index);
184
185 // Determine which roles have been changed
186 QSet<QByteArray> changedRoles;
187 QHashIterator<QByteArray, QVariant> it(values);
188 while (it.hasNext()) {
189 it.next();
190 const QByteArray role = sharedValue(it.key());
191 const QVariant value = it.value();
192
193 if (currentValues[role] != value) {
194 currentValues[role] = value;
195 changedRoles.insert(role);
196 }
197 }
198
199 if (changedRoles.isEmpty()) {
200 return false;
201 }
202
203 m_itemData[index]->values = currentValues;
204 if (changedRoles.contains("text")) {
205 QUrl url = m_itemData[index]->item.url();
206 url = url.adjusted(QUrl::RemoveFilename);
207 url.setPath(url.path() + currentValues["text"].toString());
208 m_itemData[index]->item.setUrl(url);
209 }
210
211 emitItemsChangedAndTriggerResorting(KItemRangeList() << KItemRange(index, 1), changedRoles);
212
213 return true;
214 }
215
216 void KFileItemModel::setSortDirectoriesFirst(bool dirsFirst)
217 {
218 if (dirsFirst != m_sortDirsFirst) {
219 m_sortDirsFirst = dirsFirst;
220 resortAllItems();
221 }
222 }
223
224 bool KFileItemModel::sortDirectoriesFirst() const
225 {
226 return m_sortDirsFirst;
227 }
228
229 void KFileItemModel::setSortHiddenLast(bool hiddenLast)
230 {
231 if (hiddenLast != m_sortHiddenLast) {
232 m_sortHiddenLast = hiddenLast;
233 resortAllItems();
234 }
235 }
236
237 bool KFileItemModel::sortHiddenLast() const
238 {
239 return m_sortHiddenLast;
240 }
241
242 void KFileItemModel::setShowTrashMime(bool show)
243 {
244 const auto trashMime = QStringLiteral("application/x-trash");
245 QStringList excludeFilter = m_filter.excludeMimeTypes();
246 bool wasShown = !excludeFilter.contains(trashMime);
247
248 if (show) {
249 if (!wasShown) {
250 excludeFilter.removeAll(trashMime);
251 }
252 } else {
253 if (wasShown) {
254 excludeFilter.append(trashMime);
255 }
256 }
257
258 if (wasShown != show) {
259 setExcludeMimeTypeFilter(excludeFilter);
260 }
261 }
262
263 void KFileItemModel::scheduleResortAllItems()
264 {
265 if (!m_resortAllItemsTimer->isActive()) {
266 m_resortAllItemsTimer->start();
267 }
268 }
269
270 void KFileItemModel::setShowHiddenFiles(bool show)
271 {
272 m_dirLister->setShowHiddenFiles(show);
273 setShowTrashMime(show);
274 m_dirLister->emitChanges();
275 if (show) {
276 dispatchPendingItemsToInsert();
277 }
278 }
279
280 bool KFileItemModel::showHiddenFiles() const
281 {
282 return m_dirLister->showHiddenFiles();
283 }
284
285 void KFileItemModel::setShowDirectoriesOnly(bool enabled)
286 {
287 m_dirLister->setDirOnlyMode(enabled);
288 }
289
290 bool KFileItemModel::showDirectoriesOnly() const
291 {
292 return m_dirLister->dirOnlyMode();
293 }
294
295 QMimeData *KFileItemModel::createMimeData(const KItemSet &indexes) const
296 {
297 QMimeData *data = new QMimeData();
298
299 // The following code has been taken from KDirModel::mimeData()
300 // (kdelibs/kio/kio/kdirmodel.cpp)
301 // SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
302 QList<QUrl> urls;
303 QList<QUrl> mostLocalUrls;
304 const ItemData *lastAddedItem = nullptr;
305
306 for (int index : indexes) {
307 const ItemData *itemData = m_itemData.at(index);
308 const ItemData *parent = itemData->parent;
309
310 while (parent && parent != lastAddedItem) {
311 parent = parent->parent;
312 }
313
314 if (parent && parent == lastAddedItem) {
315 // A parent of 'itemData' has been added already.
316 continue;
317 }
318
319 lastAddedItem = itemData;
320 const KFileItem &item = itemData->item;
321 if (!item.isNull()) {
322 urls << item.url();
323
324 bool isLocal;
325 mostLocalUrls << item.mostLocalUrl(&isLocal);
326 }
327 }
328
329 KUrlMimeData::setUrls(urls, mostLocalUrls, data);
330 return data;
331 }
332
333 int KFileItemModel::indexForKeyboardSearch(const QString &text, int startFromIndex) const
334 {
335 startFromIndex = qMax(0, startFromIndex);
336 for (int i = startFromIndex; i < count(); ++i) {
337 if (fileItem(i).text().startsWith(text, Qt::CaseInsensitive)) {
338 return i;
339 }
340 }
341 for (int i = 0; i < startFromIndex; ++i) {
342 if (fileItem(i).text().startsWith(text, Qt::CaseInsensitive)) {
343 return i;
344 }
345 }
346 return -1;
347 }
348
349 bool KFileItemModel::supportsDropping(int index) const
350 {
351 KFileItem item;
352 if (index == -1) {
353 item = rootItem();
354 } else {
355 item = fileItem(index);
356 }
357 return !item.isNull() && ((item.isDir() && item.isWritable()) || item.isDesktopFile());
358 }
359
360 QString KFileItemModel::roleDescription(const QByteArray &role) const
361 {
362 static QHash<QByteArray, QString> description;
363 if (description.isEmpty()) {
364 int count = 0;
365 const RoleInfoMap *map = rolesInfoMap(count);
366 for (int i = 0; i < count; ++i) {
367 if (map[i].roleTranslation.isEmpty()) {
368 continue;
369 }
370 description.insert(map[i].role, map[i].roleTranslation.toString());
371 }
372 }
373
374 return description.value(role);
375 }
376
377 QList<QPair<int, QVariant>> KFileItemModel::groups() const
378 {
379 if (!m_itemData.isEmpty() && m_groups.isEmpty()) {
380 #ifdef KFILEITEMMODEL_DEBUG
381 QElapsedTimer timer;
382 timer.start();
383 #endif
384 switch (typeForRole(sortRole())) {
385 case NameRole:
386 m_groups = nameRoleGroups();
387 break;
388 case SizeRole:
389 m_groups = sizeRoleGroups();
390 break;
391 case ModificationTimeRole:
392 m_groups = timeRoleGroups([](const ItemData *item) {
393 return item->item.time(KFileItem::ModificationTime);
394 });
395 break;
396 case CreationTimeRole:
397 m_groups = timeRoleGroups([](const ItemData *item) {
398 return item->item.time(KFileItem::CreationTime);
399 });
400 break;
401 case AccessTimeRole:
402 m_groups = timeRoleGroups([](const ItemData *item) {
403 return item->item.time(KFileItem::AccessTime);
404 });
405 break;
406 case DeletionTimeRole:
407 m_groups = timeRoleGroups([](const ItemData *item) {
408 return item->values.value("deletiontime").toDateTime();
409 });
410 break;
411 case PermissionsRole:
412 m_groups = permissionRoleGroups();
413 break;
414 case RatingRole:
415 m_groups = ratingRoleGroups();
416 break;
417 default:
418 m_groups = genericStringRoleGroups(sortRole());
419 break;
420 }
421
422 #ifdef KFILEITEMMODEL_DEBUG
423 qCDebug(DolphinDebug) << "[TIME] Calculating groups for" << count() << "items:" << timer.elapsed();
424 #endif
425 }
426
427 return m_groups;
428 }
429
430 KFileItem KFileItemModel::fileItem(int index) const
431 {
432 if (index >= 0 && index < count()) {
433 return m_itemData.at(index)->item;
434 }
435
436 return KFileItem();
437 }
438
439 KFileItem KFileItemModel::fileItem(const QUrl &url) const
440 {
441 const int indexForUrl = index(url);
442 if (indexForUrl >= 0) {
443 return m_itemData.at(indexForUrl)->item;
444 }
445 return KFileItem();
446 }
447
448 int KFileItemModel::index(const KFileItem &item) const
449 {
450 return index(item.url());
451 }
452
453 int KFileItemModel::index(const QUrl &url) const
454 {
455 const QUrl urlToFind = url.adjusted(QUrl::StripTrailingSlash);
456
457 const int itemCount = m_itemData.count();
458 int itemsInHash = m_items.count();
459
460 int index = m_items.value(urlToFind, -1);
461 while (index < 0 && itemsInHash < itemCount) {
462 // Not all URLs are stored yet in m_items. We grow m_items until either
463 // urlToFind is found, or all URLs have been stored in m_items.
464 // Note that we do not add the URLs to m_items one by one, but in
465 // larger blocks. After each block, we check if urlToFind is in
466 // m_items. We could in principle compare urlToFind with each URL while
467 // we are going through m_itemData, but comparing two QUrls will,
468 // unlike calling qHash for the URLs, trigger a parsing of the URLs
469 // which costs both CPU cycles and memory.
470 const int blockSize = 1000;
471 const int currentBlockEnd = qMin(itemsInHash + blockSize, itemCount);
472 for (int i = itemsInHash; i < currentBlockEnd; ++i) {
473 const QUrl nextUrl = m_itemData.at(i)->item.url();
474 m_items.insert(nextUrl, i);
475 }
476
477 itemsInHash = currentBlockEnd;
478 index = m_items.value(urlToFind, -1);
479 }
480
481 if (index < 0) {
482 // The item could not be found, even though all items from m_itemData
483 // should be in m_items now. We print some diagnostic information which
484 // might help to find the cause of the problem, but only once. This
485 // prevents that obtaining and printing the debugging information
486 // wastes CPU cycles and floods the shell or .xsession-errors.
487 static bool printDebugInfo = true;
488
489 if (m_items.count() != m_itemData.count() && printDebugInfo) {
490 printDebugInfo = false;
491
492 qCWarning(DolphinDebug) << "The model is in an inconsistent state.";
493 qCWarning(DolphinDebug) << "m_items.count() ==" << m_items.count();
494 qCWarning(DolphinDebug) << "m_itemData.count() ==" << m_itemData.count();
495
496 // Check if there are multiple items with the same URL.
497 QMultiHash<QUrl, int> indexesForUrl;
498 for (int i = 0; i < m_itemData.count(); ++i) {
499 indexesForUrl.insert(m_itemData.at(i)->item.url(), i);
500 }
501
502 const auto uniqueKeys = indexesForUrl.uniqueKeys();
503 for (const QUrl &url : uniqueKeys) {
504 if (indexesForUrl.count(url) > 1) {
505 qCWarning(DolphinDebug) << "Multiple items found with the URL" << url;
506
507 auto it = indexesForUrl.find(url);
508 while (it != indexesForUrl.end() && it.key() == url) {
509 const ItemData *data = m_itemData.at(it.value());
510 qCWarning(DolphinDebug) << "index" << it.value() << ":" << data->item;
511 if (data->parent) {
512 qCWarning(DolphinDebug) << "parent" << data->parent->item;
513 }
514 ++it;
515 }
516 }
517 }
518 }
519 }
520
521 return index;
522 }
523
524 KFileItem KFileItemModel::rootItem() const
525 {
526 return m_dirLister->rootItem();
527 }
528
529 void KFileItemModel::clear()
530 {
531 slotClear();
532 }
533
534 void KFileItemModel::setRoles(const QSet<QByteArray> &roles)
535 {
536 if (m_roles == roles) {
537 return;
538 }
539
540 const QSet<QByteArray> changedRoles = (roles - m_roles) + (m_roles - roles);
541 m_roles = roles;
542
543 if (count() > 0) {
544 const bool supportedExpanding = m_requestRole[ExpandedParentsCountRole];
545 const bool willSupportExpanding = roles.contains("expandedParentsCount");
546 if (supportedExpanding && !willSupportExpanding) {
547 // No expanding is supported anymore. Take care to delete all items that have an expansion level
548 // that is not 0 (and hence are part of an expanded item).
549 removeExpandedItems();
550 }
551 }
552
553 m_groups.clear();
554 resetRoles();
555
556 QSetIterator<QByteArray> it(roles);
557 while (it.hasNext()) {
558 const QByteArray &role = it.next();
559 m_requestRole[typeForRole(role)] = true;
560 }
561
562 if (count() > 0) {
563 // Update m_data with the changed requested roles
564 const int maxIndex = count() - 1;
565 for (int i = 0; i <= maxIndex; ++i) {
566 m_itemData[i]->values = retrieveData(m_itemData.at(i)->item, m_itemData.at(i)->parent);
567 }
568
569 Q_EMIT itemsChanged(KItemRangeList() << KItemRange(0, count()), changedRoles);
570 }
571
572 // Clear the 'values' of all filtered items. They will be re-populated with the
573 // correct roles the next time 'values' will be accessed via data(int).
574 QHash<KFileItem, ItemData *>::iterator filteredIt = m_filteredItems.begin();
575 const QHash<KFileItem, ItemData *>::iterator filteredEnd = m_filteredItems.end();
576 while (filteredIt != filteredEnd) {
577 (*filteredIt)->values.clear();
578 ++filteredIt;
579 }
580 }
581
582 QSet<QByteArray> KFileItemModel::roles() const
583 {
584 return m_roles;
585 }
586
587 bool KFileItemModel::setExpanded(int index, bool expanded)
588 {
589 if (!isExpandable(index) || isExpanded(index) == expanded) {
590 return false;
591 }
592
593 QHash<QByteArray, QVariant> values;
594 values.insert(sharedValue("isExpanded"), expanded);
595 if (!setData(index, values)) {
596 return false;
597 }
598
599 const KFileItem item = m_itemData.at(index)->item;
600 const QUrl url = item.url();
601 const QUrl targetUrl = item.targetUrl();
602 if (expanded) {
603 m_expandedDirs.insert(targetUrl, url);
604 m_dirLister->openUrl(url, KDirLister::Keep);
605
606 const QVariantList previouslyExpandedChildren = m_itemData.at(index)->values.value("previouslyExpandedChildren").value<QVariantList>();
607 for (const QVariant &var : previouslyExpandedChildren) {
608 m_urlsToExpand.insert(var.toUrl());
609 }
610 } else {
611 // Note that there might be (indirect) children of the folder which is to be collapsed in
612 // m_pendingItemsToInsert. To prevent that they will be inserted into the model later,
613 // possibly without a parent, which might result in a crash, we insert all pending items
614 // right now. All new items which would be without a parent will then be removed.
615 dispatchPendingItemsToInsert();
616
617 // Check if the index of the collapsed folder has changed. If that is the case, then items
618 // were inserted before the collapsed folder, and its index needs to be updated.
619 if (m_itemData.at(index)->item != item) {
620 index = this->index(item);
621 }
622
623 m_expandedDirs.remove(targetUrl);
624 m_dirLister->stop(url);
625 m_dirLister->forgetDirs(url);
626
627 const int parentLevel = expandedParentsCount(index);
628 const int itemCount = m_itemData.count();
629 const int firstChildIndex = index + 1;
630
631 QVariantList expandedChildren;
632
633 int childIndex = firstChildIndex;
634 while (childIndex < itemCount && expandedParentsCount(childIndex) > parentLevel) {
635 ItemData *itemData = m_itemData.at(childIndex);
636 if (itemData->values.value("isExpanded").toBool()) {
637 const QUrl targetUrl = itemData->item.targetUrl();
638 const QUrl url = itemData->item.url();
639 m_expandedDirs.remove(targetUrl);
640 m_dirLister->stop(url); // TODO: try to unit-test this, see https://bugs.kde.org/show_bug.cgi?id=332102#c11
641 m_dirLister->forgetDirs(url);
642 expandedChildren.append(targetUrl);
643 }
644 ++childIndex;
645 }
646 const int childrenCount = childIndex - firstChildIndex;
647
648 removeFilteredChildren(KItemRangeList() << KItemRange(index, 1 + childrenCount));
649 removeItems(KItemRangeList() << KItemRange(firstChildIndex, childrenCount), DeleteItemData);
650
651 m_itemData.at(index)->values.insert("previouslyExpandedChildren", expandedChildren);
652 }
653
654 return true;
655 }
656
657 bool KFileItemModel::isExpanded(int index) const
658 {
659 if (index >= 0 && index < count()) {
660 return m_itemData.at(index)->values.value("isExpanded").toBool();
661 }
662 return false;
663 }
664
665 bool KFileItemModel::isExpandable(int index) const
666 {
667 if (index >= 0 && index < count()) {
668 // Call data (instead of accessing m_itemData directly)
669 // to ensure that the value is initialized.
670 return data(index).value("isExpandable").toBool();
671 }
672 return false;
673 }
674
675 int KFileItemModel::expandedParentsCount(int index) const
676 {
677 if (index >= 0 && index < count()) {
678 return expandedParentsCount(m_itemData.at(index));
679 }
680 return 0;
681 }
682
683 QSet<QUrl> KFileItemModel::expandedDirectories() const
684 {
685 QSet<QUrl> result;
686 const auto dirs = m_expandedDirs;
687 for (const auto &dir : dirs) {
688 result.insert(dir);
689 }
690 return result;
691 }
692
693 void KFileItemModel::restoreExpandedDirectories(const QSet<QUrl> &urls)
694 {
695 m_urlsToExpand = urls;
696 }
697
698 void KFileItemModel::expandParentDirectories(const QUrl &url)
699 {
700 // Assure that each sub-path of the URL that should be
701 // expanded is added to m_urlsToExpand. KDirLister
702 // does not care whether the parent-URL has already been
703 // expanded.
704 QUrl urlToExpand = m_dirLister->url();
705 const int pos = urlToExpand.path().length();
706
707 // first subdir can be empty, if m_dirLister->url().path() does not end with '/'
708 // this happens if baseUrl is not root but a home directory, see FoldersPanel,
709 // so using QString::SkipEmptyParts
710 const QStringList subDirs = url.path().mid(pos).split(QDir::separator(), Qt::SkipEmptyParts);
711 for (int i = 0; i < subDirs.count() - 1; ++i) {
712 QString path = urlToExpand.path();
713 if (!path.endsWith(QLatin1Char('/'))) {
714 path.append(QLatin1Char('/'));
715 }
716 urlToExpand.setPath(path + subDirs.at(i));
717 m_urlsToExpand.insert(urlToExpand);
718 }
719
720 // KDirLister::open() must called at least once to trigger an initial
721 // loading. The pending URLs that must be restored are handled
722 // in slotCompleted().
723 QSetIterator<QUrl> it2(m_urlsToExpand);
724 while (it2.hasNext()) {
725 const int idx = index(it2.next());
726 if (idx >= 0 && !isExpanded(idx)) {
727 setExpanded(idx, true);
728 break;
729 }
730 }
731 }
732
733 void KFileItemModel::setNameFilter(const QString &nameFilter)
734 {
735 if (m_filter.pattern() != nameFilter) {
736 dispatchPendingItemsToInsert();
737 m_filter.setPattern(nameFilter);
738 applyFilters();
739 }
740 }
741
742 QString KFileItemModel::nameFilter() const
743 {
744 return m_filter.pattern();
745 }
746
747 void KFileItemModel::setMimeTypeFilters(const QStringList &filters)
748 {
749 if (m_filter.mimeTypes() != filters) {
750 dispatchPendingItemsToInsert();
751 m_filter.setMimeTypes(filters);
752 applyFilters();
753 }
754 }
755
756 QStringList KFileItemModel::mimeTypeFilters() const
757 {
758 return m_filter.mimeTypes();
759 }
760
761 void KFileItemModel::setExcludeMimeTypeFilter(const QStringList &filters)
762 {
763 if (m_filter.excludeMimeTypes() != filters) {
764 dispatchPendingItemsToInsert();
765 m_filter.setExcludeMimeTypes(filters);
766 applyFilters();
767 }
768 }
769
770 QStringList KFileItemModel::excludeMimeTypeFilter() const
771 {
772 return m_filter.excludeMimeTypes();
773 }
774
775 void KFileItemModel::applyFilters()
776 {
777 // ===STEP 1===
778 // Check which previously shown items from m_itemData must now get
779 // hidden and hence moved from m_itemData into m_filteredItems.
780
781 QList<int> newFilteredIndexes; // This structure is good for prepending. We will want an ascending sorted Container at the end, this will do fine.
782
783 // This pointer will refer to the next confirmed shown item from the point of
784 // view of the current "itemData" in the upcoming "for" loop.
785 ItemData *itemShownBelow = nullptr;
786
787 // We will iterate backwards because it's convenient to know beforehand if the item just below is its child or not.
788 for (int index = m_itemData.count() - 1; index >= 0; --index) {
789 ItemData *itemData = m_itemData.at(index);
790
791 if (m_filter.matches(itemData->item) || (itemShownBelow && itemShownBelow->parent == itemData)) {
792 // We could've entered here for two reasons:
793 // 1. This item passes the filter itself
794 // 2. This is an expanded folder that doesn't pass the filter but sees a filter-passing child just below
795
796 // So this item must remain shown.
797 // Lets register this item as the next shown item from the point of view of the next iteration of this for loop
798 itemShownBelow = itemData;
799 } else {
800 // We hide this item for now, however, for expanded folders this is not final:
801 // 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
802 newFilteredIndexes.prepend(index);
803 m_filteredItems.insert(itemData->item, itemData);
804 // indexShownBelow doesn't get updated since this item will be hidden
805 }
806 }
807
808 // This will remove the newly filtered items from m_itemData
809 removeItems(KItemRangeList::fromSortedContainer(newFilteredIndexes), KeepItemData);
810
811 // ===STEP 2===
812 // Check which hidden items from m_filteredItems should
813 // become visible again and hence moved from m_filteredItems back into m_itemData.
814
815 QList<ItemData *> newVisibleItems;
816
817 QHash<KFileItem, ItemData *> ancestorsOfNewVisibleItems; // We will make sure these also become visible in step 3.
818
819 QHash<KFileItem, ItemData *>::iterator it = m_filteredItems.begin();
820 while (it != m_filteredItems.end()) {
821 if (m_filter.matches(it.key())) {
822 newVisibleItems.append(it.value());
823
824 // If this is a child of an expanded folder, we must make sure that its whole parental chain will also be shown.
825 // We will go up through its parental chain until we either:
826 // 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
827 // nullptr. or 2 - we reach an unfiltered parent or a previously discovered ancestor.
828 for (ItemData *parent = it.value()->parent; parent && !ancestorsOfNewVisibleItems.contains(parent->item) && m_filteredItems.contains(parent->item);
829 parent = parent->parent) {
830 // We wish we could remove this parent from m_filteredItems right now, but we are iterating over it
831 // and it would mess up the iteration. We will mark it to be removed in step 3.
832 ancestorsOfNewVisibleItems.insert(parent->item, parent);
833 }
834
835 it = m_filteredItems.erase(it);
836 } else {
837 // Item remains filtered for now
838 // However, for expanded folders this is not final, we may discover later that it has unfiltered descendants.
839 ++it;
840 }
841 }
842
843 // ===STEP 3===
844 // Handles the ancestorsOfNewVisibleItems.
845 // Now that we are done iterating through m_filteredItems we can safely move the ancestorsOfNewVisibleItems from m_filteredItems to newVisibleItems.
846 for (it = ancestorsOfNewVisibleItems.begin(); it != ancestorsOfNewVisibleItems.end(); it++) {
847 if (m_filteredItems.remove(it.key())) {
848 // m_filteredItems still contained this ancestor until now so we can be sure that we aren't adding a duplicate ancestor to newVisibleItems.
849 newVisibleItems.append(it.value());
850 }
851 }
852
853 // This will insert the newly discovered unfiltered items into m_itemData
854 insertItems(newVisibleItems);
855 }
856
857 void KFileItemModel::removeFilteredChildren(const KItemRangeList &itemRanges)
858 {
859 if (m_filteredItems.isEmpty() || !m_requestRole[ExpandedParentsCountRole]) {
860 // There are either no filtered items, or it is not possible to expand
861 // folders -> there cannot be any filtered children.
862 return;
863 }
864
865 QSet<ItemData *> parents;
866 for (const KItemRange &range : itemRanges) {
867 for (int index = range.index; index < range.index + range.count; ++index) {
868 parents.insert(m_itemData.at(index));
869 }
870 }
871
872 QHash<KFileItem, ItemData *>::iterator it = m_filteredItems.begin();
873 while (it != m_filteredItems.end()) {
874 if (parents.contains(it.value()->parent)) {
875 delete it.value();
876 it = m_filteredItems.erase(it);
877 } else {
878 ++it;
879 }
880 }
881 }
882
883 QList<KFileItemModel::RoleInfo> KFileItemModel::rolesInformation()
884 {
885 static QList<RoleInfo> rolesInfo;
886 if (rolesInfo.isEmpty()) {
887 int count = 0;
888 const RoleInfoMap *map = rolesInfoMap(count);
889 for (int i = 0; i < count; ++i) {
890 if (map[i].roleType != NoRole) {
891 RoleInfo info;
892 info.role = map[i].role;
893 info.translation = map[i].roleTranslation.toString();
894 if (!map[i].groupTranslation.isEmpty()) {
895 info.group = map[i].groupTranslation.toString();
896 } else {
897 // For top level roles, groupTranslation is 0. We must make sure that
898 // info.group is an empty string then because the code that generates
899 // menus tries to put the actions into sub menus otherwise.
900 info.group = QString();
901 }
902 info.requiresBaloo = map[i].requiresBaloo;
903 info.requiresIndexer = map[i].requiresIndexer;
904 if (!map[i].tooltipTranslation.isEmpty()) {
905 info.tooltip = map[i].tooltipTranslation.toString();
906 } else {
907 info.tooltip = QString();
908 }
909 rolesInfo.append(info);
910 }
911 }
912 }
913
914 return rolesInfo;
915 }
916
917 void KFileItemModel::onGroupedSortingChanged(bool current)
918 {
919 Q_UNUSED(current)
920 m_groups.clear();
921 }
922
923 void KFileItemModel::onSortRoleChanged(const QByteArray &current, const QByteArray &previous, bool resortItems)
924 {
925 Q_UNUSED(previous)
926 m_sortRole = typeForRole(current);
927
928 if (!m_requestRole[m_sortRole]) {
929 QSet<QByteArray> newRoles = m_roles;
930 newRoles << current;
931 setRoles(newRoles);
932 }
933
934 if (resortItems) {
935 resortAllItems();
936 }
937 }
938
939 void KFileItemModel::onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
940 {
941 Q_UNUSED(current)
942 Q_UNUSED(previous)
943 resortAllItems();
944 }
945
946 void KFileItemModel::loadSortingSettings()
947 {
948 using Choice = GeneralSettings::EnumSortingChoice;
949 switch (GeneralSettings::sortingChoice()) {
950 case Choice::NaturalSorting:
951 m_naturalSorting = true;
952 m_collator.setCaseSensitivity(Qt::CaseInsensitive);
953 break;
954 case Choice::CaseSensitiveSorting:
955 m_naturalSorting = false;
956 m_collator.setCaseSensitivity(Qt::CaseSensitive);
957 break;
958 case Choice::CaseInsensitiveSorting:
959 m_naturalSorting = false;
960 m_collator.setCaseSensitivity(Qt::CaseInsensitive);
961 break;
962 default:
963 Q_UNREACHABLE();
964 }
965 // Workaround for bug https://bugreports.qt.io/browse/QTBUG-69361
966 // Force the clean state of QCollator in single thread to avoid thread safety problems in sort
967 m_collator.compare(QString(), QString());
968 }
969
970 void KFileItemModel::resortAllItems()
971 {
972 m_resortAllItemsTimer->stop();
973
974 const int itemCount = count();
975 if (itemCount <= 0) {
976 return;
977 }
978
979 #ifdef KFILEITEMMODEL_DEBUG
980 QElapsedTimer timer;
981 timer.start();
982 qCDebug(DolphinDebug) << "===========================================================";
983 qCDebug(DolphinDebug) << "Resorting" << itemCount << "items";
984 #endif
985
986 // Remember the order of the current URLs so
987 // that it can be determined which indexes have
988 // been moved because of the resorting.
989 QList<QUrl> oldUrls;
990 oldUrls.reserve(itemCount);
991 for (const ItemData *itemData : std::as_const(m_itemData)) {
992 oldUrls.append(itemData->item.url());
993 }
994
995 m_items.clear();
996 m_items.reserve(itemCount);
997
998 // Resort the items
999 sort(m_itemData.begin(), m_itemData.end());
1000 for (int i = 0; i < itemCount; ++i) {
1001 m_items.insert(m_itemData.at(i)->item.url(), i);
1002 }
1003
1004 // Determine the first index that has been moved.
1005 int firstMovedIndex = 0;
1006 while (firstMovedIndex < itemCount && firstMovedIndex == m_items.value(oldUrls.at(firstMovedIndex))) {
1007 ++firstMovedIndex;
1008 }
1009
1010 const bool itemsHaveMoved = firstMovedIndex < itemCount;
1011 if (itemsHaveMoved) {
1012 m_groups.clear();
1013
1014 int lastMovedIndex = itemCount - 1;
1015 while (lastMovedIndex > firstMovedIndex && lastMovedIndex == m_items.value(oldUrls.at(lastMovedIndex))) {
1016 --lastMovedIndex;
1017 }
1018
1019 Q_ASSERT(firstMovedIndex <= lastMovedIndex);
1020
1021 // Create a list movedToIndexes, which has the property that
1022 // movedToIndexes[i] is the new index of the item with the old index
1023 // firstMovedIndex + i.
1024 const int movedItemsCount = lastMovedIndex - firstMovedIndex + 1;
1025 QList<int> movedToIndexes;
1026 movedToIndexes.reserve(movedItemsCount);
1027 for (int i = firstMovedIndex; i <= lastMovedIndex; ++i) {
1028 const int newIndex = m_items.value(oldUrls.at(i));
1029 movedToIndexes.append(newIndex);
1030 }
1031
1032 Q_EMIT itemsMoved(KItemRange(firstMovedIndex, movedItemsCount), movedToIndexes);
1033 } else if (groupedSorting()) {
1034 // The groups might have changed even if the order of the items has not.
1035 const QList<QPair<int, QVariant>> oldGroups = m_groups;
1036 m_groups.clear();
1037 if (groups() != oldGroups) {
1038 Q_EMIT groupsChanged();
1039 }
1040 }
1041
1042 #ifdef KFILEITEMMODEL_DEBUG
1043 qCDebug(DolphinDebug) << "[TIME] Resorting of" << itemCount << "items:" << timer.elapsed();
1044 #endif
1045 }
1046
1047 void KFileItemModel::slotCompleted()
1048 {
1049 m_maximumUpdateIntervalTimer->stop();
1050 dispatchPendingItemsToInsert();
1051
1052 if (!m_urlsToExpand.isEmpty()) {
1053 // Try to find a URL that can be expanded.
1054 // Note that the parent folder must be expanded before any of its subfolders become visible.
1055 // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
1056 // -> we expand the first visible URL we find in m_restoredExpandedUrls.
1057 // Iterate over a const copy because items are deleted and inserted within the loop
1058 const auto urlsToExpand = m_urlsToExpand;
1059 for (const QUrl &url : urlsToExpand) {
1060 const int indexForUrl = index(url);
1061 if (indexForUrl >= 0) {
1062 m_urlsToExpand.remove(url);
1063 if (setExpanded(indexForUrl, true)) {
1064 // The dir lister has been triggered. This slot will be called
1065 // again after the directory has been expanded.
1066 return;
1067 }
1068 }
1069 }
1070
1071 // None of the URLs in m_restoredExpandedUrls could be found in the model. This can happen
1072 // if these URLs have been deleted in the meantime.
1073 m_urlsToExpand.clear();
1074 }
1075
1076 Q_EMIT directoryLoadingCompleted();
1077 }
1078
1079 void KFileItemModel::slotCanceled()
1080 {
1081 m_maximumUpdateIntervalTimer->stop();
1082 dispatchPendingItemsToInsert();
1083
1084 Q_EMIT directoryLoadingCanceled();
1085 }
1086
1087 void KFileItemModel::slotItemsAdded(const QUrl &directoryUrl, const KFileItemList &items)
1088 {
1089 Q_ASSERT(!items.isEmpty());
1090
1091 const QUrl parentUrl = m_expandedDirs.value(directoryUrl, directoryUrl.adjusted(QUrl::StripTrailingSlash));
1092
1093 if (m_requestRole[ExpandedParentsCountRole]) {
1094 // If the expanding of items is enabled, the call
1095 // dirLister->openUrl(url, KDirLister::Keep) in KFileItemModel::setExpanded()
1096 // might result in emitting the same items twice due to the Keep-parameter.
1097 // This case happens if an item gets expanded, collapsed and expanded again
1098 // before the items could be loaded for the first expansion.
1099 if (index(items.first().url()) >= 0) {
1100 // The items are already part of the model.
1101 return;
1102 }
1103
1104 if (directoryUrl != directory()) {
1105 // To be able to compare whether the new items may be inserted as children
1106 // of a parent item the pending items must be added to the model first.
1107 dispatchPendingItemsToInsert();
1108 }
1109
1110 // KDirLister keeps the children of items that got expanded once even if
1111 // they got collapsed again with KFileItemModel::setExpanded(false). So it must be
1112 // checked whether the parent for new items is still expanded.
1113 const int parentIndex = index(parentUrl);
1114 if (parentIndex >= 0 && !m_itemData[parentIndex]->values.value("isExpanded").toBool()) {
1115 // The parent is not expanded.
1116 return;
1117 }
1118 }
1119
1120 const QList<ItemData *> itemDataList = createItemDataList(parentUrl, items);
1121
1122 if (!m_filter.hasSetFilters()) {
1123 m_pendingItemsToInsert.append(itemDataList);
1124 } else {
1125 QSet<ItemData *> parentsToEnsureVisible;
1126
1127 // The name or type filter is active. Hide filtered items
1128 // before inserting them into the model and remember
1129 // the filtered items in m_filteredItems.
1130 for (ItemData *itemData : itemDataList) {
1131 if (m_filter.matches(itemData->item)) {
1132 m_pendingItemsToInsert.append(itemData);
1133 if (itemData->parent) {
1134 parentsToEnsureVisible.insert(itemData->parent);
1135 }
1136 } else {
1137 m_filteredItems.insert(itemData->item, itemData);
1138 }
1139 }
1140
1141 // Entire parental chains must be shown
1142 for (ItemData *parent : parentsToEnsureVisible) {
1143 for (; parent && m_filteredItems.remove(parent->item); parent = parent->parent) {
1144 m_pendingItemsToInsert.append(parent);
1145 }
1146 }
1147 }
1148
1149 if (!m_maximumUpdateIntervalTimer->isActive()) {
1150 // Assure that items get dispatched if no completed() or canceled() signal is
1151 // emitted during the maximum update interval.
1152 m_maximumUpdateIntervalTimer->start();
1153 }
1154
1155 Q_EMIT fileItemsChanged({KFileItem(directoryUrl)});
1156 }
1157
1158 int KFileItemModel::filterChildlessParents(KItemRangeList &removedItemRanges, const QSet<ItemData *> &parentsToEnsureVisible)
1159 {
1160 int filteredParentsCount = 0;
1161 // The childless parents not yet removed will always be right above the start of a removed range.
1162 // We iterate backwards to ensure the deepest folders are processed before their parents
1163 for (int i = removedItemRanges.size() - 1; i >= 0; i--) {
1164 KItemRange itemRange = removedItemRanges.at(i);
1165 const ItemData *const firstInRange = m_itemData.at(itemRange.index);
1166 ItemData *itemAbove = itemRange.index - 1 >= 0 ? m_itemData.at(itemRange.index - 1) : nullptr;
1167 const ItemData *const itemBelow = itemRange.index + itemRange.count < m_itemData.count() ? m_itemData.at(itemRange.index + itemRange.count) : nullptr;
1168
1169 if (itemAbove && firstInRange->parent == itemAbove && !m_filter.matches(itemAbove->item) && (!itemBelow || itemBelow->parent != itemAbove)
1170 && !parentsToEnsureVisible.contains(itemAbove)) {
1171 // The item above exists, is the parent, doesn't pass the filter, does not belong to parentsToEnsureVisible
1172 // and this deleted range covers all of its descendents, so none will be left.
1173 m_filteredItems.insert(itemAbove->item, itemAbove);
1174 // This range's starting index will be extended to include the parent above:
1175 --itemRange.index;
1176 ++itemRange.count;
1177 ++filteredParentsCount;
1178 KItemRange previousRange = i > 0 ? removedItemRanges.at(i - 1) : KItemRange();
1179 // We must check if this caused the range to touch the previous range, if that's the case they shall be merged
1180 if (i > 0 && previousRange.index + previousRange.count == itemRange.index) {
1181 previousRange.count += itemRange.count;
1182 removedItemRanges.replace(i - 1, previousRange);
1183 removedItemRanges.removeAt(i);
1184 } else {
1185 removedItemRanges.replace(i, itemRange);
1186 // We must revisit this range in the next iteration since its starting index changed
1187 ++i;
1188 }
1189 }
1190 }
1191 return filteredParentsCount;
1192 }
1193
1194 void KFileItemModel::slotItemsDeleted(const KFileItemList &items)
1195 {
1196 dispatchPendingItemsToInsert();
1197
1198 QVector<int> indexesToRemove;
1199 indexesToRemove.reserve(items.count());
1200 KFileItemList dirsChanged;
1201
1202 const auto currentDir = directory();
1203
1204 for (const KFileItem &item : items) {
1205 if (item.url() == currentDir) {
1206 // #473377: Delay emitting currentDirectoryRemoved() to avoid modifying KCoreDirLister
1207 // before KCoreDirListerCache::deleteDir() returns.
1208 QTimer::singleShot(0, this, [this] {
1209 Q_EMIT currentDirectoryRemoved();
1210 });
1211
1212 return;
1213 }
1214
1215 const int indexForItem = index(item);
1216 if (indexForItem >= 0) {
1217 indexesToRemove.append(indexForItem);
1218 } else {
1219 // Probably the item has been filtered.
1220 QHash<KFileItem, ItemData *>::iterator it = m_filteredItems.find(item);
1221 if (it != m_filteredItems.end()) {
1222 delete it.value();
1223 m_filteredItems.erase(it);
1224 }
1225 }
1226
1227 QUrl parentUrl = item.url().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
1228 if (dirsChanged.findByUrl(parentUrl).isNull()) {
1229 dirsChanged << KFileItem(parentUrl);
1230 }
1231 }
1232
1233 std::sort(indexesToRemove.begin(), indexesToRemove.end());
1234
1235 if (m_requestRole[ExpandedParentsCountRole] && !m_expandedDirs.isEmpty()) {
1236 // Assure that removing a parent item also results in removing all children
1237 QVector<int> indexesToRemoveWithChildren;
1238 indexesToRemoveWithChildren.reserve(m_itemData.count());
1239
1240 const int itemCount = m_itemData.count();
1241 for (int index : std::as_const(indexesToRemove)) {
1242 indexesToRemoveWithChildren.append(index);
1243
1244 const int parentLevel = expandedParentsCount(index);
1245 int childIndex = index + 1;
1246 while (childIndex < itemCount && expandedParentsCount(childIndex) > parentLevel) {
1247 indexesToRemoveWithChildren.append(childIndex);
1248 ++childIndex;
1249 }
1250 }
1251
1252 indexesToRemove = indexesToRemoveWithChildren;
1253 }
1254
1255 KItemRangeList itemRanges = KItemRangeList::fromSortedContainer(indexesToRemove);
1256 removeFilteredChildren(itemRanges);
1257
1258 // This call will update itemRanges to include the childless parents that have been filtered.
1259 const int filteredParentsCount = filterChildlessParents(itemRanges);
1260
1261 // If any childless parents were filtered, then itemRanges got updated and now contains items that were really deleted
1262 // mixed with expanded folders that are just being filtered out.
1263 // If that's the case, we pass 'DeleteItemDataIfUnfiltered' as a hint
1264 // so removeItems() will check m_filteredItems to differentiate which is which.
1265 removeItems(itemRanges, filteredParentsCount > 0 ? DeleteItemDataIfUnfiltered : DeleteItemData);
1266
1267 Q_EMIT fileItemsChanged(dirsChanged);
1268 }
1269
1270 void KFileItemModel::slotRefreshItems(const QList<QPair<KFileItem, KFileItem>> &items)
1271 {
1272 Q_ASSERT(!items.isEmpty());
1273 #ifdef KFILEITEMMODEL_DEBUG
1274 qCDebug(DolphinDebug) << "Refreshing" << items.count() << "items";
1275 #endif
1276
1277 // Get the indexes of all items that have been refreshed
1278 QList<int> indexes;
1279 indexes.reserve(items.count());
1280
1281 QSet<QByteArray> changedRoles;
1282 KFileItemList changedFiles;
1283
1284 // Contains the indexes of the currently visible items
1285 // that should get hidden and hence moved to m_filteredItems.
1286 QVector<int> newFilteredIndexes;
1287
1288 // Contains currently hidden items that should
1289 // get visible and hence removed from m_filteredItems
1290 QList<ItemData *> newVisibleItems;
1291
1292 QListIterator<QPair<KFileItem, KFileItem>> it(items);
1293
1294 while (it.hasNext()) {
1295 const QPair<KFileItem, KFileItem> &itemPair = it.next();
1296 const KFileItem &oldItem = itemPair.first;
1297 const KFileItem &newItem = itemPair.second;
1298 const int indexForItem = index(oldItem);
1299 const bool newItemMatchesFilter = m_filter.matches(newItem);
1300 if (indexForItem >= 0) {
1301 m_itemData[indexForItem]->item = newItem;
1302
1303 // Keep old values as long as possible if they could not retrieved synchronously yet.
1304 // The update of the values will be done asynchronously by KFileItemModelRolesUpdater.
1305 ItemData *const itemData = m_itemData.at(indexForItem);
1306 QHashIterator<QByteArray, QVariant> it(retrieveData(newItem, itemData->parent));
1307 while (it.hasNext()) {
1308 it.next();
1309 const QByteArray &role = it.key();
1310 if (itemData->values.value(role) != it.value()) {
1311 itemData->values.insert(role, it.value());
1312 changedRoles.insert(role);
1313 }
1314 }
1315
1316 m_items.remove(oldItem.url());
1317 // We must maintain m_items consistent with m_itemData for now, this very loop is using it.
1318 // We leave it to be cleared by removeItems() later, when m_itemData actually gets updated.
1319 m_items.insert(newItem.url(), indexForItem);
1320 if (newItemMatchesFilter
1321 || (itemData->values.value("isExpanded").toBool()
1322 && (indexForItem + 1 < m_itemData.count() && m_itemData.at(indexForItem + 1)->parent == itemData))) {
1323 // We are lenient with expanded folders that originally had visible children.
1324 // If they become childless now they will be caught by filterChildlessParents()
1325 changedFiles.append(newItem);
1326 indexes.append(indexForItem);
1327 } else {
1328 newFilteredIndexes.append(indexForItem);
1329 m_filteredItems.insert(newItem, itemData);
1330 }
1331 } else {
1332 // Check if 'oldItem' is one of the filtered items.
1333 QHash<KFileItem, ItemData *>::iterator it = m_filteredItems.find(oldItem);
1334 if (it != m_filteredItems.end()) {
1335 ItemData *const itemData = it.value();
1336 itemData->item = newItem;
1337
1338 // The data stored in 'values' might have changed. Therefore, we clear
1339 // 'values' and re-populate it the next time it is requested via data(int).
1340 // Before clearing, we must remember if it was expanded and the expanded parents count,
1341 // otherwise these states would be lost. The data() method will deal with this special case.
1342 const bool isExpanded = itemData->values.value("isExpanded").toBool();
1343 bool hasExpandedParentsCount = false;
1344 const int expandedParentsCount = itemData->values.value("expandedParentsCount").toInt(&hasExpandedParentsCount);
1345 itemData->values.clear();
1346 if (isExpanded) {
1347 itemData->values.insert("isExpanded", true);
1348 if (hasExpandedParentsCount) {
1349 itemData->values.insert("expandedParentsCount", expandedParentsCount);
1350 }
1351 }
1352
1353 m_filteredItems.erase(it);
1354 if (newItemMatchesFilter) {
1355 newVisibleItems.append(itemData);
1356 } else {
1357 m_filteredItems.insert(newItem, itemData);
1358 }
1359 }
1360 }
1361 }
1362
1363 std::sort(newFilteredIndexes.begin(), newFilteredIndexes.end());
1364
1365 // We must keep track of parents of new visible items since they must be shown no matter what
1366 // They will be considered "immune" to filterChildlessParents()
1367 QSet<ItemData *> parentsToEnsureVisible;
1368
1369 for (ItemData *item : newVisibleItems) {
1370 for (ItemData *parent = item->parent; parent && !parentsToEnsureVisible.contains(parent); parent = parent->parent) {
1371 parentsToEnsureVisible.insert(parent);
1372 }
1373 }
1374 for (ItemData *parent : parentsToEnsureVisible) {
1375 // We make sure they are all unfiltered.
1376 if (m_filteredItems.remove(parent->item)) {
1377 // If it is being unfiltered now, we mark it to be inserted by appending it to newVisibleItems
1378 newVisibleItems.append(parent);
1379 // It could be in newFilteredIndexes, we must remove it if it's there:
1380 const int parentIndex = index(parent->item);
1381 if (parentIndex >= 0) {
1382 QVector<int>::iterator it = std::lower_bound(newFilteredIndexes.begin(), newFilteredIndexes.end(), parentIndex);
1383 if (it != newFilteredIndexes.end() && *it == parentIndex) {
1384 newFilteredIndexes.erase(it);
1385 }
1386 }
1387 }
1388 }
1389
1390 KItemRangeList removedRanges = KItemRangeList::fromSortedContainer(newFilteredIndexes);
1391
1392 // This call will update itemRanges to include the childless parents that have been filtered.
1393 filterChildlessParents(removedRanges, parentsToEnsureVisible);
1394
1395 removeItems(removedRanges, KeepItemData);
1396
1397 // Show previously hidden items that should get visible
1398 insertItems(newVisibleItems);
1399
1400 // Final step: we will emit 'itemsChanged' and 'fileItemsChanged' signals and trigger the asynchronous re-sorting logic.
1401
1402 // If the changed items have been created recently, they might not be in m_items yet.
1403 // In that case, the list 'indexes' might be empty.
1404 if (indexes.isEmpty()) {
1405 return;
1406 }
1407
1408 if (newVisibleItems.count() > 0 || removedRanges.count() > 0) {
1409 // The original indexes have changed and are now worthless since items were removed and/or inserted.
1410 indexes.clear();
1411 // m_items is not yet rebuilt at this point, so we use our own means to resolve the new indexes.
1412 const QSet<const KFileItem> changedFilesSet(changedFiles.cbegin(), changedFiles.cend());
1413 for (int i = 0; i < m_itemData.count(); i++) {
1414 if (changedFilesSet.contains(m_itemData.at(i)->item)) {
1415 indexes.append(i);
1416 }
1417 }
1418 } else {
1419 std::sort(indexes.begin(), indexes.end());
1420 }
1421
1422 // Extract the item-ranges out of the changed indexes
1423 const KItemRangeList itemRangeList = KItemRangeList::fromSortedContainer(indexes);
1424 emitItemsChangedAndTriggerResorting(itemRangeList, changedRoles);
1425
1426 Q_EMIT fileItemsChanged(changedFiles);
1427 }
1428
1429 void KFileItemModel::slotClear()
1430 {
1431 #ifdef KFILEITEMMODEL_DEBUG
1432 qCDebug(DolphinDebug) << "Clearing all items";
1433 #endif
1434
1435 qDeleteAll(m_filteredItems);
1436 m_filteredItems.clear();
1437 m_groups.clear();
1438
1439 m_maximumUpdateIntervalTimer->stop();
1440 m_resortAllItemsTimer->stop();
1441
1442 qDeleteAll(m_pendingItemsToInsert);
1443 m_pendingItemsToInsert.clear();
1444
1445 const int removedCount = m_itemData.count();
1446 if (removedCount > 0) {
1447 qDeleteAll(m_itemData);
1448 m_itemData.clear();
1449 m_items.clear();
1450 Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(0, removedCount));
1451 }
1452
1453 m_expandedDirs.clear();
1454 }
1455
1456 void KFileItemModel::slotSortingChoiceChanged()
1457 {
1458 loadSortingSettings();
1459 resortAllItems();
1460 }
1461
1462 void KFileItemModel::dispatchPendingItemsToInsert()
1463 {
1464 if (!m_pendingItemsToInsert.isEmpty()) {
1465 insertItems(m_pendingItemsToInsert);
1466 m_pendingItemsToInsert.clear();
1467 }
1468 }
1469
1470 void KFileItemModel::insertItems(QList<ItemData *> &newItems)
1471 {
1472 if (newItems.isEmpty()) {
1473 return;
1474 }
1475
1476 #ifdef KFILEITEMMODEL_DEBUG
1477 QElapsedTimer timer;
1478 timer.start();
1479 qCDebug(DolphinDebug) << "===========================================================";
1480 qCDebug(DolphinDebug) << "Inserting" << newItems.count() << "items";
1481 #endif
1482
1483 m_groups.clear();
1484 prepareItemsForSorting(newItems);
1485
1486 // Natural sorting of items can be very slow. However, it becomes much faster
1487 // if the input sequence is already mostly sorted. Therefore, we first sort
1488 // 'newItems' according to the QStrings using QString::operator<(), which is quite fast.
1489 if (m_naturalSorting) {
1490 if (m_sortRole == NameRole) {
1491 parallelMergeSort(newItems.begin(), newItems.end(), nameLessThan, QThread::idealThreadCount());
1492 } else if (isRoleValueNatural(m_sortRole)) {
1493 auto lambdaLessThan = [&](const KFileItemModel::ItemData *a, const KFileItemModel::ItemData *b) {
1494 const QByteArray role = roleForType(m_sortRole);
1495 return a->values.value(role).toString() < b->values.value(role).toString();
1496 };
1497 parallelMergeSort(newItems.begin(), newItems.end(), lambdaLessThan, QThread::idealThreadCount());
1498 }
1499 }
1500
1501 sort(newItems.begin(), newItems.end());
1502
1503 #ifdef KFILEITEMMODEL_DEBUG
1504 qCDebug(DolphinDebug) << "[TIME] Sorting:" << timer.elapsed();
1505 #endif
1506
1507 KItemRangeList itemRanges;
1508 const int existingItemCount = m_itemData.count();
1509 const int newItemCount = newItems.count();
1510 const int totalItemCount = existingItemCount + newItemCount;
1511
1512 if (existingItemCount == 0) {
1513 // Optimization for the common special case that there are no
1514 // items in the model yet. Happens, e.g., when entering a folder.
1515 m_itemData = newItems;
1516 itemRanges << KItemRange(0, newItemCount);
1517 } else {
1518 m_itemData.reserve(totalItemCount);
1519 for (int i = existingItemCount; i < totalItemCount; ++i) {
1520 m_itemData.append(nullptr);
1521 }
1522
1523 // We build the new list m_itemData in reverse order to minimize
1524 // the number of moves and guarantee O(N) complexity.
1525 int targetIndex = totalItemCount - 1;
1526 int sourceIndexExistingItems = existingItemCount - 1;
1527 int sourceIndexNewItems = newItemCount - 1;
1528
1529 int rangeCount = 0;
1530
1531 while (sourceIndexNewItems >= 0) {
1532 ItemData *newItem = newItems.at(sourceIndexNewItems);
1533 if (sourceIndexExistingItems >= 0 && lessThan(newItem, m_itemData.at(sourceIndexExistingItems), m_collator)) {
1534 // Move an existing item to its new position. If any new items
1535 // are behind it, push the item range to itemRanges.
1536 if (rangeCount > 0) {
1537 itemRanges << KItemRange(sourceIndexExistingItems + 1, rangeCount);
1538 rangeCount = 0;
1539 }
1540
1541 m_itemData[targetIndex] = m_itemData.at(sourceIndexExistingItems);
1542 --sourceIndexExistingItems;
1543 } else {
1544 // Insert a new item into the list.
1545 ++rangeCount;
1546 m_itemData[targetIndex] = newItem;
1547 --sourceIndexNewItems;
1548 }
1549 --targetIndex;
1550 }
1551
1552 // Push the final item range to itemRanges.
1553 if (rangeCount > 0) {
1554 itemRanges << KItemRange(sourceIndexExistingItems + 1, rangeCount);
1555 }
1556
1557 // Note that itemRanges is still sorted in reverse order.
1558 std::reverse(itemRanges.begin(), itemRanges.end());
1559 }
1560
1561 // The indexes in m_items are not correct anymore. Therefore, we clear m_items.
1562 // It will be re-populated with the updated indices if index(const QUrl&) is called.
1563 m_items.clear();
1564
1565 Q_EMIT itemsInserted(itemRanges);
1566
1567 #ifdef KFILEITEMMODEL_DEBUG
1568 qCDebug(DolphinDebug) << "[TIME] Inserting of" << newItems.count() << "items:" << timer.elapsed();
1569 #endif
1570 }
1571
1572 void KFileItemModel::removeItems(const KItemRangeList &itemRanges, RemoveItemsBehavior behavior)
1573 {
1574 if (itemRanges.isEmpty()) {
1575 return;
1576 }
1577
1578 m_groups.clear();
1579
1580 // Step 1: Remove the items from m_itemData, and free the ItemData.
1581 int removedItemsCount = 0;
1582 for (const KItemRange &range : itemRanges) {
1583 removedItemsCount += range.count;
1584
1585 for (int index = range.index; index < range.index + range.count; ++index) {
1586 if (behavior == DeleteItemData || (behavior == DeleteItemDataIfUnfiltered && !m_filteredItems.contains(m_itemData.at(index)->item))) {
1587 delete m_itemData.at(index);
1588 }
1589
1590 m_itemData[index] = nullptr;
1591 }
1592 }
1593
1594 // Step 2: Remove the ItemData pointers from the list m_itemData.
1595 int target = itemRanges.at(0).index;
1596 int source = itemRanges.at(0).index + itemRanges.at(0).count;
1597 int nextRange = 1;
1598
1599 const int oldItemDataCount = m_itemData.count();
1600 while (source < oldItemDataCount) {
1601 m_itemData[target] = m_itemData[source];
1602 ++target;
1603 ++source;
1604
1605 if (nextRange < itemRanges.count() && source == itemRanges.at(nextRange).index) {
1606 // Skip the items in the next removed range.
1607 source += itemRanges.at(nextRange).count;
1608 ++nextRange;
1609 }
1610 }
1611
1612 m_itemData.erase(m_itemData.end() - removedItemsCount, m_itemData.end());
1613
1614 // The indexes in m_items are not correct anymore. Therefore, we clear m_items.
1615 // It will be re-populated with the updated indices if index(const QUrl&) is called.
1616 m_items.clear();
1617
1618 Q_EMIT itemsRemoved(itemRanges);
1619 }
1620
1621 QList<KFileItemModel::ItemData *> KFileItemModel::createItemDataList(const QUrl &parentUrl, const KFileItemList &items) const
1622 {
1623 if (m_sortRole == TypeRole) {
1624 // Try to resolve the MIME-types synchronously to prevent a reordering of
1625 // the items when sorting by type (per default MIME-types are resolved
1626 // asynchronously by KFileItemModelRolesUpdater).
1627 determineMimeTypes(items, 200);
1628 }
1629
1630 // We search for the parent in m_itemData and then in m_filteredItems if necessary
1631 const int parentIndex = index(parentUrl);
1632 ItemData *parentItem = parentIndex < 0 ? m_filteredItems.value(KFileItem(parentUrl), nullptr) : m_itemData.at(parentIndex);
1633
1634 QList<ItemData *> itemDataList;
1635 itemDataList.reserve(items.count());
1636
1637 for (const KFileItem &item : items) {
1638 ItemData *itemData = new ItemData();
1639 itemData->item = item;
1640 itemData->parent = parentItem;
1641 itemDataList.append(itemData);
1642 }
1643
1644 return itemDataList;
1645 }
1646
1647 void KFileItemModel::prepareItemsForSorting(QList<ItemData *> &itemDataList)
1648 {
1649 switch (m_sortRole) {
1650 case ExtensionRole:
1651 case PermissionsRole:
1652 case OwnerRole:
1653 case GroupRole:
1654 case DestinationRole:
1655 case PathRole:
1656 case DeletionTimeRole:
1657 // These roles can be determined with retrieveData, and they have to be stored
1658 // in the QHash "values" for the sorting.
1659 for (ItemData *itemData : std::as_const(itemDataList)) {
1660 if (itemData->values.isEmpty()) {
1661 itemData->values = retrieveData(itemData->item, itemData->parent);
1662 }
1663 }
1664 break;
1665
1666 case TypeRole:
1667 // At least store the data including the file type for items with known MIME type.
1668 for (ItemData *itemData : std::as_const(itemDataList)) {
1669 if (itemData->values.isEmpty()) {
1670 const KFileItem item = itemData->item;
1671 if (item.isDir() || item.isMimeTypeKnown()) {
1672 itemData->values = retrieveData(itemData->item, itemData->parent);
1673 }
1674 }
1675 }
1676 break;
1677
1678 default:
1679 // The other roles are either resolved by KFileItemModelRolesUpdater
1680 // (this includes the SizeRole for directories), or they do not need
1681 // to be stored in the QHash "values" for sorting because the data can
1682 // be retrieved directly from the KFileItem (NameRole, SizeRole for files,
1683 // DateRole).
1684 break;
1685 }
1686 }
1687
1688 int KFileItemModel::expandedParentsCount(const ItemData *data)
1689 {
1690 // The hash 'values' is only guaranteed to contain the key "expandedParentsCount"
1691 // if the corresponding item is expanded, and it is not a top-level item.
1692 const ItemData *parent = data->parent;
1693 if (parent) {
1694 if (parent->parent) {
1695 Q_ASSERT(parent->values.contains("expandedParentsCount"));
1696 return parent->values.value("expandedParentsCount").toInt() + 1;
1697 } else {
1698 return 1;
1699 }
1700 } else {
1701 return 0;
1702 }
1703 }
1704
1705 void KFileItemModel::removeExpandedItems()
1706 {
1707 QVector<int> indexesToRemove;
1708
1709 const int maxIndex = m_itemData.count() - 1;
1710 for (int i = 0; i <= maxIndex; ++i) {
1711 const ItemData *itemData = m_itemData.at(i);
1712 if (itemData->parent) {
1713 indexesToRemove.append(i);
1714 }
1715 }
1716
1717 removeItems(KItemRangeList::fromSortedContainer(indexesToRemove), DeleteItemData);
1718 m_expandedDirs.clear();
1719
1720 // Also remove all filtered items which have a parent.
1721 QHash<KFileItem, ItemData *>::iterator it = m_filteredItems.begin();
1722 const QHash<KFileItem, ItemData *>::iterator end = m_filteredItems.end();
1723
1724 while (it != end) {
1725 if (it.value()->parent) {
1726 delete it.value();
1727 it = m_filteredItems.erase(it);
1728 } else {
1729 ++it;
1730 }
1731 }
1732 }
1733
1734 void KFileItemModel::emitItemsChangedAndTriggerResorting(const KItemRangeList &itemRanges, const QSet<QByteArray> &changedRoles)
1735 {
1736 Q_EMIT itemsChanged(itemRanges, changedRoles);
1737
1738 // Trigger a resorting if necessary. Note that this can happen even if the sort
1739 // role has not changed at all because the file name can be used as a fallback.
1740 if (changedRoles.contains(sortRole()) || changedRoles.contains(roleForType(NameRole))) {
1741 for (const KItemRange &range : itemRanges) {
1742 bool needsResorting = false;
1743
1744 const int first = range.index;
1745 const int last = range.index + range.count - 1;
1746
1747 // Resorting the model is necessary if
1748 // (a) The first item in the range is "lessThan" its predecessor,
1749 // (b) the successor of the last item is "lessThan" the last item, or
1750 // (c) the internal order of the items in the range is incorrect.
1751 if (first > 0 && lessThan(m_itemData.at(first), m_itemData.at(first - 1), m_collator)) {
1752 needsResorting = true;
1753 } else if (last < count() - 1 && lessThan(m_itemData.at(last + 1), m_itemData.at(last), m_collator)) {
1754 needsResorting = true;
1755 } else {
1756 for (int index = first; index < last; ++index) {
1757 if (lessThan(m_itemData.at(index + 1), m_itemData.at(index), m_collator)) {
1758 needsResorting = true;
1759 break;
1760 }
1761 }
1762 }
1763
1764 if (needsResorting) {
1765 m_resortAllItemsTimer->start();
1766 return;
1767 }
1768 }
1769 }
1770
1771 if (groupedSorting() && changedRoles.contains(sortRole())) {
1772 // The position is still correct, but the groups might have changed
1773 // if the changed item is either the first or the last item in a
1774 // group.
1775 // In principle, we could try to find out if the item really is the
1776 // first or last one in its group and then update the groups
1777 // (possibly with a delayed timer to make sure that we don't
1778 // re-calculate the groups very often if items are updated one by
1779 // one), but starting m_resortAllItemsTimer is easier.
1780 m_resortAllItemsTimer->start();
1781 }
1782 }
1783
1784 void KFileItemModel::resetRoles()
1785 {
1786 for (int i = 0; i < RolesCount; ++i) {
1787 m_requestRole[i] = false;
1788 }
1789 }
1790
1791 KFileItemModel::RoleType KFileItemModel::typeForRole(const QByteArray &role) const
1792 {
1793 static QHash<QByteArray, RoleType> roles;
1794 if (roles.isEmpty()) {
1795 // Insert user visible roles that can be accessed with
1796 // KFileItemModel::roleInformation()
1797 int count = 0;
1798 const RoleInfoMap *map = rolesInfoMap(count);
1799 for (int i = 0; i < count; ++i) {
1800 roles.insert(map[i].role, map[i].roleType);
1801 }
1802
1803 // Insert internal roles (take care to synchronize the implementation
1804 // with KFileItemModel::roleForType() in case if a change is done).
1805 roles.insert("isDir", IsDirRole);
1806 roles.insert("isLink", IsLinkRole);
1807 roles.insert("isHidden", IsHiddenRole);
1808 roles.insert("isExpanded", IsExpandedRole);
1809 roles.insert("isExpandable", IsExpandableRole);
1810 roles.insert("expandedParentsCount", ExpandedParentsCountRole);
1811
1812 Q_ASSERT(roles.count() == RolesCount);
1813 }
1814
1815 return roles.value(role, NoRole);
1816 }
1817
1818 QByteArray KFileItemModel::roleForType(RoleType roleType) const
1819 {
1820 static QHash<RoleType, QByteArray> roles;
1821 if (roles.isEmpty()) {
1822 // Insert user visible roles that can be accessed with
1823 // KFileItemModel::roleInformation()
1824 int count = 0;
1825 const RoleInfoMap *map = rolesInfoMap(count);
1826 for (int i = 0; i < count; ++i) {
1827 roles.insert(map[i].roleType, map[i].role);
1828 }
1829
1830 // Insert internal roles (take care to synchronize the implementation
1831 // with KFileItemModel::typeForRole() in case if a change is done).
1832 roles.insert(IsDirRole, "isDir");
1833 roles.insert(IsLinkRole, "isLink");
1834 roles.insert(IsHiddenRole, "isHidden");
1835 roles.insert(IsExpandedRole, "isExpanded");
1836 roles.insert(IsExpandableRole, "isExpandable");
1837 roles.insert(ExpandedParentsCountRole, "expandedParentsCount");
1838
1839 Q_ASSERT(roles.count() == RolesCount);
1840 };
1841
1842 return roles.value(roleType);
1843 }
1844
1845 QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem &item, const ItemData *parent) const
1846 {
1847 // It is important to insert only roles that are fast to retrieve. E.g.
1848 // KFileItem::iconName() can be very expensive if the MIME-type is unknown
1849 // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater.
1850 QHash<QByteArray, QVariant> data;
1851 data.insert(sharedValue("url"), item.url());
1852
1853 const bool isDir = item.isDir();
1854 if (m_requestRole[IsDirRole] && isDir) {
1855 data.insert(sharedValue("isDir"), true);
1856 }
1857
1858 if (m_requestRole[IsLinkRole] && item.isLink()) {
1859 data.insert(sharedValue("isLink"), true);
1860 }
1861
1862 if (m_requestRole[IsHiddenRole]) {
1863 data.insert(sharedValue("isHidden"), item.isHidden() || item.mimetype() == QStringLiteral("application/x-trash"));
1864 }
1865
1866 if (m_requestRole[NameRole]) {
1867 data.insert(sharedValue("text"), item.text());
1868 }
1869
1870 if (m_requestRole[ExtensionRole] && !isDir) {
1871 // TODO KF6 use KFileItem::suffix 464722
1872 data.insert(sharedValue("extension"), QFileInfo(item.name()).suffix());
1873 }
1874
1875 if (m_requestRole[SizeRole] && !isDir) {
1876 data.insert(sharedValue("size"), item.size());
1877 }
1878
1879 if (m_requestRole[ModificationTimeRole]) {
1880 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1881 // having several thousands of items. Instead read the raw number from UDSEntry directly
1882 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1883 const long long dateTime = item.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
1884 data.insert(sharedValue("modificationtime"), dateTime);
1885 }
1886
1887 if (m_requestRole[CreationTimeRole]) {
1888 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1889 // having several thousands of items. Instead read the raw number from UDSEntry directly
1890 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1891 const long long dateTime = item.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
1892 data.insert(sharedValue("creationtime"), dateTime);
1893 }
1894
1895 if (m_requestRole[AccessTimeRole]) {
1896 // Don't use KFileItem::timeString() or KFileItem::time() as this is too expensive when
1897 // having several thousands of items. Instead read the raw number from UDSEntry directly
1898 // and the formatting of the date-time will be done on-demand by the view when the date will be shown.
1899 const long long dateTime = item.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME, -1);
1900 data.insert(sharedValue("accesstime"), dateTime);
1901 }
1902
1903 if (m_requestRole[PermissionsRole]) {
1904 data.insert(sharedValue("permissions"), QVariantList() << item.permissionsString() << item.permissions());
1905 }
1906
1907 if (m_requestRole[OwnerRole]) {
1908 data.insert(sharedValue("owner"), item.user());
1909 }
1910
1911 if (m_requestRole[GroupRole]) {
1912 data.insert(sharedValue("group"), item.group());
1913 }
1914
1915 if (m_requestRole[DestinationRole]) {
1916 QString destination = item.linkDest();
1917 if (destination.isEmpty()) {
1918 destination = QLatin1Char('-');
1919 }
1920 data.insert(sharedValue("destination"), destination);
1921 }
1922
1923 if (m_requestRole[PathRole]) {
1924 QString path;
1925 if (item.url().scheme() == QLatin1String("trash")) {
1926 path = item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA);
1927 } else {
1928 // For performance reasons cache the home-path in a static QString
1929 // (see QDir::homePath() for more details)
1930 static QString homePath;
1931 if (homePath.isEmpty()) {
1932 homePath = QDir::homePath();
1933 }
1934
1935 path = item.localPath();
1936 if (path.startsWith(homePath)) {
1937 path.replace(0, homePath.length(), QLatin1Char('~'));
1938 }
1939 }
1940
1941 const int index = path.lastIndexOf(item.text());
1942 path = path.mid(0, index - 1);
1943 data.insert(sharedValue("path"), path);
1944 }
1945
1946 if (m_requestRole[DeletionTimeRole]) {
1947 QDateTime deletionTime;
1948 if (item.url().scheme() == QLatin1String("trash")) {
1949 deletionTime = QDateTime::fromString(item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA + 1), Qt::ISODate);
1950 }
1951 data.insert(sharedValue("deletiontime"), deletionTime);
1952 }
1953
1954 if (m_requestRole[IsExpandableRole] && isDir) {
1955 data.insert(sharedValue("isExpandable"), true);
1956 }
1957
1958 if (m_requestRole[ExpandedParentsCountRole]) {
1959 if (parent) {
1960 const int level = expandedParentsCount(parent) + 1;
1961 data.insert(sharedValue("expandedParentsCount"), level);
1962 }
1963 }
1964
1965 if (item.isMimeTypeKnown()) {
1966 QString iconName = item.iconName();
1967 if (!QIcon::hasThemeIcon(iconName)) {
1968 QMimeType mimeType = QMimeDatabase().mimeTypeForName(item.mimetype());
1969 iconName = mimeType.genericIconName();
1970 }
1971
1972 data.insert(sharedValue("iconName"), iconName);
1973
1974 if (m_requestRole[TypeRole]) {
1975 data.insert(sharedValue("type"), item.mimeComment());
1976 }
1977 } else if (m_requestRole[TypeRole] && isDir) {
1978 static const QString folderMimeType = item.mimeComment();
1979 data.insert(sharedValue("type"), folderMimeType);
1980 }
1981
1982 return data;
1983 }
1984
1985 bool KFileItemModel::lessThan(const ItemData *a, const ItemData *b, const QCollator &collator) const
1986 {
1987 int result = 0;
1988
1989 if (a->parent != b->parent) {
1990 const int expansionLevelA = expandedParentsCount(a);
1991 const int expansionLevelB = expandedParentsCount(b);
1992
1993 // If b has a higher expansion level than a, check if a is a parent
1994 // of b, and make sure that both expansion levels are equal otherwise.
1995 for (int i = expansionLevelB; i > expansionLevelA; --i) {
1996 if (b->parent == a) {
1997 return true;
1998 }
1999 b = b->parent;
2000 }
2001
2002 // If a has a higher expansion level than a, check if b is a parent
2003 // of a, and make sure that both expansion levels are equal otherwise.
2004 for (int i = expansionLevelA; i > expansionLevelB; --i) {
2005 if (a->parent == b) {
2006 return false;
2007 }
2008 a = a->parent;
2009 }
2010
2011 Q_ASSERT(expandedParentsCount(a) == expandedParentsCount(b));
2012
2013 // Compare the last parents of a and b which are different.
2014 while (a->parent != b->parent) {
2015 a = a->parent;
2016 b = b->parent;
2017 }
2018 }
2019
2020 // Show hidden files and folders last
2021 if (m_sortHiddenLast) {
2022 const bool isHiddenA = a->item.isHidden();
2023 const bool isHiddenB = b->item.isHidden();
2024 if (isHiddenA && !isHiddenB) {
2025 return false;
2026 } else if (!isHiddenA && isHiddenB) {
2027 return true;
2028 }
2029 }
2030
2031 if (m_sortDirsFirst || (ContentDisplaySettings::directorySizeCount() && m_sortRole == SizeRole)) {
2032 const bool isDirA = a->item.isDir();
2033 const bool isDirB = b->item.isDir();
2034 if (isDirA && !isDirB) {
2035 return true;
2036 } else if (!isDirA && isDirB) {
2037 return false;
2038 }
2039 }
2040
2041 result = sortRoleCompare(a, b, collator);
2042
2043 return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
2044 }
2045
2046 void KFileItemModel::sort(const QList<KFileItemModel::ItemData *>::iterator &begin, const QList<KFileItemModel::ItemData *>::iterator &end) const
2047 {
2048 auto lambdaLessThan = [&](const KFileItemModel::ItemData *a, const KFileItemModel::ItemData *b) {
2049 return lessThan(a, b, m_collator);
2050 };
2051
2052 if (m_sortRole == NameRole || isRoleValueNatural(m_sortRole)) {
2053 // Sorting by string can be expensive, in particular if natural sorting is
2054 // enabled. Use all CPU cores to speed up the sorting process.
2055 static const int numberOfThreads = QThread::idealThreadCount();
2056 parallelMergeSort(begin, end, lambdaLessThan, numberOfThreads);
2057 } else {
2058 // Sorting by other roles is quite fast. Use only one thread to prevent
2059 // problems caused by non-reentrant comparison functions, see
2060 // https://bugs.kde.org/show_bug.cgi?id=312679
2061 mergeSort(begin, end, lambdaLessThan);
2062 }
2063 }
2064
2065 int KFileItemModel::sortRoleCompare(const ItemData *a, const ItemData *b, const QCollator &collator) const
2066 {
2067 // This function must never return 0, because that would break stable
2068 // sorting, which leads to all kinds of bugs.
2069 // See: https://bugs.kde.org/show_bug.cgi?id=433247
2070 // If two items have equal sort values, let the fallbacks at the bottom of
2071 // the function handle it.
2072 const KFileItem &itemA = a->item;
2073 const KFileItem &itemB = b->item;
2074
2075 int result = 0;
2076
2077 switch (m_sortRole) {
2078 case NameRole:
2079 // The name role is handled as default fallback after the switch
2080 break;
2081
2082 case SizeRole: {
2083 if (ContentDisplaySettings::directorySizeCount() && itemA.isDir()) {
2084 // folders first then
2085 // items A and B are folders thanks to lessThan checks
2086 auto valueA = a->values.value("count");
2087 auto valueB = b->values.value("count");
2088 if (valueA.isNull()) {
2089 if (!valueB.isNull()) {
2090 return -1;
2091 }
2092 } else if (valueB.isNull()) {
2093 return +1;
2094 } else {
2095 if (valueA.toLongLong() < valueB.toLongLong()) {
2096 return -1;
2097 } else if (valueA.toLongLong() > valueB.toLongLong()) {
2098 return +1;
2099 }
2100 }
2101 break;
2102 }
2103
2104 KIO::filesize_t sizeA = 0;
2105 if (itemA.isDir()) {
2106 sizeA = a->values.value("size").toULongLong();
2107 } else {
2108 sizeA = itemA.size();
2109 }
2110 KIO::filesize_t sizeB = 0;
2111 if (itemB.isDir()) {
2112 sizeB = b->values.value("size").toULongLong();
2113 } else {
2114 sizeB = itemB.size();
2115 }
2116 if (sizeA < sizeB) {
2117 return -1;
2118 } else if (sizeA > sizeB) {
2119 return +1;
2120 }
2121 break;
2122 }
2123
2124 case ModificationTimeRole: {
2125 const long long dateTimeA = itemA.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
2126 const long long dateTimeB = itemB.entry().numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
2127 if (dateTimeA < dateTimeB) {
2128 return -1;
2129 } else if (dateTimeA > dateTimeB) {
2130 return +1;
2131 }
2132 break;
2133 }
2134
2135 case AccessTimeRole: {
2136 const long long dateTimeA = itemA.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME, -1);
2137 const long long dateTimeB = itemB.entry().numberValue(KIO::UDSEntry::UDS_ACCESS_TIME, -1);
2138 if (dateTimeA < dateTimeB) {
2139 return -1;
2140 } else if (dateTimeA > dateTimeB) {
2141 return +1;
2142 }
2143 break;
2144 }
2145
2146 case CreationTimeRole: {
2147 const long long dateTimeA = itemA.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
2148 const long long dateTimeB = itemB.entry().numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
2149 if (dateTimeA < dateTimeB) {
2150 return -1;
2151 } else if (dateTimeA > dateTimeB) {
2152 return +1;
2153 }
2154 break;
2155 }
2156
2157 case DeletionTimeRole: {
2158 const QDateTime dateTimeA = a->values.value("deletiontime").toDateTime();
2159 const QDateTime dateTimeB = b->values.value("deletiontime").toDateTime();
2160 if (dateTimeA < dateTimeB) {
2161 return -1;
2162 } else if (dateTimeA > dateTimeB) {
2163 return +1;
2164 }
2165 break;
2166 }
2167
2168 case RatingRole:
2169 case WidthRole:
2170 case HeightRole:
2171 case PublisherRole:
2172 case PageCountRole:
2173 case WordCountRole:
2174 case LineCountRole:
2175 case TrackRole:
2176 case ReleaseYearRole: {
2177 result = a->values.value(roleForType(m_sortRole)).toInt() - b->values.value(roleForType(m_sortRole)).toInt();
2178 break;
2179 }
2180
2181 case DimensionsRole: {
2182 const QByteArray role = roleForType(m_sortRole);
2183 const QSize dimensionsA = a->values.value(role).toSize();
2184 const QSize dimensionsB = b->values.value(role).toSize();
2185
2186 if (dimensionsA.width() == dimensionsB.width()) {
2187 result = dimensionsA.height() - dimensionsB.height();
2188 } else {
2189 result = dimensionsA.width() - dimensionsB.width();
2190 }
2191 break;
2192 }
2193
2194 default: {
2195 const QByteArray role = roleForType(m_sortRole);
2196 const QString roleValueA = a->values.value(role).toString();
2197 const QString roleValueB = b->values.value(role).toString();
2198 if (!roleValueA.isEmpty() && roleValueB.isEmpty()) {
2199 return -1;
2200 } else if (roleValueA.isEmpty() && !roleValueB.isEmpty()) {
2201 return +1;
2202 } else if (isRoleValueNatural(m_sortRole)) {
2203 result = stringCompare(roleValueA, roleValueB, collator);
2204 } else {
2205 result = QString::compare(roleValueA, roleValueB);
2206 }
2207 break;
2208 }
2209 }
2210
2211 if (result != 0) {
2212 // The current sort role was sufficient to define an order
2213 return result;
2214 }
2215
2216 // Fallback #1: Compare the text of the items
2217 result = stringCompare(itemA.text(), itemB.text(), collator);
2218 if (result != 0) {
2219 return result;
2220 }
2221
2222 // Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
2223 result = stringCompare(itemA.name(), itemB.name(), collator);
2224 if (result != 0) {
2225 return result;
2226 }
2227
2228 // Fallback #3: It must be assured that the sort order is always unique even if two values have been
2229 // equal. In this case a comparison of the URL is done which is unique in all cases
2230 // within KDirLister.
2231 return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive);
2232 }
2233
2234 int KFileItemModel::stringCompare(const QString &a, const QString &b, const QCollator &collator) const
2235 {
2236 QMutexLocker collatorLock(s_collatorMutex());
2237
2238 if (m_naturalSorting) {
2239 return collator.compare(a, b);
2240 }
2241
2242 const int result = QString::compare(a, b, collator.caseSensitivity());
2243 if (result != 0 || collator.caseSensitivity() == Qt::CaseSensitive) {
2244 // Only return the result, if the strings are not equal. If they are equal by a case insensitive
2245 // comparison, still a deterministic sort order is required. A case sensitive
2246 // comparison is done as fallback.
2247 return result;
2248 }
2249
2250 return QString::compare(a, b, Qt::CaseSensitive);
2251 }
2252
2253 QList<QPair<int, QVariant>> KFileItemModel::nameRoleGroups() const
2254 {
2255 Q_ASSERT(!m_itemData.isEmpty());
2256
2257 const int maxIndex = count() - 1;
2258 QList<QPair<int, QVariant>> groups;
2259
2260 QString groupValue;
2261 QChar firstChar;
2262 for (int i = 0; i <= maxIndex; ++i) {
2263 if (isChildItem(i)) {
2264 continue;
2265 }
2266
2267 const QString name = m_itemData.at(i)->item.text();
2268
2269 // Use the first character of the name as group indication
2270 QChar newFirstChar = name.at(0).toUpper();
2271 if (newFirstChar == QLatin1Char('~') && name.length() > 1) {
2272 newFirstChar = name.at(1).toUpper();
2273 }
2274
2275 if (firstChar != newFirstChar) {
2276 QString newGroupValue;
2277 if (newFirstChar.isLetter()) {
2278 if (m_collator.compare(newFirstChar, QChar(QLatin1Char('A'))) >= 0 && m_collator.compare(newFirstChar, QChar(QLatin1Char('Z'))) <= 0) {
2279 // WARNING! Symbols based on latin 'Z' like 'Z' with acute are treated wrong as non Latin and put in a new group.
2280
2281 // Try to find a matching group in the range 'A' to 'Z'.
2282 static std::vector<QChar> lettersAtoZ;
2283 lettersAtoZ.reserve('Z' - 'A' + 1);
2284 if (lettersAtoZ.empty()) {
2285 for (char c = 'A'; c <= 'Z'; ++c) {
2286 lettersAtoZ.push_back(QLatin1Char(c));
2287 }
2288 }
2289
2290 auto localeAwareLessThan = [this](QChar c1, QChar c2) -> bool {
2291 return m_collator.compare(c1, c2) < 0;
2292 };
2293
2294 std::vector<QChar>::iterator it = std::lower_bound(lettersAtoZ.begin(), lettersAtoZ.end(), newFirstChar, localeAwareLessThan);
2295 if (it != lettersAtoZ.end()) {
2296 if (localeAwareLessThan(newFirstChar, *it)) {
2297 // newFirstChar belongs to the group preceding *it.
2298 // Example: for an umlaut 'A' in the German locale, *it would be 'B' now.
2299 --it;
2300 }
2301 newGroupValue = *it;
2302 }
2303
2304 } else {
2305 // Symbols from non Latin-based scripts
2306 newGroupValue = newFirstChar;
2307 }
2308 } else if (newFirstChar >= QLatin1Char('0') && newFirstChar <= QLatin1Char('9')) {
2309 // Apply group '0 - 9' for any name that starts with a digit
2310 newGroupValue = i18nc("@title:group Groups that start with a digit", "0 - 9");
2311 } else {
2312 newGroupValue = i18nc("@title:group", "Others");
2313 }
2314
2315 if (newGroupValue != groupValue) {
2316 groupValue = newGroupValue;
2317 groups.append(QPair<int, QVariant>(i, newGroupValue));
2318 }
2319
2320 firstChar = newFirstChar;
2321 }
2322 }
2323 return groups;
2324 }
2325
2326 QList<QPair<int, QVariant>> KFileItemModel::sizeRoleGroups() const
2327 {
2328 Q_ASSERT(!m_itemData.isEmpty());
2329
2330 const int maxIndex = count() - 1;
2331 QList<QPair<int, QVariant>> groups;
2332
2333 QString groupValue;
2334 for (int i = 0; i <= maxIndex; ++i) {
2335 if (isChildItem(i)) {
2336 continue;
2337 }
2338
2339 const KFileItem &item = m_itemData.at(i)->item;
2340 KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
2341 QString newGroupValue;
2342 if (!item.isNull() && item.isDir()) {
2343 if (ContentDisplaySettings::directorySizeCount() || m_sortDirsFirst) {
2344 newGroupValue = i18nc("@title:group Size", "Folders");
2345 } else {
2346 fileSize = m_itemData.at(i)->values.value("size").toULongLong();
2347 }
2348 }
2349
2350 if (newGroupValue.isEmpty()) {
2351 if (fileSize < 5 * 1024 * 1024) { // < 5 MB
2352 newGroupValue = i18nc("@title:group Size", "Small");
2353 } else if (fileSize < 10 * 1024 * 1024) { // < 10 MB
2354 newGroupValue = i18nc("@title:group Size", "Medium");
2355 } else {
2356 newGroupValue = i18nc("@title:group Size", "Big");
2357 }
2358 }
2359
2360 if (newGroupValue != groupValue) {
2361 groupValue = newGroupValue;
2362 groups.append(QPair<int, QVariant>(i, newGroupValue));
2363 }
2364 }
2365
2366 return groups;
2367 }
2368
2369 QList<QPair<int, QVariant>> KFileItemModel::timeRoleGroups(const std::function<QDateTime(const ItemData *)> &fileTimeCb) const
2370 {
2371 Q_ASSERT(!m_itemData.isEmpty());
2372
2373 const int maxIndex = count() - 1;
2374 QList<QPair<int, QVariant>> groups;
2375
2376 const QDate currentDate = QDate::currentDate();
2377
2378 QDate previousFileDate;
2379 QString groupValue;
2380 for (int i = 0; i <= maxIndex; ++i) {
2381 if (isChildItem(i)) {
2382 continue;
2383 }
2384
2385 const QDateTime fileTime = fileTimeCb(m_itemData.at(i));
2386 const QDate fileDate = fileTime.date();
2387 if (fileDate == previousFileDate) {
2388 // The current item is in the same group as the previous item
2389 continue;
2390 }
2391 previousFileDate = fileDate;
2392
2393 const int daysDistance = fileDate.daysTo(currentDate);
2394
2395 QString newGroupValue;
2396 if (currentDate.year() == fileDate.year() && currentDate.month() == fileDate.month()) {
2397 switch (daysDistance / 7) {
2398 case 0:
2399 switch (daysDistance) {
2400 case 0:
2401 newGroupValue = i18nc("@title:group Date", "Today");
2402 break;
2403 case 1:
2404 newGroupValue = i18nc("@title:group Date", "Yesterday");
2405 break;
2406 default:
2407 newGroupValue = fileTime.toString(i18nc("@title:group Date: The week day name: dddd", "dddd"));
2408 newGroupValue = i18nc(
2409 "Can be used to script translation of \"dddd\""
2410 "with context @title:group Date",
2411 "%1",
2412 newGroupValue);
2413 }
2414 break;
2415 case 1:
2416 newGroupValue = i18nc("@title:group Date", "One Week Ago");
2417 break;
2418 case 2:
2419 newGroupValue = i18nc("@title:group Date", "Two Weeks Ago");
2420 break;
2421 case 3:
2422 newGroupValue = i18nc("@title:group Date", "Three Weeks Ago");
2423 break;
2424 case 4:
2425 case 5:
2426 newGroupValue = i18nc("@title:group Date", "Earlier this Month");
2427 break;
2428 default:
2429 Q_ASSERT(false);
2430 }
2431 } else {
2432 const QDate lastMonthDate = currentDate.addMonths(-1);
2433 if (lastMonthDate.year() == fileDate.year() && lastMonthDate.month() == fileDate.month()) {
2434 if (daysDistance == 1) {
2435 const KLocalizedString format = ki18nc(
2436 "@title:group Date: "
2437 "MMMM is full month name in current locale, and yyyy is "
2438 "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 "
2439 "part of the text that should not be formatted as a date",
2440 "'Yesterday' (MMMM, yyyy)");
2441 const QString translatedFormat = format.toString();
2442 if (translatedFormat.count(QLatin1Char('\'')) == 2) {
2443 newGroupValue = fileTime.toString(translatedFormat);
2444 newGroupValue = i18nc(
2445 "Can be used to script translation of "
2446 "\"'Yesterday' (MMMM, yyyy)\" with context @title:group Date",
2447 "%1",
2448 newGroupValue);
2449 } else {
2450 qCWarning(DolphinDebug).nospace()
2451 << "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
2452 const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
2453 newGroupValue = fileTime.toString(untranslatedFormat);
2454 }
2455 } else if (daysDistance <= 7) {
2456 newGroupValue =
2457 fileTime.toString(i18nc("@title:group Date: "
2458 "The week day name: dddd, MMMM is full month name "
2459 "in current locale, and yyyy is full year number.",
2460 "dddd (MMMM, yyyy)"));
2461 newGroupValue = i18nc(
2462 "Can be used to script translation of "
2463 "\"dddd (MMMM, yyyy)\" with context @title:group Date",
2464 "%1",
2465 newGroupValue);
2466 } else if (daysDistance <= 7 * 2) {
2467 const KLocalizedString format = ki18nc(
2468 "@title:group Date: "
2469 "MMMM is full month name in current locale, and yyyy is "
2470 "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 "
2471 "part of the text that should not be formatted as a date",
2472 "'One Week Ago' (MMMM, yyyy)");
2473 const QString translatedFormat = format.toString();
2474 if (translatedFormat.count(QLatin1Char('\'')) == 2) {
2475 newGroupValue = fileTime.toString(translatedFormat);
2476 newGroupValue = i18nc(
2477 "Can be used to script translation of "
2478 "\"'One Week Ago' (MMMM, yyyy)\" with context @title:group Date",
2479 "%1",
2480 newGroupValue);
2481 } else {
2482 qCWarning(DolphinDebug).nospace()
2483 << "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
2484 const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
2485 newGroupValue = fileTime.toString(untranslatedFormat);
2486 }
2487 } else if (daysDistance <= 7 * 3) {
2488 const KLocalizedString format = ki18nc(
2489 "@title:group Date: "
2490 "MMMM is full month name in current locale, and yyyy is "
2491 "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 "
2492 "part of the text that should not be formatted as a date",
2493 "'Two Weeks Ago' (MMMM, yyyy)");
2494 const QString translatedFormat = format.toString();
2495 if (translatedFormat.count(QLatin1Char('\'')) == 2) {
2496 newGroupValue = fileTime.toString(translatedFormat);
2497 newGroupValue = i18nc(
2498 "Can be used to script translation of "
2499 "\"'Two Weeks Ago' (MMMM, yyyy)\" with context @title:group Date",
2500 "%1",
2501 newGroupValue);
2502 } else {
2503 qCWarning(DolphinDebug).nospace()
2504 << "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
2505 const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
2506 newGroupValue = fileTime.toString(untranslatedFormat);
2507 }
2508 } else if (daysDistance <= 7 * 4) {
2509 const KLocalizedString format = ki18nc(
2510 "@title:group Date: "
2511 "MMMM is full month name in current locale, and yyyy is "
2512 "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 "
2513 "part of the text that should not be formatted as a date",
2514 "'Three Weeks Ago' (MMMM, yyyy)");
2515 const QString translatedFormat = format.toString();
2516 if (translatedFormat.count(QLatin1Char('\'')) == 2) {
2517 newGroupValue = fileTime.toString(translatedFormat);
2518 newGroupValue = i18nc(
2519 "Can be used to script translation of "
2520 "\"'Three Weeks Ago' (MMMM, yyyy)\" with context @title:group Date",
2521 "%1",
2522 newGroupValue);
2523 } else {
2524 qCWarning(DolphinDebug).nospace()
2525 << "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
2526 const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
2527 newGroupValue = fileTime.toString(untranslatedFormat);
2528 }
2529 } else {
2530 const KLocalizedString format = ki18nc(
2531 "@title:group Date: "
2532 "MMMM is full month name in current locale, and yyyy is "
2533 "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 "
2534 "part of the text that should not be formatted as a date",
2535 "'Earlier on' MMMM, yyyy");
2536 const QString translatedFormat = format.toString();
2537 if (translatedFormat.count(QLatin1Char('\'')) == 2) {
2538 newGroupValue = fileTime.toString(translatedFormat);
2539 newGroupValue = i18nc(
2540 "Can be used to script translation of "
2541 "\"'Earlier on' MMMM, yyyy\" with context @title:group Date",
2542 "%1",
2543 newGroupValue);
2544 } else {
2545 qCWarning(DolphinDebug).nospace()
2546 << "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
2547 const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
2548 newGroupValue = fileTime.toString(untranslatedFormat);
2549 }
2550 }
2551 } else {
2552 newGroupValue =
2553 fileTime.toString(i18nc("@title:group "
2554 "The month and year: MMMM is full month name in current locale, "
2555 "and yyyy is full year number",
2556 "MMMM, yyyy"));
2557 newGroupValue = i18nc(
2558 "Can be used to script translation of "
2559 "\"MMMM, yyyy\" with context @title:group Date",
2560 "%1",
2561 newGroupValue);
2562 }
2563 }
2564
2565 if (newGroupValue != groupValue) {
2566 groupValue = newGroupValue;
2567 groups.append(QPair<int, QVariant>(i, newGroupValue));
2568 }
2569 }
2570
2571 return groups;
2572 }
2573
2574 QList<QPair<int, QVariant>> KFileItemModel::permissionRoleGroups() const
2575 {
2576 Q_ASSERT(!m_itemData.isEmpty());
2577
2578 const int maxIndex = count() - 1;
2579 QList<QPair<int, QVariant>> groups;
2580
2581 QString permissionsString;
2582 QString groupValue;
2583 for (int i = 0; i <= maxIndex; ++i) {
2584 if (isChildItem(i)) {
2585 continue;
2586 }
2587
2588 const ItemData *itemData = m_itemData.at(i);
2589 const QString newPermissionsString = itemData->values.value("permissions").toString();
2590 if (newPermissionsString == permissionsString) {
2591 continue;
2592 }
2593 permissionsString = newPermissionsString;
2594
2595 const QFileInfo info(itemData->item.url().toLocalFile());
2596
2597 // Set user string
2598 QString user;
2599 if (info.permission(QFile::ReadUser)) {
2600 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
2601 }
2602 if (info.permission(QFile::WriteUser)) {
2603 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
2604 }
2605 if (info.permission(QFile::ExeUser)) {
2606 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
2607 }
2608 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.length() - 2);
2609
2610 // Set group string
2611 QString group;
2612 if (info.permission(QFile::ReadGroup)) {
2613 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
2614 }
2615 if (info.permission(QFile::WriteGroup)) {
2616 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
2617 }
2618 if (info.permission(QFile::ExeGroup)) {
2619 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
2620 }
2621 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.length() - 2);
2622
2623 // Set others string
2624 QString others;
2625 if (info.permission(QFile::ReadOther)) {
2626 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
2627 }
2628 if (info.permission(QFile::WriteOther)) {
2629 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
2630 }
2631 if (info.permission(QFile::ExeOther)) {
2632 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
2633 }
2634 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.length() - 2);
2635
2636 const QString newGroupValue = i18nc("@title:group Files and folders by permissions", "User: %1 | Group: %2 | Others: %3", user, group, others);
2637 if (newGroupValue != groupValue) {
2638 groupValue = newGroupValue;
2639 groups.append(QPair<int, QVariant>(i, newGroupValue));
2640 }
2641 }
2642
2643 return groups;
2644 }
2645
2646 QList<QPair<int, QVariant>> KFileItemModel::ratingRoleGroups() const
2647 {
2648 Q_ASSERT(!m_itemData.isEmpty());
2649
2650 const int maxIndex = count() - 1;
2651 QList<QPair<int, QVariant>> groups;
2652
2653 int groupValue = -1;
2654 for (int i = 0; i <= maxIndex; ++i) {
2655 if (isChildItem(i)) {
2656 continue;
2657 }
2658 const int newGroupValue = m_itemData.at(i)->values.value("rating", 0).toInt();
2659 if (newGroupValue != groupValue) {
2660 groupValue = newGroupValue;
2661 groups.append(QPair<int, QVariant>(i, newGroupValue));
2662 }
2663 }
2664
2665 return groups;
2666 }
2667
2668 QList<QPair<int, QVariant>> KFileItemModel::genericStringRoleGroups(const QByteArray &role) const
2669 {
2670 Q_ASSERT(!m_itemData.isEmpty());
2671
2672 const int maxIndex = count() - 1;
2673 QList<QPair<int, QVariant>> groups;
2674
2675 bool isFirstGroupValue = true;
2676 QString groupValue;
2677 for (int i = 0; i <= maxIndex; ++i) {
2678 if (isChildItem(i)) {
2679 continue;
2680 }
2681 const QString newGroupValue = m_itemData.at(i)->values.value(role).toString();
2682 if (newGroupValue != groupValue || isFirstGroupValue) {
2683 groupValue = newGroupValue;
2684 groups.append(QPair<int, QVariant>(i, newGroupValue));
2685 isFirstGroupValue = false;
2686 }
2687 }
2688
2689 return groups;
2690 }
2691
2692 void KFileItemModel::emitSortProgress(int resolvedCount)
2693 {
2694 // Be tolerant against a resolvedCount with a wrong range.
2695 // Although there should not be a case where KFileItemModelRolesUpdater
2696 // (= caller) provides a wrong range, it is important to emit
2697 // a useful progress information even if there is an unexpected
2698 // implementation issue.
2699
2700 const int itemCount = count();
2701 if (resolvedCount >= itemCount) {
2702 m_sortingProgressPercent = -1;
2703 if (m_resortAllItemsTimer->isActive()) {
2704 m_resortAllItemsTimer->stop();
2705 resortAllItems();
2706 }
2707
2708 Q_EMIT directorySortingProgress(100);
2709 } else if (itemCount > 0) {
2710 resolvedCount = qBound(0, resolvedCount, itemCount);
2711
2712 const int progress = resolvedCount * 100 / itemCount;
2713 if (m_sortingProgressPercent != progress) {
2714 m_sortingProgressPercent = progress;
2715 Q_EMIT directorySortingProgress(progress);
2716 }
2717 }
2718 }
2719
2720 const KFileItemModel::RoleInfoMap *KFileItemModel::rolesInfoMap(int &count)
2721 {
2722 static const RoleInfoMap rolesInfoMap[] = {
2723 // clang-format off
2724 // | role | roleType | role translation | group translation | requires Baloo | requires indexer
2725 { nullptr, NoRole, KLazyLocalizedString(), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2726 { "text", NameRole, kli18nc("@label", "Name"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2727 { "size", SizeRole, kli18nc("@label", "Size"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2728 { "modificationtime", ModificationTimeRole, kli18nc("@label", "Modified"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2729 { "creationtime", CreationTimeRole, kli18nc("@label", "Created"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2730 { "accesstime", AccessTimeRole, kli18nc("@label", "Accessed"), KLazyLocalizedString(), kli18nc("@tooltip", "The date format can be selected in settings."), false, false },
2731 { "type", TypeRole, kli18nc("@label", "Type"), KLazyLocalizedString(), KLazyLocalizedString(), false, false },
2732 { "rating", RatingRole, kli18nc("@label", "Rating"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2733 { "tags", TagsRole, kli18nc("@label", "Tags"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2734 { "comment", CommentRole, kli18nc("@label", "Comment"), KLazyLocalizedString(), KLazyLocalizedString(), true, false },
2735 { "title", TitleRole, kli18nc("@label", "Title"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2736 { "author", AuthorRole, kli18nc("@label", "Author"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2737 { "publisher", PublisherRole, kli18nc("@label", "Publisher"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2738 { "pageCount", PageCountRole, kli18nc("@label", "Page Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2739 { "wordCount", WordCountRole, kli18nc("@label", "Word Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2740 { "lineCount", LineCountRole, kli18nc("@label", "Line Count"), kli18nc("@label", "Document"), KLazyLocalizedString(), true, true },
2741 { "imageDateTime", ImageDateTimeRole, kli18nc("@label", "Date Photographed"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2742 { "dimensions", DimensionsRole, kli18nc("@label width x height", "Dimensions"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2743 { "width", WidthRole, kli18nc("@label", "Width"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2744 { "height", HeightRole, kli18nc("@label", "Height"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2745 { "orientation", OrientationRole, kli18nc("@label", "Orientation"), kli18nc("@label", "Image"), KLazyLocalizedString(), true, true },
2746 { "artist", ArtistRole, kli18nc("@label", "Artist"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2747 { "genre", GenreRole, kli18nc("@label", "Genre"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2748 { "album", AlbumRole, kli18nc("@label", "Album"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2749 { "duration", DurationRole, kli18nc("@label", "Duration"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2750 { "bitrate", BitrateRole, kli18nc("@label", "Bitrate"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2751 { "track", TrackRole, kli18nc("@label", "Track"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2752 { "releaseYear", ReleaseYearRole, kli18nc("@label", "Release Year"), kli18nc("@label", "Audio"), KLazyLocalizedString(), true, true },
2753 { "aspectRatio", AspectRatioRole, kli18nc("@label", "Aspect Ratio"), kli18nc("@label", "Video"), KLazyLocalizedString(), true, true },
2754 { "frameRate", FrameRateRole, kli18nc("@label", "Frame Rate"), kli18nc("@label", "Video"), KLazyLocalizedString(), true, true },
2755 { "path", PathRole, kli18nc("@label", "Path"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2756 { "extension", ExtensionRole, kli18nc("@label", "File Extension"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2757 { "deletiontime", DeletionTimeRole, kli18nc("@label", "Deletion Time"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2758 { "destination", DestinationRole, kli18nc("@label", "Link Destination"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2759 { "originUrl", OriginUrlRole, kli18nc("@label", "Downloaded From"), kli18nc("@label", "Other"), KLazyLocalizedString(), true, false },
2760 { "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 },
2761 { "owner", OwnerRole, kli18nc("@label", "Owner"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2762 { "group", GroupRole, kli18nc("@label", "User Group"), kli18nc("@label", "Other"), KLazyLocalizedString(), false, false },
2763 };
2764 // clang-format on
2765
2766 count = sizeof(rolesInfoMap) / sizeof(RoleInfoMap);
2767 return rolesInfoMap;
2768 }
2769
2770 void KFileItemModel::determineMimeTypes(const KFileItemList &items, int timeout)
2771 {
2772 QElapsedTimer timer;
2773 timer.start();
2774 for (const KFileItem &item : items) {
2775 // Only determine mime types for files here. For directories,
2776 // KFileItem::determineMimeType() reads the .directory file inside to
2777 // load the icon, but this is not necessary at all if we just need the
2778 // type. Some special code for setting the correct mime type for
2779 // directories is in retrieveData().
2780 if (!item.isDir()) {
2781 item.determineMimeType();
2782 }
2783
2784 if (timer.elapsed() > timeout) {
2785 // Don't block the user interface, let the remaining items
2786 // be resolved asynchronously.
2787 return;
2788 }
2789 }
2790 }
2791
2792 QByteArray KFileItemModel::sharedValue(const QByteArray &value)
2793 {
2794 static QSet<QByteArray> pool;
2795 const QSet<QByteArray>::const_iterator it = pool.constFind(value);
2796
2797 if (it != pool.constEnd()) {
2798 return *it;
2799 } else {
2800 pool.insert(value);
2801 return value;
2802 }
2803 }
2804
2805 bool KFileItemModel::isConsistent() const
2806 {
2807 // m_items may contain less items than m_itemData because m_items
2808 // is populated lazily, see KFileItemModel::index(const QUrl& url).
2809 if (m_items.count() > m_itemData.count()) {
2810 return false;
2811 }
2812
2813 for (int i = 0, iMax = count(); i < iMax; ++i) {
2814 // Check if m_items and m_itemData are consistent.
2815 const KFileItem item = fileItem(i);
2816 if (item.isNull()) {
2817 qCWarning(DolphinDebug) << "Item" << i << "is null";
2818 return false;
2819 }
2820
2821 const int itemIndex = index(item);
2822 if (itemIndex != i) {
2823 qCWarning(DolphinDebug) << "Item" << i << "has a wrong index:" << itemIndex;
2824 return false;
2825 }
2826
2827 // Check if the items are sorted correctly.
2828 if (i > 0 && !lessThan(m_itemData.at(i - 1), m_itemData.at(i), m_collator)) {
2829 qCWarning(DolphinDebug) << "The order of items" << i - 1 << "and" << i << "is wrong:" << fileItem(i - 1) << fileItem(i);
2830 return false;
2831 }
2832
2833 // Check if all parent-child relationships are consistent.
2834 const ItemData *data = m_itemData.at(i);
2835 const ItemData *parent = data->parent;
2836 if (parent) {
2837 if (expandedParentsCount(data) != expandedParentsCount(parent) + 1) {
2838 qCWarning(DolphinDebug) << "expandedParentsCount is inconsistent for parent" << parent->item << "and child" << data->item;
2839 return false;
2840 }
2841
2842 const int parentIndex = index(parent->item);
2843 if (parentIndex >= i) {
2844 qCWarning(DolphinDebug) << "Index" << parentIndex << "of parent" << parent->item << "is not smaller than index" << i << "of child"
2845 << data->item;
2846 return false;
2847 }
2848 }
2849 }
2850
2851 return true;
2852 }
2853
2854 void KFileItemModel::slotListerError(KIO::Job *job)
2855 {
2856 if (job->error() == KIO::ERR_IS_FILE) {
2857 if (auto *listJob = qobject_cast<KIO::ListJob *>(job)) {
2858 Q_EMIT urlIsFileError(listJob->url());
2859 }
2860 } else {
2861 const QString errorString = job->errorString();
2862 Q_EMIT errorMessage(!errorString.isEmpty() ? errorString : i18nc("@info:status", "Unknown error."));
2863 }
2864 }
2865
2866 #include "moc_kfileitemmodel.cpp"