Fix naming: book parts vs. chapters.
[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
11 /** A book. */
12 class Book: public QObject
13 {
14     Q_OBJECT
15
16 public:
17
18     /** Content item in the table of contents. */
19     struct ContentItem
20     {
21         QString href;
22         QString name;
23     };
24
25     /** Bookmark: a volume index and a relative position in volume. */
26     struct Bookmark
27     {
28         Bookmark(int part_, qreal pos_): part(part_), pos(pos_) {}
29         Bookmark() {part = pos = 0;}
30         int part;
31         qreal pos;
32         bool operator<(const Bookmark&other) const {
33             return (part == other.part)? (pos < other.pos): (part < other.part);
34         }
35     };
36
37     /** Construct a book from an EPUB file in path. */
38     Book(const QString &path, QObject *parent = 0);
39
40     /** Default constructor. */
41     Book();
42
43     /** Load book from persistent storage. */
44     void load();
45
46     /** Save book to persistent storage. */
47     void save();
48
49     /** Extract and parse EPUB contents, fill in all members except mPath. */
50     bool open();
51
52     /** Clear toc and content members, remove extracted content files. */
53     void close();
54
55     /** Return path to EPUB. */
56     QString path() const;
57
58     /**
59      * Return path to root directory of extracted EPUB.
60      * Only valid after parsing Book::opsPath().
61      */
62     QString rootPath() const;
63
64     /** Return temporary directory path for extracting EPUB file. */
65     QString tmpDir() const;
66
67     /** Clear directory. */
68     bool clearDir(const QString &directory);
69
70     /** Set last bookmark. */
71     void setLastBookmark(int part, qreal position);
72
73     /** Get last bookmark. */
74     Bookmark lastBookmark() const;
75
76     /** Add bookmark. */
77     void addBookmark(int part, qreal position);
78
79     /** Delete bookmark. */
80     void deleteBookmark(int index);
81
82     /** List bookmarks. */
83     QList<Bookmark> bookmarks() const;
84
85     /**
86      * Get friendly name.
87      * @return @see title or path name combined with author(s) name.
88      */
89     QString name() const;
90
91     /** Get short friendly name: title or file name. */
92     QString shortName() const;
93
94     /** Get chapter index from toc index. */
95     int chapterFromToc(int index);
96
97     /** Get toc index from chapter index. */
98     int tocFromChapter(int index);
99
100     QString title;                          //< Book title from EPUB.
101     QStringList toc;                        //< Table of contents from EPUB.
102     QHash<QString, ContentItem> content;    //< Content items from EPUB.
103     QImage cover;                           //< Cover image.
104     QStringList creators;                   //< Creators.
105     QString date;                           //< Date of creation.
106     QString publisher;                      //< Publisher.
107     QString datePublished;                  //< Date of publishing.
108     QString subject;                        //< Subject.
109     QString source;                         //< Source.
110     QString rights;                         //< Rights.
111     QString tocPath;                        //< Path to toc ncx.
112     QString coverPath;                      //< Path to cover html.
113     QStringList chapters;                   //< Main navigation items from EPUB.
114
115 signals:
116     /** Emitted if @see open() succeeds. */
117     void opened(const QString &bookPath);
118
119 protected:
120     /** Extract EPUB as ZIP. */
121     bool extract();
122
123     /** Parse exteacted EPUB. */
124     bool parse();
125
126     /** Clear all book fields except path. */
127     void clear();
128
129     /** Get location of OPS file in EPUB archive. */
130     QString opsPath();
131
132     QString mPath;                          //< Path to EPUB file.
133     Bookmark mLastBookmark;                 //< Last position read.
134     QList<Bookmark> mBookmarks;             //< List of bookmarks.
135     QString mRootPath;                      //< Path to root item in EPUB dir.
136 };
137
138 #endif // BOOK_H