1 /*
2 * Copyright (C) 2009-2010 Geometer Plus <contact@geometerplus.com>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifndef __BOOKSDB_H__
21 #define __BOOKSDB_H__
22
23 #include <set>
24 #include <map>
25 #include <deque>
26
27 #include "../sqldb/implsqlite/SQLiteDataBase.h"
28 #include "DBRunnables.h"
29
30 #include "../../fbreader/ReadingState.h"
31
32 class Book;
33
34 class BooksDB : public SQLiteDataBase {
35
36 public:
37 static const std::string DATABASE_NAME;
38 static const std::string STATE_DATABASE_NAME;
39 static const std::string NET_DATABASE_NAME;
40
41 static BooksDB &Instance();
42
43 private:
44 static shared_ptr<BooksDB> ourInstance;
45
46 BooksDB(const std::string &path);
47
48 public:
49 virtual ~BooksDB();
50
51 public:
52 bool initDatabase();
53 bool isInitialized() const;
54 bool clearDatabase();
55
56 shared_ptr<Book> loadBook(const std::string &fileName);
57 bool saveBook(const shared_ptr<Book> book);
58 bool saveAuthors(const shared_ptr<Book> book);
59 bool saveSeries(const shared_ptr<Book> book);
60 bool saveTags(const shared_ptr<Book> book);
61
62 int getFileSize(const std::string &fileName);
63 bool setFileSize(const std::string &fileName, int size);
64
65 bool setEncoding(const Book &book, const std::string &encoding);
66
67 bool loadFileEntries(const std::string &fileName, std::vector<std::string> &entries);
68 bool saveFileEntries(const std::string &fileName, const std::vector<std::string> &entries);
69
70 bool loadRecentBooks(std::vector<std::string> &fileNames);
71 bool saveRecentBooks(const BookList &books);
72
73 bool loadBooks(BookList &books);
74
75 bool loadBookStateStack(const Book &book, std::deque<ReadingState> &stack);
76 bool saveBookStateStack(const Book &book, const std::deque<ReadingState> &stack);
77
78 bool removeBook(const Book &book);
79
80 std::string getPalmType(const std::string &fileName);
81 bool setPalmType(const std::string &fileName, const std::string &type);
82
83 std::string getNetFile(const std::string &url);
84 bool setNetFile(const std::string &url, const std::string &fileName);
85 bool unsetNetFile(const std::string &url);
86
87 bool loadBookState(const Book &book, ReadingState &state);
88 bool setBookState(const Book &book, const ReadingState &state);
89
90 int loadStackPos(const Book &book);
91 bool setStackPos(const Book &book, int stackPos);
92
93 bool insertIntoBookList(const Book &book);
94 bool deleteFromBookList(const Book &book);
95 bool checkBookList(const Book &book);
96
97 private:
98 public:
99 shared_ptr<Tag> getTagById(int id) const;
100 void loadAllTagsById() const;
101
102 private:
103 void loadSeries(Book &book);
104 void loadSeries(const std::map<int,shared_ptr<Book> > &books);
105 void loadAuthors(Book &book);
106 void loadAuthors(const std::map<int,shared_ptr<Book> > &books);
107 void loadTags(Book &book);
108 void loadTags(const std::map<int,shared_ptr<Book> > &books);
109
110 std::string getFileName(int fileId);
111
112 private:
113 void initCommands();
114
115 private:
116 bool myInitialized;
117
118 shared_ptr<SaveTableBookRunnable> mySaveTableBook;
119 shared_ptr<SaveAuthorsRunnable> mySaveAuthors;
120 shared_ptr<SaveSeriesRunnable> mySaveSeries;
121 shared_ptr<SaveTagsRunnable> mySaveTags;
122 shared_ptr<SaveBookRunnable> mySaveBook;
123 shared_ptr<SaveFileEntriesRunnable> mySaveFileEntries;
124
125 shared_ptr<FindFileIdRunnable> myFindFileId;
126
127 shared_ptr<LoadFileEntriesRunnable> myLoadFileEntries;
128
129 shared_ptr<LoadRecentBooksRunnable> myLoadRecentBooks;
130 shared_ptr<SaveRecentBooksRunnable> mySaveRecentBooks;
131
132 shared_ptr<SaveBookStateStackRunnable> mySaveBookStateStack;
133
134 shared_ptr<DeleteBookRunnable> myDeleteBook;
135
136 shared_ptr<DBCommand> myLoadBook;
137
138 shared_ptr<DBCommand> myGetFileSize;
139 shared_ptr<DBCommand> mySetFileSize;
140
141 shared_ptr<DBCommand> myFindFileName;
142
143 shared_ptr<DBCommand> myFindAuthorId;
144
145 shared_ptr<DBCommand> myLoadBooks;
146
147 shared_ptr<DBCommand> myLoadBookStateStack;
148
149 shared_ptr<DBCommand> myGetPalmType;
150 shared_ptr<DBCommand> mySetPalmType;
151
152 shared_ptr<DBCommand> myGetNetFile;
153 shared_ptr<DBCommand> mySetNetFile;
154
155 shared_ptr<DBCommand> myLoadStackPos;
156 shared_ptr<DBCommand> mySetStackPos;
157 shared_ptr<DBCommand> myLoadBookState;
158 shared_ptr<DBCommand> mySetBookState;
159
160 shared_ptr<DBCommand> myInsertBookList;
161 shared_ptr<DBCommand> myDeleteBookList;
162 shared_ptr<DBCommand> myCheckBookList;
163
164 private: // disable copying
165 BooksDB(const BooksDB &);
166 const BooksDB &operator = (const BooksDB &);
167 };
168
169
isInitialized()170 inline bool BooksDB::isInitialized() const { return myInitialized; }
171
172 #endif /* __BOOKSDB_H__ */
173