Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to new api introduced in kiwix/libkiwix#991 #992

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);
Expand All @@ -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));
Expand All @@ -224,7 +225,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 @@ -343,7 +344,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 @@ -358,7 +359,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 @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 @@ -457,7 +457,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 @@ -111,7 +111,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
10 changes: 6 additions & 4 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ 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),
search->getEstimatedMatches(),
start);
renderer.setSearchPattern(searchQuery);
renderer.setSearchBookQuery("content="+bookId.toStdString());
renderer.setProtocolPrefix("zim://");
renderer.setSearchProtocolPrefix("zim://" + host.toStdString() + "/");
renderer.setPageLength(pageLength);
auto content = renderer.getHtml();
IdNameMapper mapper;
auto content = renderer.getHtml(mapper, nullptr);
QBuffer *buffer = new QBuffer;
buffer->setData(content.data(), content.size());
connect(request, &QObject::destroyed, buffer, &QObject::deleteLater);
Expand Down
Loading