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