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