diff --git a/applets/kicker/plugin/fileentry.cpp b/applets/kicker/plugin/fileentry.cpp index 54d1dbd41..de633d3ad 100644 --- a/applets/kicker/plugin/fileentry.cpp +++ b/applets/kicker/plugin/fileentry.cpp @@ -9,14 +9,22 @@ #include #include +#include -FileEntry::FileEntry(AbstractModel *owner, const QUrl &url) +FileEntry::FileEntry(AbstractModel *owner, const QUrl &url, const QString &mimeType) : AbstractEntry(owner) , m_fileItem(nullptr) { if (url.isValid()) { - m_fileItem = new KFileItem(url); - m_fileItem->determineMimeType(); + if (url.isLocalFile()) { + m_fileItem = new KFileItem(url, mimeType); + m_fileItem->determineMimeType(); + } else { + KIO::StatJob *job = KIO::statDetails(url, KIO::StatJob::SourceSide, KIO::StatBasic, KIO::JobFlag::HideProgressInfo); + if (job->exec()) { + m_fileItem = new KFileItem(job->statResult(), url); + } + } } } diff --git a/applets/kicker/plugin/fileentry.h b/applets/kicker/plugin/fileentry.h index 200875b2a..09822ac6c 100644 --- a/applets/kicker/plugin/fileentry.h +++ b/applets/kicker/plugin/fileentry.h @@ -13,7 +13,7 @@ class KFileItem; class FileEntry : public AbstractEntry { public: - explicit FileEntry(AbstractModel *owner, const QUrl &url); + explicit FileEntry(AbstractModel *owner, const QUrl &url, const QString &mimeType = QString()); ~FileEntry() override; EntryType type() const override diff --git a/applets/kicker/plugin/kastatsfavoritesmodel.cpp b/applets/kicker/plugin/kastatsfavoritesmodel.cpp index e0142fcb9..8243a05ef 100644 --- a/applets/kicker/plugin/kastatsfavoritesmodel.cpp +++ b/applets/kicker/plugin/kastatsfavoritesmodel.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -37,6 +38,7 @@ using namespace KAStats::Terms; QString agentForUrl(const QString &url) { + QUrl u(url); // clang-format off return url.startsWith(QLatin1String("ktp:")) ? AGENT_CONTACTS @@ -48,6 +50,8 @@ QString agentForUrl(const QString &url) ? AGENT_DOCUMENTS : (url.startsWith(QLatin1String("file:/")) && !url.endsWith(QLatin1String(".desktop"))) ? AGENT_DOCUMENTS + : (u.scheme() != QLatin1String("file") && !u.scheme().isEmpty() && KProtocolInfo::isKnownProtocol(u.scheme())) + ? AGENT_DOCUMENTS // use applications as the default : AGENT_APPLICATIONS; // clang-format on @@ -139,7 +143,7 @@ public: return NormalizedId(this, id); } - QSharedPointer entryForResource(const QString &resource) const + QSharedPointer entryForResource(const QString &resource, const QString &mimeType = QString()) const { using SP = QSharedPointer; @@ -150,9 +154,9 @@ public: } else if (agent == AGENT_DOCUMENTS) { if (resource.startsWith(QLatin1String("/"))) { - return SP(new FileEntry(q, QUrl::fromLocalFile(resource))); + return SP(new FileEntry(q, QUrl::fromLocalFile(resource), mimeType)); } else { - return SP(new FileEntry(q, QUrl(resource))); + return SP(new FileEntry(q, QUrl(resource), mimeType)); } } else if (agent == AGENT_APPLICATIONS) { @@ -205,7 +209,7 @@ public: for (const auto &result : results) { qCDebug(KICKER_DEBUG) << "Got " << result.resource() << " -->"; - addResult(result.resource(), -1, false); + addResult(result.resource(), -1, false, result.mimetype()); } // Normalizing all the ids @@ -240,7 +244,7 @@ public: qCDebug(KICKER_DEBUG) << "After ordering: " << itemStrings; } - void addResult(const QString &_resource, int index, bool notifyModel = true) + void addResult(const QString &_resource, int index, bool notifyModel = true, const QString &mimeType = QString()) { // We want even files to have a proper URL const auto resource = _resource.startsWith(QLatin1Char('/')) ? QUrl::fromLocalFile(_resource).toString() : _resource; @@ -250,7 +254,7 @@ public: if (m_itemEntries.contains(resource)) return; - auto entry = entryForResource(resource); + auto entry = entryForResource(resource, mimeType); if (!entry || !entry->isValid()) { qCDebug(KICKER_DEBUG) << "Entry is not valid!" << resource; @@ -279,7 +283,10 @@ public: auto url = entry->url(); - m_itemEntries[resource] = m_itemEntries[entry->id()] = m_itemEntries[url.toString()] = m_itemEntries[url.toLocalFile()] = entry; + m_itemEntries[resource] = m_itemEntries[entry->id()] = m_itemEntries[url.toString()] = entry; + if (!url.toLocalFile().isEmpty()) { + m_itemEntries[url.toLocalFile()] = entry; + } auto normalized = normalizedId(resource); m_items.insert(index, normalized); @@ -293,7 +300,7 @@ public: void removeResult(const QString &resource) { - auto normalized = normalizedId(resource); + const auto normalized = normalizedId(resource); // If we know this item will not really be removed, // but only that activities it is on have changed, @@ -305,17 +312,23 @@ public: qCDebug(KICKER_DEBUG) << "Removing result" << resource; - auto index = m_items.indexOf(normalizedId(resource)); + auto index = m_items.indexOf(normalized); if (index == -1) return; beginRemoveRows(QModelIndex(), index, index); - auto entry = m_itemEntries[resource]; + const auto entry = m_itemEntries[resource]; m_items.removeAt(index); // Removing the entry from the cache - std::remove(m_itemEntries.begin(), m_itemEntries.end(), entry); + QMutableHashIterator> i(m_itemEntries); + while (i.hasNext()) { + i.next(); + if (i.value() == entry) { + i.remove(); + } + } endRemoveRows();