2
* Copyright 2008 Chani Armitage <chani@kde.org>
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.
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
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.
20
#include "appletinterface.h"
24
#include <QScriptEngine>
25
#include <QSignalMapper>
30
#include <KServiceTypeTrader>
32
#include <Plasma/Plasma>
33
#include <Plasma/Applet>
34
#include <Plasma/Context>
35
#include <Plasma/Package>
37
AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
39
m_appletScriptEngine(parent),
42
connect(this, SIGNAL(releaseVisualFocus()), applet(), SIGNAL(releaseVisualFocus()));
43
connect(this, SIGNAL(configNeedsSaving()), applet(), SIGNAL(configNeedsSaving()));
46
AppletInterface::~AppletInterface()
50
Plasma::DataEngine* AppletInterface::dataEngine(const QString &name)
52
return applet()->dataEngine(name);
55
AppletInterface::FormFactor AppletInterface::formFactor() const
57
return static_cast<FormFactor>(applet()->formFactor());
60
AppletInterface::Location AppletInterface::location() const
62
return static_cast<Location>(applet()->location());
65
QString AppletInterface::currentActivity() const
67
return applet()->context()->currentActivity();
70
AppletInterface::AspectRatioMode AppletInterface::aspectRatioMode() const
72
return static_cast<AspectRatioMode>(applet()->aspectRatioMode());
75
void AppletInterface::setAspectRatioMode(AppletInterface::AspectRatioMode mode)
77
applet()->setAspectRatioMode(static_cast<Plasma::AspectRatioMode>(mode));
80
bool AppletInterface::shouldConserveResources() const
82
return applet()->shouldConserveResources();
85
void AppletInterface::setFailedToLaunch(bool failed, const QString &reason)
87
m_appletScriptEngine->setFailedToLaunch(failed, reason);
90
bool AppletInterface::isBusy() const
92
return applet()->isBusy();
95
void AppletInterface::setBusy(bool busy)
97
applet()->setBusy(busy);
100
AppletInterface::BackgroundHints AppletInterface::backgroundHints() const
102
return static_cast<BackgroundHints>(static_cast<int>(applet()->backgroundHints()));
105
void AppletInterface::setBackgroundHints(BackgroundHints hint)
107
applet()->setBackgroundHints(Plasma::Applet::BackgroundHints(hint));
110
void AppletInterface::setConfigurationRequired(bool needsConfiguring, const QString &reason)
112
m_appletScriptEngine->setConfigurationRequired(needsConfiguring, reason);
115
void AppletInterface::update(const QRectF &rect)
117
applet()->update(rect);
120
QString AppletInterface::activeConfig() const
122
return m_currentConfig.isEmpty() ? "main" : m_currentConfig;
125
void AppletInterface::setActiveConfig(const QString &name)
127
if (name == "main") {
128
m_currentConfig.clear();
132
Plasma::ConfigLoader *loader = m_configs.value(name, 0);
135
QString path = applet()->package()->filePath("config", name + ".xml");
136
if (path.isEmpty()) {
141
KConfigGroup cg = applet()->config();
142
loader = new Plasma::ConfigLoader(&cg, &f, this);
143
m_configs.insert(name, loader);
146
m_currentConfig = name;
149
void AppletInterface::writeConfig(const QString &entry, const QVariant &value)
151
Plasma::ConfigLoader *config = 0;
152
if (m_currentConfig.isEmpty()) {
153
config = applet()->configScheme();
155
config = m_configs.value(m_currentConfig, 0);
159
KConfigSkeletonItem *item = config->findItemByName(entry);
161
item->setProperty(value);
162
config->blockSignals(true);
163
config->writeConfig();
164
config->blockSignals(false);
165
m_appletScriptEngine->configNeedsSaving();
170
QScriptValue AppletInterface::readConfig(const QString &entry) const
172
Plasma::ConfigLoader *config = 0;
175
if (m_currentConfig.isEmpty()) {
176
config = applet()->configScheme();
178
config = m_configs.value(m_currentConfig, 0);
182
result = config->property(entry);
185
return m_appletScriptEngine->variantToScriptValue(result);
188
QString AppletInterface::file(const QString &fileType)
190
return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData());
193
QString AppletInterface::file(const QString &fileType, const QString &filePath)
195
return m_appletScriptEngine->package()->filePath(fileType.toLocal8Bit().constData(), filePath);
198
const Plasma::Package *AppletInterface::package() const
200
return m_appletScriptEngine->package();
203
QList<QAction*> AppletInterface::contextualActions() const
205
QList<QAction*> actions;
206
Plasma::Applet *a = applet();
208
foreach (const QString &name, m_actions) {
209
QAction *action = a->action(name);
219
QSizeF AppletInterface::size() const
221
return applet()->size();
224
QRectF AppletInterface::rect() const
226
return applet()->contentsRect();
229
void AppletInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut)
231
Plasma::Applet *a = applet();
232
QAction *action = a->action(name);
235
action->setText(text);
237
action = new QAction(text, this);
238
a->addAction(name, action);
240
Q_ASSERT(!m_actions.contains(name));
241
m_actions.insert(name);
243
if (!m_actionSignals) {
244
m_actionSignals = new QSignalMapper(this);
245
connect(m_actionSignals, SIGNAL(mapped(QString)),
246
m_appletScriptEngine, SLOT(executeAction(QString)));
249
connect(action, SIGNAL(triggered()), m_actionSignals, SLOT(map()));
250
m_actionSignals->setMapping(action, name);
253
if (!icon.isEmpty()) {
254
action->setIcon(KIcon(icon));
257
if (!shortcut.isEmpty()) {
258
action->setShortcut(shortcut);
261
action->setObjectName(name);
264
void AppletInterface::removeAction(const QString &name)
266
Plasma::Applet *a = applet();
267
QAction *action = a->action(name);
270
if (m_actionSignals) {
271
m_actionSignals->removeMappings(action);
277
m_actions.remove(name);
280
void AppletInterface::resize(qreal w, qreal h)
282
applet()->resize(w,h);
285
void AppletInterface::setMinimumSize(qreal w, qreal h)
287
applet()->setMinimumSize(w,h);
290
void AppletInterface::setPreferredSize(qreal w, qreal h)
292
applet()->setPreferredSize(w,h);
295
void AppletInterface::dataUpdated(QString source, Plasma::DataEngine::Data data)
297
m_appletScriptEngine->dataUpdated(source, data);
300
QGraphicsLayout *AppletInterface::layout() const
302
return applet()->layout();
305
void AppletInterface::setLayout(QGraphicsLayout *layout)
307
applet()->setLayout(layout);
310
bool AppletInterface::immutable() const
312
return applet()->immutability() != Plasma::Mutable;
315
bool AppletInterface::userConfiguring() const
317
return applet()->isUserConfiguring();
320
int AppletInterface::apiVersion() const
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()) {
328
return offers.first()->property("X-KDE-PluginInfo-Version", QVariant::Int).toInt();
331
bool AppletInterface::include(const QString &script)
333
const QString path = package()->filePath("scripts", script);
334
if (path.isEmpty()) {
338
return m_appletScriptEngine->include(path);
341
bool AppletInterface::hasExtension(const QString &extension) const
343
return m_appletScriptEngine->loadedExtensions().contains(extension.toLower());
346
void AppletInterface::debug(const QString &msg)
351
QObject *AppletInterface::findChild(const QString &name) const
353
if (name.isEmpty()) {
357
foreach (QGraphicsItem *item, applet()->childItems()) {
358
QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget *>(item);
359
if (widget && widget->objectName() == name) {
367
Plasma::Extender *AppletInterface::extender() const
369
return m_appletScriptEngine->extender();
372
void AppletInterface::addEventListener(const QString &event, const QScriptValue &func)
374
m_appletScriptEngine->addEventListener(event, func);
377
void AppletInterface::removeEventListener(const QString &event, const QScriptValue &func)
379
m_appletScriptEngine->removeEventListener(event, func);
382
void AppletInterface::gc()
384
QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage()));
388
PopupAppletInterface::PopupAppletInterface(SimpleJavaScriptApplet *parent)
389
: AppletInterface(parent)
393
void PopupAppletInterface::setPopupIcon(const QIcon &icon)
395
popupApplet()->setPopupIcon(icon);
398
QIcon PopupAppletInterface::popupIcon()
400
return popupApplet()->popupIcon();
403
void PopupAppletInterface::setPopupIconByName(const QString &name)
405
return popupApplet()->setPopupIcon(name);
408
void PopupAppletInterface::setPassivePopup(bool passive)
410
popupApplet()->setPassivePopup(passive);
413
bool PopupAppletInterface::isPassivePopup() const
415
return popupApplet()->isPassivePopup();
418
void PopupAppletInterface::togglePopup()
420
popupApplet()->togglePopup();
423
void PopupAppletInterface::hidePopup()
425
popupApplet()->hidePopup();
428
void PopupAppletInterface::showPopup()
430
popupApplet()->showPopup();
433
void PopupAppletInterface::setPopupWidget(QGraphicsWidget *widget)
435
popupApplet()->setGraphicsWidget(widget);
438
QGraphicsWidget *PopupAppletInterface::popupWidget()
440
return popupApplet()->graphicsWidget();
443
#include "appletinterface.moc"