1 /*
2     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #include "plasma-desktop-runner.h"
8 
9 #include <KIO/CommandLauncherJob>
10 #include <QDBusConnection>
11 #include <QDBusConnectionInterface>
12 #include <QDBusServiceWatcher>
13 
14 #include <QDebug>
15 
16 #include <KLocalizedString>
17 
18 K_PLUGIN_CLASS_WITH_JSON(PlasmaDesktopRunner, "plasma-runner-plasma-desktop.json")
19 
20 static const QString s_plasmaService = QLatin1String("org.kde.plasmashell");
21 
PlasmaDesktopRunner(QObject * parent,const KPluginMetaData & metaData,const QVariantList & args)22 PlasmaDesktopRunner::PlasmaDesktopRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
23     : Plasma::AbstractRunner(parent, metaData, args)
24     , m_desktopConsoleKeyword(i18nc("Note this is a KRunner keyword", "desktop console"))
25     , m_kwinConsoleKeyword(i18nc("Note this is a KRunner keyword", "wm console"))
26 {
27     setObjectName(QLatin1String("Plasma-Desktop"));
28     addSyntax(Plasma::RunnerSyntax(m_desktopConsoleKeyword,
29                                    i18n("Opens the Plasma desktop interactive console "
30                                         "with a file path to a script on disk.")));
31     addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "desktop console :q:"),
32                                    i18n("Opens the Plasma desktop interactive console "
33                                         "with a file path to a script on disk.")));
34     addSyntax(Plasma::RunnerSyntax(m_kwinConsoleKeyword,
35                                    i18n("Opens the KWin interactive console "
36                                         "with a file path to a script on disk.")));
37     addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "wm console :q:"),
38                                    i18n("Opens the KWin interactive console "
39                                         "with a file path to a script on disk.")));
40 }
41 
~PlasmaDesktopRunner()42 PlasmaDesktopRunner::~PlasmaDesktopRunner()
43 {
44 }
45 
match(Plasma::RunnerContext & context)46 void PlasmaDesktopRunner::match(Plasma::RunnerContext &context)
47 {
48     if (context.query().startsWith(m_desktopConsoleKeyword, Qt::CaseInsensitive)) {
49         Plasma::QueryMatch match(this);
50         match.setId(QStringLiteral("plasma-desktop-console"));
51         match.setType(Plasma::QueryMatch::ExactMatch);
52         match.setIconName(QStringLiteral("plasma"));
53         match.setText(i18n("Open Plasma desktop interactive console"));
54         match.setRelevance(1.0);
55         context.addMatch(match);
56     }
57     if (context.query().startsWith(m_kwinConsoleKeyword, Qt::CaseInsensitive)) {
58         Plasma::QueryMatch match(this);
59         match.setId(QStringLiteral("plasma-desktop-console"));
60         match.setType(Plasma::QueryMatch::ExactMatch);
61         match.setIconName(QStringLiteral("kwin"));
62         match.setText(i18n("Open KWin interactive console"));
63         match.setRelevance(1.0);
64         context.addMatch(match);
65     }
66 }
67 
run(const Plasma::RunnerContext & context,const Plasma::QueryMatch & match)68 void PlasmaDesktopRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
69 {
70     Q_UNUSED(match)
71 
72     bool showPlasmaConsole = context.query().startsWith(m_desktopConsoleKeyword, Qt::CaseInsensitive);
73     QStringList args{showPlasmaConsole ? QStringLiteral("--plasma") : QStringLiteral("--kwin")};
74     auto job = new KIO::CommandLauncherJob(QStringLiteral("plasma-interactiveconsole"), args);
75     job->start();
76 }
77 
78 #include "plasma-desktop-runner.moc"
79