Don't reload volume if bookmark is in the same volume.
[dorian] / book.h
1 #ifndef BOOK_H
2 #define BOOK_H
3
4 #include <QString>
5 #include <QStringList>
6 #include <QHash>
7 #include <QIcon>
8
9 /** A book. */
10 class Book
11 {
12 public:
13
14     /** Content item in the table of contents. */
15     struct ContentItem
16     {
17         QString href;
18         QString type;
19     };
20
21     /** Bookmark: a volume index and a relative position in volume. */
22     struct Bookmark
23     {
24         Bookmark(int chapter_, qreal pos_): chapter(chapter_), pos(pos_) {}
25         Bookmark() {chapter = pos = 0;}
26         int chapter;
27         qreal pos;
28         bool operator<(const Bookmark&other) const {
29             if (chapter != other.chapter) {
30                 return chapter < other.chapter;
31             } else {
32                 return pos < other.pos;
33             }
34         }
35     };
36
37     /** Construct a book from an EPUB file in path. */
38     explicit Book(const QString &path);
39
40     /** Load book from persistent storage. */
41     void load();
42
43     /** Save book to persistent storage. */
44     void save();
45
46     /** Extract and parse EPUB contents, fill in all members except mPath. */
47     void open();
48
49     /** Clear toc and content members, remove extracted content files. */
50     void close();
51
52     /** Return path to EPUB. */
53     QString path() const;
54
55     /**
56      * Return path to root directory of extracted EPUB.
57      * Only valid after parsing Book::opsPath().
58      */
59     QString rootPath() const;
60
61     /** Return temporary directory path for extracting EPUB file. */
62     QString tmpDir() const;
63
64     /** Clear directory. */
65     bool clearDir(const QString &directory);
66
67     /** Set last bookmark. */
68     void setLastBookmark(int chapter, qreal position);
69
70     /** Get last bookmark. */
71     Bookmark lastBookmark() const;
72
73     /** Add bookmark. */
74     void addBookmark(int chapter, qreal position);
75
76     /** List bookmarks. */
77     QList<Bookmark> bookmarks() const;
78
79     QString title;                          //< Book title from EPUB.
80     QStringList toc;                        //< Table of contents from EPUB.
81     QHash<QString, ContentItem> content;    //< Content items from EPUB.
82     QIcon cover;                            //< Cover image.
83     QStringList creators;                   //< Creators.
84     QString date;                           //< Date of creation.
85     QString publisher;                      //< Publisher.
86     QString datePublished;                  //< Date of publishing.
87     QString subject;                        //< Subject.
88     QString source;                         //< Source.
89     QString rights;                         //< Rights.
90
91 protected:
92     /** Indicate failure by creating a single "error" content item. */
93     void fail(const QString &details,
94               const QString &error = QString("Could not open book"));
95
96     /** Extract EPUB as ZIP. */
97     bool extract();
98
99     /** Parse exteacted EPUB. */
100     bool parse();
101
102     /** Clear all book fields except path. */
103     void clear();
104
105     /** Get location of OPS file in EPUB archive. */
106     QString opsPath();
107
108     QString mPath;                          //< Path to EPUB file.
109     Bookmark mLastBookmark;                 //< Last position read.
110     QList<Bookmark> mBookmarks;             //< List of bookmarks.
111     QString mRootPath;                      //< Path to root item in EPUB dir.
112 };
113
114 #endif // BOOK_H