diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index a3cf8c3be..17041dd09 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -197,7 +197,8 @@ namespace kiwix { return library.current; } - bool Manager::addBookFromPath(const string pathToOpen, const string pathToSave, const string url, const bool checkMetaData) { + /* Add a book to the library. Return empty string if failed, book id otherwise */ + string Manager::addBookFromPathAndGetId(const string pathToOpen, const string pathToSave, const string url, const bool checkMetaData) { kiwix::Book book; if (this->readBookFromPath(pathToOpen, book)) { @@ -212,13 +213,18 @@ namespace kiwix { (checkMetaData && !book.title.empty() && !book.language.empty() && !book.date.empty())) { book.url = url; library.addBook(book); - return true; + return book.id; } } - return false; + return ""; } + /* Wrapper over Manager::addBookFromPath which return a bool instead of a string */ + bool Manager::addBookFromPath(const string pathToOpen, const string pathToSave, const string url, const bool checkMetaData) { + return !(this->addBookFromPathAndGetId(pathToOpen, pathToSave, url, checkMetaData).empty()); + } + bool Manager::readBookFromPath(const string path, kiwix::Book &book) { try { @@ -337,7 +343,8 @@ namespace kiwix { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { if ( itr->id == id) { itr->indexPath = path; - itr->indexPathAbsolute = path; + itr->indexPathAbsolute = isRelativePath(path) ? + computeAbsolutePath(removeLastPathElement(writableLibraryPath, true, false), path) : path; itr->indexType = type; return true; } @@ -351,7 +358,8 @@ namespace kiwix { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { if ( itr->id == id) { itr->path = path; - itr->pathAbsolute = path; + itr->pathAbsolute = isRelativePath(path) ? + computeAbsolutePath(removeLastPathElement(writableLibraryPath, true, false), path) : path; return true; } } diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 7e05129ec..5e5fc1d9b 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -54,7 +54,10 @@ namespace kiwix { bool setBookIndex(const string id, const string path, const supportedIndexType type); bool setBookPath(const string id, const string path); string getCurrentBookId(); - bool addBookFromPath(const string pathToOpen, const string pathToSave = "", const string url = "", const bool checkMetaData = false); + string addBookFromPathAndGetId(const string pathToOpen, const string pathToSave = "", const string url = "", + const bool checkMetaData = false); + bool addBookFromPath(const string pathToOpen, const string pathToSave = "", const string url = "", + const bool checkMetaData = false); Library cloneLibrary(); bool getBookById(const string id, Book &book); unsigned int getBookCount(const bool localBooks, const bool remoteBooks);