QXmppResultSet.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Olivier Goffart <ogoffart@woboq.com>
  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 "QXmppConstants.h"
  24. #include "QXmppResultSet.h"
  25. #include "QXmppUtils.h"
  26. #include <QDomElement>
  27. #include <QDebug>
  28. QXmppResultSetQuery::QXmppResultSetQuery()
  29. : m_index(-1)
  30. , m_max(-1)
  31. {}
  32. /// Returns the maximum number of results.
  33. ///
  34. /// \note -1 means no limit, 0 means no results are wanted.
  35. ///
  36. int QXmppResultSetQuery::max() const
  37. {
  38. return m_max;
  39. }
  40. /// Sets the maximum number of results.
  41. ///
  42. /// \note -1 means no limit, 0 means no results are wanted.
  43. void QXmppResultSetQuery::setMax(int max)
  44. {
  45. m_max = max;
  46. }
  47. /// Returns the index for the first element in the page.
  48. ///
  49. /// This is used for retrieving pages out of order.
  50. int QXmppResultSetQuery::index() const
  51. {
  52. return m_index;
  53. }
  54. /// Sets the index for the first element in the page.
  55. ///
  56. /// This is used for retrieving pages out of order.
  57. void QXmppResultSetQuery::setIndex(int index)
  58. {
  59. m_index=index;
  60. }
  61. /// Returns the UID of the first result in the next page.
  62. ///
  63. /// This is used for for paging backwards through results.
  64. QString QXmppResultSetQuery::before() const
  65. {
  66. return m_before;
  67. }
  68. /// Sets the UID of the first result in the next page.
  69. ///
  70. /// This is used for for paging backwards through results.
  71. void QXmppResultSetQuery::setBefore(const QString& before)
  72. {
  73. m_before=before;
  74. }
  75. /// Returns the UID of the last result in the previous page.
  76. ///
  77. /// This is used for for paging forwards through results.
  78. QString QXmppResultSetQuery::after() const
  79. {
  80. return m_after;
  81. }
  82. /// Sets the UID of the last result in the previous page.
  83. ///
  84. /// This is used for for paging forwards through results.
  85. void QXmppResultSetQuery::setAfter(const QString& after)
  86. {
  87. m_after=after;
  88. }
  89. /// Returns true if no result set information is present.
  90. bool QXmppResultSetQuery::isNull() const
  91. {
  92. return m_max == -1 && m_index == -1 && m_after.isNull() && m_before.isNull();
  93. }
  94. /// \cond
  95. void QXmppResultSetQuery::parse(const QDomElement& element)
  96. {
  97. QDomElement setElement = (element.tagName() == "set") ? element : element.firstChildElement("set");
  98. if (setElement.namespaceURI() == ns_rsm) {
  99. bool ok = false;
  100. m_max = setElement.firstChildElement("max").text().toInt(&ok);
  101. if (!ok) m_max = -1;
  102. m_after = setElement.firstChildElement("after").text();
  103. m_before = setElement.firstChildElement("before").text();
  104. m_index = setElement.firstChildElement("index").text().toInt(&ok);
  105. if (!ok) m_index = -1;
  106. }
  107. }
  108. void QXmppResultSetQuery::toXml(QXmlStreamWriter* writer) const
  109. {
  110. if (isNull())
  111. return;
  112. writer->writeStartElement("set");
  113. writer->writeAttribute("xmlns", ns_rsm);
  114. if (m_max >= 0)
  115. helperToXmlAddTextElement(writer, "max", QString::number(m_max));
  116. if (!m_after.isNull())
  117. helperToXmlAddTextElement(writer, "after", m_after);
  118. if (!m_before.isNull())
  119. helperToXmlAddTextElement(writer, "before", m_before);
  120. if (m_index >= 0)
  121. helperToXmlAddTextElement(writer, "index", QString::number(m_index));
  122. writer->writeEndElement();
  123. }
  124. /// \endcond
  125. QXmppResultSetReply::QXmppResultSetReply()
  126. : m_count(-1)
  127. , m_index(-1)
  128. {}
  129. /// Returns the UID of the first result in the page.
  130. QString QXmppResultSetReply::first() const
  131. {
  132. return m_first;
  133. }
  134. /// Sets the UID of the first result in the page.
  135. void QXmppResultSetReply::setFirst(const QString& first)
  136. {
  137. m_first=first;
  138. }
  139. /// Returns the UID of the last result in the page.
  140. QString QXmppResultSetReply::last() const
  141. {
  142. return m_last;
  143. }
  144. /// Sets the UID of the last result in the page.
  145. void QXmppResultSetReply::setLast(const QString& last)
  146. {
  147. m_last=last;
  148. }
  149. /// Returns the total number of items in the set.
  150. ///
  151. /// \note This may be an approximate count.
  152. int QXmppResultSetReply::count() const
  153. {
  154. return m_count;
  155. }
  156. /// Sets the total number of items in the set.
  157. ///
  158. /// \note This may be an approximate count.
  159. void QXmppResultSetReply::setCount(int count)
  160. {
  161. m_count = count;
  162. }
  163. /// Returns the index for the first result in the page.
  164. ///
  165. /// This is used for retrieving pages out of order.
  166. ///
  167. /// \note This may be an approximate index.
  168. int QXmppResultSetReply::index() const
  169. {
  170. return m_index;
  171. }
  172. /// Sets the index for the first result in the page.
  173. ///
  174. /// This is used for retrieving pages out of order.
  175. ///
  176. /// \note This may be an approximate index.
  177. void QXmppResultSetReply::setIndex(int index)
  178. {
  179. m_index = index;
  180. }
  181. /// Returns true if no result set information is present.
  182. bool QXmppResultSetReply::isNull() const
  183. {
  184. return m_count == -1 && m_index == -1 && m_first.isNull() && m_last.isNull();
  185. }
  186. /// \cond
  187. void QXmppResultSetReply::parse(const QDomElement& element)
  188. {
  189. QDomElement setElement = (element.tagName() == "set") ? element : element.firstChildElement("set");
  190. if (setElement.namespaceURI() == ns_rsm) {
  191. m_count = setElement.firstChildElement("count").text().toInt();
  192. QDomElement firstElem = setElement.firstChildElement("first");
  193. m_first = firstElem.text();
  194. bool ok = false;
  195. m_index = firstElem.attribute("index").toInt(&ok);
  196. if(!ok) m_index = -1;
  197. m_last = setElement.firstChildElement("last").text();
  198. }
  199. }
  200. void QXmppResultSetReply::toXml(QXmlStreamWriter* writer) const
  201. {
  202. if (isNull())
  203. return;
  204. writer->writeStartElement("set");
  205. writer->writeAttribute("xmlns", ns_rsm);
  206. if (!m_first.isNull() || m_index >= 0) {
  207. writer->writeStartElement("first");
  208. if (m_index >= 0)
  209. writer->writeAttribute("index", QString::number(m_index));
  210. writer->writeCharacters(m_first);
  211. writer->writeEndElement();
  212. }
  213. if (!m_last.isNull())
  214. helperToXmlAddTextElement(writer, "last", m_last);
  215. if (m_count >= 0)
  216. helperToXmlAddTextElement(writer, "count", QString::number(m_count));
  217. writer->writeEndElement();
  218. }
  219. /// \endcond