Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagation of library updates to the embedded kiwix server #714

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,51 @@
#include <thread>
#include <QMessageBox>

////////////////////////////////////////////////////////////////////////////////
// KiwixApp::NameMapperProxy
////////////////////////////////////////////////////////////////////////////////

KiwixApp::NameMapperProxy::NameMapperProxy(kiwix::Library& lib)
: library(lib)
{
update();
}

void KiwixApp::NameMapperProxy::update()
{
const auto newNameMapper = new kiwix::HumanReadableNameMapper(library, false);
std::lock_guard<std::mutex> lock(mutex);
nameMapper.reset(newNameMapper);
}

KiwixApp::NameMapperProxy::NameMapperHandle
KiwixApp::NameMapperProxy::currentNameMapper() const
{
// Return a copy of the handle to the current NameMapper object. It will
// ensure that the object survives any call to NameMapperProxy::update()
// made before the completion of any pending operation on that object.
std::lock_guard<std::mutex> lock(mutex);
return nameMapper;
}

std::string KiwixApp::NameMapperProxy::getNameForId(const std::string& id)
{
// Ensure that the current nameMapper object survives a concurrent call
// to NameMapperProxy::update()
return currentNameMapper()->getNameForId(id);
}

std::string KiwixApp::NameMapperProxy::getIdForName(const std::string& name)
{
// Ensure that the current nameMapper object survives a concurrent call
// to NameMapperProxy::update()
return currentNameMapper()->getIdForName(name);
}

////////////////////////////////////////////////////////////////////////////////
// KiwixApp
////////////////////////////////////////////////////////////////////////////////

KiwixApp::KiwixApp(int& argc, char *argv[])
: QtSingleApplication("kiwix-desktop", argc, argv),
m_profile(),
Expand All @@ -23,7 +68,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_downloader(nullptr),
mp_manager(nullptr),
mp_mainWindow(nullptr),
m_nameMapper(m_library.getKiwixLibrary(), false),
m_nameMapper(m_library.getKiwixLibrary()),
m_server(&m_library.getKiwixLibrary(), &m_nameMapper)
{
try {
Expand Down Expand Up @@ -411,6 +456,7 @@ void KiwixApp::postInit() {
[=](const QString& title) { emit currentTitleChanged(title); });
connect(mp_tabWidget, &TabBar::libraryPageDisplayed, this, &KiwixApp::disableItemsOnLibraryPage);
emit(m_library.booksChanged());
connect(&m_library, &Library::booksChanged, this, &KiwixApp::updateNameMapper);
disableItemsOnLibraryPage(true);
}

Expand All @@ -422,3 +468,8 @@ void KiwixApp::disableItemsOnLibraryPage(bool libraryDisplayed)
KiwixApp::instance()->getAction(KiwixApp::ZoomOutAction)->setDisabled(libraryDisplayed);
KiwixApp::instance()->getAction(KiwixApp::ZoomResetAction)->setDisabled(libraryDisplayed);
}

void KiwixApp::updateNameMapper()
{
m_nameMapper.update();
}
26 changes: 25 additions & 1 deletion src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <QTranslator>
#include <kiwix/name_mapper.h>

#include <mutex>


class KiwixApp : public QtSingleApplication
{
Expand Down Expand Up @@ -99,11 +101,33 @@ public slots:
void toggleSideBar(KiwixApp::SideBarType type);
void printPage();
void disableItemsOnLibraryPage(bool displayed);
void updateNameMapper();

protected:
void createAction();
void postInit();

private: // types
class NameMapperProxy : public kiwix::NameMapper {
typedef std::shared_ptr<kiwix::NameMapper> NameMapperHandle;
public:
explicit NameMapperProxy(kiwix::Library& library);

virtual std::string getNameForId(const std::string& id);
virtual std::string getIdForName(const std::string& name);

void update();

private:
NameMapperHandle currentNameMapper() const;

private:
mutable std::mutex mutex;
kiwix::Library& library;
NameMapperHandle nameMapper;
};


private:
QTranslator m_qtTranslator, m_appTranslator;
SettingsManager m_settingsManager;
Expand All @@ -116,7 +140,7 @@ public slots:
TabBar* mp_tabWidget;
SideBarType m_currentSideType;
QErrorMessage* mp_errorDialog;
kiwix::HumanReadableNameMapper m_nameMapper;
NameMapperProxy m_nameMapper;
kiwix::Server m_server;
Translation m_translation;

Expand Down