[rkward/work/qtwebengine] /: Support help:/ protocol in QWebEngine-basedHTML window

Thomas Friedrichsmeier null at kde.org
Tue Mar 31 14:19:03 BST 2020


Git commit b0a94c15f27928289745ecd21d1b0e5e7a710daf by Thomas Friedrichsmeier.
Committed on 31/03/2020 at 13:18.
Pushed by tfry into branch 'work/qtwebengine'.

Support help:/ protocol in QWebEngine-basedHTML window

M  +1    -1    CMakeLists.txt
M  +1    -1    ChangeLog
M  +11   -0    rkward/main.cpp
M  +32   -0    rkward/windows/rkhtmlwindow.cpp

https://commits.kde.org/rkward/b0a94c15f27928289745ecd21d1b0e5e7a710daf

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6a8397d0..c3938843 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,7 +25,7 @@ FIND_PACKAGE(Qt5 5.2 CONFIG REQUIRED COMPONENTS Widgets Core Xml Network Script
 FIND_PACKAGE(KF5 5.2 REQUIRED COMPONENTS CoreAddons DocTools I18n XmlGui TextEditor WidgetsAddons Parts Config Notifications WindowSystem OPTIONAL_COMPONENTS Crash)
 IF(NOT NO_QT_WEBENGINE)
 	FIND_PACKAGE(Qt5 5.2 OPTIONAL_COMPONENTS WebEngineWidgets)
-	IF(NOT Qt5WebEngineWidgets_FOUND OR Qt5WebEngineWidgets_VERSION VERSION_LESS "5.14.0")
+	IF(NOT Qt5WebEngineWidgets_FOUND OR Qt5WebEngineWidgets_VERSION VERSION_LESS "5.12.0")
 		MESSAGE(STATUS "QWebEngine not available (or version < 5.12). Falling back to QtWebKit")
 		SET(NO_QT_WEBENGINE 1)
 	ENDIF()
diff --git a/ChangeLog b/ChangeLog
index 420480cb..a5fcb91b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,7 +2,7 @@
 - Support using QWebEngine instead of QtWebKit (still supported)
   - TODO:
     - Restore scroll position (needs to happen *after* page load)
-    - Support help:// protocol
+    - Should all KIO-schemes be supported (not too hard to do, help:/ already supported)?
     - Support pdf
 - <text> elements in plugins may now also contain clickable links, including rkward://-scheme links
 * TODO: Bring new code hinting features to the console window!
diff --git a/rkward/main.cpp b/rkward/main.cpp
index b1cf82b7..36b52718 100644
--- a/rkward/main.cpp
+++ b/rkward/main.cpp
@@ -233,8 +233,19 @@ QString resolveRSpecOrFail (QString input, QString message) {
 	return QString(); // not reached
 }
 
+#ifndef NO_QT_WEBENGINE
+#include <QWebEngineUrlScheme>
+#endif
+
 int main (int argc, char *argv[]) {
 	RK_Debug::RK_Debug_Level = DL_WARNING;
+#ifndef NO_QT_WEBENGINE
+	// annoingly, QWebEngineUrlSchemes have to be registered before creating the app.
+	QWebEngineUrlScheme scheme("help");
+	scheme.setSyntax (QWebEngineUrlScheme::Syntax::Path);
+	scheme.setFlags (QWebEngineUrlScheme::LocalScheme|QWebEngineUrlScheme::LocalAccessAllowed);
+	QWebEngineUrlScheme::registerScheme (scheme);
+#endif
 	QApplication app (argc, argv);
 #ifdef WITH_KCRASH
 	KCrash::setDrKonqiEnabled (true);
diff --git a/rkward/windows/rkhtmlwindow.cpp b/rkward/windows/rkhtmlwindow.cpp
index d3ea32ff..f7926847 100644
--- a/rkward/windows/rkhtmlwindow.cpp
+++ b/rkward/windows/rkhtmlwindow.cpp
@@ -221,6 +221,35 @@ protected:
 #endif
 };
 
+#ifndef NO_QT_WEBENGINE
+#include <QWebEngineUrlSchemeHandler>
+#include <QWebEngineUrlRequestJob>
+#include <QBuffer>
+class RKWebEngineKIOForwarder : public QWebEngineUrlSchemeHandler {
+public:
+	RKWebEngineKIOForwarder (QObject *parent) : QWebEngineUrlSchemeHandler (parent) {}
+	void requestStarted (QWebEngineUrlRequestJob *request) {
+		KIO::StoredTransferJob *job = KIO::storedGet(request->requestUrl (), KIO::NoReload, KIO::HideProgressInfo);
+		connect (job, &KIO::StoredTransferJob::result, this, [this, job](){ kioJobFinished(job); });
+		jobs.insert (job, request);
+	}
+private:
+	void kioJobFinished (KIO::StoredTransferJob* job) {
+		QWebEngineUrlRequestJob* request = jobs.take (job);
+		if (!request) {
+			return;
+		}
+		if (job->error ()) {
+			request->fail (QWebEngineUrlRequestJob::UrlInvalid);  // TODO
+			return;
+		}
+		QBuffer *buf = new QBuffer (request);
+		buf->setData (job->data ());
+		request->reply (QMimeDatabase ().mimeTypeForData (job->data ()).name ().toUtf8 (), buf);
+	}
+	QMap<KIO::StoredTransferJob*,QPointer<QWebEngineUrlRequestJob>> jobs;
+};
+#endif
 
 RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (parent, RKMDIWindow::HelpWindow) {
 	RK_TRACE (APP);
@@ -239,6 +268,9 @@ RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (par
 #else
 	findbar = new RKFindBar (this, true);
 	findbar->setPrimaryOptions (QList<QWidget*>() << findbar->getOption (RKFindBar::FindAsYouType) << findbar->getOption (RKFindBar::MatchCase));
+	if (!QWebEngineProfile::defaultProfile ()->urlSchemeHandler ("help")) {
+		QWebEngineProfile::defaultProfile ()->installUrlSchemeHandler ("help", new RKWebEngineKIOForwarder (RKWardMainWindow::getMain()));
+	}
 #endif
 	layout->addWidget (findbar);
 	findbar->hide ();



More information about the rkward-tracker mailing list