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

« back to all changes in this revision

Viewing changes to libs/plasmagenericshell/scripting/containment.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 Aaron Seigo <aseigo@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 "containment.h"
 
21
 
 
22
#include <QAction>
 
23
 
 
24
#include <Plasma/Corona>
 
25
#include <Plasma/Containment>
 
26
#include <Plasma/Wallpaper>
 
27
 
 
28
#include "scriptengine.h"
 
29
#include "widget.h"
 
30
 
 
31
namespace WorkspaceScripting
 
32
{
 
33
 
 
34
class Containment::Private
 
35
{
 
36
public:
 
37
    QWeakPointer<Plasma::Containment> containment;
 
38
    QString oldWallpaperPlugin;
 
39
    QString wallpaperPlugin;
 
40
    QString oldWallpaperMode;
 
41
    QString wallpaperMode;
 
42
};
 
43
 
 
44
Containment::Containment(Plasma::Containment *containment, QObject *parent)
 
45
    : Applet(parent),
 
46
      d(new Containment::Private)
 
47
{
 
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();
 
54
    }
 
55
}
 
56
 
 
57
Containment::~Containment()
 
58
{
 
59
    if (d->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);
 
69
        }
 
70
    }
 
71
 
 
72
    reloadConfigIfNeeded();
 
73
    delete d;
 
74
}
 
75
 
 
76
int Containment::screen() const
 
77
{
 
78
    if (!d->containment) {
 
79
        return -1;
 
80
    }
 
81
 
 
82
    return d->containment.data()->screen();
 
83
}
 
84
 
 
85
void Containment::setScreen(int screen)
 
86
{
 
87
    if (d->containment) {
 
88
        d->containment.data()->setScreen(screen);
 
89
    }
 
90
}
 
91
 
 
92
QString Containment::wallpaperPlugin() const
 
93
{
 
94
    return d->wallpaperPlugin;
 
95
}
 
96
 
 
97
void Containment::setWallpaperPlugin(const QString &wallpaperPlugin)
 
98
{
 
99
    d->wallpaperPlugin = wallpaperPlugin;
 
100
}
 
101
 
 
102
 
 
103
QString Containment::wallpaperMode() const
 
104
{
 
105
    return d->wallpaperMode;
 
106
}
 
107
 
 
108
void Containment::setWallpaperMode(const QString &wallpaperMode)
 
109
{
 
110
    d->wallpaperMode = wallpaperMode;
 
111
}
 
112
 
 
113
int Containment::desktop() const
 
114
{
 
115
    if (!d->containment) {
 
116
        return -1;
 
117
    }
 
118
 
 
119
    return d->containment.data()->desktop();
 
120
}
 
121
 
 
122
void Containment::setDesktop(int desktop)
 
123
{
 
124
    if (d->containment) {
 
125
        d->containment.data()->setScreen(d->containment.data()->screen(), desktop);
 
126
    }
 
127
}
 
128
 
 
129
QString Containment::formFactor() const
 
130
{
 
131
    if (!d->containment) {
 
132
        return "Planar";
 
133
    }
 
134
 
 
135
    switch (d->containment.data()->formFactor()) {
 
136
        case Plasma::Planar:
 
137
            return "planar";
 
138
            break;
 
139
        case Plasma::MediaCenter:
 
140
            return "mediacenter";
 
141
            break;
 
142
        case Plasma::Horizontal:
 
143
            return "horizontal";
 
144
            break;
 
145
        case Plasma::Vertical:
 
146
            return "vertical";
 
147
            break;
 
148
    }
 
149
 
 
150
    return "Planar";
 
151
}
 
152
 
 
153
QList<int> Containment::widgetIds() const
 
154
{
 
155
    //FIXME: the ints could overflow since Applet::id() returns a uint,
 
156
    //       however QScript deals with QList<uint> very, very poory
 
157
    QList<int> w;
 
158
 
 
159
    if (d->containment) {
 
160
        foreach (const Plasma::Applet *applet, d->containment.data()->applets()) {
 
161
            w.append(applet->id());
 
162
        }
 
163
    }
 
164
 
 
165
    return w;
 
166
}
 
167
 
 
168
QScriptValue Containment::widgetById(QScriptContext *context, QScriptEngine *engine)
 
169
{
 
170
    if (context->argumentCount() == 0) {
 
171
        return context->throwError(i18n("widgetById requires an id"));
 
172
    }
 
173
 
 
174
    const uint id = context->argument(0).toInt32();
 
175
    Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
 
176
 
 
177
    if (!c) {
 
178
        return engine->undefinedValue();
 
179
    }
 
180
 
 
181
    if (c->d->containment) {
 
182
        foreach (Plasma::Applet *w, c->d->containment.data()->applets()) {
 
183
            if (w->id() == id) {
 
184
                ScriptEngine *env = ScriptEngine::envFor(engine);
 
185
                return env->wrap(w);
 
186
            }
 
187
        }
 
188
    }
 
189
 
 
190
    return engine->undefinedValue();
 
191
}
 
192
 
 
193
QScriptValue Containment::addWidget(QScriptContext *context, QScriptEngine *engine)
 
194
{
 
195
    if (context->argumentCount() == 0) {
 
196
        return context->throwError(i18n("widgetById requires a name of a widget or a widget object"));
 
197
    }
 
198
 
 
199
    Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
 
200
 
 
201
    if (!c || !c->d->containment) {
 
202
        return engine->undefinedValue();
 
203
    }
 
204
 
 
205
    QScriptValue v = context->argument(0);
 
206
    Plasma::Applet *applet = 0;
 
207
    if (v.isString()) {
 
208
        applet = c->d->containment.data()->addApplet(v.toString());
 
209
        if (applet) {
 
210
            ScriptEngine *env = ScriptEngine::envFor(engine);
 
211
            return env->wrap(applet);
 
212
        }
 
213
    } else if (Widget *widget = qobject_cast<Widget*>(v.toQObject())) {
 
214
        applet = widget->applet();
 
215
        c->d->containment.data()->addApplet(applet);
 
216
        return v;
 
217
    }
 
218
 
 
219
    return engine->undefinedValue();
 
220
}
 
221
 
 
222
QScriptValue Containment::widgets(QScriptContext *context, QScriptEngine *engine)
 
223
{
 
224
    Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
 
225
 
 
226
    if (!c || !c->d->containment) {
 
227
        return engine->undefinedValue();
 
228
    }
 
229
 
 
230
    const QString widgetType = context->argumentCount() > 0 ? context->argument(0).toString() : QString();
 
231
    QScriptValue widgets = engine->newArray();
 
232
    ScriptEngine *env = ScriptEngine::envFor(engine);
 
233
    int count = 0;
 
234
 
 
235
    foreach (Plasma::Applet *widget, c->d->containment.data()->applets()) {
 
236
        if (widgetType.isEmpty() || widget->pluginName() == widgetType) {
 
237
            widgets.setProperty(count, env->wrap(widget));
 
238
            ++count;
 
239
        }
 
240
    }
 
241
 
 
242
    widgets.setProperty("length", count);
 
243
    return widgets;
 
244
}
 
245
 
 
246
uint Containment::id() const
 
247
{
 
248
    if (!d->containment) {
 
249
        return 0;
 
250
    }
 
251
 
 
252
    return d->containment.data()->id();
 
253
}
 
254
 
 
255
QString Containment::name() const
 
256
{
 
257
    if (!d->containment) {
 
258
        return QString();
 
259
    }
 
260
 
 
261
    return d->containment.data()->activity();
 
262
}
 
263
 
 
264
void Containment::setName(const QString &name)
 
265
{
 
266
    if (d->containment) {
 
267
        d->containment.data()->setActivity(name);
 
268
    }
 
269
}
 
270
 
 
271
QString Containment::type() const
 
272
{
 
273
    if (!d->containment) {
 
274
        return QString();
 
275
    }
 
276
 
 
277
    return d->containment.data()->pluginName();
 
278
}
 
279
 
 
280
void Containment::remove()
 
281
{
 
282
    if (d->containment) {
 
283
        d->containment.data()->destroy(false);
 
284
    }
 
285
}
 
286
 
 
287
void Containment::showConfigurationInterface()
 
288
{
 
289
    if (d->containment) {
 
290
        QAction *configAction = d->containment.data()->action("configure");
 
291
        if (configAction && configAction->isEnabled()) {
 
292
            configAction->trigger();
 
293
        }
 
294
    }
 
295
}
 
296
 
 
297
Plasma::Applet *Containment::applet() const
 
298
{
 
299
    return d->containment.data();
 
300
}
 
301
 
 
302
Plasma::Containment *Containment::containment() const
 
303
{
 
304
    return d->containment.data();
 
305
}
 
306
 
 
307
}
 
308
 
 
309
#include "containment.moc"
 
310