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/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()); 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 789b2e0..f5efa57 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; } @@ -84,6 +82,7 @@ protected: private: QTranslator m_qtTranslator, m_appTranslator; + UrlSchemeHandler m_schemeHandler; Library m_library; kiwix::Downloader* mp_downloader; ContentManager m_manager; @@ -92,8 +91,6 @@ private: QWidget* mp_currentSideBar; 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/urlschemehandler.cpp b/src/urlschemehandler.cpp index a8c4461..b1e4b3d 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,44 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater); 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) +{ + auto qurl = request->requestUrl(); + 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 97b2320..ea8a06c 100644 --- a/src/urlschemehandler.h +++ b/src/urlschemehandler.h @@ -8,6 +8,9 @@ class UrlSchemeHandler : public QWebEngineUrlSchemeHandler public: UrlSchemeHandler(); void requestStarted(QWebEngineUrlRequestJob *request); +private: + void handleMetaRequest(QWebEngineUrlRequestJob *request); + void handleContentRequest(QWebEngineUrlRequestJob *request); }; #endif // URLSCHEMEHANDLER_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); }