Show reading progress.
[dorian] / model / book.h
1 #ifndef BOOK_H
2 #define BOOK_H
3
4 #include <QString>
5 #include <QStringList>
6 #include <QHash>
7 #include <QIcon>
8 #include <QMetaType>
9 #include <QObject>
10
11 /** A book. */
12 class Book: public QObject
13 {
14     Q_OBJECT
15
16 public:
17
18     /** Content item in the table of contents. */
19     struct ContentItem
20     {
21         QString href;
22         QString name;
23         qint64 size;
24     };
25
26     /** Bookmark: a volume index and a relative position in volume. */
27     struct Bookmark
28     {
29         Bookmark(int part_, qreal pos_): part(part_), pos(pos_) {}
30         Bookmark() {part = pos = 0;}
31         int part;
32         qreal pos;
33         bool operator<(const Bookmark&other) const {
34             return (part == other.part)? (pos < other.pos): (part < other.part);
35         }
36     };
37
38     /** Construct a book from an EPUB file in path. */
39     Book(const QString &path, QObject *parent = 0);
40
41     /** Default constructor. */
42     Book();
43
44     /** Load book from persistent storage. */
45     void load();
46
47     /** Save book to persistent storage. */
48     void save();
49
50     /** Extract and parse EPUB contents, fill in all members except mPath. */
51     bool open();
52
53     /** Clear toc and content members, remove extracted content files. */
54     void close();
55
56     /** Return path to EPUB. */
57     QString path() const;
58
59     /**
60      * Return path to root directory of extracted EPUB.
61      * Only valid after parsing Book::opsPath().
62      */
63     QString rootPath() const;
64
65     /** Return temporary directory path for extracting EPUB file. */
66     QString tmpDir() const;
67
68     /** Clear directory. */
69     bool clearDir(const QString &directory);
70
71     /** Set last bookmark. */
72     void setLastBookmark(int part, qreal position);
73
74     /** Get last bookmark. */
75     Bookmark lastBookmark() const;
76
77     /** Add bookmark. */
78     void addBookmark(int part, qreal position);
79
80     /** Delete bookmark. */
81     void deleteBookmark(int index);
82
83     /** List bookmarks. */
84     QList<Bookmark> bookmarks() const;
85
86     /**
87      * Get friendly name.
88      * @return @see title or path name combined with author(s) name.
89      */
90     QString name() const;
91
92     /** Get short friendly name: title or file name. */
93     QString shortName() const;
94
95     /** Get chapter index from part index. */
96     int chapterFromPart(int index);
97
98     /** Get part index from chapter index. */
99     int partFromChapter(int index);
100
101     /** Get progress (0..1) corresponding to part index and part position. */
102     qreal getProgress(int part, qreal position);
103
104     QString title;                          //< Book title from EPUB.
105     QStringList parts;                      //< EPUB part list.
106     QHash<QString, ContentItem> content;    //< Content items from EPUB.
107     QImage cover;                           //< Cover image.
108     QStringList creators;                   //< Creators.
109     QString date;                           //< Date of creation.
110     QString publisher;                      //< Publisher.
111     QString datePublished;                  //< Date of publishing.
112     QString subject;                        //< Subject.
113     QString source;                         //< Source.
114     QString rights;                         //< Rights.
115     QString tocPath;                        //< Path to toc ncx.
116     QString coverPath;                      //< Path to cover html.
117     QStringList chapters;                   //< Main navigation items from EPUB.
118     qint64 size;                            //< Size of all parts.
119
120 signals:
121     /** Emitted if @see open() succeeds. */
122     void opened(const QString &bookPath);
123
124 protected:
125     /** Extract EPUB as ZIP. */
126     bool extract();
127
128     /** Parse extracted EPUB. */
129     bool parse();
130
131     /** Clear all book fields except path. */
132     void clear();
133
134     /** Get location of OPS file in EPUB archive. */
135     QString opsPath();
136
137     QString mPath;                          //< Path to EPUB file.
138     Bookmark mLastBookmark;                 //< Last position read.
139     QList<Bookmark> mBookmarks;             //< List of bookmarks.
140     QString mRootPath;                      //< Path to root item in EPUB dir.
141 };
142
143 #endif // BOOK_H