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