Commit 79e0cfe2 authored by Robby Stephenson's avatar Robby Stephenson
Browse files

Open target=_blank links externally

With KHTML, even links with target=_blank were opened externally. With
QWebEngineView, intercept the window creation, capture the url being
loaded, then load it externally.

BUG: 445871
FIXED-IN: 3.4.3
parent 0df70015
2021-12-18 Robby Stephenson <robby@periapsis.org>
* Fixed bug with opening target=_blank links (Bug 445871).
2021-12-11 Robby Stephenson <robby@periapsis.org>
* Fixed crashing bug in audio file importer (Bug 446551).
......
......@@ -124,14 +124,33 @@ bool EntryViewPage::acceptNavigationRequest(const QUrl& url_, QWebEnginePage::Na
}
if(type_ == QWebEnginePage::NavigationTypeLinkClicked) {
const QUrl u = Kernel::self()->URL().resolved(url_);
QDesktopServices::openUrl(u);
// do not load this new url inside the entry view, return false
openExternalLink(url_);
return false;
}
return true;
}
// intercept window open commands, including target=_blank
// see https://bugs.kde.org/show_bug.cgi?id=445871
QWebEnginePage* EntryViewPage::createWindow(QWebEnginePage::WebWindowType type_) {
Q_UNUSED(type_);
auto page = new QWebEnginePage(this);
connect(page, &QWebEnginePage::urlChanged, this, [this](const QUrl& u) {
openExternalLink(u);
auto page = static_cast<QWebEnginePage*>(sender());
page->action(QWebEnginePage::Stop)->trigger(); // stop the loading, further is unneccesary
page->deleteLater();
});
return page;
}
void EntryViewPage::openExternalLink(const QUrl& url_) {
const QUrl finalUrl = Kernel::self()->URL().resolved(url_);
QDesktopServices::openUrl(finalUrl);
}
EntryView::EntryView(QWidget* parent_) : QWebEngineView(parent_),
m_handler(nullptr), m_tempFile(nullptr), m_useGradientImages(true), m_checkCommonFile(true) {
EntryViewPage* page = new EntryViewPage(this);
......@@ -256,7 +275,7 @@ void EntryView::showEntry(Tellico::Data::EntryPtr entry_) {
}
#if 0
myWarning() << "turn me off!";
myWarning() << "EntryView::showEntry() - turn me off!";
QFile f2(QLatin1String("/tmp/test.html"));
if(f2.open(QIODevice::WriteOnly)) {
QTextStream t(&f2);
......
......@@ -27,7 +27,6 @@
#include "datavectors.h"
#ifdef USE_KHTML
#include <KHTMLPart>
#include <KHTMLView>
......@@ -165,6 +164,10 @@ Q_SIGNALS:
protected:
virtual bool acceptNavigationRequest(const QUrl& url, QWebEnginePage::NavigationType type, bool isMainFrame) Q_DECL_OVERRIDE;
virtual QWebEnginePage* createWindow(QWebEnginePage::WebWindowType type) Q_DECL_OVERRIDE;
private:
void openExternalLink(const QUrl& url);
};
#endif
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment