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