diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-02-19 16:41:04 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-02-19 16:41:04 +0000 |
| commit | 3bdcb8f14ff62b44d7ddd021bf187f8f5cae5a59 (patch) | |
| tree | cda5a66825fcc671ccdd6234900bed91f3a107d7 /source | |
| parent | 1ce7a46bf44ee6317f8bb5dd2bb80fde2df759de (diff) | |
| download | qxmpp-3bdcb8f14ff62b44d7ddd021bf187f8f5cae5a59.tar.gz | |
make QXmppElement API more like Qt's QDomElement
Diffstat (limited to 'source')
| -rw-r--r-- | source/QXmppElement.cpp | 54 | ||||
| -rw-r--r-- | source/QXmppElement.h | 2 |
2 files changed, 34 insertions, 22 deletions
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<QString, QString> attributes; QList<QXmppElementPrivate*> 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<QXmppElementPrivate*> &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 diff --git a/source/QXmppElement.h b/source/QXmppElement.h index 780d00c0..2c179043 100644 --- a/source/QXmppElement.h +++ b/source/QXmppElement.h @@ -54,8 +54,8 @@ public: void setAttribute(const QString &name, const QString &value); void appendChild(const QXmppElement &child); - QXmppElementList children() const; QXmppElement firstChildElement(const QString &name = QString()) const; + QXmppElement nextSiblingElement(const QString &name = QString()) const; void removeChild(const QXmppElement &child); QString tagName() const; |
