Addressed libkiwix API changes

Fixed the build failure caused by the libkiwix API changes done in
kiwix/libkiwix#636.
This commit is contained in:
Veloman Yunkan 2021-11-28 13:40:58 +04:00 committed by Matthieu Gautier
parent 643a35a996
commit 9c467d7d69
6 changed files with 23 additions and 78 deletions

View File

@ -50,7 +50,7 @@ QStringList ContentManager::getTranslations(const QStringList &keys)
QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)
{
QStringList values;
kiwix::Book* b = [=]()->kiwix::Book* {
const kiwix::Book* b = [=]()->const kiwix::Book* {
try {
return &mp_library->getBookById(id);
} catch (...) {
@ -137,7 +137,9 @@ QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &k
try {
d = mp_downloader->getDownload(b.getDownloadId());
} catch(...) {
b.setDownloadId("");
kiwix::Book bCopy(b);
bCopy.setDownloadId("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy);
mp_library->save();
emit(mp_library->booksChanged());
return values;
@ -146,11 +148,13 @@ QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &k
d->updateStatus(true);
if (d->getStatus() == kiwix::Download::K_COMPLETE) {
QString tmp(QString::fromStdString(d->getPath()));
b.setPath(QDir::toNativeSeparators(tmp).toStdString());
b.setDownloadId("");
b.setPathValid(true);
kiwix::Book bCopy(b);
bCopy.setPath(QDir::toNativeSeparators(tmp).toStdString());
bCopy.setDownloadId("");
bCopy.setPathValid(true);
// removing book url so that download link in kiwix-serve is not displayed.
b.setUrl("");
bCopy.setUrl("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy);
mp_library->save();
mp_library->bookmarksChanged();
if (!m_local) {
@ -209,7 +213,7 @@ QString ContentManager::downloadBook(const QString &id)
{
if (!mp_downloader)
return "";
auto& book = [&]()->kiwix::Book& {
const auto& book = [&]()->const kiwix::Book& {
try {
return m_remoteLibrary.getBookById(id.toStdString());
} catch (...) {
@ -233,8 +237,9 @@ QString ContentManager::downloadBook(const QString &id)
} catch (std::exception& e) {
return "";
}
book.setDownloadId(download->getDid());
mp_library->addBookToLibrary(book);
kiwix::Book bookCopy(book);
bookCopy.setDownloadId(download->getDid());
mp_library->addBookToLibrary(bookCopy);
mp_library->save();
emit(oneBookChanged(id));
return QString::fromStdString(download->getDid());

View File

@ -15,47 +15,6 @@
#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
////////////////////////////////////////////////////////////////////////////////
@ -68,7 +27,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_downloader(nullptr),
mp_manager(nullptr),
mp_mainWindow(nullptr),
m_nameMapper(m_library.getKiwixLibrary()),
m_nameMapper(m_library.getKiwixLibrary(), false),
m_server(&m_library.getKiwixLibrary(), &m_nameMapper)
{
try {

View File

@ -106,27 +106,6 @@ 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;
@ -139,7 +118,7 @@ private:
TabBar* mp_tabWidget;
SideBarType m_currentSideType;
QErrorMessage* mp_errorDialog;
NameMapperProxy m_nameMapper;
kiwix::UpdatableNameMapper m_nameMapper;
kiwix::Server m_server;
Translation m_translation;

View File

@ -9,7 +9,9 @@
class LibraryManipulator: public kiwix::LibraryManipulator {
public:
LibraryManipulator(Library* p_library)
: mp_library(p_library) {}
: kiwix::LibraryManipulator(&p_library->getKiwixLibrary())
, mp_library(p_library)
{}
virtual ~LibraryManipulator() {}
bool addBookToLibrary(kiwix::Book book) {
auto ret = mp_library->m_library.addBook(book);
@ -118,7 +120,7 @@ void Library::save()
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
}
kiwix::Book &Library::getBookById(QString id)
const kiwix::Book &Library::getBookById(QString id) const
{
return m_library.getBookById(id.toStdString());
}

View File

@ -40,7 +40,7 @@ public:
void save();
kiwix::Library& getKiwixLibrary() { return m_library; }
public slots:
kiwix::Book& getBookById(QString id);
const kiwix::Book& getBookById(QString id) const;
signals:
void booksChanged();

View File

@ -78,8 +78,8 @@ UrlSchemeHandler::handleMetaRequest(QWebEngineUrlRequestJob* request)
class IdNameMapper : public kiwix::NameMapper {
std::string getNameForId(const std::string& id) { return id + ".zim"; }
std::string getIdForName(const std::string& id) { return id.substr(0, id.size()-4); }
std::string getNameForId(const std::string& id) const { return id + ".zim"; }
std::string getIdForName(const std::string& id) const { return id.substr(0, id.size()-4); }
};