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

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/webkit/webapplet.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
 
 
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:
 
10
 
 
11
The above copyright notice and this permission notice shall be included in
 
12
all copies or substantial portions of the Software.
 
13
 
 
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.
 
21
 */
 
22
#include "webapplet.h"
 
23
 
 
24
#include "webpage.h"
 
25
 
 
26
#include <QPainter>
 
27
#include <QWebView>
 
28
#include <QWebFrame>
 
29
#include <QWebPage>
 
30
#include <QFile>
 
31
 
 
32
#include <KDebug>
 
33
 
 
34
#include <Plasma/Applet>
 
35
#include <Plasma/Package>
 
36
#include <Plasma/WebView>
 
37
 
 
38
using namespace Plasma;
 
39
 
 
40
class WebApplet::Private
 
41
{
 
42
public:
 
43
    Private()
 
44
        : view(0)
 
45
    {
 
46
    }
 
47
 
 
48
    void init(WebApplet *q)
 
49
    {
 
50
        loaded = false;
 
51
 
 
52
        Plasma::Applet *applet = q->applet();
 
53
        applet->setAcceptsHoverEvents(true);
 
54
 
 
55
        view = new Plasma::WebView(applet);
 
56
        QObject::connect(view, SIGNAL(loadFinished(bool)),
 
57
                         q, SLOT(loadFinished(bool)));
 
58
        QObject::connect(view->page(), SIGNAL(frameCreated(QWebFrame *)),
 
59
                         q, SLOT(connectFrame(QWebFrame *)));
 
60
        q->connectFrame(view->mainFrame());
 
61
 
 
62
        view->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
 
63
        view->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
 
64
 
 
65
        QPalette palette = view->palette();
 
66
        palette.setColor(QPalette::Base, Qt::transparent);
 
67
        view->page()->setPalette(palette);
 
68
    }
 
69
 
 
70
    Plasma::WebView *view;
 
71
    bool loaded;
 
72
};
 
73
 
 
74
WebApplet::WebApplet(QObject *parent, const QVariantList &args)
 
75
    : AppletScript(parent),
 
76
      d(new Private)
 
77
{
 
78
    Q_UNUSED(args)
 
79
}
 
80
 
 
81
WebApplet::~WebApplet()
 
82
{
 
83
    delete d;
 
84
}
 
85
 
 
86
bool WebApplet::init()
 
87
{
 
88
    d->init(this);
 
89
 
 
90
    QString webpage;
 
91
    webpage = package()->filePath("mainscript");
 
92
 
 
93
    if (webpage.isEmpty()) {
 
94
        kDebug() << "fail! no page";
 
95
        delete d->view;
 
96
        d->view = 0;
 
97
        return false;
 
98
    }
 
99
 
 
100
    KUrl url(package()->filePath("html"));
 
101
    kDebug() << webpage << package()->path() << url;
 
102
    d->view->mainFrame()->setHtml(dataFor(webpage), url);
 
103
    return true;
 
104
}
 
105
 
 
106
void WebApplet::paintInterface(QPainter *painter,
 
107
                               const QStyleOptionGraphicsItem *option,
 
108
                               const QRect &contentsRect)
 
109
{
 
110
    Q_UNUSED(painter)
 
111
    Q_UNUSED(option)
 
112
    Q_UNUSED(contentsRect)
 
113
}
 
114
 
 
115
Plasma::WebView* WebApplet::view() const
 
116
{
 
117
    return d->view;
 
118
}
 
119
 
 
120
void WebApplet::loadFinished(bool success)
 
121
{
 
122
    d->loaded = success;
 
123
}
 
124
 
 
125
void WebApplet::connectFrame(QWebFrame *frame)
 
126
{
 
127
    connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
 
128
            this, SLOT(initJsObjects()));
 
129
}
 
130
 
 
131
void WebApplet::initJsObjects()
 
132
{
 
133
}
 
134
 
 
135
QByteArray WebApplet::dataFor(const QString &str)
 
136
{
 
137
    QFile f(str);
 
138
    f.open(QIODevice::ReadOnly);
 
139
    QByteArray data = f.readAll();
 
140
    f.close();
 
141
    return data;
 
142
}
 
143
 
 
144
QWebPage *WebApplet::page()
 
145
{
 
146
    return d->view ? d->view->page() : 0;
 
147
}
 
148
 
 
149
bool WebApplet::loaded()
 
150
{
 
151
    return d->loaded;
 
152
}
 
153
 
 
154
#include "webapplet.moc"