Merge pull request #992 from kiwix/adapt_libkiwix_991

This commit is contained in:
Matthieu Gautier 2023-10-12 18:07:06 +02:00 committed by GitHub
commit d721eea9d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 45 deletions

View File

@ -26,6 +26,7 @@
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent), : QObject(parent),
mp_library(library), mp_library(library),
mp_remoteLibrary(kiwix::Library::create()),
mp_downloader(downloader), mp_downloader(downloader),
m_remoteLibraryManager() m_remoteLibraryManager()
{ {
@ -185,7 +186,7 @@ void ContentManager::setCategories()
{ {
QStringList categories; QStringList categories;
if (m_local) { if (m_local) {
auto categoryData = mp_library->getKiwixLibrary().getBooksCategories(); auto categoryData = mp_library->getKiwixLibrary()->getBooksCategories();
for (auto category : categoryData) { for (auto category : categoryData) {
auto categoryName = QString::fromStdString(category); auto categoryName = QString::fromStdString(category);
categories.push_back(categoryName); categories.push_back(categoryName);
@ -201,7 +202,7 @@ void ContentManager::setLanguages()
{ {
LanguageList languages; LanguageList languages;
if (m_local) { if (m_local) {
auto languageData = mp_library->getKiwixLibrary().getBooksLanguages(); auto languageData = mp_library->getKiwixLibrary()->getBooksLanguages();
for (auto language : languageData) { for (auto language : languageData) {
auto langCode = QString::fromStdString(language); auto langCode = QString::fromStdString(language);
auto selfName = QString::fromStdString(kiwix::getLanguageSelfName(language)); auto selfName = QString::fromStdString(kiwix::getLanguageSelfName(language));
@ -224,7 +225,7 @@ QMap<QString, QVariant> ContentManager::getBookInfos(QString id, const QStringLi
} catch (...) { } catch (...) {
try { try {
QMutexLocker locker(&remoteLibraryLocker); QMutexLocker locker(&remoteLibraryLocker);
return &m_remoteLibrary.getBookById(id.toStdString()); return &mp_remoteLibrary->getBookById(id.toStdString());
} catch(...) { return nullptr; } } catch(...) { return nullptr; }
} }
}(); }();
@ -343,7 +344,7 @@ QMap<QString, QVariant> ContentManager::updateDownloadInfos(QString id, const QS
} catch(...) { } catch(...) {
kiwix::Book bCopy(b); kiwix::Book bCopy(b);
bCopy.setDownloadId(""); bCopy.setDownloadId("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy); mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy);
mp_library->save(); mp_library->save();
emit(mp_library->booksChanged()); emit(mp_library->booksChanged());
return values; return values;
@ -358,7 +359,7 @@ QMap<QString, QVariant> ContentManager::updateDownloadInfos(QString id, const QS
bCopy.setPathValid(true); bCopy.setPathValid(true);
// removing book url so that download link in kiwix-serve is not displayed. // removing book url so that download link in kiwix-serve is not displayed.
bCopy.setUrl(""); bCopy.setUrl("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy); mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy);
mp_library->save(); mp_library->save();
mp_library->bookmarksChanged(); mp_library->bookmarksChanged();
if (!m_local) { if (!m_local) {
@ -443,7 +444,7 @@ QString ContentManager::downloadBook(const QString &id)
const auto& book = [&]()->const kiwix::Book& { const auto& book = [&]()->const kiwix::Book& {
try { try {
QMutexLocker locker(&remoteLibraryLocker); QMutexLocker locker(&remoteLibraryLocker);
return m_remoteLibrary.getBookById(id.toStdString()); return mp_remoteLibrary->getBookById(id.toStdString());
} catch (...) { } catch (...) {
return mp_library->getBookById(id); return mp_library->getBookById(id);
} }
@ -684,8 +685,8 @@ void ContentManager::updateLibrary() {
void ContentManager::updateRemoteLibrary(const QString& content) { void ContentManager::updateRemoteLibrary(const QString& content) {
QtConcurrent::run([=]() { QtConcurrent::run([=]() {
QMutexLocker locker(&remoteLibraryLocker); QMutexLocker locker(&remoteLibraryLocker);
m_remoteLibrary = kiwix::Library(); mp_remoteLibrary = kiwix::Library::create();
kiwix::Manager manager(&m_remoteLibrary); kiwix::Manager manager(mp_remoteLibrary);
manager.readOpds(content.toStdString(), CATALOG_URL); manager.readOpds(content.toStdString(), CATALOG_URL);
emit(this->booksChanged()); emit(this->booksChanged());
emit(this->pendingRequest(false)); emit(this->pendingRequest(false));
@ -744,8 +745,8 @@ QStringList ContentManager::getBookIds()
} else { } else {
filter.remote(true); filter.remote(true);
QMutexLocker locker(&remoteLibraryLocker); QMutexLocker locker(&remoteLibraryLocker);
auto bookIds = m_remoteLibrary.filter(filter); auto bookIds = mp_remoteLibrary->filter(filter);
m_remoteLibrary.sort(bookIds, m_sortBy, m_sortOrderAsc); mp_remoteLibrary->sort(bookIds, m_sortBy, m_sortOrderAsc);
QStringList list; QStringList list;
for(auto& bookId:bookIds) { for(auto& bookId:bookIds) {
list.append(QString::fromStdString(bookId)); list.append(QString::fromStdString(bookId));

View File

@ -35,7 +35,7 @@ public:
private: private:
Library* mp_library; Library* mp_library;
kiwix::Library m_remoteLibrary; kiwix::LibraryPtr mp_remoteLibrary;
kiwix::Downloader* mp_downloader; kiwix::Downloader* mp_downloader;
OpdsRequestManager m_remoteLibraryManager; OpdsRequestManager m_remoteLibraryManager;
ContentManagerView* mp_view; ContentManagerView* mp_view;

View File

@ -31,8 +31,8 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_downloader(nullptr), mp_downloader(nullptr),
mp_manager(nullptr), mp_manager(nullptr),
mp_mainWindow(nullptr), mp_mainWindow(nullptr),
m_nameMapper(m_library.getKiwixLibrary(), false), mp_nameMapper(std::make_shared<kiwix::UpdatableNameMapper>(m_library.getKiwixLibrary(), false)),
m_server(&m_library.getKiwixLibrary(), &m_nameMapper) m_server(m_library.getKiwixLibrary(), mp_nameMapper)
{ {
try { try {
m_translation.setTranslation(QLocale()); m_translation.setTranslation(QLocale());
@ -457,7 +457,7 @@ void KiwixApp::handleItemsState(TabType tabType)
void KiwixApp::updateNameMapper() void KiwixApp::updateNameMapper()
{ {
m_nameMapper.update(); mp_nameMapper->update();
} }
void KiwixApp::printVersions(std::ostream& out) { void KiwixApp::printVersions(std::ostream& out) {

View File

@ -111,7 +111,7 @@ private:
ContentManager* mp_manager; ContentManager* mp_manager;
MainWindow* mp_mainWindow; MainWindow* mp_mainWindow;
QErrorMessage* mp_errorDialog; QErrorMessage* mp_errorDialog;
kiwix::UpdatableNameMapper m_nameMapper; std::shared_ptr<kiwix::UpdatableNameMapper> mp_nameMapper;
kiwix::Server m_server; kiwix::Server m_server;
Translation m_translation; Translation m_translation;
QFileSystemWatcher m_watcher; QFileSystemWatcher m_watcher;

View File

@ -11,26 +11,26 @@
class LibraryManipulator: public kiwix::LibraryManipulator { class LibraryManipulator: public kiwix::LibraryManipulator {
public: public:
LibraryManipulator(Library* p_library) LibraryManipulator(Library* p_library)
: kiwix::LibraryManipulator(&p_library->getKiwixLibrary()) : kiwix::LibraryManipulator(p_library->getKiwixLibrary())
, mp_library(p_library) , mp_library(p_library)
{} {}
virtual ~LibraryManipulator() {} virtual ~LibraryManipulator() {}
bool addBookToLibrary(kiwix::Book book) { 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()); emit(mp_library->booksChanged());
return ret; return ret;
} }
void addBookmarkToLibrary(kiwix::Bookmark bookmark) { void addBookmarkToLibrary(kiwix::Bookmark bookmark) {
mp_library->m_library.addBookmark(bookmark); mp_library->mp_library->addBookmark(bookmark);
} }
Library* mp_library; Library* mp_library;
}; };
Library::Library(const QString& libraryDirectory) Library::Library(const QString& libraryDirectory)
: m_libraryDirectory(libraryDirectory) : mp_library(kiwix::Library::create()),
m_libraryDirectory(libraryDirectory)
{ {
auto manipulator = LibraryManipulator(this); auto manager = kiwix::Manager(LibraryManipulator(this));
auto manager = kiwix::Manager(&manipulator);
manager.readFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"), false); manager.readFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"), false);
manager.readBookmarkFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.bookmarks.xml")); manager.readBookmarkFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.bookmarks.xml"));
emit(booksChanged()); emit(booksChanged());
@ -44,11 +44,11 @@ Library::~Library()
QString Library::openBookFromPath(const QString &zimPath) QString Library::openBookFromPath(const QString &zimPath)
{ {
try { try {
auto& book = m_library.getBookByPath(zimPath.toStdString()); auto& book = mp_library->getBookByPath(zimPath.toStdString());
return QString::fromStdString(book.getId()); return QString::fromStdString(book.getId());
} catch(std::out_of_range& e) { } } catch(std::out_of_range& e) { }
kiwix::Manager manager(&m_library); kiwix::Manager manager(mp_library);
auto id = manager.addBookFromPathAndGetId(zimPath.toStdString()); auto id = manager.addBookFromPathAndGetId(zimPath.toStdString());
if (id == "") { if (id == "") {
throw std::invalid_argument("invalid zim file"); throw std::invalid_argument("invalid zim file");
@ -60,18 +60,18 @@ QString Library::openBookFromPath(const QString &zimPath)
std::shared_ptr<zim::Archive> Library::getArchive(const QString &zimId) 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) 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 Library::getBookIds() const
{ {
QStringList list; QStringList list;
for(auto& id: m_library.getBooksIds()) { for(auto& id: mp_library->getBooksIds()) {
list.append(QString::fromStdString(id)); list.append(QString::fromStdString(id));
} }
return list; return list;
@ -80,8 +80,8 @@ QStringList Library::getBookIds() const
QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const
{ {
QStringList list; QStringList list;
auto bookIds = m_library.filter(filter); auto bookIds = mp_library->filter(filter);
m_library.sort(bookIds, sortBy, ascending); mp_library->sort(bookIds, sortBy, ascending);
for(auto& id: bookIds) { for(auto& id: bookIds) {
list.append(QString::fromStdString(id)); list.append(QString::fromStdString(id));
} }
@ -90,29 +90,29 @@ QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedLi
void Library::addBookToLibrary(kiwix::Book &book) void Library::addBookToLibrary(kiwix::Book &book)
{ {
m_library.addBook(book); mp_library->addBook(book);
} }
void Library::removeBookFromLibraryById(const QString& id) { void Library::removeBookFromLibraryById(const QString& id) {
m_library.removeBookById(id.toStdString()); mp_library->removeBookById(id.toStdString());
} }
void Library::addBookmark(kiwix::Bookmark &bookmark) void Library::addBookmark(kiwix::Bookmark &bookmark)
{ {
m_library.addBookmark(bookmark); mp_library->addBookmark(bookmark);
emit bookmarksChanged(); emit bookmarksChanged();
} }
void Library::removeBookmark(const QString &zimId, const QString &url) 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(); emit bookmarksChanged();
} }
void Library::save() void Library::save()
{ {
m_library.writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml")); mp_library->writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"));
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml")); mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
} }
void Library::setMonitorDirZims(QString monitorDir, QStringList zimList) void Library::setMonitorDirZims(QString monitorDir, QStringList zimList)
@ -152,15 +152,14 @@ void Library::updateFromDir(QString monitorDir)
#endif #endif
QStringList addedZims = (newDir - oldDir).values(); QStringList addedZims = (newDir - oldDir).values();
QStringList removedZims = (oldDir - newDir).values(); QStringList removedZims = (oldDir - newDir).values();
auto manipulator = LibraryManipulator(this); auto manager = kiwix::Manager(LibraryManipulator(this));
auto manager = kiwix::Manager(&manipulator);
bool needsRefresh = !removedZims.empty(); bool needsRefresh = !removedZims.empty();
for (auto book : addedZims) { for (auto book : addedZims) {
needsRefresh |= manager.addBookFromPath(book.toStdString()); needsRefresh |= manager.addBookFromPath(book.toStdString());
} }
for (auto bookPath : removedZims) { for (auto bookPath : removedZims) {
try { try {
removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId())); removeBookFromLibraryById(QString::fromStdString(mp_library->getBookByPath(bookPath.toStdString()).getId()));
} catch (...) {} } catch (...) {}
} }
if (needsRefresh) { if (needsRefresh) {
@ -178,5 +177,5 @@ void Library::asyncUpdateFromDir(QString dir)
const kiwix::Book &Library::getBookById(QString id) const const kiwix::Book &Library::getBookById(QString id) const
{ {
return m_library.getBookById(id.toStdString()); return mp_library->getBookById(id.toStdString());
} }

View File

@ -33,7 +33,7 @@ public:
std::shared_ptr<zim::Searcher> getSearcher(const QString& zimId); std::shared_ptr<zim::Searcher> getSearcher(const QString& zimId);
QStringList getBookIds() const; QStringList getBookIds() const;
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) 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; QStringList getLibraryZimsFromDir(QString dir) const;
void setMonitorDirZims(QString monitorDir, QStringList zimList); void setMonitorDirZims(QString monitorDir, QStringList zimList);
void addBookToLibrary(kiwix::Book& book); void addBookToLibrary(kiwix::Book& book);
@ -43,7 +43,7 @@ public:
void save(); void save();
void updateFromDir(QString dir); void updateFromDir(QString dir);
void asyncUpdateFromDir(QString dir); void asyncUpdateFromDir(QString dir);
kiwix::Library& getKiwixLibrary() { return m_library; } kiwix::LibraryPtr getKiwixLibrary() { return mp_library; }
public slots: public slots:
const kiwix::Book& getBookById(QString id) const; const kiwix::Book& getBookById(QString id) const;
@ -53,7 +53,7 @@ signals:
private: private:
QMutex m_updateFromDirMutex; QMutex m_updateFromDirMutex;
kiwix::Library m_library; kiwix::LibraryPtr mp_library;
QString m_libraryDirectory; QString m_libraryDirectory;
QMap<QString, QStringList> m_knownZimsInDir; QMap<QString, QStringList> m_knownZimsInDir;
friend class LibraryManipulator; friend class LibraryManipulator;

View File

@ -131,15 +131,17 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request)
request->fail(QWebEngineUrlRequestJob::UrlInvalid); request->fail(QWebEngineUrlRequestJob::UrlInvalid);
return; return;
} }
IdNameMapper nameMapper; kiwix::SearchRenderer renderer(
kiwix::SearchRenderer renderer(search->getResults(start, pageLength), &nameMapper, search->getEstimatedMatches(), search->getResults(start, pageLength),
start); search->getEstimatedMatches(),
start);
renderer.setSearchPattern(searchQuery); renderer.setSearchPattern(searchQuery);
renderer.setSearchBookQuery("content="+bookId.toStdString()); renderer.setSearchBookQuery("content="+bookId.toStdString());
renderer.setProtocolPrefix("zim://"); renderer.setProtocolPrefix("zim://");
renderer.setSearchProtocolPrefix("zim://" + host.toStdString() + "/"); renderer.setSearchProtocolPrefix("zim://" + host.toStdString() + "/");
renderer.setPageLength(pageLength); renderer.setPageLength(pageLength);
auto content = renderer.getHtml(); IdNameMapper mapper;
auto content = renderer.getHtml(mapper, nullptr);
QBuffer *buffer = new QBuffer; QBuffer *buffer = new QBuffer;
buffer->setData(content.data(), content.size()); buffer->setData(content.data(), content.size());
connect(request, &QObject::destroyed, buffer, &QObject::deleteLater); connect(request, &QObject::destroyed, buffer, &QObject::deleteLater);