New minor version. Database upgrade preparations.
[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     /** Extract and parse EPUB contents, fill in all members except mPath. */
55     bool open();
56
57     /** Extract and parse metadata only, fill in all members except mPath. */
58     void peek();
59
60     /** Clear toc and content members, remove extracted content files. */
61     void close();
62
63     /** Return path to EPUB. */
64     QString path() const;
65
66     /**
67      * Return path to root directory of extracted EPUB.
68      * Only valid after parsing Book::opsPath().
69      */
70     QString rootPath() const;
71
72     /** Return temporary directory path for extracting EPUB file. */
73     QString tmpDir() const;
74
75     /** Clear directory. */
76     bool clearDir(const QString &directory);
77
78     /** Set last bookmark. */
79     void setLastBookmark(int part, qreal position);
80
81     /** Get last bookmark. */
82     Bookmark lastBookmark() const;
83
84     /** Add bookmark. */
85     void addBookmark(int part, qreal position);
86
87     /** Delete bookmark. */
88     void deleteBookmark(int index);
89
90     /** List bookmarks. */
91     QList<Bookmark> bookmarks() const;
92
93     /**
94      * Get friendly name.
95      * @return @see title or path name combined with author(s) name.
96      */
97     QString name() const;
98
99     /** Get short friendly name: title or file name. */
100     QString shortName() const;
101
102     /** Get chapter index from part index. */
103     int chapterFromPart(int index);
104
105     /** Get part index from chapter index. */
106     int partFromChapter(int index);
107
108     /** Get progress (0..1) corresponding to part index and part position. */
109     qreal getProgress(int part, qreal position);
110
111     QString title;                          //< Book title from EPUB.
112     QStringList parts;                      //< EPUB part list.
113     QHash<QString, ContentItem> content;    //< Content items from EPUB.
114     QImage cover;                           //< Cover image.
115     QStringList creators;                   //< Creators.
116     QString date;                           //< Date of creation.
117     QString publisher;                      //< Publisher.
118     QString datePublished;                  //< Date of publishing.
119     QString subject;                        //< Subject.
120     QString source;                         //< Source.
121     QString rights;                         //< Rights.
122     QString tocPath;                        //< Path to toc NCX file.
123     QString coverPath;                      //< Path to cover HTML file.
124     QStringList chapters;                   //< Main navigation items from EPUB.
125     qint64 size;                            //< Size of all parts.
126
127 signals:
128     /** Emitted if @see open() succeeds. */
129     void opened(const QString &bookPath);
130
131 protected:
132     /** Extract EPUB as ZIP. */
133     bool extract(const QStringList &excludedExtensions);
134
135     /** Extract metadata from EPUB. */
136     bool extractMetaData();
137
138     /** Parse extracted EPUB. */
139     bool parse();
140
141     /** Clear all book fields except path. */
142     void clear();
143
144     /** Get location of OPS file in EPUB archive. */
145     QString opsPath();
146
147     QString mPath;                          //< Path to EPUB file.
148     Bookmark mLastBookmark;                 //< Last position read.
149     QList<Bookmark> mBookmarks;             //< List of bookmarks.
150     QString mRootPath;                      //< Path to root item in EPUB dir.
151     QTemporaryFile mTempFile;               //< Guards extracting books.
152 };
153
154 #endif // BOOK_H