From 3bdcb8f14ff62b44d7ddd021bf187f8f5cae5a59 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Fri, 19 Feb 2010 16:41:04 +0000 Subject: make QXmppElement API more like Qt's QDomElement --- source/QXmppElement.cpp | 54 ++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'source/QXmppElement.cpp') diff --git a/source/QXmppElement.cpp b/source/QXmppElement.cpp index 1b431c2e..028770c0 100644 --- a/source/QXmppElement.cpp +++ b/source/QXmppElement.cpp @@ -35,6 +35,7 @@ public: QAtomicInt counter; + QXmppElementPrivate *parent; QMap attributes; QList children; QString name; @@ -42,12 +43,12 @@ public: }; QXmppElementPrivate::QXmppElementPrivate() - : counter(1) + : counter(1), parent(NULL) { } QXmppElementPrivate::QXmppElementPrivate(const QDomElement &element) - : counter(1) + : counter(1), parent(NULL) { if (element.isNull()) return; @@ -68,9 +69,13 @@ QXmppElementPrivate::QXmppElementPrivate(const QDomElement &element) while (!childNode.isNull()) { if (childNode.isElement()) - children.append(new QXmppElementPrivate(childNode.toElement())); - else if (childNode.isText()) + { + QXmppElementPrivate *child = new QXmppElementPrivate(childNode.toElement()); + child->parent = this; + children.append(child); + } else if (childNode.isText()) { value += childNode.toText().data(); + } childNode = childNode.nextSibling(); } } @@ -136,26 +141,32 @@ void QXmppElement::setAttribute(const QString &name, const QString &value) void QXmppElement::appendChild(const QXmppElement &child) { - if (!d->children.contains(child.d)) - { + if (child.d->parent == d) + return; + + if (child.d->parent) + child.d->parent->children.removeAll(child.d); + else child.d->counter.ref(); - d->children.append(child.d); - } + d->children.append(child.d); } -QXmppElementList QXmppElement::children() const +QXmppElement QXmppElement::firstChildElement(const QString &name) const { - QXmppElementList list; foreach (QXmppElementPrivate *child_d, d->children) - list.append(QXmppElement(child_d)); - return list; + if (name.isEmpty() || child_d->name == name) + return QXmppElement(child_d); + return QXmppElement(); } -QXmppElement QXmppElement::firstChildElement(const QString &name) const +QXmppElement QXmppElement::nextSiblingElement(const QString &name) const { - foreach (const QXmppElement &child, d->children) - if (name.isEmpty() || child.tagName() == name) - return child; + if (!d->parent) + return QXmppElement(); + const QList &siblings_d = d->parent->children; + for (int i = siblings_d.indexOf(d) + 1; i < siblings_d.size(); i++) + if (name.isEmpty() || siblings_d[i]->name == name) + return QXmppElement(siblings_d[i]); return QXmppElement(); } @@ -166,11 +177,12 @@ bool QXmppElement::isNull() const void QXmppElement::removeChild(const QXmppElement &child) { - if (d->children.contains(child.d)) - { - d->children.removeAll(child.d); - child.d->counter.deref(); - } + if (child.d->parent != d) + return; + + d->children.removeAll(child.d); + child.d->counter.deref(); + child.d->parent = NULL; } QString QXmppElement::tagName() const -- cgit v1.2.3