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

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
Copyright (c) 2007 Zack Rusin <zack@kde.org>
Copyright (c) 2008 Petri Damstén <damu@iki.fi>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "plasmawebapplet.h"

#include <QAction>
#include <QWebPage>
#include <QWebFrame>

#include <KColorScheme>

#include <Plasma/DataEngineManager>
#include <Plasma/Applet>
#include <Plasma/Theme>
#include <Plasma/WebView>

#define JS_CONSTANTS_CONSTRAINT \
"var NoConstraint = %1;\n"\
"var FormFactorConstraint = %2;\n"\
"var LocationConstraint = %3;\n"\
"var ScreenConstraint = %4;\n"\
"var SizeConstraint = %5;\n"\
"var ImmutableConstraint = %6;\n"\
"var StartupCompletedConstraint = %7;\n"\
"var ContextConstraint = %8;\n"\
"var AllConstraints = %9;\n"

#define JS_CONSTANTS_BACKGROUND \
"var NoBackground = %1;\n"\
"var StandardBackground = %2;\n"\
"var TranslucentBackground = %3;\n"\
"var DefaultBackground = %5;\n"

#define JS_CONSTANTS_SCROLLBAR \
"var QtHorizontal = %1;\n"\
"var QtVertical = %2;\n"\
"var ScrollBarAsNeeded = %3;\n"\
"var ScrollBarAlwaysOff = %4;\n"\
"var ScrollBarAlwaysOn = %5;\n"\

#define JS_CONSTANTS_ASPECTRATIO \
"var InvalidAspectRatioMode = %1;\n"\
"var IgnoreAspectRatio = %2;\n"\
"var KeepAspectRatio = %3;\n"\
"var Square = %4;\n"\
"var ConstrainedSquare = %5;\n"\
"var FixedSize = %6;\n"

#define JS_CONSTANTS_FORMFACTOR \
"var Planar = %1;\n"\
"var MediaCenter = %2;\n"\
"var Horizontal = %3;\n"\
"var Vertical = %4;\n"\

#define JS_CONSTANTS_LOCATION \
"var Floating = %1;\n"\
"var Desktop = %2;\n"\
"var FullScreen = %3;\n"\
"var TopEdge = %4;\n"\
"var BottomEdge = %5;\n"\
"var LeftEdge = %6;\n"\
"var RightEdge = %7;\n"

#define JS_CONSTANTS_OTHER \
"var size_width = 0;\n"\
"var size_height = 1;\n"\
"var point_x = 0;\n"\
"var point_y = 1;\n"\
"var rect_x = 0;\n"\
"var rect_y = 1;\n"\
"var rect_width = 2;\n"\
"var rect_height = 3;\n"\
"var margin_left = 0;\n"\
"var margin_top = 1;\n"\
"var margin_right = 2;\n"\
"var margin_bottom = 3;\n"\

#define CSS "body { font-family: %3; font-size: %4pt; color:%1; background-color:%2 }\n"

QString PlasmaWebApplet::s_jsConstants;

PlasmaWebApplet::PlasmaWebApplet(QObject *parent, const QVariantList &args)
: WebApplet(parent, args)
{
    if (s_jsConstants.isEmpty()) {
        s_jsConstants = JS_CONSTANTS_OTHER;
        s_jsConstants += QString(JS_CONSTANTS_CONSTRAINT)
                .arg(Plasma::NoConstraint)
                .arg(Plasma::FormFactorConstraint)
                .arg(Plasma::LocationConstraint)
                .arg(Plasma::ScreenConstraint)
                .arg(Plasma::SizeConstraint)
                .arg(Plasma::ImmutableConstraint)
                .arg(Plasma::StartupCompletedConstraint)
                .arg(Plasma::ContextConstraint)
                .arg(Plasma::AllConstraints);
        s_jsConstants += QString(JS_CONSTANTS_BACKGROUND)
                .arg(Plasma::Applet::NoBackground)
                .arg(Plasma::Applet::StandardBackground)
                .arg(Plasma::Applet::TranslucentBackground)
                .arg(Plasma::Applet::DefaultBackground);
        s_jsConstants += QString(JS_CONSTANTS_SCROLLBAR)
                .arg(Qt::Horizontal)
                .arg(Qt::Vertical)
                .arg(Qt::ScrollBarAsNeeded)
                .arg(Qt::ScrollBarAlwaysOff)
                .arg(Qt::ScrollBarAlwaysOn);
        s_jsConstants += QString(JS_CONSTANTS_ASPECTRATIO)
                .arg(Plasma::InvalidAspectRatioMode)
                .arg(Plasma::IgnoreAspectRatio)
                .arg(Plasma::KeepAspectRatio)
                .arg(Plasma::Square)
                .arg(Plasma::ConstrainedSquare)
                .arg(Plasma::FixedSize);
        s_jsConstants += QString(JS_CONSTANTS_FORMFACTOR)
                .arg(Plasma::Planar)
                .arg(Plasma::MediaCenter)
                .arg(Plasma::Horizontal)
                .arg(Plasma::Vertical);
        s_jsConstants += QString(JS_CONSTANTS_LOCATION)
                .arg(Plasma::Floating)
                .arg(Plasma::Desktop)
                .arg(Plasma::FullScreen)
                .arg(Plasma::TopEdge)
                .arg(Plasma::BottomEdge)
                .arg(Plasma::LeftEdge)
                .arg(Plasma::RightEdge);
    }
}

PlasmaWebApplet::~PlasmaWebApplet()
{
}

bool PlasmaWebApplet::init()
{
    if (WebApplet::init()) {
        connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
                this, SLOT(themeChanged()));
        makeStylesheet();
        page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
        QAction *inspectAction = page()->action(QWebPage::InspectElement);
        inspectAction->setText(i18n("Inspect this widget"));
        return true;
    }

    return false;
}

void PlasmaWebApplet::makeStylesheet()
{
    if (!page()) {
        return;
    }

    // this temporary file contains the style sheet to be used when loading/reloading the
    // web content. we keep it around for the lifetime of the widget since it needs to be there
    // when reloaded

    //TODO perhaps share this file between all instances? perhaps even keep it persistent on disk
    // to limit disk write/deletes. that would be simple enough, the only trick would be to
    // ensure it updates (and only updates once) on Plasma theme changes so that it doesn't get
    // written to in a flury by every PlasmaWebApplet when the theme updates
    // probably a reference counted singleton would be the way to go here.
    if (!m_styleSheetFile.open()) {
        return;
    }

    KColorScheme plasmaColorTheme = KColorScheme(QPalette::Active, KColorScheme::View,
            Plasma::Theme::defaultTheme()->colorScheme());
    QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
    QColor backgroundColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
    QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);

    QString css = QString(CSS).arg(textColor.name())
                              .arg(backgroundColor.name())
                              .arg(font.family())
                              .arg(font.pointSize());
    m_styleSheetFile.write(css.toUtf8());
    page()->settings()->setUserStyleSheetUrl(QUrl(m_styleSheetFile.fileName()));
    m_styleSheetFile.close();
}

void PlasmaWebApplet::themeChanged()
{
    makeStylesheet();
    callJsFunction("themeChanged");
}

void PlasmaWebApplet::loadFinished(bool success)
{
    WebApplet::loadFinished(success);
    if (success) {
        page()->mainFrame()->evaluateJavaScript(s_jsConstants);
        callJsFunction("init");
    }
}

void PlasmaWebApplet::constraintsEvent(Plasma::Constraints constraints)
{
    if (page() && constraints & Plasma::SizeConstraint) {
        qreal left;
        qreal top;
        qreal right;
        qreal bottom;
        applet()->getContentsMargins(&left, &top, &right, &bottom);
        view()->setPos(QPointF(left, top));
        view()->resize(WebApplet::size() - QSizeF(left + right, top + bottom));
        //kDebug() << WebApplet::size() << left << right << top << bottom << page()->size();
    }
    callJsFunction("constraintsEvent", QVariantList() << (int)constraints);
}

QVariant PlasmaWebApplet::arg(int index) const
{
    return m_args[index];
}

QObject* PlasmaWebApplet::objArg(int index) const
{
    return m_args[index].value<QObject*>();
}

QString PlasmaWebApplet::name() const
{
    return applet()->name();
}

uint PlasmaWebApplet::id() const
{
    return applet()->id();
}

int PlasmaWebApplet::formFactor() const
{
    return (int)applet()->formFactor();
}

int PlasmaWebApplet::location() const
{
    return (int)applet()->location();
}

QString PlasmaWebApplet::pluginName() const
{
    return applet()->pluginName();
}

QString PlasmaWebApplet::icon() const
{
    return applet()->icon();
}

QString PlasmaWebApplet::category() const
{
    return applet()->category();
}

bool PlasmaWebApplet::shouldConserveResources() const
{
    return applet()->shouldConserveResources();
}

QStringList PlasmaWebApplet::listAllDataEngines()
{
    return Plasma::DataEngineManager::listAllEngines();
}

QObject* PlasmaWebApplet::dataEngine(const QString& name)
{
    QString id = QString("%1").arg(applet()->id());
    Plasma::DataEngine *de = applet()->dataEngine(name);
    DataEngineWrapper *wrapper = de->findChild<DataEngineWrapper*>(id);
    if (!wrapper) {
        wrapper = new DataEngineWrapper(de, this);
        wrapper->setObjectName(id);
    }
    return wrapper;
}

QObject* PlasmaWebApplet::config()
{
    m_config.setConfig(applet()->config());
    return &m_config;
}

QObject* PlasmaWebApplet::globalConfig()
{
    m_globalConfig.setConfig(applet()->globalConfig());
    return &m_globalConfig;
}

void PlasmaWebApplet::resize(qreal w, qreal h)
{
    applet()->resize(w, h);
}

void PlasmaWebApplet::setBackgroundHints(int hints)
{
    applet()->setBackgroundHints((Plasma::Applet::BackgroundHints)hints);
}

void PlasmaWebApplet::setScrollBarPolicy(int orientation, int policy)
{
    page()->mainFrame()->setScrollBarPolicy((Qt::Orientation)orientation,
                                            (Qt::ScrollBarPolicy)policy);
}

void PlasmaWebApplet::setAspectRatioMode(int mode)
{
    applet()->setAspectRatioMode((Plasma::AspectRatioMode)mode);
}

QVariant PlasmaWebApplet::callJsFunction(const QString& func, const QVariantList& args)
{
    if (loaded()) {
        m_args = args;
        QString cmd = "if (window." + func + ") { " + func + '(';
        for(int i = 0; i < args.count(); ++i) {
            if (i > 0) {
                cmd += ',';
            }
            if (args[i].canConvert<QObject*>()) {
                cmd += QString("window.plasmoid.objArg(%1)").arg(i);
            } else {
                cmd += QString("window.plasmoid.arg(%1)").arg(i);
            }
        }
        cmd += ") }";
        //kDebug() << cmd;
        return page()->mainFrame()->evaluateJavaScript(cmd);
    }
    return QVariant();
}

void PlasmaWebApplet::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
{
    m_dataEngineData.setData(data);
    callJsFunction("dataUpdated",
                   QVariantList() << source << QVariant::fromValue((QObject*)&m_dataEngineData));
}

void PlasmaWebApplet::configChanged()
{
    callJsFunction("configChanged");
}

void PlasmaWebApplet::initJsObjects()
{
    QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
    Q_ASSERT(frame);
    frame->addToJavaScriptWindowObject(QLatin1String("plasmoid"), this);
}

QVariantList PlasmaWebApplet::geometry()
{
    return QVariantList() << applet()->geometry().left() << applet()->geometry().top()
                          << applet()->geometry().width() << applet()->geometry().height();
}

QVariantList PlasmaWebApplet::screenRect()
{
    return QVariantList() << applet()->screenRect().left() << applet()->screenRect().top()
                          << applet()->screenRect().width() << applet()->screenRect().height();
}

int PlasmaWebApplet::backgroundHints()
{
    return applet()->backgroundHints();
}

int PlasmaWebApplet::aspectRatioMode()
{
    return applet()->aspectRatioMode();
}

void PlasmaWebApplet::setConfigurationRequired(bool needsConfiguring, const QString &reason)
{
    WebApplet::setConfigurationRequired(needsConfiguring, reason);
}

void PlasmaWebApplet::setMaximumSize(qreal w, qreal h)
{
    applet()->setMaximumSize(w, h);
}

void PlasmaWebApplet::setMinimumSize(qreal w, qreal h)
{
    applet()->setMinimumSize(w, h);
}

void PlasmaWebApplet::setPreferredSize(qreal w, qreal h)
{
    applet()->setPreferredSize(w, h);
}

QVariantList PlasmaWebApplet::maximumSize()
{
    return QVariantList() << applet()->maximumSize().width() << applet()->maximumSize().height();
}

QVariantList PlasmaWebApplet::minimumSize()
{
    return QVariantList() << applet()->minimumSize().width() << applet()->minimumSize().height();
}

QVariantList PlasmaWebApplet::preferredSize()
{
    return QVariantList() << applet()->preferredSize().width()
                          << applet()->preferredSize().height();
}

QVariantList PlasmaWebApplet::getContentsMargins()
{
    qreal left;
    qreal top;
    qreal right;
    qreal bottom;
    applet()->getContentsMargins(&left, &top, &right, &bottom);
    return QVariantList() << left << top << right << bottom;
}

void PlasmaWebApplet::setGeometry(qreal x, qreal y, qreal w, qreal h)
{
    applet()->setGeometry(x, y, w, h);
}

void PlasmaWebApplet::setPos(qreal x, qreal y)
{
    applet()->setPos(x, y);
}

QVariantList PlasmaWebApplet::pos()
{
    return QVariantList() << applet()->pos().x() << applet()->pos().y();
}

QVariantList PlasmaWebApplet::size()
{
    return QVariantList() << applet()->size().width() << applet()->size().height();
}

void PlasmaWebApplet::setFailedToLaunch(bool failed, const QString &reason)
{
    WebApplet::setFailedToLaunch(failed, reason);
}

void PlasmaWebApplet::update()
{
    applet()->update();
}

bool PlasmaWebApplet::isBusy() const
{
    return applet()->isBusy();
}

void PlasmaWebApplet::setBusy(bool busy)
{
    applet()->setBusy(busy);
}

#include "plasmawebapplet.moc"