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_downloader(nullptr),
mp_manager(nullptr), mp_manager(nullptr),
mp_mainWindow(nullptr), mp_mainWindow(nullptr),
mp_server(new kiwix::KiwixServe( mp_server(new kiwix::Server(&m_library.getKiwixLibrary()))
appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"),
m_settingsManager.getKiwixServerPort()))
{ {
try { try {
m_translation.setTranslation(QLocale()); m_translation.setTranslation(QLocale());
@ -99,7 +97,7 @@ void KiwixApp::init()
KiwixApp::~KiwixApp() KiwixApp::~KiwixApp()
{ {
if (mp_server) { if (mp_server) {
mp_server->shutDown(); mp_server->stop();
delete mp_server; delete mp_server;
} }
if (mp_downloader) { if (mp_downloader) {

View File

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

View File

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

View File

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

View File

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