plasma-desktop-runner.cpp 6.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *   Copyright (C) 2009 Aaron Seigo <aseigo@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   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 Library 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 "plasma-desktop-runner.h"

#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <QDBusConnectionInterface>

#include <QDebug>

27
#include <KLocalizedString>
28

29
K_EXPORT_PLASMA_RUNNER_WITH_JSON(PlasmaDesktopRunner, "plasma-runner-plasma-desktop.json")
30

31
static const QString s_plasmaService = QLatin1String("org.kde.plasmashell");
32 33 34 35 36 37 38 39 40 41

PlasmaDesktopRunner::PlasmaDesktopRunner(QObject *parent, const QVariantList &args)
    : Plasma::AbstractRunner(parent, args),
      m_desktopConsoleKeyword(i18nc("Note this is a KRunner keyword", "desktop console")),
      m_kwinConsoleKeyword(i18nc("Note this is a KRunner keyword", "wm console")),
      m_enabled(false)
{
    setObjectName( QLatin1String("Plasma-Desktop" ));
    QDBusServiceWatcher *watcher = new QDBusServiceWatcher(s_plasmaService, QDBusConnection::sessionBus(),
                                                           QDBusServiceWatcher::WatchForOwnerChange, this);
David Edmundson's avatar
David Edmundson committed
42 43
    connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged,
            this, &PlasmaDesktopRunner::checkAvailability);
44 45 46 47 48 49 50 51 52 53 54
    checkAvailability(QString(), QString(), QString());
}

PlasmaDesktopRunner::~PlasmaDesktopRunner()
{
}

void PlasmaDesktopRunner::match(Plasma::RunnerContext &context)
{
    if (m_enabled && context.query().startsWith(m_desktopConsoleKeyword, Qt::CaseInsensitive)) {
        Plasma::QueryMatch match(this);
55
        match.setId(QStringLiteral("plasma-desktop-console"));
56
        match.setType(Plasma::QueryMatch::ExactMatch);
57
        match.setIconName(QStringLiteral("plasma"));
58 59
        match.setText(i18n("Open Plasma desktop interactive console"));
        match.setRelevance(1.0);
60
        context.addMatch(match);
61 62 63
    }
    if (m_enabled && context.query().startsWith(m_kwinConsoleKeyword, Qt::CaseInsensitive)) {
        Plasma::QueryMatch match(this);
64
        match.setId(QStringLiteral("plasma-desktop-console"));
65
        match.setType(Plasma::QueryMatch::ExactMatch);
66
        match.setIconName(QStringLiteral("kwin"));
67 68
        match.setText(i18n("Open KWin interactive console"));
        match.setRelevance(1.0);
69
        context.addMatch(match);
70 71 72 73 74 75 76 77 78 79 80 81
    }
}

void PlasmaDesktopRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(match)

    if (m_enabled) {
        QDBusMessage message;

        QString query = context.query();
        if (query.compare(m_desktopConsoleKeyword, Qt::CaseInsensitive) == 0) {
82 83
            message = QDBusMessage::createMethodCall(s_plasmaService, QStringLiteral("/PlasmaShell"),
                                                     QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("showInteractiveConsole"));
84
        } else if (query.startsWith(m_desktopConsoleKeyword)) {
85 86
            message = QDBusMessage::createMethodCall(s_plasmaService, QStringLiteral("/PlasmaShell"),
                                                     QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("loadScriptInInteractiveConsole"));
87 88 89 90 91 92
            query.replace(m_desktopConsoleKeyword, QString(), Qt::CaseInsensitive);
            QList<QVariant> args;
            args << query;
            message.setArguments(args);
        }
        if (query.compare(m_kwinConsoleKeyword, Qt::CaseInsensitive) == 0) {
93 94
            message = QDBusMessage::createMethodCall(s_plasmaService, QStringLiteral("/PlasmaShell"),
                                                     QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("showInteractiveKWinConsole"));
95
        } else if (query.startsWith(m_kwinConsoleKeyword)) {
96 97
            message = QDBusMessage::createMethodCall(s_plasmaService, QStringLiteral("/PlasmaShell"),
                                                     QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("loadKWinScriptInInteractiveConsole"));
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            query.replace(m_kwinConsoleKeyword, QString(), Qt::CaseInsensitive);
            QList<QVariant> args;
            args << query;
            message.setArguments(args);
        }

        QDBusConnection::sessionBus().asyncCall(message);
    }
}

void PlasmaDesktopRunner::checkAvailability(const QString &name, const QString &oldOwner, const QString &newOwner)
{
    Q_UNUSED(oldOwner)

    bool enabled = false;
    if (name.isEmpty()) {
        enabled = QDBusConnection::sessionBus().interface()->isServiceRegistered(s_plasmaService).value();
    } else {
        enabled = !newOwner.isEmpty();
    }

    if (m_enabled != enabled) {
        m_enabled = enabled;

        if (m_enabled) {
            addSyntax(Plasma::RunnerSyntax(m_desktopConsoleKeyword,
                                           i18n("Opens the Plasma desktop interactive console "
                                                "with a file path to a script on disk.")));
            addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "desktop console :q:"),
                                           i18n("Opens the Plasma desktop interactive console "
                                                "with a file path to a script on disk.")));
            addSyntax(Plasma::RunnerSyntax(m_kwinConsoleKeyword,
                                           i18n("Opens the KWin interactive console "
                                                "with a file path to a script on disk.")));
            addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "wm console :q:"),
                                           i18n("Opens the KWin interactive console "
                                                "with a file path to a script on disk.")));
        } else {
            setSyntaxes(QList<Plasma::RunnerSyntax>());
        }
    }
}

141
#include "plasma-desktop-runner.moc"
142