Merge pull request #714 from kiwix/uptodate_name_mapper

This commit is contained in:
Matthieu Gautier 2021-11-03 14:15:05 +01:00 committed by GitHub
commit 7c0a249493
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 2 deletions

View File

@ -15,6 +15,51 @@
#include <thread> #include <thread>
#include <QMessageBox> #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[]) KiwixApp::KiwixApp(int& argc, char *argv[])
: QtSingleApplication("kiwix-desktop", argc, argv), : QtSingleApplication("kiwix-desktop", argc, argv),
m_profile(), m_profile(),
@ -23,7 +68,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),
m_nameMapper(m_library.getKiwixLibrary(), false), m_nameMapper(m_library.getKiwixLibrary()),
m_server(&m_library.getKiwixLibrary(), &m_nameMapper) m_server(&m_library.getKiwixLibrary(), &m_nameMapper)
{ {
try { try {
@ -411,6 +456,7 @@ void KiwixApp::postInit() {
[=](const QString& title) { emit currentTitleChanged(title); }); [=](const QString& title) { emit currentTitleChanged(title); });
connect(mp_tabWidget, &TabBar::libraryPageDisplayed, this, &KiwixApp::disableItemsOnLibraryPage); connect(mp_tabWidget, &TabBar::libraryPageDisplayed, this, &KiwixApp::disableItemsOnLibraryPage);
emit(m_library.booksChanged()); emit(m_library.booksChanged());
connect(&m_library, &Library::booksChanged, this, &KiwixApp::updateNameMapper);
disableItemsOnLibraryPage(true); disableItemsOnLibraryPage(true);
} }
@ -422,3 +468,8 @@ void KiwixApp::disableItemsOnLibraryPage(bool libraryDisplayed)
KiwixApp::instance()->getAction(KiwixApp::ZoomOutAction)->setDisabled(libraryDisplayed); KiwixApp::instance()->getAction(KiwixApp::ZoomOutAction)->setDisabled(libraryDisplayed);
KiwixApp::instance()->getAction(KiwixApp::ZoomResetAction)->setDisabled(libraryDisplayed); KiwixApp::instance()->getAction(KiwixApp::ZoomResetAction)->setDisabled(libraryDisplayed);
} }
void KiwixApp::updateNameMapper()
{
m_nameMapper.update();
}

View File

@ -18,6 +18,8 @@
#include <QTranslator> #include <QTranslator>
#include <kiwix/name_mapper.h> #include <kiwix/name_mapper.h>
#include <mutex>
class KiwixApp : public QtSingleApplication class KiwixApp : public QtSingleApplication
{ {
@ -99,11 +101,33 @@ public slots:
void toggleSideBar(KiwixApp::SideBarType type); void toggleSideBar(KiwixApp::SideBarType type);
void printPage(); void printPage();
void disableItemsOnLibraryPage(bool displayed); void disableItemsOnLibraryPage(bool displayed);
void updateNameMapper();
protected: protected:
void createAction(); void createAction();
void postInit(); 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: private:
QTranslator m_qtTranslator, m_appTranslator; QTranslator m_qtTranslator, m_appTranslator;
SettingsManager m_settingsManager; SettingsManager m_settingsManager;
@ -116,7 +140,7 @@ private:
TabBar* mp_tabWidget; TabBar* mp_tabWidget;
SideBarType m_currentSideType; SideBarType m_currentSideType;
QErrorMessage* mp_errorDialog; QErrorMessage* mp_errorDialog;
kiwix::HumanReadableNameMapper m_nameMapper; NameMapperProxy m_nameMapper;
kiwix::Server m_server; kiwix::Server m_server;
Translation m_translation; Translation m_translation;