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