From e05dd65111951f0005937bee62ccbb4bef76f86f Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sat, 7 May 2011 15:33:33 +0000 Subject: [PATCH] + last imp. in the new content manager --- src/common/kiwix/library.cpp | 4 ++++ src/common/kiwix/library.h | 8 +++++-- src/common/kiwix/manager.cpp | 44 ++++++++++++++++++++++++++++++++++-- src/common/kiwix/manager.h | 13 ++++++++--- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/common/kiwix/library.cpp b/src/common/kiwix/library.cpp index bc995cc5c..882a09dbe 100644 --- a/src/common/kiwix/library.cpp +++ b/src/common/kiwix/library.cpp @@ -43,6 +43,10 @@ namespace kiwix { Book::~Book() { } + bool Book::sortByLastOpen(const kiwix::Book &a, const kiwix::Book &b) { + return atoi(a.last.c_str()) > atoi(b.last.c_str()); + } + /* Constructor */ Library::Library(): current(""), diff --git a/src/common/kiwix/library.h b/src/common/kiwix/library.h index d9a012d9a..0a15ca917 100644 --- a/src/common/kiwix/library.h +++ b/src/common/kiwix/library.h @@ -20,11 +20,13 @@ #ifndef KIWIX_LIBRARY_H #define KIWIX_LIBRARY_H -#define KIWIX_LIBRARY_VERSION "20110503" - +#include +#include #include #include +#define KIWIX_LIBRARY_VERSION "20110503" + using namespace std; namespace kiwix { @@ -37,6 +39,8 @@ namespace kiwix { Book(); ~Book(); + static bool sortByLastOpen(const Book &a, const Book &b); + string id; string path; string last; diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index ff6da9511..f515b2650 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -46,7 +46,7 @@ namespace kiwix { book.readOnly = readOnly; book.id = bookNode.attribute("id").value(); book.path = bookNode.attribute("path").value(); - book.last = bookNode.attribute("last").value(); + book.last = bookNode.attribute("last").value() != "undefined" ? bookNode.attribute("last").value() : ""; book.indexPath = bookNode.attribute("indexPath").value(); book.indexType = bookNode.attribute("indexType").value() == "xapian" ? XAPIAN : CLUCENE; book.title = bookNode.attribute("title").value(); @@ -99,8 +99,9 @@ namespace kiwix { bookNode.append_attribute("id") = itr->id.c_str(); bookNode.append_attribute("path") = itr->path.c_str(); - if (itr->last != "") + if (itr->last != "" && itr->last != "undefined") { bookNode.append_attribute("last") = itr->last.c_str(); + } if (itr->indexPath != "") { bookNode.append_attribute("indexPath") = itr->indexPath.c_str(); @@ -219,4 +220,43 @@ namespace kiwix { } return false; } + + bool Manager::updateBookLastOpenDateById(const string id) { + std::vector::iterator itr; + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + if ( itr->id == id) { + char unixdate[12]; + sprintf (unixdate, "%d", (int)time(NULL)); + itr->last = unixdate; + return true; + } + } + + return false; + } + + bool Manager::listBooks(const supportedListMode mode) { + this->bookIdList.clear(); + std::vector::iterator itr; + + if (mode == LASTOPEN) { + std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLastOpen); + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + this->bookIdList.push_back(itr->id); + } + } else if (mode == REMOTE) { + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + if (itr->path == "") + this->bookIdList.push_back(itr->id); + } + } else { + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + if (itr->path != "") + this->bookIdList.push_back(itr->id); + } + } + + return true; + } + } diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 05d5aa22f..182a0407b 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -24,12 +24,15 @@ #include #include #include -#include "time.h" +#include +#include using namespace std; namespace kiwix { + enum supportedListMode { LASTOPEN, REMOTE, LOCAL }; + class Manager { public: @@ -45,13 +48,17 @@ namespace kiwix { bool addBookFromPath(const string path, const string url = ""); Library cloneLibrary(); bool getBookById(const string id, Book &book); - + bool updateBookLastOpenDateById(const string id); + bool listBooks(const supportedListMode); + string writableLibraryPath; + + vector bookIdList; protected: kiwix::Library library; - bool readBookFromPath(const string path, kiwix::Book &book); + bool readBookFromPath(const string path, Book &book); }; }