| 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 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QWebView::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 | |
| 33 | QWebView::~QWebView() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | QString QWebView::httpUserAgent() const |
| 38 | { |
| 39 | if (m_httpUserAgent.isEmpty()){ |
| 40 | m_httpUserAgent = d->httpUserAgent(); |
| 41 | } |
| 42 | return m_httpUserAgent; |
| 43 | } |
| 44 | |
| 45 | void QWebView::setHttpUserAgent(const QString &userAgent) |
| 46 | { |
| 47 | return d->setHttpUserAgent(userAgent); |
| 48 | } |
| 49 | |
| 50 | QUrl QWebView::url() const |
| 51 | { |
| 52 | return m_url; |
| 53 | } |
| 54 | |
| 55 | void QWebView::setUrl(const QUrl &url) |
| 56 | { |
| 57 | d->setUrl(url); |
| 58 | } |
| 59 | |
| 60 | bool QWebView::canGoBack() const |
| 61 | { |
| 62 | return d->canGoBack(); |
| 63 | } |
| 64 | |
| 65 | void QWebView::goBack() |
| 66 | { |
| 67 | d->goBack(); |
| 68 | } |
| 69 | |
| 70 | bool QWebView::canGoForward() const |
| 71 | { |
| 72 | return d->canGoForward(); |
| 73 | } |
| 74 | |
| 75 | void QWebView::goForward() |
| 76 | { |
| 77 | d->goForward(); |
| 78 | } |
| 79 | |
| 80 | void QWebView::reload() |
| 81 | { |
| 82 | d->reload(); |
| 83 | } |
| 84 | |
| 85 | void QWebView::stop() |
| 86 | { |
| 87 | d->stop(); |
| 88 | } |
| 89 | |
| 90 | QString QWebView::title() const |
| 91 | { |
| 92 | return m_title; |
| 93 | } |
| 94 | |
| 95 | int QWebView::loadProgress() const |
| 96 | { |
| 97 | return m_progress; |
| 98 | } |
| 99 | |
| 100 | bool QWebView::isLoading() const |
| 101 | { |
| 102 | return d->isLoading(); |
| 103 | } |
| 104 | |
| 105 | void QWebView::setParentView(QObject *view) |
| 106 | { |
| 107 | d->setParentView(view); |
| 108 | } |
| 109 | |
| 110 | QObject *QWebView::parentView() const |
| 111 | { |
| 112 | return d->parentView(); |
| 113 | } |
| 114 | |
| 115 | void QWebView::setGeometry(const QRect &geometry) |
| 116 | { |
| 117 | d->setGeometry(geometry); |
| 118 | } |
| 119 | |
| 120 | void QWebView::setVisibility(QWindow::Visibility visibility) |
| 121 | { |
| 122 | d->setVisibility(visibility); |
| 123 | } |
| 124 | |
| 125 | void QWebView::setVisible(bool visible) |
| 126 | { |
| 127 | d->setVisible(visible); |
| 128 | } |
| 129 | |
| 130 | void QWebView::setFocus(bool focus) |
| 131 | { |
| 132 | d->setFocus(focus); |
| 133 | } |
| 134 | |
| 135 | void QWebView::updatePolish() |
| 136 | { |
| 137 | d->updatePolish(); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | QWebViewSettings *QWebView::getSettings() const |
| 142 | { |
| 143 | return m_settings; |
| 144 | } |
| 145 | |
| 146 | void QWebView::loadHtml(const QString &html, const QUrl &baseUrl) |
| 147 | { |
| 148 | d->loadHtml(html, baseUrl); |
| 149 | } |
| 150 | |
| 151 | void QWebView::runJavaScriptPrivate(const QString &script, |
| 152 | int callbackId) |
| 153 | { |
| 154 | d->runJavaScriptPrivate(script, callbackId); |
| 155 | } |
| 156 | |
| 157 | void QWebView::setCookie(const QString &domain, const QString &name, const QString &value) |
| 158 | { |
| 159 | d->setCookie(domain, name, value); |
| 160 | } |
| 161 | |
| 162 | void QWebView::deleteCookie(const QString &domain, const QString &name) |
| 163 | { |
| 164 | d->deleteCookie(domain, name); |
| 165 | } |
| 166 | |
| 167 | void QWebView::deleteAllCookies() |
| 168 | { |
| 169 | d->deleteAllCookies(); |
| 170 | } |
| 171 | |
| 172 | void QWebView::onTitleChanged(const QString &title) |
| 173 | { |
| 174 | if (m_title == title) |
| 175 | return; |
| 176 | |
| 177 | m_title = title; |
| 178 | Q_EMIT titleChanged(); |
| 179 | } |
| 180 | |
| 181 | void QWebView::onUrlChanged(const QUrl &url) |
| 182 | { |
| 183 | if (m_url == url) |
| 184 | return; |
| 185 | |
| 186 | m_url = url; |
| 187 | Q_EMIT urlChanged(); |
| 188 | } |
| 189 | |
| 190 | void QWebView::onLoadProgressChanged(int progress) |
| 191 | { |
| 192 | if (m_progress == progress) |
| 193 | return; |
| 194 | |
| 195 | m_progress = progress; |
| 196 | Q_EMIT loadProgressChanged(); |
| 197 | } |
| 198 | |
| 199 | void 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 | |
| 208 | void QWebView::onHttpUserAgentChanged(const QString &userAgent) |
| 209 | { |
| 210 | if (m_httpUserAgent == userAgent) |
| 211 | return; |
| 212 | m_httpUserAgent = userAgent; |
| 213 | Q_EMIT httpUserAgentChanged(); |
| 214 | } |
| 215 | |
| 216 | void QWebView::init() |
| 217 | { |
| 218 | d->init(); |
| 219 | } |
| 220 | |
| 221 | QWebViewSettings::QWebViewSettings(QAbstractWebViewSettings *settings) |
| 222 | : d(settings) |
| 223 | { |
| 224 | Q_ASSERT(settings != nullptr); |
| 225 | } |
| 226 | |
| 227 | QWebViewSettings::~QWebViewSettings() |
| 228 | { |
| 229 | |
| 230 | } |
| 231 | |
| 232 | bool QWebViewSettings::localStorageEnabled() const |
| 233 | { |
| 234 | return d->localStorageEnabled(); |
| 235 | } |
| 236 | |
| 237 | void QWebViewSettings::setLocalStorageEnabled(bool enabled) |
| 238 | { |
| 239 | if (d->localStorageEnabled() == enabled) |
| 240 | return; |
| 241 | |
| 242 | d->setLocalStorageEnabled(enabled); |
| 243 | emit localStorageEnabledChanged(); |
| 244 | } |
| 245 | |
| 246 | bool QWebViewSettings::javaScriptEnabled() const |
| 247 | { |
| 248 | return d->javascriptEnabled(); |
| 249 | } |
| 250 | |
| 251 | void QWebViewSettings::setJavaScriptEnabled(bool enabled) |
| 252 | { |
| 253 | if (d->javascriptEnabled() == enabled) |
| 254 | return; |
| 255 | |
| 256 | d->setJavascriptEnabled(enabled); |
| 257 | emit javaScriptEnabledChanged(); |
| 258 | } |
| 259 | |
| 260 | void QWebViewSettings::setAllowFileAccess(bool enabled) |
| 261 | { |
| 262 | if (d->allowFileAccess() == enabled) |
| 263 | return; |
| 264 | |
| 265 | d->setAllowFileAccess(enabled); |
| 266 | emit allowFileAccessChanged(); |
| 267 | } |
| 268 | |
| 269 | bool QWebViewSettings::allowFileAccess() const |
| 270 | { |
| 271 | return d->allowFileAccess(); |
| 272 | } |
| 273 | |
| 274 | bool QWebViewSettings::localContentCanAccessFileUrls() const |
| 275 | { |
| 276 | return d->localContentCanAccessFileUrls(); |
| 277 | } |
| 278 | |
| 279 | void QWebViewSettings::setLocalContentCanAccessFileUrls(bool enabled) |
| 280 | { |
| 281 | if (d->localContentCanAccessFileUrls() == enabled) |
| 282 | return; |
| 283 | |
| 284 | d->setLocalContentCanAccessFileUrls(enabled); |
| 285 | emit localContentCanAccessFileUrlsChanged(); |
| 286 | } |
| 287 | |
| 288 | QT_END_NAMESPACE |
| 289 | |