QXmppByteStreamIq.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "QXmppByteStreamIq.h"
  25. #include "QXmppConstants.h"
  26. #include "QXmppUtils.h"
  27. QString QXmppByteStreamIq::StreamHost::host() const
  28. {
  29. return m_host;
  30. }
  31. void QXmppByteStreamIq::StreamHost::setHost(const QString &host)
  32. {
  33. m_host = host;
  34. }
  35. QString QXmppByteStreamIq::StreamHost::jid() const
  36. {
  37. return m_jid;
  38. }
  39. void QXmppByteStreamIq::StreamHost::setJid(const QString &jid)
  40. {
  41. m_jid = jid;
  42. }
  43. quint16 QXmppByteStreamIq::StreamHost::port() const
  44. {
  45. return m_port;
  46. }
  47. void QXmppByteStreamIq::StreamHost::setPort(quint16 port)
  48. {
  49. m_port = port;
  50. }
  51. QString QXmppByteStreamIq::StreamHost::zeroconf() const
  52. {
  53. return m_zeroconf;
  54. }
  55. void QXmppByteStreamIq::StreamHost::setZeroconf(const QString &zeroconf)
  56. {
  57. m_zeroconf = zeroconf;
  58. }
  59. QXmppByteStreamIq::Mode QXmppByteStreamIq::mode() const
  60. {
  61. return m_mode;
  62. }
  63. void QXmppByteStreamIq::setMode(QXmppByteStreamIq::Mode mode)
  64. {
  65. m_mode = mode;
  66. }
  67. QString QXmppByteStreamIq::sid() const
  68. {
  69. return m_sid;
  70. }
  71. void QXmppByteStreamIq::setSid(const QString &sid)
  72. {
  73. m_sid = sid;
  74. }
  75. QString QXmppByteStreamIq::activate() const
  76. {
  77. return m_activate;
  78. }
  79. void QXmppByteStreamIq::setActivate(const QString &activate)
  80. {
  81. m_activate = activate;
  82. }
  83. QList<QXmppByteStreamIq::StreamHost> QXmppByteStreamIq::streamHosts() const
  84. {
  85. return m_streamHosts;
  86. }
  87. void QXmppByteStreamIq::setStreamHosts(const QList<QXmppByteStreamIq::StreamHost> &streamHosts)
  88. {
  89. m_streamHosts = streamHosts;
  90. }
  91. QString QXmppByteStreamIq::streamHostUsed() const
  92. {
  93. return m_streamHostUsed;
  94. }
  95. void QXmppByteStreamIq::setStreamHostUsed(const QString &jid)
  96. {
  97. m_streamHostUsed = jid;
  98. }
  99. /// \cond
  100. bool QXmppByteStreamIq::isByteStreamIq(const QDomElement &element)
  101. {
  102. return element.firstChildElement("query").namespaceURI() == ns_bytestreams;
  103. }
  104. void QXmppByteStreamIq::parseElementFromChild(const QDomElement &element)
  105. {
  106. QDomElement queryElement = element.firstChildElement("query");
  107. m_sid = queryElement.attribute("sid");
  108. const QString modeStr = queryElement.attribute("mode");
  109. if (modeStr == "tcp")
  110. m_mode = Tcp;
  111. else if (modeStr == "udp")
  112. m_mode = Udp;
  113. else
  114. m_mode = None;
  115. QDomElement hostElement = queryElement.firstChildElement("streamhost");
  116. while (!hostElement.isNull())
  117. {
  118. StreamHost streamHost;
  119. streamHost.setHost(hostElement.attribute("host"));
  120. streamHost.setJid(hostElement.attribute("jid"));
  121. streamHost.setPort(hostElement.attribute("port").toInt());
  122. streamHost.setZeroconf(hostElement.attribute("zeroconf"));
  123. m_streamHosts.append(streamHost);
  124. hostElement = hostElement.nextSiblingElement("streamhost");
  125. }
  126. m_activate = queryElement.firstChildElement("activate").text();
  127. m_streamHostUsed = queryElement.firstChildElement("streamhost-used").attribute("jid");
  128. }
  129. void QXmppByteStreamIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  130. {
  131. writer->writeStartElement("query");
  132. writer->writeAttribute("xmlns", ns_bytestreams);
  133. helperToXmlAddAttribute(writer, "sid", m_sid);
  134. QString modeStr;
  135. if (m_mode == Tcp)
  136. modeStr = "tcp";
  137. else if (m_mode == Udp)
  138. modeStr = "udp";
  139. helperToXmlAddAttribute(writer, "mode", modeStr);
  140. foreach (const StreamHost& streamHost, m_streamHosts)
  141. {
  142. writer->writeStartElement("streamhost");
  143. helperToXmlAddAttribute(writer, "host", streamHost.host());
  144. helperToXmlAddAttribute(writer, "jid", streamHost.jid());
  145. helperToXmlAddAttribute(writer, "port", QString::number(streamHost.port()));
  146. helperToXmlAddAttribute(writer, "zeroconf", streamHost.zeroconf());
  147. writer->writeEndElement();
  148. }
  149. if (!m_activate.isEmpty())
  150. helperToXmlAddTextElement(writer, "activate", m_activate);
  151. if (!m_streamHostUsed.isEmpty())
  152. {
  153. writer->writeStartElement("streamhost-used");
  154. helperToXmlAddAttribute(writer, "jid", m_streamHostUsed);
  155. writer->writeEndElement();
  156. }
  157. writer->writeEndElement();
  158. }
  159. /// \endcond