Improve chances to find cover image.
[dorian] / model / ncxhandler.h
1 #ifndef NCXHANDLER_H
2 #define NCXHANDLER_H
3
4 #include <QXmlContentHandler>
5
6 #include "book.h"
7
8 /** XML content handler for NCX format. */
9 class NcxHandler: public QXmlContentHandler
10 {
11 public:
12     bool endDocument() {return true;}
13     bool endPrefixMapping(const QString &) {return true;}
14     QString errorString() const {return "";}
15     bool ignorableWhitespace(const QString &) {return true;}
16     bool processingInstruction(const QString &, const QString &) {return true;}
17     void setDocumentLocator(QXmlLocator *) {}
18     bool skippedEntity(const QString &) {return true;}
19     bool startDocument() {return true;}
20     bool startPrefixMapping(const QString &, const QString &) {return true;}
21
22     NcxHandler(Book &b): book(b)
23     {
24         book.toc.clear();
25     }
26
27     bool characters(const QString &ch)
28     {
29         currentText += ch;
30         return true;
31     }
32
33     bool endElement(const QString &namespaceUri, const QString &name,
34                     const QString &qName)
35     {
36         (void)namespaceUri;
37         (void)qName;
38         if (name == "text") {
39             contentTitle = currentText;
40         } else if (name == "navPoint") {
41             Book::ContentItem item;
42             item.href = book.rootPath() + "/" + contentUrl;
43             item.name = contentTitle;
44             book.content[contentId] = item;
45             book.toc.append(contentId);
46         }
47         return true;
48     }
49
50     bool startElement(const QString &namespaceUri, const QString &name,
51                       const QString &qName, const QXmlAttributes &attrs)
52     {
53         (void)namespaceUri;
54         (void)qName;
55         currentText = "";
56         if (name == "navPoint") {
57             contentId = attrs.value("id");
58         } else if (name == "content") {
59             contentUrl = attrs.value("src");
60         }
61         return true;
62     }
63
64 private:
65     Book &book;
66     QString currentText;
67     QString contentId;
68     QString contentUrl;
69     QString contentTitle;
70 };
71
72 #endif // NCXHANDLER_H