~ubuntu-branches/ubuntu/natty/kdebase-runtime/natty-proposed

« back to all changes in this revision

Viewing changes to plasma/scriptengines/javascript/simplebindings/appletinterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-24 11:07:10 UTC
  • mto: (0.8.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: james.westby@ubuntu.com-20101124110710-6dbsyw0yh21qvn82
Tags: upstream-4.5.80
ImportĀ upstreamĀ versionĀ 4.5.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Copyright 2008 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 "appletinterface.h"
21
 
 
22
 
#include <QAction>
23
 
#include <QFile>
24
 
#include <QScriptEngine>
25
 
#include <QSignalMapper>
26
 
#include <QTimer>
27
 
 
28
 
#include <KIcon>
29
 
#include <KService>
30
 
#include <KServiceTypeTrader>
31
 
 
32
 
#include <Plasma/Plasma>
33
 
#include <Plasma/Applet>
34
 
#include <Plasma/Context>
35
 
#include <Plasma/Package>
36
 
 
37
 
AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
38
 
    : QObject(parent),
39
 
      m_appletScriptEngine(parent),
40
 
      m_actionSignals(0)
41
 
{
42
 
    connect(this, SIGNAL(releaseVisualFocus()), applet(), SIGNAL(releaseVisualFocus()));
43
 
    connect(this, SIGNAL(configNeedsSaving()), applet(), SIGNAL(configNeedsSaving()));
44
 
}
45
 
 
46
 
AppletInterface::~AppletInterface()
47
 
{
48
 
}
49
 
 
50
 
Plasma::DataEngine* AppletInterface::dataEngine(const QString &name)
51
 
{
52
 
    return applet()->dataEngine(name);
53
 
}
54
 
 
55
 
AppletInterface::FormFactor AppletInterface::formFactor() const
56
 
{
57
 
    return static_cast<FormFactor>(applet()->formFactor());
58
 
}
59
 
 
60
 
AppletInterface::Location AppletInterface::location() const
61
 
{
62
 
    return static_cast<Location>(applet()->location());
63
 
}
64
 
 
65
 
QString AppletInterface::currentActivity() const
66
 
{
67
 
    return applet()->context()->currentActivity();
68
 
}
69
 
 
70
 
AppletInterface::AspectRatioMode AppletInterface::aspectRatioMode() const
71
 
{
72
 
    return static_cast<AspectRatioMode>(applet()->aspectRatioMode());
73
 
}
74
 
 
75
 
void AppletInterface::setAspectRatioMode(AppletInterface::AspectRatioMode mode)
76
 
{
77
 
    applet()->setAspectRatioMode(static_cast<Plasma::AspectRatioMode>(mode));
78
 
}
79
 
 
80
 
bool AppletInterface::shouldConserveResources() const
81
 
{
82
 
    return applet()->shouldConserveResources();
83
 
}
84
 
 
85
 
void AppletInterface::setFailedToLaunch(bool failed, const QString &reason)
86
 
{
87
 
    m_appletScriptEngine->setFailedToLaunch(failed, reason);
88
 
}
89
 
 
90
 
bool AppletInterface::isBusy() const
91
 
{
92
 
    return applet()->isBusy();
93
 
}
94
 
 
95
 
void AppletInterface::setBusy(bool busy)
96
 
{
97
 
    applet()->setBusy(busy);
98
 
}
99
 
 
100
 
AppletInterface::BackgroundHints AppletInterface::backgroundHints() const
101
 
{
102
 
    return static_cast<BackgroundHints>(static_cast<int>(applet()->backgroundHints()));
103
 
}
104
 
 
105
 
void AppletInterface::setBackgroundHints(BackgroundHints hint)
106
 
{
107
 
    applet()->setBackgroundHints(Plasma::Applet::BackgroundHints(hint));
108
 
}
109
 
 
110
 
void AppletInterface::setConfigurationRequired(bool needsConfiguring, const QString &reason)
111
 
{
112
 
    m_appletScriptEngine->setConfigurationRequired(needsConfiguring, reason);
113
 
}
114
 
 
115
 
void AppletInterface::update(const QRectF &rect)
116
 
{
117
 
    applet()->update(rect);
118
 
}
119
 
 
120
 
QString AppletInterface::activeConfig() const
121
 
{
122
 
    return m_currentConfig.isEmpty() ? "main" : m_currentConfig;
123
 
}
124
 
 
125
 
void AppletInterface::setActiveConfig(const QString &name)
126
 
{
127
 
    if (name == "main") {
128
 
        m_currentConfig.clear();
129
 
        return;
130
 
    }
131
 
 
132
 
    Plasma::ConfigLoader *loader = m_configs.value(name, 0);
133
 
 
134
 
    if (!loader) {
135
 
        QString path = applet()->package()->filePath("config", name + ".xml");
136
 
        if (path.isEmpty()) {
137
 
            return;
138
 
        }
139
 
 
140
 
        QFile f(path);
141
 
        KConfigGroup cg = applet()->config();
142
 
        loader = new Plasma::ConfigLoader(&cg, &f, this);
143
 
        m_configs.insert(name, loader);
144
 
    }
145
 
 
146
 
    m_currentConfig = name;
147
 
}
148
 
 
149
 
void AppletInterface::writeConfig(const QString &entry, const QVariant &value)
150
 
{
151
 
    Plasma::ConfigLoader *config = 0;
152
 
    if (m_currentConfig.isEmpty()) {
153
 
        config = applet()->configScheme();
154
 
    } else {
155
 
        config = m_configs.value(m_currentConfig, 0);
156
 
    }
157
 
 
158
 
    if (config) {
159
 
        KConfigSkeletonItem *item = config->findItemByName(entry);
160
 
        if (item) {
161
 
            item->setProperty(value);
162
 
            config->blockSignals(true);
163
 
            config->writeConfig();
164
 
            config->blockSignals(false);
165
 
            m_appletScriptEngine->configNeedsSaving();
166
 
        }
167
 
    }
168
 
}
169
 
 
170
 
QScriptValue AppletInterface::readConfig(const QString &entry) const
171
 
{
172
 
    Plasma::ConfigLoader *config = 0;
173
 
    QVariant result;
174
 
 
175
 
    if (m_currentConfig.isEmpty()) {
176
 
        config = applet()->configScheme();
177
 
    } else {
178
 
        config = m_configs.value(m_currentConfig, 0);
179
 
    }
180
 
 
181
 
    if (config) {
182
 
        result = config->property(entry);
183
 
    }
184
 
 
185
 
    return m_appletScriptEngine->variantToScriptValue(result);
186
 
}
187
 
 
188
 
QString AppletInterface::file(const QString &fileType)
189
 
{
190
 
    return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData());
191
 
}
192
 
 
193
 
QString AppletInterface::file(const QString &fileType, const QString &filePath)
194
 
{
195
 
    return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData(), filePath);
196
 
}
197
 
 
198
 
const Plasma::Package *AppletInterface::package() const
199
 
{
200
 
    return m_appletScriptEngine->package();
201
 
}
202
 
 
203
 
QList<QAction*> AppletInterface::contextualActions() const
204
 
{
205
 
    QList<QAction*> actions;
206
 
    Plasma::Applet *a = applet();
207
 
 
208
 
    foreach (const QString &name, m_actions) {
209
 
        QAction *action = a->action(name);
210
 
 
211
 
        if (action) {
212
 
            actions << action;
213
 
        }
214
 
    }
215
 
 
216
 
    return actions;
217
 
}
218
 
 
219
 
QSizeF AppletInterface::size() const
220
 
{
221
 
    return applet()->size();
222
 
}
223
 
 
224
 
QRectF AppletInterface::rect() const
225
 
{
226
 
    return applet()->contentsRect();
227
 
}
228
 
 
229
 
void AppletInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut)
230
 
{
231
 
    Plasma::Applet *a = applet();
232
 
    QAction *action = a->action(name);
233
 
 
234
 
    if (action) {
235
 
        action->setText(text);
236
 
    } else {
237
 
        action = new QAction(text, this);
238
 
        a->addAction(name, action);
239
 
 
240
 
        Q_ASSERT(!m_actions.contains(name));
241
 
        m_actions.insert(name);
242
 
 
243
 
        if (!m_actionSignals) {
244
 
            m_actionSignals = new QSignalMapper(this);
245
 
            connect(m_actionSignals, SIGNAL(mapped(QString)),
246
 
                    m_appletScriptEngine, SLOT(executeAction(QString)));
247
 
        }
248
 
 
249
 
        connect(action, SIGNAL(triggered()), m_actionSignals, SLOT(map()));
250
 
        m_actionSignals->setMapping(action, name);
251
 
    }
252
 
 
253
 
    if (!icon.isEmpty()) {
254
 
        action->setIcon(KIcon(icon));
255
 
    }
256
 
 
257
 
    if (!shortcut.isEmpty()) {
258
 
        action->setShortcut(shortcut);
259
 
    }
260
 
 
261
 
    action->setObjectName(name);
262
 
}
263
 
 
264
 
void AppletInterface::removeAction(const QString &name)
265
 
{
266
 
    Plasma::Applet *a = applet();
267
 
    QAction *action = a->action(name);
268
 
 
269
 
    if (action) {
270
 
        if (m_actionSignals) {
271
 
            m_actionSignals->removeMappings(action);
272
 
        }
273
 
 
274
 
        delete action;
275
 
    }
276
 
 
277
 
    m_actions.remove(name);
278
 
}
279
 
 
280
 
void AppletInterface::resize(qreal w, qreal h)
281
 
{
282
 
    applet()->resize(w,h);
283
 
}
284
 
 
285
 
void AppletInterface::setMinimumSize(qreal w, qreal h)
286
 
{
287
 
    applet()->setMinimumSize(w,h);
288
 
}
289
 
 
290
 
void AppletInterface::setPreferredSize(qreal w, qreal h)
291
 
{
292
 
    applet()->setPreferredSize(w,h);
293
 
}
294
 
 
295
 
void AppletInterface::dataUpdated(QString source, Plasma::DataEngine::Data data)
296
 
{
297
 
    m_appletScriptEngine->dataUpdated(source, data);
298
 
}
299
 
 
300
 
QGraphicsLayout *AppletInterface::layout() const
301
 
{
302
 
    return applet()->layout();
303
 
}
304
 
 
305
 
void AppletInterface::setLayout(QGraphicsLayout *layout)
306
 
{
307
 
    applet()->setLayout(layout);
308
 
}
309
 
 
310
 
bool AppletInterface::immutable() const
311
 
{
312
 
    return applet()->immutability() != Plasma::Mutable;
313
 
}
314
 
 
315
 
bool AppletInterface::userConfiguring() const
316
 
{
317
 
    return applet()->isUserConfiguring();
318
 
}
319
 
 
320
 
int AppletInterface::apiVersion() const
321
 
{
322
 
    const QString constraint("[X-Plasma-API] == 'javascript' and 'Applet' in [X-Plasma-ComponentTypes]");
323
 
    KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
324
 
    if (offers.isEmpty()) {
325
 
        return -1;
326
 
    }
327
 
 
328
 
    return offers.first()->property("X-KDE-PluginInfo-Version", QVariant::Int).toInt();
329
 
}
330
 
 
331
 
bool AppletInterface::include(const QString &script)
332
 
{
333
 
    const QString path = package()->filePath("scripts", script);
334
 
    if (path.isEmpty()) {
335
 
        return false;
336
 
    }
337
 
 
338
 
    return m_appletScriptEngine->include(path);
339
 
}
340
 
 
341
 
bool AppletInterface::hasExtension(const QString &extension) const
342
 
{
343
 
    return m_appletScriptEngine->loadedExtensions().contains(extension.toLower());
344
 
}
345
 
 
346
 
void AppletInterface::debug(const QString &msg)
347
 
{
348
 
    kDebug() << msg;
349
 
}
350
 
 
351
 
QObject *AppletInterface::findChild(const QString &name) const
352
 
{
353
 
    if (name.isEmpty()) {
354
 
        return 0;
355
 
    }
356
 
 
357
 
    foreach (QGraphicsItem *item, applet()->childItems()) {
358
 
        QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget *>(item);
359
 
        if (widget && widget->objectName() == name) {
360
 
            return widget;
361
 
        }
362
 
    }
363
 
 
364
 
    return 0;
365
 
}
366
 
 
367
 
Plasma::Extender *AppletInterface::extender() const
368
 
{
369
 
    return m_appletScriptEngine->extender();
370
 
}
371
 
 
372
 
void AppletInterface::addEventListener(const QString &event, const QScriptValue &func)
373
 
{
374
 
    m_appletScriptEngine->addEventListener(event, func);
375
 
}
376
 
 
377
 
void AppletInterface::removeEventListener(const QString &event, const QScriptValue &func)
378
 
{
379
 
    m_appletScriptEngine->removeEventListener(event, func);
380
 
}
381
 
 
382
 
void AppletInterface::gc()
383
 
{
384
 
    QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage()));
385
 
}
386
 
 
387
 
 
388
 
PopupAppletInterface::PopupAppletInterface(SimpleJavaScriptApplet *parent)
389
 
    : AppletInterface(parent)
390
 
{
391
 
}
392
 
 
393
 
void PopupAppletInterface::setPopupIcon(const QIcon &icon)
394
 
{
395
 
    popupApplet()->setPopupIcon(icon);
396
 
}
397
 
 
398
 
QIcon PopupAppletInterface::popupIcon()
399
 
{
400
 
    return popupApplet()->popupIcon();
401
 
}
402
 
 
403
 
void PopupAppletInterface::setPopupIconByName(const QString &name)
404
 
{
405
 
    return popupApplet()->setPopupIcon(name);
406
 
}
407
 
 
408
 
void PopupAppletInterface::setPassivePopup(bool passive)
409
 
{
410
 
    popupApplet()->setPassivePopup(passive);
411
 
}
412
 
 
413
 
bool PopupAppletInterface::isPassivePopup() const
414
 
{
415
 
    return popupApplet()->isPassivePopup();
416
 
}
417
 
 
418
 
void PopupAppletInterface::togglePopup()
419
 
{
420
 
    popupApplet()->togglePopup();
421
 
}
422
 
 
423
 
void PopupAppletInterface::hidePopup()
424
 
{
425
 
    popupApplet()->hidePopup();
426
 
}
427
 
 
428
 
void PopupAppletInterface::showPopup()
429
 
{
430
 
    popupApplet()->showPopup();
431
 
}
432
 
 
433
 
void PopupAppletInterface::setPopupWidget(QGraphicsWidget *widget)
434
 
{
435
 
    popupApplet()->setGraphicsWidget(widget);
436
 
}
437
 
 
438
 
QGraphicsWidget *PopupAppletInterface::popupWidget()
439
 
{
440
 
    return popupApplet()->graphicsWidget();
441
 
}
442
 
 
443
 
#include "appletinterface.moc"