Skip to content

Commit

Permalink
Merge pull request #714 from kiwix/uptodate_name_mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Nov 3, 2021
2 parents 1586868 + 44ddebf commit 7c0a249
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
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

0 comments on commit 7c0a249

Please sign in to comment.