diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 59df14df..83318e54 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -15,6 +15,51 @@ #include #include +//////////////////////////////////////////////////////////////////////////////// +// 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 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 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(), @@ -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 { @@ -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); } @@ -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(); +} diff --git a/src/kiwixapp.h b/src/kiwixapp.h index c5a6de37..098e79a8 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -18,6 +18,8 @@ #include #include +#include + class KiwixApp : public QtSingleApplication { @@ -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 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; @@ -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;