QXmppBookmarkSet.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "QXmppBookmarkSet.h"
  25. #include "QXmppUtils.h"
  26. static const char *ns_bookmarks = "storage:bookmarks";
  27. /// Constructs a new conference room bookmark.
  28. ///
  29. QXmppBookmarkConference::QXmppBookmarkConference()
  30. : m_autoJoin(false)
  31. {
  32. }
  33. /// Returns whether the client should automatically join the conference room
  34. /// on login.
  35. ///
  36. bool QXmppBookmarkConference::autoJoin() const
  37. {
  38. return m_autoJoin;
  39. }
  40. /// Sets whether the client should automatically join the conference room
  41. /// on login.
  42. ///
  43. /// \param autoJoin
  44. void QXmppBookmarkConference::setAutoJoin(bool autoJoin)
  45. {
  46. m_autoJoin = autoJoin;
  47. }
  48. /// Returns the JID of the conference room.
  49. ///
  50. QString QXmppBookmarkConference::jid() const
  51. {
  52. return m_jid;
  53. }
  54. /// Sets the JID of the conference room.
  55. ///
  56. /// \param jid
  57. void QXmppBookmarkConference::setJid(const QString &jid)
  58. {
  59. m_jid = jid;
  60. }
  61. /// Returns the friendly name for the bookmark.
  62. ///
  63. QString QXmppBookmarkConference::name() const
  64. {
  65. return m_name;
  66. }
  67. /// Sets the friendly name for the bookmark.
  68. ///
  69. /// \param name
  70. void QXmppBookmarkConference::setName(const QString &name)
  71. {
  72. m_name = name;
  73. }
  74. /// Returns the preferred nickname for the conference room.
  75. ///
  76. QString QXmppBookmarkConference::nickName() const
  77. {
  78. return m_nickName;
  79. }
  80. /// Sets the preferred nickname for the conference room.
  81. ///
  82. /// \param nickName
  83. void QXmppBookmarkConference::setNickName(const QString &nickName)
  84. {
  85. m_nickName = nickName;
  86. }
  87. /// Returns the friendly name for the bookmark.
  88. ///
  89. QString QXmppBookmarkUrl::name() const
  90. {
  91. return m_name;
  92. }
  93. /// Sets the friendly name for the bookmark.
  94. ///
  95. /// \param name
  96. void QXmppBookmarkUrl::setName(const QString &name)
  97. {
  98. m_name = name;
  99. }
  100. /// Returns the URL for the web page.
  101. ///
  102. QUrl QXmppBookmarkUrl::url() const
  103. {
  104. return m_url;
  105. }
  106. /// Sets the URL for the web page.
  107. ///
  108. /// \param url
  109. void QXmppBookmarkUrl::setUrl(const QUrl &url)
  110. {
  111. m_url = url;
  112. }
  113. /// Returns the conference rooms bookmarks in this bookmark set.
  114. ///
  115. QList<QXmppBookmarkConference> QXmppBookmarkSet::conferences() const
  116. {
  117. return m_conferences;
  118. }
  119. /// Sets the conference rooms bookmarks in this bookmark set.
  120. ///
  121. /// \param conferences
  122. void QXmppBookmarkSet::setConferences(const QList<QXmppBookmarkConference> &conferences)
  123. {
  124. m_conferences = conferences;
  125. }
  126. /// Returns the web page bookmarks in this bookmark set.
  127. ///
  128. QList<QXmppBookmarkUrl> QXmppBookmarkSet::urls() const
  129. {
  130. return m_urls;
  131. }
  132. /// Sets the web page bookmarks in this bookmark set.
  133. ///
  134. /// \param urls
  135. void QXmppBookmarkSet::setUrls(const QList<QXmppBookmarkUrl> &urls)
  136. {
  137. m_urls = urls;
  138. }
  139. /// \cond
  140. bool QXmppBookmarkSet::isBookmarkSet(const QDomElement &element)
  141. {
  142. return element.tagName() == "storage" &&
  143. element.namespaceURI() == ns_bookmarks;
  144. }
  145. void QXmppBookmarkSet::parse(const QDomElement &element)
  146. {
  147. QDomElement childElement = element.firstChildElement();
  148. while (!childElement.isNull())
  149. {
  150. if (childElement.tagName() == "conference")
  151. {
  152. QXmppBookmarkConference conference;
  153. conference.setAutoJoin(childElement.attribute("autojoin") == "true" || childElement.attribute("autojoin") == "1");
  154. conference.setJid(childElement.attribute("jid"));
  155. conference.setName(childElement.attribute("name"));
  156. conference.setNickName(childElement.firstChildElement("nick").text());
  157. m_conferences << conference;
  158. }
  159. else if (childElement.tagName() == "url")
  160. {
  161. QXmppBookmarkUrl url;
  162. url.setName(childElement.attribute("name"));
  163. url.setUrl(childElement.attribute("url"));
  164. m_urls << url;
  165. }
  166. childElement = childElement.nextSiblingElement();
  167. }
  168. }
  169. void QXmppBookmarkSet::toXml(QXmlStreamWriter *writer) const
  170. {
  171. writer->writeStartElement("storage");
  172. writer->writeAttribute("xmlns", ns_bookmarks);
  173. foreach (const QXmppBookmarkConference &conference, m_conferences)
  174. {
  175. writer->writeStartElement("conference");
  176. if (conference.autoJoin())
  177. helperToXmlAddAttribute(writer, "autojoin", "true");
  178. helperToXmlAddAttribute(writer, "jid", conference.jid());
  179. helperToXmlAddAttribute(writer, "name", conference.name());
  180. if (!conference.nickName().isEmpty())
  181. helperToXmlAddTextElement(writer, "nick", conference.nickName());
  182. writer->writeEndElement();
  183. }
  184. foreach (const QXmppBookmarkUrl &url, m_urls)
  185. {
  186. writer->writeStartElement("url");
  187. helperToXmlAddAttribute(writer, "name", url.name());
  188. helperToXmlAddAttribute(writer, "url", url.url().toString());
  189. writer->writeEndElement();
  190. }
  191. writer->writeEndElement();
  192. }
  193. /// \endcond