Commit 49230140 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇
Browse files

[Image Wallpaper] Fix thumbnail generation when model is reloaded in-flight

GHNS Quick Button signals changedEntriesChanged initially even if nothing changed.
While this is a bug that should be fixed in GHNS, the wallpaper model didn't handle the case well,
when the model was reloaded while a preview was being generated.

It looks up whether a preview exists by QUrl and stores a QPersistentModelIndex.
However, when the model is reloaded and it accesses data for the same QUrl, the job might still be in-flight,
but the QPersistentModelIndex is invalid as the model has been reset.

This fixes it by performing all lookups exclusively on QPersistentModelIndex.
The hash lookup is optimized for data() (loads of accesses) with the slower key() lookup being done only
when the less frequently occurring thumbnail generation finishes/fails.
It will now do two preview jobs initially, but at least it shows something again...

BUG: 419234
FIXED-IN: 5.18.4

Differential Revision: https://phabricator.kde.org/D28420
parent 2caf4d0b
......@@ -347,11 +347,11 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const
return *cachedPreview;
}
const QUrl file = QUrl::fromLocalFile(path);
if (!m_previewJobs.contains(file) && file.isValid()) {
const QUrl url = QUrl::fromLocalFile(path);
const QPersistentModelIndex persistentIndex(index);
if (!m_previewJobsUrls.contains(persistentIndex) && url.isValid()) {
KFileItemList list;
list.append(KFileItem(file, QString(), 0));
list.append(KFileItem(url, QString(), 0));
QStringList availablePlugins = KIO::PreviewJob::availablePlugins();
KIO::PreviewJob* job = KIO::filePreview(list,
QSize(m_screenshotSize*1.6,
......@@ -361,7 +361,7 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const
this, &BackgroundListModel::showPreview);
connect(job, &KIO::PreviewJob::failed,
this, &BackgroundListModel::previewFailed);
const_cast<BackgroundListModel *>(this)->m_previewJobs.insert(file, QPersistentModelIndex(index));
const_cast<BackgroundListModel *>(this)->m_previewJobsUrls.insert(persistentIndex, url);
}
return QVariant();
......@@ -436,8 +436,8 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev
return;
}
QPersistentModelIndex index = m_previewJobs.value(item.url());
m_previewJobs.remove(item.url());
QPersistentModelIndex index = m_previewJobsUrls.key(item.url());
m_previewJobsUrls.remove(index);
if (!index.isValid()) {
return;
......@@ -457,7 +457,7 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev
void BackgroundListModel::previewFailed(const KFileItem &item)
{
m_previewJobs.remove(item.url());
m_previewJobsUrls.remove(m_previewJobsUrls.key(item.url()));
}
KPackage::Package BackgroundListModel::package(int index) const
......
......@@ -114,7 +114,7 @@ private:
QSet<QString> m_removableWallpapers;
QHash<QString, QSize> m_sizeCache;
QHash<QUrl, QPersistentModelIndex> m_previewJobs;
QHash<QPersistentModelIndex, QUrl> m_previewJobsUrls;
KDirWatch m_dirwatch;
QCache<QString, QPixmap> m_imageCache;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment