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