From 66a9a69480f1fd0b8d8afa0f75d38deaa47863cb Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 6 Sep 2018 11:36:03 +0200 Subject: [PATCH] Move the code updating a book from a reader in the Book class. --- include/library.h | 2 ++ src/library.cpp | 21 +++++++++++++++++++++ src/manager.cpp | 31 ++----------------------------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/include/library.h b/include/library.h index 064b302c2..d3a40ab3d 100644 --- a/include/library.h +++ b/include/library.h @@ -38,6 +38,7 @@ namespace kiwix enum supportedIndexType { UNKNOWN, XAPIAN }; class OPDSDumper; +class Reader; /** * A class to store information about a book (a zim file) @@ -49,6 +50,7 @@ class Book ~Book(); bool update(const Book& other); + void update(const Reader& reader); static bool sortByTitle(const Book& a, const Book& b); static bool sortBySize(const Book& a, const Book& b); static bool sortByDate(const Book& a, const Book& b); diff --git a/src/library.cpp b/src/library.cpp index 65fc628a3..572ba0ec5 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -18,6 +18,7 @@ */ #include "library.h" +#include "reader.h" #include @@ -97,6 +98,26 @@ bool Book::update(const kiwix::Book& other) return true; } +void Book::update(const kiwix::Reader& reader) +{ + m_path = reader.getZimFilePath(); + m_id = reader.getId(); + m_description = reader.getDescription(); + m_language = reader.getLanguage(); + m_date = reader.getDate(); + m_creator = reader.getCreator(); + m_publisher = reader.getPublisher(); + m_title = reader.getTitle(); + m_name = reader.getName(); + m_tags = reader.getTags(); + m_origId = reader.getOrigId(); + m_articleCount = reader.getArticleCount(); + m_mediaCount = reader.getMediaCount(); + m_size = reader.getFileSize(); + + reader.getFavicon(m_favicon, m_faviconMimeType); +} + std::string Book::getHumanReadableIdFromPath() { std::string id = m_path; diff --git a/src/manager.cpp b/src/manager.cpp index f08790135..b84ad76c5 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -244,35 +244,8 @@ bool Manager::addBookFromPath(const string pathToOpen, bool Manager::readBookFromPath(const string path, kiwix::Book* book) { try { - kiwix::Reader* reader = new kiwix::Reader(path); - - if (book != NULL) { - book->setPath(path); - book->setId(reader->getId()); - book->setDescription(reader->getDescription()); - book->setLanguage(reader->getLanguage()); - book->setDate(reader->getDate()); - book->setCreator(reader->getCreator()); - book->setPublisher(reader->getPublisher()); - book->setTitle(reader->getTitle()); - book->setName(reader->getName()); - book->setTags(reader->getTags()); - book->setOrigId(reader->getOrigId()); - book->setArticleCount(reader->getArticleCount()); - book->setMediaCount(reader->getMediaCount()); - book->setSize(reader->getFileSize()); - - string favicon; - string faviconMimeType; - if (reader->getFavicon(favicon, faviconMimeType)) { - book->setFavicon(base64_encode( - reinterpret_cast(favicon.c_str()), - favicon.length())); - book->setFaviconMimeType(faviconMimeType); - } - } - - delete reader; + kiwix::Reader reader(path); + book->update(reader); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return false;