not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / scriptengines / javascript / appletinterface.cpp
blob499872ab72963caed6cbf00f830483ccc0efe6d2
1 /*
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"
22 #include <QAction>
23 #include <QFile>
24 #include <QSignalMapper>
26 #include <KDE/KIcon>
28 #include <Plasma/Plasma>
29 #include <Plasma/Applet>
30 #include <Plasma/Context>
31 #include <Plasma/DataEngine>
32 #include <Plasma/Package>
34 #include "simplejavascriptapplet.h"
36 AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
37 : QObject(parent),
38 m_appletScriptEngine(parent),
39 m_actionSignals(0)
41 connect(this, SIGNAL(releaseVisualFocus()), applet(), SIGNAL(releaseVisualFocus()));
42 connect(this, SIGNAL(configNeedsSaving()), applet(), SIGNAL(configNeedsSaving()));
45 AppletInterface::~AppletInterface()
49 Plasma::DataEngine* AppletInterface::dataEngine(const QString &name)
51 return applet()->dataEngine(name);
54 AppletInterface::FormFactor AppletInterface::formFactor()
56 return static_cast<FormFactor>(applet()->formFactor());
59 AppletInterface::Location AppletInterface::location()
61 return static_cast<Location>(applet()->location());
64 QString AppletInterface::currentActivity()
66 return applet()->context()->currentActivity();
69 AppletInterface::AspectRatioMode AppletInterface::aspectRatioMode()
71 return static_cast<AspectRatioMode>(applet()->aspectRatioMode());
74 void AppletInterface::setAspectRatioMode(AppletInterface::AspectRatioMode mode)
76 applet()->setAspectRatioMode(static_cast<Plasma::AspectRatioMode>(mode));
79 bool AppletInterface::shouldConserveResources()
81 return applet()->shouldConserveResources();
84 void AppletInterface::setFailedToLaunch(bool failed, const QString &reason)
86 m_appletScriptEngine->setFailedToLaunch(failed, reason);
89 bool AppletInterface::isBusy()
91 return applet()->isBusy();
94 void AppletInterface::setBusy(bool busy)
96 applet()->setBusy(busy);
99 void AppletInterface::setConfigurationRequired(bool needsConfiguring, const QString &reason)
101 m_appletScriptEngine->setConfigurationRequired(needsConfiguring, reason);
104 void AppletInterface::update()
106 applet()->update();
109 QString AppletInterface::activeConfig() const
111 return m_currentConfig.isEmpty() ? "main" : m_currentConfig;
114 void AppletInterface::setActiveConfig(const QString &name)
116 if (name == "main") {
117 m_currentConfig = QString();
118 return;
121 Plasma::ConfigLoader *loader = m_configs.value(name, 0);
123 if (!loader) {
124 QString path = applet()->package()->filePath("config", name + ".xml");
125 if (path.isEmpty()) {
126 return;
129 QFile f(path);
130 KConfigGroup cg = applet()->config();
131 loader = new Plasma::ConfigLoader(&cg, &f, this);
132 m_configs.insert(name, loader);
135 m_currentConfig = name;
138 void AppletInterface::writeConfig(const QString &entry, const QVariant &value)
140 Plasma::ConfigLoader *config = 0;
141 if (m_currentConfig.isEmpty()) {
142 config = applet()->configScheme();
143 } else {
144 config = m_configs.value(m_currentConfig, 0);
147 if (config) {
148 KConfigSkeletonItem *item = config->findItemByName(entry);
149 if (item) {
150 item->setProperty(value);
151 config->writeConfig();
152 m_appletScriptEngine->configNeedsSaving();
157 QVariant AppletInterface::readConfig(const QString &entry) const
159 Plasma::ConfigLoader *config = 0;
160 if (m_currentConfig.isEmpty()) {
161 config = applet()->configScheme();
162 } else {
163 config = m_configs.value(m_currentConfig, 0);
166 if (config) {
167 return config->property(entry);
170 return QVariant();
173 const Plasma::Package *AppletInterface::package() const
175 return m_appletScriptEngine->package();
178 QList<QAction*> AppletInterface::contextualActions() const
180 QList<QAction*> actions;
181 Plasma::Applet *a = applet();
183 foreach (const QString name, m_actions) {
184 QAction *action = a->action(name);
186 if (action) {
187 actions << action;
191 return actions;
194 QSizeF AppletInterface::size() const
196 return applet()->size();
199 void AppletInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut)
201 Plasma::Applet *a = applet();
202 QAction *action = a->action(name);
204 if (action) {
205 action->setText(text);
206 } else {
207 action = new QAction(text, this);
208 a->addAction(name, action);
210 Q_ASSERT(!m_actions.contains(name));
211 m_actions.insert(name);
213 if (!m_actionSignals) {
214 m_actionSignals = new QSignalMapper(this);
215 connect(m_actionSignals, SIGNAL(mapped(QString)),
216 m_appletScriptEngine, SLOT(executeAction(QString)));
219 connect(action, SIGNAL(triggered()), m_actionSignals, SLOT(map()));
220 m_actionSignals->setMapping(action, name);
223 action->setIcon(icon.isEmpty() ? QIcon() : KIcon(icon));
224 action->setShortcut(shortcut);
225 action->setObjectName(name);
228 void AppletInterface::removeAction(const QString &name)
230 Plasma::Applet *a = applet();
231 QAction *action = a->action(name);
233 if (action) {
234 if (m_actionSignals) {
235 m_actionSignals->removeMappings(action);
238 delete action;
241 m_actions.remove(name);
244 void AppletInterface::resize(qreal w, qreal h)
246 applet()->resize(w,h);
249 void AppletInterface::setMinimumSize(qreal w, qreal h)
251 applet()->setMinimumSize(w,h);
254 void AppletInterface::setPreferredSize(qreal w, qreal h)
256 applet()->setPreferredSize(w,h);
259 void AppletInterface::dataUpdated(QString source, Plasma::DataEngine::Data data)
261 m_appletScriptEngine->dataUpdated(source, data);
264 Plasma::Applet *AppletInterface::applet() const
266 return m_appletScriptEngine->applet();
269 #include "appletinterface.moc"