/src/sip/jabber/tomahawksipmessagefactory.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 146 lines · 100 code · 18 blank · 28 comment · 19 complexity · c520d7da322d71f48731e43eae3aaa1c MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "tomahawksipmessagefactory.h"
  19. #include <QStringList>
  20. #include <QXmlStreamWriter>
  21. #include <QVariant>
  22. #include "utils/logger.h"
  23. using namespace Jreen;
  24. TomahawkSipMessageFactory::TomahawkSipMessageFactory()
  25. {
  26. m_depth = 0;
  27. m_state = AtNowhere;
  28. }
  29. TomahawkSipMessageFactory::~TomahawkSipMessageFactory()
  30. {
  31. }
  32. QStringList TomahawkSipMessageFactory::features() const
  33. {
  34. return QStringList(TOMAHAWK_SIP_MESSAGE_NS);
  35. }
  36. bool TomahawkSipMessageFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
  37. {
  38. Q_UNUSED(uri);
  39. Q_UNUSED(attributes);
  40. return name == QLatin1String("tomahawk") && uri == TOMAHAWK_SIP_MESSAGE_NS;
  41. }
  42. void TomahawkSipMessageFactory::handleStartElement(const QStringRef &name, const QStringRef &uri,
  43. const QXmlStreamAttributes &attributes)
  44. {
  45. m_depth++;
  46. if (m_depth == 1) {
  47. m_state = AtNowhere;
  48. m_ip = QString();
  49. m_port = -1;
  50. m_uniqname = QString();
  51. m_key = QString();
  52. m_visible = false;
  53. } else if (m_depth == 2) {
  54. if (name == QLatin1String("transport"))
  55. {
  56. // qDebug() << "Found Transport";
  57. m_state = AtTransport;
  58. m_uniqname = attributes.value(QLatin1String("uniqname")).toString();
  59. m_key = attributes.value(QLatin1String("pwd")).toString();
  60. }
  61. } else if(m_depth == 3) {
  62. if (name == QLatin1String("candidate"))
  63. {
  64. m_state = AtCandidate;
  65. // qDebug() << "Found candidate";
  66. m_ip = attributes.value(QLatin1String("ip")).toString();
  67. m_port = attributes.value(QLatin1String("port")).toString().toInt();
  68. m_visible = true;
  69. }
  70. }
  71. Q_UNUSED(uri);
  72. Q_UNUSED(attributes);
  73. }
  74. void TomahawkSipMessageFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
  75. {
  76. if (m_depth == 3)
  77. m_state = AtNowhere;
  78. Q_UNUSED(name);
  79. Q_UNUSED(uri);
  80. m_depth--;
  81. }
  82. void TomahawkSipMessageFactory::handleCharacterData(const QStringRef &text)
  83. {
  84. /*if (m_state == AtUtc) {
  85. //m_utc = Util::fromStamp(text.toString());
  86. } else if (m_state == AtTzo) {
  87. QString str = text.toString();
  88. int multiple = str.startsWith('-') ? -1 : 1;
  89. //QTime delta = QTime::fromString(str.mid(1), QLatin1String("hh:mm"));
  90. //m_tzo = multiple * (delta.hour() * 60 + delta.minute());
  91. }*/
  92. Q_UNUSED(text);
  93. }
  94. void TomahawkSipMessageFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
  95. {
  96. TomahawkSipMessage *sipMessage = se_cast<TomahawkSipMessage*>(extension);
  97. writer->writeStartElement(QLatin1String("tomahawk"));
  98. writer->writeDefaultNamespace(TOMAHAWK_SIP_MESSAGE_NS);
  99. if(sipMessage->visible())
  100. {
  101. // add transport tag
  102. writer->writeStartElement(QLatin1String("transport"));
  103. writer->writeAttribute(QLatin1String("pwd"), sipMessage->key());
  104. writer->writeAttribute(QLatin1String("uniqname"), sipMessage->uniqname());
  105. writer->writeEmptyElement(QLatin1String("candidate"));
  106. writer->writeAttribute(QLatin1String("component"), "1");
  107. writer->writeAttribute(QLatin1String("id"), "el0747fg11"); // FIXME
  108. writer->writeAttribute(QLatin1String("ip"), sipMessage->ip());
  109. writer->writeAttribute(QLatin1String("network"), "1");
  110. writer->writeAttribute(QLatin1String("port"), QVariant(sipMessage->port()).toString());
  111. writer->writeAttribute(QLatin1String("priority"), "1"); //TODO
  112. writer->writeAttribute(QLatin1String("protocol"), "tcp");
  113. writer->writeAttribute(QLatin1String("type"), "host"); //FIXME: correct?!
  114. writer->writeEndElement();
  115. }
  116. else
  117. {
  118. writer->writeEmptyElement(QLatin1String("transport"));
  119. }
  120. writer->writeEndElement();
  121. }
  122. Payload::Ptr TomahawkSipMessageFactory::createPayload()
  123. {
  124. if(m_visible)
  125. return Payload::Ptr(new TomahawkSipMessage(m_ip, m_port, m_uniqname, m_key));
  126. else
  127. return Payload::Ptr(new TomahawkSipMessage());
  128. }