Skip to content

Commit

Permalink
proxy for library.kiwix.org
Browse files Browse the repository at this point in the history
  • Loading branch information
mkiol committed May 10, 2022
1 parent 65e7f95 commit 34c7b48
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 12 deletions.
10 changes: 8 additions & 2 deletions qml/FilesPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ Dialog {

MenuItem {
text: qsTr("Open %1").arg("library.kiwix.org")
onClicked: Qt.openUrlExternally("https://library.kiwix.org")
onClicked: {
// Qt.openUrlExternally("https://library.kiwix.org")
Qt.openUrlExternally(zimServer.libraryLocalUrl())
}
}
MenuItem {
enabled: !fileModel.busy
Expand Down Expand Up @@ -89,7 +92,10 @@ Dialog {
anchors.bottom: parent.bottom
anchors.bottomMargin: Theme.iconSizeMedium
text: qsTr("Open %1").arg("library.kiwix.org")
onClicked: Qt.openUrlExternally("https://library.kiwix.org")
onClicked: {
// Qt.openUrlExternally("https://library.kiwix.org")
Qt.openUrlExternally(zimServer.libraryLocalUrl())
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions qml/WebViewPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ WebViewPage {
utils.readAsset("scripts/night_mode.js") + "\n" +
utils.readAsset("scripts/zoom.js") + "\n" +
"var res = {night_mode: false,zoom: false}\n" +
//"try {\n" +
"try {\n" +
"res.night_mode = _night_mode_init()\n" +
"res.zoom = _zoom_init()\n" +
//"} catch {}\n" +
"} catch {}\n" +
"return res\n"
view.runJavaScript(script, function(res) {
console.log("js init done:", JSON.stringify(res))
Expand Down
527 changes: 527 additions & 0 deletions scripts/library-kiwix-org.html

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

Utils::Utils(QObject *parent) : QObject{parent} { createCacheDir(); }

QString Utils::readAsset(const QString &path) const {
QByteArray Utils::readAssetStatic(const QString &path) {
QFile file{SailfishApp::pathTo(path).toLocalFile()};

if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "could not open file:" << path << file.errorString();
return {};
}

return QString::fromUtf8(file.readAll());
return file.readAll();
}

QString Utils::readAsset(const QString &path) const {
return QString::fromUtf8(readAssetStatic(path));
}

QString Utils::homeDir() const {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef UTILS_H
#define UTILS_H

#include <QByteArray>
#include <QColor>
#include <QObject>
#include <QString>
Expand All @@ -18,6 +19,7 @@ class Utils : public QObject {
public:
explicit Utils(QObject *parent = nullptr);
static bool createCacheDir();
static QByteArray readAssetStatic(const QString &path);
Q_INVOKABLE QString readAsset(const QString &path) const;
Q_INVOKABLE QString homeDir() const;
Q_INVOKABLE void copyToClipboard(const QString &text) const;
Expand Down
27 changes: 25 additions & 2 deletions src/zimserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "articlemodel.h"
#include "filemodel.h"
#include "settings.h"
#include "utils.h"

ZimServer *ZimServer::m_instance = nullptr;

Expand All @@ -46,7 +47,7 @@ ZimServer *ZimServer::instance(QObject *parent) {

ZimServer::ZimServer(QObject *parent) : QThread{parent}, m_server{parent} {
connect(&m_server, &QHttpServer::newRequest, this,
&ZimServer::requestHandler);
&ZimServer::handleHttpRequest);
connect(FileModel::instance(), &FileModel::busyChanged, this,
&ZimServer::handleFilemodelChanged, Qt::QueuedConnection);
connect(
Expand Down Expand Up @@ -375,9 +376,24 @@ std::pair<QString, QString> ZimServer::parseUrl(const QUrl &url) {
return result;
}

void ZimServer::requestHandler(QHttpRequest *req, QHttpResponse *resp) {
void ZimServer::handleLibraryRequest([[maybe_unused]] QHttpRequest *req,
QHttpResponse *resp) {
auto content = Utils::readAssetStatic("scripts/library-kiwix-org.html");
resp->setHeader("Content-Length", QString::number(content.size()));
resp->setHeader("Content-Type", "text/html");
resp->setHeader("Connection", "close");
resp->writeHead(200);
resp->end(content);
}

void ZimServer::handleHttpRequest(QHttpRequest *req, QHttpResponse *resp) {
qDebug() << "http request:" << req->url().path();

if (req->url().path() == "/library") {
handleLibraryRequest(req, resp);
return;
}

auto [uuid, path] = parseUrl(req->url());

if (path.isEmpty()) {
Expand Down Expand Up @@ -435,6 +451,13 @@ void ZimServer::requestHandler(QHttpRequest *req, QHttpResponse *resp) {
resp->end(art->content);
}

QUrl ZimServer::libraryLocalUrl() const {
QUrl url{"http://localhost"};
url.setPort(port);
url.setPath("/library");
return url;
}

QUrl ZimServer::localUrl(const QString &path, const QString &uuid) {
QUrl url{"http://localhost"};
url.setPort(port);
Expand Down
6 changes: 3 additions & 3 deletions src/zimserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ZimServer : public QThread {
Q_INVOKABLE void articleAsync(const QUrl &url);
Q_INVOKABLE void openUrl(const QUrl &url, const QString &title);
Q_INVOKABLE QString titleFromUrl(const QUrl &url) const;
Q_INVOKABLE QUrl libraryLocalUrl() const;
QList<SearchResult> search(QString phrase) const;

signals:
Expand All @@ -78,9 +79,6 @@ class ZimServer : public QThread {
void urlReady(const QUrl &url, const QString &title);
void busyChanged();

private slots:
void requestHandler(QHttpRequest *req, QHttpResponse *resp);

private:
enum class WorkType { none, article, loadZim };
static const int maxSearch = 20; // max size of search result
Expand Down Expand Up @@ -130,6 +128,8 @@ class ZimServer : public QThread {
void articleAsyncWork();
void loadZimAsyncWork();
QVariantList files() const;
void handleHttpRequest(QHttpRequest *req, QHttpResponse *resp);
void handleLibraryRequest(QHttpRequest *req, QHttpResponse *resp);
};

#endif // ZIMSERVER_H
3 changes: 2 additions & 1 deletion zimpedia.pro
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ include($${LIB_DIR}/qhttpserver/qhttpserver.pri)

OTHER_FILES += \
$$files(qml/*.qml) \
$$files(rpm/*)
$$files(rpm/*) \
$$files(scripts/*)

SOURCES += \
$${SRC_DIR}/main.cpp \
Expand Down

0 comments on commit 34c7b48

Please sign in to comment.