QXmppBookmarkManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "QXmppBookmarkManager.h"
  25. #include "QXmppBookmarkSet.h"
  26. #include "QXmppClient.h"
  27. #include "QXmppConstants.h"
  28. #include "QXmppIq.h"
  29. #include "QXmppUtils.h"
  30. // The QXmppPrivateStorageIq class represents an XML private storage IQ
  31. // as defined by XEP-0049: Private XML Storage.
  32. //
  33. // FIXME: currently, we only handle bookmarks
  34. class QXmppPrivateStorageIq : public QXmppIq
  35. {
  36. public:
  37. QXmppBookmarkSet bookmarks() const;
  38. void setBookmarks(const QXmppBookmarkSet &bookmark);
  39. static bool isPrivateStorageIq(const QDomElement &element);
  40. protected:
  41. void parseElementFromChild(const QDomElement &element);
  42. void toXmlElementFromChild(QXmlStreamWriter *writer) const;
  43. private:
  44. QXmppBookmarkSet m_bookmarks;
  45. };
  46. QXmppBookmarkSet QXmppPrivateStorageIq::bookmarks() const
  47. {
  48. return m_bookmarks;
  49. }
  50. void QXmppPrivateStorageIq::setBookmarks(const QXmppBookmarkSet &bookmarks)
  51. {
  52. m_bookmarks = bookmarks;
  53. }
  54. bool QXmppPrivateStorageIq::isPrivateStorageIq(const QDomElement &element)
  55. {
  56. const QDomElement queryElement = element.firstChildElement("query");
  57. return queryElement.namespaceURI() == ns_private &&
  58. QXmppBookmarkSet::isBookmarkSet(queryElement.firstChildElement());
  59. }
  60. void QXmppPrivateStorageIq::parseElementFromChild(const QDomElement &element)
  61. {
  62. const QDomElement queryElement = element.firstChildElement("query");
  63. m_bookmarks.parse(queryElement.firstChildElement());
  64. }
  65. void QXmppPrivateStorageIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  66. {
  67. writer->writeStartElement("query");
  68. writer->writeAttribute("xmlns", ns_private);
  69. m_bookmarks.toXml(writer);
  70. writer->writeEndElement();
  71. }
  72. class QXmppBookmarkManagerPrivate
  73. {
  74. public:
  75. QXmppBookmarkSet bookmarks;
  76. QXmppBookmarkSet pendingBookmarks;
  77. QString pendingId;
  78. bool bookmarksReceived;
  79. };
  80. /// Constructs a new bookmark manager.
  81. ///
  82. QXmppBookmarkManager::QXmppBookmarkManager()
  83. : d(new QXmppBookmarkManagerPrivate)
  84. {
  85. d->bookmarksReceived = false;
  86. }
  87. /// Destroys a bookmark manager.
  88. ///
  89. QXmppBookmarkManager::~QXmppBookmarkManager()
  90. {
  91. delete d;
  92. }
  93. /// Returns true if the bookmarks have been received from the server,
  94. /// false otherwise.
  95. ///
  96. bool QXmppBookmarkManager::areBookmarksReceived() const
  97. {
  98. return d->bookmarksReceived;
  99. }
  100. /// Returns the bookmarks stored on the server.
  101. ///
  102. /// Before calling this method, check that the bookmarks
  103. /// have indeed been received by calling areBookmarksReceived().
  104. ///
  105. QXmppBookmarkSet QXmppBookmarkManager::bookmarks() const
  106. {
  107. return d->bookmarks;
  108. }
  109. /// Stores the bookmarks on the server.
  110. ///
  111. /// \param bookmarks
  112. bool QXmppBookmarkManager::setBookmarks(const QXmppBookmarkSet &bookmarks)
  113. {
  114. QXmppPrivateStorageIq iq;
  115. iq.setType(QXmppIq::Set);
  116. iq.setBookmarks(bookmarks);
  117. if (!client()->sendPacket(iq))
  118. return false;
  119. d->pendingBookmarks = bookmarks;
  120. d->pendingId = iq.id();
  121. return true;
  122. }
  123. /// \cond
  124. void QXmppBookmarkManager::setClient(QXmppClient *client)
  125. {
  126. bool check;
  127. Q_UNUSED(check);
  128. QXmppClientExtension::setClient(client);
  129. check = connect(client, SIGNAL(connected()),
  130. this, SLOT(slotConnected()));
  131. Q_ASSERT(check);
  132. check = connect(client, SIGNAL(disconnected()),
  133. this, SLOT(slotDisconnected()));
  134. Q_ASSERT(check);
  135. }
  136. bool QXmppBookmarkManager::handleStanza(const QDomElement &stanza)
  137. {
  138. if (stanza.tagName() == "iq")
  139. {
  140. if (QXmppPrivateStorageIq::isPrivateStorageIq(stanza))
  141. {
  142. QXmppPrivateStorageIq iq;
  143. iq.parse(stanza);
  144. if (iq.type() == QXmppIq::Result)
  145. {
  146. d->bookmarks = iq.bookmarks();
  147. d->bookmarksReceived = true;
  148. emit bookmarksReceived(d->bookmarks);
  149. }
  150. return true;
  151. }
  152. else if (!d->pendingId.isEmpty() && stanza.attribute("id") == d->pendingId)
  153. {
  154. QXmppIq iq;
  155. iq.parse(stanza);
  156. if (iq.type() == QXmppIq::Result)
  157. {
  158. d->bookmarks = d->pendingBookmarks;
  159. emit bookmarksReceived(d->bookmarks);
  160. }
  161. d->pendingId = QString();
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /// \endcond
  168. void QXmppBookmarkManager::slotConnected()
  169. {
  170. QXmppPrivateStorageIq iq;
  171. iq.setType(QXmppIq::Get);
  172. client()->sendPacket(iq);
  173. }
  174. void QXmppBookmarkManager::slotDisconnected()
  175. {
  176. d->bookmarks = QXmppBookmarkSet();
  177. d->bookmarksReceived = false;
  178. }