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