diff --git a/applets/kicker/CMakeLists.txt b/applets/kicker/CMakeLists.txt index 3834593ec..3e183545a 100644 --- a/applets/kicker/CMakeLists.txt +++ b/applets/kicker/CMakeLists.txt @@ -1,87 +1,88 @@ add_definitions( -DQT_USE_QSTRINGBUILDER -DQT_NO_CAST_TO_ASCII # -DQT_NO_CAST_FROM_ASCII -DQT_STRICT_ITERATORS -DQT_NO_URL_CAST_FROM_STRING -DQT_NO_CAST_FROM_BYTEARRAY -DQT_NO_SIGNALS_SLOTS_KEYWORDS -DQT_USE_FAST_OPERATOR_PLUS -DTRANSLATION_DOMAIN=\"libkicker\" ) set(kickerplugin_SRCS plugin/abstractentry.cpp plugin/abstractmodel.cpp plugin/actionlist.cpp plugin/appentry.cpp plugin/appsmodel.cpp plugin/computermodel.cpp plugin/contactentry.cpp plugin/containmentinterface.cpp plugin/draghelper.cpp plugin/simplefavoritesmodel.cpp plugin/kastatsfavoritesmodel.cpp plugin/fileentry.cpp plugin/forwardingmodel.cpp plugin/placeholdermodel.cpp plugin/funnelmodel.cpp plugin/dashboardwindow.cpp plugin/kickerplugin.cpp plugin/menuentryeditor.cpp plugin/processrunner.cpp plugin/rootmodel.cpp plugin/runnermodel.cpp plugin/runnermatchesmodel.cpp plugin/recentcontactsmodel.cpp plugin/recentusagemodel.cpp plugin/submenu.cpp plugin/systementry.cpp plugin/systemmodel.cpp plugin/systemsettings.cpp plugin/wheelinterceptor.cpp plugin/windowsystem.cpp plugin/funnelmodel.cpp ) ecm_qt_declare_logging_category(kickerplugin_SRCS HEADER debug.h IDENTIFIER KICKER_DEBUG CATEGORY_NAME org.kde.plasma.kicker) qt5_add_dbus_interface(kickerplugin_SRCS ${CMAKE_SOURCE_DIR}/krunner/dbus/org.kde.krunner.App.xml krunner_interface) qt5_add_dbus_interface(kickerplugin_SRCS ${CMAKE_SOURCE_DIR}/ksmserver/org.kde.KSMServerInterface.xml ksmserver_interface) install(FILES plugin/qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/private/kicker) add_library(kickerplugin SHARED ${kickerplugin_SRCS}) target_link_libraries(kickerplugin Qt5::Core Qt5::DBus Qt5::Qml Qt5::Quick Qt5::X11Extras KF5::Activities KF5::ActivitiesStats KF5::ConfigCore KF5::CoreAddons KF5::I18n KF5::ItemModels KF5::KDELibs4Support # FIXME: New Solid power management API doesn't exist yet, so we need to use deprecated stuff. KF5::KIOCore KF5::KIOWidgets + KF5::KIOFileWidgets KF5::People KF5::PeopleWidgets KF5::PlasmaQuick KF5::Runner KF5::Service KF5::Solid KF5::WindowSystem PW::KWorkspace) if (${HAVE_APPSTREAMQT}) target_link_libraries(kickerplugin AppStreamQt) endif() install(TARGETS kickerplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/private/kicker) diff --git a/applets/kicker/plugin/recentusagemodel.cpp b/applets/kicker/plugin/recentusagemodel.cpp index 6168b713d..37bc0709f 100644 --- a/applets/kicker/plugin/recentusagemodel.cpp +++ b/applets/kicker/plugin/recentusagemodel.cpp @@ -1,491 +1,526 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "recentusagemodel.h" #include "actionlist.h" #include "appsmodel.h" #include "appentry.h" #include "kastatsfavoritesmodel.h" #include #include #include #include #include #include +#include #if HAVE_X11 #include #endif #include #include #include #include #include #include #include +#include #include #include #include namespace KAStats = KActivities::Stats; using namespace KAStats; using namespace KAStats::Terms; GroupSortProxy::GroupSortProxy(AbstractModel *parentModel, QAbstractItemModel *sourceModel) : QSortFilterProxyModel(parentModel) { sourceModel->setParent(this); setSourceModel(sourceModel); sort(0); } GroupSortProxy::~GroupSortProxy() { } InvalidAppsFilterProxy::InvalidAppsFilterProxy(AbstractModel *parentModel, QAbstractItemModel *sourceModel) : QSortFilterProxyModel(parentModel) , m_parentModel(parentModel) { connect(parentModel, &AbstractModel::favoritesModelChanged, this, &InvalidAppsFilterProxy::connectNewFavoritesModel); connectNewFavoritesModel(); sourceModel->setParent(this); setSourceModel(sourceModel); } InvalidAppsFilterProxy::~InvalidAppsFilterProxy() { } void InvalidAppsFilterProxy::connectNewFavoritesModel() { KAStatsFavoritesModel* favoritesModel = static_cast(m_parentModel->favoritesModel()); connect(favoritesModel, &KAStatsFavoritesModel::favoritesChanged, this, &QSortFilterProxyModel::invalidate); invalidate(); } bool InvalidAppsFilterProxy::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { Q_UNUSED(source_parent); const QString resource = sourceModel()->index(source_row, 0).data(ResultModel::ResourceRole).toString(); if (resource.startsWith(QLatin1String("applications:"))) { KService::Ptr service = KService::serviceByStorageId(resource.section(QLatin1Char(':'), 1)); KAStatsFavoritesModel* favoritesModel = m_parentModel ? static_cast(m_parentModel->favoritesModel()) : nullptr; return (service && (!favoritesModel || !favoritesModel->isFavorite(service->storageId()))); } return true; } bool InvalidAppsFilterProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const { return (left.row() < right.row()); } bool GroupSortProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const { const QString &lResource = sourceModel()->data(left, ResultModel::ResourceRole).toString(); const QString &rResource = sourceModel()->data(right, ResultModel::ResourceRole).toString(); if (lResource.startsWith(QLatin1String("applications:")) && !rResource.startsWith(QLatin1String("applications:"))) { return true; } else if (!lResource.startsWith(QLatin1String("applications:")) && rResource.startsWith(QLatin1String("applications:"))) { return false; } return (left.row() < right.row()); } RecentUsageModel::RecentUsageModel(QObject *parent, IncludeUsage usage, int ordering) : ForwardingModel(parent) , m_usage(usage) , m_ordering((Ordering)ordering) , m_complete(false) +, m_placesModel(new KFilePlacesModel(this)) { refresh(); } RecentUsageModel::~RecentUsageModel() { } RecentUsageModel::IncludeUsage RecentUsageModel::usage() const { return m_usage; } QString RecentUsageModel::description() const { switch (m_usage) { case AppsAndDocs: return i18n("Recently Used"); case OnlyApps: return i18n("Applications"); case OnlyDocs: default: return i18n("Documents"); } } QString RecentUsageModel::resourceAt(int row) const { QSortFilterProxyModel *sourceProxy = qobject_cast(sourceModel()); if (sourceProxy) { return sourceProxy->sourceModel()->data(sourceProxy->mapToSource(sourceProxy->index(row, 0)), ResultModel::ResourceRole).toString(); } return sourceModel()->data(index(row, 0), ResultModel::ResourceRole).toString(); } QVariant RecentUsageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } const QString &resource = resourceAt(index.row()); if (resource.startsWith(QLatin1String("applications:"))) { return appData(resource, role); } else { return docData(resource, role); } } QVariant RecentUsageModel::appData(const QString &resource, int role) const { const QString storageId = resource.section(QLatin1Char(':'), 1); KService::Ptr service = KService::serviceByStorageId(storageId); QStringList allowedTypes({ QLatin1String("Service"), QLatin1String("Application") }); if (!service || !allowedTypes.contains(service->property(QLatin1String("Type")).toString()) || service->exec().isEmpty()) { return QVariant(); } if (role == Qt::DisplayRole) { AppsModel *parentModel = qobject_cast(QObject::parent()); if (parentModel) { return AppEntry::nameFromService(service, (AppEntry::NameFormat)qobject_cast(QObject::parent())->appNameFormat()); } else { return AppEntry::nameFromService(service, AppEntry::NameOnly); } } else if (role == Qt::DecorationRole) { return QIcon::fromTheme(service->icon(), QIcon::fromTheme(QStringLiteral("unknown"))); } else if (role == Kicker::DescriptionRole) { return service->comment(); } else if (role == Kicker::GroupRole) { return i18n("Applications"); } else if (role == Kicker::FavoriteIdRole) { return service->storageId(); } else if (role == Kicker::HasActionListRole) { return true; } else if (role == Kicker::ActionListRole) { QVariantList actionList; const QVariantList &jumpList = Kicker::jumpListActions(service); if (!jumpList.isEmpty()) { actionList << jumpList << Kicker::createSeparatorActionItem(); } const QVariantList &recentDocuments = Kicker::recentDocumentActions(service); if (!recentDocuments.isEmpty()) { actionList << recentDocuments << Kicker::createSeparatorActionItem(); } const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Application"), QStringLiteral("forget")); actionList << forgetAction; const QVariantMap &forgetAllAction = Kicker::createActionItem(forgetAllActionName(), QStringLiteral("forgetAll")); actionList << forgetAllAction; return actionList; } return QVariant(); } QVariant RecentUsageModel::docData(const QString &resource, int role) const { QUrl url(resource); if (url.scheme().isEmpty()) { url.setScheme(QStringLiteral("file")); } #if KIO_VERSION >= QT_VERSION_CHECK(5,57,0) // Avoid calling QT_LSTAT and accessing recent documents const KFileItem fileItem(url, KFileItem::SkipMimeTypeFromContent); #else const KFileItem fileItem(url); #endif if (!url.isValid()) { return QVariant(); } if (role == Qt::DisplayRole) { + const auto index = m_placesModel->closestItem(fileItem.url()); + if (index.isValid()) { + const auto parentUrl = m_placesModel->url(index); + if (parentUrl == fileItem.url()) { + return m_placesModel->text(index); + } + } return fileItem.text(); } else if (role == Qt::DecorationRole) { return QIcon::fromTheme(fileItem.iconName(), QIcon::fromTheme(QStringLiteral("unknown"))); } else if (role == Kicker::GroupRole) { return i18n("Documents"); } else if (role == Kicker::FavoriteIdRole || role == Kicker::UrlRole) { return url.toString(); + } else if (role == Kicker::DescriptionRole) { + QString desc = fileItem.localPath(); + + const auto index = m_placesModel->closestItem(fileItem.url()); + if (index.isValid()) { + // the current file has a parent in placesModel + const auto parentUrl = m_placesModel->url(index); + if (parentUrl == fileItem.url()) { + // if the current item is a place + return QString(); + } + desc.truncate(desc.lastIndexOf(QChar('/'))); + const auto text = m_placesModel->text(index); + desc.replace(0, parentUrl.path().length(), text); + } else { + // remove filename + desc.truncate(desc.lastIndexOf(QChar('/'))); + } + return desc; } else if (role == Kicker::UrlRole) { return url; } else if (role == Kicker::HasActionListRole) { return true; } else if (role == Kicker::ActionListRole) { QVariantList actionList = Kicker::createActionListForFileItem(fileItem); actionList << Kicker::createSeparatorActionItem(); + const QVariantMap &openParentFolder = Kicker::createActionItem(i18n("Open Containing Folder"), QStringLiteral("openParentFolder")); + actionList << openParentFolder; + const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Document"), QStringLiteral("forget")); actionList << forgetAction; const QVariantMap &forgetAllAction = Kicker::createActionItem(forgetAllActionName(), QStringLiteral("forgetAll")); actionList << forgetAllAction; return actionList; } return QVariant(); } bool RecentUsageModel::trigger(int row, const QString &actionId, const QVariant &argument) { Q_UNUSED(argument) bool withinBounds = row >= 0 && row < rowCount(); if (actionId.isEmpty() && withinBounds) { const QString &resource = resourceAt(row); if (!resource.startsWith(QLatin1String("applications:"))) { const QUrl resourceUrl = docData(resource, Kicker::UrlRole).toUrl(); const QList urlsList{resourceUrl}; QMimeDatabase db; QMimeType mime = db.mimeTypeForUrl(resourceUrl); KService::Ptr service = KMimeTypeTrader::self()->preferredService(mime.name()); if (service) { KRun::runApplication(*service, urlsList, nullptr); } else { QTimer::singleShot(0, [urlsList] { KRun::displayOpenWithDialog(urlsList, nullptr); }); } return true; } const QString storageId = resource.section(QLatin1Char(':'), 1); KService::Ptr service = KService::serviceByStorageId(storageId); if (!service) { return false; } quint32 timeStamp = 0; #if HAVE_X11 if (QX11Info::isPlatformX11()) { timeStamp = QX11Info::appUserTime(); } #endif // TODO Once we depend on KDE Frameworks 5.24 and D1902 is merged, use KRun::runApplication instead KRun::runService(*service, {}, nullptr, true, {}, KStartupInfo::createNewStartupIdForTimestamp(timeStamp)); KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + storageId), QStringLiteral("org.kde.plasma.kicker")); return true; } else if (actionId == QLatin1String("forget") && withinBounds) { if (m_activitiesModel) { QModelIndex idx = sourceModel()->index(row, 0); QSortFilterProxyModel *sourceProxy = qobject_cast(sourceModel()); while (sourceProxy) { idx = sourceProxy->mapToSource(idx); sourceProxy = qobject_cast(sourceProxy->sourceModel()); } static_cast(m_activitiesModel.data())->forgetResource(idx.row()); } return false; + } else if (actionId == QLatin1String("openParentFolder") && withinBounds) { + const auto url = QUrl::fromUserInput(resourceAt(row)); + KIO::highlightInFileManager({url}); } else if (actionId == QLatin1String("forgetAll")) { if (m_activitiesModel) { static_cast(m_activitiesModel.data())->forgetAllResources(); } return false; } else if (withinBounds) { const QString &resource = resourceAt(row); if (resource.startsWith(QLatin1String("applications:"))) { const QString storageId = sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString().section(QLatin1Char(':'), 1); KService::Ptr service = KService::serviceByStorageId(storageId); if (service) { return Kicker::handleRecentDocumentAction(service, actionId, argument); } } else { bool close = false; QUrl url(sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString()); KFileItem item(url); if (Kicker::handleFileItemAction(item, actionId, argument, &close)) { return close; } } } return false; } bool RecentUsageModel::hasActions() const { return rowCount(); } QVariantList RecentUsageModel::actions() const { QVariantList actionList; if (rowCount()) { actionList << Kicker::createActionItem(forgetAllActionName(), QStringLiteral("forgetAll")); } return actionList; } QString RecentUsageModel::forgetAllActionName() const { switch (m_usage) { case AppsAndDocs: return i18n("Forget All"); case OnlyApps: return i18n("Forget All Applications"); case OnlyDocs: default: return i18n("Forget All Documents"); } } void RecentUsageModel::setOrdering(int ordering) { if (ordering == m_ordering) return; m_ordering = (Ordering)ordering; refresh(); emit orderingChanged(ordering); } int RecentUsageModel::ordering() const { return m_ordering; } void RecentUsageModel::classBegin() { } void RecentUsageModel::componentComplete() { m_complete = true; refresh(); } void RecentUsageModel::refresh() { if (qmlEngine(this) && !m_complete) { return; } QAbstractItemModel *oldModel = sourceModel(); disconnectSignals(); setSourceModel(nullptr); delete oldModel; auto query = UsedResources | (m_ordering == Recent ? RecentlyUsedFirst : HighScoredFirst) | Agent::any() | Type::any() | Activity::current(); switch (m_usage) { case AppsAndDocs: { query = query | Url::startsWith(QStringLiteral("applications:")) | Url::file() | Limit(30); break; } case OnlyApps: { query = query | Url::startsWith(QStringLiteral("applications:")) | Limit(15); break; } case OnlyDocs: default: { query = query | Url::file() | Limit(15); } } m_activitiesModel = new ResultModel(query); QAbstractItemModel *model = m_activitiesModel; QModelIndex index; if (model->canFetchMore(index)) { model->fetchMore(index); } if (m_usage != OnlyDocs) { model = new InvalidAppsFilterProxy(this, model); } if (m_usage == AppsAndDocs) { model = new GroupSortProxy(this, model); } setSourceModel(model); } diff --git a/applets/kicker/plugin/recentusagemodel.h b/applets/kicker/plugin/recentusagemodel.h index 04c5936a2..a8a0fef04 100644 --- a/applets/kicker/plugin/recentusagemodel.h +++ b/applets/kicker/plugin/recentusagemodel.h @@ -1,115 +1,117 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef RECENTUSAGEMODEL_H #define RECENTUSAGEMODEL_H #include "forwardingmodel.h" #include #include +#include class GroupSortProxy : public QSortFilterProxyModel { Q_OBJECT public: explicit GroupSortProxy(AbstractModel *parentModel, QAbstractItemModel *sourceModel); ~GroupSortProxy() override; protected: bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; }; class InvalidAppsFilterProxy : public QSortFilterProxyModel { Q_OBJECT public: explicit InvalidAppsFilterProxy(AbstractModel *parentModel, QAbstractItemModel *sourceModel); ~InvalidAppsFilterProxy() override; protected: bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override; bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; private Q_SLOTS: void connectNewFavoritesModel(); private: QPointer m_parentModel; }; class RecentUsageModel : public ForwardingModel, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(int ordering READ ordering WRITE setOrdering NOTIFY orderingChanged) public: enum IncludeUsage { AppsAndDocs, OnlyApps, OnlyDocs }; enum Ordering { Recent, Popular }; explicit RecentUsageModel( QObject *parent = nullptr, IncludeUsage usage = AppsAndDocs, int ordering = Recent); ~RecentUsageModel() override; QString description() const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Q_INVOKABLE bool trigger(int row, const QString &actionId, const QVariant &argument) override; bool hasActions() const override; QVariantList actions() const override; IncludeUsage usage() const; void setOrdering(int ordering); int ordering() const; void classBegin() override; void componentComplete() override; Q_SIGNALS: void orderingChanged(int ordering); private Q_SLOTS: void refresh() override; private: QVariant appData(const QString &resource, int role) const; QVariant docData(const QString &resource, int role) const; QString resourceAt(int row) const; QString forgetAllActionName() const; IncludeUsage m_usage; QPointer m_activitiesModel; Ordering m_ordering; bool m_complete; + KFilePlacesModel *m_placesModel; }; #endif