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