Kicker: prevents opening a recent file whose application does not support opening

wilder-5.24
Méven Car 5 years ago
parent bbb8343a2d
commit ad37f96dc4
  1. 23
      applets/kicker/plugin/actionlist.cpp
  2. 25
      applets/kicker/plugin/recentusagemodel.cpp
  3. 2
      applets/kicker/plugin/recentusagemodel.h

@ -285,6 +285,7 @@ QVariantList recentDocumentActions(KService::Ptr service)
while (list.count() < 6 && resultIt != results.end()) { while (list.count() < 6 && resultIt != results.end()) {
const QString resource = (*resultIt).resource(); const QString resource = (*resultIt).resource();
const QString mimeType = (*resultIt).mimetype();
++resultIt; ++resultIt;
const QUrl url(resource); const QUrl url(resource);
@ -303,7 +304,7 @@ QVariantList recentDocumentActions(KService::Ptr service)
list << createTitleActionItem(i18n("Recent Files")); list << createTitleActionItem(i18n("Recent Files"));
} }
QVariantMap item = createActionItem(url.fileName(), fileItem.iconName(), QStringLiteral("_kicker_recentDocument"), resource); QVariantMap item = createActionItem(url.fileName(), fileItem.iconName(), QStringLiteral("_kicker_recentDocument"), QStringList{resource, mimeType});
list << item; list << item;
} }
@ -343,14 +344,30 @@ bool handleRecentDocumentAction(KService::Ptr service, const QString &actionId,
return false; return false;
} }
QString argument = _argument.toString(); const QStringList argument = _argument.toStringList();
const auto resource = argument.at(0);
const auto mimeType = argument.at(1);
// prevents using a service file that does not support opening a mime type for a file it created
// for instance a screenshot tool
if (!mimeType.isEmpty()) {
if (!service->hasMimeType(mimeType)) {
// needs to find the application that supports this mimetype
service = KApplicationTrader::preferredService(mimeType);
if (!service) {
// no service found to handle the mimetype
return false;
}
}
}
if (argument.isEmpty()) { if (argument.isEmpty()) {
return false; return false;
} }
auto *job = new KIO::ApplicationLauncherJob(service); auto *job = new KIO::ApplicationLauncherJob(service);
job->setUrls({QUrl(argument)}); job->setUrls({QUrl::fromUserInput(resource)});
job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled)); job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
return job->exec(); return job->exec();
} }

@ -29,6 +29,7 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <KNotificationJobUiDelegate> #include <KNotificationJobUiDelegate>
#include <KRun> #include <KRun>
#include <KService/KApplicationTrader>
#include <KService> #include <KService>
#include <KStartupInfo> #include <KStartupInfo>
@ -159,14 +160,19 @@ QString RecentUsageModel::description() const
} }
QString RecentUsageModel::resourceAt(int row) const QString RecentUsageModel::resourceAt(int row) const
{
return rowValueAt(row, ResultModel::ResourceRole).toString();
}
QVariant RecentUsageModel::rowValueAt(int row, ResultModel::Roles role) const
{ {
QSortFilterProxyModel *sourceProxy = qobject_cast<QSortFilterProxyModel *>(sourceModel()); QSortFilterProxyModel *sourceProxy = qobject_cast<QSortFilterProxyModel *>(sourceModel());
if (sourceProxy) { if (sourceProxy) {
return sourceProxy->sourceModel()->data(sourceProxy->mapToSource(sourceProxy->index(row, 0)), ResultModel::ResourceRole).toString(); return sourceProxy->sourceModel()->data(sourceProxy->mapToSource(sourceProxy->index(row, 0)), role).toString();
} }
return sourceModel()->data(index(row, 0), ResultModel::ResourceRole).toString(); return sourceModel()->data(index(row, 0), role);
} }
QVariant RecentUsageModel::data(const QModelIndex &index, int role) const QVariant RecentUsageModel::data(const QModelIndex &index, int role) const
@ -339,6 +345,7 @@ bool RecentUsageModel::trigger(int row, const QString &actionId, const QVariant
if (actionId.isEmpty() && withinBounds) { if (actionId.isEmpty() && withinBounds) {
const QString &resource = resourceAt(row); const QString &resource = resourceAt(row);
const QString &mimeType = rowValueAt(row, ResultModel::MimeType).toString();
if (!resource.startsWith(QLatin1String("applications:"))) { if (!resource.startsWith(QLatin1String("applications:"))) {
const QUrl resourceUrl = docData(resource, Kicker::UrlRole).toUrl(); const QUrl resourceUrl = docData(resource, Kicker::UrlRole).toUrl();
@ -364,6 +371,20 @@ bool RecentUsageModel::trigger(int row, const QString &actionId, const QVariant
} }
#endif #endif
// prevents using a service file that does not support opening a mime type for a file it created
// for instance a screenshot tool
if (!mimeType.isEmpty()) {
if (!service->hasMimeType(mimeType)) {
// needs to find the application that supports this mimetype
service = KApplicationTrader::preferredService(mimeType);
if (!service) {
// no service found to handle the mimetype
return false;
}
}
}
auto *job = new KIO::ApplicationLauncherJob(service); auto *job = new KIO::ApplicationLauncherJob(service);
job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled)); job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
job->setStartupId(KStartupInfo::createNewStartupIdForTimestamp(timeStamp)); job->setStartupId(KStartupInfo::createNewStartupIdForTimestamp(timeStamp));

@ -11,6 +11,7 @@
#include <KFilePlacesModel> #include <KFilePlacesModel>
#include <QQmlParserStatus> #include <QQmlParserStatus>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <KActivities/Stats/ResultModel>
class QModelIndex; class QModelIndex;
class KFileItem; class KFileItem;
@ -100,6 +101,7 @@ private:
QVariant docData(const QString &resource, int role) const; QVariant docData(const QString &resource, int role) const;
QString resourceAt(int row) const; QString resourceAt(int row) const;
QVariant rowValueAt(int row, KActivities::Stats::ResultModel::Roles role) const;
QString forgetAllActionName() const; QString forgetAllActionName() const;

Loading…
Cancel
Save