diff --git a/src/alkwebpage.cpp b/src/alkwebpage.cpp index 3a7f796..eae19b2 100644 --- a/src/alkwebpage.cpp +++ b/src/alkwebpage.cpp @@ -1,224 +1,254 @@ /*************************************************************************** * Copyright 2018 Ralf Habacker * * * * This file is part of libalkimia. * * * * libalkimia is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * * as published by the Free Software Foundation; either version 2.1 of * * the License or (at your option) version 3 or any later version. * * * * libalkimia is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see * ***************************************************************************/ #include "alkwebpage.h" #if defined(BUILD_WITH_WEBENGINE) #include #include #include #include +class AlkWebPage::Private : public QObject +{ + Q_OBJECT +public: + AlkWebPage *q; + + Private(AlkWebPage *_q) : q(_q) {} + void slotUrlChanged(const QUrl &url) + { + // This workaround is necessary because QWebEnginePage::urlChanged() + // returns the html content set with setContent() as url. + if (url.scheme().startsWith("http")) + emit q->urlChanged(url); + else + emit q->urlChanged(QUrl()); + } +}; + +AlkWebPage::AlkWebPage(QWidget *parent) + : QWebEnginePage(parent) + , d(new Private(this)) +{ + connect(this, &QWebEnginePage::urlChanged, d, &Private::slotUrlChanged); +} + +AlkWebPage::~AlkWebPage() +{ + delete d; +} + QWidget *AlkWebPage::widget() { if (!view()) setView(new QWebEngineView); return view(); } void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) { Q_UNUSED(acceptLanguage) setUrl(url); } QString AlkWebPage::toHtml() { QString html; QEventLoop loop; QWebEnginePage::toHtml([&html, &loop](const QString &result) { html = result; loop.quit(); } ); loop.exec(); return html; } QString AlkWebPage::getFirstElement(const QString &symbol) { Q_UNUSED(symbol) return QString(); } void AlkWebPage::setWebInspectorEnabled(bool state) { Q_UNUSED(state) } bool AlkWebPage::webInspectorEnabled() { return false; } #include "alkwebpage.moc" #elif defined(BUILD_WITH_WEBKIT) #include #include #include #include #include class AlkWebPage::Private { public: QWebInspector *inspector; Private() : inspector(nullptr) { } ~Private() { delete inspector; } void setWebInspectorEnabled(bool enable, QWebPage* page) { page->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, enable); delete inspector; inspector = nullptr; if (enable) { inspector = new QWebInspector(); inspector->setPage(page); } } bool webInspectorEnabled(QWebPage *page) { return page->settings()->testAttribute(QWebSettings::DeveloperExtrasEnabled); } }; AlkWebPage::AlkWebPage(QWidget *parent) : QWebView(parent) , d(new Private) { page()->settings()->setAttribute(QWebSettings::JavaEnabled, false); page()->settings()->setAttribute(QWebSettings::AutoLoadImages, false); page()->settings()->setAttribute(QWebSettings::PluginsEnabled, false); } AlkWebPage::~AlkWebPage() { delete d; } QWidget *AlkWebPage::widget() { return this; } void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) { QNetworkRequest request; request.setUrl(url); if (!acceptLanguage.isEmpty()) request.setRawHeader("Accept-Language", acceptLanguage.toLocal8Bit()); QWebView::load(request); } QString AlkWebPage::toHtml() { QWebFrame *frame = page()->mainFrame(); return frame->toHtml(); } QString AlkWebPage::getFirstElement(const QString &symbol) { QWebFrame *frame = page()->mainFrame(); QWebElement element = frame->findFirstElement(symbol); return element.toPlainText(); } void AlkWebPage::setWebInspectorEnabled(bool enable) { d->setWebInspectorEnabled(enable, page()); } bool AlkWebPage::webInspectorEnabled() { return d->webInspectorEnabled(page()); } #else class AlkWebPage::Private { public: }; AlkWebPage::AlkWebPage(QWidget *parent) : QWidget(parent) , d(new Private) { } AlkWebPage::~AlkWebPage() { delete d; } QWidget *AlkWebPage::widget() { return this; } void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage) { Q_UNUSED(url) Q_UNUSED(acceptLanguage) } void AlkWebPage::setUrl(const QUrl &url) { Q_UNUSED(url) } void AlkWebPage::setContent(const QString &s) { Q_UNUSED(s) } QString AlkWebPage::toHtml() { return QString(); } QString AlkWebPage::getFirstElement(const QString &symbol) { Q_UNUSED(symbol) return QString(); } void AlkWebPage::setWebInspectorEnabled(bool enable) { Q_UNUSED(enable) } bool AlkWebPage::webInspectorEnabled() { return false; } #endif diff --git a/src/alkwebpage.h b/src/alkwebpage.h index 878d581..d9d13e0 100644 --- a/src/alkwebpage.h +++ b/src/alkwebpage.h @@ -1,109 +1,119 @@ /*************************************************************************** * Copyright 2018 Ralf Habacker * * * * This file is part of libalkimia. * * * * libalkimia is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * * as published by the Free Software Foundation; either version 2.1 of * * the License or (at your option) version 3 or any later version. * * * * libalkimia is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see * ***************************************************************************/ #ifndef ALKWEBPAGE_H #define ALKWEBPAGE_H #include #include class QUrl; #if defined(BUILD_WITH_WEBENGINE) #include class ALK_EXPORT AlkWebPage : public QWebEnginePage { Q_OBJECT public: + AlkWebPage(QWidget *parent = nullptr); + virtual ~AlkWebPage(); + QWidget *widget(); void load(const QUrl &url, const QString &acceptLanguage); QString toHtml(); QString getFirstElement(const QString &symbol); void setWebInspectorEnabled(bool state); bool webInspectorEnabled(); + +Q_SIGNALS: + void urlChanged(const QUrl &url); + +private: + class Private; + Private *d; }; #elif defined(BUILD_WITH_WEBKIT) #include /** * The AlkWebPage class provides an interface * to a browser component with javascript support * It is used for fetching and showing web pages. * * @author Ralf Habacker */ class ALK_EXPORT AlkWebPage : public QWebView { public: AlkWebPage(QWidget *parent = nullptr); virtual ~AlkWebPage(); QWidget *widget(); void load(const QUrl &url, const QString &acceptLanguage); QString toHtml(); QString getFirstElement(const QString &symbol); void setWebInspectorEnabled(bool enable); bool webInspectorEnabled(); private: class Private; Private *d; }; #else #include /** * The AlkWebPage class provides an interface * to a browser component with javascript support * It is used for fetching and showing web pages. * * @author Ralf Habacker */ class ALK_EXPORT AlkWebPage : public QWidget { public: AlkWebPage(QWidget *parent = nullptr); virtual ~AlkWebPage(); QWidget *widget(); void load(const QUrl &url, const QString &acceptLanguage); void setUrl(const QUrl &url); void setContent(const QString &s); QString toHtml(); QString getFirstElement(const QString &symbol); void setWebInspectorEnabled(bool enable); bool webInspectorEnabled(); Q_SIGNALS: void loadStarted(); void loadFinished(bool); private: class Private; Private *d; }; #endif #endif // ALKWEBPAGE_H