Use the server feature in kiwix-lib instead of spawning a new process.

This commit is contained in:
Matthieu Gautier 2020-10-07 14:29:48 +02:00
parent 2979668c3a
commit bbfbf2bf1e
5 changed files with 12 additions and 15 deletions

View File

@ -23,9 +23,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_downloader(nullptr),
mp_manager(nullptr),
mp_mainWindow(nullptr),
mp_server(new kiwix::KiwixServe(
appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"),
m_settingsManager.getKiwixServerPort()))
mp_server(new kiwix::Server(&m_library.getKiwixLibrary()))
{
try {
m_translation.setTranslation(QLocale());
@ -99,7 +97,7 @@ void KiwixApp::init()
KiwixApp::~KiwixApp()
{
if (mp_server) {
mp_server->shutDown();
mp_server->stop();
delete mp_server;
}
if (mp_downloader) {

View File

@ -77,7 +77,7 @@ public:
TabBar* getTabWidget() { return mp_tabWidget; }
QAction* getAction(Actions action);
QString getLibraryDirectory() { return m_libraryDirectory; };
kiwix::KiwixServe* getLocalServer() { return mp_server; }
kiwix::Server* getLocalServer() { return mp_server; }
SettingsManager* getSettingsManager() { return &m_settingsManager; };
SideBarType getSideType() { return m_currentSideType; }
QString getText(const QString &key) { return m_translation.getText(key); };
@ -113,7 +113,7 @@ private:
TabBar* mp_tabWidget;
SideBarType m_currentSideType;
QErrorMessage* mp_errorDialog;
kiwix::KiwixServe* mp_server;
kiwix::Server* mp_server;
Translation m_translation;
QAction* mpa_actions[MAX_ACTION];

View File

@ -38,6 +38,7 @@ public:
void addBookmark(kiwix::Bookmark& bookmark);
void removeBookmark(const QString& zimId, const QString& url);
void save();
kiwix::Library& getKiwixLibrary() { return m_library; }
public slots:
kiwix::Book& getBookById(QString id);

View File

@ -19,7 +19,7 @@ LocalKiwixServer::LocalKiwixServer(QWidget *parent) :
setStyleSheet(style);
mp_server = KiwixApp::instance()->getLocalServer();
m_port = mp_server->getPort();
m_port = KiwixApp::instance()->getSettingsManager()->getKiwixServerPort();
connect(ui->KiwixServerButton, SIGNAL(clicked()), this, SLOT(runOrStopServer()));
connect(ui->OpenInBrowserButton, SIGNAL(clicked()), this, SLOT(openInBrowser()));
@ -51,7 +51,7 @@ void LocalKiwixServer::openInBrowser()
QUrl url;
url.setScheme("http");
url.setHost(m_ipAddress);
url.setPort(mp_server->getPort());
url.setPort(m_port);
QDesktopServices::openUrl(url);
}
@ -59,17 +59,15 @@ void LocalKiwixServer::runOrStopServer()
{
if (!m_active) {
mp_server->setPort(m_port);
mp_server->run();
ui->IpAddress->setText(m_ipAddress + ":" + QString::number(m_port));
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!mp_server->isRunning()) {
if (!mp_server->start()) {
QMessageBox messageBox;
messageBox.critical(0,"Error","An error has occured !");
return;
}
m_active = true;
} else {
mp_server->shutDown();
mp_server->stop();
m_active = false;
}

View File

@ -2,7 +2,7 @@
#define LOCALKIWIXSERVER_H
#include <QDialog>
#include <kiwix/kiwixserve.h>
#include <kiwix/server.h>
namespace Ui {
class LocalKiwixServer;
@ -22,7 +22,7 @@ public slots:
private:
Ui::LocalKiwixServer *ui;
kiwix::KiwixServe* mp_server;
kiwix::Server* mp_server;
bool m_active = false;
QString m_ipAddress;
int m_port;