Skip to content

Commit

Permalink
Adapt to new api introduced in kiwix/libkiwix#991
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Aug 23, 2023
1 parent 7557378 commit 97b86c2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 44 deletions.
21 changes: 11 additions & 10 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -183,7 +184,7 @@ void ContentManager::setCategories()
{
QStringList categories;
if (m_local) {
auto categoryData = mp_library->getKiwixLibrary().getBooksCategories();
auto categoryData = mp_library->getKiwixLibrary()->getBooksCategories();
categories.push_back("all");
for (auto category : categoryData) {
auto categoryName = QString::fromStdString(category);
Expand All @@ -200,7 +201,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));
Expand All @@ -223,7 +224,7 @@ QMap<QString, QVariant> 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; }
}
}();
Expand Down Expand Up @@ -342,7 +343,7 @@ QMap<QString, QVariant> 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;
Expand All @@ -357,7 +358,7 @@ QMap<QString, QVariant> 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) {
Expand Down Expand Up @@ -442,7 +443,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);
}
Expand Down Expand Up @@ -663,8 +664,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));
Expand Down Expand Up @@ -738,8 +739,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));
Expand Down
2 changes: 1 addition & 1 deletion src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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;
Expand Down
6 changes: 3 additions & 3 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<kiwix::UpdatableNameMapper>(m_library.getKiwixLibrary(), false)),
m_server(m_library.getKiwixLibrary(), mp_nameMapper)
{
try {
m_translation.setTranslation(QLocale());
Expand Down Expand Up @@ -462,7 +462,7 @@ void KiwixApp::handleItemsState(TabType tabType)

void KiwixApp::updateNameMapper()
{
m_nameMapper.update();
mp_nameMapper->update();
}

void KiwixApp::printVersions(std::ostream& out) {
Expand Down
2 changes: 1 addition & 1 deletion src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public slots:
ContentManager* mp_manager;
MainWindow* mp_mainWindow;
QErrorMessage* mp_errorDialog;
kiwix::UpdatableNameMapper m_nameMapper;
std::shared_ptr<kiwix::UpdatableNameMapper> mp_nameMapper;
kiwix::Server m_server;
Translation m_translation;
QFileSystemWatcher m_watcher;
Expand Down
45 changes: 22 additions & 23 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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");
Expand All @@ -60,18 +60,18 @@ QString Library::openBookFromPath(const QString &zimPath)

std::shared_ptr<zim::Archive> Library::getArchive(const QString &zimId)
{
return m_library.getArchiveById(zimId.toStdString());
return mp_library->getArchiveById(zimId.toStdString());
}

std::shared_ptr<zim::Searcher> 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;
Expand All @@ -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));
}
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
6 changes: 3 additions & 3 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Library : public QObject
std::shared_ptr<zim::Searcher> getSearcher(const QString& zimId);
QStringList getBookIds() const;
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const;
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return m_library.getBookmarks(onlyValidBookmarks); }
const std::vector<kiwix::Bookmark> 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);
Expand All @@ -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;

Expand All @@ -53,7 +53,7 @@ public slots:

private:
QMutex m_updateFromDirMutex;
kiwix::Library m_library;
kiwix::LibraryPtr mp_library;
QString m_libraryDirectory;
QMap<QString, QStringList> m_knownZimsInDir;
friend class LibraryManipulator;
Expand Down
8 changes: 5 additions & 3 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IdNameMapper>(),
search->getEstimatedMatches(),
start);
renderer.setSearchPattern(searchQuery);
renderer.setSearchBookQuery("content="+bookId.toStdString());
renderer.setProtocolPrefix("zim://");
Expand Down

0 comments on commit 97b86c2

Please sign in to comment.