File indexing completed on 2024-09-08 12:53:32
0001 /* 0002 SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de 0003 0004 This file is part of libalkimia. 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 */ 0008 0009 #include "alkwebpage.h" 0010 0011 #if defined(BUILD_WITH_WEBENGINE) 0012 #include <QEventLoop> 0013 #include <QWebEnginePage> 0014 #include <QWebEngineView> 0015 #include <QUrl> 0016 0017 class AlkWebPage::Private : public QObject 0018 { 0019 Q_OBJECT 0020 public: 0021 AlkWebPage *q; 0022 0023 Private(AlkWebPage *_q) : q(_q) {} 0024 void slotUrlChanged(const QUrl &url) 0025 { 0026 // This workaround is necessary because QWebEnginePage::urlChanged() 0027 // returns the html content set with setContent() as url. 0028 if (url.scheme().startsWith("http")) 0029 emit q->urlChanged(url); 0030 else 0031 emit q->urlChanged(QUrl()); 0032 } 0033 }; 0034 0035 AlkWebPage::AlkWebPage(QWidget *parent) 0036 : QWebEnginePage(parent) 0037 , d(new Private(this)) 0038 { 0039 connect(this, &QWebEnginePage::urlChanged, d, &Private::slotUrlChanged); 0040 } 0041 0042 AlkWebPage::~AlkWebPage() 0043 { 0044 delete d; 0045 } 0046 0047 QWidget *AlkWebPage::widget() 0048 { 0049 if (!view()) 0050 setView(new QWebEngineView); 0051 return view(); 0052 } 0053 0054 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) 0055 { 0056 Q_UNUSED(acceptLanguage) 0057 0058 setUrl(url); 0059 } 0060 0061 QString AlkWebPage::toHtml() 0062 { 0063 QString html; 0064 QEventLoop loop; 0065 QWebEnginePage::toHtml([&html, &loop](const QString &result) 0066 { 0067 html = result; 0068 loop.quit(); 0069 } 0070 ); 0071 loop.exec(); 0072 return html; 0073 } 0074 0075 void AlkWebPage::setContent(const QString &s) 0076 { 0077 setHtml(s); 0078 } 0079 0080 QString AlkWebPage::getFirstElement(const QString &symbol) 0081 { 0082 Q_UNUSED(symbol) 0083 0084 return QString(); 0085 } 0086 0087 void AlkWebPage::setWebInspectorEnabled(bool state) 0088 { 0089 Q_UNUSED(state) 0090 } 0091 0092 bool AlkWebPage::webInspectorEnabled() 0093 { 0094 return false; 0095 } 0096 0097 #include "alkwebpage.moc" 0098 0099 #elif defined(BUILD_WITH_WEBKIT) 0100 #include <QWebFrame> 0101 #include <QWebElement> 0102 #include <QWebInspector> 0103 #include <QWebView> 0104 #include <QNetworkRequest> 0105 0106 class AlkWebPage::Private 0107 { 0108 public: 0109 QWebInspector *inspector; 0110 AlkWebPage *p; 0111 QNetworkAccessManager *networkAccessManager; 0112 Private(AlkWebPage *parent) 0113 : inspector(nullptr), 0114 p(parent), 0115 networkAccessManager(new QNetworkAccessManager) 0116 { 0117 #if QT_VERSION >= QT_VERSION_CHECK(5,9,0) 0118 // see https://community.kde.org/Policies/API_to_Avoid#QNetworkAccessManager 0119 networkAccessManager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); 0120 #endif 0121 p->page()->setNetworkAccessManager(networkAccessManager); 0122 } 0123 0124 ~Private() 0125 { 0126 p->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, false); 0127 inspector->setPage(nullptr); 0128 delete inspector; 0129 delete networkAccessManager; 0130 } 0131 0132 void setWebInspectorEnabled(bool enable) 0133 { 0134 p->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, enable); 0135 if (enable && !inspector) { 0136 inspector = new QWebInspector(); 0137 inspector->setPage(p->page()); 0138 } 0139 } 0140 0141 bool webInspectorEnabled() 0142 { 0143 return p->page()->settings()->testAttribute(QWebSettings::DeveloperExtrasEnabled); 0144 } 0145 }; 0146 0147 AlkWebPage::AlkWebPage(QWidget *parent) 0148 : QWebView(parent) 0149 , d(new Private(this)) 0150 { 0151 page()->settings()->setAttribute(QWebSettings::JavaEnabled, false); 0152 page()->settings()->setAttribute(QWebSettings::AutoLoadImages, false); 0153 page()->settings()->setAttribute(QWebSettings::PluginsEnabled, false); 0154 } 0155 0156 AlkWebPage::~AlkWebPage() 0157 { 0158 delete d; 0159 } 0160 0161 QWidget *AlkWebPage::widget() 0162 { 0163 return this; 0164 } 0165 0166 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) 0167 { 0168 QNetworkRequest request; 0169 request.setUrl(url); 0170 if (!acceptLanguage.isEmpty()) 0171 request.setRawHeader("Accept-Language", acceptLanguage.toLocal8Bit()); 0172 QWebView::load(request); 0173 } 0174 0175 QString AlkWebPage::toHtml() 0176 { 0177 QWebFrame *frame = page()->mainFrame(); 0178 return frame->toHtml(); 0179 } 0180 0181 QString AlkWebPage::getFirstElement(const QString &symbol) 0182 { 0183 QWebFrame *frame = page()->mainFrame(); 0184 QWebElement element = frame->findFirstElement(symbol); 0185 return element.toPlainText(); 0186 } 0187 0188 void AlkWebPage::setWebInspectorEnabled(bool enable) 0189 { 0190 d->setWebInspectorEnabled(enable); 0191 } 0192 0193 bool AlkWebPage::webInspectorEnabled() 0194 { 0195 return d->webInspectorEnabled(); 0196 } 0197 0198 #else 0199 0200 class AlkWebPage::Private 0201 { 0202 public: 0203 }; 0204 0205 AlkWebPage::AlkWebPage(QWidget *parent) 0206 : QTextBrowser(parent) 0207 , d(new Private) 0208 { 0209 setOpenExternalLinks(false); 0210 setOpenLinks(false); 0211 } 0212 0213 AlkWebPage::~AlkWebPage() 0214 { 0215 delete d; 0216 } 0217 0218 QWidget *AlkWebPage::widget() 0219 { 0220 return this; 0221 } 0222 0223 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) 0224 { 0225 Q_UNUSED(url) 0226 Q_UNUSED(acceptLanguage) 0227 } 0228 0229 void AlkWebPage::setUrl(const QUrl &url) 0230 { 0231 Q_UNUSED(url) 0232 } 0233 0234 void AlkWebPage::setContent(const QString &s) 0235 { 0236 setHtml(s); 0237 } 0238 0239 QString AlkWebPage::getFirstElement(const QString &symbol) 0240 { 0241 Q_UNUSED(symbol) 0242 0243 return QString(); 0244 } 0245 0246 void AlkWebPage::setWebInspectorEnabled(bool enable) 0247 { 0248 Q_UNUSED(enable) 0249 } 0250 0251 bool AlkWebPage::webInspectorEnabled() 0252 { 0253 return false; 0254 } 0255 0256 #endif