QXmppIq.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Manjeet Dahiya
  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 "QXmppUtils.h"
  24. #include "QXmppIq.h"
  25. #include <QDomElement>
  26. #include <QXmlStreamWriter>
  27. static const char* iq_types[] = {
  28. "error",
  29. "get",
  30. "set",
  31. "result"
  32. };
  33. class QXmppIqPrivate : public QSharedData
  34. {
  35. public:
  36. QXmppIq::Type type;
  37. };
  38. /// Constructs a QXmppIq with the specified \a type.
  39. ///
  40. /// \param type
  41. QXmppIq::QXmppIq(QXmppIq::Type type)
  42. : QXmppStanza()
  43. , d(new QXmppIqPrivate)
  44. {
  45. d->type = type;
  46. generateAndSetNextId();
  47. }
  48. /// Constructs a copy of \a other.
  49. QXmppIq::QXmppIq(const QXmppIq &other)
  50. : QXmppStanza(other)
  51. , d(other.d)
  52. {
  53. }
  54. QXmppIq::~QXmppIq()
  55. {
  56. }
  57. /// Assigns \a other to this IQ.
  58. QXmppIq& QXmppIq::operator=(const QXmppIq &other)
  59. {
  60. QXmppStanza::operator=(other);
  61. d = other.d;
  62. return *this;
  63. }
  64. /// Returns the IQ's type.
  65. ///
  66. QXmppIq::Type QXmppIq::type() const
  67. {
  68. return d->type;
  69. }
  70. /// Sets the IQ's type.
  71. ///
  72. /// \param type
  73. void QXmppIq::setType(QXmppIq::Type type)
  74. {
  75. d->type = type;
  76. }
  77. /// \cond
  78. void QXmppIq::parse(const QDomElement &element)
  79. {
  80. QXmppStanza::parse(element);
  81. const QString type = element.attribute("type");
  82. for (int i = Error; i <= Result; i++) {
  83. if (type == iq_types[i]) {
  84. d->type = static_cast<Type>(i);
  85. break;
  86. }
  87. }
  88. parseElementFromChild(element);
  89. }
  90. void QXmppIq::parseElementFromChild(const QDomElement &element)
  91. {
  92. QXmppElementList extensions;
  93. QDomElement itemElement = element.firstChildElement();
  94. while (!itemElement.isNull())
  95. {
  96. extensions.append(QXmppElement(itemElement));
  97. itemElement = itemElement.nextSiblingElement();
  98. }
  99. setExtensions(extensions);
  100. }
  101. void QXmppIq::toXml( QXmlStreamWriter *xmlWriter ) const
  102. {
  103. xmlWriter->writeStartElement("iq");
  104. helperToXmlAddAttribute(xmlWriter, "id", id());
  105. helperToXmlAddAttribute(xmlWriter, "to", to());
  106. helperToXmlAddAttribute(xmlWriter, "from", from());
  107. helperToXmlAddAttribute(xmlWriter, "type", iq_types[d->type]);
  108. toXmlElementFromChild(xmlWriter);
  109. error().toXml(xmlWriter);
  110. xmlWriter->writeEndElement();
  111. }
  112. void QXmppIq::toXmlElementFromChild( QXmlStreamWriter *writer ) const
  113. {
  114. foreach (const QXmppElement &extension, extensions())
  115. extension.toXml(writer);
  116. }
  117. /// \endcond