X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=model%2Fncxhandler.h;h=05a66e1106893f233fc8e2ea9da049d553b061b8;hb=4f302c4427507fec544f32b9003035e93f32b2e2;hp=170c8ab550fe95c4280791b7b6514bb9ce0dafd8;hpb=198b9339f119bc261ee0d25a1a66b3b7c73e1226;p=dorian diff --git a/model/ncxhandler.h b/model/ncxhandler.h index 170c8ab..05a66e1 100644 --- a/model/ncxhandler.h +++ b/model/ncxhandler.h @@ -1,49 +1,76 @@ #ifndef NCXHANDLER_H #define NCXHANDLER_H -#include - +#include "xmlhandler.h" #include "book.h" #include "trace.h" /** XML content handler for NCX format. */ -class NcxHandler: public QXmlContentHandler +class NcxHandler: public XmlHandler { public: - bool endDocument() {return true;} - bool endPrefixMapping(const QString &) {return true;} - QString errorString() const {return "";} - bool ignorableWhitespace(const QString &) {return true;} - bool processingInstruction(const QString &, const QString &) {return true;} - void setDocumentLocator(QXmlLocator *) {} - bool skippedEntity(const QString &) {return true;} - bool startDocument() {return true;} - bool startPrefixMapping(const QString &, const QString &) {return true;} + struct TreeItem + { + TreeItem(const QString &i, TreeItem *p = 0): id(i), parent(p) { + if (parent) { + parent->children.append(this); + depth = parent->depth + 1; + } else { + depth = 0; + } + } + ~TreeItem() { + qDeleteAll(children); + } + void addToBook(Book &book) { + Book::ContentItem contentItem; + contentItem.href = href; + contentItem.name = QString(" ").repeated(depth) + name; + contentItem.size = 0; + book.content[id] = contentItem; + book.chapters.append(id); + qDebug() << "TreeItem::addToBook" << id << contentItem.href + << contentItem.name; + foreach (TreeItem *child, children) { + child->addToBook(book); + } + } + QList children; + QString id; + QString href; + QString name; + TreeItem *parent; + int depth; + }; - NcxHandler(Book &b): book(b) { + NcxHandler(Book &b): book(b), rootItem(0), currentItem(0) { book.chapters.clear(); } - bool characters(const QString &ch) { - currentText += ch; - return true; + ~NcxHandler() { + delete rootItem; } bool endElement(const QString &namespaceUri, const QString &name, const QString &qName) { - Trace t("NcxHandler::endElement" + name); (void)namespaceUri; (void)qName; if (name == "text") { - contentTitle = currentText; + if (currentItem) { + if (currentItem) { + currentItem->name = currentText; + } + } } else if (name == "navPoint") { - qDebug() << "url" << contentUrl << "\ntitl" << contentTitle - << "\nid" << contentId; - Book::ContentItem item; - item.href = book.rootPath() + "/" + contentUrl; - item.name = contentTitle; - book.content[contentId] = item; - book.chapters.append(contentId); + if (currentItem) { + currentItem = currentItem->parent; + if (currentItem == 0) { + // Root navigation point reached, dump TOC tree + rootItem->addToBook(book); + delete rootItem; + rootItem = 0; + } + } } return true; } @@ -54,19 +81,26 @@ public: (void)qName; currentText = ""; if (name == "navPoint") { - contentId = attrs.value("id"); + TreeItem *item = new TreeItem(attrs.value("id"), currentItem); + if (!rootItem) { + rootItem = item; + } + currentItem = item; } else if (name == "content") { - contentUrl = attrs.value("src"); + if (currentItem) { + currentItem->href = attrs.value("src"); + } } return true; } private: Book &book; - QString currentText; QString contentId; QString contentUrl; QString contentTitle; + TreeItem *rootItem; + TreeItem *currentItem; }; #endif // NCXHANDLER_H