From ab80463b58a40f035555810d79bd60d67f0f1ee3 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 23 Aug 2023 17:29:41 +0200 Subject: [PATCH] Adapt to new api introduced in kiwix/libkiwix#991 --- src/contentmanager.cpp | 21 ++++++++++--------- src/contentmanager.h | 2 +- src/kiwixapp.cpp | 6 +++--- src/kiwixapp.h | 2 +- src/library.cpp | 45 ++++++++++++++++++++-------------------- src/library.h | 6 +++--- src/urlschemehandler.cpp | 8 ++++--- 7 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index c71ef8e2..26f323d0 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -26,6 +26,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) : QObject(parent), mp_library(library), + mp_remoteLibrary(kiwix::Library::create()), mp_downloader(downloader), m_remoteLibraryManager() { @@ -185,7 +186,7 @@ void ContentManager::setCategories() { QStringList categories; if (m_local) { - auto categoryData = mp_library->getKiwixLibrary().getBooksCategories(); + auto categoryData = mp_library->getKiwixLibrary()->getBooksCategories(); for (auto category : categoryData) { auto categoryName = QString::fromStdString(category); categories.push_back(categoryName); @@ -201,7 +202,7 @@ void ContentManager::setLanguages() { LanguageList languages; if (m_local) { - auto languageData = mp_library->getKiwixLibrary().getBooksLanguages(); + auto languageData = mp_library->getKiwixLibrary()->getBooksLanguages(); for (auto language : languageData) { auto langCode = QString::fromStdString(language); auto selfName = QString::fromStdString(kiwix::getLanguageSelfName(language)); @@ -224,7 +225,7 @@ QMap ContentManager::getBookInfos(QString id, const QStringLi } catch (...) { try { QMutexLocker locker(&remoteLibraryLocker); - return &m_remoteLibrary.getBookById(id.toStdString()); + return &mp_remoteLibrary->getBookById(id.toStdString()); } catch(...) { return nullptr; } } }(); @@ -343,7 +344,7 @@ QMap ContentManager::updateDownloadInfos(QString id, const QS } catch(...) { kiwix::Book bCopy(b); bCopy.setDownloadId(""); - mp_library->getKiwixLibrary().addOrUpdateBook(bCopy); + mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy); mp_library->save(); emit(mp_library->booksChanged()); return values; @@ -358,7 +359,7 @@ QMap ContentManager::updateDownloadInfos(QString id, const QS bCopy.setPathValid(true); // removing book url so that download link in kiwix-serve is not displayed. bCopy.setUrl(""); - mp_library->getKiwixLibrary().addOrUpdateBook(bCopy); + mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy); mp_library->save(); mp_library->bookmarksChanged(); if (!m_local) { @@ -443,7 +444,7 @@ QString ContentManager::downloadBook(const QString &id) const auto& book = [&]()->const kiwix::Book& { try { QMutexLocker locker(&remoteLibraryLocker); - return m_remoteLibrary.getBookById(id.toStdString()); + return mp_remoteLibrary->getBookById(id.toStdString()); } catch (...) { return mp_library->getBookById(id); } @@ -684,8 +685,8 @@ void ContentManager::updateLibrary() { void ContentManager::updateRemoteLibrary(const QString& content) { QtConcurrent::run([=]() { QMutexLocker locker(&remoteLibraryLocker); - m_remoteLibrary = kiwix::Library(); - kiwix::Manager manager(&m_remoteLibrary); + mp_remoteLibrary = kiwix::Library::create(); + kiwix::Manager manager(mp_remoteLibrary); manager.readOpds(content.toStdString(), CATALOG_URL); emit(this->booksChanged()); emit(this->pendingRequest(false)); @@ -744,8 +745,8 @@ QStringList ContentManager::getBookIds() } else { filter.remote(true); QMutexLocker locker(&remoteLibraryLocker); - auto bookIds = m_remoteLibrary.filter(filter); - m_remoteLibrary.sort(bookIds, m_sortBy, m_sortOrderAsc); + auto bookIds = mp_remoteLibrary->filter(filter); + mp_remoteLibrary->sort(bookIds, m_sortBy, m_sortOrderAsc); QStringList list; for(auto& bookId:bookIds) { list.append(QString::fromStdString(bookId)); diff --git a/src/contentmanager.h b/src/contentmanager.h index b7f1051d..84a75bad 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -35,7 +35,7 @@ class ContentManager : public QObject private: Library* mp_library; - kiwix::Library m_remoteLibrary; + kiwix::LibraryPtr mp_remoteLibrary; kiwix::Downloader* mp_downloader; OpdsRequestManager m_remoteLibraryManager; ContentManagerView* mp_view; diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index a47274bc..290358ad 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -31,8 +31,8 @@ KiwixApp::KiwixApp(int& argc, char *argv[]) mp_downloader(nullptr), mp_manager(nullptr), mp_mainWindow(nullptr), - m_nameMapper(m_library.getKiwixLibrary(), false), - m_server(&m_library.getKiwixLibrary(), &m_nameMapper) + mp_nameMapper(std::make_shared(m_library.getKiwixLibrary(), false)), + m_server(m_library.getKiwixLibrary(), mp_nameMapper) { try { m_translation.setTranslation(QLocale()); @@ -457,7 +457,7 @@ void KiwixApp::handleItemsState(TabType tabType) void KiwixApp::updateNameMapper() { - m_nameMapper.update(); + mp_nameMapper->update(); } void KiwixApp::printVersions(std::ostream& out) { diff --git a/src/kiwixapp.h b/src/kiwixapp.h index 4a4befbb..dd0a2180 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -111,7 +111,7 @@ public slots: ContentManager* mp_manager; MainWindow* mp_mainWindow; QErrorMessage* mp_errorDialog; - kiwix::UpdatableNameMapper m_nameMapper; + std::shared_ptr mp_nameMapper; kiwix::Server m_server; Translation m_translation; QFileSystemWatcher m_watcher; diff --git a/src/library.cpp b/src/library.cpp index 7ec73185..5937f1bf 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -11,26 +11,26 @@ class LibraryManipulator: public kiwix::LibraryManipulator { public: LibraryManipulator(Library* p_library) - : kiwix::LibraryManipulator(&p_library->getKiwixLibrary()) + : kiwix::LibraryManipulator(p_library->getKiwixLibrary()) , mp_library(p_library) {} virtual ~LibraryManipulator() {} bool addBookToLibrary(kiwix::Book book) { - auto ret = mp_library->m_library.addBook(book); + auto ret = mp_library->mp_library->addBook(book); emit(mp_library->booksChanged()); return ret; } void addBookmarkToLibrary(kiwix::Bookmark bookmark) { - mp_library->m_library.addBookmark(bookmark); + mp_library->mp_library->addBookmark(bookmark); } Library* mp_library; }; Library::Library(const QString& libraryDirectory) - : m_libraryDirectory(libraryDirectory) + : mp_library(kiwix::Library::create()), + m_libraryDirectory(libraryDirectory) { - auto manipulator = LibraryManipulator(this); - auto manager = kiwix::Manager(&manipulator); + auto manager = kiwix::Manager(LibraryManipulator(this)); manager.readFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"), false); manager.readBookmarkFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.bookmarks.xml")); emit(booksChanged()); @@ -44,11 +44,11 @@ Library::~Library() QString Library::openBookFromPath(const QString &zimPath) { try { - auto& book = m_library.getBookByPath(zimPath.toStdString()); + auto& book = mp_library->getBookByPath(zimPath.toStdString()); return QString::fromStdString(book.getId()); } catch(std::out_of_range& e) { } - kiwix::Manager manager(&m_library); + kiwix::Manager manager(mp_library); auto id = manager.addBookFromPathAndGetId(zimPath.toStdString()); if (id == "") { throw std::invalid_argument("invalid zim file"); @@ -60,18 +60,18 @@ QString Library::openBookFromPath(const QString &zimPath) std::shared_ptr Library::getArchive(const QString &zimId) { - return m_library.getArchiveById(zimId.toStdString()); + return mp_library->getArchiveById(zimId.toStdString()); } std::shared_ptr Library::getSearcher(const QString &zimId) { - return m_library.getSearcherById(zimId.toStdString()); + return mp_library->getSearcherById(zimId.toStdString()); } QStringList Library::getBookIds() const { QStringList list; - for(auto& id: m_library.getBooksIds()) { + for(auto& id: mp_library->getBooksIds()) { list.append(QString::fromStdString(id)); } return list; @@ -80,8 +80,8 @@ QStringList Library::getBookIds() const QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const { QStringList list; - auto bookIds = m_library.filter(filter); - m_library.sort(bookIds, sortBy, ascending); + auto bookIds = mp_library->filter(filter); + mp_library->sort(bookIds, sortBy, ascending); for(auto& id: bookIds) { list.append(QString::fromStdString(id)); } @@ -90,29 +90,29 @@ QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedLi void Library::addBookToLibrary(kiwix::Book &book) { - m_library.addBook(book); + mp_library->addBook(book); } void Library::removeBookFromLibraryById(const QString& id) { - m_library.removeBookById(id.toStdString()); + mp_library->removeBookById(id.toStdString()); } void Library::addBookmark(kiwix::Bookmark &bookmark) { - m_library.addBookmark(bookmark); + mp_library->addBookmark(bookmark); emit bookmarksChanged(); } void Library::removeBookmark(const QString &zimId, const QString &url) { - m_library.removeBookmark(zimId.toStdString(), url.toStdString()); + mp_library->removeBookmark(zimId.toStdString(), url.toStdString()); emit bookmarksChanged(); } void Library::save() { - m_library.writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml")); - m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml")); + mp_library->writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml")); + mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml")); } void Library::setMonitorDirZims(QString monitorDir, QStringList zimList) @@ -152,15 +152,14 @@ void Library::updateFromDir(QString monitorDir) #endif QStringList addedZims = (newDir - oldDir).values(); QStringList removedZims = (oldDir - newDir).values(); - auto manipulator = LibraryManipulator(this); - auto manager = kiwix::Manager(&manipulator); + auto manager = kiwix::Manager(LibraryManipulator(this)); bool needsRefresh = !removedZims.empty(); for (auto book : addedZims) { needsRefresh |= manager.addBookFromPath(book.toStdString()); } for (auto bookPath : removedZims) { try { - removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId())); + removeBookFromLibraryById(QString::fromStdString(mp_library->getBookByPath(bookPath.toStdString()).getId())); } catch (...) {} } if (needsRefresh) { @@ -178,5 +177,5 @@ void Library::asyncUpdateFromDir(QString dir) const kiwix::Book &Library::getBookById(QString id) const { - return m_library.getBookById(id.toStdString()); + return mp_library->getBookById(id.toStdString()); } diff --git a/src/library.h b/src/library.h index b741c672..c228b33a 100644 --- a/src/library.h +++ b/src/library.h @@ -33,7 +33,7 @@ class Library : public QObject std::shared_ptr getSearcher(const QString& zimId); QStringList getBookIds() const; QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const; - const std::vector getBookmarks(bool onlyValidBookmarks = false) const { return m_library.getBookmarks(onlyValidBookmarks); } + const std::vector getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); } QStringList getLibraryZimsFromDir(QString dir) const; void setMonitorDirZims(QString monitorDir, QStringList zimList); void addBookToLibrary(kiwix::Book& book); @@ -43,7 +43,7 @@ class Library : public QObject void save(); void updateFromDir(QString dir); void asyncUpdateFromDir(QString dir); - kiwix::Library& getKiwixLibrary() { return m_library; } + kiwix::LibraryPtr getKiwixLibrary() { return mp_library; } public slots: const kiwix::Book& getBookById(QString id) const; @@ -53,7 +53,7 @@ public slots: private: QMutex m_updateFromDirMutex; - kiwix::Library m_library; + kiwix::LibraryPtr mp_library; QString m_libraryDirectory; QMap m_knownZimsInDir; friend class LibraryManipulator; diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index f18bd724..8a6dd3d3 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -131,9 +131,11 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request) request->fail(QWebEngineUrlRequestJob::UrlInvalid); return; } - IdNameMapper nameMapper; - kiwix::SearchRenderer renderer(search->getResults(start, pageLength), &nameMapper, search->getEstimatedMatches(), - start); + kiwix::SearchRenderer renderer( + search->getResults(start, pageLength), + std::make_shared(), + search->getEstimatedMatches(), + start); renderer.setSearchPattern(searchQuery); renderer.setSearchBookQuery("content="+bookId.toStdString()); renderer.setProtocolPrefix("zim://");