diff --git a/src/contentManager/contentManager.cpp b/src/contentManager/contentManager.cpp index 0044a57..2115f23 100644 --- a/src/contentManager/contentManager.cpp +++ b/src/contentManager/contentManager.cpp @@ -91,53 +91,42 @@ bool ContentManager::AddBookFromPath(string &path) { return false; } -/*bool ContentManager::RemoveBookById(const char* &id, bool *retVal) { - *retVal = PR_FALSE; - const char *cid; - NS_CStringGetData(id, &cid); - - try { - if (this->manager.removeBookById(cid)) { - *retVal = PR_TRUE; +bool ContentManager::RemoveBookById(string &id) { + + try { + return this->manager.removeBookById(id.c_str()); + } catch (exception &e) { + cerr << e.what() << endl; + return false; } - } catch (exception &e) { - cerr << e.what() << endl; - } - - return NS_OK; + return false; } -bool ContentManager::SetCurrentBookId(const char* &id, bool *retVal) { - *retVal = PR_FALSE; - const char *cid; - NS_CStringGetData(id, &cid); - - try { - if (this->manager.setCurrentBookId(cid)) { - *retVal = PR_TRUE; +bool ContentManager::SetCurrentBookId(string &id) { + + try { + return this->manager.setCurrentBookId(id.c_str()); + } catch (exception &e) { + cerr << e.what() << endl; + return false; } - } catch (exception &e) { - cerr << e.what() << endl; - } - - return NS_OK; + + return false; + } + +string ContentManager::GetCurrentBookId() { + string none = ""; + try { + return this->manager.getCurrentBookId(); + } catch (exception &e) { + cerr << e.what() << endl; + return none; + } + + return none; } -bool ContentManager::GetCurrentBookId(char* &id, bool *retVal) { - *retVal = PR_FALSE; - - try { - string current = this->manager.getCurrentBookId(); - id = nsDependentCString(current.data(), current.size()); - *retVal = PR_TRUE; - } catch (exception &e) { - cerr << e.what() << endl; - } - - return NS_OK; -} - -bool ContentManager::GetBookById(const char* &id, +/*bool ContentManager::GetBookById(const char* &id, char* &path, char* &title, char* &indexPath, @@ -193,38 +182,33 @@ bool ContentManager::GetBookById(const char* &id, } return NS_OK; -} +}*/ -bool ContentManager::UpdateBookLastOpenDateById(const char* &id, bool *retVal) { - *retVal = PR_FALSE; - const char *cid; - NS_CStringGetData(id, &cid); - - try { - if (this->manager.updateBookLastOpenDateById(cid)) { - *retVal = PR_TRUE; +bool ContentManager::UpdateBookLastOpenDateById(string &id) { + try { + return this->manager.updateBookLastOpenDateById(id.c_str()); + } catch (exception &e) { + cerr << e.what() << endl; + return false; } - } catch (exception &e) { - cerr << e.what() << endl; - } - - return NS_OK; + + return false; } -bool ContentManager::GetBookCount(const bool localBooks, const bool remoteBooks, PRUint32 *count, bool *retVal) { - *retVal = PR_TRUE; - *count = 0; +unsigned int ContentManager::GetBookCount(const bool localBooks, const bool remoteBooks) { + int count = 0; - try { - *count = this->manager.getBookCount(localBooks, remoteBooks); - } catch (exception &e) { - cerr << e.what() << endl; - } + try { + return this->manager.getBookCount(localBooks, remoteBooks); + } catch (exception &e) { + cerr << e.what() << endl; + return count; + } - return NS_OK; + return count; } -bool ContentManager::ListBooks(const char* &mode, const char* &sortBy, PRUint32 maxSize, +/*bool ContentManager::ListBooks(const char* &mode, const char* &sortBy, PRUint32 maxSize, const char* &language, const char* &publisher, const char* &search, bool *retVal) { *retVal = PR_FALSE; const char *cmode; NS_CStringGetData(mode, &cmode); @@ -265,100 +249,69 @@ bool ContentManager::ListBooks(const char* &mode, const char* &sortBy, PRUint32 } - return NS_OK; -} - -bool ContentManager::GetListNextBookId(char* &id, bool *retVal) { - *retVal = PR_FALSE; - - try { - if (!this->manager.bookIdList.empty()) { - string idString = *(this->manager.bookIdList.begin()); - id = nsDependentCString(idString.data(), idString.size()); - this->manager.bookIdList.erase(this->manager.bookIdList.begin()); - *retVal = PR_TRUE; - } - } catch (exception &e) { - cerr << e.what() << endl; - } - - return NS_OK; -} - -bool ContentManager::SetBookIndex(const char* &id, const string &path, const char* &indexType, bool *retVal) { - *retVal = PR_FALSE; - - const char *cid; - NS_CStringGetData(id, &cid); - const char *cindexType; - NS_CStringGetData(indexType, &cindexType); - const char *pathToSave = strdup(nsStringToUTF8(path)); - - try { - kiwix::supportedIndexType iType; - if (std::string(cindexType) == "clucene") { - iType = kiwix::CLUCENE; - } else { - iType = kiwix::XAPIAN; - } - - if (this->manager.setBookIndex(cid, pathToSave, iType)) { - *retVal = PR_TRUE; - } - } catch (exception &e) { - cerr << e.what() << endl; - } - - free((void*)pathToSave); - - return NS_OK; -} - -bool ContentManager::SetBookPath(const char* &id, const string &path, bool *retVal) { - *retVal = PR_FALSE; - - const char *cid; - NS_CStringGetData(id, &cid); - const char *pathToSave = strdup(nsStringToUTF8(path)); - - try { - if (this->manager.setBookPath(cid, pathToSave)) { - *retVal = PR_TRUE; - } - } catch (exception &e) { - cerr << e.what() << endl; - } - - free((void*)pathToSave); - - return NS_OK; -} - -bool ContentManager::GetBooksLanguages(char* &languages, bool *retVal) { - *retVal = PR_TRUE; - string languagesStr = ""; - - vector booksLanguages = this->manager.getBooksLanguages(); - vector::iterator itr; - for ( itr = booksLanguages.begin(); itr != booksLanguages.end(); ++itr ) { - languagesStr += *itr + ";"; - } - - languages = nsDependentCString(languagesStr.data(), languagesStr.size()); - return NS_OK; -} - -bool ContentManager::GetBooksPublishers(char* &publishers, bool *retVal) { - *retVal = PR_TRUE; - string publishersStr = ""; - - vector booksPublishers = this->manager.getBooksPublishers(); - vector::iterator itr; - for ( itr = booksPublishers.begin(); itr != booksPublishers.end(); ++itr ) { - publishersStr += *itr + ";"; - } - - publishers = nsDependentCString(publishersStr.data(), publishersStr.size()); return NS_OK; }*/ +const char* ContentManager::GetListNextBookId() { + + string id; + try { + if (!this->manager.bookIdList.empty()) { + id = *(this->manager.bookIdList.begin()); + this->manager.bookIdList.erase(this->manager.bookIdList.begin()); + } + } catch (exception &e) { + cerr << e.what() << endl; + } + return id.c_str(); +} + +bool ContentManager::SetBookIndex(string &id, string &path, string &indexType) { + try { + kiwix::supportedIndexType iType; + if (indexType == "clucene") { + iType = kiwix::CLUCENE; + } else { + iType = kiwix::XAPIAN; + } + + return this->manager.setBookIndex(id.c_str(), path.c_str(), iType); + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +bool ContentManager::SetBookPath(string &id, string &path) { + try { + return this->manager.setBookPath(id.c_str(), path.c_str()); + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + return false; +} + +string ContentManager::GetBooksLanguages() { + string languagesStr = ""; + + vector booksLanguages = this->manager.getBooksLanguages(); + vector::iterator itr; + for ( itr = booksLanguages.begin(); itr != booksLanguages.end(); ++itr ) { + languagesStr += *itr + ";"; + } + return languagesStr; +} + +string ContentManager::GetBooksPublishers() { + string publishersStr = ""; + + vector booksPublishers = this->manager.getBooksPublishers(); + vector::iterator itr; + for ( itr = booksPublishers.begin(); itr != booksPublishers.end(); ++itr ) { + publishersStr += *itr + ";"; + } + return publishersStr; +} diff --git a/src/contentManager/contentManager.h b/src/contentManager/contentManager.h index 0fdd5c9..1ad405a 100644 --- a/src/contentManager/contentManager.h +++ b/src/contentManager/contentManager.h @@ -16,22 +16,22 @@ class ContentManager bool WriteLibrary(); bool WriteLibraryToFile(string&); bool AddBookFromPath(string&); - /*bool RemoveBookById(string); - bool GetBookById(string, string, string, + bool RemoveBookById(string&); + bool SetCurrentBookId(string&); + string GetCurrentBookId(); + /*bool GetBookById(string, string, string, string, string, string, string, string, string, string, string, string, - string, string); - bool SetCurrentBookId(string); - bool GetCurrentBookId(string); - bool GetListNextBookId(string); - bool SetBookIndex(string, string, string); - bool SetBookPath(string, string); - bool UpdateBookLastOpenDateById(string); - bool ListBooks(string, string, unsigned int, string, string, string); - bool GetBookCount(bool, bool, unsigned int); - bool GetBooksLanguages(string); - bool GetBooksPublishers(string);*/ + string, string);*/ + bool UpdateBookLastOpenDateById(string&); + unsigned int GetBookCount(bool, bool); + const char* GetListNextBookId(); + bool SetBookIndex(string&, string&, string&); + bool SetBookPath(string&, string&); + string GetBooksLanguages(); + string GetBooksPublishers(); + //bool ListBooks(string, string, unsigned int, string, string, string); protected: kiwix::Manager manager; diff --git a/src/contentManager/contentManagerTester.cpp b/src/contentManager/contentManagerTester.cpp index ce837df..a75c5c2 100644 --- a/src/contentManager/contentManagerTester.cpp +++ b/src/contentManager/contentManagerTester.cpp @@ -66,6 +66,44 @@ int main(int argc, char* argv[]) else cout << "Unable to write library to " << lpath << endl; + string bookid = "57b0b7c3-25a5-431e-431e-dce1771ee052963f"; + /*if (cont->RemoveBookById(bookid)) + cout << "Successfully removed book " << bookid << endl; + else + cout << "Unable to remove book " << bookid << endl;*/ + + if (cont->SetCurrentBookId(bookid)) + cout << "Successfully set current book " << bookid << endl; + else + cout << "Unable to set current book " << bookid << endl; + + cout << "Current Book ID: " << cont->GetCurrentBookId() << endl; + + if (cont->UpdateBookLastOpenDateById(bookid)) + cout << "Successfully updated last open date for book " << bookid << endl; + else + cout << "Unable to update last open date for book " << bookid << endl; + + cout << "Total book count: " << cont->GetBookCount(true, true) << endl; + + cout << "Next Book: " << cont->GetListNextBookId() << endl; + + string indexPath = "/home/reg/wksw.index"; + string indexMethod = "xapian"; + if (cont->SetBookIndex(bookid, indexPath, indexMethod)) + cout << "Successfully set index for book " << bookid << endl; + else + cout << "Unable to set index for book " << bookid << endl; + + if (cont->SetBookPath(bookid, zimPath)) + cout << "Successfully set path for book " << bookid << endl; + else + cout << "Unable to set path for book " << bookid << endl; + + cout << "Languages: " << cont->GetBooksLanguages() << endl; + + cout << "Publishers: " << cont->GetBooksPublishers() << endl; + cont->WriteLibraryToFile(lpath); delete cont;