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 : QAbstractWebView(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::javaScriptResult,
27 context: this, slot: &QWebView::javaScriptResult);
28 connect(sender: d, signal: &QAbstractWebView::cookieAdded, context: this, slot: &QWebView::cookieAdded);
29 connect(sender: d, signal: &QAbstractWebView::cookieRemoved, context: this, slot: &QWebView::cookieRemoved);
30}
31
32QWebView::~QWebView()
33{
34}
35
36QString QWebView::httpUserAgent() const
37{
38 if (m_httpUserAgent.isEmpty()){
39 m_httpUserAgent = d->httpUserAgent();
40 }
41 return m_httpUserAgent;
42}
43
44void QWebView::setHttpUserAgent(const QString &userAgent)
45{
46 return d->setHttpUserAgent(userAgent);
47}
48
49QUrl QWebView::url() const
50{
51 return m_url;
52}
53
54void QWebView::setUrl(const QUrl &url)
55{
56 d->setUrl(url);
57}
58
59bool QWebView::canGoBack() const
60{
61 return d->canGoBack();
62}
63
64void QWebView::goBack()
65{
66 d->goBack();
67}
68
69bool QWebView::canGoForward() const
70{
71 return d->canGoForward();
72}
73
74void QWebView::goForward()
75{
76 d->goForward();
77}
78
79void QWebView::reload()
80{
81 d->reload();
82}
83
84void QWebView::stop()
85{
86 d->stop();
87}
88
89QString QWebView::title() const
90{
91 return m_title;
92}
93
94int QWebView::loadProgress() const
95{
96 return m_progress;
97}
98
99bool QWebView::isLoading() const
100{
101 return d->isLoading();
102}
103
104QWebViewSettings *QWebView::getSettings() const
105{
106 return m_settings;
107}
108
109QWindow *QWebView::nativeWindow() const
110{
111 return d->nativeWindow();
112}
113
114void QWebView::loadHtml(const QString &html, const QUrl &baseUrl)
115{
116 d->loadHtml(html, baseUrl);
117}
118
119void QWebView::runJavaScriptPrivate(const QString &script,
120 int callbackId)
121{
122 d->runJavaScriptPrivate(script, callbackId);
123}
124
125void QWebView::setCookie(const QString &domain, const QString &name, const QString &value)
126{
127 d->setCookie(domain, name, value);
128}
129
130void QWebView::deleteCookie(const QString &domain, const QString &name)
131{
132 d->deleteCookie(domain, name);
133}
134
135void QWebView::deleteAllCookies()
136{
137 d->deleteAllCookies();
138}
139
140void QWebView::onTitleChanged(const QString &title)
141{
142 if (m_title == title)
143 return;
144
145 m_title = title;
146 Q_EMIT titleChanged();
147}
148
149void QWebView::onUrlChanged(const QUrl &url)
150{
151 if (m_url == url)
152 return;
153
154 m_url = url;
155 Q_EMIT urlChanged();
156}
157
158void QWebView::onLoadProgressChanged(int progress)
159{
160 if (m_progress == progress)
161 return;
162
163 m_progress = progress;
164 Q_EMIT loadProgressChanged();
165}
166
167void QWebView::onLoadingChanged(const QWebViewLoadRequestPrivate &loadRequest)
168{
169 if (loadRequest.m_status == QWebView::LoadFailedStatus)
170 m_progress = 0;
171
172 onUrlChanged(url: loadRequest.m_url);
173 Q_EMIT loadingChanged(loadRequest);
174}
175
176void QWebView::onHttpUserAgentChanged(const QString &userAgent)
177{
178 if (m_httpUserAgent == userAgent)
179 return;
180 m_httpUserAgent = userAgent;
181 Q_EMIT httpUserAgentChanged();
182}
183
184QWebViewSettings::QWebViewSettings(QAbstractWebViewSettings *settings)
185 : d(settings)
186{
187 Q_ASSERT(settings != nullptr);
188}
189
190QWebViewSettings::~QWebViewSettings()
191{
192
193}
194
195bool QWebViewSettings::localStorageEnabled() const
196{
197 return d->localStorageEnabled();
198}
199
200void QWebViewSettings::setLocalStorageEnabled(bool enabled)
201{
202 if (d->localStorageEnabled() == enabled)
203 return;
204
205 d->setLocalStorageEnabled(enabled);
206 emit localStorageEnabledChanged();
207}
208
209bool QWebViewSettings::javaScriptEnabled() const
210{
211 return d->javaScriptEnabled();
212}
213
214void QWebViewSettings::setJavaScriptEnabled(bool enabled)
215{
216 if (d->javaScriptEnabled() == enabled)
217 return;
218
219 d->setJavaScriptEnabled(enabled);
220 emit javaScriptEnabledChanged();
221}
222
223void QWebViewSettings::setAllowFileAccess(bool enabled)
224{
225 if (d->allowFileAccess() == enabled)
226 return;
227
228 d->setAllowFileAccess(enabled);
229 emit allowFileAccessChanged();
230}
231
232bool QWebViewSettings::allowFileAccess() const
233{
234 return d->allowFileAccess();
235}
236
237bool QWebViewSettings::localContentCanAccessFileUrls() const
238{
239 return d->localContentCanAccessFileUrls();
240}
241
242void QWebViewSettings::setLocalContentCanAccessFileUrls(bool enabled)
243{
244 if (d->localContentCanAccessFileUrls() == enabled)
245 return;
246
247 d->setLocalContentCanAccessFileUrls(enabled);
248 emit localContentCanAccessFileUrlsChanged();
249}
250
251QT_END_NAMESPACE
252

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