diff options
| author | Olivier Goffart <ogoffart@woboq.com> | 2012-07-06 17:48:33 +0200 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2012-07-16 16:13:37 +0200 |
| commit | 1b445c310f9a3978c850c75f4070ba0ad7cb0523 (patch) | |
| tree | 51fee5fb84e5f296bb5e73cd46e2f0616b149b34 /src/base | |
| parent | 2c54c28a78bde8bfa40652fe01bf810945bfc027 (diff) | |
| download | qxmpp-1b445c310f9a3978c850c75f4070ba0ad7cb0523.tar.gz | |
Introduce QXmppResultSetQuery/Reply to handle XEP-0059
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/QXmppArchiveIq.cpp | 42 | ||||
| -rw-r--r-- | src/base/QXmppArchiveIq.h | 6 | ||||
| -rw-r--r-- | src/base/QXmppResultSet.cpp | 203 | ||||
| -rw-r--r-- | src/base/QXmppResultSet.h | 101 | ||||
| -rw-r--r-- | src/base/base.pri | 2 |
5 files changed, 325 insertions, 29 deletions
diff --git a/src/base/QXmppArchiveIq.cpp b/src/base/QXmppArchiveIq.cpp index a194f61f..ca489cff 100644 --- a/src/base/QXmppArchiveIq.cpp +++ b/src/base/QXmppArchiveIq.cpp @@ -27,7 +27,6 @@ #include "QXmppUtils.h" static const char *ns_archive = "urn:xmpp:archive"; -static const char *ns_rsm = "http://jabber.org/protocol/rsm"; QXmppArchiveMessage::QXmppArchiveMessage() : m_received(false) @@ -248,7 +247,7 @@ void QXmppArchiveChatIq::toXmlElementFromChild(QXmlStreamWriter *writer) const /// Constructs a QXmppArchiveListIq. QXmppArchiveListIq::QXmppArchiveListIq() - : QXmppIq(QXmppIq::Get), m_max(0) + : QXmppIq(QXmppIq::Get) { } @@ -271,7 +270,7 @@ void QXmppArchiveListIq::setChats(const QList<QXmppArchiveChat> &chats) int QXmppArchiveListIq::max() const { - return m_max; + return m_rsmQuery.max(); } /// Sets the maximum number of results. @@ -280,7 +279,7 @@ int QXmppArchiveListIq::max() const void QXmppArchiveListIq::setMax(int max) { - m_max = max; + m_rsmQuery.setMax(max); } /// Returns the JID which archived conversations must match. @@ -347,9 +346,8 @@ void QXmppArchiveListIq::parseElementFromChild(const QDomElement &element) m_start = QXmppUtils::datetimeFromString(listElement.attribute("start")); m_end = QXmppUtils::datetimeFromString(listElement.attribute("end")); - QDomElement setElement = listElement.firstChildElement("set"); - if (setElement.namespaceURI() == ns_rsm) - m_max = setElement.firstChildElement("max").text().toInt(); + m_rsmQuery.parse(listElement); + m_rsmReply.parse(listElement); QDomElement child = listElement.firstChildElement(); while (!child.isNull()) @@ -374,13 +372,10 @@ void QXmppArchiveListIq::toXmlElementFromChild(QXmlStreamWriter *writer) const helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start)); if (m_end.isValid()) helperToXmlAddAttribute(writer, "end", QXmppUtils::datetimeToString(m_end)); - if (m_max > 0) - { - writer->writeStartElement("set"); - writer->writeAttribute("xmlns", ns_rsm); - helperToXmlAddTextElement(writer, "max", QString::number(m_max)); - writer->writeEndElement(); - } + if (!m_rsmQuery.isNull()) + m_rsmQuery.toXml(writer); + else if (!m_rsmReply.isNull()) + m_rsmReply.toXml(writer); foreach (const QXmppArchiveChat &chat, m_chats) chat.toXml(writer); writer->writeEndElement(); @@ -484,7 +479,7 @@ void QXmppArchiveRemoveIq::toXmlElementFromChild(QXmlStreamWriter *writer) const } QXmppArchiveRetrieveIq::QXmppArchiveRetrieveIq() - : QXmppIq(QXmppIq::Get), m_max(0) + : QXmppIq(QXmppIq::Get) { } @@ -493,7 +488,7 @@ QXmppArchiveRetrieveIq::QXmppArchiveRetrieveIq() int QXmppArchiveRetrieveIq::max() const { - return m_max; + return m_rsm.max(); } /// Sets the maximum number of results. @@ -502,7 +497,7 @@ int QXmppArchiveRetrieveIq::max() const void QXmppArchiveRetrieveIq::setMax(int max) { - m_max = max; + m_rsm.setMax(max); } /// Returns the start date/time for the archived conversations. @@ -550,9 +545,8 @@ void QXmppArchiveRetrieveIq::parseElementFromChild(const QDomElement &element) QDomElement retrieveElement = element.firstChildElement("retrieve"); m_with = retrieveElement.attribute("with"); m_start = QXmppUtils::datetimeFromString(retrieveElement.attribute("start")); - QDomElement setElement = retrieveElement.firstChildElement("set"); - if (setElement.namespaceURI() == ns_rsm) - m_max = setElement.firstChildElement("max").text().toInt(); + m_rsm.parse(retrieveElement); + } void QXmppArchiveRetrieveIq::toXmlElementFromChild(QXmlStreamWriter *writer) const @@ -561,12 +555,6 @@ void QXmppArchiveRetrieveIq::toXmlElementFromChild(QXmlStreamWriter *writer) con writer->writeAttribute("xmlns", ns_archive); helperToXmlAddAttribute(writer, "with", m_with); helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start)); - if (m_max > 0) - { - writer->writeStartElement("set"); - writer->writeAttribute("xmlns", ns_rsm); - helperToXmlAddTextElement(writer, "max", QString::number(m_max)); - writer->writeEndElement(); - } + m_rsm.toXml(writer); writer->writeEndElement(); } diff --git a/src/base/QXmppArchiveIq.h b/src/base/QXmppArchiveIq.h index 7101f1e8..8c3bccf2 100644 --- a/src/base/QXmppArchiveIq.h +++ b/src/base/QXmppArchiveIq.h @@ -25,6 +25,7 @@ #define QXMPPARCHIVEIQ_H #include "QXmppIq.h" +#include "QXmppResultSet.h" #include <QDateTime> @@ -155,11 +156,12 @@ protected: /// \endcond private: - int m_max; QString m_with; QDateTime m_start; QDateTime m_end; QList<QXmppArchiveChat> m_chats; + QXmppResultSetQuery m_rsmQuery; + QXmppResultSetReply m_rsmReply; }; /// \brief Represents an archive remove IQ as defined by XEP-0136: Message Archiving. @@ -223,7 +225,7 @@ protected: /// \endcond private: - int m_max; + QXmppResultSetQuery m_rsm; QString m_with; QDateTime m_start; }; diff --git a/src/base/QXmppResultSet.cpp b/src/base/QXmppResultSet.cpp new file mode 100644 index 00000000..cfed9e83 --- /dev/null +++ b/src/base/QXmppResultSet.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2008-2011 The QXmpp developers + * + * Author: + * Olivier Goffart <ogoffart@woboq.com> + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + + +#include "QXmppResultSet.h" +#include "QXmppUtils.h" + +#include <QDomElement> +#include <QDebug> + +static const char *ns_rsm = "http://jabber.org/protocol/rsm"; + +QXmppResultSetQuery::QXmppResultSetQuery() + : m_index(-1), m_max(-1) +{} + +int QXmppResultSetQuery::max() const +{ + return m_max; +} + +void QXmppResultSetQuery::setMax(int max) +{ + m_max = max; +} + +int QXmppResultSetQuery::index() const +{ + return m_index; +} + +void QXmppResultSetQuery::setIndex(int index) +{ + m_index=index; +} + +QString QXmppResultSetQuery::before() const +{ + return m_before; +} + +void QXmppResultSetQuery::setBefore(const QString& before) +{ + m_before=before; +} + +QString QXmppResultSetQuery::after() const +{ + return m_after; +} + +void QXmppResultSetQuery::setAfter(const QString& after) +{ + m_after=after; +} + +bool QXmppResultSetQuery::isNull() const +{ + return m_max == -1 && m_index == -1 && m_after.isNull() && m_before.isNull(); +} + +void QXmppResultSetQuery::parse(const QDomElement& element) +{ + QDomElement setElement = (element.tagName() == "set") ? element : element.firstChildElement("set"); + if (setElement.namespaceURI() == ns_rsm) { + bool ok = false; + m_max = setElement.firstChildElement("max").text().toInt(&ok); + if (!ok) m_max = -1; + m_after = setElement.firstChildElement("after").text(); + m_before = setElement.firstChildElement("before").text(); + m_index = setElement.firstChildElement("index").text().toInt(&ok); + if (!ok) m_index = -1; + } +} + +void QXmppResultSetQuery::toXml(QXmlStreamWriter* writer) const +{ + if (isNull()) + return; + writer->writeStartElement("set"); + writer->writeAttribute("xmlns", ns_rsm); + if (m_max >= 0) + helperToXmlAddTextElement(writer, "max", QString::number(m_max)); + if (!m_after.isNull()) + helperToXmlAddTextElement(writer, "after", m_after); + if (!m_before.isNull()) + helperToXmlAddTextElement(writer, "before", m_before); + if (m_index >= 0) + helperToXmlAddTextElement(writer, "index", QString::number(m_index)); + writer->writeEndElement(); +} + + + +QXmppResultSetReply::QXmppResultSetReply() + : m_count(-1), m_index(-1) +{} + +QString QXmppResultSetReply::first() const +{ + return m_first; +} + +void QXmppResultSetReply::setFirst(const QString& first) +{ + m_first=first; +} + +QString QXmppResultSetReply::last() const +{ + return m_last; +} + +void QXmppResultSetReply::setLast(const QString& last) +{ + m_last=last; +} + +int QXmppResultSetReply::count() const +{ + return m_count; +} + +void QXmppResultSetReply::setCount(int count) +{ + m_count = count; +} + +int QXmppResultSetReply::index() const +{ + return m_index; +} + +void QXmppResultSetReply::setIndex(int index) +{ + m_index=index; +} + +bool QXmppResultSetReply::isNull() const +{ + return m_count == -1 && m_index == -1 && m_first.isNull() && m_last.isNull(); +} + + +void QXmppResultSetReply::parse(const QDomElement& element) +{ + QDomElement setElement = (element.tagName() == "set") ? element : element.firstChildElement("set"); + if (setElement.namespaceURI() == ns_rsm) { + m_count = setElement.firstChildElement("count").text().toInt(); + QDomElement firstElem = setElement.firstChildElement("first"); + m_first = firstElem.text(); + bool ok = false; + m_index = firstElem.attribute("index").toInt(&ok); + if(!ok) m_index = -1; + m_last = setElement.firstChildElement("last").text(); + } +} + +void QXmppResultSetReply::toXml(QXmlStreamWriter* writer) const +{ + if (isNull()) + return; + writer->writeStartElement("set"); + writer->writeAttribute("xmlns", ns_rsm); + if (!m_first.isNull() || m_index >= 0) { + writer->writeStartElement("first"); + if (m_index >= 0) + writer->writeAttribute("index", QString::number(m_index)); + writer->writeCharacters(m_first); + writer->writeEndElement(); + } + if (!m_last.isNull()) + helperToXmlAddTextElement(writer, "last", m_last); + + if (m_count >= 0) + helperToXmlAddTextElement(writer, "count", QString::number(m_count)); + writer->writeEndElement(); +} + + + + + diff --git a/src/base/QXmppResultSet.h b/src/base/QXmppResultSet.h new file mode 100644 index 00000000..c2f1f070 --- /dev/null +++ b/src/base/QXmppResultSet.h @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2008-2011 The QXmpp developers + * + * Author: + * Olivier Goffart <ogoffart@woboq.com> + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#ifndef QXMPPRESULTSET_H +#define QXMPPRESULTSET_H + +#include "QXmppElement.h" +#include <QDateTime> + +class QXmlStreamWriter; +class QDomElement; + +/// \brief The QXmppResultSetQuery represents a set element in a query +/// as defined by XEP-0059: Result Set Management. + +class QXMPP_EXPORT QXmppResultSetQuery +{ +public: + QXmppResultSetQuery(); + + int max() const; + void setMax(int max); + + int index() const; + void setIndex(int index); + + QString before() const; + void setBefore(const QString &before ); + + QString after() const; + void setAfter(const QString &after ); + + bool isNull() const; + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + int m_index; + int m_max; + QString m_after; + QString m_before; +}; + +/// \brief The QXmppResultSetQuery represents a set element in a reply +/// as defined by XEP-0059: Result Set Management. + +class QXMPP_EXPORT QXmppResultSetReply +{ +public: + QXmppResultSetReply(); + + QString first() const; + void setFirst(const QString &first ); + + QString last() const; + void setLast(const QString &last ); + + int count() const; + void setCount(int count); + + int index() const; + void setIndex(int index); + + bool isNull() const; + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + int m_count; + int m_index; + QString m_first; + QString m_last; +}; + +#endif // QXMPPRESULTSET_H diff --git a/src/base/base.pri b/src/base/base.pri index db0be9bd..a76459a8 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -23,6 +23,7 @@ INSTALL_HEADERS += \ base/QXmppPingIq.h \ base/QXmppPresence.h \ base/QXmppPubSubIq.h \ + base/QXmppResultSet.h \ base/QXmppRosterIq.h \ base/QXmppRpcIq.h \ base/QXmppRtpChannel.h \ @@ -61,6 +62,7 @@ SOURCES += \ base/QXmppPingIq.cpp \ base/QXmppPresence.cpp \ base/QXmppPubSubIq.cpp \ + base/QXmppResultSet.cpp \ base/QXmppRosterIq.cpp \ base/QXmppRpcIq.cpp \ base/QXmppRtpChannel.cpp \ |
