diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-02-03 16:13:38 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-02-03 16:13:38 +0000 |
| commit | 468450f99b8549beb9bc1f68e50acbef728e6ad0 (patch) | |
| tree | c17e569b73a8abab90d222125b99d48911555d4d /source | |
| parent | 3f55d48265edd996ed8d4d52300f299a8715b3a1 (diff) | |
| download | qxmpp-468450f99b8549beb9bc1f68e50acbef728e6ad0.tar.gz | |
add initial XEP-0136 IQs and manager
Diffstat (limited to 'source')
| -rw-r--r-- | source/QXmppArchiveIq.cpp | 244 | ||||
| -rw-r--r-- | source/QXmppArchiveIq.h | 124 | ||||
| -rw-r--r-- | source/QXmppArchiveManager.cpp | 73 | ||||
| -rw-r--r-- | source/QXmppArchiveManager.h | 60 |
4 files changed, 501 insertions, 0 deletions
diff --git a/source/QXmppArchiveIq.cpp b/source/QXmppArchiveIq.cpp new file mode 100644 index 00000000..45e1017c --- /dev/null +++ b/source/QXmppArchiveIq.cpp @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2010 Bolloré telecom + * + * Author: + * Jeremy Lainé + * + * 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 "QXmppArchiveIq.h" +#include "QXmppUtils.h" + +#include <QDebug> +#include <QDomElement> + +static const char *ns_archive = "urn:xmpp:archive"; + +static QString dateToString(const QDateTime &dt) +{ + return dt.toUTC().toString(Qt::ISODate) + ".000000Z"; +} + +static QDateTime stringToDate(const QString &str) +{ + QDateTime dt = QDateTime::fromString(str, Qt::ISODate); + dt.setTimeSpec(Qt::UTC); + return dt; +} + +bool QXmppArchiveChatIq::isArchiveChatIq( QDomElement &element ) +{ + QDomElement chatElement = element.firstChildElement("chat"); + return !chatElement.attribute("with").isEmpty(); + //return (chatElement.namespaceURI() == ns_archive); +} + +QXmppArchiveChat QXmppArchiveChatIq::getChat() const +{ + return m_chat; +} + +void QXmppArchiveChatIq::parse( QDomElement &element ) +{ + QDomElement chatElement = element.firstChildElement("chat"); + m_chat.subject = chatElement.attribute("subject"); + m_chat.start = stringToDate(chatElement.attribute("start")); + m_chat.version = chatElement.attribute("version").toInt(); + m_chat.with = chatElement.attribute("with"); + + QDomElement child = chatElement.firstChildElement(); + while (!child.isNull()) + { + if ((child.tagName() == "from") || (child.tagName() == "to")) + { + QXmppArchiveMessage message; + message.datetime = m_chat.start.addSecs(child.attribute("secs").toInt()); + message.body = child.firstChildElement("body").text(); + message.local = (child.tagName() == "to"); + m_chat.messages << message; + } + child = child.nextSiblingElement(); + } +} + +QXmppArchiveListIq::QXmppArchiveListIq() + : QXmppIq(QXmppIq::Get), m_max(0) +{ +} + +QList<QXmppArchiveChat> QXmppArchiveListIq::getChats() const +{ + return m_chats; +} + +int QXmppArchiveListIq::getMax() const +{ + return m_max; +} + +void QXmppArchiveListIq::setMax(int max) +{ + m_max = max; +} + +QString QXmppArchiveListIq::getWith() const +{ + return m_with; +} + +void QXmppArchiveListIq::setWith( const QString &with ) +{ + m_with = with; +} + +QDateTime QXmppArchiveListIq::getStart() const +{ + return m_start; +} + +void QXmppArchiveListIq::setStart( const QDateTime &start ) +{ + m_start = start; +} + +QDateTime QXmppArchiveListIq::getEnd() const +{ + return m_end; +} + +void QXmppArchiveListIq::setEnd( const QDateTime &end ) +{ + m_end = end; +} + +bool QXmppArchiveListIq::isArchiveListIq( QDomElement &element ) +{ + QDomElement listElement = element.firstChildElement("list"); + return (listElement.namespaceURI() == ns_archive); +} + +void QXmppArchiveListIq::parse( QDomElement &element ) +{ + QDomElement listElement = element.firstChildElement("list"); + m_with = element.attribute("with"); + + QDomElement child = listElement.firstChildElement(); + while (!child.isNull()) + { + if (child.tagName() == "chat") + { + QXmppArchiveChat chat; + chat.with = child.attribute("with"); + chat.start = stringToDate(child.attribute("start")); + m_chats << chat; + } + child = child.nextSiblingElement(); + } +} + +void QXmppArchiveListIq::toXmlElementFromChild(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("list"); + helperToXmlAddAttribute(writer, "xmlns", ns_archive); + if (!m_with.isEmpty()) + helperToXmlAddAttribute(writer, "with", m_with); + if (m_start.isValid()) + helperToXmlAddAttribute(writer, "start", dateToString(m_start)); + if (m_end.isValid()) + helperToXmlAddAttribute(writer, "end", dateToString(m_start)); + if (m_max > 0) + { + writer->writeStartElement("set"); + helperToXmlAddAttribute(writer, "xmlns", "http://jabber.org/protocol/rsm"); + if (m_max > 0) + helperToXmlAddTextElement(writer, "max", QString::number(m_max)); + writer->writeEndElement(); + } + writer->writeEndElement(); +} + +bool QXmppArchivePrefIq::isArchivePrefIq( QDomElement &element ) +{ + QDomElement prefElement = element.firstChildElement("pref"); + return (prefElement.namespaceURI() == ns_archive); +} + +void QXmppArchivePrefIq::parse( QDomElement &element ) +{ + QDomElement queryElement = element.firstChildElement("pref"); + //setId( element.attribute("id")); +} + +void QXmppArchivePrefIq::toXmlElementFromChild( QXmlStreamWriter *writer ) const +{ + writer->writeStartElement("pref"); + helperToXmlAddAttribute(writer, "xmlns", ns_archive); + writer->writeEndElement(); +} + +QXmppArchiveRetrieveIq::QXmppArchiveRetrieveIq() + : QXmppIq(QXmppIq::Get), m_max(0) +{ +} + +int QXmppArchiveRetrieveIq::getMax() const +{ + return m_max; +} + +void QXmppArchiveRetrieveIq::setMax(int max) +{ + m_max = max; +} + +QDateTime QXmppArchiveRetrieveIq::getStart() const +{ + return m_start; +} + +void QXmppArchiveRetrieveIq::setStart( const QDateTime &start ) +{ + m_start = start; +} + +QString QXmppArchiveRetrieveIq::getWith() const +{ + return m_with; +} + +void QXmppArchiveRetrieveIq::setWith( const QString &with ) +{ + m_with = with; +} + +void QXmppArchiveRetrieveIq::toXmlElementFromChild( QXmlStreamWriter *writer ) const +{ + writer->writeStartElement("retrieve"); + helperToXmlAddAttribute(writer, "xmlns", ns_archive); + helperToXmlAddAttribute(writer, "with", m_with); + helperToXmlAddAttribute(writer, "start", dateToString(m_start)); + if (m_max > 0) + { + writer->writeStartElement("set"); + helperToXmlAddAttribute(writer, "xmlns", "http://jabber.org/protocol/rsm"); + if (m_max > 0) + helperToXmlAddTextElement(writer, "max", QString::number(m_max)); + writer->writeEndElement(); + } + writer->writeEndElement(); +} diff --git a/source/QXmppArchiveIq.h b/source/QXmppArchiveIq.h new file mode 100644 index 00000000..fabdebc7 --- /dev/null +++ b/source/QXmppArchiveIq.h @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2010 Bolloré telecom + * + * Author: + * Jeremy Lainé + * + * 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 QXMPPARCHIVEIQ_H +#define QXMPPARCHIVEIQ_H + +#include "QXmppIq.h" + +#include <QDateTime> + +class QXmlStreamWriter; +class QDomElement; + +class QXmppArchiveMessage +{ +public: + QDateTime datetime; + bool local; + QString body; +}; + +class QXmppArchiveChat +{ +public: + QString with; + QDateTime start; + + QString subject; + QList<QXmppArchiveMessage> messages; + int version; +}; + +class QXmppArchiveChatIq : public QXmppIq +{ +public: + void parse( QDomElement &element ); + static bool isArchiveChatIq( QDomElement &element ); + + QXmppArchiveChat getChat() const; + +private: + QXmppArchiveChat m_chat; +}; + +class QXmppArchiveListIq : public QXmppIq +{ +public: + QXmppArchiveListIq(); + void toXmlElementFromChild(QXmlStreamWriter *writer) const; + void parse( QDomElement &element ); + static bool isArchiveListIq( QDomElement &element ); + + QList<QXmppArchiveChat> getChats() const; + + int getMax() const; + void setMax(int max); + + QString getWith() const; + void setWith( const QString &with ); + + QDateTime getStart() const; + void setStart(const QDateTime &start ); + + QDateTime getEnd() const; + void setEnd(const QDateTime &end ); + +private: + int m_max; + QString m_with; + QDateTime m_start; + QDateTime m_end; + QList<QXmppArchiveChat> m_chats; +}; + +class QXmppArchiveRetrieveIq : public QXmppIq +{ +public: + QXmppArchiveRetrieveIq(); + void toXmlElementFromChild(QXmlStreamWriter *writer) const; + + int getMax() const; + void setMax(int max); + + QDateTime getStart() const; + void setStart( const QDateTime &start ); + + QString getWith() const; + void setWith( const QString &with ); + +private: + int m_max; + QString m_with; + QDateTime m_start; +}; + +class QXmppArchivePrefIq : public QXmppIq +{ +public: + void toXmlElementFromChild(QXmlStreamWriter *writer) const; + void parse( QDomElement &element ); + static bool isArchivePrefIq( QDomElement &element ); +}; + +#endif // QXMPPARCHIVEIQ_H diff --git a/source/QXmppArchiveManager.cpp b/source/QXmppArchiveManager.cpp new file mode 100644 index 00000000..5c404b8c --- /dev/null +++ b/source/QXmppArchiveManager.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2010 Bolloré telecom + * + * Author: + * Jeremy Lainé + * + * 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 "QXmppArchiveIq.h" +#include "QXmppArchiveManager.h" +#include "QXmppClient.h" + +#include <QDebug> + +QXmppArchiveManager::QXmppArchiveManager(QXmppClient *client) : + QObject(client), m_client(client) +{ +} + +void QXmppArchiveManager::archiveChatIqReceived(const QXmppArchiveChatIq &chatIq) +{ + emit archiveChatReceived(chatIq.getChat()); +} + +void QXmppArchiveManager::archiveListIqReceived(const QXmppArchiveListIq &listIq) +{ + emit archiveListReceived(listIq.getChats()); +} + +void QXmppArchiveManager::archivePrefIqReceived(const QXmppArchivePrefIq &prefIq) +{ + qDebug() << "got archive preferences"; +} + +void QXmppArchiveManager::listCollections(const QString &jid, const QDateTime &start, const QDateTime &end, int max) +{ + QXmppArchiveListIq packet; + packet.setMax(max); + packet.setWith(jid); + packet.setStart(start); + packet.setEnd(end); + m_client->sendPacket(packet); +} + +void QXmppArchiveManager::retrieveCollection(const QString &jid, const QDateTime &start, int max) +{ + QXmppArchiveRetrieveIq packet; + packet.setMax(max); + packet.setStart(start); + packet.setWith(jid); + m_client->sendPacket(packet); +} + +void QXmppArchiveManager::getPreferences() +{ + QXmppArchivePrefIq packet; + m_client->sendPacket(packet); +} diff --git a/source/QXmppArchiveManager.h b/source/QXmppArchiveManager.h new file mode 100644 index 00000000..bacdbc08 --- /dev/null +++ b/source/QXmppArchiveManager.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2010 Bolloré telecom + * + * Author: + * Jeremy Lainé + * + * 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 QXMPPARCHIVEMANAGER_H +#define QXMPPARCHIVEMANAGER_H + +#include <QDateTime> +#include <QObject> + +class QXmppArchiveChat; +class QXmppArchiveChatIq; +class QXmppArchiveListIq; +class QXmppArchivePrefIq; +class QXmppClient; + +class QXmppArchiveManager : public QObject +{ + Q_OBJECT + +public: + QXmppArchiveManager(QXmppClient* client); + void listCollections(const QString &jid, const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime(), int max = 0); + void retrieveCollection(const QString &jid, const QDateTime &start, int max = 0); + void getPreferences(); + +signals: + void archiveListReceived(const QList<QXmppArchiveChat>&); + void archiveChatReceived(const QXmppArchiveChat&); + +private slots: + void archiveChatIqReceived(const QXmppArchiveChatIq&); + void archiveListIqReceived(const QXmppArchiveListIq&); + void archivePrefIqReceived(const QXmppArchivePrefIq&); + +private: + // reference to to client object (no ownership) + QXmppClient* m_client; +}; + +#endif |
