mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 03:26:05 -04:00
Merge pull request #992 from kiwix/adapt_libkiwix_991
This commit is contained in:
commit
d721eea9d1
@ -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<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; }
|
||||
}
|
||||
}();
|
||||
@ -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;
|
||||
@ -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) {
|
||||
@ -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));
|
||||
|
@ -35,7 +35,7 @@ public:
|
||||
|
||||
private:
|
||||
Library* mp_library;
|
||||
kiwix::Library m_remoteLibrary;
|
||||
kiwix::LibraryPtr mp_remoteLibrary;
|
||||
kiwix::Downloader* mp_downloader;
|
||||
OpdsRequestManager m_remoteLibraryManager;
|
||||
ContentManagerView* mp_view;
|
||||
|
@ -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());
|
||||
@ -457,7 +457,7 @@ void KiwixApp::handleItemsState(TabType tabType)
|
||||
|
||||
void KiwixApp::updateNameMapper()
|
||||
{
|
||||
m_nameMapper.update();
|
||||
mp_nameMapper->update();
|
||||
}
|
||||
|
||||
void KiwixApp::printVersions(std::ostream& out) {
|
||||
|
@ -111,7 +111,7 @@ private:
|
||||
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;
|
||||
|
@ -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<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;
|
||||
@ -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());
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public:
|
||||
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);
|
||||
@ -43,7 +43,7 @@ public:
|
||||
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 @@ signals:
|
||||
|
||||
private:
|
||||
QMutex m_updateFromDirMutex;
|
||||
kiwix::Library m_library;
|
||||
kiwix::LibraryPtr mp_library;
|
||||
QString m_libraryDirectory;
|
||||
QMap<QString, QStringList> m_knownZimsInDir;
|
||||
friend class LibraryManipulator;
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user