mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-29 15:53:31 -04:00
Move the server management in kiwixapp.
`LocalKiwixServer` is "just" a UI to start/configure or stop the server. One important change is that instead of having one server we configure and start/stop when needed, we are now creating a new server each time we want to start the server. It doesn't change the behavior, as libkiwix is destroying/recreating for us, but it will help us to use the new API which use a `Server::Configuration`.
This commit is contained in:
parent
30b242f91c
commit
9a9e5815f9
@ -33,7 +33,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
|
|||||||
mp_manager(nullptr),
|
mp_manager(nullptr),
|
||||||
mp_mainWindow(nullptr),
|
mp_mainWindow(nullptr),
|
||||||
m_nameMapper(m_library.getKiwixLibrary(), false),
|
m_nameMapper(m_library.getKiwixLibrary(), false),
|
||||||
m_server(&m_library.getKiwixLibrary(), &m_nameMapper)
|
mp_server(nullptr)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
m_translation.setTranslation(QLocale());
|
m_translation.setTranslation(QLocale());
|
||||||
@ -114,7 +114,9 @@ void KiwixApp::init()
|
|||||||
|
|
||||||
KiwixApp::~KiwixApp()
|
KiwixApp::~KiwixApp()
|
||||||
{
|
{
|
||||||
m_server.stop();
|
if (mp_server) {
|
||||||
|
mp_server->stop();
|
||||||
|
}
|
||||||
if (mp_downloader) {
|
if (mp_downloader) {
|
||||||
mp_downloader->close();
|
mp_downloader->close();
|
||||||
delete mp_downloader;
|
delete mp_downloader;
|
||||||
@ -456,6 +458,24 @@ void KiwixApp::updateNameMapper()
|
|||||||
m_nameMapper.update();
|
m_nameMapper.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool KiwixApp::runServer(const QString& ipAddress, int port) {
|
||||||
|
auto settings = getSettingsManager();
|
||||||
|
if (mp_server) {
|
||||||
|
mp_server->stop();
|
||||||
|
}
|
||||||
|
mp_server.reset(new kiwix::Server(&m_library.getKiwixLibrary(), &m_nameMapper));
|
||||||
|
mp_server->setPort(port);
|
||||||
|
mp_server->setAddress(ipAddress.toStdString());
|
||||||
|
settings->setKiwixServerPort(port);
|
||||||
|
settings->setKiwixServerIpAddress(ipAddress);
|
||||||
|
return mp_server->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KiwixApp::stopServer() {
|
||||||
|
mp_server->stop();
|
||||||
|
mp_server.reset();
|
||||||
|
}
|
||||||
|
|
||||||
void KiwixApp::printVersions(std::ostream& out) {
|
void KiwixApp::printVersions(std::ostream& out) {
|
||||||
out << version.toStdString() << std::endl;
|
out << version.toStdString() << std::endl;
|
||||||
out << "+ libqt (compile time) " << QT_VERSION_STR << std::endl;
|
out << "+ libqt (compile time) " << QT_VERSION_STR << std::endl;
|
||||||
|
@ -79,12 +79,14 @@ public:
|
|||||||
TabBar* getTabWidget() { return getMainWindow()->getTabBar(); }
|
TabBar* getTabWidget() { return getMainWindow()->getTabBar(); }
|
||||||
QAction* getAction(Actions action);
|
QAction* getAction(Actions action);
|
||||||
QString getLibraryDirectory() { return m_libraryDirectory; };
|
QString getLibraryDirectory() { return m_libraryDirectory; };
|
||||||
kiwix::Server* getLocalServer() { return &m_server; }
|
|
||||||
SettingsManager* getSettingsManager() { return &m_settingsManager; };
|
SettingsManager* getSettingsManager() { return &m_settingsManager; };
|
||||||
QString getText(const QString &key) { return m_translation.getText(key); };
|
QString getText(const QString &key) { return m_translation.getText(key); };
|
||||||
void setMonitorDir(const QString &dir);
|
void setMonitorDir(const QString &dir);
|
||||||
bool isCurrentArticleBookmarked();
|
bool isCurrentArticleBookmarked();
|
||||||
|
|
||||||
|
bool runServer(const QString& ipAddress, int port);
|
||||||
|
void stopServer();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void openZimFile(const QString& zimfile="");
|
void openZimFile(const QString& zimfile="");
|
||||||
void openUrl(const QString& url, bool newTab=true);
|
void openUrl(const QString& url, bool newTab=true);
|
||||||
@ -109,7 +111,7 @@ private:
|
|||||||
MainWindow* mp_mainWindow;
|
MainWindow* mp_mainWindow;
|
||||||
QErrorMessage* mp_errorDialog;
|
QErrorMessage* mp_errorDialog;
|
||||||
kiwix::UpdatableNameMapper m_nameMapper;
|
kiwix::UpdatableNameMapper m_nameMapper;
|
||||||
kiwix::Server m_server;
|
std::unique_ptr<kiwix::Server> mp_server;
|
||||||
Translation m_translation;
|
Translation m_translation;
|
||||||
QFileSystemWatcher m_watcher;
|
QFileSystemWatcher m_watcher;
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ LocalKiwixServer::LocalKiwixServer(QWidget *parent) :
|
|||||||
QString style(byteContent);
|
QString style(byteContent);
|
||||||
setStyleSheet(style);
|
setStyleSheet(style);
|
||||||
|
|
||||||
mp_server = KiwixApp::instance()->getLocalServer();
|
|
||||||
m_port = KiwixApp::instance()->getSettingsManager()->getKiwixServerPort();
|
m_port = KiwixApp::instance()->getSettingsManager()->getKiwixServerPort();
|
||||||
|
|
||||||
connect(ui->KiwixServerButton, SIGNAL(clicked()), this, SLOT(runOrStopServer()));
|
connect(ui->KiwixServerButton, SIGNAL(clicked()), this, SLOT(runOrStopServer()));
|
||||||
@ -81,28 +80,28 @@ void LocalKiwixServer::openInBrowser()
|
|||||||
void LocalKiwixServer::runOrStopServer()
|
void LocalKiwixServer::runOrStopServer()
|
||||||
{
|
{
|
||||||
if (!m_active) {
|
if (!m_active) {
|
||||||
auto settingsManager = KiwixApp::instance()->getSettingsManager();
|
|
||||||
m_port = ui->PortChooser->text().toInt();
|
m_port = ui->PortChooser->text().toInt();
|
||||||
mp_server->setPort(m_port);
|
m_ipAddress = ui->IpChooser->currentText();
|
||||||
m_ipAddress = (ui->IpChooser->currentText() != gt("all")) ? ui->IpChooser->currentText() : "0.0.0.0";
|
|
||||||
settingsManager->setKiwixServerPort(m_port);
|
m_active = KiwixApp::instance()->runServer(
|
||||||
settingsManager->setKiwixServerIpAddress(m_ipAddress);
|
(m_ipAddress != gt("all") ? m_ipAddress : "0.0.0.0"),
|
||||||
mp_server->setAddress(m_ipAddress.toStdString());
|
m_port
|
||||||
m_ipAddress = (m_ipAddress != "0.0.0.0") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp());
|
);
|
||||||
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
|
|
||||||
ui->IpAddress->setReadOnly(true);
|
if (!m_active) {
|
||||||
if (!mp_server->start()) {
|
|
||||||
QMessageBox messageBox;
|
QMessageBox messageBox;
|
||||||
messageBox.critical(0,gt("error-title"),gt("error-launch-server-message"));
|
messageBox.critical(0,gt("error-title"),gt("error-launch-server-message"));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
m_active = true;
|
|
||||||
} else {
|
} else {
|
||||||
mp_server->stop();
|
KiwixApp::instance()->stopServer();
|
||||||
m_active = false;
|
m_active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_active) {
|
if (m_active) {
|
||||||
|
// Update UI to display how to acces the server
|
||||||
|
m_ipAddress = m_ipAddress != gt("all") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp());
|
||||||
|
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
|
||||||
|
ui->IpAddress->setReadOnly(true);
|
||||||
ui->KiwixServerButton->setText(gt("stop-kiwix-server"));
|
ui->KiwixServerButton->setText(gt("stop-kiwix-server"));
|
||||||
ui->KiwixServerText->setText(gt("kiwix-server-running-message"));
|
ui->KiwixServerText->setText(gt("kiwix-server-running-message"));
|
||||||
ui->stackedWidget->setCurrentIndex(1);
|
ui->stackedWidget->setCurrentIndex(1);
|
||||||
|
@ -22,7 +22,6 @@ public slots:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::LocalKiwixServer *ui;
|
Ui::LocalKiwixServer *ui;
|
||||||
kiwix::Server* mp_server;
|
|
||||||
bool m_active = false;
|
bool m_active = false;
|
||||||
QString m_ipAddress;
|
QString m_ipAddress;
|
||||||
int m_port;
|
int m_port;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user