Alkimia API

alkwebpage.cpp
1/*
2 SPDX-FileCopyrightText: 2018, 2024 Ralf Habacker ralf.habacker @freenet.de
3
4 This file is part of libalkimia.
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9#include "alkwebpage.h"
10
11#include "alkwebview.h"
12
13#if defined(BUILD_WITH_WEBENGINE)
14#include "../3rdparty/qtwebengine/tests/auto/util/util.h"
15
16#include <klocalizedstring.h>
17
18#include <QContextMenuEvent>
19#include <QDesktopServices>
20#include <QEventLoop>
21#include <QMenu>
22#include <QPointer>
23#include <QTimer>
24#include <QUrl>
25#include <QWebEnginePage>
26#include <QWebEngineProfile>
27#include <QWebEngineView>
28
29class AlkWebPage::Private : public QObject
30{
32public:
33 AlkWebPage *q;
34 int timeout{-1}; // msec
35
36 explicit Private(AlkWebPage *_q)
37 : q(_q)
38 {
39 }
40 Private(const Private& right) = delete;
41 Private& operator=(const Private& right) = delete;
42};
43
44AlkWebPage::AlkWebPage(QWidget *parent)
45 : QWebEnginePage(parent)
46 , d(new Private(this))
47{
48}
49
50AlkWebPage::~AlkWebPage()
51{
52 delete d;
53}
54
55void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
56{
57 profile()->setHttpAcceptLanguage(acceptLanguage);
58 setUrl(url);
59}
60
62{
63 CallbackSpy<QString> spy;
64 QWebEnginePage::toHtml(spy.ref());
65 QString html = d->timeout != -1 ? spy.waitForResult(d->timeout) : spy.waitForResult();
66 return html;
67}
68
69QStringList AlkWebPage::getAllElements(const QString &symbol)
70{
71 Q_UNUSED(symbol)
72
73 return QStringList();
74}
75
76QString AlkWebPage::getFirstElement(const QString &symbol)
77{
78 Q_UNUSED(symbol)
79
80 return QString();
81}
82
83void AlkWebPage::setTimeout(int timeout)
84{
85 d->timeout = timeout;
86}
87
88int AlkWebPage::timeout()
89{
90 return d->timeout;
91}
92
93bool AlkWebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
94{
95 if (type == QWebEnginePage::NavigationTypeRedirect && isMainFrame)
96 Q_EMIT loadRedirectedTo(url);
97 return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
98}
99
100#include "alkwebpage.moc"
101
102#elif defined(BUILD_WITH_WEBKIT)
103#include <QWebFrame>
104#include <QWebElement>
105#include <QWebInspector>
106#include <QWebView>
107#include <QNetworkRequest>
108
109class AlkWebPage::Private
110{
111public:
112 AlkWebPage *p;
113 QNetworkAccessManager *networkAccessManager;
114 explicit Private(AlkWebPage *parent)
115 : p(parent)
116 , networkAccessManager(new QNetworkAccessManager)
117 {
118#if QT_VERSION >= QT_VERSION_CHECK(5,9,0)
119 // see https://community.kde.org/Policies/API_to_Avoid#QNetworkAccessManager
121#endif
122 p->setNetworkAccessManager(networkAccessManager);
123 }
124
125 ~Private()
126 {
127 delete networkAccessManager;
128 }
129};
130
131AlkWebPage::AlkWebPage(QObject *parent)
132 : QWebPage(parent)
133 , d(new Private(this))
134{
135 settings()->setAttribute(QWebSettings::JavaEnabled, false);
136 settings()->setAttribute(QWebSettings::AutoLoadImages, false);
137 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
138}
139
140AlkWebPage::~AlkWebPage()
141{
142 delete d;
143}
144
145void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
146{
147 QNetworkRequest request;
148 request.setUrl(url);
149 if (!acceptLanguage.isEmpty())
150 request.setRawHeader("Accept-Language", acceptLanguage.toLocal8Bit());
151#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
152 if (url.query().toLower().contains(QLatin1String("method=post"))) {
153 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
154 mainFrame()->load(request, QNetworkAccessManager::PostOperation, url.query().toUtf8());
155#else
156 if (url.hasQueryItem(QLatin1String("method")) && url.queryItemValue(QLatin1String("method")).toLower()== QLatin1String("post")) {
157 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
158 mainFrame()->load(request, QNetworkAccessManager::PostOperation);
159#endif
160 } else
161 mainFrame()->load(request);
162}
163
164void AlkWebPage::setHtml(const QString &data)
165{
166 // TODO baseurl
167 mainFrame()->setHtml(data);
168}
169
171{
172 QWebFrame *frame = mainFrame();
173 return frame->toHtml();
174}
175
176QStringList AlkWebPage::getAllElements(const QString &symbol)
177{
178 QStringList result;
179 QWebFrame *frame = mainFrame();
180 QWebElementCollection elements = frame->findAllElements(symbol);
181 for (const auto &e: elements) {
182 result.append(e.toPlainText());
183 }
184 return result;
185}
186
187QString AlkWebPage::getFirstElement(const QString &symbol)
188{
189 QWebFrame *frame = mainFrame();
190 QWebElement element = frame->findFirstElement(symbol);
191 return element.toPlainText();
192}
193
194#else
195
196#include <QEventLoop>
197#include <QNetworkAccessManager>
198#include <QNetworkReply>
199#include <QThread>
200
201class AlkWebPage::Private
202{
203public:
204};
205
206AlkWebPage::AlkWebPage(QWidget *parent)
208 , d(new Private)
209{
210 setOpenExternalLinks(false);
211 setOpenLinks(false);
212 connect(this, SIGNAL(sourceChanged(QUrl)), SIGNAL(loadRedirectedTo(QUrl)));
213}
214
215AlkWebPage::~AlkWebPage()
216{
217 delete d;
218}
219
220void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
221{
222 Q_UNUSED(acceptLanguage)
223#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
225#else
226 setSource(url);
227#endif
228 Q_EMIT loadStarted();
229}
230
231void AlkWebPage::setUrl(const QUrl &url)
232{
233 load(url, QString());
234}
235
236QStringList AlkWebPage::getAllElements(const QString &symbol)
237{
238 Q_UNUSED(symbol)
239
240 return QStringList();
241}
242
243QString AlkWebPage::getFirstElement(const QString &symbol)
244{
245 Q_UNUSED(symbol)
246
247 return QString();
248}
249
250QVariant AlkWebPage::loadResource(int type, const QUrl &name)
251{
252 switch(type) {
255 QNetworkAccessManager networkManager;
256 QNetworkRequest request;
257 request.setUrl(name);
258 QNetworkReply* reply = networkManager.get(request);
259 QEventLoop loop;
260 connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
261 connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
262 loop.exec();
263 QString data = reply->readAll();
264 Q_EMIT loadFinished(true);
265 return data;
266 }
267
268 return QVariant();
269}
270
271#endif
The AlkWebPage class provides an object to load and view web documents to provide functionality like ...
Definition alkwebpage.h:100
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
const QList< QKeySequence > & quit()
int exec(ProcessEventsFlags flags)
QByteArray readAll()
void append(QList< T > &&value)
QNetworkReply * get(const QNetworkRequest &request)
void setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
void setHeader(KnownHeaders header, const QVariant &value)
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
void setUrl(const QUrl &url)
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QByteArray toLocal8Bit() const const
QString toLower() const const
QByteArray toUtf8() const const
void setSource(const QUrl &url, QTextDocument::ResourceType type)
QString toHtml() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString query(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.