QXmppPubSubIq.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  6. *
  7. * Source:
  8. * http://code.google.com/p/qxmpp
  9. *
  10. * This file is a part of QXmpp library.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. */
  23. #include <QDomElement>
  24. #include "QXmppConstants.h"
  25. #include "QXmppPubSubIq.h"
  26. #include "QXmppUtils.h"
  27. static const char *ns_pubsub = "http://jabber.org/protocol/pubsub";
  28. static const char *pubsub_queries[] = {
  29. "affiliations",
  30. "default",
  31. "items",
  32. "publish",
  33. "retract",
  34. "subscribe",
  35. "subscription",
  36. "subscriptions",
  37. "unsubscribe",
  38. };
  39. /// Returns the ID of the PubSub item.
  40. ///
  41. QString QXmppPubSubItem::id() const
  42. {
  43. return m_id;
  44. }
  45. /// Sets the ID of the PubSub item.
  46. ///
  47. /// \param id
  48. void QXmppPubSubItem::setId(const QString &id)
  49. {
  50. m_id = id;
  51. }
  52. /// Returns the contents of the PubSub item.
  53. ///
  54. QXmppElement QXmppPubSubItem::contents() const
  55. {
  56. return m_contents;
  57. }
  58. /// Sets the contents of the PubSub item.
  59. ///
  60. /// \param contents
  61. void QXmppPubSubItem::setContents(const QXmppElement &contents)
  62. {
  63. m_contents = contents;
  64. }
  65. /// \cond
  66. void QXmppPubSubItem::parse(const QDomElement &element)
  67. {
  68. m_id = element.attribute("id");
  69. m_contents = QXmppElement(element.firstChildElement());
  70. }
  71. void QXmppPubSubItem::toXml(QXmlStreamWriter *writer) const
  72. {
  73. writer->writeStartElement("item");
  74. helperToXmlAddAttribute(writer, "id", m_id);
  75. m_contents.toXml(writer);
  76. writer->writeEndElement();
  77. }
  78. /// \endcond
  79. /// Returns the PubSub queryType for this IQ.
  80. ///
  81. QXmppPubSubIq::QueryType QXmppPubSubIq::queryType() const
  82. {
  83. return m_queryType;
  84. }
  85. /// Sets the PubSub queryType for this IQ.
  86. ///
  87. /// \param queryType
  88. void QXmppPubSubIq::setQueryType(QXmppPubSubIq::QueryType queryType)
  89. {
  90. m_queryType = queryType;
  91. }
  92. /// Returns the JID being queried.
  93. ///
  94. QString QXmppPubSubIq::queryJid() const
  95. {
  96. return m_queryJid;
  97. }
  98. /// Sets the JID being queried.
  99. ///
  100. /// \param queryJid
  101. void QXmppPubSubIq::setQueryJid(const QString &queryJid)
  102. {
  103. m_queryJid = queryJid;
  104. }
  105. /// Returns the node being queried.
  106. ///
  107. QString QXmppPubSubIq::queryNode() const
  108. {
  109. return m_queryNode;
  110. }
  111. /// Sets the node being queried.
  112. ///
  113. /// \param queryNode
  114. void QXmppPubSubIq::setQueryNode(const QString &queryNode)
  115. {
  116. m_queryNode = queryNode;
  117. }
  118. /// Returns the subscription ID.
  119. ///
  120. QString QXmppPubSubIq::subscriptionId() const
  121. {
  122. return m_subscriptionId;
  123. }
  124. /// Sets the subscription ID.
  125. ///
  126. /// \param subscriptionId
  127. void QXmppPubSubIq::setSubscriptionId(const QString &subscriptionId)
  128. {
  129. m_subscriptionId = subscriptionId;
  130. }
  131. /// Returns the IQ's items.
  132. ///
  133. QList<QXmppPubSubItem> QXmppPubSubIq::items() const
  134. {
  135. return m_items;
  136. }
  137. /// Sets the IQ's items.
  138. ///
  139. /// \param items
  140. void QXmppPubSubIq::setItems(const QList<QXmppPubSubItem> &items)
  141. {
  142. m_items = items;
  143. }
  144. /// \cond
  145. bool QXmppPubSubIq::isPubSubIq(const QDomElement &element)
  146. {
  147. const QDomElement pubSubElement = element.firstChildElement("pubsub");
  148. return pubSubElement.namespaceURI() == ns_pubsub;
  149. }
  150. void QXmppPubSubIq::parseElementFromChild(const QDomElement &element)
  151. {
  152. const QDomElement pubSubElement = element.firstChildElement("pubsub");
  153. const QDomElement queryElement = pubSubElement.firstChildElement();
  154. // determine query type
  155. const QString tagName = queryElement.tagName();
  156. for (int i = ItemsQuery; i <= SubscriptionsQuery; i++)
  157. {
  158. if (tagName == pubsub_queries[i])
  159. {
  160. m_queryType = static_cast<QueryType>(i);
  161. break;
  162. }
  163. }
  164. m_queryJid = queryElement.attribute("jid");
  165. m_queryNode = queryElement.attribute("node");
  166. // parse contents
  167. QDomElement childElement;
  168. switch (m_queryType)
  169. {
  170. case QXmppPubSubIq::ItemsQuery:
  171. case QXmppPubSubIq::PublishQuery:
  172. childElement = queryElement.firstChildElement("item");
  173. while (!childElement.isNull())
  174. {
  175. QXmppPubSubItem item;
  176. item.parse(childElement);
  177. m_items << item;
  178. childElement = childElement.nextSiblingElement("item");
  179. }
  180. break;
  181. case QXmppPubSubIq::SubscriptionQuery:
  182. m_subscriptionId = queryElement.attribute("subid");
  183. m_subscriptionType = queryElement.attribute("subscription");
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. void QXmppPubSubIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  190. {
  191. writer->writeStartElement("pubsub");
  192. writer->writeAttribute("xmlns", ns_pubsub);
  193. // write query type
  194. writer->writeStartElement(pubsub_queries[m_queryType]);
  195. helperToXmlAddAttribute(writer, "jid", m_queryJid);
  196. helperToXmlAddAttribute(writer, "node", m_queryNode);
  197. // write contents
  198. switch (m_queryType)
  199. {
  200. case QXmppPubSubIq::ItemsQuery:
  201. case QXmppPubSubIq::PublishQuery:
  202. foreach (const QXmppPubSubItem &item, m_items)
  203. item.toXml(writer);
  204. break;
  205. case QXmppPubSubIq::SubscriptionQuery:
  206. helperToXmlAddAttribute(writer, "subid", m_subscriptionId);
  207. helperToXmlAddAttribute(writer, "subscription", m_subscriptionType);
  208. break;
  209. default:
  210. break;
  211. }
  212. writer->writeEndElement();
  213. writer->writeEndElement();
  214. }
  215. /// \endcond