add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / webkit / webapplet.cpp
blobbff28e5cc38f4bc9fa94a565aab2a16866a53d63
1 /*
2 Copyright (c) 2007 Zack Rusin <zack@kde.org>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
22 #include "webapplet.h"
24 #include "webpage.h"
26 #include <QPainter>
27 #include <QWebView>
28 #include <QWebFrame>
29 #include <QWebPage>
30 #include <QFile>
32 #include <Plasma/Applet>
33 #include <Plasma/Package>
34 #include <Plasma/WebView>
36 using namespace Plasma;
38 class WebApplet::Private
40 public:
41 Private()
42 : page(0)
46 void init(WebApplet *q)
48 loaded = false;
50 Plasma::Applet *applet = q->applet();
51 applet->setAcceptsHoverEvents(true);
53 page = new Plasma::WebView(applet);
54 page->setPage(new WebPage(page));
55 QObject::connect(page, SIGNAL(loadFinished(bool)),
56 q, SLOT(loadFinished(bool)));
57 QObject::connect(page->page(), SIGNAL(frameCreated(QWebFrame *)),
58 q, SLOT(connectFrame(QWebFrame *)));
59 q->connectFrame(page->mainFrame());
61 page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
62 page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
64 QPalette palette = page->palette();
65 palette.setBrush(QPalette::Background, QBrush(Qt::transparent));
66 page->setPalette(palette);
69 Plasma::WebView *page;
70 bool loaded;
73 WebApplet::WebApplet(QObject *parent, const QVariantList &args)
74 : AppletScript(parent),
75 d(new Private)
77 Q_UNUSED(args)
80 WebApplet::~WebApplet()
82 delete d;
85 bool WebApplet::init()
87 d->init(this);
89 QString webpage;
90 webpage = package()->filePath("mainscript");
91 //kDebug() << "webpage is at" << webpage;
92 if (webpage.isEmpty()) {
93 return false;
95 //kDebug() << package()->path() << package()->filePath("root");
96 d->page->mainFrame()->setHtml(dataFor(webpage), QUrl(package()->filePath("root")));
97 return true;
100 void WebApplet::paintInterface(QPainter *painter,
101 const QStyleOptionGraphicsItem *option,
102 const QRect &contentsRect)
104 Q_UNUSED(painter)
105 Q_UNUSED(option)
106 Q_UNUSED(contentsRect)
109 Plasma::WebView* WebApplet::view() const
111 return d->page;
114 void WebApplet::loadFinished(bool success)
116 d->loaded = success;
119 void WebApplet::connectFrame(QWebFrame *frame)
121 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
122 this, SLOT(initJsObjects()));
125 void WebApplet::initJsObjects()
129 QByteArray WebApplet::dataFor(const QString &str)
131 QFile f(str);
132 f.open(QIODevice::ReadOnly);
133 QByteArray data = f.readAll();
134 f.close();
135 return data;
138 Plasma::WebView* WebApplet::page()
140 return d->page;
143 bool WebApplet::loaded()
145 return d->loaded;
148 #include "webapplet.moc"