2
Copyright (c) 2007 Zack Rusin <zack@kde.org>
3
Copyright (c) 2008 Petri Damstén <damu@iki.fi>
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:
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
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
24
#include "plasmawebapplet.h"
30
#include <KColorScheme>
32
#include <Plasma/DataEngineManager>
33
#include <Plasma/Applet>
34
#include <Plasma/Theme>
35
#include <Plasma/WebView>
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"
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"
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"\
61
#define JS_CONSTANTS_ASPECTRATIO \
62
"var InvalidAspectRatioMode = %1;\n"\
63
"var IgnoreAspectRatio = %2;\n"\
64
"var KeepAspectRatio = %3;\n"\
66
"var ConstrainedSquare = %5;\n"\
67
"var FixedSize = %6;\n"
69
#define JS_CONSTANTS_FORMFACTOR \
71
"var MediaCenter = %2;\n"\
72
"var Horizontal = %3;\n"\
73
"var Vertical = %4;\n"\
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"
84
#define JS_CONSTANTS_OTHER \
85
"var size_width = 0;\n"\
86
"var size_height = 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"\
98
#define CSS "body { font-family: %3; font-size: %4pt; color:%1; background-color:%2 }\n"
100
QString PlasmaWebApplet::s_jsConstants;
102
PlasmaWebApplet::PlasmaWebApplet(QObject *parent, const QVariantList &args)
103
: WebApplet(parent, args)
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)
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)
133
.arg(Plasma::ConstrainedSquare)
134
.arg(Plasma::FixedSize);
135
s_jsConstants += QString(JS_CONSTANTS_FORMFACTOR)
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);
151
PlasmaWebApplet::~PlasmaWebApplet()
155
bool PlasmaWebApplet::init()
157
if (WebApplet::init()) {
158
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
159
this, SLOT(themeChanged()));
161
page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
162
QAction *inspectAction = page()->action(QWebPage::InspectElement);
163
inspectAction->setText(i18n("Inspect this widget"));
170
void PlasmaWebApplet::makeStylesheet()
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
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()) {
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);
195
QString css = QString(CSS).arg(textColor.name())
196
.arg(backgroundColor.name())
198
.arg(font.pointSize());
199
m_styleSheetFile.write(css.toUtf8());
200
page()->settings()->setUserStyleSheetUrl(QUrl(m_styleSheetFile.fileName()));
201
m_styleSheetFile.close();
204
void PlasmaWebApplet::themeChanged()
207
callJsFunction("themeChanged");
210
void PlasmaWebApplet::loadFinished(bool success)
212
WebApplet::loadFinished(success);
214
page()->mainFrame()->evaluateJavaScript(s_jsConstants);
215
callJsFunction("init");
219
void PlasmaWebApplet::constraintsEvent(Plasma::Constraints constraints)
221
if (page() && constraints & Plasma::SizeConstraint) {
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();
231
callJsFunction("constraintsEvent", QVariantList() << (int)constraints);
234
QVariant PlasmaWebApplet::arg(int index) const
236
return m_args[index];
239
QObject* PlasmaWebApplet::objArg(int index) const
241
return m_args[index].value<QObject*>();
244
QString PlasmaWebApplet::name() const
246
return applet()->name();
249
uint PlasmaWebApplet::id() const
251
return applet()->id();
254
int PlasmaWebApplet::formFactor() const
256
return (int)applet()->formFactor();
259
int PlasmaWebApplet::location() const
261
return (int)applet()->location();
264
QString PlasmaWebApplet::pluginName() const
266
return applet()->pluginName();
269
QString PlasmaWebApplet::icon() const
271
return applet()->icon();
274
QString PlasmaWebApplet::category() const
276
return applet()->category();
279
bool PlasmaWebApplet::shouldConserveResources() const
281
return applet()->shouldConserveResources();
284
QStringList PlasmaWebApplet::listAllDataEngines()
286
return Plasma::DataEngineManager::listAllEngines();
289
QObject* PlasmaWebApplet::dataEngine(const QString& name)
291
QString id = QString("%1").arg(applet()->id());
292
Plasma::DataEngine *de = applet()->dataEngine(name);
293
DataEngineWrapper *wrapper = de->findChild<DataEngineWrapper*>(id);
295
wrapper = new DataEngineWrapper(de, this);
296
wrapper->setObjectName(id);
301
QObject* PlasmaWebApplet::config()
303
m_config.setConfig(applet()->config());
307
QObject* PlasmaWebApplet::globalConfig()
309
m_globalConfig.setConfig(applet()->globalConfig());
310
return &m_globalConfig;
313
void PlasmaWebApplet::resize(qreal w, qreal h)
315
applet()->resize(w, h);
318
void PlasmaWebApplet::setBackgroundHints(int hints)
320
applet()->setBackgroundHints((Plasma::Applet::BackgroundHints)hints);
323
void PlasmaWebApplet::setScrollBarPolicy(int orientation, int policy)
325
page()->mainFrame()->setScrollBarPolicy((Qt::Orientation)orientation,
326
(Qt::ScrollBarPolicy)policy);
329
void PlasmaWebApplet::setAspectRatioMode(int mode)
331
applet()->setAspectRatioMode((Plasma::AspectRatioMode)mode);
334
QVariant PlasmaWebApplet::callJsFunction(const QString& func, const QVariantList& args)
338
QString cmd = "if (window." + func + ") { " + func + '(';
339
for(int i = 0; i < args.count(); ++i) {
343
if (args[i].canConvert<QObject*>()) {
344
cmd += QString("window.plasmoid.objArg(%1)").arg(i);
346
cmd += QString("window.plasmoid.arg(%1)").arg(i);
351
return page()->mainFrame()->evaluateJavaScript(cmd);
356
void PlasmaWebApplet::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
358
m_dataEngineData.setData(data);
359
callJsFunction("dataUpdated",
360
QVariantList() << source << QVariant::fromValue((QObject*)&m_dataEngineData));
363
void PlasmaWebApplet::configChanged()
365
callJsFunction("configChanged");
368
void PlasmaWebApplet::initJsObjects()
370
QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
372
frame->addToJavaScriptWindowObject(QLatin1String("plasmoid"), this);
375
QVariantList PlasmaWebApplet::geometry()
377
return QVariantList() << applet()->geometry().left() << applet()->geometry().top()
378
<< applet()->geometry().width() << applet()->geometry().height();
381
QVariantList PlasmaWebApplet::screenRect()
383
return QVariantList() << applet()->screenRect().left() << applet()->screenRect().top()
384
<< applet()->screenRect().width() << applet()->screenRect().height();
387
int PlasmaWebApplet::backgroundHints()
389
return applet()->backgroundHints();
392
int PlasmaWebApplet::aspectRatioMode()
394
return applet()->aspectRatioMode();
397
void PlasmaWebApplet::setConfigurationRequired(bool needsConfiguring, const QString &reason)
399
WebApplet::setConfigurationRequired(needsConfiguring, reason);
402
void PlasmaWebApplet::setMaximumSize(qreal w, qreal h)
404
applet()->setMaximumSize(w, h);
407
void PlasmaWebApplet::setMinimumSize(qreal w, qreal h)
409
applet()->setMinimumSize(w, h);
412
void PlasmaWebApplet::setPreferredSize(qreal w, qreal h)
414
applet()->setPreferredSize(w, h);
417
QVariantList PlasmaWebApplet::maximumSize()
419
return QVariantList() << applet()->maximumSize().width() << applet()->maximumSize().height();
422
QVariantList PlasmaWebApplet::minimumSize()
424
return QVariantList() << applet()->minimumSize().width() << applet()->minimumSize().height();
427
QVariantList PlasmaWebApplet::preferredSize()
429
return QVariantList() << applet()->preferredSize().width()
430
<< applet()->preferredSize().height();
433
QVariantList PlasmaWebApplet::getContentsMargins()
439
applet()->getContentsMargins(&left, &top, &right, &bottom);
440
return QVariantList() << left << top << right << bottom;
443
void PlasmaWebApplet::setGeometry(qreal x, qreal y, qreal w, qreal h)
445
applet()->setGeometry(x, y, w, h);
448
void PlasmaWebApplet::setPos(qreal x, qreal y)
450
applet()->setPos(x, y);
453
QVariantList PlasmaWebApplet::pos()
455
return QVariantList() << applet()->pos().x() << applet()->pos().y();
458
QVariantList PlasmaWebApplet::size()
460
return QVariantList() << applet()->size().width() << applet()->size().height();
463
void PlasmaWebApplet::setFailedToLaunch(bool failed, const QString &reason)
465
WebApplet::setFailedToLaunch(failed, reason);
468
void PlasmaWebApplet::update()
473
bool PlasmaWebApplet::isBusy() const
475
return applet()->isBusy();
478
void PlasmaWebApplet::setBusy(bool busy)
480
applet()->setBusy(busy);
483
#include "plasmawebapplet.moc"