]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodelrolesupdater.cpp
Merge branch 'master' of git.kde.org:dolphin
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.cpp
index 5c212a98422f0a937c53edea02646ad5117bc39c..6e1c3d3ddd08a62d423665b2db6b2d1a446348f7 100644 (file)
 
 #include <KConfig>
 #include <KConfigGroup>
-#include <KDebug>
+#include <KSharedConfig>
 #include <KFileItem>
-#include <KGlobal>
 #include <KIconLoader>
 #include <KJobWidgets>
 #include <KIO/JobUiDelegate>
 #include <KIO/PreviewJob>
+#include <KPluginLoader>
+#include <KOverlayIconPlugin>
 
 #include "private/kpixmapmodifier.h"
 #include "private/kdirectorycontentscounter.h"
 #ifdef HAVE_BALOO
     #include "private/kbaloorolesprovider.h"
     #include <Baloo/File>
-    #include <Baloo/FileFetchJob>
     #include <Baloo/FileMonitor>
 #endif
 
+
 // #define KFILEITEMMODELROLESUPDATER_DEBUG
 
 namespace {
@@ -97,11 +98,11 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO
 {
     Q_ASSERT(model);
 
-    const KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
+    const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
     m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList()
-                                                         << "directorythumbnail"
-                                                         << "imagethumbnail"
-                                                         << "jpegthumbnail");
+                                                         << QStringLiteral("directorythumbnail")
+                                                         << QStringLiteral("imagethumbnail")
+                                                         << QStringLiteral("jpegthumbnail"));
 
     connect(m_model, &KFileItemModel::itemsInserted,
             this,    &KFileItemModelRolesUpdater::slotItemsInserted);
@@ -131,6 +132,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<KOverlayIconPlugin*>(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,7 +293,7 @@ void KFileItemModelRolesUpdater::setRoles(const QSet<QByteArray>& 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);
@@ -361,9 +374,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);
@@ -503,7 +516,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 +525,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 {
@@ -622,7 +635,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 +670,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 +707,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<KFileItem>();
+    Baloo::File file(item.localPath());
+    file.load();
 
     const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance();
     QHash<QByteArray, QVariant> data;
@@ -731,8 +737,7 @@ void KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished(KJob* kjob)
         data.insert(role, QVariant());
     }
 
-    Baloo::FileFetchJob* job = static_cast<Baloo::FileFetchJob*>(kjob);
-    QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(job->file(), m_roles));
+    QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(file, m_roles));
     while (it.hasNext()) {
         it.next();
         data.insert(it.key(), it.value());
@@ -744,6 +749,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 +762,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<QByteArray, QVariant> data;
 
@@ -820,7 +829,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 +864,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 +907,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 +941,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 +985,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 +1086,37 @@ QHash<QByteArray, QVariant> 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<QByteArray, QVariant> 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) {