]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.cpp
Make calls to resolveNextPendingRoles and resolveNextSortRole delayed
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.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 "kfileitemmodelrolesupdater.h"
21
22 #include "kfileitemmodel.h"
23
24 #include <KConfig>
25 #include <KConfigGroup>
26 #include <KDebug>
27 #include <KDirWatch>
28 #include <KFileItem>
29 #include <KGlobal>
30 #include <KIO/JobUiDelegate>
31 #include <KIO/PreviewJob>
32
33 #include "private/kpixmapmodifier.h"
34
35 #include <QApplication>
36 #include <QPainter>
37 #include <QPixmap>
38 #include <QElapsedTimer>
39 #include <QTimer>
40
41 #include <algorithm>
42
43 #ifdef HAVE_NEPOMUK
44 #include "private/knepomukrolesprovider.h"
45 #include <Nepomuk2/ResourceWatcher>
46 #endif
47
48 // Required includes for subItemsCount():
49 #ifdef Q_WS_WIN
50 #include <QDir>
51 #else
52 #include <dirent.h>
53 #include <QFile>
54 #endif
55
56 // #define KFILEITEMMODELROLESUPDATER_DEBUG
57
58 namespace {
59 // Maximum time in ms that the KFileItemModelRolesUpdater
60 // may perform a blocking operation
61 const int MaxBlockTimeout = 200;
62
63 // If the number of items is smaller than ResolveAllItemsLimit,
64 // the roles of all items will be resolved.
65 const int ResolveAllItemsLimit = 500;
66
67 // Not only the visible area, but up to ReadAheadPages before and after
68 // this area will be resolved.
69 const int ReadAheadPages = 5;
70 }
71
72 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent) :
73 QObject(parent),
74 m_state(Idle),
75 m_previewChangedDuringPausing(false),
76 m_iconSizeChangedDuringPausing(false),
77 m_rolesChangedDuringPausing(false),
78 m_previewShown(false),
79 m_enlargeSmallPreviews(true),
80 m_clearPreviews(false),
81 m_finishedItems(),
82 m_model(model),
83 m_iconSize(),
84 m_firstVisibleIndex(0),
85 m_lastVisibleIndex(-1),
86 m_maximumVisibleItems(50),
87 m_roles(),
88 m_resolvableRoles(),
89 m_enabledPlugins(),
90 m_pendingSortRoleItems(),
91 m_pendingSortRoleIndexes(),
92 m_pendingIndexes(),
93 m_pendingPreviewItems(),
94 m_previewJob(),
95 m_recentlyChangedItemsTimer(0),
96 m_recentlyChangedItems(),
97 m_changedItems(),
98 m_dirWatcher(0),
99 m_watchedDirs()
100 #ifdef HAVE_NEPOMUK
101 , m_nepomukResourceWatcher(0),
102 m_nepomukUriItems()
103 #endif
104 {
105 Q_ASSERT(model);
106
107 const KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
108 m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList()
109 << "directorythumbnail"
110 << "imagethumbnail"
111 << "jpegthumbnail");
112
113 connect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
114 this, SLOT(slotItemsInserted(KItemRangeList)));
115 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
116 this, SLOT(slotItemsRemoved(KItemRangeList)));
117 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
118 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
119 connect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
120 this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
121 connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
122 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
123
124 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
125 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
126 m_recentlyChangedItemsTimer = new QTimer(this);
127 m_recentlyChangedItemsTimer->setInterval(1000);
128 m_recentlyChangedItemsTimer->setSingleShot(true);
129 connect(m_recentlyChangedItemsTimer, SIGNAL(timeout()), this, SLOT(resolveRecentlyChangedItems()));
130
131 m_resolvableRoles.insert("size");
132 m_resolvableRoles.insert("type");
133 m_resolvableRoles.insert("isExpandable");
134 #ifdef HAVE_NEPOMUK
135 m_resolvableRoles += KNepomukRolesProvider::instance().roles();
136 #endif
137
138 // When folders are expandable or the item-count is shown for folders, it is necessary
139 // to watch the number of items of the sub-folder to be able to react on changes.
140 m_dirWatcher = new KDirWatch(this);
141 connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
142 }
143
144 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
145 {
146 killPreviewJob();
147 }
148
149 void KFileItemModelRolesUpdater::setIconSize(const QSize& size)
150 {
151 if (size != m_iconSize) {
152 m_iconSize = size;
153 if (m_state == Paused) {
154 m_iconSizeChangedDuringPausing = true;
155 } else if (m_previewShown) {
156 // An icon size change requires the regenerating of
157 // all previews
158 m_finishedItems.clear();
159 startUpdating();
160 }
161 }
162 }
163
164 QSize KFileItemModelRolesUpdater::iconSize() const
165 {
166 return m_iconSize;
167 }
168
169 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index, int count)
170 {
171 if (index < 0) {
172 index = 0;
173 }
174 if (count < 0) {
175 count = 0;
176 }
177
178 if (index == m_firstVisibleIndex && count == m_lastVisibleIndex - m_firstVisibleIndex + 1) {
179 // The range has not been changed
180 return;
181 }
182
183 m_firstVisibleIndex = index;
184 m_lastVisibleIndex = qMin(index + count - 1, m_model->count() - 1);
185
186 startUpdating();
187 }
188
189 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count)
190 {
191 m_maximumVisibleItems = count;
192 }
193
194 void KFileItemModelRolesUpdater::setPreviewsShown(bool show)
195 {
196 if (show == m_previewShown) {
197 return;
198 }
199
200 m_previewShown = show;
201 if (!show) {
202 m_clearPreviews = true;
203 }
204
205 updateAllPreviews();
206 }
207
208 bool KFileItemModelRolesUpdater::previewsShown() const
209 {
210 return m_previewShown;
211 }
212
213 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge)
214 {
215 if (enlarge != m_enlargeSmallPreviews) {
216 m_enlargeSmallPreviews = enlarge;
217 if (m_previewShown) {
218 updateAllPreviews();
219 }
220 }
221 }
222
223 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
224 {
225 return m_enlargeSmallPreviews;
226 }
227
228 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList& list)
229 {
230 if (m_enabledPlugins != list) {
231 m_enabledPlugins = list;
232 if (m_previewShown) {
233 updateAllPreviews();
234 }
235 }
236 }
237
238 void KFileItemModelRolesUpdater::setPaused(bool paused)
239 {
240 if (paused == (m_state == Paused)) {
241 return;
242 }
243
244 if (paused) {
245 m_state = Paused;
246 killPreviewJob();
247 } else {
248 const bool updatePreviews = (m_iconSizeChangedDuringPausing && m_previewShown) ||
249 m_previewChangedDuringPausing;
250 const bool resolveAll = updatePreviews || m_rolesChangedDuringPausing;
251 if (resolveAll) {
252 m_finishedItems.clear();
253 }
254
255 m_iconSizeChangedDuringPausing = false;
256 m_previewChangedDuringPausing = false;
257 m_rolesChangedDuringPausing = false;
258
259 if (!m_pendingSortRoleItems.isEmpty()) {
260 m_state = ResolvingSortRole;
261 resolveNextSortRole();
262 } else {
263 m_state = Idle;
264 startUpdating();
265 }
266 }
267 }
268
269 void KFileItemModelRolesUpdater::setRoles(const QSet<QByteArray>& roles)
270 {
271 if (m_roles != roles) {
272 m_roles = roles;
273
274 #ifdef HAVE_NEPOMUK
275 // Check whether there is at least one role that must be resolved
276 // with the help of Nepomuk. If this is the case, a (quite expensive)
277 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
278 // the role gets watched for changes.
279 const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance();
280 bool hasNepomukRole = false;
281 QSetIterator<QByteArray> it(roles);
282 while (it.hasNext()) {
283 const QByteArray& role = it.next();
284 if (rolesProvider.roles().contains(role)) {
285 hasNepomukRole = true;
286 break;
287 }
288 }
289
290 if (hasNepomukRole && !m_nepomukResourceWatcher) {
291 Q_ASSERT(m_nepomukUriItems.isEmpty());
292
293 m_nepomukResourceWatcher = new Nepomuk2::ResourceWatcher(this);
294 connect(m_nepomukResourceWatcher, SIGNAL(propertyChanged(Nepomuk2::Resource,Nepomuk2::Types::Property,QVariantList,QVariantList)),
295 this, SLOT(applyChangedNepomukRoles(Nepomuk2::Resource)));
296 } else if (!hasNepomukRole && m_nepomukResourceWatcher) {
297 delete m_nepomukResourceWatcher;
298 m_nepomukResourceWatcher = 0;
299 m_nepomukUriItems.clear();
300 }
301 #endif
302
303 if (m_state == Paused) {
304 m_rolesChangedDuringPausing = true;
305 } else {
306 startUpdating();
307 }
308 }
309 }
310
311 QSet<QByteArray> KFileItemModelRolesUpdater::roles() const
312 {
313 return m_roles;
314 }
315
316 bool KFileItemModelRolesUpdater::isPaused() const
317 {
318 return m_state == Paused;
319 }
320
321 QStringList KFileItemModelRolesUpdater::enabledPlugins() const
322 {
323 return m_enabledPlugins;
324 }
325
326 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList& itemRanges)
327 {
328 QElapsedTimer timer;
329 timer.start();
330
331 // Determine the sort role synchronously for as many items as possible.
332 if (m_resolvableRoles.contains(m_model->sortRole())) {
333 int insertedCount = 0;
334 foreach (const KItemRange& range, itemRanges) {
335 const int lastIndex = insertedCount + range.index + range.count - 1;
336 for (int i = insertedCount + range.index; i <= lastIndex; ++i) {
337 if (timer.elapsed() < MaxBlockTimeout) {
338 applySortRole(i);
339 } else {
340 m_pendingSortRoleItems.insert(m_model->fileItem(i));
341 }
342 }
343 insertedCount += range.count;
344 }
345
346 applySortProgressToModel();
347
348 // If there are still items whose sort role is unknown, return
349 // and handle them asynchronously.
350 if (!m_pendingSortRoleItems.isEmpty()) {
351 if (m_state != ResolvingSortRole) {
352 // Trigger the asynchronous determination of the sort role.
353 killPreviewJob();
354 m_state = ResolvingSortRole;
355 resolveNextSortRole();
356 }
357 return;
358 }
359 }
360
361 startUpdating();
362 }
363
364 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRanges)
365 {
366 Q_UNUSED(itemRanges);
367
368 const bool allItemsRemoved = (m_model->count() == 0);
369
370 if (!m_watchedDirs.isEmpty()) {
371 // Don't let KDirWatch watch for removed items
372 if (allItemsRemoved) {
373 foreach (const QString& path, m_watchedDirs) {
374 m_dirWatcher->removeDir(path);
375 }
376 m_watchedDirs.clear();
377 } else {
378 QMutableSetIterator<QString> it(m_watchedDirs);
379 while (it.hasNext()) {
380 const QString& path = it.next();
381 if (m_model->index(KUrl(path)) < 0) {
382 m_dirWatcher->removeDir(path);
383 it.remove();
384 }
385 }
386 }
387 }
388
389 #ifdef HAVE_NEPOMUK
390 if (m_nepomukResourceWatcher) {
391 // Don't let the ResourceWatcher watch for removed items
392 if (allItemsRemoved) {
393 m_nepomukResourceWatcher->setResources(QList<Nepomuk2::Resource>());
394 m_nepomukResourceWatcher->stop();
395 m_nepomukUriItems.clear();
396 } else {
397 QList<Nepomuk2::Resource> newResources;
398 const QList<Nepomuk2::Resource> oldResources = m_nepomukResourceWatcher->resources();
399 foreach (const Nepomuk2::Resource& resource, oldResources) {
400 const QUrl uri = resource.uri();
401 const KUrl itemUrl = m_nepomukUriItems.value(uri);
402 if (m_model->index(itemUrl) >= 0) {
403 newResources.append(resource);
404 } else {
405 m_nepomukUriItems.remove(uri);
406 }
407 }
408 m_nepomukResourceWatcher->setResources(newResources);
409 if (newResources.isEmpty()) {
410 Q_ASSERT(m_nepomukUriItems.isEmpty());
411 m_nepomukResourceWatcher->stop();
412 }
413 }
414 }
415 #endif
416
417 if (allItemsRemoved) {
418 m_state = Idle;
419
420 m_finishedItems.clear();
421 m_pendingSortRoleItems.clear();
422 m_pendingSortRoleIndexes.clear();
423 m_pendingIndexes.clear();
424 m_pendingPreviewItems.clear();
425 m_recentlyChangedItems.clear();
426 m_recentlyChangedItemsTimer->stop();
427 m_changedItems.clear();
428
429 killPreviewJob();
430 } else {
431 // Only remove the items from m_finishedItems. They will be removed
432 // from the other sets later on.
433 QSet<KFileItem>::iterator it = m_finishedItems.begin();
434 while (it != m_finishedItems.end()) {
435 if (m_model->index(*it) < 0) {
436 it = m_finishedItems.erase(it);
437 } else {
438 ++it;
439 }
440 }
441
442 // The visible items might have changed.
443 startUpdating();
444 }
445 }
446
447 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, QList<int> movedToIndexes)
448 {
449 Q_UNUSED(itemRange);
450 Q_UNUSED(movedToIndexes);
451
452 // The indexes of the items with missing sort role are not valid any more.
453 m_pendingSortRoleIndexes.clear();
454
455 // The visible items might have changed.
456 startUpdating();
457 }
458
459 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList& itemRanges,
460 const QSet<QByteArray>& roles)
461 {
462 Q_UNUSED(roles);
463
464 // Find out if slotItemsChanged() has been done recently. If that is the
465 // case, resolving the roles is postponed until a timer has exceeded
466 // to prevent expensive repeated updates if files are updated frequently.
467 const bool itemsChangedRecently = m_recentlyChangedItemsTimer->isActive();
468
469 QSet<KFileItem>& targetSet = itemsChangedRecently ? m_recentlyChangedItems : m_changedItems;
470
471 foreach (const KItemRange& itemRange, itemRanges) {
472 int index = itemRange.index;
473 for (int count = itemRange.count; count > 0; --count) {
474 const KFileItem item = m_model->fileItem(index);
475 targetSet.insert(item);
476 ++index;
477 }
478 }
479
480 m_recentlyChangedItemsTimer->start();
481
482 if (!itemsChangedRecently) {
483 updateChangedItems();
484 }
485 }
486
487 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray& current,
488 const QByteArray& previous)
489 {
490 Q_UNUSED(current);
491 Q_UNUSED(previous);
492
493 if (m_resolvableRoles.contains(current)) {
494 m_pendingSortRoleItems.clear();
495 m_pendingSortRoleIndexes.clear();
496 m_finishedItems.clear();
497
498 const int count = m_model->count();
499 QElapsedTimer timer;
500 timer.start();
501
502 // Determine the sort role synchronously for as many items as possible.
503 for (int index = 0; index < count; ++index) {
504 if (timer.elapsed() < MaxBlockTimeout) {
505 applySortRole(index);
506 } else {
507 m_pendingSortRoleItems.insert(m_model->fileItem(index));
508 }
509 }
510
511 applySortProgressToModel();
512
513 if (!m_pendingSortRoleItems.isEmpty()) {
514 // Trigger the asynchronous determination of the sort role.
515 killPreviewJob();
516 m_state = ResolvingSortRole;
517 resolveNextSortRole();
518 }
519 } else {
520 m_state = Idle;
521 m_pendingSortRoleItems.clear();
522 m_pendingSortRoleIndexes.clear();
523 applySortProgressToModel();
524 }
525 }
526
527 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPixmap& pixmap)
528 {
529 if (m_state != PreviewJobRunning) {
530 return;
531 }
532
533 m_changedItems.remove(item);
534
535 const int index = m_model->index(item);
536 if (index < 0) {
537 return;
538 }
539
540 QPixmap scaledPixmap = pixmap;
541
542 const QString mimeType = item.mimetype();
543 const int slashIndex = mimeType.indexOf(QLatin1Char('/'));
544 const QString mimeTypeGroup = mimeType.left(slashIndex);
545 if (mimeTypeGroup == QLatin1String("image")) {
546 if (m_enlargeSmallPreviews) {
547 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
548 } else {
549 // Assure that small previews don't get enlarged. Instead they
550 // should be shown centered within the frame.
551 const QSize contentSize = KPixmapModifier::sizeInsideFrame(m_iconSize);
552 const bool enlargingRequired = scaledPixmap.width() < contentSize.width() &&
553 scaledPixmap.height() < contentSize.height();
554 if (enlargingRequired) {
555 QSize frameSize = scaledPixmap.size();
556 frameSize.scale(m_iconSize, Qt::KeepAspectRatio);
557
558 QPixmap largeFrame(frameSize);
559 largeFrame.fill(Qt::transparent);
560
561 KPixmapModifier::applyFrame(largeFrame, frameSize);
562
563 QPainter painter(&largeFrame);
564 painter.drawPixmap((largeFrame.width() - scaledPixmap.width()) / 2,
565 (largeFrame.height() - scaledPixmap.height()) / 2,
566 scaledPixmap);
567 scaledPixmap = largeFrame;
568 } else {
569 // The image must be shrinked as it is too large to fit into
570 // the available icon size
571 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
572 }
573 }
574 } else {
575 KPixmapModifier::scale(scaledPixmap, m_iconSize);
576 }
577
578 QHash<QByteArray, QVariant> data = rolesData(item);
579 data.insert("iconPixmap", scaledPixmap);
580
581 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
582 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
583 m_model->setData(index, data);
584 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
585 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
586
587 m_finishedItems.insert(item);
588 }
589
590 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item)
591 {
592 if (m_state != PreviewJobRunning) {
593 return;
594 }
595
596 m_changedItems.remove(item);
597
598 const int index = m_model->index(item);
599 if (index >= 0) {
600 QHash<QByteArray, QVariant> data;
601 data.insert("iconPixmap", QPixmap());
602
603 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
604 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
605 m_model->setData(index, data);
606 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
607 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
608
609 applyResolvedRoles(item, ResolveAll);
610 m_finishedItems.insert(item);
611 }
612 }
613
614 void KFileItemModelRolesUpdater::slotPreviewJobFinished(KJob* job)
615 {
616 Q_UNUSED(job);
617
618 m_previewJob = 0;
619
620 if (m_state != PreviewJobRunning) {
621 return;
622 }
623
624 m_state = Idle;
625
626 if (!m_pendingPreviewItems.isEmpty()) {
627 startPreviewJob(m_pendingPreviewItems);
628 } else {
629 if (!m_changedItems.isEmpty()) {
630 updateChangedItems();
631 }
632 }
633 }
634
635 void KFileItemModelRolesUpdater::resolveNextSortRole()
636 {
637 if (m_state != ResolvingSortRole) {
638 return;
639 }
640
641 if (m_pendingSortRoleItems.count() != m_pendingSortRoleIndexes.count()) {
642 // The indexes with missing sort role have to be updated.
643 m_pendingSortRoleIndexes.clear();
644 foreach (const KFileItem& item, m_pendingSortRoleItems) {
645 const int index = m_model->index(item);
646 if (index < 0) {
647 m_pendingSortRoleItems.remove(item);
648 } else {
649 m_pendingSortRoleIndexes.append(index);
650 }
651 }
652
653 std::sort(m_pendingSortRoleIndexes.begin(), m_pendingSortRoleIndexes.end());
654 }
655
656 // Try to update an item in the visible range.
657 QList<int>::iterator it = std::lower_bound(m_pendingSortRoleIndexes.begin(),
658 m_pendingSortRoleIndexes.end(),
659 m_firstVisibleIndex);
660
661 // It seems that there is no such item. Start with the first item in the list.
662 if (it == m_pendingSortRoleIndexes.end()) {
663 it = m_pendingSortRoleIndexes.begin();
664 }
665
666 while (it != m_pendingSortRoleIndexes.end()) {
667 // TODO: Note that removing an index from the list m_pendingSortRoleIndexes
668 // at a random position is O(N). We might need a better solution
669 // to make sure that this does not harm the performance if
670 // many items have to be sorted.
671 const int index = *it;
672 const KFileItem item = m_model->fileItem(index);
673
674 // Continue if the sort role has already been determined for the
675 // item, and the item has not been changed recently.
676 if (!m_changedItems.contains(item) && m_model->data(index).contains(m_model->sortRole())) {
677 m_pendingSortRoleItems.remove(item);
678 m_pendingSortRoleIndexes.erase(it);
679
680 // Check if we are at the end of the list (note that the list's end has changed).
681 if (it != m_pendingSortRoleIndexes.end()) {
682 ++it;
683 }
684 continue;
685 }
686
687 applySortRole(index);
688 m_pendingSortRoleItems.remove(item);
689 m_pendingSortRoleIndexes.erase(it);
690 break;
691 }
692
693 if (!m_pendingSortRoleItems.isEmpty()) {
694 applySortProgressToModel();
695 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
696 } else {
697 m_state = Idle;
698
699 // Prevent that we try to update the items twice.
700 disconnect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
701 this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
702 applySortProgressToModel();
703 connect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
704 this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
705 startUpdating();
706 }
707 }
708
709 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
710 {
711 if (m_state != ResolvingAllRoles && m_state != PreviewJobRunning) {
712 return;
713 }
714
715 while (!m_pendingIndexes.isEmpty()) {
716 const int index = m_pendingIndexes.takeFirst();
717 const KFileItem item = m_model->fileItem(index);
718
719 if (m_finishedItems.contains(item)) {
720 continue;
721 }
722
723 if (m_previewShown) {
724 // Only determine the icon. The other roles are resolved when the preview is received.
725 applyResolvedRoles(item, ResolveFast);
726 } else {
727 applyResolvedRoles(item, ResolveAll);
728 m_finishedItems.insert(item);
729 m_changedItems.remove(item);
730 }
731
732 break;
733 }
734
735 if (!m_pendingIndexes.isEmpty()) {
736 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
737 } else if (m_state != PreviewJobRunning) {
738 m_state = Idle;
739
740 if (m_clearPreviews) {
741 // Only go through the list if there are items which might still have previews.
742 if (m_finishedItems.count() != m_model->count()) {
743 QHash<QByteArray, QVariant> data;
744 data.insert("iconPixmap", QPixmap());
745
746 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
747 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
748 for (int index = 0; index <= m_model->count(); ++index) {
749 if (m_model->data(index).contains("iconPixmap")) {
750 m_model->setData(index, data);
751 }
752 }
753 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
754 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
755
756 }
757 m_clearPreviews = false;
758 }
759
760 if (!m_changedItems.isEmpty()) {
761 updateChangedItems();
762 }
763 }
764 }
765
766 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
767 {
768 m_changedItems += m_recentlyChangedItems;
769 m_recentlyChangedItems.clear();
770 updateChangedItems();
771 }
772
773 void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk2::Resource& resource)
774 {
775 #ifdef HAVE_NEPOMUK
776 const KUrl itemUrl = m_nepomukUriItems.value(resource.uri());
777 const KFileItem item = m_model->fileItem(itemUrl);
778
779 if (item.isNull()) {
780 // itemUrl is not in the model anymore, probably because
781 // the corresponding file has been deleted in the meantime.
782 return;
783 }
784
785 QHash<QByteArray, QVariant> data = rolesData(item);
786
787 const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance();
788 QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(resource, m_roles));
789 while (it.hasNext()) {
790 it.next();
791 data.insert(it.key(), it.value());
792 }
793
794 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
795 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
796 const int index = m_model->index(item);
797 m_model->setData(index, data);
798 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
799 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
800 #else
801 #ifndef Q_CC_MSVC
802 Q_UNUSED(resource);
803 #endif
804 #endif
805 }
806
807 void KFileItemModelRolesUpdater::slotDirWatchDirty(const QString& path)
808 {
809 const bool getSizeRole = m_roles.contains("size");
810 const bool getIsExpandableRole = m_roles.contains("isExpandable");
811
812 if (getSizeRole || getIsExpandableRole) {
813 const int index = m_model->index(KUrl(path));
814 if (index >= 0) {
815 if (!m_model->fileItem(index).isDir()) {
816 // If INotify is used, KDirWatch issues the dirty() signal
817 // also for changed files inside the directory, even if we
818 // don't enable this behavior explicitly (see bug 309740).
819 return;
820 }
821
822 QHash<QByteArray, QVariant> data;
823
824 const int count = subItemsCount(path);
825 if (getSizeRole) {
826 data.insert("size", count);
827 }
828 if (getIsExpandableRole) {
829 data.insert("isExpandable", count > 0);
830 }
831
832 // Note that we do not block the itemsChanged signal here.
833 // This ensures that a new preview will be generated.
834 m_model->setData(index, data);
835 }
836 }
837 }
838
839 void KFileItemModelRolesUpdater::startUpdating()
840 {
841 // Updating the items in and near the visible area makes sense only
842 // if sorting is finished.
843 if (m_state == ResolvingSortRole || m_state == Paused) {
844 return;
845 }
846
847 if (m_finishedItems.count() == m_model->count()) {
848 // All roles have been resolved already.
849 m_state = Idle;
850 return;
851 }
852
853 int lastVisibleIndex = m_lastVisibleIndex;
854 if (lastVisibleIndex <= 0) {
855 // Guess a reasonable value for the last visible index if the view
856 // has not told us about the real value yet.
857 lastVisibleIndex = qMin(m_firstVisibleIndex + m_maximumVisibleItems, m_model->count() - 1);
858 if (lastVisibleIndex <= 0) {
859 lastVisibleIndex = qMin(200, m_model->count() - 1);
860 }
861 }
862
863 // Terminate all updates that are currently active.
864 killPreviewJob();
865 m_pendingIndexes.clear();
866
867 QElapsedTimer timer;
868 timer.start();
869
870 // Determine the icons for the visible items synchronously.
871 int index;
872 for (index = m_firstVisibleIndex; index <= lastVisibleIndex && timer.elapsed() < MaxBlockTimeout; ++index) {
873 const KFileItem item = m_model->fileItem(index);
874 applyResolvedRoles(item, ResolveFast);
875 }
876 const int firstIndexWithoutIcon = index;
877
878 // Start the preview job or the asynchronous resolving of all roles.
879 QList<int> indexes = indexesToResolve();
880
881 if (m_previewShown) {
882 KFileItemList itemsToResolve;
883 foreach (int index, indexes) {
884 const KFileItem item = m_model->fileItem(index);
885 if (!m_finishedItems.contains(item)) {
886 itemsToResolve.append(m_model->fileItem(index));
887
888 // Remember the items which have no icon yet. A fast
889 // asynchronous resolving will be done to make sure
890 // that icons are loaded as quickly as possible, i.e.,
891 // before the previews arrive.
892 if (index < m_firstVisibleIndex || index >= firstIndexWithoutIcon) {
893 m_pendingIndexes.append(index);
894 }
895 }
896 }
897
898 startPreviewJob(itemsToResolve);
899
900 // Determine the icons asynchronously as fast as possible.
901 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
902 } else {
903 m_pendingIndexes = indexes;
904 // Trigger the asynchronous resolving of all roles.
905 m_state = ResolvingAllRoles;
906 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
907 }
908 }
909
910 void KFileItemModelRolesUpdater::startPreviewJob(const KFileItemList items)
911 {
912 m_state = PreviewJobRunning;
913
914 if (items.isEmpty()) {
915 QMetaObject::invokeMethod(this, "slotPreviewJobFinished", Qt::QueuedConnection, Q_ARG(KJob*, 0));
916 return;
917 }
918
919 // PreviewJob internally caches items always with the size of
920 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
921 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
922 // do a downscaling anyhow because of the frame, so in this case only the provided
923 // cache sizes are requested.
924 const QSize cacheSize = (m_iconSize.width() > 128) || (m_iconSize.height() > 128)
925 ? QSize(256, 256) : QSize(128, 128);
926
927 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
928 // worst case) might block the application for several seconds. To prevent such
929 // a blocking, we only pass items with known mime type to the preview job
930 // (if the icon has already been determined for an item in startUpdating()
931 // or resolveNextPendingRoles(), the type is known).
932 // This also prevents that repeated expensive mime type determinations are
933 // triggered here if a huge folder is loaded, and startUpdating() is called
934 // repeatedly.
935 //
936 // Note that we always pass at least one item to the preview job to prevent
937 // that we get an endless startPreviewJob()/slotPreviewJobFinished() loop
938 // if there are no items with known mime types yet for some reason.
939 const int count = items.count();
940 int previewJobItemCount = 1;
941
942 // TODO: This will start a job with one item only if this function is
943 // called from slotPreviewJobFinished(), and resolveNextPendingRoles()
944 // has not reached the items yet. This can happen if the previous preview
945 // job has finished very fast because generating previews failed for all
946 // items.
947 //
948 // Idea to improve this: if the mime type of the first item is unknown,
949 // determine mime types synchronously for a while.
950 while (previewJobItemCount < qMin(count, m_maximumVisibleItems) &&
951 items.at(previewJobItemCount).isMimeTypeKnown()) {
952 ++previewJobItemCount;
953 }
954
955 KFileItemList itemSubSet;
956 itemSubSet.reserve(previewJobItemCount);
957 m_pendingPreviewItems.clear();
958 m_pendingPreviewItems.reserve(count - previewJobItemCount);
959
960 for (int i = 0; i < previewJobItemCount; ++i) {
961 itemSubSet.append(items.at(i));
962 }
963
964 for (int i = previewJobItemCount; i < count; ++i) {
965 m_pendingPreviewItems.append(items.at(i));
966 }
967
968 KIO::PreviewJob* job = new KIO::PreviewJob(itemSubSet, cacheSize, &m_enabledPlugins);
969
970 job->setIgnoreMaximumSize(itemSubSet.first().isLocalFile());
971 if (job->ui()) {
972 job->ui()->setWindow(qApp->activeWindow());
973 }
974
975 connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)),
976 this, SLOT(slotGotPreview(KFileItem,QPixmap)));
977 connect(job, SIGNAL(failed(KFileItem)),
978 this, SLOT(slotPreviewFailed(KFileItem)));
979 connect(job, SIGNAL(finished(KJob*)),
980 this, SLOT(slotPreviewJobFinished(KJob*)));
981
982 m_previewJob = job;
983 }
984
985 void KFileItemModelRolesUpdater::updateChangedItems()
986 {
987 if (m_state == Paused) {
988 return;
989 }
990
991 if (m_changedItems.isEmpty()) {
992 return;
993 }
994
995 m_finishedItems -= m_changedItems;
996
997 if (m_resolvableRoles.contains(m_model->sortRole())) {
998 m_pendingSortRoleItems += m_changedItems;
999
1000 if (m_state != ResolvingSortRole) {
1001 // Stop the preview job if necessary, and trigger the
1002 // asynchronous determination of the sort role.
1003 killPreviewJob();
1004 m_state = ResolvingSortRole;
1005 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
1006 }
1007
1008 return;
1009 }
1010
1011 QList<int> visibleChangedIndexes;
1012 QList<int> invisibleChangedIndexes;
1013
1014 foreach (const KFileItem& item, m_changedItems) {
1015 const int index = m_model->index(item);
1016
1017 if (index < 0) {
1018 continue;
1019 }
1020
1021 if (index >= m_firstVisibleIndex && index <= m_lastVisibleIndex) {
1022 visibleChangedIndexes.append(index);
1023 } else {
1024 invisibleChangedIndexes.append(index);
1025 }
1026 }
1027
1028 std::sort(visibleChangedIndexes.begin(), visibleChangedIndexes.end());
1029
1030 if (m_previewShown) {
1031 KFileItemList visibleChangedItems;
1032 KFileItemList invisibleChangedItems;
1033
1034 foreach (int index, visibleChangedIndexes) {
1035 visibleChangedItems.append(m_model->fileItem(index));
1036 }
1037
1038 foreach (int index, invisibleChangedIndexes) {
1039 invisibleChangedItems.append(m_model->fileItem(index));
1040 }
1041
1042 if (m_previewJob) {
1043 m_pendingPreviewItems += visibleChangedItems + invisibleChangedItems;
1044 } else {
1045 startPreviewJob(visibleChangedItems + invisibleChangedItems);
1046 }
1047 } else {
1048 const bool resolvingInProgress = !m_pendingIndexes.isEmpty();
1049 m_pendingIndexes = visibleChangedIndexes + m_pendingIndexes + invisibleChangedIndexes;
1050 if (!resolvingInProgress) {
1051 // Trigger the asynchronous resolving of the changed roles.
1052 m_state = ResolvingAllRoles;
1053 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
1054 }
1055 }
1056 }
1057
1058 void KFileItemModelRolesUpdater::applySortRole(int index)
1059 {
1060 QHash<QByteArray, QVariant> data;
1061 const KFileItem item = m_model->fileItem(index);
1062
1063 if (index >= m_firstVisibleIndex && index <= m_lastVisibleIndex) {
1064 // Determine the icon.
1065 applyResolvedRoles(item, ResolveFast);
1066 }
1067
1068 if (m_model->sortRole() == "type") {
1069 if (!item.isMimeTypeKnown()) {
1070 item.determineMimeType();
1071 }
1072
1073 data.insert("type", item.mimeComment());
1074 } else if (m_model->sortRole() == "size" && item.isLocalFile() && item.isDir()) {
1075 const QString path = item.localPath();
1076 data.insert("size", subItemsCount(path));
1077 } else {
1078 // Probably the sort role is a Nepomuk role - just determine all roles.
1079 data = rolesData(item);
1080 }
1081
1082 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1083 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1084 m_model->setData(index, data);
1085 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1086 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1087 }
1088
1089 void KFileItemModelRolesUpdater::applySortProgressToModel()
1090 {
1091 // Inform the model about the progress of the resolved items,
1092 // so that it can give an indication when the sorting has been finished.
1093 const int resolvedCount = m_model->count() - m_pendingSortRoleItems.count();
1094 m_model->emitSortProgress(resolvedCount);
1095 }
1096
1097 bool KFileItemModelRolesUpdater::applyResolvedRoles(const KFileItem& item, ResolveHint hint)
1098 {
1099 if (item.isNull()) {
1100 return false;
1101 }
1102
1103 const bool resolveAll = (hint == ResolveAll);
1104
1105 bool iconChanged = false;
1106 if (!item.isMimeTypeKnown() || !item.isFinalIconKnown()) {
1107 item.determineMimeType();
1108 iconChanged = true;
1109 } else if (m_state == ResolvingSortRole || m_state == PreviewJobRunning) {
1110 // We are currently performing a fast determination of all icons
1111 // in the visible area.
1112 const int index = m_model->index(item);
1113 if (!m_model->data(index).contains("iconName")) {
1114 iconChanged = true;
1115 }
1116 }
1117
1118 if (iconChanged || resolveAll || m_clearPreviews) {
1119 const int index = m_model->index(item);
1120 if (index < 0) {
1121 return false;
1122 }
1123
1124 QHash<QByteArray, QVariant> data;
1125 if (resolveAll) {
1126 data = rolesData(item);
1127 }
1128
1129 data.insert("iconName", item.iconName());
1130
1131 if (m_clearPreviews) {
1132 data.insert("iconPixmap", QPixmap());
1133 }
1134
1135 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1136 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1137 m_model->setData(index, data);
1138 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1139 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1140 return true;
1141 }
1142
1143 return false;
1144 }
1145
1146 QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileItem& item) const
1147 {
1148 QHash<QByteArray, QVariant> data;
1149
1150 const bool getSizeRole = m_roles.contains("size");
1151 const bool getIsExpandableRole = m_roles.contains("isExpandable");
1152
1153 if ((getSizeRole || getIsExpandableRole) && item.isDir()) {
1154 if (item.isLocalFile()) {
1155 const QString path = item.localPath();
1156 const int count = subItemsCount(path);
1157 if (getSizeRole) {
1158 data.insert("size", count);
1159 }
1160 if (getIsExpandableRole) {
1161 data.insert("isExpandable", count > 0);
1162 }
1163
1164 if (!m_dirWatcher->contains(path)) {
1165 m_dirWatcher->addDir(path);
1166 m_watchedDirs.insert(path);
1167 }
1168 } else if (getSizeRole) {
1169 data.insert("size", -1); // -1 indicates an unknown number of items
1170 }
1171 }
1172
1173 if (m_roles.contains("type")) {
1174 data.insert("type", item.mimeComment());
1175 }
1176
1177 data.insert("iconOverlays", item.overlays());
1178
1179 #ifdef HAVE_NEPOMUK
1180 if (m_nepomukResourceWatcher) {
1181 const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance();
1182 Nepomuk2::Resource resource(item.nepomukUri());
1183 QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(resource, m_roles));
1184 while (it.hasNext()) {
1185 it.next();
1186 data.insert(it.key(), it.value());
1187 }
1188
1189 QUrl uri = resource.uri();
1190 if (uri.isEmpty()) {
1191 // TODO: Is there another way to explicitly create a resource?
1192 // We need a resource to be able to track it for changes.
1193 resource.setRating(0);
1194 uri = resource.uri();
1195 }
1196 if (!uri.isEmpty() && !m_nepomukUriItems.contains(uri)) {
1197 m_nepomukResourceWatcher->addResource(resource);
1198
1199 if (m_nepomukUriItems.isEmpty()) {
1200 m_nepomukResourceWatcher->start();
1201 }
1202
1203 m_nepomukUriItems.insert(uri, item.url());
1204 }
1205 }
1206 #endif
1207
1208 return data;
1209 }
1210
1211 int KFileItemModelRolesUpdater::subItemsCount(const QString& path) const
1212 {
1213 const bool countHiddenFiles = m_model->showHiddenFiles();
1214 const bool showFoldersOnly = m_model->showDirectoriesOnly();
1215
1216 #ifdef Q_WS_WIN
1217 QDir dir(path);
1218 QDir::Filters filters = QDir::NoDotAndDotDot | QDir::System;
1219 if (countHiddenFiles) {
1220 filters |= QDir::Hidden;
1221 }
1222 if (showFoldersOnly) {
1223 filters |= QDir::Dirs;
1224 } else {
1225 filters |= QDir::AllEntries;
1226 }
1227 return dir.entryList(filters).count();
1228 #else
1229 // Taken from kdelibs/kio/kio/kdirmodel.cpp
1230 // Copyright (C) 2006 David Faure <faure@kde.org>
1231
1232 int count = -1;
1233 DIR* dir = ::opendir(QFile::encodeName(path));
1234 if (dir) { // krazy:exclude=syscalls
1235 count = 0;
1236 struct dirent *dirEntry = 0;
1237 while ((dirEntry = ::readdir(dir))) {
1238 if (dirEntry->d_name[0] == '.') {
1239 if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
1240 // Skip "." or hidden files
1241 continue;
1242 }
1243 if (dirEntry->d_name[1] == '.' && dirEntry->d_name[2] == '\0') {
1244 // Skip ".."
1245 continue;
1246 }
1247 }
1248
1249 // If only directories are counted, consider an unknown file type and links also
1250 // as directory instead of trying to do an expensive stat()
1251 // (see bugs 292642 and 299997).
1252 const bool countEntry = !showFoldersOnly ||
1253 dirEntry->d_type == DT_DIR ||
1254 dirEntry->d_type == DT_LNK ||
1255 dirEntry->d_type == DT_UNKNOWN;
1256 if (countEntry) {
1257 ++count;
1258 }
1259 }
1260 ::closedir(dir);
1261 }
1262 return count;
1263 #endif
1264 }
1265
1266 void KFileItemModelRolesUpdater::updateAllPreviews()
1267 {
1268 if (m_state == Paused) {
1269 m_previewChangedDuringPausing = true;
1270 } else {
1271 m_finishedItems.clear();
1272 startUpdating();
1273 }
1274 }
1275
1276 void KFileItemModelRolesUpdater::killPreviewJob()
1277 {
1278 if (m_previewJob) {
1279 disconnect(m_previewJob, SIGNAL(gotPreview(KFileItem,QPixmap)),
1280 this, SLOT(slotGotPreview(KFileItem,QPixmap)));
1281 disconnect(m_previewJob, SIGNAL(failed(KFileItem)),
1282 this, SLOT(slotPreviewFailed(KFileItem)));
1283 disconnect(m_previewJob, SIGNAL(finished(KJob*)),
1284 this, SLOT(slotPreviewJobFinished(KJob*)));
1285 m_previewJob->kill();
1286 m_previewJob = 0;
1287 m_pendingPreviewItems.clear();
1288 }
1289 }
1290
1291 QList<int> KFileItemModelRolesUpdater::indexesToResolve() const
1292 {
1293 const int count = m_model->count();
1294
1295 QList<int> result;
1296 result.reserve(ResolveAllItemsLimit);
1297
1298 // Add visible items.
1299 for (int i = m_firstVisibleIndex; i <= m_lastVisibleIndex; ++i) {
1300 result.append(i);
1301 }
1302
1303 // We need a reasonable upper limit for number of items to resolve after
1304 // and before the visible range. m_maximumVisibleItems can be quite large
1305 // when using Compace View.
1306 const int readAheadItems = qMin(ReadAheadPages * m_maximumVisibleItems, ResolveAllItemsLimit / 2);
1307
1308 // Add items after the visible range.
1309 const int endExtendedVisibleRange = qMin(m_lastVisibleIndex + readAheadItems, count - 1);
1310 for (int i = m_lastVisibleIndex + 1; i <= endExtendedVisibleRange; ++i) {
1311 result.append(i);
1312 }
1313
1314 // Add items before the visible range in reverse order.
1315 const int beginExtendedVisibleRange = qMax(0, m_firstVisibleIndex - readAheadItems);
1316 for (int i = m_firstVisibleIndex - 1; i >= beginExtendedVisibleRange; --i) {
1317 result.append(i);
1318 }
1319
1320 // Add items on the last page.
1321 const int beginLastPage = qMax(qMin(endExtendedVisibleRange + 1, count - 1), count - m_maximumVisibleItems);
1322 for (int i = beginLastPage; i < count; ++i) {
1323 result.append(i);
1324 }
1325
1326 // Add items on the first page.
1327 const int endFirstPage = qMin(qMax(beginExtendedVisibleRange - 1, 0), m_maximumVisibleItems);
1328 for (int i = 0; i <= endFirstPage; ++i) {
1329 result.append(i);
1330 }
1331
1332 // Continue adding items until ResolveAllItemsLimit is reached.
1333 int remainingItems = ResolveAllItemsLimit - result.count();
1334
1335 for (int i = endExtendedVisibleRange + 1; i < beginLastPage && remainingItems > 0; ++i) {
1336 result.append(i);
1337 --remainingItems;
1338 }
1339
1340 for (int i = beginExtendedVisibleRange - 1; i > endFirstPage && remainingItems > 0; --i) {
1341 result.append(i);
1342 --remainingItems;
1343 }
1344
1345 return result;
1346 }
1347
1348 #include "kfileitemmodelrolesupdater.moc"