]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.cpp
Fix wrong path->URL conversion.
[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 <KSharedConfig>
27 #include <KFileItem>
28 #include <KIconLoader>
29 #include <KJobWidgets>
30 #include <KIO/JobUiDelegate>
31 #include <KIO/PreviewJob>
32 #include <KPluginLoader>
33 #include <KOverlayIconPlugin>
34
35 #include "private/kpixmapmodifier.h"
36 #include "private/kdirectorycontentscounter.h"
37
38 #include <QApplication>
39 #include <QPainter>
40 #include <QPixmap>
41 #include <QElapsedTimer>
42 #include <QTimer>
43
44 #include <algorithm>
45
46 #ifdef HAVE_BALOO
47 #include "private/kbaloorolesprovider.h"
48 #include <Baloo/File>
49 #include <Baloo/FileMonitor>
50 #endif
51
52
53 // #define KFILEITEMMODELROLESUPDATER_DEBUG
54
55 namespace {
56 // Maximum time in ms that the KFileItemModelRolesUpdater
57 // may perform a blocking operation
58 const int MaxBlockTimeout = 200;
59
60 // If the number of items is smaller than ResolveAllItemsLimit,
61 // the roles of all items will be resolved.
62 const int ResolveAllItemsLimit = 500;
63
64 // Not only the visible area, but up to ReadAheadPages before and after
65 // this area will be resolved.
66 const int ReadAheadPages = 5;
67 }
68
69 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent) :
70 QObject(parent),
71 m_state(Idle),
72 m_previewChangedDuringPausing(false),
73 m_iconSizeChangedDuringPausing(false),
74 m_rolesChangedDuringPausing(false),
75 m_previewShown(false),
76 m_enlargeSmallPreviews(true),
77 m_clearPreviews(false),
78 m_finishedItems(),
79 m_model(model),
80 m_iconSize(),
81 m_firstVisibleIndex(0),
82 m_lastVisibleIndex(-1),
83 m_maximumVisibleItems(50),
84 m_roles(),
85 m_resolvableRoles(),
86 m_enabledPlugins(),
87 m_pendingSortRoleItems(),
88 m_pendingIndexes(),
89 m_pendingPreviewItems(),
90 m_previewJob(),
91 m_recentlyChangedItemsTimer(0),
92 m_recentlyChangedItems(),
93 m_changedItems(),
94 m_directoryContentsCounter(0)
95 #ifdef HAVE_BALOO
96 , m_balooFileMonitor(0)
97 #endif
98 {
99 Q_ASSERT(model);
100
101 const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
102 m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList()
103 << "directorythumbnail"
104 << "imagethumbnail"
105 << "jpegthumbnail");
106
107 connect(m_model, &KFileItemModel::itemsInserted,
108 this, &KFileItemModelRolesUpdater::slotItemsInserted);
109 connect(m_model, &KFileItemModel::itemsRemoved,
110 this, &KFileItemModelRolesUpdater::slotItemsRemoved);
111 connect(m_model, &KFileItemModel::itemsChanged,
112 this, &KFileItemModelRolesUpdater::slotItemsChanged);
113 connect(m_model, &KFileItemModel::itemsMoved,
114 this, &KFileItemModelRolesUpdater::slotItemsMoved);
115 connect(m_model, &KFileItemModel::sortRoleChanged,
116 this, &KFileItemModelRolesUpdater::slotSortRoleChanged);
117
118 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
119 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
120 m_recentlyChangedItemsTimer = new QTimer(this);
121 m_recentlyChangedItemsTimer->setInterval(1000);
122 m_recentlyChangedItemsTimer->setSingleShot(true);
123 connect(m_recentlyChangedItemsTimer, &QTimer::timeout, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems);
124
125 m_resolvableRoles.insert("size");
126 m_resolvableRoles.insert("type");
127 m_resolvableRoles.insert("isExpandable");
128 #ifdef HAVE_BALOO
129 m_resolvableRoles += KBalooRolesProvider::instance().roles();
130 #endif
131
132 m_directoryContentsCounter = new KDirectoryContentsCounter(m_model, this);
133 connect(m_directoryContentsCounter, &KDirectoryContentsCounter::result,
134 this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived);
135
136 auto plugins = KPluginLoader::instantiatePlugins(QStringLiteral("kf5/overlayicon"), nullptr, this);
137 foreach (QObject *it, plugins) {
138 auto plugin = qobject_cast<KOverlayIconPlugin*>(it);
139 if (plugin) {
140 m_overlayIconsPlugin.append(plugin);
141 connect(plugin, &KOverlayIconPlugin::overlaysChanged, this, &KFileItemModelRolesUpdater::slotOverlaysChanged);
142 } else {
143 // not our/valid plugin, so delete the created object
144 it->deleteLater();
145 }
146 }
147 }
148
149 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
150 {
151 killPreviewJob();
152 }
153
154 void KFileItemModelRolesUpdater::setIconSize(const QSize& size)
155 {
156 if (size != m_iconSize) {
157 m_iconSize = size;
158 if (m_state == Paused) {
159 m_iconSizeChangedDuringPausing = true;
160 } else if (m_previewShown) {
161 // An icon size change requires the regenerating of
162 // all previews
163 m_finishedItems.clear();
164 startUpdating();
165 }
166 }
167 }
168
169 QSize KFileItemModelRolesUpdater::iconSize() const
170 {
171 return m_iconSize;
172 }
173
174 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index, int count)
175 {
176 if (index < 0) {
177 index = 0;
178 }
179 if (count < 0) {
180 count = 0;
181 }
182
183 if (index == m_firstVisibleIndex && count == m_lastVisibleIndex - m_firstVisibleIndex + 1) {
184 // The range has not been changed
185 return;
186 }
187
188 m_firstVisibleIndex = index;
189 m_lastVisibleIndex = qMin(index + count - 1, m_model->count() - 1);
190
191 startUpdating();
192 }
193
194 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count)
195 {
196 m_maximumVisibleItems = count;
197 }
198
199 void KFileItemModelRolesUpdater::setPreviewsShown(bool show)
200 {
201 if (show == m_previewShown) {
202 return;
203 }
204
205 m_previewShown = show;
206 if (!show) {
207 m_clearPreviews = true;
208 }
209
210 updateAllPreviews();
211 }
212
213 bool KFileItemModelRolesUpdater::previewsShown() const
214 {
215 return m_previewShown;
216 }
217
218 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge)
219 {
220 if (enlarge != m_enlargeSmallPreviews) {
221 m_enlargeSmallPreviews = enlarge;
222 if (m_previewShown) {
223 updateAllPreviews();
224 }
225 }
226 }
227
228 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
229 {
230 return m_enlargeSmallPreviews;
231 }
232
233 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList& list)
234 {
235 if (m_enabledPlugins != list) {
236 m_enabledPlugins = list;
237 if (m_previewShown) {
238 updateAllPreviews();
239 }
240 }
241 }
242
243 void KFileItemModelRolesUpdater::setPaused(bool paused)
244 {
245 if (paused == (m_state == Paused)) {
246 return;
247 }
248
249 if (paused) {
250 m_state = Paused;
251 killPreviewJob();
252 } else {
253 const bool updatePreviews = (m_iconSizeChangedDuringPausing && m_previewShown) ||
254 m_previewChangedDuringPausing;
255 const bool resolveAll = updatePreviews || m_rolesChangedDuringPausing;
256 if (resolveAll) {
257 m_finishedItems.clear();
258 }
259
260 m_iconSizeChangedDuringPausing = false;
261 m_previewChangedDuringPausing = false;
262 m_rolesChangedDuringPausing = false;
263
264 if (!m_pendingSortRoleItems.isEmpty()) {
265 m_state = ResolvingSortRole;
266 resolveNextSortRole();
267 } else {
268 m_state = Idle;
269 }
270
271 startUpdating();
272 }
273 }
274
275 void KFileItemModelRolesUpdater::setRoles(const QSet<QByteArray>& roles)
276 {
277 if (m_roles != roles) {
278 m_roles = roles;
279
280 #ifdef HAVE_BALOO
281 // Check whether there is at least one role that must be resolved
282 // with the help of Baloo. If this is the case, a (quite expensive)
283 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
284 // the role gets watched for changes.
285 const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance();
286 bool hasBalooRole = false;
287 QSetIterator<QByteArray> it(roles);
288 while (it.hasNext()) {
289 const QByteArray& role = it.next();
290 if (rolesProvider.roles().contains(role)) {
291 hasBalooRole = true;
292 break;
293 }
294 }
295
296 if (hasBalooRole && m_balooConfig.fileIndexingEnabled() && !m_balooFileMonitor) {
297 m_balooFileMonitor = new Baloo::FileMonitor(this);
298 connect(m_balooFileMonitor, &Baloo::FileMonitor::fileMetaDataChanged,
299 this, &KFileItemModelRolesUpdater::applyChangedBalooRoles);
300 } else if (!hasBalooRole && m_balooFileMonitor) {
301 delete m_balooFileMonitor;
302 m_balooFileMonitor = 0;
303 }
304 #endif
305
306 if (m_state == Paused) {
307 m_rolesChangedDuringPausing = true;
308 } else {
309 startUpdating();
310 }
311 }
312 }
313
314 QSet<QByteArray> KFileItemModelRolesUpdater::roles() const
315 {
316 return m_roles;
317 }
318
319 bool KFileItemModelRolesUpdater::isPaused() const
320 {
321 return m_state == Paused;
322 }
323
324 QStringList KFileItemModelRolesUpdater::enabledPlugins() const
325 {
326 return m_enabledPlugins;
327 }
328
329 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList& itemRanges)
330 {
331 QElapsedTimer timer;
332 timer.start();
333
334 // Determine the sort role synchronously for as many items as possible.
335 if (m_resolvableRoles.contains(m_model->sortRole())) {
336 int insertedCount = 0;
337 foreach (const KItemRange& range, itemRanges) {
338 const int lastIndex = insertedCount + range.index + range.count - 1;
339 for (int i = insertedCount + range.index; i <= lastIndex; ++i) {
340 if (timer.elapsed() < MaxBlockTimeout) {
341 applySortRole(i);
342 } else {
343 m_pendingSortRoleItems.insert(m_model->fileItem(i));
344 }
345 }
346 insertedCount += range.count;
347 }
348
349 applySortProgressToModel();
350
351 // If there are still items whose sort role is unknown, check if the
352 // asynchronous determination of the sort role is already in progress,
353 // and start it if that is not the case.
354 if (!m_pendingSortRoleItems.isEmpty() && m_state != ResolvingSortRole) {
355 killPreviewJob();
356 m_state = ResolvingSortRole;
357 resolveNextSortRole();
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 #ifdef HAVE_BALOO
371 if (m_balooFileMonitor) {
372 // Don't let the FileWatcher watch for removed items
373 if (allItemsRemoved) {
374 m_balooFileMonitor->clear();
375 } else {
376 QStringList newFileList;
377 foreach (const QString& itemUrl, m_balooFileMonitor->files()) {
378 if (m_model->index(itemUrl) >= 0) {
379 newFileList.append(itemUrl);
380 }
381 }
382 m_balooFileMonitor->setFiles(newFileList);
383 }
384 }
385 #endif
386
387 if (allItemsRemoved) {
388 m_state = Idle;
389
390 m_finishedItems.clear();
391 m_pendingSortRoleItems.clear();
392 m_pendingIndexes.clear();
393 m_pendingPreviewItems.clear();
394 m_recentlyChangedItems.clear();
395 m_recentlyChangedItemsTimer->stop();
396 m_changedItems.clear();
397
398 killPreviewJob();
399 } else {
400 // Only remove the items from m_finishedItems. They will be removed
401 // from the other sets later on.
402 QSet<KFileItem>::iterator it = m_finishedItems.begin();
403 while (it != m_finishedItems.end()) {
404 if (m_model->index(*it) < 0) {
405 it = m_finishedItems.erase(it);
406 } else {
407 ++it;
408 }
409 }
410
411 // The visible items might have changed.
412 startUpdating();
413 }
414 }
415
416 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, QList<int> movedToIndexes)
417 {
418 Q_UNUSED(itemRange);
419 Q_UNUSED(movedToIndexes);
420
421 // The visible items might have changed.
422 startUpdating();
423 }
424
425 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList& itemRanges,
426 const QSet<QByteArray>& roles)
427 {
428 Q_UNUSED(roles);
429
430 // Find out if slotItemsChanged() has been done recently. If that is the
431 // case, resolving the roles is postponed until a timer has exceeded
432 // to prevent expensive repeated updates if files are updated frequently.
433 const bool itemsChangedRecently = m_recentlyChangedItemsTimer->isActive();
434
435 QSet<KFileItem>& targetSet = itemsChangedRecently ? m_recentlyChangedItems : m_changedItems;
436
437 foreach (const KItemRange& itemRange, itemRanges) {
438 int index = itemRange.index;
439 for (int count = itemRange.count; count > 0; --count) {
440 const KFileItem item = m_model->fileItem(index);
441 targetSet.insert(item);
442 ++index;
443 }
444 }
445
446 m_recentlyChangedItemsTimer->start();
447
448 if (!itemsChangedRecently) {
449 updateChangedItems();
450 }
451 }
452
453 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray& current,
454 const QByteArray& previous)
455 {
456 Q_UNUSED(current);
457 Q_UNUSED(previous);
458
459 if (m_resolvableRoles.contains(current)) {
460 m_pendingSortRoleItems.clear();
461 m_finishedItems.clear();
462
463 const int count = m_model->count();
464 QElapsedTimer timer;
465 timer.start();
466
467 // Determine the sort role synchronously for as many items as possible.
468 for (int index = 0; index < count; ++index) {
469 if (timer.elapsed() < MaxBlockTimeout) {
470 applySortRole(index);
471 } else {
472 m_pendingSortRoleItems.insert(m_model->fileItem(index));
473 }
474 }
475
476 applySortProgressToModel();
477
478 if (!m_pendingSortRoleItems.isEmpty()) {
479 // Trigger the asynchronous determination of the sort role.
480 killPreviewJob();
481 m_state = ResolvingSortRole;
482 resolveNextSortRole();
483 }
484 } else {
485 m_state = Idle;
486 m_pendingSortRoleItems.clear();
487 applySortProgressToModel();
488 }
489 }
490
491 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPixmap& pixmap)
492 {
493 if (m_state != PreviewJobRunning) {
494 return;
495 }
496
497 m_changedItems.remove(item);
498
499 const int index = m_model->index(item);
500 if (index < 0) {
501 return;
502 }
503
504 QPixmap scaledPixmap = pixmap;
505
506 const QString mimeType = item.mimetype();
507 const int slashIndex = mimeType.indexOf(QLatin1Char('/'));
508 const QString mimeTypeGroup = mimeType.left(slashIndex);
509 if (mimeTypeGroup == QLatin1String("image")) {
510 if (m_enlargeSmallPreviews) {
511 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
512 } else {
513 // Assure that small previews don't get enlarged. Instead they
514 // should be shown centered within the frame.
515 const QSize contentSize = KPixmapModifier::sizeInsideFrame(m_iconSize);
516 const bool enlargingRequired = scaledPixmap.width() < contentSize.width() &&
517 scaledPixmap.height() < contentSize.height();
518 if (enlargingRequired) {
519 QSize frameSize = scaledPixmap.size() / scaledPixmap.devicePixelRatio();
520 frameSize.scale(m_iconSize, Qt::KeepAspectRatio);
521
522 QPixmap largeFrame(frameSize);
523 largeFrame.fill(Qt::transparent);
524
525 KPixmapModifier::applyFrame(largeFrame, frameSize);
526
527 QPainter painter(&largeFrame);
528 painter.drawPixmap((largeFrame.width() - scaledPixmap.width() / scaledPixmap.devicePixelRatio()) / 2,
529 (largeFrame.height() - scaledPixmap.height() / scaledPixmap.devicePixelRatio()) / 2,
530 scaledPixmap);
531 scaledPixmap = largeFrame;
532 } else {
533 // The image must be shrinked as it is too large to fit into
534 // the available icon size
535 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
536 }
537 }
538 } else {
539 KPixmapModifier::scale(scaledPixmap, m_iconSize);
540 }
541
542 QHash<QByteArray, QVariant> data = rolesData(item);
543
544 const QStringList overlays = data["iconOverlays"].toStringList();
545 // Strangely KFileItem::overlays() returns empty string-values, so
546 // we need to check first whether an overlay must be drawn at all.
547 // It is more efficient to do it here, as KIconLoader::drawOverlays()
548 // assumes that an overlay will be drawn and has some additional
549 // setup time.
550 foreach (const QString& overlay, overlays) {
551 if (!overlay.isEmpty()) {
552 // There is at least one overlay, draw all overlays above m_pixmap
553 // and cancel the check
554 KIconLoader::global()->drawOverlays(overlays, scaledPixmap, KIconLoader::Desktop);
555 break;
556 }
557 }
558
559 data.insert("iconPixmap", scaledPixmap);
560
561 disconnect(m_model, &KFileItemModel::itemsChanged,
562 this, &KFileItemModelRolesUpdater::slotItemsChanged);
563 m_model->setData(index, data);
564 connect(m_model, &KFileItemModel::itemsChanged,
565 this, &KFileItemModelRolesUpdater::slotItemsChanged);
566
567 m_finishedItems.insert(item);
568 }
569
570 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item)
571 {
572 if (m_state != PreviewJobRunning) {
573 return;
574 }
575
576 m_changedItems.remove(item);
577
578 const int index = m_model->index(item);
579 if (index >= 0) {
580 QHash<QByteArray, QVariant> data;
581 data.insert("iconPixmap", QPixmap());
582
583 disconnect(m_model, &KFileItemModel::itemsChanged,
584 this, &KFileItemModelRolesUpdater::slotItemsChanged);
585 m_model->setData(index, data);
586 connect(m_model, &KFileItemModel::itemsChanged,
587 this, &KFileItemModelRolesUpdater::slotItemsChanged);
588
589 applyResolvedRoles(index, ResolveAll);
590 m_finishedItems.insert(item);
591 }
592 }
593
594 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
595 {
596 m_previewJob = 0;
597
598 if (m_state != PreviewJobRunning) {
599 return;
600 }
601
602 m_state = Idle;
603
604 if (!m_pendingPreviewItems.isEmpty()) {
605 startPreviewJob();
606 } else {
607 if (!m_changedItems.isEmpty()) {
608 updateChangedItems();
609 }
610 }
611 }
612
613 void KFileItemModelRolesUpdater::resolveNextSortRole()
614 {
615 if (m_state != ResolvingSortRole) {
616 return;
617 }
618
619 QSet<KFileItem>::iterator it = m_pendingSortRoleItems.begin();
620 while (it != m_pendingSortRoleItems.end()) {
621 const KFileItem item = *it;
622 const int index = m_model->index(item);
623
624 // Continue if the sort role has already been determined for the
625 // item, and the item has not been changed recently.
626 if (!m_changedItems.contains(item) && m_model->data(index).contains(m_model->sortRole())) {
627 it = m_pendingSortRoleItems.erase(it);
628 continue;
629 }
630
631 applySortRole(index);
632 m_pendingSortRoleItems.erase(it);
633 break;
634 }
635
636 if (!m_pendingSortRoleItems.isEmpty()) {
637 applySortProgressToModel();
638 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
639 } else {
640 m_state = Idle;
641
642 // Prevent that we try to update the items twice.
643 disconnect(m_model, &KFileItemModel::itemsMoved,
644 this, &KFileItemModelRolesUpdater::slotItemsMoved);
645 applySortProgressToModel();
646 connect(m_model, &KFileItemModel::itemsMoved,
647 this, &KFileItemModelRolesUpdater::slotItemsMoved);
648 startUpdating();
649 }
650 }
651
652 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
653 {
654 if (m_state != ResolvingAllRoles) {
655 return;
656 }
657
658 while (!m_pendingIndexes.isEmpty()) {
659 const int index = m_pendingIndexes.takeFirst();
660 const KFileItem item = m_model->fileItem(index);
661
662 if (m_finishedItems.contains(item)) {
663 continue;
664 }
665
666 applyResolvedRoles(index, ResolveAll);
667 m_finishedItems.insert(item);
668 m_changedItems.remove(item);
669 break;
670 }
671
672 if (!m_pendingIndexes.isEmpty()) {
673 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
674 } else {
675 m_state = Idle;
676
677 if (m_clearPreviews) {
678 // Only go through the list if there are items which might still have previews.
679 if (m_finishedItems.count() != m_model->count()) {
680 QHash<QByteArray, QVariant> data;
681 data.insert("iconPixmap", QPixmap());
682
683 disconnect(m_model, &KFileItemModel::itemsChanged,
684 this, &KFileItemModelRolesUpdater::slotItemsChanged);
685 for (int index = 0; index <= m_model->count(); ++index) {
686 if (m_model->data(index).contains("iconPixmap")) {
687 m_model->setData(index, data);
688 }
689 }
690 connect(m_model, &KFileItemModel::itemsChanged,
691 this, &KFileItemModelRolesUpdater::slotItemsChanged);
692
693 }
694 m_clearPreviews = false;
695 }
696
697 if (!m_changedItems.isEmpty()) {
698 updateChangedItems();
699 }
700 }
701 }
702
703 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
704 {
705 m_changedItems += m_recentlyChangedItems;
706 m_recentlyChangedItems.clear();
707 updateChangedItems();
708 }
709
710 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString& itemUrl)
711 {
712 #ifdef HAVE_BALOO
713 const KFileItem item = m_model->fileItem(itemUrl);
714
715 if (item.isNull()) {
716 // itemUrl is not in the model anymore, probably because
717 // the corresponding file has been deleted in the meantime.
718 return;
719 }
720 applyChangedBalooRolesForItem(item);
721 #endif
722 }
723
724 void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem &item)
725 {
726 #ifdef HAVE_BALOO
727 Baloo::File file(item.localPath());
728 file.load();
729
730 const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance();
731 QHash<QByteArray, QVariant> data;
732
733 foreach (const QByteArray& role, rolesProvider.roles()) {
734 // Overwrite all the role values with an empty QVariant, because the roles
735 // provider doesn't overwrite it when the property value list is empty.
736 // See bug 322348
737 data.insert(role, QVariant());
738 }
739
740 QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(file, m_roles));
741 while (it.hasNext()) {
742 it.next();
743 data.insert(it.key(), it.value());
744 }
745
746 disconnect(m_model, &KFileItemModel::itemsChanged,
747 this, &KFileItemModelRolesUpdater::slotItemsChanged);
748 const int index = m_model->index(item);
749 m_model->setData(index, data);
750 connect(m_model, &KFileItemModel::itemsChanged,
751 this, &KFileItemModelRolesUpdater::slotItemsChanged);
752 #else
753 #ifndef Q_CC_MSVC
754 Q_UNUSED(itemUrl);
755 #endif
756 #endif
757 }
758
759 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString& path, int count)
760 {
761 const bool getSizeRole = m_roles.contains("size");
762 const bool getIsExpandableRole = m_roles.contains("isExpandable");
763
764 if (getSizeRole || getIsExpandableRole) {
765 const int index = m_model->index(QUrl::fromLocalFile(path));
766 if (index >= 0) {
767 QHash<QByteArray, QVariant> data;
768
769 if (getSizeRole) {
770 data.insert("size", count);
771 }
772 if (getIsExpandableRole) {
773 data.insert("isExpandable", count > 0);
774 }
775
776 disconnect(m_model, &KFileItemModel::itemsChanged,
777 this, &KFileItemModelRolesUpdater::slotItemsChanged);
778 m_model->setData(index, data);
779 connect(m_model, &KFileItemModel::itemsChanged,
780 this, &KFileItemModelRolesUpdater::slotItemsChanged);
781 }
782 }
783 }
784
785 void KFileItemModelRolesUpdater::startUpdating()
786 {
787 if (m_state == Paused) {
788 return;
789 }
790
791 if (m_finishedItems.count() == m_model->count()) {
792 // All roles have been resolved already.
793 m_state = Idle;
794 return;
795 }
796
797 // Terminate all updates that are currently active.
798 killPreviewJob();
799 m_pendingIndexes.clear();
800
801 QElapsedTimer timer;
802 timer.start();
803
804 // Determine the icons for the visible items synchronously.
805 updateVisibleIcons();
806
807 // A detailed update of the items in and near the visible area
808 // only makes sense if sorting is finished.
809 if (m_state == ResolvingSortRole) {
810 return;
811 }
812
813 // Start the preview job or the asynchronous resolving of all roles.
814 QList<int> indexes = indexesToResolve();
815
816 if (m_previewShown) {
817 m_pendingPreviewItems.clear();
818 m_pendingPreviewItems.reserve(indexes.count());
819
820 foreach (int index, indexes) {
821 const KFileItem item = m_model->fileItem(index);
822 if (!m_finishedItems.contains(item)) {
823 m_pendingPreviewItems.append(item);
824 }
825 }
826
827 startPreviewJob();
828 } else {
829 m_pendingIndexes = indexes;
830 // Trigger the asynchronous resolving of all roles.
831 m_state = ResolvingAllRoles;
832 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
833 }
834 }
835
836 void KFileItemModelRolesUpdater::updateVisibleIcons()
837 {
838 int lastVisibleIndex = m_lastVisibleIndex;
839 if (lastVisibleIndex <= 0) {
840 // Guess a reasonable value for the last visible index if the view
841 // has not told us about the real value yet.
842 lastVisibleIndex = qMin(m_firstVisibleIndex + m_maximumVisibleItems, m_model->count() - 1);
843 if (lastVisibleIndex <= 0) {
844 lastVisibleIndex = qMin(200, m_model->count() - 1);
845 }
846 }
847
848 QElapsedTimer timer;
849 timer.start();
850
851 // Try to determine the final icons for all visible items.
852 int index;
853 for (index = m_firstVisibleIndex; index <= lastVisibleIndex && timer.elapsed() < MaxBlockTimeout; ++index) {
854 applyResolvedRoles(index, ResolveFast);
855 }
856
857 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
858 // preliminary icons (i.e., without mime type determination) for the
859 // remaining items.
860 }
861
862 void KFileItemModelRolesUpdater::startPreviewJob()
863 {
864 m_state = PreviewJobRunning;
865
866 if (m_pendingPreviewItems.isEmpty()) {
867 QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished()));
868 return;
869 }
870
871 // PreviewJob internally caches items always with the size of
872 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
873 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
874 // do a downscaling anyhow because of the frame, so in this case only the provided
875 // cache sizes are requested.
876 const QSize cacheSize = (m_iconSize.width() > 128) || (m_iconSize.height() > 128)
877 ? QSize(256, 256) : QSize(128, 128);
878
879 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
880 // worst case) might block the application for several seconds. To prevent such
881 // a blocking, we only pass items with known mime type to the preview job.
882 const int count = m_pendingPreviewItems.count();
883 KFileItemList itemSubSet;
884 itemSubSet.reserve(count);
885
886 if (m_pendingPreviewItems.first().isMimeTypeKnown()) {
887 // Some mime types are known already, probably because they were
888 // determined when loading the icons for the visible items. Start
889 // a preview job for all items at the beginning of the list which
890 // have a known mime type.
891 do {
892 itemSubSet.append(m_pendingPreviewItems.takeFirst());
893 } while (!m_pendingPreviewItems.isEmpty() && m_pendingPreviewItems.first().isMimeTypeKnown());
894 } else {
895 // Determine mime types for MaxBlockTimeout ms, and start a preview
896 // job for the corresponding items.
897 QElapsedTimer timer;
898 timer.start();
899
900 do {
901 const KFileItem item = m_pendingPreviewItems.takeFirst();
902 item.determineMimeType();
903 itemSubSet.append(item);
904 } while (!m_pendingPreviewItems.isEmpty() && timer.elapsed() < MaxBlockTimeout);
905 }
906
907 KIO::PreviewJob* job = new KIO::PreviewJob(itemSubSet, cacheSize, &m_enabledPlugins);
908
909 job->setIgnoreMaximumSize(itemSubSet.first().isLocalFile());
910 if (job->ui()) {
911 KJobWidgets::setWindow(job, qApp->activeWindow());
912 }
913
914 connect(job, &KIO::PreviewJob::gotPreview,
915 this, &KFileItemModelRolesUpdater::slotGotPreview);
916 connect(job, &KIO::PreviewJob::failed,
917 this, &KFileItemModelRolesUpdater::slotPreviewFailed);
918 connect(job, &KIO::PreviewJob::finished,
919 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished);
920
921 m_previewJob = job;
922 }
923
924 void KFileItemModelRolesUpdater::updateChangedItems()
925 {
926 if (m_state == Paused) {
927 return;
928 }
929
930 if (m_changedItems.isEmpty()) {
931 return;
932 }
933
934 m_finishedItems -= m_changedItems;
935
936 if (m_resolvableRoles.contains(m_model->sortRole())) {
937 m_pendingSortRoleItems += m_changedItems;
938
939 if (m_state != ResolvingSortRole) {
940 // Stop the preview job if necessary, and trigger the
941 // asynchronous determination of the sort role.
942 killPreviewJob();
943 m_state = ResolvingSortRole;
944 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
945 }
946
947 return;
948 }
949
950 QList<int> visibleChangedIndexes;
951 QList<int> invisibleChangedIndexes;
952
953 foreach (const KFileItem& item, m_changedItems) {
954 const int index = m_model->index(item);
955
956 if (index < 0) {
957 m_changedItems.remove(item);
958 continue;
959 }
960
961 if (index >= m_firstVisibleIndex && index <= m_lastVisibleIndex) {
962 visibleChangedIndexes.append(index);
963 } else {
964 invisibleChangedIndexes.append(index);
965 }
966 }
967
968 std::sort(visibleChangedIndexes.begin(), visibleChangedIndexes.end());
969
970 if (m_previewShown) {
971 foreach (int index, visibleChangedIndexes) {
972 m_pendingPreviewItems.append(m_model->fileItem(index));
973 }
974
975 foreach (int index, invisibleChangedIndexes) {
976 m_pendingPreviewItems.append(m_model->fileItem(index));
977 }
978
979 if (!m_previewJob) {
980 startPreviewJob();
981 }
982 } else {
983 const bool resolvingInProgress = !m_pendingIndexes.isEmpty();
984 m_pendingIndexes = visibleChangedIndexes + m_pendingIndexes + invisibleChangedIndexes;
985 if (!resolvingInProgress) {
986 // Trigger the asynchronous resolving of the changed roles.
987 m_state = ResolvingAllRoles;
988 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
989 }
990 }
991 }
992
993 void KFileItemModelRolesUpdater::applySortRole(int index)
994 {
995 QHash<QByteArray, QVariant> data;
996 const KFileItem item = m_model->fileItem(index);
997
998 if (m_model->sortRole() == "type") {
999 if (!item.isMimeTypeKnown()) {
1000 item.determineMimeType();
1001 }
1002
1003 data.insert("type", item.mimeComment());
1004 } else if (m_model->sortRole() == "size" && item.isLocalFile() && item.isDir()) {
1005 const QString path = item.localPath();
1006 data.insert("size", m_directoryContentsCounter->countDirectoryContentsSynchronously(path));
1007 } else {
1008 // Probably the sort role is a baloo role - just determine all roles.
1009 data = rolesData(item);
1010 }
1011
1012 disconnect(m_model, &KFileItemModel::itemsChanged,
1013 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1014 m_model->setData(index, data);
1015 connect(m_model, &KFileItemModel::itemsChanged,
1016 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1017 }
1018
1019 void KFileItemModelRolesUpdater::applySortProgressToModel()
1020 {
1021 // Inform the model about the progress of the resolved items,
1022 // so that it can give an indication when the sorting has been finished.
1023 const int resolvedCount = m_model->count() - m_pendingSortRoleItems.count();
1024 m_model->emitSortProgress(resolvedCount);
1025 }
1026
1027 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index, ResolveHint hint)
1028 {
1029 const KFileItem item = m_model->fileItem(index);
1030 const bool resolveAll = (hint == ResolveAll);
1031
1032 bool iconChanged = false;
1033 if (!item.isMimeTypeKnown() || !item.isFinalIconKnown()) {
1034 item.determineMimeType();
1035 iconChanged = true;
1036 } else if (!m_model->data(index).contains("iconName")) {
1037 iconChanged = true;
1038 }
1039
1040 if (iconChanged || resolveAll || m_clearPreviews) {
1041 if (index < 0) {
1042 return false;
1043 }
1044
1045 QHash<QByteArray, QVariant> data;
1046 if (resolveAll) {
1047 data = rolesData(item);
1048 }
1049
1050 data.insert("iconName", item.iconName());
1051
1052 if (m_clearPreviews) {
1053 data.insert("iconPixmap", QPixmap());
1054 }
1055
1056 disconnect(m_model, &KFileItemModel::itemsChanged,
1057 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1058 m_model->setData(index, data);
1059 connect(m_model, &KFileItemModel::itemsChanged,
1060 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1061 return true;
1062 }
1063
1064 return false;
1065 }
1066
1067 QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileItem& item)
1068 {
1069 QHash<QByteArray, QVariant> data;
1070
1071 const bool getSizeRole = m_roles.contains("size");
1072 const bool getIsExpandableRole = m_roles.contains("isExpandable");
1073
1074 if ((getSizeRole || getIsExpandableRole) && item.isDir()) {
1075 if (item.isLocalFile()) {
1076 // Tell m_directoryContentsCounter that we want to count the items
1077 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1078 const QString path = item.localPath();
1079 m_directoryContentsCounter->addDirectory(path);
1080 } else if (getSizeRole) {
1081 data.insert("size", -1); // -1 indicates an unknown number of items
1082 }
1083 }
1084
1085 if (m_roles.contains("type")) {
1086 data.insert("type", item.mimeComment());
1087 }
1088
1089 QStringList overlays = item.overlays();
1090 foreach(KOverlayIconPlugin *it, m_overlayIconsPlugin) {
1091 overlays.append(it->getOverlays(item.url()));
1092 }
1093 data.insert("iconOverlays", overlays);
1094
1095 #ifdef HAVE_BALOO
1096 if (m_balooFileMonitor) {
1097 m_balooFileMonitor->addFile(item.localPath());
1098 applyChangedBalooRolesForItem(item);
1099 }
1100 #endif
1101 return data;
1102 }
1103
1104 void KFileItemModelRolesUpdater::slotOverlaysChanged(const QUrl& url, const QStringList &)
1105 {
1106 const KFileItem item = m_model->fileItem(url);
1107 if (item.isNull()) {
1108 return;
1109 }
1110 const int index = m_model->index(item);
1111 QHash<QByteArray, QVariant> data = m_model->data(index);
1112 QStringList overlays = item.overlays();
1113 foreach (KOverlayIconPlugin *it, m_overlayIconsPlugin) {
1114 overlays.append(it->getOverlays(url));
1115 }
1116 data.insert("iconOverlays", overlays);
1117 m_model->setData(index, data);
1118 }
1119
1120 void KFileItemModelRolesUpdater::updateAllPreviews()
1121 {
1122 if (m_state == Paused) {
1123 m_previewChangedDuringPausing = true;
1124 } else {
1125 m_finishedItems.clear();
1126 startUpdating();
1127 }
1128 }
1129
1130 void KFileItemModelRolesUpdater::killPreviewJob()
1131 {
1132 if (m_previewJob) {
1133 disconnect(m_previewJob, &KIO::PreviewJob::gotPreview,
1134 this, &KFileItemModelRolesUpdater::slotGotPreview);
1135 disconnect(m_previewJob, &KIO::PreviewJob::failed,
1136 this, &KFileItemModelRolesUpdater::slotPreviewFailed);
1137 disconnect(m_previewJob, &KIO::PreviewJob::finished,
1138 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished);
1139 m_previewJob->kill();
1140 m_previewJob = 0;
1141 m_pendingPreviewItems.clear();
1142 }
1143 }
1144
1145 QList<int> KFileItemModelRolesUpdater::indexesToResolve() const
1146 {
1147 const int count = m_model->count();
1148
1149 QList<int> result;
1150 result.reserve(ResolveAllItemsLimit);
1151
1152 // Add visible items.
1153 for (int i = m_firstVisibleIndex; i <= m_lastVisibleIndex; ++i) {
1154 result.append(i);
1155 }
1156
1157 // We need a reasonable upper limit for number of items to resolve after
1158 // and before the visible range. m_maximumVisibleItems can be quite large
1159 // when using Compace View.
1160 const int readAheadItems = qMin(ReadAheadPages * m_maximumVisibleItems, ResolveAllItemsLimit / 2);
1161
1162 // Add items after the visible range.
1163 const int endExtendedVisibleRange = qMin(m_lastVisibleIndex + readAheadItems, count - 1);
1164 for (int i = m_lastVisibleIndex + 1; i <= endExtendedVisibleRange; ++i) {
1165 result.append(i);
1166 }
1167
1168 // Add items before the visible range in reverse order.
1169 const int beginExtendedVisibleRange = qMax(0, m_firstVisibleIndex - readAheadItems);
1170 for (int i = m_firstVisibleIndex - 1; i >= beginExtendedVisibleRange; --i) {
1171 result.append(i);
1172 }
1173
1174 // Add items on the last page.
1175 const int beginLastPage = qMax(qMin(endExtendedVisibleRange + 1, count - 1), count - m_maximumVisibleItems);
1176 for (int i = beginLastPage; i < count; ++i) {
1177 result.append(i);
1178 }
1179
1180 // Add items on the first page.
1181 const int endFirstPage = qMin(qMax(beginExtendedVisibleRange - 1, 0), m_maximumVisibleItems);
1182 for (int i = 0; i <= endFirstPage; ++i) {
1183 result.append(i);
1184 }
1185
1186 // Continue adding items until ResolveAllItemsLimit is reached.
1187 int remainingItems = ResolveAllItemsLimit - result.count();
1188
1189 for (int i = endExtendedVisibleRange + 1; i < beginLastPage && remainingItems > 0; ++i) {
1190 result.append(i);
1191 --remainingItems;
1192 }
1193
1194 for (int i = beginExtendedVisibleRange - 1; i > endFirstPage && remainingItems > 0; --i) {
1195 result.append(i);
1196 --remainingItems;
1197 }
1198
1199 return result;
1200 }
1201