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