Mercurial > projects > qtd
diff demos/browser/xbel.d @ 73:7bfd46c330dc
more porting
author | mandel |
---|---|
date | Fri, 22 May 2009 10:59:00 +0000 |
parents | 71b382c10ef6 |
children | 37caa90ce503 |
line wrap: on
line diff
--- a/demos/browser/xbel.d Wed May 20 22:44:31 2009 +0000 +++ b/demos/browser/xbel.d Fri May 22 10:59:00 2009 +0000 @@ -41,319 +41,317 @@ module xbel; -import QtCore.QXmlStreamReader; -import QtCore.QDateTime; +import qt.core.QXmlStreamReader; +import qt.core.QDateTime; +import qt.core.QFile; -import QtCore.QFile; class BookmarkNode { public: - enum Type { - Root, - Folder, - Bookmark, - Separator - }; + + enum Type { + Root, + Folder, + Bookmark, + Separator + }; - this(Type type = Root, BookmarkNode *parent = null) -{ - expanded = false; - m_parent = parent; - m_type = type; - - if (parent) - parent.add(this); -} - - ~this() -{ - if (m_parent) - m_parent.remove(this); - qDeleteAll(m_children); - m_parent = 0; - m_type = BookmarkNode::Root; -} + this(Type type = Root, BookmarkNode parent = null) + { + expanded = false; + m_parent = parent; + m_type = type; + + if (parent) + parent.add(this); + } + + ~this() + { + if (m_parent) + m_parent.remove(this); + qDeleteAll(m_children); + m_parent = 0; + m_type = BookmarkNode.Root; + } - bool operator==(const BookmarkNode &other) -{ - if (url != other.url - || title != other.title - || desc != other.desc - || expanded != other.expanded - || m_type != other.m_type - || m_children.count() != other.m_children.count()) - return false; + bool operator==(BookmarkNode other) + { + if (url != other.url || title != other.title || desc != other.desc || expanded != other.expanded + || m_type != other.m_type || m_children.count() != other.m_children.count()) + return false; + + for (int i = 0; i < m_children.count(); ++i) + if (!((*(m_children[i])) == (*(other.m_children[i])))) + return false; + return true; + } - for (int i = 0; i < m_children.count(); ++i) - if (!((*(m_children[i])) == (*(other.m_children[i])))) - return false; - return true; -} + Type type() const + { + return m_type; + } - Type type() const -{ - return m_type; -} + void setType(Type type) + { + m_type = type; + } - void setType(Type type) -{ - m_type = type; -} - - QList<BookmarkNode *> children() const -{ - return m_children; -} + QList<BookmarkNode > children() const + { + return m_children; + } - BookmarkNode *parent() const -{ - return m_parent; -} + BookmarkNode parent() const + { + return m_parent; + } - void add(BookmarkNode *child, int offset = -1) -{ - Q_ASSERT(child.m_type != Root); - if (child.m_parent) - child.m_parent.remove(child); - child.m_parent = this; - if (-1 == offset) - offset = m_children.size(); - m_children.insert(offset, child); -} + void add(BookmarkNode child, int offset = -1) + { + assert(child.m_type != Root); + if (child.m_parent) + child.m_parent.remove(child); + child.m_parent = this; + if (-1 == offset) + offset = m_children.size(); + m_children.insert(offset, child); + } - void remove(BookmarkNode *child) -{ - child.m_parent = 0; - m_children.removeAll(child); -} + void remove(BookmarkNode child) + { + child.m_parent = 0; + m_children.removeAll(child); + } - QString url; - QString title; - QString desc; - bool expanded; + QString url; + QString title; + QString desc; + bool expanded; private: - BookmarkNode *m_parent; - Type m_type; - QList<BookmarkNode *> m_children; -}; + BookmarkNode m_parent; + Type m_type; + QList<BookmarkNode> m_children; +} class XbelReader : public QXmlStreamReader { public: -this() -{ -} + + this() + { + } -BookmarkNode *read(const QString &fileName) -{ - QFile file(fileName); - if (!file.exists()) { - return new BookmarkNode(BookmarkNode::Root); - } - file.open(QFile::ReadOnly); - return read(&file); -} + BookmarkNode read(QString fileName) + { + QFile file(fileName); + if (!file.exists()) { + return new BookmarkNode(BookmarkNode.Root); + } + file.open(QFile.ReadOnly); + return read(&file); + } -BookmarkNode *read(QIODevice *device) -{ - BookmarkNode *root = new BookmarkNode(BookmarkNode::Root); - setDevice(device); - while (!atEnd()) { - readNext(); - if (isStartElement()) { - QString version = attributes().value(QLatin1String("version")).toString(); - if (name() == QLatin1String("xbel") - && (version.isEmpty() || version == QLatin1String("1.0"))) { - readXBEL(root); - } else { - raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); - } - } - } - return root; -} + BookmarkNode read(QIODevice device) + { + BookmarkNode root = new BookmarkNode(BookmarkNode.Root); + setDevice(device); + while (!atEnd()) { + readNext(); + if (isStartElement()) { + QString version_ = attributes().value(QLatin1String("version")).toString(); + if (name() == QLatin1String("xbel") && (version_.isEmpty() || version_ == QLatin1String("1.0"))) { + readXBEL(root); + } else { + raiseError(QObject.tr("The file is not an XBEL version 1.0 file.")); + } + } + } + return root; + } private: - void skipUnknownElement() -{ - Q_ASSERT(isStartElement()); + void skipUnknownElement() + { + assert(isStartElement()); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; - while (!atEnd()) { - readNext(); + if (isStartElement()) + skipUnknownElement(); + } + } + + void readXBEL(BookmarkNode parent) + { + assert(isStartElement() && name() == QLatin1String("xbel")); + + while (!atEnd()) { + readNext(); + if (isEndElement()) + break; - if (isEndElement()) - break; + if (isStartElement()) { + if (name() == QLatin1String("folder")) + readFolder(parent); + else if (name() == QLatin1String("bookmark")) + readBookmarkNode(parent); + else if (name() == QLatin1String("separator")) + readSeparator(parent); + else + skipUnknownElement(); + } + } + } - if (isStartElement()) - skipUnknownElement(); - } -} + void readTitle(BookmarkNode parent) + { + assert(isStartElement() && name() == QLatin1String("title")); + parent.title = readElementText(); + } + + void readDescription(BookmarkNode parent) + { + assert(isStartElement() && name() == QLatin1String("desc")); + parent.desc = readElementText(); + } - void readXBEL(BookmarkNode *parent) -{ - Q_ASSERT(isStartElement() && name() == QLatin1String("xbel")); + void readSeparator(BookmarkNode parent) + { + new BookmarkNode(BookmarkNode.Separator, parent); + // empty elements have a start and end element + readNext(); + } + - while (!atEnd()) { - readNext(); - if (isEndElement()) - break; + void readFolder(BookmarkNode parent) + { + assert(isStartElement() && name() == QLatin1String("folder")); + + BookmarkNode folder = new BookmarkNode(BookmarkNode.Folder, parent); + folder.expanded = (attributes().value(QLatin1String("folded")) == QLatin1String("no")); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; - if (isStartElement()) { - if (name() == QLatin1String("folder")) - readFolder(parent); - else if (name() == QLatin1String("bookmark")) - readBookmarkNode(parent); - else if (name() == QLatin1String("separator")) - readSeparator(parent); - else - skipUnknownElement(); - } - } + if (isStartElement()) { + if (name() == QLatin1String("title")) + readTitle(folder); + else if (name() == QLatin1String("desc")) + readDescription(folder); + else if (name() == QLatin1String("folder")) + readFolder(folder); + else if (name() == QLatin1String("bookmark")) + readBookmarkNode(folder); + else if (name() == QLatin1String("separator")) + readSeparator(folder); + else + skipUnknownElement(); + } + } + } + + void readBookmarkNode(BookmarkNode parent) + { + assert(isStartElement() && name() == QLatin1String("bookmark")); + BookmarkNode bookmark = new BookmarkNode(BookmarkNode.Bookmark, parent); + bookmark.url = attributes().value(QLatin1String("href")).toString(); + while (!atEnd()) { + readNext(); + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == QLatin1String("title")) + readTitle(bookmark); + else if (name() == QLatin1String("desc")) + readDescription(bookmark); + else + skipUnknownElement(); + } + } + if (bookmark.title.isEmpty()) + bookmark.title = QObject.tr("Unknown title"); + } } - void readTitle(BookmarkNode *parent) -{ - Q_ASSERT(isStartElement() && name() == QLatin1String("title")); - parent.title = readElementText(); -} - - void readDescription(BookmarkNode *parent) -{ - Q_ASSERT(isStartElement() && name() == QLatin1String("desc")); - parent.desc = readElementText(); -} - - void readSeparator(BookmarkNode *parent) -{ - new BookmarkNode(BookmarkNode::Separator, parent); - // empty elements have a start and end element - readNext(); -} - - - void readFolder(BookmarkNode *parent) -{ - Q_ASSERT(isStartElement() && name() == QLatin1String("folder")); - - BookmarkNode *folder = new BookmarkNode(BookmarkNode::Folder, parent); - folder.expanded = (attributes().value(QLatin1String("folded")) == QLatin1String("no")); - - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) { - if (name() == QLatin1String("title")) - readTitle(folder); - else if (name() == QLatin1String("desc")) - readDescription(folder); - else if (name() == QLatin1String("folder")) - readFolder(folder); - else if (name() == QLatin1String("bookmark")) - readBookmarkNode(folder); - else if (name() == QLatin1String("separator")) - readSeparator(folder); - else - skipUnknownElement(); - } - } -} - - void readBookmarkNode(BookmarkNode *parent) -{ - Q_ASSERT(isStartElement() && name() == QLatin1String("bookmark")); - BookmarkNode *bookmark = new BookmarkNode(BookmarkNode::Bookmark, parent); - bookmark.url = attributes().value(QLatin1String("href")).toString(); - while (!atEnd()) { - readNext(); - if (isEndElement()) - break; - - if (isStartElement()) { - if (name() == QLatin1String("title")) - readTitle(bookmark); - else if (name() == QLatin1String("desc")) - readDescription(bookmark); - else - skipUnknownElement(); - } - } - if (bookmark.title.isEmpty()) - bookmark.title = QObject::tr("Unknown title"); -} - -}; - -import QtCore.QXmlStreamWriter; +import qt.core.QXmlStreamWriter; class XbelWriter : public QXmlStreamWriter { public: - this() -{ - setAutoFormatting(true); -} - bool write(const QString &fileName, const BookmarkNode *root); -{ - QFile file(fileName); - if (!root || !file.open(QFile::WriteOnly)) - return false; - return write(&file, root); -} -bool write(QIODevice *device, const BookmarkNode *root); -{ - setDevice(device); + this() + { + setAutoFormatting(true); + } + + bool write(QString fileName, BookmarkNode root); + { + QFile file(fileName); + if (!root || !file.open(QFile.WriteOnly)) + return false; + return write(&file, root); + } - writeStartDocument(); - writeDTD(QLatin1String("<!DOCTYPE xbel>")); - writeStartElement(QLatin1String("xbel")); - writeAttribute(QLatin1String("version"), QLatin1String("1.0")); - if (root.type() == BookmarkNode::Root) { - for (int i = 0; i < root.children().count(); ++i) - writeItem(root.children().at(i)); - } else { - writeItem(root); - } + bool write(QIODevice device, BookmarkNode root); + { + setDevice(device); - writeEndDocument(); - return true; -} + writeStartDocument(); + writeDTD(QLatin1String("<!DOCTYPE xbel>")); + writeStartElement(QLatin1String("xbel")); + writeAttribute(QLatin1String("version"), QLatin1String("1.0")); + if (root.type() == BookmarkNode.Root) { + for (int i = 0; i < root.children().count(); ++i) + writeItem(root.children().at(i)); + } else { + writeItem(root); + } + writeEndDocument(); + return true; + } private: - void writeItem(const BookmarkNode *parent) -{ - switch (parent.type()) { - case BookmarkNode::Folder: - writeStartElement(QLatin1String("folder")); - writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes")); - writeTextElement(QLatin1String("title"), parent.title); - for (int i = 0; i < parent.children().count(); ++i) - writeItem(parent.children().at(i)); - writeEndElement(); - break; - case BookmarkNode::Bookmark: - writeStartElement(QLatin1String("bookmark")); - if (!parent.url.isEmpty()) - writeAttribute(QLatin1String("href"), parent.url); - writeTextElement(QLatin1String("title"), parent.title); - if (!parent.desc.isEmpty()) - writeAttribute(QLatin1String("desc"), parent.desc); - writeEndElement(); - break; - case BookmarkNode::Separator: - writeEmptyElement(QLatin1String("separator")); - break; - default: - break; - } + + void writeItem(BookmarkNode parent) + { + switch (parent.type()) { + case BookmarkNode.Folder: + writeStartElement(QLatin1String("folder")); + writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes")); + writeTextElement(QLatin1String("title"), parent.title); + for (int i = 0; i < parent.children().count(); ++i) + writeItem(parent.children().at(i)); + writeEndElement(); + break; + case BookmarkNode.Bookmark: + writeStartElement(QLatin1String("bookmark")); + if (!parent.url.isEmpty()) + writeAttribute(QLatin1String("href"), parent.url); + writeTextElement(QLatin1String("title"), parent.title); + if (!parent.desc.isEmpty()) + writeAttribute(QLatin1String("desc"), parent.desc); + writeEndElement(); + break; + case BookmarkNode.Separator: + writeEmptyElement(QLatin1String("separator")); + break; + default: + break; + } + } } -}