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