mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Use tabs to display KiwixWebView.
This is the first step to handling several zim file at the same time. - The Library class is used has a handler mapping zimId to kiwix::Reader. - The KiwixWebView display url of the form : "zim://<zimId>.zim/path". - The KiwixSchemeHandler uses the host (containing the zimid) to get the reader from the library and serve the content.
This commit is contained in:
parent
bb9f60acc7
commit
7e4c042824
@ -36,7 +36,8 @@ SOURCES += \
|
|||||||
kiwixapp.cpp \
|
kiwixapp.cpp \
|
||||||
blobbuffer.cpp \
|
blobbuffer.cpp \
|
||||||
kiwixrequestinterceptor.cpp \
|
kiwixrequestinterceptor.cpp \
|
||||||
kiwixwebview.cpp
|
kiwixwebview.cpp \
|
||||||
|
library.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
mainwindow.h \
|
mainwindow.h \
|
||||||
@ -44,7 +45,8 @@ HEADERS += \
|
|||||||
kiwixapp.h \
|
kiwixapp.h \
|
||||||
blobbuffer.h \
|
blobbuffer.h \
|
||||||
kiwixrequestinterceptor.h \
|
kiwixrequestinterceptor.h \
|
||||||
kiwixwebview.h
|
kiwixwebview.h \
|
||||||
|
library.h
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
mainwindow.ui
|
mainwindow.ui
|
||||||
|
20
kiwixapp.cpp
20
kiwixapp.cpp
@ -2,8 +2,7 @@
|
|||||||
#include "zim/error.h"
|
#include "zim/error.h"
|
||||||
|
|
||||||
KiwixApp::KiwixApp(int& argc, char *argv[])
|
KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||||
: QApplication(argc, argv),
|
: QApplication(argc, argv)
|
||||||
reader(nullptr)
|
|
||||||
{
|
{
|
||||||
mainWindow = new MainWindow;
|
mainWindow = new MainWindow;
|
||||||
setApplicationName("kiwix-desktop");
|
setApplicationName("kiwix-desktop");
|
||||||
@ -13,30 +12,17 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
|
|||||||
KiwixApp::~KiwixApp()
|
KiwixApp::~KiwixApp()
|
||||||
{
|
{
|
||||||
delete mainWindow;
|
delete mainWindow;
|
||||||
if (reader)
|
|
||||||
delete reader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KiwixApp::openZimFile(const QString &zimfile)
|
void KiwixApp::openZimFile(const QString &zimfile)
|
||||||
{
|
{
|
||||||
if (reader)
|
|
||||||
delete reader;
|
|
||||||
const std::string zimfile_ = zimfile.toLocal8Bit().constData();
|
const std::string zimfile_ = zimfile.toLocal8Bit().constData();
|
||||||
std::cout << "Opening " << zimfile_ << std::endl;
|
std::cout << "Opening " << zimfile_ << std::endl;
|
||||||
try {
|
try {
|
||||||
reader = new kiwix::Reader(zimfile_);
|
auto zimId = library.openBook(zimfile);
|
||||||
} catch (const zim::ZimFileFormatError& e) {
|
mainWindow->displayReader(library.getReader(zimId));
|
||||||
std::cout << "Cannot open " << zimfile_ << std::endl;
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
reader = nullptr;
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cout << "oup" << e.what() << std::endl;
|
std::cout << "oup" << e.what() << std::endl;
|
||||||
reader = nullptr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kiwix::Reader* KiwixApp::getReader()
|
|
||||||
{
|
|
||||||
return reader;
|
|
||||||
}
|
|
||||||
|
12
kiwixapp.h
12
kiwixapp.h
@ -5,7 +5,7 @@
|
|||||||
#include "kiwixrequestinterceptor.h"
|
#include "kiwixrequestinterceptor.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <kiwix/reader.h>
|
#include "library.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
|
||||||
|
|
||||||
@ -16,12 +16,16 @@ public:
|
|||||||
virtual ~KiwixApp();
|
virtual ~KiwixApp();
|
||||||
|
|
||||||
void openZimFile(const QString& zimfile);
|
void openZimFile(const QString& zimfile);
|
||||||
kiwix::Reader* getReader();
|
|
||||||
|
|
||||||
|
|
||||||
|
KiwixSchemeHandler* getSchemeHandler() { return &schemeHandler; }
|
||||||
|
KiwixRequestInterceptor* getRequestInterceptor() { return &requestIntercetor; }
|
||||||
|
Library* getLibrary() { return &library; }
|
||||||
private:
|
private:
|
||||||
kiwix::Reader* reader;
|
Library library;
|
||||||
MainWindow* mainWindow;
|
MainWindow* mainWindow;
|
||||||
|
|
||||||
|
KiwixSchemeHandler schemeHandler;
|
||||||
|
KiwixRequestInterceptor requestIntercetor;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // KIWIXAPP_H
|
#endif // KIWIXAPP_H
|
||||||
|
@ -16,11 +16,14 @@ void
|
|||||||
KiwixSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request)
|
KiwixSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request)
|
||||||
{
|
{
|
||||||
std::cout << "Handling request " << request->requestUrl().toString().toUtf8().constData() << std::endl;
|
std::cout << "Handling request " << request->requestUrl().toString().toUtf8().constData() << std::endl;
|
||||||
std::string url = request->requestUrl().path().toUtf8().constData();
|
auto qurl = request->requestUrl();
|
||||||
|
std::string url = qurl.path().toUtf8().constData();
|
||||||
std::cout << "Url is " << url << std::endl;
|
std::cout << "Url is " << url << std::endl;
|
||||||
if (url[0] == '/')
|
if (url[0] == '/')
|
||||||
url = url.substr(1);
|
url = url.substr(1);
|
||||||
auto reader = static_cast<KiwixApp*>(KiwixApp::instance())->getReader();
|
auto library = static_cast<KiwixApp*>(KiwixApp::instance())->getLibrary();
|
||||||
|
auto zim_id = qurl.host();
|
||||||
|
auto reader = library->getReader(zim_id);
|
||||||
if ( reader == nullptr) {
|
if ( reader == nullptr) {
|
||||||
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
|
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
|
||||||
return;
|
return;
|
||||||
|
@ -2,15 +2,25 @@
|
|||||||
|
|
||||||
#include <QWebEngineProfile>
|
#include <QWebEngineProfile>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "kiwixapp.h"
|
||||||
|
|
||||||
KiwixWebView::KiwixWebView(QWidget *parent)
|
KiwixWebView::KiwixWebView(QWidget *parent)
|
||||||
: QWebEngineView(parent)
|
: QWebEngineView(parent)
|
||||||
{
|
{
|
||||||
auto profile = page()->profile();
|
auto profile = page()->profile();
|
||||||
profile->installUrlSchemeHandler("zim", &schemeHandler);
|
auto app = static_cast<KiwixApp*>(KiwixApp::instance());
|
||||||
profile->setRequestInterceptor(&requestInterceptor);
|
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
|
||||||
|
profile->setRequestInterceptor(app->getRequestInterceptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
KiwixWebView::~KiwixWebView()
|
KiwixWebView::~KiwixWebView()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
void KiwixWebView::initFromReader(std::shared_ptr<kiwix::Reader> reader)
|
||||||
|
{
|
||||||
|
std::string url("zim://");
|
||||||
|
url += reader->getId();
|
||||||
|
url += ".zim/";
|
||||||
|
page()->setUrl(QUrl(QString::fromStdString(url)));
|
||||||
|
}
|
||||||
|
@ -2,9 +2,7 @@
|
|||||||
#define KIWIXWEBVIEW_H
|
#define KIWIXWEBVIEW_H
|
||||||
|
|
||||||
#include <QWebEngineView>
|
#include <QWebEngineView>
|
||||||
#include "kiwixschemehandler.h"
|
#include <kiwix/reader.h>
|
||||||
#include "kiwixrequestinterceptor.h"
|
|
||||||
|
|
||||||
|
|
||||||
class KiwixWebView : public QWebEngineView
|
class KiwixWebView : public QWebEngineView
|
||||||
{
|
{
|
||||||
@ -14,9 +12,7 @@ public:
|
|||||||
KiwixWebView(QWidget *parent = Q_NULLPTR);
|
KiwixWebView(QWidget *parent = Q_NULLPTR);
|
||||||
virtual ~KiwixWebView();
|
virtual ~KiwixWebView();
|
||||||
|
|
||||||
private:
|
void initFromReader(std::shared_ptr<kiwix::Reader> reader);
|
||||||
KiwixSchemeHandler schemeHandler;
|
|
||||||
KiwixRequestInterceptor requestInterceptor;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // KIWIXWEBVIEW_H
|
#endif // KIWIXWEBVIEW_H
|
||||||
|
30
library.cpp
Normal file
30
library.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "library.h"
|
||||||
|
|
||||||
|
Library::Library()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Library::openBook(const QString &zimPath)
|
||||||
|
{
|
||||||
|
for(auto it=readers_map.begin();
|
||||||
|
it != readers_map.end();
|
||||||
|
it++)
|
||||||
|
{
|
||||||
|
if(QString::fromStdString(it->second->getZimFilePath()) == zimPath)
|
||||||
|
return it->first;
|
||||||
|
}
|
||||||
|
const std::string zimPath_ = zimPath.toLocal8Bit().constData();
|
||||||
|
auto reader = std::shared_ptr<kiwix::Reader>(new kiwix::Reader(zimPath_));
|
||||||
|
auto id = QString::fromStdString(reader->getId() + ".zim");
|
||||||
|
readers_map[id] = reader;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<kiwix::Reader> Library::getReader(const QString &zimId)
|
||||||
|
{
|
||||||
|
auto it = readers_map.find(zimId);
|
||||||
|
if (it != readers_map.end())
|
||||||
|
return it->second;
|
||||||
|
return nullptr;
|
||||||
|
}
|
18
library.h
Normal file
18
library.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LIBRARY_H
|
||||||
|
#define LIBRARY_H
|
||||||
|
|
||||||
|
#include <kiwix/manager.h>
|
||||||
|
#include <map>
|
||||||
|
#include <qstring.h>
|
||||||
|
|
||||||
|
class Library : public kiwix::Manager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Library();
|
||||||
|
QString openBook(const QString& zimPath);
|
||||||
|
std::shared_ptr<kiwix::Reader> getReader(const QString& zimId);
|
||||||
|
private:
|
||||||
|
std::map<QString, std::shared_ptr<kiwix::Reader>> readers_map;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // LIBRARY_H
|
@ -9,10 +9,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||||||
ui(new Ui::MainWindow)
|
ui(new Ui::MainWindow)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
ui->webview->page()->setUrl(QUrl("http://foo.zim"));
|
ui->tabWidget->tabBar()->setExpanding(false);
|
||||||
//ui->webview->page()->setUrl(QUrl("http://localhost:8080"));
|
|
||||||
|
|
||||||
QObject::connect(ui->webview, SIGNAL(urlChanged(const QUrl&)), this, SLOT(on_urlChanged_triggered(const QUrl&)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
@ -20,14 +17,16 @@ MainWindow::~MainWindow()
|
|||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_pushButton_clicked()
|
void MainWindow::displayReader(std::shared_ptr<kiwix::Reader> reader)
|
||||||
{
|
{
|
||||||
ui->webview->reload();
|
auto webview = new KiwixWebView();
|
||||||
}
|
std::string favicon_content;
|
||||||
|
std::string favicon_mimetype;
|
||||||
|
reader->getFavicon(favicon_content, favicon_mimetype);
|
||||||
void MainWindow::on_urlChanged_triggered(const QUrl& url)
|
QPixmap pixmap;
|
||||||
{
|
pixmap.loadFromData((const uchar*)favicon_content.data(), favicon_content.size());
|
||||||
std::cout << "new url : " << url.toString().toUtf8().constData() << std::endl;
|
auto icon = QIcon(pixmap);
|
||||||
ui->addressBar->setText(url.toString());
|
// Ownership of webview is passed to the tabWidget
|
||||||
|
ui->tabWidget->addTab(webview, icon, QString::fromStdString(reader->getTitle()));
|
||||||
|
webview->initFromReader(reader);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include "kiwixwebview.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
@ -15,12 +16,11 @@ public:
|
|||||||
explicit MainWindow(QWidget *parent = 0);
|
explicit MainWindow(QWidget *parent = 0);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
private slots:
|
void displayReader(std::shared_ptr<kiwix::Reader> reader);
|
||||||
void on_pushButton_clicked();
|
|
||||||
void on_urlChanged_triggered(const QUrl& url);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
std::map<std::shared_ptr<kiwix::Reader>, KiwixWebView*> webviews_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
@ -16,28 +16,17 @@
|
|||||||
<widget class="QWidget" name="centralWidget">
|
<widget class="QWidget" name="centralWidget">
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
<property name="sizeConstraint">
|
<property name="tabShape">
|
||||||
<enum>QLayout::SetMinimumSize</enum>
|
<enum>QTabWidget::Rounded</enum>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<property name="currentIndex">
|
||||||
<widget class="QLineEdit" name="addressBar">
|
<number>-1</number>
|
||||||
<property name="enabled">
|
</property>
|
||||||
<bool>false</bool>
|
<property name="documentMode">
|
||||||
</property>
|
<bool>false</bool>
|
||||||
</widget>
|
</property>
|
||||||
</item>
|
</widget>
|
||||||
<item>
|
|
||||||
<widget class="QPushButton" name="refreshButton">
|
|
||||||
<property name="text">
|
|
||||||
<string>Refresh</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="KiwixWebView" name="webview" native="true"/>
|
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
@ -68,14 +57,6 @@
|
|||||||
<widget class="QStatusBar" name="statusBar"/>
|
<widget class="QStatusBar" name="statusBar"/>
|
||||||
</widget>
|
</widget>
|
||||||
<layoutdefault spacing="6" margin="11"/>
|
<layoutdefault spacing="6" margin="11"/>
|
||||||
<customwidgets>
|
|
||||||
<customwidget>
|
|
||||||
<class>KiwixWebView</class>
|
|
||||||
<extends>QWidget</extends>
|
|
||||||
<header>kiwixwebview.h</header>
|
|
||||||
<container>1</container>
|
|
||||||
</customwidget>
|
|
||||||
</customwidgets>
|
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user