QXmppMucIq.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "QXmppMucIq.h"
  26. #include "QXmppUtils.h"
  27. QXmppMucItem::QXmppMucItem()
  28. : m_affiliation(QXmppMucItem::UnspecifiedAffiliation),
  29. m_role(QXmppMucItem::UnspecifiedRole)
  30. {
  31. }
  32. /// Returns true if the current item is null.
  33. bool QXmppMucItem::isNull() const
  34. {
  35. return m_actor.isEmpty() &&
  36. m_affiliation == UnspecifiedAffiliation &&
  37. m_jid.isEmpty() &&
  38. m_nick.isEmpty() &&
  39. m_reason.isEmpty() &&
  40. m_role == UnspecifiedRole;
  41. }
  42. /// Returns the actor for this item, for instance the admin who kicked
  43. /// a user out of a room.
  44. QString QXmppMucItem::actor() const
  45. {
  46. return m_actor;
  47. }
  48. /// Sets the \a actor for this item, for instance the admin who kicked
  49. /// a user out of a room.
  50. void QXmppMucItem::setActor(const QString &actor)
  51. {
  52. m_actor = actor;
  53. }
  54. /// Returns the user's affiliation, i.e. long-lived permissions.
  55. QXmppMucItem::Affiliation QXmppMucItem::affiliation() const
  56. {
  57. return m_affiliation;
  58. }
  59. /// \cond
  60. QXmppMucItem::Affiliation QXmppMucItem::affiliationFromString(const QString &affiliationStr)
  61. {
  62. if (affiliationStr == "owner")
  63. return QXmppMucItem::OwnerAffiliation;
  64. else if (affiliationStr == "admin")
  65. return QXmppMucItem::AdminAffiliation;
  66. else if (affiliationStr == "member")
  67. return QXmppMucItem::MemberAffiliation;
  68. else if (affiliationStr == "outcast")
  69. return QXmppMucItem::OutcastAffiliation;
  70. else if (affiliationStr == "none")
  71. return QXmppMucItem::NoAffiliation;
  72. else
  73. return QXmppMucItem::UnspecifiedAffiliation;
  74. }
  75. QString QXmppMucItem::affiliationToString(Affiliation affiliation)
  76. {
  77. switch (affiliation) {
  78. case QXmppMucItem::OwnerAffiliation:
  79. return "owner";
  80. case QXmppMucItem::AdminAffiliation:
  81. return "admin";
  82. case QXmppMucItem::MemberAffiliation:
  83. return "member";
  84. case QXmppMucItem::OutcastAffiliation:
  85. return "outcast";
  86. case QXmppMucItem::NoAffiliation:
  87. return "none";
  88. default:
  89. return QString();
  90. }
  91. }
  92. /// \endcond
  93. /// Sets the user's affiliation, i.e. long-lived permissions.
  94. ///
  95. /// \param affiliation
  96. void QXmppMucItem::setAffiliation(Affiliation affiliation)
  97. {
  98. m_affiliation = affiliation;
  99. }
  100. /// Returns the user's real JID.
  101. QString QXmppMucItem::jid() const
  102. {
  103. return m_jid;
  104. }
  105. /// Sets the user's real JID.
  106. ///
  107. /// \param jid
  108. void QXmppMucItem::setJid(const QString &jid)
  109. {
  110. m_jid = jid;
  111. }
  112. /// Returns the user's nickname.
  113. QString QXmppMucItem::nick() const
  114. {
  115. return m_nick;
  116. }
  117. /// Sets the user's nickname.
  118. ///
  119. /// \param nick
  120. void QXmppMucItem::setNick(const QString &nick)
  121. {
  122. m_nick = nick;
  123. }
  124. /// Returns the reason for this item, for example the reason for kicking
  125. /// a user out of a room.
  126. QString QXmppMucItem::reason() const
  127. {
  128. return m_reason;
  129. }
  130. /// Sets the \a reason for this item, for example the reason for kicking
  131. /// a user out of a room.
  132. void QXmppMucItem::setReason(const QString &reason)
  133. {
  134. m_reason = reason;
  135. }
  136. /// Returns the user's role, i.e. short-lived permissions.
  137. QXmppMucItem::Role QXmppMucItem::role() const
  138. {
  139. return m_role;
  140. }
  141. /// \cond
  142. QXmppMucItem::Role QXmppMucItem::roleFromString(const QString &roleStr)
  143. {
  144. if (roleStr == "moderator")
  145. return QXmppMucItem::ModeratorRole;
  146. else if (roleStr == "participant")
  147. return QXmppMucItem::ParticipantRole;
  148. else if (roleStr == "visitor")
  149. return QXmppMucItem::VisitorRole;
  150. else if (roleStr == "none")
  151. return QXmppMucItem::NoRole;
  152. else
  153. return QXmppMucItem::UnspecifiedRole;
  154. }
  155. QString QXmppMucItem::roleToString(Role role)
  156. {
  157. switch (role) {
  158. case QXmppMucItem::ModeratorRole:
  159. return "moderator";
  160. case QXmppMucItem::ParticipantRole:
  161. return "participant";
  162. case QXmppMucItem::VisitorRole:
  163. return "visitor";
  164. case QXmppMucItem::NoRole:
  165. return "none";
  166. default:
  167. return QString();
  168. }
  169. }
  170. /// \endcond
  171. /// Sets the user's role, i.e. short-lived permissions.
  172. ///
  173. /// \param role
  174. void QXmppMucItem::setRole(Role role)
  175. {
  176. m_role = role;
  177. }
  178. /// \cond
  179. void QXmppMucItem::parse(const QDomElement &element)
  180. {
  181. m_affiliation = QXmppMucItem::affiliationFromString(element.attribute("affiliation").toLower());
  182. m_jid = element.attribute("jid");
  183. m_nick = element.attribute("nick");
  184. m_role = QXmppMucItem::roleFromString(element.attribute("role").toLower());
  185. m_actor = element.firstChildElement("actor").attribute("jid");
  186. m_reason = element.firstChildElement("reason").text();
  187. }
  188. void QXmppMucItem::toXml(QXmlStreamWriter *writer) const
  189. {
  190. writer->writeStartElement("item");
  191. helperToXmlAddAttribute(writer, "affiliation", affiliationToString(m_affiliation));
  192. helperToXmlAddAttribute(writer, "jid", m_jid);
  193. helperToXmlAddAttribute(writer, "nick", m_nick);
  194. helperToXmlAddAttribute(writer, "role", roleToString(m_role));
  195. if (!m_actor.isEmpty()) {
  196. writer->writeStartElement("actor");
  197. helperToXmlAddAttribute(writer, "jid", m_actor);
  198. writer->writeEndElement();
  199. }
  200. if (!m_reason.isEmpty())
  201. helperToXmlAddTextElement(writer, "reason", m_reason);
  202. writer->writeEndElement();
  203. }
  204. /// \endcond
  205. /// Returns the IQ's items.
  206. QList<QXmppMucItem> QXmppMucAdminIq::items() const
  207. {
  208. return m_items;
  209. }
  210. /// Sets the IQ's items.
  211. ///
  212. /// \param items
  213. void QXmppMucAdminIq::setItems(const QList<QXmppMucItem> &items)
  214. {
  215. m_items = items;
  216. }
  217. /// \cond
  218. bool QXmppMucAdminIq::isMucAdminIq(const QDomElement &element)
  219. {
  220. QDomElement queryElement = element.firstChildElement("query");
  221. return (queryElement.namespaceURI() == ns_muc_admin);
  222. }
  223. void QXmppMucAdminIq::parseElementFromChild(const QDomElement &element)
  224. {
  225. QDomElement queryElement = element.firstChildElement("query");
  226. QDomElement child = queryElement.firstChildElement("item");
  227. while (!child.isNull())
  228. {
  229. QXmppMucItem item;
  230. item.parse(child);
  231. m_items << item;
  232. child = child.nextSiblingElement("item");
  233. }
  234. }
  235. void QXmppMucAdminIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  236. {
  237. writer->writeStartElement("query");
  238. writer->writeAttribute("xmlns", ns_muc_admin);
  239. foreach (const QXmppMucItem &item, m_items)
  240. item.toXml(writer);
  241. writer->writeEndElement();
  242. }
  243. /// \endcond
  244. /// Returns the IQ's data form.
  245. QXmppDataForm QXmppMucOwnerIq::form() const
  246. {
  247. return m_form;
  248. }
  249. /// Sets the IQ's data form.
  250. ///
  251. /// \param form
  252. void QXmppMucOwnerIq::setForm(const QXmppDataForm &form)
  253. {
  254. m_form = form;
  255. }
  256. /// \cond
  257. bool QXmppMucOwnerIq::isMucOwnerIq(const QDomElement &element)
  258. {
  259. QDomElement queryElement = element.firstChildElement("query");
  260. return (queryElement.namespaceURI() == ns_muc_owner);
  261. }
  262. void QXmppMucOwnerIq::parseElementFromChild(const QDomElement &element)
  263. {
  264. QDomElement queryElement = element.firstChildElement("query");
  265. m_form.parse(queryElement.firstChildElement("x"));
  266. }
  267. void QXmppMucOwnerIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  268. {
  269. writer->writeStartElement("query");
  270. writer->writeAttribute("xmlns", ns_muc_owner);
  271. m_form.toXml(writer);
  272. writer->writeEndElement();
  273. }
  274. /// \endcond