Make orientation switch explicit on Symbian, too.
[dorian] / model / ncxhandler.h
index 170c8ab..05a66e1 100644 (file)
@@ -1,49 +1,76 @@
 #ifndef NCXHANDLER_H
 #define NCXHANDLER_H
 
-#include <QXmlContentHandler>
-
+#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<TreeItem *> 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