From d11f027fbeaefce09d5f343aae631e6e4b566006 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Mon, 10 Oct 2011 14:24:32 +0000 Subject: [PATCH] + content manager sortBy --- src/common/kiwix/library.cpp | 17 +++++++++++++++++ src/common/kiwix/library.h | 5 +++++ src/common/kiwix/manager.cpp | 13 ++++++++++++- src/common/kiwix/manager.h | 3 ++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/common/kiwix/library.cpp b/src/common/kiwix/library.cpp index 0e2a0b84a..60bd28ebc 100644 --- a/src/common/kiwix/library.cpp +++ b/src/common/kiwix/library.cpp @@ -45,10 +45,27 @@ namespace kiwix { Book::~Book() { } + /* Sort functions */ bool Book::sortByLastOpen(const kiwix::Book &a, const kiwix::Book &b) { return atoi(a.last.c_str()) > atoi(b.last.c_str()); } + bool Book::sortByTitle(const kiwix::Book &a, const kiwix::Book &b) { + return strcmp(a.title.c_str(), b.title.c_str()) < 0; + } + + bool Book::sortByDate(const kiwix::Book &a, const kiwix::Book &b) { + return atoi(a.date.c_str()) < atoi(b.date.c_str()); + } + + bool Book::sortBySize(const kiwix::Book &a, const kiwix::Book &b) { + return atoi(a.size.c_str()) < atoi(b.size.c_str()); + } + + bool Book::sortByPublisher(const kiwix::Book &a, const kiwix::Book &b) { + return strcmp(a.creator.c_str(), b.creator.c_str()) < 0; + } + /* Constructor */ Library::Library(): current(""), diff --git a/src/common/kiwix/library.h b/src/common/kiwix/library.h index 2a050135d..147b85710 100644 --- a/src/common/kiwix/library.h +++ b/src/common/kiwix/library.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #define KIWIX_LIBRARY_VERSION "20110515" @@ -40,6 +41,10 @@ namespace kiwix { ~Book(); static bool sortByLastOpen(const Book &a, const Book &b); + 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); + static bool sortByPublisher(const Book &a, const Book &b); string id; string path; diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index b0f642df7..c6f1b6e49 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -403,10 +403,21 @@ namespace kiwix { return result; } - bool Manager::listBooks(const supportedListMode mode) { + bool Manager::listBooks(const supportedListMode mode, const supportedListSortBy sortBy) { this->bookIdList.clear(); std::vector::iterator itr; + /* Sort */ + if (sortBy == TITLE) { + std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByTitle); + } else if (sortBy == SIZE) { + std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortBySize); + } else if (sortBy == DATE) { + std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByDate); + } else if (sortBy == PUBLISHER) { + std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher); + } + if (mode == LASTOPEN) { std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLastOpen); for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 94a056b5a..c98e0495c 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -39,6 +39,7 @@ using namespace std; namespace kiwix { enum supportedListMode { LASTOPEN, REMOTE, LOCAL }; + enum supportedListSortBy { TITLE, SIZE, DATE, PUBLISHER }; class Manager { @@ -61,7 +62,7 @@ namespace kiwix { unsigned int getBookCount(const bool localBooks, const bool remoteBooks); bool updateBookLastOpenDateById(const string id); void removeBookPaths(); - bool listBooks(const supportedListMode); + bool listBooks(const supportedListMode mode, const supportedListSortBy sortBy); string writableLibraryPath;