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

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/webkit/plasmawebapplet.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 (c) 2007 Zack Rusin <zack@kde.org>
 
3
Copyright (c) 2008 Petri Damstén <damu@iki.fi>
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
of this software and associated documentation files (the "Software"), to deal
 
7
in the Software without restriction, including without limitation the rights
 
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the Software is
 
10
furnished to do so, subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
21
THE SOFTWARE.
 
22
*/
 
23
 
 
24
#include "plasmawebapplet.h"
 
25
 
 
26
#include <QAction>
 
27
#include <QWebPage>
 
28
#include <QWebFrame>
 
29
 
 
30
#include <KColorScheme>
 
31
 
 
32
#include <Plasma/DataEngineManager>
 
33
#include <Plasma/Applet>
 
34
#include <Plasma/Theme>
 
35
#include <Plasma/WebView>
 
36
 
 
37
#define JS_CONSTANTS_CONSTRAINT \
 
38
"var NoConstraint = %1;\n"\
 
39
"var FormFactorConstraint = %2;\n"\
 
40
"var LocationConstraint = %3;\n"\
 
41
"var ScreenConstraint = %4;\n"\
 
42
"var SizeConstraint = %5;\n"\
 
43
"var ImmutableConstraint = %6;\n"\
 
44
"var StartupCompletedConstraint = %7;\n"\
 
45
"var ContextConstraint = %8;\n"\
 
46
"var AllConstraints = %9;\n"
 
47
 
 
48
#define JS_CONSTANTS_BACKGROUND \
 
49
"var NoBackground = %1;\n"\
 
50
"var StandardBackground = %2;\n"\
 
51
"var TranslucentBackground = %3;\n"\
 
52
"var DefaultBackground = %5;\n"
 
53
 
 
54
#define JS_CONSTANTS_SCROLLBAR \
 
55
"var QtHorizontal = %1;\n"\
 
56
"var QtVertical = %2;\n"\
 
57
"var ScrollBarAsNeeded = %3;\n"\
 
58
"var ScrollBarAlwaysOff = %4;\n"\
 
59
"var ScrollBarAlwaysOn = %5;\n"\
 
60
 
 
61
#define JS_CONSTANTS_ASPECTRATIO \
 
62
"var InvalidAspectRatioMode = %1;\n"\
 
63
"var IgnoreAspectRatio = %2;\n"\
 
64
"var KeepAspectRatio = %3;\n"\
 
65
"var Square = %4;\n"\
 
66
"var ConstrainedSquare = %5;\n"\
 
67
"var FixedSize = %6;\n"
 
68
 
 
69
#define JS_CONSTANTS_FORMFACTOR \
 
70
"var Planar = %1;\n"\
 
71
"var MediaCenter = %2;\n"\
 
72
"var Horizontal = %3;\n"\
 
73
"var Vertical = %4;\n"\
 
74
 
 
75
#define JS_CONSTANTS_LOCATION \
 
76
"var Floating = %1;\n"\
 
77
"var Desktop = %2;\n"\
 
78
"var FullScreen = %3;\n"\
 
79
"var TopEdge = %4;\n"\
 
80
"var BottomEdge = %5;\n"\
 
81
"var LeftEdge = %6;\n"\
 
82
"var RightEdge = %7;\n"
 
83
 
 
84
#define JS_CONSTANTS_OTHER \
 
85
"var size_width = 0;\n"\
 
86
"var size_height = 1;\n"\
 
87
"var point_x = 0;\n"\
 
88
"var point_y = 1;\n"\
 
89
"var rect_x = 0;\n"\
 
90
"var rect_y = 1;\n"\
 
91
"var rect_width = 2;\n"\
 
92
"var rect_height = 3;\n"\
 
93
"var margin_left = 0;\n"\
 
94
"var margin_top = 1;\n"\
 
95
"var margin_right = 2;\n"\
 
96
"var margin_bottom = 3;\n"\
 
97
 
 
98
#define CSS "body { font-family: %3; font-size: %4pt; color:%1; background-color:%2 }\n"
 
99
 
 
100
QString PlasmaWebApplet::s_jsConstants;
 
101
 
 
102
PlasmaWebApplet::PlasmaWebApplet(QObject *parent, const QVariantList &args)
 
103
: WebApplet(parent, args)
 
104
{
 
105
    if (s_jsConstants.isEmpty()) {
 
106
        s_jsConstants = JS_CONSTANTS_OTHER;
 
107
        s_jsConstants += QString(JS_CONSTANTS_CONSTRAINT)
 
108
                .arg(Plasma::NoConstraint)
 
109
                .arg(Plasma::FormFactorConstraint)
 
110
                .arg(Plasma::LocationConstraint)
 
111
                .arg(Plasma::ScreenConstraint)
 
112
                .arg(Plasma::SizeConstraint)
 
113
                .arg(Plasma::ImmutableConstraint)
 
114
                .arg(Plasma::StartupCompletedConstraint)
 
115
                .arg(Plasma::ContextConstraint)
 
116
                .arg(Plasma::AllConstraints);
 
117
        s_jsConstants += QString(JS_CONSTANTS_BACKGROUND)
 
118
                .arg(Plasma::Applet::NoBackground)
 
119
                .arg(Plasma::Applet::StandardBackground)
 
120
                .arg(Plasma::Applet::TranslucentBackground)
 
121
                .arg(Plasma::Applet::DefaultBackground);
 
122
        s_jsConstants += QString(JS_CONSTANTS_SCROLLBAR)
 
123
                .arg(Qt::Horizontal)
 
124
                .arg(Qt::Vertical)
 
125
                .arg(Qt::ScrollBarAsNeeded)
 
126
                .arg(Qt::ScrollBarAlwaysOff)
 
127
                .arg(Qt::ScrollBarAlwaysOn);
 
128
        s_jsConstants += QString(JS_CONSTANTS_ASPECTRATIO)
 
129
                .arg(Plasma::InvalidAspectRatioMode)
 
130
                .arg(Plasma::IgnoreAspectRatio)
 
131
                .arg(Plasma::KeepAspectRatio)
 
132
                .arg(Plasma::Square)
 
133
                .arg(Plasma::ConstrainedSquare)
 
134
                .arg(Plasma::FixedSize);
 
135
        s_jsConstants += QString(JS_CONSTANTS_FORMFACTOR)
 
136
                .arg(Plasma::Planar)
 
137
                .arg(Plasma::MediaCenter)
 
138
                .arg(Plasma::Horizontal)
 
139
                .arg(Plasma::Vertical);
 
140
        s_jsConstants += QString(JS_CONSTANTS_LOCATION)
 
141
                .arg(Plasma::Floating)
 
142
                .arg(Plasma::Desktop)
 
143
                .arg(Plasma::FullScreen)
 
144
                .arg(Plasma::TopEdge)
 
145
                .arg(Plasma::BottomEdge)
 
146
                .arg(Plasma::LeftEdge)
 
147
                .arg(Plasma::RightEdge);
 
148
    }
 
149
}
 
150
 
 
151
PlasmaWebApplet::~PlasmaWebApplet()
 
152
{
 
153
}
 
154
 
 
155
bool PlasmaWebApplet::init()
 
156
{
 
157
    if (WebApplet::init()) {
 
158
        connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
 
159
                this, SLOT(themeChanged()));
 
160
        makeStylesheet();
 
161
        page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
 
162
        QAction *inspectAction = page()->action(QWebPage::InspectElement);
 
163
        inspectAction->setText(i18n("Inspect this widget"));
 
164
        return true;
 
165
    }
 
166
 
 
167
    return false;
 
168
}
 
169
 
 
170
void PlasmaWebApplet::makeStylesheet()
 
171
{
 
172
    if (!page()) {
 
173
        return;
 
174
    }
 
175
 
 
176
    // this temporary file contains the style sheet to be used when loading/reloading the
 
177
    // web content. we keep it around for the lifetime of the widget since it needs to be there
 
178
    // when reloaded
 
179
 
 
180
    //TODO perhaps share this file between all instances? perhaps even keep it persistent on disk
 
181
    // to limit disk write/deletes. that would be simple enough, the only trick would be to
 
182
    // ensure it updates (and only updates once) on Plasma theme changes so that it doesn't get
 
183
    // written to in a flury by every PlasmaWebApplet when the theme updates
 
184
    // probably a reference counted singleton would be the way to go here.
 
185
    if (!m_styleSheetFile.open()) {
 
186
        return;
 
187
    }
 
188
 
 
189
    KColorScheme plasmaColorTheme = KColorScheme(QPalette::Active, KColorScheme::View,
 
190
            Plasma::Theme::defaultTheme()->colorScheme());
 
191
    QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
192
    QColor backgroundColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
 
193
    QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
 
194
 
 
195
    QString css = QString(CSS).arg(textColor.name())
 
196
                              .arg(backgroundColor.name())
 
197
                              .arg(font.family())
 
198
                              .arg(font.pointSize());
 
199
    m_styleSheetFile.write(css.toUtf8());
 
200
    page()->settings()->setUserStyleSheetUrl(QUrl(m_styleSheetFile.fileName()));
 
201
    m_styleSheetFile.close();
 
202
}
 
203
 
 
204
void PlasmaWebApplet::themeChanged()
 
205
{
 
206
    makeStylesheet();
 
207
    callJsFunction("themeChanged");
 
208
}
 
209
 
 
210
void PlasmaWebApplet::loadFinished(bool success)
 
211
{
 
212
    WebApplet::loadFinished(success);
 
213
    if (success) {
 
214
        page()->mainFrame()->evaluateJavaScript(s_jsConstants);
 
215
        callJsFunction("init");
 
216
    }
 
217
}
 
218
 
 
219
void PlasmaWebApplet::constraintsEvent(Plasma::Constraints constraints)
 
220
{
 
221
    if (page() && constraints & Plasma::SizeConstraint) {
 
222
        qreal left;
 
223
        qreal top;
 
224
        qreal right;
 
225
        qreal bottom;
 
226
        applet()->getContentsMargins(&left, &top, &right, &bottom);
 
227
        view()->setPos(QPointF(left, top));
 
228
        view()->resize(WebApplet::size() - QSizeF(left + right, top + bottom));
 
229
        //kDebug() << WebApplet::size() << left << right << top << bottom << page()->size();
 
230
    }
 
231
    callJsFunction("constraintsEvent", QVariantList() << (int)constraints);
 
232
}
 
233
 
 
234
QVariant PlasmaWebApplet::arg(int index) const
 
235
{
 
236
    return m_args[index];
 
237
}
 
238
 
 
239
QObject* PlasmaWebApplet::objArg(int index) const
 
240
{
 
241
    return m_args[index].value<QObject*>();
 
242
}
 
243
 
 
244
QString PlasmaWebApplet::name() const
 
245
{
 
246
    return applet()->name();
 
247
}
 
248
 
 
249
uint PlasmaWebApplet::id() const
 
250
{
 
251
    return applet()->id();
 
252
}
 
253
 
 
254
int PlasmaWebApplet::formFactor() const
 
255
{
 
256
    return (int)applet()->formFactor();
 
257
}
 
258
 
 
259
int PlasmaWebApplet::location() const
 
260
{
 
261
    return (int)applet()->location();
 
262
}
 
263
 
 
264
QString PlasmaWebApplet::pluginName() const
 
265
{
 
266
    return applet()->pluginName();
 
267
}
 
268
 
 
269
QString PlasmaWebApplet::icon() const
 
270
{
 
271
    return applet()->icon();
 
272
}
 
273
 
 
274
QString PlasmaWebApplet::category() const
 
275
{
 
276
    return applet()->category();
 
277
}
 
278
 
 
279
bool PlasmaWebApplet::shouldConserveResources() const
 
280
{
 
281
    return applet()->shouldConserveResources();
 
282
}
 
283
 
 
284
QStringList PlasmaWebApplet::listAllDataEngines()
 
285
{
 
286
    return Plasma::DataEngineManager::listAllEngines();
 
287
}
 
288
 
 
289
QObject* PlasmaWebApplet::dataEngine(const QString& name)
 
290
{
 
291
    QString id = QString("%1").arg(applet()->id());
 
292
    Plasma::DataEngine *de = applet()->dataEngine(name);
 
293
    DataEngineWrapper *wrapper = de->findChild<DataEngineWrapper*>(id);
 
294
    if (!wrapper) {
 
295
        wrapper = new DataEngineWrapper(de, this);
 
296
        wrapper->setObjectName(id);
 
297
    }
 
298
    return wrapper;
 
299
}
 
300
 
 
301
QObject* PlasmaWebApplet::config()
 
302
{
 
303
    m_config.setConfig(applet()->config());
 
304
    return &m_config;
 
305
}
 
306
 
 
307
QObject* PlasmaWebApplet::globalConfig()
 
308
{
 
309
    m_globalConfig.setConfig(applet()->globalConfig());
 
310
    return &m_globalConfig;
 
311
}
 
312
 
 
313
void PlasmaWebApplet::resize(qreal w, qreal h)
 
314
{
 
315
    applet()->resize(w, h);
 
316
}
 
317
 
 
318
void PlasmaWebApplet::setBackgroundHints(int hints)
 
319
{
 
320
    applet()->setBackgroundHints((Plasma::Applet::BackgroundHints)hints);
 
321
}
 
322
 
 
323
void PlasmaWebApplet::setScrollBarPolicy(int orientation, int policy)
 
324
{
 
325
    page()->mainFrame()->setScrollBarPolicy((Qt::Orientation)orientation,
 
326
                                            (Qt::ScrollBarPolicy)policy);
 
327
}
 
328
 
 
329
void PlasmaWebApplet::setAspectRatioMode(int mode)
 
330
{
 
331
    applet()->setAspectRatioMode((Plasma::AspectRatioMode)mode);
 
332
}
 
333
 
 
334
QVariant PlasmaWebApplet::callJsFunction(const QString& func, const QVariantList& args)
 
335
{
 
336
    if (loaded()) {
 
337
        m_args = args;
 
338
        QString cmd = "if (window." + func + ") { " + func + '(';
 
339
        for(int i = 0; i < args.count(); ++i) {
 
340
            if (i > 0) {
 
341
                cmd += ',';
 
342
            }
 
343
            if (args[i].canConvert<QObject*>()) {
 
344
                cmd += QString("window.plasmoid.objArg(%1)").arg(i);
 
345
            } else {
 
346
                cmd += QString("window.plasmoid.arg(%1)").arg(i);
 
347
            }
 
348
        }
 
349
        cmd += ") }";
 
350
        //kDebug() << cmd;
 
351
        return page()->mainFrame()->evaluateJavaScript(cmd);
 
352
    }
 
353
    return QVariant();
 
354
}
 
355
 
 
356
void PlasmaWebApplet::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
 
357
{
 
358
    m_dataEngineData.setData(data);
 
359
    callJsFunction("dataUpdated",
 
360
                   QVariantList() << source << QVariant::fromValue((QObject*)&m_dataEngineData));
 
361
}
 
362
 
 
363
void PlasmaWebApplet::configChanged()
 
364
{
 
365
    callJsFunction("configChanged");
 
366
}
 
367
 
 
368
void PlasmaWebApplet::initJsObjects()
 
369
{
 
370
    QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
 
371
    Q_ASSERT(frame);
 
372
    frame->addToJavaScriptWindowObject(QLatin1String("plasmoid"), this);
 
373
}
 
374
 
 
375
QVariantList PlasmaWebApplet::geometry()
 
376
{
 
377
    return QVariantList() << applet()->geometry().left() << applet()->geometry().top()
 
378
                          << applet()->geometry().width() << applet()->geometry().height();
 
379
}
 
380
 
 
381
QVariantList PlasmaWebApplet::screenRect()
 
382
{
 
383
    return QVariantList() << applet()->screenRect().left() << applet()->screenRect().top()
 
384
                          << applet()->screenRect().width() << applet()->screenRect().height();
 
385
}
 
386
 
 
387
int PlasmaWebApplet::backgroundHints()
 
388
{
 
389
    return applet()->backgroundHints();
 
390
}
 
391
 
 
392
int PlasmaWebApplet::aspectRatioMode()
 
393
{
 
394
    return applet()->aspectRatioMode();
 
395
}
 
396
 
 
397
void PlasmaWebApplet::setConfigurationRequired(bool needsConfiguring, const QString &reason)
 
398
{
 
399
    WebApplet::setConfigurationRequired(needsConfiguring, reason);
 
400
}
 
401
 
 
402
void PlasmaWebApplet::setMaximumSize(qreal w, qreal h)
 
403
{
 
404
    applet()->setMaximumSize(w, h);
 
405
}
 
406
 
 
407
void PlasmaWebApplet::setMinimumSize(qreal w, qreal h)
 
408
{
 
409
    applet()->setMinimumSize(w, h);
 
410
}
 
411
 
 
412
void PlasmaWebApplet::setPreferredSize(qreal w, qreal h)
 
413
{
 
414
    applet()->setPreferredSize(w, h);
 
415
}
 
416
 
 
417
QVariantList PlasmaWebApplet::maximumSize()
 
418
{
 
419
    return QVariantList() << applet()->maximumSize().width() << applet()->maximumSize().height();
 
420
}
 
421
 
 
422
QVariantList PlasmaWebApplet::minimumSize()
 
423
{
 
424
    return QVariantList() << applet()->minimumSize().width() << applet()->minimumSize().height();
 
425
}
 
426
 
 
427
QVariantList PlasmaWebApplet::preferredSize()
 
428
{
 
429
    return QVariantList() << applet()->preferredSize().width()
 
430
                          << applet()->preferredSize().height();
 
431
}
 
432
 
 
433
QVariantList PlasmaWebApplet::getContentsMargins()
 
434
{
 
435
    qreal left;
 
436
    qreal top;
 
437
    qreal right;
 
438
    qreal bottom;
 
439
    applet()->getContentsMargins(&left, &top, &right, &bottom);
 
440
    return QVariantList() << left << top << right << bottom;
 
441
}
 
442
 
 
443
void PlasmaWebApplet::setGeometry(qreal x, qreal y, qreal w, qreal h)
 
444
{
 
445
    applet()->setGeometry(x, y, w, h);
 
446
}
 
447
 
 
448
void PlasmaWebApplet::setPos(qreal x, qreal y)
 
449
{
 
450
    applet()->setPos(x, y);
 
451
}
 
452
 
 
453
QVariantList PlasmaWebApplet::pos()
 
454
{
 
455
    return QVariantList() << applet()->pos().x() << applet()->pos().y();
 
456
}
 
457
 
 
458
QVariantList PlasmaWebApplet::size()
 
459
{
 
460
    return QVariantList() << applet()->size().width() << applet()->size().height();
 
461
}
 
462
 
 
463
void PlasmaWebApplet::setFailedToLaunch(bool failed, const QString &reason)
 
464
{
 
465
    WebApplet::setFailedToLaunch(failed, reason);
 
466
}
 
467
 
 
468
void PlasmaWebApplet::update()
 
469
{
 
470
    applet()->update();
 
471
}
 
472
 
 
473
bool PlasmaWebApplet::isBusy() const
 
474
{
 
475
    return applet()->isBusy();
 
476
}
 
477
 
 
478
void PlasmaWebApplet::setBusy(bool busy)
 
479
{
 
480
    applet()->setBusy(busy);
 
481
}
 
482
 
 
483
#include "plasmawebapplet.moc"