~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/runners/plasma-desktop/plasma-desktop-runner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2009 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License version 2 as
 
6
 *   published by the Free Software Foundation
 
7
 *
 
8
 *   This program is distributed in the hope that it will be useful,
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *   GNU General Public License for more details
 
12
 *
 
13
 *   You should have received a copy of the GNU Library General Public
 
14
 *   License along with this program; if not, write to the
 
15
 *   Free Software Foundation, Inc.,
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "plasma-desktop-runner.h"
 
20
 
 
21
#include <QDBusConnection>
 
22
#include <QDBusServiceWatcher>
 
23
#include <QDBusConnectionInterface>
 
24
 
 
25
#include <KDebug>
 
26
#include <KIcon>
 
27
#include <KLocale>
 
28
#include <KRun>
 
29
 
 
30
#include <plasma/theme.h>
 
31
 
 
32
static const QString s_plasmaService = "org.kde.plasma-desktop";
 
33
 
 
34
PlasmaDesktopRunner::PlasmaDesktopRunner(QObject *parent, const QVariantList &args)
 
35
    : Plasma::AbstractRunner(parent, args),
 
36
      m_desktopConsoleKeyword(i18nc("Note this is a KRunner keyword", "desktop console")),
 
37
      m_enabled(false)
 
38
{
 
39
    setObjectName( QLatin1String("Plasma-Desktop" ));
 
40
    setIgnoredTypes(Plasma::RunnerContext::FileSystem |
 
41
                    Plasma::RunnerContext::NetworkLocation |
 
42
                    Plasma::RunnerContext::Help);
 
43
    QDBusServiceWatcher *watcher = new QDBusServiceWatcher(s_plasmaService, QDBusConnection::sessionBus(),
 
44
                                                           QDBusServiceWatcher::WatchForOwnerChange, this);
 
45
    connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
 
46
            this, SLOT(checkAvailability(QString,QString,QString)));
 
47
    checkAvailability(QString(), QString(), QString());
 
48
}
 
49
 
 
50
PlasmaDesktopRunner::~PlasmaDesktopRunner()
 
51
{
 
52
}
 
53
 
 
54
void PlasmaDesktopRunner::match(Plasma::RunnerContext &context)
 
55
{
 
56
    if (m_enabled && context.query().startsWith(m_desktopConsoleKeyword, Qt::CaseInsensitive)) {
 
57
        Plasma::QueryMatch match(this);
 
58
        match.setId("plasma-desktop-console");
 
59
        match.setType(Plasma::QueryMatch::ExactMatch);
 
60
        match.setIcon(KIcon("plasma"));
 
61
        match.setText(i18n("Open Plasma desktop interactive console"));
 
62
        match.setRelevance(1.0);
 
63
        context.addMatch(context.query(), match);
 
64
    }
 
65
}
 
66
 
 
67
void PlasmaDesktopRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
 
68
{
 
69
    Q_UNUSED(match)
 
70
 
 
71
    if (m_enabled) {
 
72
        QDBusMessage message;
 
73
 
 
74
        QString query = context.query();
 
75
        if (query.compare(m_desktopConsoleKeyword, Qt::CaseInsensitive) == 0) {
 
76
            message = QDBusMessage::createMethodCall(s_plasmaService, "/MainApplication",
 
77
                                                     QString(), "showInteractiveConsole");
 
78
        } else {
 
79
            message = QDBusMessage::createMethodCall(s_plasmaService, "/MainApplication",
 
80
                                                     QString(), "loadScriptInInteractiveConsole");
 
81
            query.replace(m_desktopConsoleKeyword, QString(), Qt::CaseInsensitive);
 
82
            QList<QVariant> args;
 
83
            args << query;
 
84
            message.setArguments(args);
 
85
        }
 
86
 
 
87
        QDBusConnection::sessionBus().asyncCall(message);
 
88
    }
 
89
}
 
90
 
 
91
void PlasmaDesktopRunner::checkAvailability(const QString &name, const QString &oldOwner, const QString &newOwner)
 
92
{
 
93
    Q_UNUSED(oldOwner)
 
94
 
 
95
    bool enabled = false;
 
96
    if (name.isEmpty()) {
 
97
        enabled = QDBusConnection::sessionBus().interface()->isServiceRegistered(s_plasmaService).value();
 
98
    } else {
 
99
        enabled = !newOwner.isEmpty();
 
100
    }
 
101
 
 
102
    if (m_enabled != enabled) {
 
103
        m_enabled = enabled;
 
104
 
 
105
        if (m_enabled) {
 
106
            addSyntax(Plasma::RunnerSyntax(m_desktopConsoleKeyword,
 
107
                                           i18n("Opens the Plasma desktop interactive console "
 
108
                                                "with a file path to a script on disk.")));
 
109
            addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "desktop console :q:"),
 
110
                                           i18n("Opens the Plasma desktop interactive console "
 
111
                                                "with a file path to a script on disk.")));
 
112
        } else {
 
113
            setSyntaxes(QList<Plasma::RunnerSyntax>());
 
114
        }
 
115
    }
 
116
}
 
117
 
 
118
 
 
119
#include "plasma-desktop-runner.moc"