1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
* Copyright 2009 Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "containment.h"
#include <QAction>
#include <QQuickItem>
#include <klocalizedstring.h>
#include <kactioncollection.h>
#include <kdeclarative/configpropertymap.h>
#include <Plasma/Corona>
#include <Plasma/Containment>
#include <Plasma/PluginLoader>
#include "scriptengine.h"
#include "widget.h"
namespace WorkspaceScripting
{
class Containment::Private
{
public:
QPointer<Plasma::Containment> containment;
QString oldWallpaperPlugin;
QString wallpaperPlugin;
QString oldWallpaperMode;
QString wallpaperMode;
};
Containment::Containment(Plasma::Containment *containment, QObject *parent)
: Applet(parent),
d(new Containment::Private)
{
d->containment = containment;
setCurrentConfigGroup(QStringList());
setCurrentGlobalConfigGroup(QStringList());
if (containment) {
d->oldWallpaperPlugin = d->wallpaperPlugin = containment->wallpaper();
}
}
Containment::~Containment()
{
if (d->containment) {
Plasma::Containment *containment = d->containment.data();
if (d->oldWallpaperPlugin != d->wallpaperPlugin ||
d->oldWallpaperMode != d->wallpaperMode) {
containment->setWallpaper(d->wallpaperPlugin);
}
}
reloadConfigIfNeeded();
delete d;
}
int Containment::screen() const
{
if (!d->containment) {
return -1;
}
return d->containment.data()->screen();
}
QString Containment::wallpaperPlugin() const
{
return d->wallpaperPlugin;
}
void Containment::setWallpaperPlugin(const QString &wallpaperPlugin)
{
d->wallpaperPlugin = wallpaperPlugin;
}
QString Containment::wallpaperMode() const
{
return d->wallpaperMode;
}
void Containment::setWallpaperMode(const QString &wallpaperMode)
{
d->wallpaperMode = wallpaperMode;
}
QString Containment::formFactor() const
{
if (!d->containment) {
return QStringLiteral("Planar");
}
switch (d->containment.data()->formFactor()) {
case Plasma::Types::Planar:
return QStringLiteral("planar");
break;
case Plasma::Types::MediaCenter:
return QStringLiteral("mediacenter");
break;
case Plasma::Types::Horizontal:
return QStringLiteral("horizontal");
break;
case Plasma::Types::Vertical:
return QStringLiteral("vertical");
break;
case Plasma::Types::Application:
return QStringLiteral("application");
break;
}
return QStringLiteral("Planar");
}
QList<int> Containment::widgetIds() const
{
//FIXME: the ints could overflow since Applet::id() returns a uint,
// however QScript deals with QList<uint> very, very poory
QList<int> w;
if (d->containment) {
foreach (const Plasma::Applet *applet, d->containment.data()->applets()) {
w.append(applet->id());
}
}
return w;
}
QScriptValue Containment::widgetById(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() == 0) {
return context->throwError(i18n("widgetById requires an id"));
}
const uint id = context->argument(0).toInt32();
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
if (!c) {
return engine->undefinedValue();
}
if (c->d->containment) {
foreach (Plasma::Applet *w, c->d->containment.data()->applets()) {
if (w->id() == id) {
ScriptEngine *env = ScriptEngine::envFor(engine);
return env->wrap(w);
}
}
}
return engine->undefinedValue();
}
QScriptValue Containment::addWidget(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() == 0) {
return context->throwError(i18n("addWidget requires a name of a widget or a widget object"));
}
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
if (!c || !c->d->containment) {
return engine->undefinedValue();
}
QScriptValue v = context->argument(0);
Plasma::Applet *applet = 0;
QRectF geometry(-1, -1, -1, -1);
if (context->argumentCount() > 4) {
//The user provided a geometry as parameter
if (context->argument(1).isNumber() &&
context->argument(2).isNumber() &&
context->argument(3).isNumber() &&
context->argument(4).isNumber()) {
//Try to reconstruct a rectangle from the object hat has been passed
//It's expected a js object such as
//addWidget("org.kde.plasma.analogclock", 0, 100, 300, 400);
geometry = QRectF(context->argument(1).toNumber(),
context->argument(2).toNumber(),
context->argument(3).toNumber(),
context->argument(4).toNumber());
}
}
if (v.isString()) {
//A position has been supplied: search for the containment's graphics object
QQuickItem *containmentItem = nullptr;
if (geometry.x() >= 0 && geometry.y() >= 0) {
containmentItem = c->d->containment.data()->property("_plasma_graphicObject").value<QQuickItem *>();
Plasma::Applet *applet = nullptr;
if (containmentItem) {
QMetaObject::invokeMethod(containmentItem , "createApplet", Qt::DirectConnection, Q_RETURN_ARG(Plasma::Applet *, applet), Q_ARG(QString, v.toString()), Q_ARG(QVariantList, QVariantList()), Q_ARG(QRectF, geometry));
}
if (applet) {
ScriptEngine *env = ScriptEngine::envFor(engine);
return env->wrap(applet);
}
}
//Case in which either:
// * a geometry wasn't provided
// * containmentItem wasn't found
applet = c->d->containment.data()->createApplet(v.toString());
if (applet) {
ScriptEngine *env = ScriptEngine::envFor(engine);
return env->wrap(applet);
}
} else if (Widget *widget = qobject_cast<Widget*>(v.toQObject())) {
applet = widget->applet();
c->d->containment.data()->addApplet(applet);
return v;
}
return engine->undefinedValue();
}
QScriptValue Containment::widgets(QScriptContext *context, QScriptEngine *engine)
{
Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
if (!c || !c->d->containment) {
return engine->undefinedValue();
}
const QString widgetType = context->argumentCount() > 0 ? context->argument(0).toString() : QString();
QScriptValue widgets = engine->newArray();
ScriptEngine *env = ScriptEngine::envFor(engine);
int count = 0;
foreach (Plasma::Applet *widget, c->d->containment.data()->applets()) {
if (widgetType.isEmpty() || widget->pluginInfo().pluginName() == widgetType) {
widgets.setProperty(count, env->wrap(widget));
++count;
}
}
widgets.setProperty(QStringLiteral("length"), count);
return widgets;
}
uint Containment::id() const
{
if (!d->containment) {
return 0;
}
return d->containment.data()->id();
}
QString Containment::type() const
{
if (!d->containment) {
return QString();
}
return d->containment.data()->pluginInfo().pluginName();
}
void Containment::remove()
{
if (d->containment) {
d->containment.data()->destroy();
}
}
void Containment::showConfigurationInterface()
{
if (d->containment) {
QAction *configAction = d->containment.data()->actions()->action(QStringLiteral("configure"));
if (configAction && configAction->isEnabled()) {
configAction->trigger();
}
}
}
Plasma::Applet *Containment::applet() const
{
return d->containment.data();
}
Plasma::Containment *Containment::containment() const
{
return d->containment.data();
}
}
|