QXmppVersionManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <QCoreApplication>
  24. #include <QDomElement>
  25. #include "QXmppClient.h"
  26. #include "QXmppConstants.h"
  27. #include "QXmppGlobal.h"
  28. #include "QXmppVersionManager.h"
  29. #include "QXmppVersionIq.h"
  30. class QXmppVersionManagerPrivate
  31. {
  32. public:
  33. QString clientName;
  34. QString clientVersion;
  35. QString clientOs;
  36. };
  37. QXmppVersionManager::QXmppVersionManager()
  38. : d(new QXmppVersionManagerPrivate)
  39. {
  40. d->clientName = qApp->applicationName();
  41. if (d->clientName.isEmpty())
  42. d->clientName = "Based on QXmpp";
  43. #if defined(Q_OS_LINUX)
  44. d->clientOs = QString::fromLatin1("Linux");
  45. #elif defined(Q_OS_MAC)
  46. d->clientOs = QString::fromLatin1("Mac OS");
  47. #elif defined(Q_OS_SYMBIAN)
  48. d->clientOs = QString::fromLatin1("Symbian");
  49. #elif defined(Q_OS_WIN)
  50. d->clientOs = QString::fromLatin1("Windows");
  51. #endif
  52. d->clientVersion = qApp->applicationVersion();
  53. if (d->clientVersion.isEmpty())
  54. d->clientVersion = QXmppVersion();
  55. }
  56. QXmppVersionManager::~QXmppVersionManager()
  57. {
  58. delete d;
  59. }
  60. /// Request version information from the specified XMPP entity.
  61. ///
  62. /// \param jid
  63. QString QXmppVersionManager::requestVersion(const QString& jid)
  64. {
  65. QXmppVersionIq request;
  66. request.setType(QXmppIq::Get);
  67. request.setTo(jid);
  68. if(client()->sendPacket(request))
  69. return request.id();
  70. else
  71. return QString();
  72. }
  73. /// Sets the local XMPP client's name.
  74. ///
  75. /// \param name
  76. void QXmppVersionManager::setClientName(const QString& name)
  77. {
  78. d->clientName = name;
  79. }
  80. /// Sets the local XMPP client's version.
  81. ///
  82. /// \param version
  83. void QXmppVersionManager::setClientVersion(const QString& version)
  84. {
  85. d->clientVersion = version;
  86. }
  87. /// Sets the local XMPP client's operating system.
  88. ///
  89. /// \param os
  90. void QXmppVersionManager::setClientOs(const QString& os)
  91. {
  92. d->clientOs = os;
  93. }
  94. /// Returns the local XMPP client's name.
  95. ///
  96. /// By default this is set to the QApplication::applicationName(), or
  97. /// "Based on QXmpp" if not specified.
  98. QString QXmppVersionManager::clientName() const
  99. {
  100. return d->clientName;
  101. }
  102. /// Returns the local XMPP client's version.
  103. ///
  104. /// By default this is set to QApplication::applicationVersion(), or
  105. /// QXmpp's version if not specified.
  106. QString QXmppVersionManager::clientVersion() const
  107. {
  108. return d->clientVersion;
  109. }
  110. /// Returns the local XMPP client's operating system.
  111. ///
  112. /// By default this is "Linux", "Mac OS", "Symbian" or "Windows" depending
  113. /// on the platform QXmpp was compiled for.
  114. QString QXmppVersionManager::clientOs() const
  115. {
  116. return d->clientOs;
  117. }
  118. /// \cond
  119. QStringList QXmppVersionManager::discoveryFeatures() const
  120. {
  121. // XEP-0092: Software Version
  122. return QStringList() << ns_version;
  123. }
  124. bool QXmppVersionManager::handleStanza(const QDomElement &element)
  125. {
  126. if (element.tagName() == "iq" && QXmppVersionIq::isVersionIq(element))
  127. {
  128. QXmppVersionIq versionIq;
  129. versionIq.parse(element);
  130. if (versionIq.type() == QXmppIq::Get) {
  131. // respond to query
  132. QXmppVersionIq responseIq;
  133. responseIq.setType(QXmppIq::Result);
  134. responseIq.setId(versionIq.id());
  135. responseIq.setTo(versionIq.from());
  136. responseIq.setName(clientName());
  137. responseIq.setVersion(clientVersion());
  138. responseIq.setOs(clientOs());
  139. client()->sendPacket(responseIq);
  140. } else if (versionIq.type() == QXmppIq::Result) {
  141. // emit response
  142. emit versionReceived(versionIq);
  143. }
  144. return true;
  145. }
  146. return false;
  147. }
  148. /// \endcond