]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodel.cpp
Fix regression that caused "(I18N_EMPTY_MESSAGE)" sub menus in menus
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kfileitemmodel.h"
21
22 #include <KDirModel>
23 #include <KGlobalSettings>
24 #include <KLocale>
25 #include <KStringHandler>
26 #include <KDebug>
27
28 #include "private/kfileitemmodelsortalgorithm.h"
29 #include "private/kfileitemmodeldirlister.h"
30
31 #include <QApplication>
32 #include <QMimeData>
33 #include <QTimer>
34
35 // #define KFILEITEMMODEL_DEBUG
36
37 KFileItemModel::KFileItemModel(QObject* parent) :
38 KItemModelBase("text", parent),
39 m_dirLister(0),
40 m_naturalSorting(KGlobalSettings::naturalSorting()),
41 m_sortDirsFirst(true),
42 m_sortRole(NameRole),
43 m_sortingProgressPercent(-1),
44 m_roles(),
45 m_caseSensitivity(Qt::CaseInsensitive),
46 m_itemData(),
47 m_items(),
48 m_filter(),
49 m_filteredItems(),
50 m_requestRole(),
51 m_maximumUpdateIntervalTimer(0),
52 m_resortAllItemsTimer(0),
53 m_pendingItemsToInsert(),
54 m_groups(),
55 m_expandedParentsCountRoot(UninitializedExpandedParentsCountRoot),
56 m_expandedDirs(),
57 m_urlsToExpand()
58 {
59 m_dirLister = new KFileItemModelDirLister(this);
60 m_dirLister->setAutoUpdate(true);
61 m_dirLister->setDelayedMimeTypes(true);
62 m_dirLister->setMainWindow(qApp->activeWindow());
63
64 connect(m_dirLister, SIGNAL(started(KUrl)), this, SIGNAL(directoryLoadingStarted()));
65 connect(m_dirLister, SIGNAL(canceled()), this, SLOT(slotCanceled()));
66 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted()));
67 connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SLOT(slotNewItems(KFileItemList)));
68 connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SLOT(slotItemsDeleted(KFileItemList)));
69 connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)), this, SLOT(slotRefreshItems(QList<QPair<KFileItem,KFileItem> >)));
70 connect(m_dirLister, SIGNAL(clear()), this, SLOT(slotClear()));
71 connect(m_dirLister, SIGNAL(clear(KUrl)), this, SLOT(slotClear(KUrl)));
72 connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
73 connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
74 connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SIGNAL(directoryRedirection(KUrl,KUrl)));
75 connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
76
77 // Apply default roles that should be determined
78 resetRoles();
79 m_requestRole[NameRole] = true;
80 m_requestRole[IsDirRole] = true;
81 m_requestRole[IsLinkRole] = true;
82 m_roles.insert("text");
83 m_roles.insert("isDir");
84 m_roles.insert("isLink");
85
86 // For slow KIO-slaves like used for searching it makes sense to show results periodically even
87 // before the completed() or canceled() signal has been emitted.
88 m_maximumUpdateIntervalTimer = new QTimer(this);
89 m_maximumUpdateIntervalTimer->setInterval(2000);
90 m_maximumUpdateIntervalTimer->setSingleShot(true);
91 connect(m_maximumUpdateIntervalTimer, SIGNAL(timeout()), this, SLOT(dispatchPendingItemsToInsert()));
92
93 // When changing the value of an item which represents the sort-role a resorting must be
94 // triggered. Especially in combination with KFileItemModelRolesUpdater this might be done
95 // for a lot of items within a quite small timeslot. To prevent expensive resortings the
96 // resorting is postponed until the timer has been exceeded.
97 m_resortAllItemsTimer = new QTimer(this);
98 m_resortAllItemsTimer->setInterval(500);
99 m_resortAllItemsTimer->setSingleShot(true);
100 connect(m_resortAllItemsTimer, SIGNAL(timeout()), this, SLOT(resortAllItems()));
101
102 connect(KGlobalSettings::self(), SIGNAL(naturalSortingChanged()), this, SLOT(slotNaturalSortingChanged()));
103 }
104
105 KFileItemModel::~KFileItemModel()
106 {
107 qDeleteAll(m_itemData);
108 m_itemData.clear();
109 }
110
111 void KFileItemModel::loadDirectory(const KUrl& url)
112 {
113 m_dirLister->openUrl(url);
114 }
115
116 void KFileItemModel::refreshDirectory(const KUrl& url)
117 {
118 m_dirLister->openUrl(url, KDirLister::Reload);
119 }
120
121 KUrl KFileItemModel::directory() const
122 {
123 return m_dirLister->url();
124 }
125
126 void KFileItemModel::cancelDirectoryLoading()
127 {
128 m_dirLister->stop();
129 }
130
131 int KFileItemModel::count() const
132 {
133 return m_itemData.count();
134 }
135
136 QHash<QByteArray, QVariant> KFileItemModel::data(int index) const
137 {
138 if (index >= 0 && index < count()) {
139 return m_itemData.at(index)->values;
140 }
141 return QHash<QByteArray, QVariant>();
142 }
143
144 bool KFileItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
145 {
146 if (index < 0 || index >= count()) {
147 return false;
148 }
149
150 QHash<QByteArray, QVariant> currentValues = m_itemData.at(index)->values;
151
152 // Determine which roles have been changed
153 QSet<QByteArray> changedRoles;
154 QHashIterator<QByteArray, QVariant> it(values);
155 while (it.hasNext()) {
156 it.next();
157 const QByteArray role = it.key();
158 const QVariant value = it.value();
159
160 if (currentValues[role] != value) {
161 currentValues[role] = value;
162 changedRoles.insert(role);
163 }
164 }
165
166 if (changedRoles.isEmpty()) {
167 return false;
168 }
169
170 m_itemData[index]->values = currentValues;
171 if (changedRoles.contains("text")) {
172 KUrl url = m_itemData[index]->item.url();
173 url.setFileName(currentValues["text"].toString());
174 m_itemData[index]->item.setUrl(url);
175 }
176
177 emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
178
179 if (changedRoles.contains(sortRole())) {
180 m_resortAllItemsTimer->start();
181 }
182
183 return true;
184 }
185
186 void KFileItemModel::setSortDirectoriesFirst(bool dirsFirst)
187 {
188 if (dirsFirst != m_sortDirsFirst) {
189 m_sortDirsFirst = dirsFirst;
190 resortAllItems();
191 }
192 }
193
194 bool KFileItemModel::sortDirectoriesFirst() const
195 {
196 return m_sortDirsFirst;
197 }
198
199 void KFileItemModel::setShowHiddenFiles(bool show)
200 {
201 m_dirLister->setShowingDotFiles(show);
202 m_dirLister->emitChanges();
203 if (show) {
204 slotCompleted();
205 }
206 }
207
208 bool KFileItemModel::showHiddenFiles() const
209 {
210 return m_dirLister->showingDotFiles();
211 }
212
213 void KFileItemModel::setShowDirectoriesOnly(bool enabled)
214 {
215 m_dirLister->setDirOnlyMode(enabled);
216 }
217
218 bool KFileItemModel::showDirectoriesOnly() const
219 {
220 return m_dirLister->dirOnlyMode();
221 }
222
223 QMimeData* KFileItemModel::createMimeData(const QSet<int>& indexes) const
224 {
225 QMimeData* data = new QMimeData();
226
227 // The following code has been taken from KDirModel::mimeData()
228 // (kdelibs/kio/kio/kdirmodel.cpp)
229 // Copyright (C) 2006 David Faure <faure@kde.org>
230 KUrl::List urls;
231 KUrl::List mostLocalUrls;
232 bool canUseMostLocalUrls = true;
233
234 QSetIterator<int> it(indexes);
235 while (it.hasNext()) {
236 const int index = it.next();
237 const KFileItem item = fileItem(index);
238 if (!item.isNull()) {
239 urls << item.url();
240
241 bool isLocal;
242 mostLocalUrls << item.mostLocalUrl(isLocal);
243 if (!isLocal) {
244 canUseMostLocalUrls = false;
245 }
246 }
247 }
248
249 const bool different = canUseMostLocalUrls && mostLocalUrls != urls;
250 urls = KDirModel::simplifiedUrlList(urls); // TODO: Check if we still need KDirModel for this in KDE 5.0
251 if (different) {
252 mostLocalUrls = KDirModel::simplifiedUrlList(mostLocalUrls);
253 urls.populateMimeData(mostLocalUrls, data);
254 } else {
255 urls.populateMimeData(data);
256 }
257
258 return data;
259 }
260
261 int KFileItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
262 {
263 startFromIndex = qMax(0, startFromIndex);
264 for (int i = startFromIndex; i < count(); ++i) {
265 if (data(i)["text"].toString().startsWith(text, Qt::CaseInsensitive)) {
266 return i;
267 }
268 }
269 for (int i = 0; i < startFromIndex; ++i) {
270 if (data(i)["text"].toString().startsWith(text, Qt::CaseInsensitive)) {
271 return i;
272 }
273 }
274 return -1;
275 }
276
277 bool KFileItemModel::supportsDropping(int index) const
278 {
279 const KFileItem item = fileItem(index);
280 return !item.isNull() && (item.isDir() || item.isDesktopFile());
281 }
282
283 QString KFileItemModel::roleDescription(const QByteArray& role) const
284 {
285 static QHash<QByteArray, QString> description;
286 if (description.isEmpty()) {
287 int count = 0;
288 const RoleInfoMap* map = rolesInfoMap(count);
289 for (int i = 0; i < count; ++i) {
290 description.insert(map[i].role, i18nc(map[i].roleTranslationContext, map[i].roleTranslation));
291 }
292 }
293
294 return description.value(role);
295 }
296
297 QList<QPair<int, QVariant> > KFileItemModel::groups() const
298 {
299 if (!m_itemData.isEmpty() && m_groups.isEmpty()) {
300 #ifdef KFILEITEMMODEL_DEBUG
301 QElapsedTimer timer;
302 timer.start();
303 #endif
304 switch (typeForRole(sortRole())) {
305 case NameRole: m_groups = nameRoleGroups(); break;
306 case SizeRole: m_groups = sizeRoleGroups(); break;
307 case DateRole: m_groups = dateRoleGroups(); break;
308 case PermissionsRole: m_groups = permissionRoleGroups(); break;
309 case RatingRole: m_groups = ratingRoleGroups(); break;
310 default: m_groups = genericStringRoleGroups(sortRole()); break;
311 }
312
313 #ifdef KFILEITEMMODEL_DEBUG
314 kDebug() << "[TIME] Calculating groups for" << count() << "items:" << timer.elapsed();
315 #endif
316 }
317
318 return m_groups;
319 }
320
321 KFileItem KFileItemModel::fileItem(int index) const
322 {
323 if (index >= 0 && index < count()) {
324 return m_itemData.at(index)->item;
325 }
326
327 return KFileItem();
328 }
329
330 KFileItem KFileItemModel::fileItem(const KUrl& url) const
331 {
332 const int index = m_items.value(url, -1);
333 if (index >= 0) {
334 return m_itemData.at(index)->item;
335 }
336 return KFileItem();
337 }
338
339 int KFileItemModel::index(const KFileItem& item) const
340 {
341 if (item.isNull()) {
342 return -1;
343 }
344
345 return m_items.value(item.url(), -1);
346 }
347
348 int KFileItemModel::index(const KUrl& url) const
349 {
350 KUrl urlToFind = url;
351 urlToFind.adjustPath(KUrl::RemoveTrailingSlash);
352 return m_items.value(urlToFind, -1);
353 }
354
355 KFileItem KFileItemModel::rootItem() const
356 {
357 return m_dirLister->rootItem();
358 }
359
360 void KFileItemModel::clear()
361 {
362 slotClear();
363 }
364
365 void KFileItemModel::setRoles(const QSet<QByteArray>& roles)
366 {
367 if (m_roles == roles) {
368 return;
369 }
370 m_roles = roles;
371
372 if (count() > 0) {
373 const bool supportedExpanding = m_requestRole[ExpandedParentsCountRole];
374 const bool willSupportExpanding = roles.contains("expandedParentsCount");
375 if (supportedExpanding && !willSupportExpanding) {
376 // No expanding is supported anymore. Take care to delete all items that have an expansion level
377 // that is not 0 (and hence are part of an expanded item).
378 removeExpandedItems();
379 }
380 }
381
382 m_groups.clear();
383 resetRoles();
384
385 QSetIterator<QByteArray> it(roles);
386 while (it.hasNext()) {
387 const QByteArray& role = it.next();
388 m_requestRole[typeForRole(role)] = true;
389 }
390
391 if (count() > 0) {
392 // Update m_data with the changed requested roles
393 const int maxIndex = count() - 1;
394 for (int i = 0; i <= maxIndex; ++i) {
395 m_itemData[i]->values = retrieveData(m_itemData.at(i)->item);
396 }
397
398 kWarning() << "TODO: Emitting itemsChanged() with no information what has changed!";
399 emit itemsChanged(KItemRangeList() << KItemRange(0, count()), QSet<QByteArray>());
400 }
401 }
402
403 QSet<QByteArray> KFileItemModel::roles() const
404 {
405 return m_roles;
406 }
407
408 bool KFileItemModel::setExpanded(int index, bool expanded)
409 {
410 if (!isExpandable(index) || isExpanded(index) == expanded) {
411 return false;
412 }
413
414 QHash<QByteArray, QVariant> values;
415 values.insert("isExpanded", expanded);
416 if (!setData(index, values)) {
417 return false;
418 }
419
420 const KUrl url = m_itemData.at(index)->item.url();
421 if (expanded) {
422 m_expandedDirs.insert(url);
423 m_dirLister->openUrl(url, KDirLister::Keep);
424 } else {
425 m_expandedDirs.remove(url);
426 m_dirLister->stop(url);
427
428
429 KFileItemList itemsToRemove;
430 const int expandedParentsCount = data(index)["expandedParentsCount"].toInt();
431 ++index;
432 while (index < count() && data(index)["expandedParentsCount"].toInt() > expandedParentsCount) {
433 itemsToRemove.append(m_itemData.at(index)->item);
434 ++index;
435 }
436 removeItems(itemsToRemove);
437 }
438
439 return true;
440 }
441
442 bool KFileItemModel::isExpanded(int index) const
443 {
444 if (index >= 0 && index < count()) {
445 return m_itemData.at(index)->values.value("isExpanded").toBool();
446 }
447 return false;
448 }
449
450 bool KFileItemModel::isExpandable(int index) const
451 {
452 if (index >= 0 && index < count()) {
453 return m_itemData.at(index)->values.value("isExpandable").toBool();
454 }
455 return false;
456 }
457
458 int KFileItemModel::expandedParentsCount(int index) const
459 {
460 if (index >= 0 && index < count()) {
461 const int parentsCount = m_itemData.at(index)->values.value("expandedParentsCount").toInt();
462 if (parentsCount > 0) {
463 return parentsCount;
464 }
465 }
466 return 0;
467 }
468
469 QSet<KUrl> KFileItemModel::expandedDirectories() const
470 {
471 return m_expandedDirs;
472 }
473
474 void KFileItemModel::restoreExpandedDirectories(const QSet<KUrl>& urls)
475 {
476 m_urlsToExpand = urls;
477 }
478
479 void KFileItemModel::expandParentDirectories(const KUrl& url)
480 {
481 const int pos = m_dirLister->url().path().length();
482
483 // Assure that each sub-path of the URL that should be
484 // expanded is added to m_urlsToExpand. KDirLister
485 // does not care whether the parent-URL has already been
486 // expanded.
487 KUrl urlToExpand = m_dirLister->url();
488 const QStringList subDirs = url.path().mid(pos).split(QDir::separator());
489 for (int i = 0; i < subDirs.count() - 1; ++i) {
490 urlToExpand.addPath(subDirs.at(i));
491 m_urlsToExpand.insert(urlToExpand);
492 }
493
494 // KDirLister::open() must called at least once to trigger an initial
495 // loading. The pending URLs that must be restored are handled
496 // in slotCompleted().
497 QSetIterator<KUrl> it2(m_urlsToExpand);
498 while (it2.hasNext()) {
499 const int idx = index(it2.next());
500 if (idx >= 0 && !isExpanded(idx)) {
501 setExpanded(idx, true);
502 break;
503 }
504 }
505 }
506
507 void KFileItemModel::setNameFilter(const QString& nameFilter)
508 {
509 if (m_filter.pattern() != nameFilter) {
510 dispatchPendingItemsToInsert();
511
512 m_filter.setPattern(nameFilter);
513
514 // Check which shown items from m_itemData must get
515 // hidden and hence moved to m_filteredItems.
516 KFileItemList newFilteredItems;
517
518 foreach (ItemData* itemData, m_itemData) {
519 if (!m_filter.matches(itemData->item)) {
520 // Only filter non-expanded items as child items may never
521 // exist without a parent item
522 if (!itemData->values.value("isExpanded").toBool()) {
523 newFilteredItems.append(itemData->item);
524 m_filteredItems.insert(itemData->item);
525 }
526 }
527 }
528
529 removeItems(newFilteredItems);
530
531 // Check which hidden items from m_filteredItems should
532 // get visible again and hence removed from m_filteredItems.
533 KFileItemList newVisibleItems;
534
535 QMutableSetIterator<KFileItem> it(m_filteredItems);
536 while (it.hasNext()) {
537 const KFileItem item = it.next();
538 if (m_filter.matches(item)) {
539 newVisibleItems.append(item);
540 it.remove();
541 }
542 }
543
544 insertItems(newVisibleItems);
545 }
546 }
547
548 QString KFileItemModel::nameFilter() const
549 {
550 return m_filter.pattern();
551 }
552
553 QList<KFileItemModel::RoleInfo> KFileItemModel::rolesInformation()
554 {
555 static QList<RoleInfo> rolesInfo;
556 if (rolesInfo.isEmpty()) {
557 int count = 0;
558 const RoleInfoMap* map = rolesInfoMap(count);
559 for (int i = 0; i < count; ++i) {
560 if (map[i].roleType != NoRole) {
561 RoleInfo info;
562 info.role = map[i].role;
563 info.translation = i18nc(map[i].roleTranslationContext, map[i].roleTranslation);
564 if (map[i].groupTranslation) {
565 info.group = i18nc(map[i].groupTranslationContext, map[i].groupTranslation);
566 } else {
567 // For top level roles, groupTranslation is 0. We must make sure that
568 // info.group is an empty string then because the code that generates
569 // menus tries to put the actions into sub menus otherwise.
570 info.group = QString();
571 }
572 info.requiresNepomuk = map[i].requiresNepomuk;
573 info.requiresIndexer = map[i].requiresIndexer;
574 rolesInfo.append(info);
575 }
576 }
577 }
578
579 return rolesInfo;
580 }
581
582 void KFileItemModel::onGroupedSortingChanged(bool current)
583 {
584 Q_UNUSED(current);
585 m_groups.clear();
586 }
587
588 void KFileItemModel::onSortRoleChanged(const QByteArray& current, const QByteArray& previous)
589 {
590 Q_UNUSED(previous);
591 m_sortRole = typeForRole(current);
592
593 #ifdef KFILEITEMMODEL_DEBUG
594 if (!m_requestRole[m_sortRole]) {
595 kWarning() << "The sort-role has been changed to a role that has not been received yet";
596 }
597 #endif
598
599 resortAllItems();
600 }
601
602 void KFileItemModel::onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
603 {
604 Q_UNUSED(current);
605 Q_UNUSED(previous);
606 resortAllItems();
607 }
608
609 void KFileItemModel::resortAllItems()
610 {
611 m_resortAllItemsTimer->stop();
612
613 const int itemCount = count();
614 if (itemCount <= 0) {
615 return;
616 }
617
618 #ifdef KFILEITEMMODEL_DEBUG
619 QElapsedTimer timer;
620 timer.start();
621 kDebug() << "===========================================================";
622 kDebug() << "Resorting" << itemCount << "items";
623 #endif
624
625 // Remember the order of the current URLs so
626 // that it can be determined which indexes have
627 // been moved because of the resorting.
628 QList<KUrl> oldUrls;
629 oldUrls.reserve(itemCount);
630 foreach (const ItemData* itemData, m_itemData) {
631 oldUrls.append(itemData->item.url());
632 }
633
634 m_groups.clear();
635 m_items.clear();
636
637 // Resort the items
638 KFileItemModelSortAlgorithm::sort(this, m_itemData.begin(), m_itemData.end());
639 for (int i = 0; i < itemCount; ++i) {
640 m_items.insert(m_itemData.at(i)->item.url(), i);
641 }
642
643 // Determine the indexes that have been moved
644 QList<int> movedToIndexes;
645 movedToIndexes.reserve(itemCount);
646 for (int i = 0; i < itemCount; i++) {
647 const int newIndex = m_items.value(oldUrls.at(i).url());
648 movedToIndexes.append(newIndex);
649 }
650
651 // Don't check whether items have really been moved and always emit a
652 // itemsMoved() signal after resorting: In case of grouped items
653 // the groups might change even if the items themselves don't change their
654 // position. Let the receiver of the signal decide whether a check for moved
655 // items makes sense.
656 emit itemsMoved(KItemRange(0, itemCount), movedToIndexes);
657
658 #ifdef KFILEITEMMODEL_DEBUG
659 kDebug() << "[TIME] Resorting of" << itemCount << "items:" << timer.elapsed();
660 #endif
661 }
662
663 void KFileItemModel::slotCompleted()
664 {
665 dispatchPendingItemsToInsert();
666
667 if (!m_urlsToExpand.isEmpty()) {
668 // Try to find a URL that can be expanded.
669 // Note that the parent folder must be expanded before any of its subfolders become visible.
670 // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
671 // -> we expand the first visible URL we find in m_restoredExpandedUrls.
672 foreach (const KUrl& url, m_urlsToExpand) {
673 const int index = m_items.value(url, -1);
674 if (index >= 0) {
675 m_urlsToExpand.remove(url);
676 if (setExpanded(index, true)) {
677 // The dir lister has been triggered. This slot will be called
678 // again after the directory has been expanded.
679 return;
680 }
681 }
682 }
683
684 // None of the URLs in m_restoredExpandedUrls could be found in the model. This can happen
685 // if these URLs have been deleted in the meantime.
686 m_urlsToExpand.clear();
687 }
688
689 emit directoryLoadingCompleted();
690 }
691
692 void KFileItemModel::slotCanceled()
693 {
694 m_maximumUpdateIntervalTimer->stop();
695 dispatchPendingItemsToInsert();
696 }
697
698 void KFileItemModel::slotNewItems(const KFileItemList& items)
699 {
700 Q_ASSERT(!items.isEmpty());
701
702 if (m_requestRole[ExpandedParentsCountRole] && m_expandedParentsCountRoot >= 0) {
703 // To be able to compare whether the new items may be inserted as children
704 // of a parent item the pending items must be added to the model first.
705 dispatchPendingItemsToInsert();
706
707 KFileItem item = items.first();
708
709 // If the expanding of items is enabled, the call
710 // dirLister->openUrl(url, KDirLister::Keep) in KFileItemModel::setExpanded()
711 // might result in emitting the same items twice due to the Keep-parameter.
712 // This case happens if an item gets expanded, collapsed and expanded again
713 // before the items could be loaded for the first expansion.
714 const int index = m_items.value(item.url(), -1);
715 if (index >= 0) {
716 // The items are already part of the model.
717 return;
718 }
719
720 // KDirLister keeps the children of items that got expanded once even if
721 // they got collapsed again with KFileItemModel::setExpanded(false). So it must be
722 // checked whether the parent for new items is still expanded.
723 KUrl parentUrl = item.url().upUrl();
724 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
725 const int parentIndex = m_items.value(parentUrl, -1);
726 if (parentIndex >= 0 && !m_itemData[parentIndex]->values.value("isExpanded").toBool()) {
727 // The parent is not expanded.
728 return;
729 }
730 }
731
732 if (m_filter.pattern().isEmpty()) {
733 m_pendingItemsToInsert.append(items);
734 } else {
735 // The name-filter is active. Hide filtered items
736 // before inserting them into the model and remember
737 // the filtered items in m_filteredItems.
738 KFileItemList filteredItems;
739 foreach (const KFileItem& item, items) {
740 if (m_filter.matches(item)) {
741 filteredItems.append(item);
742 } else {
743 m_filteredItems.insert(item);
744 }
745 }
746
747 m_pendingItemsToInsert.append(filteredItems);
748 }
749
750 if (useMaximumUpdateInterval() && !m_maximumUpdateIntervalTimer->isActive()) {
751 // Assure that items get dispatched if no completed() or canceled() signal is
752 // emitted during the maximum update interval.
753 m_maximumUpdateIntervalTimer->start();
754 }
755 }
756
757 void KFileItemModel::slotItemsDeleted(const KFileItemList& items)
758 {
759 dispatchPendingItemsToInsert();
760
761 KFileItemList itemsToRemove = items;
762 if (m_requestRole[ExpandedParentsCountRole] && m_expandedParentsCountRoot >= 0) {
763 // Assure that removing a parent item also results in removing all children
764 foreach (const KFileItem& item, items) {
765 itemsToRemove.append(childItems(item));
766 }
767 }
768
769 if (!m_filteredItems.isEmpty()) {
770 foreach (const KFileItem& item, itemsToRemove) {
771 m_filteredItems.remove(item);
772 }
773 }
774
775 removeItems(itemsToRemove);
776 }
777
778 void KFileItemModel::slotRefreshItems(const QList<QPair<KFileItem, KFileItem> >& items)
779 {
780 Q_ASSERT(!items.isEmpty());
781 #ifdef KFILEITEMMODEL_DEBUG
782 kDebug() << "Refreshing" << items.count() << "items";
783 #endif
784
785 m_groups.clear();
786
787 // Get the indexes of all items that have been refreshed
788 QList<int> indexes;
789 indexes.reserve(items.count());
790
791 QListIterator<QPair<KFileItem, KFileItem> > it(items);
792 while (it.hasNext()) {
793 const QPair<KFileItem, KFileItem>& itemPair = it.next();
794 const KFileItem& oldItem = itemPair.first;
795 const KFileItem& newItem = itemPair.second;
796 const int index = m_items.value(oldItem.url(), -1);
797 if (index >= 0) {
798 m_itemData[index]->item = newItem;
799
800 // Keep old values as long as possible if they could not retrieved synchronously yet.
801 // The update of the values will be done asynchronously by KFileItemModelRolesUpdater.
802 QHashIterator<QByteArray, QVariant> it(retrieveData(newItem));
803 while (it.hasNext()) {
804 it.next();
805 m_itemData[index]->values.insert(it.key(), it.value());
806 }
807
808 m_items.remove(oldItem.url());
809 m_items.insert(newItem.url(), index);
810 indexes.append(index);
811 }
812 }
813
814 // If the changed items have been created recently, they might not be in m_items yet.
815 // In that case, the list 'indexes' might be empty.
816 if (indexes.isEmpty()) {
817 return;
818 }
819
820 // Extract the item-ranges out of the changed indexes
821 qSort(indexes);
822
823 KItemRangeList itemRangeList;
824 int previousIndex = indexes.at(0);
825 int rangeIndex = previousIndex;
826 int rangeCount = 1;
827
828 const int maxIndex = indexes.count() - 1;
829 for (int i = 1; i <= maxIndex; ++i) {
830 const int currentIndex = indexes.at(i);
831 if (currentIndex == previousIndex + 1) {
832 ++rangeCount;
833 } else {
834 itemRangeList.append(KItemRange(rangeIndex, rangeCount));
835
836 rangeIndex = currentIndex;
837 rangeCount = 1;
838 }
839 previousIndex = currentIndex;
840 }
841
842 if (rangeCount > 0) {
843 itemRangeList.append(KItemRange(rangeIndex, rangeCount));
844 }
845
846 emit itemsChanged(itemRangeList, m_roles);
847
848 resortAllItems();
849 }
850
851 void KFileItemModel::slotClear()
852 {
853 #ifdef KFILEITEMMODEL_DEBUG
854 kDebug() << "Clearing all items";
855 #endif
856
857 m_filteredItems.clear();
858 m_groups.clear();
859
860 m_maximumUpdateIntervalTimer->stop();
861 m_resortAllItemsTimer->stop();
862 m_pendingItemsToInsert.clear();
863
864 m_expandedParentsCountRoot = UninitializedExpandedParentsCountRoot;
865
866 const int removedCount = m_itemData.count();
867 if (removedCount > 0) {
868 qDeleteAll(m_itemData);
869 m_itemData.clear();
870 m_items.clear();
871 emit itemsRemoved(KItemRangeList() << KItemRange(0, removedCount));
872 }
873
874 m_expandedDirs.clear();
875 }
876
877 void KFileItemModel::slotClear(const KUrl& url)
878 {
879 Q_UNUSED(url);
880 }
881
882 void KFileItemModel::slotNaturalSortingChanged()
883 {
884 m_naturalSorting = KGlobalSettings::naturalSorting();
885 resortAllItems();
886 }
887
888 void KFileItemModel::dispatchPendingItemsToInsert()
889 {
890 if (!m_pendingItemsToInsert.isEmpty()) {
891 insertItems(m_pendingItemsToInsert);
892 m_pendingItemsToInsert.clear();
893 }
894 }
895
896 void KFileItemModel::insertItems(const KFileItemList& items)
897 {
898 if (items.isEmpty()) {
899 return;
900 }
901
902 if (m_sortRole == TypeRole) {
903 // Try to resolve the MIME-types synchronously to prevent a reordering of
904 // the items when sorting by type (per default MIME-types are resolved
905 // asynchronously by KFileItemModelRolesUpdater).
906 determineMimeTypes(items, 200);
907 }
908
909 #ifdef KFILEITEMMODEL_DEBUG
910 QElapsedTimer timer;
911 timer.start();
912 kDebug() << "===========================================================";
913 kDebug() << "Inserting" << items.count() << "items";
914 #endif
915
916 m_groups.clear();
917
918 QList<ItemData*> sortedItems = createItemDataList(items);
919 KFileItemModelSortAlgorithm::sort(this, sortedItems.begin(), sortedItems.end());
920
921 #ifdef KFILEITEMMODEL_DEBUG
922 kDebug() << "[TIME] Sorting:" << timer.elapsed();
923 #endif
924
925 KItemRangeList itemRanges;
926 int targetIndex = 0;
927 int sourceIndex = 0;
928 int insertedAtIndex = -1; // Index for the current item-range
929 int insertedCount = 0; // Count for the current item-range
930 int previouslyInsertedCount = 0; // Sum of previously inserted items for all ranges
931 while (sourceIndex < sortedItems.count()) {
932 // Find target index from m_items to insert the current item
933 // in a sorted order
934 const int previousTargetIndex = targetIndex;
935 while (targetIndex < m_itemData.count()) {
936 if (!lessThan(m_itemData.at(targetIndex), sortedItems.at(sourceIndex))) {
937 break;
938 }
939 ++targetIndex;
940 }
941
942 if (targetIndex - previousTargetIndex > 0 && insertedAtIndex >= 0) {
943 itemRanges << KItemRange(insertedAtIndex, insertedCount);
944 previouslyInsertedCount += insertedCount;
945 insertedAtIndex = targetIndex - previouslyInsertedCount;
946 insertedCount = 0;
947 }
948
949 // Insert item at the position targetIndex by transferring
950 // the ownership of the item-data from sortedItems to m_itemData.
951 // m_items will be inserted after the loop (see comment below)
952 m_itemData.insert(targetIndex, sortedItems.at(sourceIndex));
953 ++insertedCount;
954
955 if (insertedAtIndex < 0) {
956 insertedAtIndex = targetIndex;
957 Q_ASSERT(previouslyInsertedCount == 0);
958 }
959 ++targetIndex;
960 ++sourceIndex;
961 }
962
963 // The indexes of all m_items must be adjusted, not only the index
964 // of the new items
965 const int itemDataCount = m_itemData.count();
966 for (int i = 0; i < itemDataCount; ++i) {
967 m_items.insert(m_itemData.at(i)->item.url(), i);
968 }
969
970 itemRanges << KItemRange(insertedAtIndex, insertedCount);
971 emit itemsInserted(itemRanges);
972
973 #ifdef KFILEITEMMODEL_DEBUG
974 kDebug() << "[TIME] Inserting of" << items.count() << "items:" << timer.elapsed();
975 #endif
976 }
977
978 void KFileItemModel::removeItems(const KFileItemList& items)
979 {
980 if (items.isEmpty()) {
981 return;
982 }
983
984 #ifdef KFILEITEMMODEL_DEBUG
985 kDebug() << "Removing " << items.count() << "items";
986 #endif
987
988 m_groups.clear();
989
990 QList<ItemData*> sortedItems;
991 sortedItems.reserve(items.count());
992 foreach (const KFileItem& item, items) {
993 const int index = m_items.value(item.url(), -1);
994 if (index >= 0) {
995 sortedItems.append(m_itemData.at(index));
996 }
997 }
998 KFileItemModelSortAlgorithm::sort(this, sortedItems.begin(), sortedItems.end());
999
1000 QList<int> indexesToRemove;
1001 indexesToRemove.reserve(items.count());
1002
1003 // Calculate the item ranges that will get deleted
1004 KItemRangeList itemRanges;
1005 int removedAtIndex = -1;
1006 int removedCount = 0;
1007 int targetIndex = 0;
1008 foreach (const ItemData* itemData, sortedItems) {
1009 const KFileItem& itemToRemove = itemData->item;
1010
1011 const int previousTargetIndex = targetIndex;
1012 while (targetIndex < m_itemData.count()) {
1013 if (m_itemData.at(targetIndex)->item.url() == itemToRemove.url()) {
1014 break;
1015 }
1016 ++targetIndex;
1017 }
1018 if (targetIndex >= m_itemData.count()) {
1019 kWarning() << "Item that should be deleted has not been found!";
1020 return;
1021 }
1022
1023 if (targetIndex - previousTargetIndex > 0 && removedAtIndex >= 0) {
1024 itemRanges << KItemRange(removedAtIndex, removedCount);
1025 removedAtIndex = targetIndex;
1026 removedCount = 0;
1027 }
1028
1029 indexesToRemove.append(targetIndex);
1030 if (removedAtIndex < 0) {
1031 removedAtIndex = targetIndex;
1032 }
1033 ++removedCount;
1034 ++targetIndex;
1035 }
1036
1037 // Delete the items
1038 for (int i = indexesToRemove.count() - 1; i >= 0; --i) {
1039 const int indexToRemove = indexesToRemove.at(i);
1040 ItemData* data = m_itemData.at(indexToRemove);
1041
1042 m_items.remove(data->item.url());
1043
1044 delete data;
1045 m_itemData.removeAt(indexToRemove);
1046 }
1047
1048 // The indexes of all m_items must be adjusted, not only the index
1049 // of the removed items
1050 const int itemDataCount = m_itemData.count();
1051 for (int i = 0; i < itemDataCount; ++i) {
1052 m_items.insert(m_itemData.at(i)->item.url(), i);
1053 }
1054
1055 if (count() <= 0) {
1056 m_expandedParentsCountRoot = UninitializedExpandedParentsCountRoot;
1057 }
1058
1059 itemRanges << KItemRange(removedAtIndex, removedCount);
1060 emit itemsRemoved(itemRanges);
1061 }
1062
1063 QList<KFileItemModel::ItemData*> KFileItemModel::createItemDataList(const KFileItemList& items) const
1064 {
1065 QList<ItemData*> itemDataList;
1066 itemDataList.reserve(items.count());
1067
1068 foreach (const KFileItem& item, items) {
1069 ItemData* itemData = new ItemData();
1070 itemData->item = item;
1071 itemData->values = retrieveData(item);
1072 itemData->parent = 0;
1073
1074 const bool determineParent = m_requestRole[ExpandedParentsCountRole]
1075 && itemData->values["expandedParentsCount"].toInt() > 0;
1076 if (determineParent) {
1077 KUrl parentUrl = item.url().upUrl();
1078 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
1079 const int parentIndex = m_items.value(parentUrl, -1);
1080 if (parentIndex >= 0) {
1081 itemData->parent = m_itemData.at(parentIndex);
1082 } else {
1083 kWarning() << "Parent item not found for" << item.url();
1084 }
1085 }
1086
1087 itemDataList.append(itemData);
1088 }
1089
1090 return itemDataList;
1091 }
1092
1093 void KFileItemModel::removeExpandedItems()
1094 {
1095 KFileItemList expandedItems;
1096
1097 const int maxIndex = m_itemData.count() - 1;
1098 for (int i = 0; i <= maxIndex; ++i) {
1099 const ItemData* itemData = m_itemData.at(i);
1100 if (itemData->values.value("expandedParentsCount").toInt() > 0) {
1101 expandedItems.append(itemData->item);
1102 }
1103 }
1104
1105 // The m_expandedParentsCountRoot may not get reset before all items with
1106 // a bigger count have been removed.
1107 removeItems(expandedItems);
1108
1109 m_expandedParentsCountRoot = UninitializedExpandedParentsCountRoot;
1110 m_expandedDirs.clear();
1111 }
1112
1113 void KFileItemModel::resetRoles()
1114 {
1115 for (int i = 0; i < RolesCount; ++i) {
1116 m_requestRole[i] = false;
1117 }
1118 }
1119
1120 KFileItemModel::RoleType KFileItemModel::typeForRole(const QByteArray& role) const
1121 {
1122 static QHash<QByteArray, RoleType> roles;
1123 if (roles.isEmpty()) {
1124 // Insert user visible roles that can be accessed with
1125 // KFileItemModel::roleInformation()
1126 int count = 0;
1127 const RoleInfoMap* map = rolesInfoMap(count);
1128 for (int i = 0; i < count; ++i) {
1129 roles.insert(map[i].role, map[i].roleType);
1130 }
1131
1132 // Insert internal roles (take care to synchronize the implementation
1133 // with KFileItemModel::roleForType() in case if a change is done).
1134 roles.insert("isDir", IsDirRole);
1135 roles.insert("isLink", IsLinkRole);
1136 roles.insert("isExpanded", IsExpandedRole);
1137 roles.insert("isExpandable", IsExpandableRole);
1138 roles.insert("expandedParentsCount", ExpandedParentsCountRole);
1139
1140 Q_ASSERT(roles.count() == RolesCount);
1141 }
1142
1143 return roles.value(role, NoRole);
1144 }
1145
1146 QByteArray KFileItemModel::roleForType(RoleType roleType) const
1147 {
1148 static QHash<RoleType, QByteArray> roles;
1149 if (roles.isEmpty()) {
1150 // Insert user visible roles that can be accessed with
1151 // KFileItemModel::roleInformation()
1152 int count = 0;
1153 const RoleInfoMap* map = rolesInfoMap(count);
1154 for (int i = 0; i < count; ++i) {
1155 roles.insert(map[i].roleType, map[i].role);
1156 }
1157
1158 // Insert internal roles (take care to synchronize the implementation
1159 // with KFileItemModel::typeForRole() in case if a change is done).
1160 roles.insert(IsDirRole, "isDir");
1161 roles.insert(IsLinkRole, "isLink");
1162 roles.insert(IsExpandedRole, "isExpanded");
1163 roles.insert(IsExpandableRole, "isExpandable");
1164 roles.insert(ExpandedParentsCountRole, "expandedParentsCount");
1165
1166 Q_ASSERT(roles.count() == RolesCount);
1167 };
1168
1169 return roles.value(roleType);
1170 }
1171
1172 QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item) const
1173 {
1174 // It is important to insert only roles that are fast to retrieve. E.g.
1175 // KFileItem::iconName() can be very expensive if the MIME-type is unknown
1176 // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater.
1177 QHash<QByteArray, QVariant> data;
1178 data.insert("url", item.url());
1179
1180 const bool isDir = item.isDir();
1181 if (m_requestRole[IsDirRole]) {
1182 data.insert("isDir", isDir);
1183 }
1184
1185 if (m_requestRole[IsLinkRole]) {
1186 const bool isLink = item.isLink();
1187 data.insert("isLink", isLink);
1188 }
1189
1190 if (m_requestRole[NameRole]) {
1191 data.insert("text", item.text());
1192 }
1193
1194 if (m_requestRole[SizeRole]) {
1195 if (isDir) {
1196 data.insert("size", QVariant());
1197 } else {
1198 data.insert("size", item.size());
1199 }
1200 }
1201
1202 if (m_requestRole[DateRole]) {
1203 // Don't use KFileItem::timeString() as this is too expensive when
1204 // having several thousands of items. Instead the formatting of the
1205 // date-time will be done on-demand by the view when the date will be shown.
1206 const KDateTime dateTime = item.time(KFileItem::ModificationTime);
1207 data.insert("date", dateTime.dateTime());
1208 }
1209
1210 if (m_requestRole[PermissionsRole]) {
1211 data.insert("permissions", item.permissionsString());
1212 }
1213
1214 if (m_requestRole[OwnerRole]) {
1215 data.insert("owner", item.user());
1216 }
1217
1218 if (m_requestRole[GroupRole]) {
1219 data.insert("group", item.group());
1220 }
1221
1222 if (m_requestRole[DestinationRole]) {
1223 QString destination = item.linkDest();
1224 if (destination.isEmpty()) {
1225 destination = QLatin1String("-");
1226 }
1227 data.insert("destination", destination);
1228 }
1229
1230 if (m_requestRole[PathRole]) {
1231 QString path;
1232 if (item.url().protocol() == QLatin1String("trash")) {
1233 path = item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA);
1234 } else {
1235 // For performance reasons cache the home-path in a static QString
1236 // (see QDir::homePath() for more details)
1237 static QString homePath;
1238 if (homePath.isEmpty()) {
1239 homePath = QDir::homePath();
1240 }
1241
1242 path = item.localPath();
1243 if (path.startsWith(homePath)) {
1244 path.replace(0, homePath.length(), QLatin1Char('~'));
1245 }
1246 }
1247
1248 const int index = path.lastIndexOf(item.text());
1249 path = path.mid(0, index - 1);
1250 data.insert("path", path);
1251 }
1252
1253 if (m_requestRole[IsExpandedRole]) {
1254 data.insert("isExpanded", false);
1255 }
1256
1257 if (m_requestRole[IsExpandableRole]) {
1258 data.insert("isExpandable", item.isDir() && item.url() == item.targetUrl());
1259 }
1260
1261 if (m_requestRole[ExpandedParentsCountRole]) {
1262 if (m_expandedParentsCountRoot == UninitializedExpandedParentsCountRoot) {
1263 const KUrl rootUrl = m_dirLister->url();
1264 const QString protocol = rootUrl.protocol();
1265 const bool forceExpandedParentsCountRoot = (protocol == QLatin1String("trash") ||
1266 protocol == QLatin1String("nepomuk") ||
1267 protocol == QLatin1String("remote") ||
1268 protocol.contains(QLatin1String("search")));
1269 if (forceExpandedParentsCountRoot) {
1270 m_expandedParentsCountRoot = ForceExpandedParentsCountRoot;
1271 } else {
1272 const QString rootDir = rootUrl.path(KUrl::AddTrailingSlash);
1273 m_expandedParentsCountRoot = rootDir.count('/');
1274 }
1275 }
1276
1277 if (m_expandedParentsCountRoot == ForceExpandedParentsCountRoot) {
1278 data.insert("expandedParentsCount", -1);
1279 } else {
1280 const QString dir = item.url().directory(KUrl::AppendTrailingSlash);
1281 const int level = dir.count('/') - m_expandedParentsCountRoot;
1282 data.insert("expandedParentsCount", level);
1283 }
1284 }
1285
1286 if (item.isMimeTypeKnown()) {
1287 data.insert("iconName", item.iconName());
1288
1289 if (m_requestRole[TypeRole]) {
1290 data.insert("type", item.mimeComment());
1291 }
1292 }
1293
1294 return data;
1295 }
1296
1297 bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
1298 {
1299 int result = 0;
1300
1301 if (m_expandedParentsCountRoot >= 0) {
1302 result = expandedParentsCountCompare(a, b);
1303 if (result != 0) {
1304 // The items have parents with different expansion levels
1305 return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
1306 }
1307 }
1308
1309 if (m_sortDirsFirst || m_sortRole == SizeRole) {
1310 const bool isDirA = a->item.isDir();
1311 const bool isDirB = b->item.isDir();
1312 if (isDirA && !isDirB) {
1313 return true;
1314 } else if (!isDirA && isDirB) {
1315 return false;
1316 }
1317 }
1318
1319 result = sortRoleCompare(a, b);
1320
1321 return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
1322 }
1323
1324 int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
1325 {
1326 const KFileItem& itemA = a->item;
1327 const KFileItem& itemB = b->item;
1328
1329 int result = 0;
1330
1331 switch (m_sortRole) {
1332 case NameRole:
1333 // The name role is handled as default fallback after the switch
1334 break;
1335
1336 case SizeRole: {
1337 if (itemA.isDir()) {
1338 // See "if (m_sortFoldersFirst || m_sortRole == SizeRole)" in KFileItemModel::lessThan():
1339 Q_ASSERT(itemB.isDir());
1340
1341 const QVariant valueA = a->values.value("size");
1342 const QVariant valueB = b->values.value("size");
1343 if (valueA.isNull() && valueB.isNull()) {
1344 result = 0;
1345 } else if (valueA.isNull()) {
1346 result = -1;
1347 } else if (valueB.isNull()) {
1348 result = +1;
1349 } else {
1350 result = valueA.toInt() - valueB.toInt();
1351 }
1352 } else {
1353 // See "if (m_sortFoldersFirst || m_sortRole == SizeRole)" in KFileItemModel::lessThan():
1354 Q_ASSERT(!itemB.isDir());
1355 const KIO::filesize_t sizeA = itemA.size();
1356 const KIO::filesize_t sizeB = itemB.size();
1357 if (sizeA > sizeB) {
1358 result = +1;
1359 } else if (sizeA < sizeB) {
1360 result = -1;
1361 } else {
1362 result = 0;
1363 }
1364 }
1365 break;
1366 }
1367
1368 case DateRole: {
1369 const KDateTime dateTimeA = itemA.time(KFileItem::ModificationTime);
1370 const KDateTime dateTimeB = itemB.time(KFileItem::ModificationTime);
1371 if (dateTimeA < dateTimeB) {
1372 result = -1;
1373 } else if (dateTimeA > dateTimeB) {
1374 result = +1;
1375 }
1376 break;
1377 }
1378
1379 case RatingRole: {
1380 result = a->values.value("rating").toInt() - b->values.value("rating").toInt();
1381 break;
1382 }
1383
1384 case ImageSizeRole: {
1385 // Alway use a natural comparing to interpret the numbers of a string like
1386 // "1600 x 1200" for having a correct sorting.
1387 result = KStringHandler::naturalCompare(a->values.value("imageSize").toString(),
1388 b->values.value("imageSize").toString(),
1389 Qt::CaseSensitive);
1390 break;
1391 }
1392
1393 default: {
1394 const QByteArray role = roleForType(m_sortRole);
1395 result = QString::compare(a->values.value(role).toString(),
1396 b->values.value(role).toString());
1397 break;
1398 }
1399
1400 }
1401
1402 if (result != 0) {
1403 // The current sort role was sufficient to define an order
1404 return result;
1405 }
1406
1407 // Fallback #1: Compare the text of the items
1408 result = stringCompare(itemA.text(), itemB.text());
1409 if (result != 0) {
1410 return result;
1411 }
1412
1413 // Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
1414 result = stringCompare(itemA.name(m_caseSensitivity == Qt::CaseInsensitive),
1415 itemB.name(m_caseSensitivity == Qt::CaseInsensitive));
1416 if (result != 0) {
1417 return result;
1418 }
1419
1420 // Fallback #3: It must be assured that the sort order is always unique even if two values have been
1421 // equal. In this case a comparison of the URL is done which is unique in all cases
1422 // within KDirLister.
1423 return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive);
1424 }
1425
1426 int KFileItemModel::stringCompare(const QString& a, const QString& b) const
1427 {
1428 // Taken from KDirSortFilterProxyModel (kdelibs/kfile/kdirsortfilterproxymodel.*)
1429 // Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>
1430 // Copyright (C) 2006 by Dominic Battre <dominic@battre.de>
1431 // Copyright (C) 2006 by Martin Pool <mbp@canonical.com>
1432
1433 if (m_caseSensitivity == Qt::CaseInsensitive) {
1434 const int result = m_naturalSorting ? KStringHandler::naturalCompare(a, b, Qt::CaseInsensitive)
1435 : QString::compare(a, b, Qt::CaseInsensitive);
1436 if (result != 0) {
1437 // Only return the result, if the strings are not equal. If they are equal by a case insensitive
1438 // comparison, still a deterministic sort order is required. A case sensitive
1439 // comparison is done as fallback.
1440 return result;
1441 }
1442 }
1443
1444 return m_naturalSorting ? KStringHandler::naturalCompare(a, b, Qt::CaseSensitive)
1445 : QString::compare(a, b, Qt::CaseSensitive);
1446 }
1447
1448 int KFileItemModel::expandedParentsCountCompare(const ItemData* a, const ItemData* b) const
1449 {
1450 const KUrl urlA = a->item.url();
1451 const KUrl urlB = b->item.url();
1452 if (urlA.directory() == urlB.directory()) {
1453 // Both items have the same directory as parent
1454 return 0;
1455 }
1456
1457 // Check whether one item is the parent of the other item
1458 if (urlA.isParentOf(urlB)) {
1459 return (sortOrder() == Qt::AscendingOrder) ? -1 : +1;
1460 } else if (urlB.isParentOf(urlA)) {
1461 return (sortOrder() == Qt::AscendingOrder) ? +1 : -1;
1462 }
1463
1464 // Determine the maximum common path of both items and
1465 // remember the index in 'index'
1466 const QString pathA = urlA.path();
1467 const QString pathB = urlB.path();
1468
1469 const int maxIndex = qMin(pathA.length(), pathB.length()) - 1;
1470 int index = 0;
1471 while (index <= maxIndex && pathA.at(index) == pathB.at(index)) {
1472 ++index;
1473 }
1474 if (index > maxIndex) {
1475 index = maxIndex;
1476 }
1477 while ((pathA.at(index) != QLatin1Char('/') || pathB.at(index) != QLatin1Char('/')) && index > 0) {
1478 --index;
1479 }
1480
1481 // Determine the first sub-path after the common path and
1482 // check whether it represents a directory or already a file
1483 bool isDirA = true;
1484 const QString subPathA = subPath(a->item, pathA, index, &isDirA);
1485 bool isDirB = true;
1486 const QString subPathB = subPath(b->item, pathB, index, &isDirB);
1487
1488 if (m_sortDirsFirst || m_sortRole == SizeRole) {
1489 if (isDirA && !isDirB) {
1490 return (sortOrder() == Qt::AscendingOrder) ? -1 : +1;
1491 } else if (!isDirA && isDirB) {
1492 return (sortOrder() == Qt::AscendingOrder) ? +1 : -1;
1493 }
1494 }
1495
1496 // Compare the items of the parents that represent the first
1497 // different path after the common path.
1498 const QString parentPathA = pathA.left(index) + subPathA;
1499 const QString parentPathB = pathB.left(index) + subPathB;
1500
1501 const ItemData* parentA = a;
1502 while (parentA && parentA->item.url().path() != parentPathA) {
1503 parentA = parentA->parent;
1504 }
1505
1506 const ItemData* parentB = b;
1507 while (parentB && parentB->item.url().path() != parentPathB) {
1508 parentB = parentB->parent;
1509 }
1510
1511 if (parentA && parentB) {
1512 return sortRoleCompare(parentA, parentB);
1513 }
1514
1515 kWarning() << "Child items without parent detected:" << a->item.url() << b->item.url();
1516 return QString::compare(urlA.url(), urlB.url(), Qt::CaseSensitive);
1517 }
1518
1519 QString KFileItemModel::subPath(const KFileItem& item,
1520 const QString& itemPath,
1521 int start,
1522 bool* isDir) const
1523 {
1524 Q_ASSERT(isDir);
1525 const int pathIndex = itemPath.indexOf('/', start + 1);
1526 *isDir = (pathIndex > 0) || item.isDir();
1527 return itemPath.mid(start, pathIndex - start);
1528 }
1529
1530 bool KFileItemModel::useMaximumUpdateInterval() const
1531 {
1532 return !m_dirLister->url().isLocalFile();
1533 }
1534
1535 QList<QPair<int, QVariant> > KFileItemModel::nameRoleGroups() const
1536 {
1537 Q_ASSERT(!m_itemData.isEmpty());
1538
1539 const int maxIndex = count() - 1;
1540 QList<QPair<int, QVariant> > groups;
1541
1542 QString groupValue;
1543 QChar firstChar;
1544 bool isLetter = false;
1545 for (int i = 0; i <= maxIndex; ++i) {
1546 if (isChildItem(i)) {
1547 continue;
1548 }
1549
1550 const QString name = m_itemData.at(i)->values.value("text").toString();
1551
1552 // Use the first character of the name as group indication
1553 QChar newFirstChar = name.at(0).toUpper();
1554 if (newFirstChar == QLatin1Char('~') && name.length() > 1) {
1555 newFirstChar = name.at(1).toUpper();
1556 }
1557
1558 if (firstChar != newFirstChar) {
1559 QString newGroupValue;
1560 if (newFirstChar >= QLatin1Char('A') && newFirstChar <= QLatin1Char('Z')) {
1561 // Apply group 'A' - 'Z'
1562 newGroupValue = newFirstChar;
1563 isLetter = true;
1564 } else if (newFirstChar >= QLatin1Char('0') && newFirstChar <= QLatin1Char('9')) {
1565 // Apply group '0 - 9' for any name that starts with a digit
1566 newGroupValue = i18nc("@title:group Groups that start with a digit", "0 - 9");
1567 isLetter = false;
1568 } else {
1569 if (isLetter) {
1570 // If the current group is 'A' - 'Z' check whether a locale character
1571 // fits into the existing group.
1572 // TODO: This does not work in the case if e.g. the group 'O' starts with
1573 // an umlaut 'O' -> provide unit-test to document this known issue
1574 const QChar prevChar(firstChar.unicode() - ushort(1));
1575 const QChar nextChar(firstChar.unicode() + ushort(1));
1576 const QString currChar(newFirstChar);
1577 const bool partOfCurrentGroup = currChar.localeAwareCompare(prevChar) > 0 &&
1578 currChar.localeAwareCompare(nextChar) < 0;
1579 if (partOfCurrentGroup) {
1580 continue;
1581 }
1582 }
1583 newGroupValue = i18nc("@title:group", "Others");
1584 isLetter = false;
1585 }
1586
1587 if (newGroupValue != groupValue) {
1588 groupValue = newGroupValue;
1589 groups.append(QPair<int, QVariant>(i, newGroupValue));
1590 }
1591
1592 firstChar = newFirstChar;
1593 }
1594 }
1595 return groups;
1596 }
1597
1598 QList<QPair<int, QVariant> > KFileItemModel::sizeRoleGroups() const
1599 {
1600 Q_ASSERT(!m_itemData.isEmpty());
1601
1602 const int maxIndex = count() - 1;
1603 QList<QPair<int, QVariant> > groups;
1604
1605 QString groupValue;
1606 for (int i = 0; i <= maxIndex; ++i) {
1607 if (isChildItem(i)) {
1608 continue;
1609 }
1610
1611 const KFileItem& item = m_itemData.at(i)->item;
1612 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
1613 QString newGroupValue;
1614 if (!item.isNull() && item.isDir()) {
1615 newGroupValue = i18nc("@title:group Size", "Folders");
1616 } else if (fileSize < 5 * 1024 * 1024) {
1617 newGroupValue = i18nc("@title:group Size", "Small");
1618 } else if (fileSize < 10 * 1024 * 1024) {
1619 newGroupValue = i18nc("@title:group Size", "Medium");
1620 } else {
1621 newGroupValue = i18nc("@title:group Size", "Big");
1622 }
1623
1624 if (newGroupValue != groupValue) {
1625 groupValue = newGroupValue;
1626 groups.append(QPair<int, QVariant>(i, newGroupValue));
1627 }
1628 }
1629
1630 return groups;
1631 }
1632
1633 QList<QPair<int, QVariant> > KFileItemModel::dateRoleGroups() const
1634 {
1635 Q_ASSERT(!m_itemData.isEmpty());
1636
1637 const int maxIndex = count() - 1;
1638 QList<QPair<int, QVariant> > groups;
1639
1640 const QDate currentDate = KDateTime::currentLocalDateTime().date();
1641
1642 int yearForCurrentWeek = 0;
1643 int currentWeek = currentDate.weekNumber(&yearForCurrentWeek);
1644 if (yearForCurrentWeek == currentDate.year() + 1) {
1645 currentWeek = 53;
1646 }
1647
1648 QDate previousModifiedDate;
1649 QString groupValue;
1650 for (int i = 0; i <= maxIndex; ++i) {
1651 if (isChildItem(i)) {
1652 continue;
1653 }
1654
1655 const KDateTime modifiedTime = m_itemData.at(i)->item.time(KFileItem::ModificationTime);
1656 const QDate modifiedDate = modifiedTime.date();
1657 if (modifiedDate == previousModifiedDate) {
1658 // The current item is in the same group as the previous item
1659 continue;
1660 }
1661 previousModifiedDate = modifiedDate;
1662
1663 const int daysDistance = modifiedDate.daysTo(currentDate);
1664
1665 int yearForModifiedWeek = 0;
1666 int modifiedWeek = modifiedDate.weekNumber(&yearForModifiedWeek);
1667 if (yearForModifiedWeek == modifiedDate.year() + 1) {
1668 modifiedWeek = 53;
1669 }
1670
1671 QString newGroupValue;
1672 if (currentDate.year() == modifiedDate.year() && currentDate.month() == modifiedDate.month()) {
1673 if (modifiedWeek > currentWeek) {
1674 // Usecase: modified date = 2010-01-01, current date = 2010-01-22
1675 // modified week = 53, current week = 3
1676 modifiedWeek = 0;
1677 }
1678 switch (currentWeek - modifiedWeek) {
1679 case 0:
1680 switch (daysDistance) {
1681 case 0: newGroupValue = i18nc("@title:group Date", "Today"); break;
1682 case 1: newGroupValue = i18nc("@title:group Date", "Yesterday"); break;
1683 default: newGroupValue = modifiedTime.toString(i18nc("@title:group The week day name: %A", "%A"));
1684 }
1685 break;
1686 case 1:
1687 newGroupValue = i18nc("@title:group Date", "Last Week");
1688 break;
1689 case 2:
1690 newGroupValue = i18nc("@title:group Date", "Two Weeks Ago");
1691 break;
1692 case 3:
1693 newGroupValue = i18nc("@title:group Date", "Three Weeks Ago");
1694 break;
1695 case 4:
1696 case 5:
1697 newGroupValue = i18nc("@title:group Date", "Earlier this Month");
1698 break;
1699 default:
1700 Q_ASSERT(false);
1701 }
1702 } else {
1703 const QDate lastMonthDate = currentDate.addMonths(-1);
1704 if (lastMonthDate.year() == modifiedDate.year() && lastMonthDate.month() == modifiedDate.month()) {
1705 if (daysDistance == 1) {
1706 newGroupValue = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
1707 } else if (daysDistance <= 7) {
1708 newGroupValue = modifiedTime.toString(i18nc("@title:group The week day name: %A, %B is full month name in current locale, and %Y is full year number", "%A (%B, %Y)"));
1709 } else if (daysDistance <= 7 * 2) {
1710 newGroupValue = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Last Week (%B, %Y)"));
1711 } else if (daysDistance <= 7 * 3) {
1712 newGroupValue = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Two Weeks Ago (%B, %Y)"));
1713 } else if (daysDistance <= 7 * 4) {
1714 newGroupValue = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Three Weeks Ago (%B, %Y)"));
1715 } else {
1716 newGroupValue = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Earlier on %B, %Y"));
1717 }
1718 } else {
1719 newGroupValue = modifiedTime.toString(i18nc("@title:group The month and year: %B is full month name in current locale, and %Y is full year number", "%B, %Y"));
1720 }
1721 }
1722
1723 if (newGroupValue != groupValue) {
1724 groupValue = newGroupValue;
1725 groups.append(QPair<int, QVariant>(i, newGroupValue));
1726 }
1727 }
1728
1729 return groups;
1730 }
1731
1732 QList<QPair<int, QVariant> > KFileItemModel::permissionRoleGroups() const
1733 {
1734 Q_ASSERT(!m_itemData.isEmpty());
1735
1736 const int maxIndex = count() - 1;
1737 QList<QPair<int, QVariant> > groups;
1738
1739 QString permissionsString;
1740 QString groupValue;
1741 for (int i = 0; i <= maxIndex; ++i) {
1742 if (isChildItem(i)) {
1743 continue;
1744 }
1745
1746 const ItemData* itemData = m_itemData.at(i);
1747 const QString newPermissionsString = itemData->values.value("permissions").toString();
1748 if (newPermissionsString == permissionsString) {
1749 continue;
1750 }
1751 permissionsString = newPermissionsString;
1752
1753 const QFileInfo info(itemData->item.url().pathOrUrl());
1754
1755 // Set user string
1756 QString user;
1757 if (info.permission(QFile::ReadUser)) {
1758 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
1759 }
1760 if (info.permission(QFile::WriteUser)) {
1761 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
1762 }
1763 if (info.permission(QFile::ExeUser)) {
1764 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
1765 }
1766 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.count() - 2);
1767
1768 // Set group string
1769 QString group;
1770 if (info.permission(QFile::ReadGroup)) {
1771 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
1772 }
1773 if (info.permission(QFile::WriteGroup)) {
1774 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
1775 }
1776 if (info.permission(QFile::ExeGroup)) {
1777 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
1778 }
1779 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.count() - 2);
1780
1781 // Set others string
1782 QString others;
1783 if (info.permission(QFile::ReadOther)) {
1784 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
1785 }
1786 if (info.permission(QFile::WriteOther)) {
1787 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
1788 }
1789 if (info.permission(QFile::ExeOther)) {
1790 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
1791 }
1792 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.count() - 2);
1793
1794 const QString newGroupValue = i18nc("@title:group Files and folders by permissions", "User: %1 | Group: %2 | Others: %3", user, group, others);
1795 if (newGroupValue != groupValue) {
1796 groupValue = newGroupValue;
1797 groups.append(QPair<int, QVariant>(i, newGroupValue));
1798 }
1799 }
1800
1801 return groups;
1802 }
1803
1804 QList<QPair<int, QVariant> > KFileItemModel::ratingRoleGroups() const
1805 {
1806 Q_ASSERT(!m_itemData.isEmpty());
1807
1808 const int maxIndex = count() - 1;
1809 QList<QPair<int, QVariant> > groups;
1810
1811 int groupValue = -1;
1812 for (int i = 0; i <= maxIndex; ++i) {
1813 if (isChildItem(i)) {
1814 continue;
1815 }
1816 const int newGroupValue = m_itemData.at(i)->values.value("rating", 0).toInt();
1817 if (newGroupValue != groupValue) {
1818 groupValue = newGroupValue;
1819 groups.append(QPair<int, QVariant>(i, newGroupValue));
1820 }
1821 }
1822
1823 return groups;
1824 }
1825
1826 QList<QPair<int, QVariant> > KFileItemModel::genericStringRoleGroups(const QByteArray& role) const
1827 {
1828 Q_ASSERT(!m_itemData.isEmpty());
1829
1830 const int maxIndex = count() - 1;
1831 QList<QPair<int, QVariant> > groups;
1832
1833 bool isFirstGroupValue = true;
1834 QString groupValue;
1835 for (int i = 0; i <= maxIndex; ++i) {
1836 if (isChildItem(i)) {
1837 continue;
1838 }
1839 const QString newGroupValue = m_itemData.at(i)->values.value(role).toString();
1840 if (newGroupValue != groupValue || isFirstGroupValue) {
1841 groupValue = newGroupValue;
1842 groups.append(QPair<int, QVariant>(i, newGroupValue));
1843 isFirstGroupValue = false;
1844 }
1845 }
1846
1847 return groups;
1848 }
1849
1850 KFileItemList KFileItemModel::childItems(const KFileItem& item) const
1851 {
1852 KFileItemList items;
1853
1854 int index = m_items.value(item.url(), -1);
1855 if (index >= 0) {
1856 const int parentLevel = m_itemData.at(index)->values.value("expandedParentsCount").toInt();
1857 ++index;
1858 while (index < m_itemData.count() && m_itemData.at(index)->values.value("expandedParentsCount").toInt() > parentLevel) {
1859 items.append(m_itemData.at(index)->item);
1860 ++index;
1861 }
1862 }
1863
1864 return items;
1865 }
1866
1867 void KFileItemModel::emitSortProgress(int resolvedCount)
1868 {
1869 // Be tolerant against a resolvedCount with a wrong range.
1870 // Although there should not be a case where KFileItemModelRolesUpdater
1871 // (= caller) provides a wrong range, it is important to emit
1872 // a useful progress information even if there is an unexpected
1873 // implementation issue.
1874
1875 const int itemCount = count();
1876 if (resolvedCount >= itemCount) {
1877 m_sortingProgressPercent = -1;
1878 if (m_resortAllItemsTimer->isActive()) {
1879 m_resortAllItemsTimer->stop();
1880 resortAllItems();
1881 }
1882
1883 emit directorySortingProgress(100);
1884 } else if (itemCount > 0) {
1885 resolvedCount = qBound(0, resolvedCount, itemCount);
1886
1887 const int progress = resolvedCount * 100 / itemCount;
1888 if (m_sortingProgressPercent != progress) {
1889 m_sortingProgressPercent = progress;
1890 emit directorySortingProgress(progress);
1891 }
1892 }
1893 }
1894
1895 const KFileItemModel::RoleInfoMap* KFileItemModel::rolesInfoMap(int& count)
1896 {
1897 static const RoleInfoMap rolesInfoMap[] = {
1898 // | role | roleType | role translation | group translation | requires Nepomuk | requires indexer
1899 { 0, NoRole, 0, 0, 0, 0, false, false },
1900 { "text", NameRole, I18N_NOOP2_NOSTRIP("@label", "Name"), 0, 0, false, false },
1901 { "size", SizeRole, I18N_NOOP2_NOSTRIP("@label", "Size"), 0, 0, false, false },
1902 { "date", DateRole, I18N_NOOP2_NOSTRIP("@label", "Date"), 0, 0, false, false },
1903 { "type", TypeRole, I18N_NOOP2_NOSTRIP("@label", "Type"), 0, 0, false, false },
1904 { "rating", RatingRole, I18N_NOOP2_NOSTRIP("@label", "Rating"), 0, 0, true, false },
1905 { "tags", TagsRole, I18N_NOOP2_NOSTRIP("@label", "Tags"), 0, 0, true, false },
1906 { "comment", CommentRole, I18N_NOOP2_NOSTRIP("@label", "Comment"), 0, 0, true, false },
1907 { "wordCount", WordCountRole, I18N_NOOP2_NOSTRIP("@label", "Word Count"), I18N_NOOP2_NOSTRIP("@label", "Document"), true, true },
1908 { "lineCount", LineCountRole, I18N_NOOP2_NOSTRIP("@label", "Line Count"), I18N_NOOP2_NOSTRIP("@label", "Document"), true, true },
1909 { "imageSize", ImageSizeRole, I18N_NOOP2_NOSTRIP("@label", "Image Size"), I18N_NOOP2_NOSTRIP("@label", "Image"), true, true },
1910 { "orientation", OrientationRole, I18N_NOOP2_NOSTRIP("@label", "Orientation"), I18N_NOOP2_NOSTRIP("@label", "Image"), true, true },
1911 { "artist", ArtistRole, I18N_NOOP2_NOSTRIP("@label", "Artist"), I18N_NOOP2_NOSTRIP("@label", "Audio"), true, true },
1912 { "album", AlbumRole, I18N_NOOP2_NOSTRIP("@label", "Album"), I18N_NOOP2_NOSTRIP("@label", "Audio"), true, true },
1913 { "duration", DurationRole, I18N_NOOP2_NOSTRIP("@label", "Duration"), I18N_NOOP2_NOSTRIP("@label", "Audio"), true, true },
1914 { "track", TrackRole, I18N_NOOP2_NOSTRIP("@label", "Track"), I18N_NOOP2_NOSTRIP("@label", "Audio"), true, true },
1915 { "path", PathRole, I18N_NOOP2_NOSTRIP("@label", "Path"), I18N_NOOP2_NOSTRIP("@label", "Other"), false, false },
1916 { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), I18N_NOOP2_NOSTRIP("@label", "Other"), false, false },
1917 { "copiedFrom", CopiedFromRole, I18N_NOOP2_NOSTRIP("@label", "Copied From"), I18N_NOOP2_NOSTRIP("@label", "Other"), true, false },
1918 { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"), I18N_NOOP2_NOSTRIP("@label", "Other"), false, false },
1919 { "owner", OwnerRole, I18N_NOOP2_NOSTRIP("@label", "Owner"), I18N_NOOP2_NOSTRIP("@label", "Other"), false, false },
1920 { "group", GroupRole, I18N_NOOP2_NOSTRIP("@label", "User Group"), I18N_NOOP2_NOSTRIP("@label", "Other"), false, false },
1921 };
1922
1923 count = sizeof(rolesInfoMap) / sizeof(RoleInfoMap);
1924 return rolesInfoMap;
1925 }
1926
1927 void KFileItemModel::determineMimeTypes(const KFileItemList& items, int timeout)
1928 {
1929 QElapsedTimer timer;
1930 timer.start();
1931 foreach (KFileItem item, items) { // krazy:exclude=foreach
1932 item.determineMimeType();
1933 if (timer.elapsed() > timeout) {
1934 // Don't block the user interface, let the remaining items
1935 // be resolved asynchronously.
1936 return;
1937 }
1938 }
1939 }
1940
1941 #include "kfileitemmodel.moc"