1// Copyright (C) 2015 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwebview_p.h"
5#include "qwebviewplugin_p.h"
6#include "qwebviewloadrequest_p.h"
7#include "qwebviewfactory_p.h"
8
9
10QT_BEGIN_NAMESPACE
11
12QWebView::QWebView(QObject *p)
13 : QObject(p)
14 , d(QWebViewFactory::createWebView())
15 , m_settings(new QWebViewSettings(d->getSettings()))
16 , m_progress(0)
17{
18 d->setParent(this);
19 qRegisterMetaType<QWebViewLoadRequestPrivate>();
20
21 connect(sender: d, signal: &QAbstractWebView::titleChanged, context: this, slot: &QWebView::onTitleChanged);
22 connect(sender: d, signal: &QAbstractWebView::urlChanged, context: this, slot: &QWebView::onUrlChanged);
23 connect(sender: d, signal: &QAbstractWebView::loadingChanged, context: this, slot: &QWebView::onLoadingChanged);
24 connect(sender: d, signal: &QAbstractWebView::loadProgressChanged, context: this, slot: &QWebView::onLoadProgressChanged);
25 connect(sender: d, signal: &QAbstractWebView::httpUserAgentChanged, context: this, slot: &QWebView::onHttpUserAgentChanged);
26 connect(sender: d, signal: &QAbstractWebView::requestFocus, context: this, slot: &QWebView::requestFocus);
27 connect(sender: d, signal: &QAbstractWebView::javaScriptResult,
28 context: this, slot: &QWebView::javaScriptResult);
29 connect(sender: d, signal: &QAbstractWebView::cookieAdded, context: this, slot: &QWebView::cookieAdded);
30 connect(sender: d, signal: &QAbstractWebView::cookieRemoved, context: this, slot: &QWebView::cookieRemoved);
31}
32
33QWebView::~QWebView()
34{
35}
36
37QString QWebView::httpUserAgent() const
38{
39 if (m_httpUserAgent.isEmpty()){
40 m_httpUserAgent = d->httpUserAgent();
41 }
42 return m_httpUserAgent;
43}
44
45void QWebView::setHttpUserAgent(const QString &userAgent)
46{
47 return d->setHttpUserAgent(userAgent);
48}
49
50QUrl QWebView::url() const
51{
52 return m_url;
53}
54
55void QWebView::setUrl(const QUrl &url)
56{
57 d->setUrl(url);
58}
59
60bool QWebView::canGoBack() const
61{
62 return d->canGoBack();
63}
64
65void QWebView::goBack()
66{
67 d->goBack();
68}
69
70bool QWebView::canGoForward() const
71{
72 return d->canGoForward();
73}
74
75void QWebView::goForward()
76{
77 d->goForward();
78}
79
80void QWebView::reload()
81{
82 d->reload();
83}
84
85void QWebView::stop()
86{
87 d->stop();
88}
89
90QString QWebView::title() const
91{
92 return m_title;
93}
94
95int QWebView::loadProgress() const
96{
97 return m_progress;
98}
99
100bool QWebView::isLoading() const
101{
102 return d->isLoading();
103}
104
105void QWebView::setParentView(QObject *view)
106{
107 d->setParentView(view);
108}
109
110QObject *QWebView::parentView() const
111{
112 return d->parentView();
113}
114
115void QWebView::setGeometry(const QRect &geometry)
116{
117 d->setGeometry(geometry);
118}
119
120void QWebView::setVisibility(QWindow::Visibility visibility)
121{
122 d->setVisibility(visibility);
123}
124
125void QWebView::setVisible(bool visible)
126{
127 d->setVisible(visible);
128}
129
130void QWebView::setFocus(bool focus)
131{
132 d->setFocus(focus);
133}
134
135void QWebView::updatePolish()
136{
137 d->updatePolish();
138
139}
140
141QWebViewSettings *QWebView::getSettings() const
142{
143 return m_settings;
144}
145
146void QWebView::loadHtml(const QString &html, const QUrl &baseUrl)
147{
148 d->loadHtml(html, baseUrl);
149}
150
151void QWebView::runJavaScriptPrivate(const QString &script,
152 int callbackId)
153{
154 d->runJavaScriptPrivate(script, callbackId);
155}
156
157void QWebView::setCookie(const QString &domain, const QString &name, const QString &value)
158{
159 d->setCookie(domain, name, value);
160}
161
162void QWebView::deleteCookie(const QString &domain, const QString &name)
163{
164 d->deleteCookie(domain, name);
165}
166
167void QWebView::deleteAllCookies()
168{
169 d->deleteAllCookies();
170}
171
172void QWebView::onTitleChanged(const QString &title)
173{
174 if (m_title == title)
175 return;
176
177 m_title = title;
178 Q_EMIT titleChanged();
179}
180
181void QWebView::onUrlChanged(const QUrl &url)
182{
183 if (m_url == url)
184 return;
185
186 m_url = url;
187 Q_EMIT urlChanged();
188}
189
190void QWebView::onLoadProgressChanged(int progress)
191{
192 if (m_progress == progress)
193 return;
194
195 m_progress = progress;
196 Q_EMIT loadProgressChanged();
197}
198
199void QWebView::onLoadingChanged(const QWebViewLoadRequestPrivate &loadRequest)
200{
201 if (loadRequest.m_status == QWebView::LoadFailedStatus)
202 m_progress = 0;
203
204 onUrlChanged(url: loadRequest.m_url);
205 Q_EMIT loadingChanged(loadRequest);
206}
207
208void QWebView::onHttpUserAgentChanged(const QString &userAgent)
209{
210 if (m_httpUserAgent == userAgent)
211 return;
212 m_httpUserAgent = userAgent;
213 Q_EMIT httpUserAgentChanged();
214}
215
216void QWebView::init()
217{
218 d->init();
219}
220
221QWebViewSettings::QWebViewSettings(QAbstractWebViewSettings *settings)
222 : d(settings)
223{
224 Q_ASSERT(settings != nullptr);
225}
226
227QWebViewSettings::~QWebViewSettings()
228{
229
230}
231
232bool QWebViewSettings::localStorageEnabled() const
233{
234 return d->localStorageEnabled();
235}
236
237void QWebViewSettings::setLocalStorageEnabled(bool enabled)
238{
239 if (d->localStorageEnabled() == enabled)
240 return;
241
242 d->setLocalStorageEnabled(enabled);
243 emit localStorageEnabledChanged();
244}
245
246bool QWebViewSettings::javaScriptEnabled() const
247{
248 return d->javascriptEnabled();
249}
250
251void QWebViewSettings::setJavaScriptEnabled(bool enabled)
252{
253 if (d->javascriptEnabled() == enabled)
254 return;
255
256 d->setJavascriptEnabled(enabled);
257 emit javaScriptEnabledChanged();
258}
259
260void QWebViewSettings::setAllowFileAccess(bool enabled)
261{
262 if (d->allowFileAccess() == enabled)
263 return;
264
265 d->setAllowFileAccess(enabled);
266 emit allowFileAccessChanged();
267}
268
269bool QWebViewSettings::allowFileAccess() const
270{
271 return d->allowFileAccess();
272}
273
274bool QWebViewSettings::localContentCanAccessFileUrls() const
275{
276 return d->localContentCanAccessFileUrls();
277}
278
279void QWebViewSettings::setLocalContentCanAccessFileUrls(bool enabled)
280{
281 if (d->localContentCanAccessFileUrls() == enabled)
282 return;
283
284 d->setLocalContentCanAccessFileUrls(enabled);
285 emit localContentCanAccessFileUrlsChanged();
286}
287
288QT_END_NAMESPACE
289

source code of qtwebview/src/webview/qwebview.cpp