Plasma
plasma-desktop-runner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "plasma-desktop-runner.h"
00020
00021 #include <QDBusConnection>
00022 #include <QDBusConnectionInterface>
00023 #include <QDBusInterface>
00024
00025 #include <KDebug>
00026 #include <KIcon>
00027 #include <KLocale>
00028 #include <KRun>
00029 #include <KToolInvocation>
00030
00031 #include <plasma/theme.h>
00032
00033 static const QString s_plasmaService = "org.kde.plasma-desktop";
00034
00035 PlasmaDesktopRunner::PlasmaDesktopRunner(QObject *parent, const QVariantList &args)
00036 : Plasma::AbstractRunner(parent, args),
00037 m_desktopConsoleKeyword(i18nc("Note this is a KRunner keyword", "desktop console")),
00038 m_enabled(false)
00039 {
00040 setObjectName("Plasma-Desktop");
00041 setIgnoredTypes(Plasma::RunnerContext::FileSystem |
00042 Plasma::RunnerContext::NetworkLocation |
00043 Plasma::RunnerContext::Help);
00044
00045 QObject::connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
00046 this, SLOT(checkAvailability(QString,QString,QString)));
00047 checkAvailability(QString(), QString(), QString());
00048 }
00049
00050 PlasmaDesktopRunner::~PlasmaDesktopRunner()
00051 {
00052 }
00053
00054 void PlasmaDesktopRunner::match(Plasma::RunnerContext &context)
00055 {
00056 if (m_enabled && context.query().startsWith(m_desktopConsoleKeyword, Qt::CaseInsensitive)) {
00057 Plasma::QueryMatch match(this);
00058 match.setId("plasma-desktop-console");
00059 match.setType(Plasma::QueryMatch::ExactMatch);
00060 match.setIcon(KIcon("plasma"));
00061 match.setText(i18n("Open Plasma desktop interactive console"));
00062 match.setRelevance(1.0);
00063 context.addMatch(context.query(), match);
00064 }
00065 }
00066
00067 void PlasmaDesktopRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00068 {
00069 Q_UNUSED(match)
00070
00071 if (m_enabled) {
00072 QDBusMessage message;
00073
00074 QString query = context.query();
00075 if (query.compare(m_desktopConsoleKeyword, Qt::CaseInsensitive) == 0) {
00076 message = QDBusMessage::createMethodCall(s_plasmaService, "/MainApplication",
00077 QString(), "showInteractiveConsole");
00078 } else {
00079 message = QDBusMessage::createMethodCall(s_plasmaService, "/MainApplication",
00080 QString(), "loadScriptInInteractiveConsole");
00081 query.replace(m_desktopConsoleKeyword, QString(), Qt::CaseInsensitive);
00082 QList<QVariant> args;
00083 args << query;
00084 message.setArguments(args);
00085 }
00086
00087 QDBusConnection::sessionBus().asyncCall(message);
00088 }
00089 }
00090
00091 void PlasmaDesktopRunner::checkAvailability(const QString &name, const QString &oldOwner, const QString &newOwner)
00092 {
00093 Q_UNUSED(oldOwner)
00094
00095 bool enabled = false;
00096 if (name.isEmpty()) {
00097 enabled = QDBusConnection::sessionBus().interface()->isServiceRegistered(s_plasmaService).value();
00098 } else if (name == s_plasmaService) {
00099 enabled = !newOwner.isNull();
00100 } else {
00101
00102 return;
00103 }
00104
00105 if (m_enabled != enabled) {
00106 m_enabled = enabled;
00107
00108 if (m_enabled) {
00109 addSyntax(Plasma::RunnerSyntax(m_desktopConsoleKeyword,
00110 i18n("Opens the Plasma desktop interactive console "
00111 "with a file path to a script on disk.")));
00112 addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "desktop console :q:"),
00113 i18n("Opens the Plasma desktop interactive console "
00114 "with a file path to a script on disk.")));
00115 } else {
00116 setSyntaxes(QList<Plasma::RunnerSyntax>());
00117 }
00118 }
00119 }
00120
00121
00122 #include "plasma-desktop-runner.moc"