1/*
2    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20
21#ifndef qdeclarativewebview_p_h
22#define qdeclarativewebview_p_h
23
24#include <QtCore/QBasicTimer>
25#include <QtCore/QUrl>
26#include <QtDeclarative/QDeclarativeItem>
27#include <QtGui/QAction>
28#include <QtNetwork/QNetworkAccessManager>
29#include "qgraphicswebview.h"
30#include "qwebpage.h"
31
32
33QT_BEGIN_HEADER
34
35class QWebHistory;
36class QWebSettings;
37
38QT_BEGIN_NAMESPACE
39
40QT_MODULE(Declarative)
41class QDeclarativeWebSettings;
42class QDeclarativeWebViewPrivate;
43class QNetworkRequest;
44class QDeclarativeWebView;
45class QDeclarativeWebViewPrivate;
46
47class QDeclarativeWebPage : public QWebPage {
48    Q_OBJECT
49public:
50    explicit QDeclarativeWebPage(QDeclarativeWebView *parent);
51    ~QDeclarativeWebPage();
52protected:
53    QWebPage *createWindow(WebWindowType type);
54    QString chooseFile(QWebFrame *originatingFrame, const QString& oldFile);
55    void javaScriptAlert(QWebFrame *originatingFrame, const QString& msg);
56    bool javaScriptConfirm(QWebFrame *originatingFrame, const QString& msg);
57    bool javaScriptPrompt(QWebFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result);
58
59private:
60    QDeclarativeWebView *viewItem();
61};
62
63class GraphicsWebView : public QGraphicsWebView {
64    Q_OBJECT
65public:
66    GraphicsWebView(QDeclarativeWebView* parent = 0);
67protected:
68    void mousePressEvent(QGraphicsSceneMouseEvent* event);
69    void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
70    void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
71    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
72    void timerEvent(QTimerEvent* event);
73    bool sceneEvent(QEvent *event);
74
75Q_SIGNALS:
76    void doubleClick(int clickX, int clickY);
77private:
78    QDeclarativeWebView *parent;
79    QPointF pressPoint;
80    QBasicTimer pressTimer;
81    int pressTime; // milliseconds before the touch event becomes a "tap and hold"
82    friend class QDeclarativeWebView;
83};
84
85class QDeclarativeWebViewAttached;
86
87// TODO: browser plugins
88
89class QDeclarativeWebView : public QDeclarativeItem {
90    Q_OBJECT
91
92    Q_ENUMS(Status SelectionMode)
93
94    Q_PROPERTY(QString title READ title NOTIFY titleChanged)
95    Q_PROPERTY(QPixmap icon READ icon NOTIFY iconChanged)
96    Q_PROPERTY(QString statusText READ statusText NOTIFY statusTextChanged)
97
98    Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged)
99
100    Q_PROPERTY(int pressGrabTime READ pressGrabTime WRITE setPressGrabTime NOTIFY pressGrabTimeChanged)
101
102    Q_PROPERTY(int preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged)
103    Q_PROPERTY(int preferredHeight READ preferredHeight WRITE setPreferredHeight NOTIFY preferredHeightChanged)
104    Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
105    Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
106    Q_PROPERTY(Status status READ status NOTIFY statusChanged)
107
108#ifndef QT_NO_ACTION
109    Q_PROPERTY(QAction* reload READ reloadAction CONSTANT)
110    Q_PROPERTY(QAction* back READ backAction CONSTANT)
111    Q_PROPERTY(QAction* forward READ forwardAction CONSTANT)
112    Q_PROPERTY(QAction* stop READ stopAction CONSTANT)
113#endif
114
115    Q_PROPERTY(QDeclarativeWebSettings* settings READ settingsObject CONSTANT)
116
117    Q_PROPERTY(QDeclarativeListProperty<QObject> javaScriptWindowObjects READ javaScriptWindowObjects CONSTANT)
118
119    Q_PROPERTY(QDeclarativeComponent* newWindowComponent READ newWindowComponent WRITE setNewWindowComponent NOTIFY newWindowComponentChanged)
120    Q_PROPERTY(QDeclarativeItem* newWindowParent READ newWindowParent WRITE setNewWindowParent NOTIFY newWindowParentChanged)
121
122    Q_PROPERTY(bool renderingEnabled READ renderingEnabled WRITE setRenderingEnabled NOTIFY renderingEnabledChanged)
123
124    Q_PROPERTY(QSize contentsSize READ contentsSize NOTIFY contentsSizeChanged)
125    Q_PROPERTY(qreal contentsScale READ contentsScale WRITE setContentsScale NOTIFY contentsScaleChanged)
126#if QT_VERSION >= 0x040703
127    Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged REVISION 1)
128#endif
129
130public:
131    QDeclarativeWebView(QDeclarativeItem *parent = 0);
132    ~QDeclarativeWebView();
133
134    QUrl url() const;
135    void setUrl(const QUrl &);
136
137    QString title() const;
138
139    QPixmap icon() const;
140
141    Q_INVOKABLE bool heuristicZoom(int clickX, int clickY, qreal maxzoom);
142    QRect elementAreaAt(int x, int y, int minwidth, int minheight) const;
143
144    int pressGrabTime() const;
145    void setPressGrabTime(int);
146
147    int preferredWidth() const;
148    void setPreferredWidth(int);
149    int preferredHeight() const;
150    void setPreferredHeight(int);
151
152    enum Status { Null, Ready, Loading, Error };
153    Status status() const;
154    qreal progress() const;
155    QString statusText() const;
156
157#ifndef QT_NO_ACTION
158    QAction *reloadAction() const;
159    QAction *backAction() const;
160    QAction *forwardAction() const;
161    QAction *stopAction() const;
162#endif
163
164    QWebPage *page() const;
165    void setPage(QWebPage *page);
166
167    void load(const QNetworkRequest &request,
168              QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation,
169              const QByteArray &body = QByteArray());
170
171    QString html() const;
172
173    void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
174    void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
175
176    QWebHistory* history() const;
177    QWebSettings* settings() const;
178    QDeclarativeWebSettings *settingsObject() const;
179
180    bool renderingEnabled() const;
181    void setRenderingEnabled(bool);
182
183    QDeclarativeListProperty<QObject> javaScriptWindowObjects();
184
185    static QDeclarativeWebViewAttached* qmlAttachedProperties(QObject*);
186
187    QDeclarativeComponent *newWindowComponent() const;
188    void setNewWindowComponent(QDeclarativeComponent *newWindow);
189    QDeclarativeItem* newWindowParent() const;
190    void setNewWindowParent(QDeclarativeItem* newWindow);
191
192    bool isComponentCompletePublic() const { return isComponentComplete(); }
193
194    QSize contentsSize() const;
195
196    void setContentsScale(qreal scale);
197    qreal contentsScale() const;
198
199#if QT_VERSION >= 0x040703
200    Q_REVISION(1) QColor backgroundColor() const;
201    Q_REVISION(1) void setBackgroundColor(const QColor&);
202#endif
203
204Q_SIGNALS:
205    void preferredWidthChanged();
206    void preferredHeightChanged();
207    void urlChanged();
208    void progressChanged();
209    void statusChanged(Status);
210    void titleChanged(const QString&);
211    void iconChanged();
212    void statusTextChanged();
213    void htmlChanged();
214    void pressGrabTimeChanged();
215    void newWindowComponentChanged();
216    void newWindowParentChanged();
217    void renderingEnabledChanged();
218    void contentsSizeChanged(const QSize&);
219    void contentsScaleChanged();
220#if QT_VERSION >= 0x040703
221    void backgroundColorChanged();
222#endif
223
224    void loadStarted();
225    void loadFinished();
226    void loadFailed();
227
228    void doubleClick(int clickX, int clickY);
229
230    void zoomTo(qreal zoom, int centerX, int centerY);
231
232    void alert(const QString& message);
233
234public Q_SLOTS:
235    QVariant evaluateJavaScript(const QString&);
236
237private Q_SLOTS:
238    void doLoadStarted();
239    void doLoadProgress(int p);
240    void doLoadFinished(bool ok);
241    void setStatusText(const QString&);
242    void windowObjectCleared();
243    void pageUrlChanged();
244    void initialLayout();
245
246    void updateDeclarativeWebViewSize();
247
248    virtual void geometryChanged(const QRectF &newGeometry,
249                                 const QRectF &oldGeometry);
250    QDeclarativeWebView* createWindow(QWebPage::WebWindowType type);
251
252private:
253    void updateContentsSize();
254    void init();
255    virtual void componentComplete();
256    Q_DISABLE_COPY(QDeclarativeWebView)
257    QDeclarativeWebViewPrivate* d;
258    QMouseEvent* sceneMouseEventToMouseEvent(QGraphicsSceneMouseEvent*);
259    QMouseEvent* sceneHoverMoveEventToMouseEvent(QGraphicsSceneHoverEvent*);
260    friend class QDeclarativeWebPage;
261};
262
263class QDeclarativeWebViewAttached : public QObject {
264    Q_OBJECT
265    Q_PROPERTY(QString windowObjectName READ windowObjectName WRITE setWindowObjectName)
266public:
267    QDeclarativeWebViewAttached(QObject* parent)
268        : QObject(parent)
269    {
270    }
271
272    QString windowObjectName() const
273    {
274        return m_windowObjectName;
275    }
276
277    void setWindowObjectName(const QString &n)
278    {
279        m_windowObjectName = n;
280    }
281
282private:
283    QString m_windowObjectName;
284};
285
286class QDeclarativeWebSettings : public QObject {
287    Q_OBJECT
288
289    Q_PROPERTY(QString standardFontFamily READ standardFontFamily WRITE setStandardFontFamily)
290    Q_PROPERTY(QString fixedFontFamily READ fixedFontFamily WRITE setFixedFontFamily)
291    Q_PROPERTY(QString serifFontFamily READ serifFontFamily WRITE setSerifFontFamily)
292    Q_PROPERTY(QString sansSerifFontFamily READ sansSerifFontFamily WRITE setSansSerifFontFamily)
293    Q_PROPERTY(QString cursiveFontFamily READ cursiveFontFamily WRITE setCursiveFontFamily)
294    Q_PROPERTY(QString fantasyFontFamily READ fantasyFontFamily WRITE setFantasyFontFamily)
295
296    Q_PROPERTY(int minimumFontSize READ minimumFontSize WRITE setMinimumFontSize)
297    Q_PROPERTY(int minimumLogicalFontSize READ minimumLogicalFontSize WRITE setMinimumLogicalFontSize)
298    Q_PROPERTY(int defaultFontSize READ defaultFontSize WRITE setDefaultFontSize)
299    Q_PROPERTY(int defaultFixedFontSize READ defaultFixedFontSize WRITE setDefaultFixedFontSize)
300
301    Q_PROPERTY(bool autoLoadImages READ autoLoadImages WRITE setAutoLoadImages)
302    Q_PROPERTY(bool javascriptEnabled READ javascriptEnabled WRITE setJavascriptEnabled)
303    Q_PROPERTY(bool javaEnabled READ javaEnabled WRITE setJavaEnabled)
304    Q_PROPERTY(bool pluginsEnabled READ pluginsEnabled WRITE setPluginsEnabled)
305    Q_PROPERTY(bool privateBrowsingEnabled READ privateBrowsingEnabled WRITE setPrivateBrowsingEnabled)
306    Q_PROPERTY(bool javascriptCanOpenWindows READ javascriptCanOpenWindows WRITE setJavascriptCanOpenWindows)
307    Q_PROPERTY(bool javascriptCanAccessClipboard READ javascriptCanAccessClipboard WRITE setJavascriptCanAccessClipboard)
308    Q_PROPERTY(bool developerExtrasEnabled READ developerExtrasEnabled WRITE setDeveloperExtrasEnabled)
309    Q_PROPERTY(bool linksIncludedInFocusChain READ linksIncludedInFocusChain WRITE setLinksIncludedInFocusChain)
310    Q_PROPERTY(bool zoomTextOnly READ zoomTextOnly WRITE setZoomTextOnly)
311    Q_PROPERTY(bool printElementBackgrounds READ printElementBackgrounds WRITE setPrintElementBackgrounds)
312    Q_PROPERTY(bool offlineStorageDatabaseEnabled READ offlineStorageDatabaseEnabled WRITE setOfflineStorageDatabaseEnabled)
313    Q_PROPERTY(bool offlineWebApplicationCacheEnabled READ offlineWebApplicationCacheEnabled WRITE setOfflineWebApplicationCacheEnabled)
314    Q_PROPERTY(bool localStorageDatabaseEnabled READ localStorageDatabaseEnabled WRITE setLocalStorageDatabaseEnabled)
315    Q_PROPERTY(bool localContentCanAccessRemoteUrls READ localContentCanAccessRemoteUrls WRITE setLocalContentCanAccessRemoteUrls)
316
317public:
318    QDeclarativeWebSettings() {}
319
320    QString standardFontFamily() const { return s->fontFamily(QWebSettings::StandardFont); }
321    void setStandardFontFamily(const QString& f) { s->setFontFamily(QWebSettings::StandardFont, f); }
322    QString fixedFontFamily() const { return s->fontFamily(QWebSettings::FixedFont); }
323    void setFixedFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FixedFont, f); }
324    QString serifFontFamily() const { return s->fontFamily(QWebSettings::SerifFont); }
325    void setSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SerifFont, f); }
326    QString sansSerifFontFamily() const { return s->fontFamily(QWebSettings::SansSerifFont); }
327    void setSansSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SansSerifFont, f); }
328    QString cursiveFontFamily() const { return s->fontFamily(QWebSettings::CursiveFont); }
329    void setCursiveFontFamily(const QString& f) { s->setFontFamily(QWebSettings::CursiveFont, f); }
330    QString fantasyFontFamily() const { return s->fontFamily(QWebSettings::FantasyFont); }
331    void setFantasyFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FantasyFont, f); }
332
333    int minimumFontSize() const { return s->fontSize(QWebSettings::MinimumFontSize); }
334    void setMinimumFontSize(int size) { s->setFontSize(QWebSettings::MinimumFontSize, size); }
335    int minimumLogicalFontSize() const { return s->fontSize(QWebSettings::MinimumLogicalFontSize); }
336    void setMinimumLogicalFontSize(int size) { s->setFontSize(QWebSettings::MinimumLogicalFontSize, size); }
337    int defaultFontSize() const { return s->fontSize(QWebSettings::DefaultFontSize); }
338    void setDefaultFontSize(int size) { s->setFontSize(QWebSettings::DefaultFontSize, size); }
339    int defaultFixedFontSize() const { return s->fontSize(QWebSettings::DefaultFixedFontSize); }
340    void setDefaultFixedFontSize(int size) { s->setFontSize(QWebSettings::DefaultFixedFontSize, size); }
341
342    bool autoLoadImages() const { return s->testAttribute(QWebSettings::AutoLoadImages); }
343    void setAutoLoadImages(bool on) { s->setAttribute(QWebSettings::AutoLoadImages, on); }
344    bool javascriptEnabled() const { return s->testAttribute(QWebSettings::JavascriptEnabled); }
345    void setJavascriptEnabled(bool on) { s->setAttribute(QWebSettings::JavascriptEnabled, on); }
346    bool javaEnabled() const { return s->testAttribute(QWebSettings::JavaEnabled); }
347    void setJavaEnabled(bool on) { s->setAttribute(QWebSettings::JavaEnabled, on); }
348    bool pluginsEnabled() const { return s->testAttribute(QWebSettings::PluginsEnabled); }
349    void setPluginsEnabled(bool on) { s->setAttribute(QWebSettings::PluginsEnabled, on); }
350    bool privateBrowsingEnabled() const { return s->testAttribute(QWebSettings::PrivateBrowsingEnabled); }
351    void setPrivateBrowsingEnabled(bool on) { s->setAttribute(QWebSettings::PrivateBrowsingEnabled, on); }
352    bool javascriptCanOpenWindows() const { return s->testAttribute(QWebSettings::JavascriptCanOpenWindows); }
353    void setJavascriptCanOpenWindows(bool on) { s->setAttribute(QWebSettings::JavascriptCanOpenWindows, on); }
354    bool javascriptCanAccessClipboard() const { return s->testAttribute(QWebSettings::JavascriptCanAccessClipboard); }
355    void setJavascriptCanAccessClipboard(bool on) { s->setAttribute(QWebSettings::JavascriptCanAccessClipboard, on); }
356    bool developerExtrasEnabled() const { return s->testAttribute(QWebSettings::DeveloperExtrasEnabled); }
357    void setDeveloperExtrasEnabled(bool on) { s->setAttribute(QWebSettings::DeveloperExtrasEnabled, on); }
358    bool linksIncludedInFocusChain() const { return s->testAttribute(QWebSettings::LinksIncludedInFocusChain); }
359    void setLinksIncludedInFocusChain(bool on) { s->setAttribute(QWebSettings::LinksIncludedInFocusChain, on); }
360    bool zoomTextOnly() const { return s->testAttribute(QWebSettings::ZoomTextOnly); }
361    void setZoomTextOnly(bool on) { s->setAttribute(QWebSettings::ZoomTextOnly, on); }
362    bool printElementBackgrounds() const { return s->testAttribute(QWebSettings::PrintElementBackgrounds); }
363    void setPrintElementBackgrounds(bool on) { s->setAttribute(QWebSettings::PrintElementBackgrounds, on); }
364    bool offlineStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled); }
365    void setOfflineStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, on); }
366    bool offlineWebApplicationCacheEnabled() const { return s->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled); }
367    void setOfflineWebApplicationCacheEnabled(bool on) { s->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, on); }
368    bool localStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::LocalStorageDatabaseEnabled); }
369    void setLocalStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, on); }
370    bool localContentCanAccessRemoteUrls() const { return s->testAttribute(QWebSettings::LocalContentCanAccessRemoteUrls); }
371    void setLocalContentCanAccessRemoteUrls(bool on) { s->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, on); }
372
373    QWebSettings *s;
374};
375
376QT_END_NAMESPACE
377
378QML_DECLARE_TYPE(QDeclarativeWebView)
379QML_DECLARE_TYPE(QDeclarativeWebSettings)
380QML_DECLARE_TYPEINFO(QDeclarativeWebView, QML_HAS_ATTACHED_PROPERTIES)
381
382QT_END_HEADER
383
384#endif
385