Make the library create the reader.

This commit is contained in:
Matthieu Gautier 2019-08-08 15:52:19 +02:00
parent e56335109c
commit ce8fff0b42
2 changed files with 19 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
#include <memory>
#include "book.h" #include "book.h"
#include "bookmark.h" #include "bookmark.h"
@ -110,6 +111,7 @@ class Filter {
class Library class Library
{ {
std::map<std::string, kiwix::Book> m_books; std::map<std::string, kiwix::Book> m_books;
std::map<std::string, std::shared_ptr<Reader>> m_readers;
std::vector<kiwix::Bookmark> m_bookmarks; std::vector<kiwix::Bookmark> m_bookmarks;
public: public:
@ -145,6 +147,7 @@ class Library
bool removeBookmark(const std::string& zimId, const std::string& url); bool removeBookmark(const std::string& zimId, const std::string& url);
Book& getBookById(const std::string& id); Book& getBookById(const std::string& id);
std::shared_ptr<Reader> getReaderById(const std::string& id);
/** /**
* Remove a book from the library. * Remove a book from the library.

View File

@ -19,6 +19,7 @@
#include "library.h" #include "library.h"
#include "book.h" #include "book.h"
#include "reader.h"
#include "libxml_dumper.h" #include "libxml_dumper.h"
#include "tools/base64.h" #include "tools/base64.h"
@ -32,6 +33,7 @@
namespace kiwix namespace kiwix
{ {
/* Constructor */ /* Constructor */
Library::Library() Library::Library()
{ {
@ -83,6 +85,20 @@ Book& Library::getBookById(const std::string& id)
return m_books.at(id); return m_books.at(id);
} }
std::shared_ptr<Reader> Library::getReaderById(const std::string& id)
{
try {
return m_readers.at(id);
} catch (std::out_of_range& e) {}
auto book = getBookById(id);
if (!book.isPathValid())
return nullptr;
auto sptr = make_shared<Reader>(book.getPath());
m_readers[id] = sptr;
return sptr;
}
unsigned int Library::getBookCount(const bool localBooks, unsigned int Library::getBookCount(const bool localBooks,
const bool remoteBooks) const bool remoteBooks)
{ {