plugin code
[lbook_fbreader.git] / fbreader / src / collection / LastOpenedBooks.cpp
blob703fb52d21c33e73b66f210d2e9608c7429891ff
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <algorithm>
22 #include <ZLStringUtil.h>
23 #include <ZLOptions.h>
25 #include "BookCollection.h"
27 const std::string GROUP = "LastOpenedBooks";
28 const std::string BOOK = "Book";
30 LastOpenedBooks::LastOpenedBooks() :
31 MaxListSizeOption(ZLCategoryKey::STATE, GROUP, "MaxSize", 1, 100, 10) {
33 const int size = MaxListSizeOption.value();
34 for (int i = 0; i < size; ++i) {
35 std::string num = BOOK;
36 ZLStringUtil::appendNumber(num, i);
37 std::string name = ZLStringOption(ZLCategoryKey::STATE, GROUP, num, "").value();
38 if (!name.empty()) {
39 BookDescriptionPtr description = BookDescription::getDescription(name);
40 if (!description.isNull()) {
41 myBooks.push_back(description);
47 LastOpenedBooks::~LastOpenedBooks() {
48 const int size = std::min(MaxListSizeOption.value(), (long)myBooks.size());
49 for (int i = 0; i < size; ++i) {
50 std::string num = BOOK;
51 ZLStringUtil::appendNumber(num, i);
52 ZLStringOption(ZLCategoryKey::STATE, GROUP, num, "").setValue(myBooks[i]->fileName());
56 void LastOpenedBooks::addBook(const std::string &fileName) {
57 for (Books::iterator it = myBooks.begin(); it != myBooks.end(); ++it) {
58 if ((*it)->fileName() == fileName) {
59 myBooks.erase(it);
60 break;
63 BookDescriptionPtr description = BookDescription::getDescription(fileName);
64 if (!description.isNull()) {
65 myBooks.insert(myBooks.begin(), description);
67 const size_t maxSize = MaxListSizeOption.value();
68 if (myBooks.size() > maxSize) {
69 myBooks.erase(myBooks.begin() + maxSize, myBooks.end());