Merge pull request #56 from kiwix/speedup_contentManager

Speedup content manager
This commit is contained in:
Matthieu Gautier 2018-11-13 18:33:06 +01:00 committed by GitHub
commit 0a2c65211e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 58 additions and 49 deletions

View File

@ -47,7 +47,6 @@ SOURCES += \
src/blobbuffer.cpp \ src/blobbuffer.cpp \
src/library.cpp \ src/library.cpp \
src/topwidget.cpp \ src/topwidget.cpp \
src/requestinterceptor.cpp \
src/urlschemehandler.cpp \ src/urlschemehandler.cpp \
src/webview.cpp \ src/webview.cpp \
src/searchbar.cpp \ src/searchbar.cpp \
@ -67,7 +66,6 @@ HEADERS += \
src/library.h \ src/library.h \
src/topwidget.h \ src/topwidget.h \
src/kconstants.h \ src/kconstants.h \
src/requestinterceptor.h \
src/urlschemehandler.h \ src/urlschemehandler.h \
src/webview.h \ src/webview.h \
src/searchbar.h \ src/searchbar.h \

View File

@ -20,7 +20,7 @@ function createDict(keys, values) {
} }
return d; 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) { function addBook(values) {
var b = createDict(BOOK_KEYS, values); var b = createDict(BOOK_KEYS, values);
if (b.downloadId && !downloadUpdaters.hasOwnProperty(b.id)) { if (b.downloadId && !downloadUpdaters.hasOwnProperty(b.id)) {
@ -189,7 +189,8 @@ button {
<details v-for="book in books" class="book"> <details v-for="book in books" class="book">
<summary class="tablerow"> <summary class="tablerow">
<span class="tablecell cell0"> <span class="tablecell cell0">
<img v-bind:src="'data:image/png;base64,' + book.favicon"/> <img v-if="book.faviconUrl" v-bind:src="'http://' + book.faviconUrl" />
<img v-else-if="book.faviconMimeType" v-bind:src="'zim://' + book.id + '.favicon.meta'" />
</span> </span>
<span class="tablecell cell1"> <span class="tablecell cell1">
{{ book.title }} {{ book.title }}

View File

@ -68,6 +68,7 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)
ADD_V("origId", getOrigId); ADD_V("origId", getOrigId);
ADD_V("faviconMimeType", getFaviconMimeType); ADD_V("faviconMimeType", getFaviconMimeType);
ADD_V("downloadId", getDownloadId); ADD_V("downloadId", getDownloadId);
ADD_V("faviconUrl", getFaviconUrl);
if (key == "favicon") { if (key == "favicon") {
auto s = b.getFavicon(); auto s = b.getFavicon();
values.append(QByteArray::fromStdString(s).toBase64()); values.append(QByteArray::fromStdString(s).toBase64());

View File

@ -1,10 +1,15 @@
#include "contentmanagerview.h" #include "contentmanagerview.h"
#include <QFile> #include <QFile>
#include <QWebEngineProfile>
#include "kiwixapp.h"
ContentManagerView::ContentManagerView(QWidget *parent) ContentManagerView::ContentManagerView(QWidget *parent)
: QWebEngineView(parent) : QWebEngineView(parent)
{ {
page()->setWebChannel(&m_webChannel); page()->setWebChannel(&m_webChannel);
auto profile = page()->profile();
auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
} }

View File

@ -8,7 +8,6 @@
#include "tabbar.h" #include "tabbar.h"
#include "tocsidebar.h" #include "tocsidebar.h"
#include "urlschemehandler.h" #include "urlschemehandler.h"
#include "requestinterceptor.h"
#include <QApplication> #include <QApplication>
#include <QErrorMessage> #include <QErrorMessage>
@ -64,7 +63,6 @@ public:
void showMessage(const QString& message); void showMessage(const QString& message);
UrlSchemeHandler* getSchemeHandler() { return &m_schemeHandler; } UrlSchemeHandler* getSchemeHandler() { return &m_schemeHandler; }
RequestInterceptor* getRequestInterceptor() { return &m_requestInterceptor; }
Library* getLibrary() { return &m_library; } Library* getLibrary() { return &m_library; }
MainWindow* getMainWindow() { return mp_mainWindow; } MainWindow* getMainWindow() { return mp_mainWindow; }
kiwix::Downloader* getDownloader() { return mp_downloader; } kiwix::Downloader* getDownloader() { return mp_downloader; }
@ -84,6 +82,7 @@ protected:
private: private:
QTranslator m_qtTranslator, m_appTranslator; QTranslator m_qtTranslator, m_appTranslator;
UrlSchemeHandler m_schemeHandler;
Library m_library; Library m_library;
kiwix::Downloader* mp_downloader; kiwix::Downloader* mp_downloader;
ContentManager m_manager; ContentManager m_manager;
@ -92,8 +91,6 @@ private:
QWidget* mp_currentSideBar; QWidget* mp_currentSideBar;
QErrorMessage* mp_errorDialog; QErrorMessage* mp_errorDialog;
UrlSchemeHandler m_schemeHandler;
RequestInterceptor m_requestInterceptor;
QAction* mpa_actions[MAX_ACTION]; QAction* mpa_actions[MAX_ACTION];
}; };

View File

@ -1,22 +0,0 @@
#include "requestinterceptor.h"
#include <QWebEngineUrlRequestInfo>
#include <QDebug>
#include <iostream>
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));
}

View File

@ -1,14 +0,0 @@
#ifndef REQUESTINTERCEPTOR_H
#define REQUESTINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
RequestInterceptor();
virtual void interceptRequest(QWebEngineUrlRequestInfo &info);
};
#endif // REQUESTINTERCEPTOR_H

View File

@ -13,7 +13,7 @@ UrlSchemeHandler::UrlSchemeHandler()
void void
UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request)
{ {
auto qurl = request->requestUrl(); auto qurl = request->requestUrl();
std::string url = qurl.path().toUtf8().constData(); std::string url = qurl.path().toUtf8().constData();
@ -30,18 +30,18 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request)
kiwix::Entry entry; kiwix::Entry entry;
try { try {
entry = reader->getEntryFromPath(url); entry = reader->getEntryFromPath(url);
} catch (kiwix::NoEntry& e) { } catch (kiwix::NoEntry&) {
url = "A/" + url; url = "A/" + url;
try { try {
entry = reader->getEntryFromPath(url); entry = reader->getEntryFromPath(url);
} catch (kiwix::NoEntry& e) { } catch (kiwix::NoEntry&) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound); request->fail(QWebEngineUrlRequestJob::UrlNotFound);
return; return;
} }
} }
try { try {
entry = entry.getFinalEntry(); entry = entry.getFinalEntry();
} catch (kiwix::NoEntry& e) { } catch (kiwix::NoEntry&) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound); request->fail(QWebEngineUrlRequestJob::UrlNotFound);
return; return;
} }
@ -51,3 +51,44 @@ UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request)
connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater); connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater);
request->reply(mimeType, buffer); 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);
}
}

View File

@ -8,6 +8,9 @@ class UrlSchemeHandler : public QWebEngineUrlSchemeHandler
public: public:
UrlSchemeHandler(); UrlSchemeHandler();
void requestStarted(QWebEngineUrlRequestJob *request); void requestStarted(QWebEngineUrlRequestJob *request);
private:
void handleMetaRequest(QWebEngineUrlRequestJob *request);
void handleContentRequest(QWebEngineUrlRequestJob *request);
}; };
#endif // URLSCHEMEHANDLER_H #endif // URLSCHEMEHANDLER_H

View File

@ -13,7 +13,6 @@ WebView::WebView(QWidget *parent)
auto profile = page()->profile(); auto profile = page()->profile();
auto app = KiwixApp::instance(); auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
profile->setRequestInterceptor(app->getRequestInterceptor());
QObject::connect(this, &QWebEngineView::urlChanged, this, &WebView::onUrlChanged); QObject::connect(this, &QWebEngineView::urlChanged, this, &WebView::onUrlChanged);
} }