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

« back to all changes in this revision

Viewing changes to plasma/generic/containmentactions/applauncher/launch.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 2009 by Chani Armitage <chani@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 as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (at your option) any later version.
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "launch.h"
 
21
 
 
22
#include <QGraphicsSceneMouseEvent>
 
23
#include <QGraphicsSceneWheelEvent>
 
24
 
 
25
#include <KDebug>
 
26
#include <KIcon>
 
27
#include <KMenu>
 
28
 
 
29
#include <Plasma/DataEngine>
 
30
#include <Plasma/Containment>
 
31
#include <Plasma/ServiceJob>
 
32
 
 
33
AppLauncher::AppLauncher(QObject *parent, const QVariantList &args)
 
34
    : Plasma::ContainmentActions(parent, args),
 
35
      m_action(new QAction(this))
 
36
{
 
37
    m_menu = new KMenu();
 
38
    connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(switchTo(QAction*)));
 
39
 
 
40
    m_action->setMenu(m_menu);
 
41
}
 
42
 
 
43
AppLauncher::~AppLauncher()
 
44
{
 
45
    delete m_menu;
 
46
}
 
47
 
 
48
void AppLauncher::init(const KConfigGroup &)
 
49
{
 
50
}
 
51
 
 
52
void AppLauncher::contextEvent(QEvent *event)
 
53
{
 
54
    makeMenu();
 
55
    m_menu->adjustSize();
 
56
    m_menu->exec(popupPosition(m_menu->size(), event));
 
57
}
 
58
 
 
59
QList<QAction*> AppLauncher::contextualActions()
 
60
{
 
61
    makeMenu();
 
62
 
 
63
    QList<QAction*> list;
 
64
    list << m_action;
 
65
    return list;
 
66
}
 
67
 
 
68
void AppLauncher::makeMenu()
 
69
{
 
70
    Plasma::DataEngine *apps = dataEngine("apps");
 
71
    if (!apps->isValid()) {
 
72
        return;
 
73
    }
 
74
 
 
75
    m_menu->clear();
 
76
 
 
77
    //add the whole kmenu
 
78
    Plasma::DataEngine::Data app = dataEngine("apps")->query("/");
 
79
    const QStringList sources = app.value("entries").toStringList();
 
80
    foreach (const QString &source, sources) {
 
81
        addApp(m_menu, source);
 
82
    }
 
83
}
 
84
 
 
85
bool AppLauncher::addApp(QMenu *menu, const QString &source)
 
86
{
 
87
    if (source == "---") {
 
88
        menu->addSeparator();
 
89
        return false;
 
90
    }
 
91
 
 
92
    Plasma::DataEngine::Data app = dataEngine("apps")->query(source);
 
93
 
 
94
    if (!app.value("display").toBool()) {
 
95
        kDebug() << "hidden entry" << source;
 
96
        return false;
 
97
    }
 
98
    QString name = app.value("name").toString();
 
99
    if (name.isEmpty()) {
 
100
        kDebug() << "failed source" << source;
 
101
        return false;
 
102
    }
 
103
 
 
104
    name.replace("&", "&&"); //escaping
 
105
    KIcon icon(app.value("iconName").toString());
 
106
 
 
107
    if (app.value("isApp").toBool()) {
 
108
        QAction *action = menu->addAction(icon, name);
 
109
        action->setData(source);
 
110
    } else { //ooh, it's actually a group!
 
111
        QMenu *subMenu = menu->addMenu(icon, name);
 
112
        bool hasEntries = false;
 
113
        foreach (const QString &source, app.value("entries").toStringList()) {
 
114
            hasEntries = addApp(subMenu, source) || hasEntries;
 
115
        }
 
116
 
 
117
        if (!hasEntries) {
 
118
            delete subMenu;
 
119
            return false;
 
120
        }
 
121
    }
 
122
    return true;
 
123
}
 
124
 
 
125
void AppLauncher::switchTo(QAction *action)
 
126
{
 
127
    QString source = action->data().toString();
 
128
    kDebug() << source;
 
129
    Plasma::Service *service = dataEngine("apps")->serviceForSource(source);
 
130
    if (service) {
 
131
        Plasma::ServiceJob *job = service->startOperationCall(service->operationDescription("launch"));
 
132
        connect(job, SIGNAL(finished(KJob*)), service, SLOT(deleteLater()));
 
133
    }
 
134
}
 
135
 
 
136
#include "launch.moc"