File indexing completed on 2025-01-26 05:00:54
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de> 0003 * SPDX-FileCopyrightText: 2011 Aaron Seigo <aseigo@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "ActivityRunner.h" 0009 0010 #include <Activities.h> 0011 #include <KLocalizedString> 0012 #include <krunner1adaptor.h> 0013 0014 K_PLUGIN_CLASS(ActivityRunner) 0015 0016 ActivityRunner::ActivityRunner(QObject *parent) 0017 : Plugin(parent) 0018 , m_activitiesService(nullptr) 0019 , m_keywordi18n(i18nc("KRunner keyword", "activity")) 0020 , m_keyword(QStringLiteral("activity")) 0021 { 0022 setName(QStringLiteral("org.kde.ActivityManager.ActivityRunner")); 0023 new Krunner1Adaptor(this); 0024 qDBusRegisterMetaType<RemoteMatch>(); 0025 qDBusRegisterMetaType<RemoteMatches>(); 0026 qDBusRegisterMetaType<RemoteAction>(); 0027 qDBusRegisterMetaType<RemoteActions>(); 0028 QDBusConnection::sessionBus().registerObject(QStringLiteral("/runner"), this); 0029 QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.runners.activities")); 0030 } 0031 0032 ActivityRunner::~ActivityRunner() = default; 0033 0034 bool ActivityRunner::init(QHash<QString, QObject *> &modules) 0035 { 0036 Plugin::init(modules); 0037 0038 m_activitiesService = modules[QStringLiteral("activities")]; 0039 return true; 0040 } 0041 0042 RemoteMatches ActivityRunner::Match(const QString &query) 0043 { 0044 Q_ASSERT(m_activitiesService); 0045 const auto currentActivity = Plugin::retrieve<QString>(m_activitiesService, "CurrentActivity"); 0046 auto activities = Plugin::retrieve<ActivityInfoList>(m_activitiesService, "ListActivitiesWithInformation"); 0047 0048 const QString term = query.trimmed(); 0049 bool list = false; 0050 QString name; 0051 0052 if (term.startsWith(m_keyword, Qt::CaseInsensitive)) { 0053 name = term.right(term.size() - m_keyword.size()).trimmed(); 0054 } else if (term.startsWith(m_keywordi18n, Qt::CaseInsensitive)) { 0055 name = term.right(term.size() - m_keywordi18n.size()).trimmed(); 0056 } else { 0057 list = true; 0058 } 0059 0060 QList<RemoteMatch> matches; 0061 for (const ActivityInfo &activityInfo : std::as_const(activities)) { 0062 if (currentActivity != activityInfo.id) { 0063 auto info = Plugin::retrieve<ActivityInfo>(m_activitiesService, "ActivityInformation", Q_ARG(QString, activityInfo.id)); 0064 if (list || info.name.startsWith(name, Qt::CaseInsensitive)) { 0065 RemoteMatch m; 0066 m.id = activityInfo.id; 0067 m.text = i18n("Switch to \"%1\"", info.name); 0068 m.iconName = info.icon; 0069 m.type = Type::ExactMatch; 0070 m.relevance = 0.7 + ((activityInfo.state == Activities::State::Running || activityInfo.state == Activities::State::Starting) ? 0.1 : 0); 0071 matches.append(m); 0072 } 0073 } 0074 } 0075 0076 return matches; 0077 } 0078 0079 QVariantMap ActivityRunner::Config() 0080 { 0081 return { 0082 {"MinLetterCount", qMin(m_keyword.count(), m_keywordi18n.count())}, 0083 {"MatchRegex", '^' + m_keyword + '|' + m_keywordi18n}, 0084 }; 0085 } 0086 0087 RemoteActions ActivityRunner::Actions() 0088 { 0089 return {}; 0090 } 0091 0092 void ActivityRunner::Run(const QString &matchId, const QString & /*actionId*/) 0093 { 0094 QMetaObject::invokeMethod(m_activitiesService, "SetCurrentActivity", Q_ARG(QString, matchId)); 0095 } 0096 0097 #include "ActivityRunner.moc" 0098 0099 #include "moc_ActivityRunner.cpp"