Increase default trace level. Set level from Developer dialog box.
[dorian] / opshandler.h
1 #ifndef OPSHANDLER_H
2 #define OPSHANDLER_H
3
4 #include <QXmlContentHandler>
5
6 #include "book.h"
7
8 /** XML content handler for OPS format. */
9 class OpsHandler: public QXmlContentHandler
10 {
11 public:
12     OpsHandler(Book &b): book(b), partCount(0) {}
13     bool endDocument() {return true;}
14     bool endPrefixMapping(const QString &) {return true;}
15     QString errorString() const {return "";}
16     bool ignorableWhitespace(const QString &) {return true;}
17     bool processingInstruction(const QString &, const QString &) {
18         return true;
19     }
20     void setDocumentLocator(QXmlLocator *) {}
21     bool skippedEntity(const QString &) {return true;}
22     bool startDocument() {return true;}
23     bool startPrefixMapping(const QString &, const QString &) {return true;}
24
25     bool characters(const QString &ch)
26     {
27         currentText += ch;
28         return true;
29     }
30
31     bool endElement(const QString &namespaceUri, const QString &name,
32                     const QString &qName)
33     {
34         (void)namespaceUri;
35         (void)qName;
36         if (currentText != "") {
37             if (name == "title") {
38                 book.title = currentText;
39             }
40             else if (name == "creator") {
41                 book.creators.append(currentText);
42             }
43             else if (name == "publisher") {
44                 book.publisher = currentText;
45             }
46             else if (name == "subject") {
47                 book.subject = currentText;
48             }
49             else if (name == "source") {
50                 book.source = currentText;
51             }
52             else if (name == "rights") {
53                 book.rights = currentText;
54             }
55         }
56         return true;
57     }
58
59     bool startElement(const QString &namespaceUri, const QString &name,
60                       const QString &qName, const QXmlAttributes &attrs)
61     {
62         (void)namespaceUri;
63         (void)qName;
64         currentText = "";
65
66         if (name == "item") {
67             Book::ContentItem item;
68             item.href = book.rootPath() + "/" + attrs.value("href");
69             item.name = QString("Part %1").arg(partCount + 1);
70             QString key = attrs.value("id");
71             book.content[key] = item;
72             partCount++;
73         }
74         else if (name == "itemref") {
75             book.toc.append(attrs.value("idref"));
76         }
77         return true;
78     }
79
80 private:
81     Book &book;
82     QString currentText;
83     int partCount;
84 };
85
86 #endif // OPSHANDLER_H