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/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 \

View File

@ -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 {
<details v-for="book in books" class="book">
<summary class="tablerow">
<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 class="tablecell cell1">
{{ book.title }}

View File

@ -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());

View File

@ -1,10 +1,15 @@
#include "contentmanagerview.h"
#include <QFile>
#include <QWebEngineProfile>
#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());
}

View File

@ -8,7 +8,6 @@
#include "tabbar.h"
#include "tocsidebar.h"
#include "urlschemehandler.h"
#include "requestinterceptor.h"
#include <QApplication>
#include <QErrorMessage>
@ -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];
};

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
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);
}
}

View File

@ -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

View File

@ -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);
}