From 940368b8ace64258cc2942999749a0b184a29a71 Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Sat, 22 May 2021 21:17:45 +0530 Subject: [PATCH] Add m_archives and getArchiveById to Library These members will mirror the functionality offered by equivalent usage of Reader class. --- include/library.h | 3 +++ include/reader.h | 9 ++++++++- src/library.cpp | 29 +++++++++++++++++++++++++++-- src/reader.cpp | 5 +++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/include/library.h b/include/library.h index 95d22e4bd..c5e026e2f 100644 --- a/include/library.h +++ b/include/library.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "book.h" #include "bookmark.h" @@ -146,6 +147,7 @@ class Library { std::map m_books; std::map> m_readers; + std::map> m_archives; std::vector m_bookmarks; class BookDB; std::unique_ptr m_bookDB; @@ -198,6 +200,7 @@ class Library const Book& getBookByPath(const std::string& path) const; Book& getBookByPath(const std::string& path); std::shared_ptr getReaderById(const std::string& id); + std::shared_ptr getArchiveById(const std::string& id); /** * Remove a book from the library. diff --git a/include/reader.h b/include/reader.h index 4001a5ff7..d969ea284 100644 --- a/include/reader.h +++ b/include/reader.h @@ -91,6 +91,13 @@ class Reader * (.zim extesion). */ explicit Reader(const string zimFilePath); + + /** + * Create a Reader to read a zim file given by the Archive. + * + * @param archive The shared pointer to the Archive object. + */ + explicit Reader(const std::shared_ptr archive); #ifndef _WIN32 explicit Reader(int fd); Reader(int fd, zim::offset_type offset, zim::size_type size); @@ -488,7 +495,7 @@ class Reader zim::Archive* getZimArchive() const; protected: - std::unique_ptr zimArchive; + std::shared_ptr zimArchive; std::string zimFilePath; SuggestionsList_t suggestions; diff --git a/src/library.cpp b/src/library.cpp index 1147282df..c2e324ebc 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -108,6 +108,7 @@ bool Library::removeBookById(const std::string& id) { m_bookDB->delete_document("Q" + id); m_readers.erase(id); + m_archives.erase(id); return m_books.erase(id) == 1; } @@ -146,11 +147,35 @@ std::shared_ptr Library::getReaderById(const std::string& id) return m_readers.at(id); } catch (std::out_of_range& e) {} + try { + auto reader = make_shared(m_archives.at(id)); + m_readers[id] = reader; + return reader; + } catch (std::out_of_range& e) {} + auto book = getBookById(id); if (!book.isPathValid()) return nullptr; - auto sptr = make_shared(book.getPath()); - m_readers[id] = sptr; + + auto archive = make_shared(book.getPath()); + m_archives[id] = archive; + auto reader = make_shared(archive); + m_readers[id] = reader; + return reader; +} + +std::shared_ptr Library::getArchiveById(const std::string& id) +{ + try { + return m_archives.at(id); + } catch (std::out_of_range& e) {} + + auto book = getBookById(id); + if (!book.isPathValid()) + return nullptr; + + auto sptr = make_shared(book.getPath()); + m_archives[id] = sptr; return sptr; } diff --git a/src/reader.cpp b/src/reader.cpp index c493ca72d..4a2d84234 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -86,6 +86,11 @@ Reader::Reader(const string zimFilePath) srand(time(nullptr)); } +Reader::Reader(const std::shared_ptr archive) + : zimArchive(archive), + zimFilePath(archive->getFilename()) + {} + #ifndef _WIN32 Reader::Reader(int fd) : zimArchive(new zim::Archive(fd)),