From 5ca4d8903f952bf65a6bb9168f77773af050afe8 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Nov 2018 18:07:40 +0100 Subject: [PATCH 1/5] Remove the requestInterceptor. This is useless. It was use to rewrite http request to zim request and have the custumUrlShemeHandler handle the request. However, we now want to open http request in a external browser. And it is already the case, the http requests are never intercepted. --- kiwix-desktop.pro | 2 -- src/kiwixapp.h | 3 --- src/requestinterceptor.cpp | 22 ---------------------- src/requestinterceptor.h | 14 -------------- src/webview.cpp | 1 - 5 files changed, 42 deletions(-) delete mode 100644 src/requestinterceptor.cpp delete mode 100644 src/requestinterceptor.h diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index ad23ff6..d6862bc 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -47,7 +47,6 @@ SOURCES += \ src/blobbuffer.cpp \ src/library.cpp \ src/topwidget.cpp \ - src/requestinterceptor.cpp \ src/urlschemehandler.cpp \ src/webview.cpp \ src/searchbar.cpp \ @@ -67,7 +66,6 @@ HEADERS += \ src/library.h \ src/topwidget.h \ src/kconstants.h \ - src/requestinterceptor.h \ src/urlschemehandler.h \ src/webview.h \ src/searchbar.h \ diff --git a/src/kiwixapp.h b/src/kiwixapp.h index 789b2e0..ea2459e 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -8,7 +8,6 @@ #include "tabbar.h" #include "tocsidebar.h" #include "urlschemehandler.h" -#include "requestinterceptor.h" #include #include @@ -64,7 +63,6 @@ public: void showMessage(const QString& message); UrlSchemeHandler* getSchemeHandler() { return &m_schemeHandler; } - RequestInterceptor* getRequestInterceptor() { return &m_requestInterceptor; } Library* getLibrary() { return &m_library; } MainWindow* getMainWindow() { return mp_mainWindow; } kiwix::Downloader* getDownloader() { return mp_downloader; } @@ -93,7 +91,6 @@ private: QErrorMessage* mp_errorDialog; UrlSchemeHandler m_schemeHandler; - RequestInterceptor m_requestInterceptor; QAction* mpa_actions[MAX_ACTION]; }; diff --git a/src/requestinterceptor.cpp b/src/requestinterceptor.cpp deleted file mode 100644 index 34d2aaa..0000000 --- a/src/requestinterceptor.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "requestinterceptor.h" - -#include -#include -#include - -RequestInterceptor::RequestInterceptor() -{ - -} - - -void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) -{ - auto url = info.requestUrl(); - auto urlString = url.toString(); - if (urlString.startsWith("http://")) { - urlString.replace(0, 7, "zim://"); - } - info.redirect(QUrl(urlString)); -} - diff --git a/src/requestinterceptor.h b/src/requestinterceptor.h deleted file mode 100644 index 8c60244..0000000 --- a/src/requestinterceptor.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef REQUESTINTERCEPTOR_H -#define REQUESTINTERCEPTOR_H - -#include - - -class RequestInterceptor : public QWebEngineUrlRequestInterceptor -{ -public: - RequestInterceptor(); - virtual void interceptRequest(QWebEngineUrlRequestInfo &info); -}; - -#endif // REQUESTINTERCEPTOR_H diff --git a/src/webview.cpp b/src/webview.cpp index 658da89..bb643b3 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -13,7 +13,6 @@ WebView::WebView(QWidget *parent) auto profile = page()->profile(); auto app = KiwixApp::instance(); profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); - profile->setRequestInterceptor(app->getRequestInterceptor()); QObject::connect(this, &QWebEngineView::urlChanged, this, &WebView::onUrlChanged); } From 29e27fa5d2e78d31e21b393774075d013b72a0ed Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Nov 2018 18:09:43 +0100 Subject: [PATCH 2/5] Add the custom zim UrlSchemeHandler to the contentManager view. This way, the contentManager can get some information about a zim file using the zim scheme. We have to initialize the m_schemeHandler before the m_manager. --- src/contentmanagerview.cpp | 5 +++++ src/kiwixapp.h | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index eee8634..5b40249 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -1,10 +1,15 @@ #include "contentmanagerview.h" #include +#include +#include "kiwixapp.h" ContentManagerView::ContentManagerView(QWidget *parent) : QWebEngineView(parent) { page()->setWebChannel(&m_webChannel); + auto profile = page()->profile(); + auto app = KiwixApp::instance(); + profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); } diff --git a/src/kiwixapp.h b/src/kiwixapp.h index ea2459e..f5efa57 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -82,6 +82,7 @@ protected: private: QTranslator m_qtTranslator, m_appTranslator; + UrlSchemeHandler m_schemeHandler; Library m_library; kiwix::Downloader* mp_downloader; ContentManager m_manager; @@ -90,7 +91,6 @@ private: QWidget* mp_currentSideBar; QErrorMessage* mp_errorDialog; - UrlSchemeHandler m_schemeHandler; QAction* mpa_actions[MAX_ACTION]; }; From dd52ac8255e76889dd366907c404defef6f34024 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Nov 2018 18:11:57 +0100 Subject: [PATCH 3/5] Move the urlSchemeHandler content handling in a specific method. Just a bit of code reformating. --- src/urlschemehandler.cpp | 20 ++++++++++++++++---- src/urlschemehandler.h | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index a8c4461..409a976 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -13,7 +13,7 @@ UrlSchemeHandler::UrlSchemeHandler() void -UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) +UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request) { auto qurl = request->requestUrl(); std::string url = qurl.path().toUtf8().constData(); @@ -30,18 +30,18 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) kiwix::Entry entry; try { entry = reader->getEntryFromPath(url); - } catch (kiwix::NoEntry& e) { + } catch (kiwix::NoEntry&) { url = "A/" + url; try { entry = reader->getEntryFromPath(url); - } catch (kiwix::NoEntry& e) { + } catch (kiwix::NoEntry&) { request->fail(QWebEngineUrlRequestJob::UrlNotFound); return; } } try { entry = entry.getFinalEntry(); - } catch (kiwix::NoEntry& e) { + } catch (kiwix::NoEntry&) { request->fail(QWebEngineUrlRequestJob::UrlNotFound); return; } @@ -51,3 +51,15 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater); request->reply(mimeType, buffer); } + +void +UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) +{ + auto qurl = request->requestUrl(); + auto host = qurl.host(); + if (host.endsWith(".zim")) { + handleContentRequest(request); + } else { + request->fail(QWebEngineUrlRequestJob::UrlNotFound); + } +} diff --git a/src/urlschemehandler.h b/src/urlschemehandler.h index 97b2320..bb61f52 100644 --- a/src/urlschemehandler.h +++ b/src/urlschemehandler.h @@ -8,6 +8,8 @@ class UrlSchemeHandler : public QWebEngineUrlSchemeHandler public: UrlSchemeHandler(); void requestStarted(QWebEngineUrlRequestJob *request); +private: + void handleContentRequest(QWebEngineUrlRequestJob *request); }; #endif // URLSCHEMEHANDLER_H From a98ea49cac5e5f970b4f09d8e8b7fd7f2f6b9c6c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Nov 2018 18:13:23 +0100 Subject: [PATCH 4/5] Be able to request some zim's metadata using a custom url. Only favicon for now. --- src/urlschemehandler.cpp | 29 +++++++++++++++++++++++++++++ src/urlschemehandler.h | 1 + 2 files changed, 30 insertions(+) diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index 409a976..b1e4b3d 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -52,6 +52,33 @@ UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request) request->reply(mimeType, buffer); } +void +UrlSchemeHandler::handleMetaRequest(QWebEngineUrlRequestJob* request) +{ + auto qurl = request->requestUrl(); + auto host = qurl.host(); + auto parts = host.split('.'); + auto zimId = parts[0]; + auto metaName = parts[1]; + + auto library = KiwixApp::instance()->getLibrary(); + auto reader = library->getReader(zimId+".zim"); + if ( reader == nullptr) { + request->fail(QWebEngineUrlRequestJob::UrlNotFound); + return; + } + if (metaName == "favicon") { + std::string mimeType; + std::string content; + QBuffer* buffer = new QBuffer; + reader->getFavicon(content, mimeType); + buffer->setData(content.data(), content.size()); + connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater); + request->reply(QByteArray::fromStdString(mimeType), buffer); + } + request->fail(QWebEngineUrlRequestJob::UrlNotFound); +} + void UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) { @@ -59,6 +86,8 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) auto host = qurl.host(); if (host.endsWith(".zim")) { handleContentRequest(request); + } else if (host.endsWith(".meta")) { + handleMetaRequest(request); } else { request->fail(QWebEngineUrlRequestJob::UrlNotFound); } diff --git a/src/urlschemehandler.h b/src/urlschemehandler.h index bb61f52..ea8a06c 100644 --- a/src/urlschemehandler.h +++ b/src/urlschemehandler.h @@ -9,6 +9,7 @@ public: UrlSchemeHandler(); void requestStarted(QWebEngineUrlRequestJob *request); private: + void handleMetaRequest(QWebEngineUrlRequestJob *request); void handleContentRequest(QWebEngineUrlRequestJob *request); }; From 4cbf001de4e95b8df3b845e4faf39db34e1906fa Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 13 Nov 2018 18:16:12 +0100 Subject: [PATCH 5/5] Use real url for icon in the contentManager instead of data url. The update of book info are made somehow sync. (They are async, but somehow, the view update hangout). Getting the favicon content is pretty long and it seems that the view (and getting remote content) take a long time. By using a real url, QWebViewEngine handle everything nicely and it speed up everything. --- resources/texts/_contentManager.html | 5 +++-- src/contentmanager.cpp | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/resources/texts/_contentManager.html b/resources/texts/_contentManager.html index 28c5a5e..e7f3b89 100644 --- a/resources/texts/_contentManager.html +++ b/resources/texts/_contentManager.html @@ -20,7 +20,7 @@ function createDict(keys, values) { } return d; } -const BOOK_KEYS = ["id", "name", "path", "url", "size", "description", "title", "tags", "date", "favicon", "faviconMimeType", "downloadId"]; +const BOOK_KEYS = ["id", "name", "path", "url", "size", "description", "title", "tags", "date", "faviconUrl", "faviconMimeType", "downloadId"]; function addBook(values) { var b = createDict(BOOK_KEYS, values); if (b.downloadId && !downloadUpdaters.hasOwnProperty(b.id)) { @@ -189,7 +189,8 @@ button {
- + + {{ book.title }} diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 48025fc..3e96ac4 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -68,6 +68,7 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys) ADD_V("origId", getOrigId); ADD_V("faviconMimeType", getFaviconMimeType); ADD_V("downloadId", getDownloadId); + ADD_V("faviconUrl", getFaviconUrl); if (key == "favicon") { auto s = b.getFavicon(); values.append(QByteArray::fromStdString(s).toBase64());