2
* Copyright 2009 Aaron Seigo <aseigo@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 "containment.h"
24
#include <Plasma/Corona>
25
#include <Plasma/Containment>
26
#include <Plasma/Wallpaper>
28
#include "scriptengine.h"
31
namespace WorkspaceScripting
34
class Containment::Private
37
QWeakPointer<Plasma::Containment> containment;
38
QString oldWallpaperPlugin;
39
QString wallpaperPlugin;
40
QString oldWallpaperMode;
41
QString wallpaperMode;
44
Containment::Containment(Plasma::Containment *containment, QObject *parent)
46
d(new Containment::Private)
48
d->containment = containment;
49
setCurrentConfigGroup(QStringList());
50
setCurrentGlobalConfigGroup(QStringList());
51
if (containment && containment->wallpaper()) {
52
d->oldWallpaperPlugin = d->wallpaperPlugin = containment->wallpaper()->pluginName();
53
d->oldWallpaperMode = d->wallpaperMode = containment->wallpaper()->renderingMode().name();
57
Containment::~Containment()
60
Plasma::Containment *containment = d->containment.data();
61
if (d->oldWallpaperPlugin != d->wallpaperPlugin ||
62
d->oldWallpaperMode != d->wallpaperMode) {
63
containment->setWallpaper(d->wallpaperPlugin, d->wallpaperMode);
64
} else if (wallpaperConfigDirty() && containment->wallpaper()) {
65
KConfigGroup cg(containment->config());
66
cg = KConfigGroup(&cg, "Wallpaper");
67
cg = KConfigGroup(&cg, containment->wallpaper()->pluginName());
68
containment->wallpaper()->restore(cg);
72
reloadConfigIfNeeded();
76
int Containment::screen() const
78
if (!d->containment) {
82
return d->containment.data()->screen();
85
void Containment::setScreen(int screen)
88
d->containment.data()->setScreen(screen);
92
QString Containment::wallpaperPlugin() const
94
return d->wallpaperPlugin;
97
void Containment::setWallpaperPlugin(const QString &wallpaperPlugin)
99
d->wallpaperPlugin = wallpaperPlugin;
103
QString Containment::wallpaperMode() const
105
return d->wallpaperMode;
108
void Containment::setWallpaperMode(const QString &wallpaperMode)
110
d->wallpaperMode = wallpaperMode;
113
int Containment::desktop() const
115
if (!d->containment) {
119
return d->containment.data()->desktop();
122
void Containment::setDesktop(int desktop)
124
if (d->containment) {
125
d->containment.data()->setScreen(d->containment.data()->screen(), desktop);
129
QString Containment::formFactor() const
131
if (!d->containment) {
135
switch (d->containment.data()->formFactor()) {
139
case Plasma::MediaCenter:
140
return "mediacenter";
142
case Plasma::Horizontal:
145
case Plasma::Vertical:
153
QList<int> Containment::widgetIds() const
155
//FIXME: the ints could overflow since Applet::id() returns a uint,
156
// however QScript deals with QList<uint> very, very poory
159
if (d->containment) {
160
foreach (const Plasma::Applet *applet, d->containment.data()->applets()) {
161
w.append(applet->id());
168
QScriptValue Containment::widgetById(QScriptContext *context, QScriptEngine *engine)
170
if (context->argumentCount() == 0) {
171
return context->throwError(i18n("widgetById requires an id"));
174
const uint id = context->argument(0).toInt32();
175
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
178
return engine->undefinedValue();
181
if (c->d->containment) {
182
foreach (Plasma::Applet *w, c->d->containment.data()->applets()) {
184
ScriptEngine *env = ScriptEngine::envFor(engine);
190
return engine->undefinedValue();
193
QScriptValue Containment::addWidget(QScriptContext *context, QScriptEngine *engine)
195
if (context->argumentCount() == 0) {
196
return context->throwError(i18n("widgetById requires a name of a widget or a widget object"));
199
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
201
if (!c || !c->d->containment) {
202
return engine->undefinedValue();
205
QScriptValue v = context->argument(0);
206
Plasma::Applet *applet = 0;
208
applet = c->d->containment.data()->addApplet(v.toString());
210
ScriptEngine *env = ScriptEngine::envFor(engine);
211
return env->wrap(applet);
213
} else if (Widget *widget = qobject_cast<Widget*>(v.toQObject())) {
214
applet = widget->applet();
215
c->d->containment.data()->addApplet(applet);
219
return engine->undefinedValue();
222
QScriptValue Containment::widgets(QScriptContext *context, QScriptEngine *engine)
224
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
226
if (!c || !c->d->containment) {
227
return engine->undefinedValue();
230
const QString widgetType = context->argumentCount() > 0 ? context->argument(0).toString() : QString();
231
QScriptValue widgets = engine->newArray();
232
ScriptEngine *env = ScriptEngine::envFor(engine);
235
foreach (Plasma::Applet *widget, c->d->containment.data()->applets()) {
236
if (widgetType.isEmpty() || widget->pluginName() == widgetType) {
237
widgets.setProperty(count, env->wrap(widget));
242
widgets.setProperty("length", count);
246
uint Containment::id() const
248
if (!d->containment) {
252
return d->containment.data()->id();
255
QString Containment::name() const
257
if (!d->containment) {
261
return d->containment.data()->activity();
264
void Containment::setName(const QString &name)
266
if (d->containment) {
267
d->containment.data()->setActivity(name);
271
QString Containment::type() const
273
if (!d->containment) {
277
return d->containment.data()->pluginName();
280
void Containment::remove()
282
if (d->containment) {
283
d->containment.data()->destroy(false);
287
void Containment::showConfigurationInterface()
289
if (d->containment) {
290
QAction *configAction = d->containment.data()->action("configure");
291
if (configAction && configAction->isEnabled()) {
292
configAction->trigger();
297
Plasma::Applet *Containment::applet() const
299
return d->containment.data();
302
Plasma::Containment *Containment::containment() const
304
return d->containment.data();
309
#include "containment.moc"