X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/4cf04b91dd8804f2536fc31ae2f1b486bfc8cf9c..1a6b3c0a2baba955259d6083c0a3f25dfb44a682:/src/kitemviews/kfileitemmodelrolesupdater.cpp diff --git a/src/kitemviews/kfileitemmodelrolesupdater.cpp b/src/kitemviews/kfileitemmodelrolesupdater.cpp index acb3e0f99..a08f1ae15 100644 --- a/src/kitemviews/kfileitemmodelrolesupdater.cpp +++ b/src/kitemviews/kfileitemmodelrolesupdater.cpp @@ -23,13 +23,14 @@ #include #include -#include +#include #include -#include #include #include #include #include +#include +#include #include "private/kpixmapmodifier.h" #include "private/kdirectorycontentscounter.h" @@ -44,11 +45,11 @@ #ifdef HAVE_BALOO #include "private/kbaloorolesprovider.h" - #include - #include - #include + #include + #include #endif + // #define KFILEITEMMODELROLESUPDATER_DEBUG namespace { @@ -87,21 +88,18 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO m_pendingIndexes(), m_pendingPreviewItems(), m_previewJob(), - m_recentlyChangedItemsTimer(0), + m_recentlyChangedItemsTimer(nullptr), m_recentlyChangedItems(), m_changedItems(), - m_directoryContentsCounter(0) + m_directoryContentsCounter(nullptr) #ifdef HAVE_BALOO - , m_balooFileMonitor(0) + , m_balooFileMonitor(nullptr) #endif { Q_ASSERT(model); - const KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings"); - m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList() - << "directorythumbnail" - << "imagethumbnail" - << "jpegthumbnail"); + const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings"); + m_enabledPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins()); connect(m_model, &KFileItemModel::itemsInserted, this, &KFileItemModelRolesUpdater::slotItemsInserted); @@ -131,6 +129,18 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO m_directoryContentsCounter = new KDirectoryContentsCounter(m_model, this); connect(m_directoryContentsCounter, &KDirectoryContentsCounter::result, this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived); + + auto plugins = KPluginLoader::instantiatePlugins(QStringLiteral("kf5/overlayicon"), nullptr, qApp); + foreach (QObject *it, plugins) { + auto plugin = qobject_cast(it); + if (plugin) { + m_overlayIconsPlugin.append(plugin); + connect(plugin, &KOverlayIconPlugin::overlaysChanged, this, &KFileItemModelRolesUpdater::slotOverlaysChanged); + } else { + // not our/valid plugin, so delete the created object + it->deleteLater(); + } + } } KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater() @@ -280,13 +290,13 @@ void KFileItemModelRolesUpdater::setRoles(const QSet& roles) } } - if (hasBalooRole && !m_balooFileMonitor) { + if (hasBalooRole && m_balooConfig.fileIndexingEnabled() && !m_balooFileMonitor) { m_balooFileMonitor = new Baloo::FileMonitor(this); connect(m_balooFileMonitor, &Baloo::FileMonitor::fileMetaDataChanged, this, &KFileItemModelRolesUpdater::applyChangedBalooRoles); } else if (!hasBalooRole && m_balooFileMonitor) { delete m_balooFileMonitor; - m_balooFileMonitor = 0; + m_balooFileMonitor = nullptr; } #endif @@ -361,9 +371,9 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRang m_balooFileMonitor->clear(); } else { QStringList newFileList; - foreach (const QString& itemUrl, m_balooFileMonitor->files()) { - if (m_model->index(itemUrl) >= 0) { - newFileList.append(itemUrl); + foreach (const QString& file, m_balooFileMonitor->files()) { + if (m_model->index(QUrl::fromLocalFile(file)) >= 0) { + newFileList.append(file); } } m_balooFileMonitor->setFiles(newFileList); @@ -492,8 +502,12 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi const QString mimeType = item.mimetype(); const int slashIndex = mimeType.indexOf(QLatin1Char('/')); - const QString mimeTypeGroup = mimeType.left(slashIndex); - if (mimeTypeGroup == QLatin1String("image")) { + const bool isFontPreview = mimeType.right(slashIndex).contains(QLatin1String("font")); + const bool isFolderPreview = item.isDir(); + const bool isWindowsExePreview = mimeType == QLatin1String("application/x-ms-dos-executable") || + mimeType == QLatin1String("application/x-msdownload"); + + if (!isFolderPreview && !isFontPreview && !isWindowsExePreview) { if (m_enlargeSmallPreviews) { KPixmapModifier::applyFrame(scaledPixmap, m_iconSize); } else { @@ -503,7 +517,7 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi const bool enlargingRequired = scaledPixmap.width() < contentSize.width() && scaledPixmap.height() < contentSize.height(); if (enlargingRequired) { - QSize frameSize = scaledPixmap.size(); + QSize frameSize = scaledPixmap.size() / scaledPixmap.devicePixelRatio(); frameSize.scale(m_iconSize, Qt::KeepAspectRatio); QPixmap largeFrame(frameSize); @@ -512,8 +526,8 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi KPixmapModifier::applyFrame(largeFrame, frameSize); QPainter painter(&largeFrame); - painter.drawPixmap((largeFrame.width() - scaledPixmap.width()) / 2, - (largeFrame.height() - scaledPixmap.height()) / 2, + painter.drawPixmap((largeFrame.width() - scaledPixmap.width() / scaledPixmap.devicePixelRatio()) / 2, + (largeFrame.height() - scaledPixmap.height() / scaledPixmap.devicePixelRatio()) / 2, scaledPixmap); scaledPixmap = largeFrame; } else { @@ -580,7 +594,7 @@ void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item) void KFileItemModelRolesUpdater::slotPreviewJobFinished() { - m_previewJob = 0; + m_previewJob = nullptr; if (m_state != PreviewJobRunning) { return; @@ -622,7 +636,7 @@ void KFileItemModelRolesUpdater::resolveNextSortRole() if (!m_pendingSortRoleItems.isEmpty()) { applySortProgressToModel(); - QTimer::singleShot(0, this, SLOT(resolveNextSortRole())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole); } else { m_state = Idle; @@ -657,7 +671,7 @@ void KFileItemModelRolesUpdater::resolveNextPendingRoles() } if (!m_pendingIndexes.isEmpty()) { - QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles); } else { m_state = Idle; @@ -694,32 +708,25 @@ void KFileItemModelRolesUpdater::resolveRecentlyChangedItems() updateChangedItems(); } -void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString& itemUrl) +void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString& file) { #ifdef HAVE_BALOO - const KFileItem item = m_model->fileItem(itemUrl); + const KFileItem item = m_model->fileItem(QUrl::fromLocalFile(file)); if (item.isNull()) { // itemUrl is not in the model anymore, probably because // the corresponding file has been deleted in the meantime. return; } - - Baloo::FileFetchJob* job = new Baloo::FileFetchJob(item.localPath()); - connect(job, &Baloo::FileFetchJob::finished, this, &KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished); - job->setProperty("item", QVariant::fromValue(item)); - job->start(); -#else -#ifndef Q_CC_MSVC - Q_UNUSED(itemUrl); -#endif + applyChangedBalooRolesForItem(item); #endif } -void KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished(KJob* kjob) +void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem &item) { #ifdef HAVE_BALOO - const KFileItem item = kjob->property("item").value(); + Baloo::File file(item.localPath()); + file.load(); const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance(); QHash data; @@ -731,8 +738,7 @@ void KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished(KJob* kjob) data.insert(role, QVariant()); } - Baloo::FileFetchJob* job = static_cast(kjob); - QHashIterator it(rolesProvider.roleValues(job->file(), m_roles)); + QHashIterator it(rolesProvider.roleValues(file, m_roles)); while (it.hasNext()) { it.next(); data.insert(it.key(), it.value()); @@ -744,6 +750,10 @@ void KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished(KJob* kjob) m_model->setData(index, data); connect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged); +#else +#ifndef Q_CC_MSVC + Q_UNUSED(item); +#endif #endif } @@ -753,7 +763,7 @@ void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QStrin const bool getIsExpandableRole = m_roles.contains("isExpandable"); if (getSizeRole || getIsExpandableRole) { - const int index = m_model->index(KUrl(path)); + const int index = m_model->index(QUrl::fromLocalFile(path)); if (index >= 0) { QHash data; @@ -820,7 +830,7 @@ void KFileItemModelRolesUpdater::startUpdating() m_pendingIndexes = indexes; // Trigger the asynchronous resolving of all roles. m_state = ResolvingAllRoles; - QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles); } } @@ -855,7 +865,7 @@ void KFileItemModelRolesUpdater::startPreviewJob() m_state = PreviewJobRunning; if (m_pendingPreviewItems.isEmpty()) { - QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished); return; } @@ -898,7 +908,7 @@ void KFileItemModelRolesUpdater::startPreviewJob() KIO::PreviewJob* job = new KIO::PreviewJob(itemSubSet, cacheSize, &m_enabledPlugins); job->setIgnoreMaximumSize(itemSubSet.first().isLocalFile()); - if (job->ui()) { + if (job->uiDelegate()) { KJobWidgets::setWindow(job, qApp->activeWindow()); } @@ -932,7 +942,7 @@ void KFileItemModelRolesUpdater::updateChangedItems() // asynchronous determination of the sort role. killPreviewJob(); m_state = ResolvingSortRole; - QTimer::singleShot(0, this, SLOT(resolveNextSortRole())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole); } return; @@ -976,7 +986,7 @@ void KFileItemModelRolesUpdater::updateChangedItems() if (!resolvingInProgress) { // Trigger the asynchronous resolving of the changed roles. m_state = ResolvingAllRoles; - QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles())); + QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles); } } } @@ -1077,17 +1087,37 @@ QHash KFileItemModelRolesUpdater::rolesData(const KFileIte data.insert("type", item.mimeComment()); } - data.insert("iconOverlays", item.overlays()); + QStringList overlays = item.overlays(); + foreach(KOverlayIconPlugin *it, m_overlayIconsPlugin) { + overlays.append(it->getOverlays(item.url())); + } + data.insert("iconOverlays", overlays); #ifdef HAVE_BALOO if (m_balooFileMonitor) { m_balooFileMonitor->addFile(item.localPath()); - applyChangedBalooRoles(item.localPath()); + applyChangedBalooRolesForItem(item); } #endif return data; } +void KFileItemModelRolesUpdater::slotOverlaysChanged(const QUrl& url, const QStringList &) +{ + const KFileItem item = m_model->fileItem(url); + if (item.isNull()) { + return; + } + const int index = m_model->index(item); + QHash data = m_model->data(index); + QStringList overlays = item.overlays(); + foreach (KOverlayIconPlugin *it, m_overlayIconsPlugin) { + overlays.append(it->getOverlays(url)); + } + data.insert("iconOverlays", overlays); + m_model->setData(index, data); +} + void KFileItemModelRolesUpdater::updateAllPreviews() { if (m_state == Paused) { @@ -1108,7 +1138,7 @@ void KFileItemModelRolesUpdater::killPreviewJob() disconnect(m_previewJob, &KIO::PreviewJob::finished, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished); m_previewJob->kill(); - m_previewJob = 0; + m_previewJob = nullptr; m_pendingPreviewItems.clear(); } } @@ -1170,4 +1200,3 @@ QList KFileItemModelRolesUpdater::indexesToResolve() const return result; } -#include "kfileitemmodelrolesupdater.moc"