/src/libtomahawk/sip/SipInfo.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 282 lines · 200 code · 58 blank · 24 comment · 39 complexity · 5958e38185ba132d478f8a2d0febe3f6 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2011, Dominik Schmidt <dev@dominik-schmidt.de>
  4. * Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5. *
  6. * Tomahawk is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Tomahawk is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "SipInfo.h"
  20. #include "utils/Logger.h"
  21. #include "utils/Json.h"
  22. #include <QVariant>
  23. class SipInfoPrivate : public QSharedData
  24. {
  25. public:
  26. SipInfoPrivate()
  27. : port( -1 )
  28. {
  29. }
  30. SipInfoPrivate( const SipInfoPrivate& other )
  31. : QSharedData( other )
  32. , visible( other.visible )
  33. , host( other.host )
  34. , port( other.port )
  35. , nodeId( other.nodeId )
  36. , key( other.key )
  37. {
  38. }
  39. ~SipInfoPrivate() { }
  40. QVariant visible;
  41. QString host;
  42. int port;
  43. QString nodeId;
  44. QString key;
  45. };
  46. SipInfo::SipInfo()
  47. {
  48. d = new SipInfoPrivate;
  49. }
  50. SipInfo::SipInfo( const SipInfo& other )
  51. : QObject()
  52. , d( other.d )
  53. {
  54. }
  55. SipInfo::~SipInfo()
  56. {
  57. }
  58. SipInfo&
  59. SipInfo::operator=( const SipInfo& other )
  60. {
  61. d = other.d;
  62. return *this;
  63. }
  64. void
  65. SipInfo::clear()
  66. {
  67. d->visible.clear();
  68. d->host = QString();
  69. d->port = -1;
  70. d->nodeId = QString();
  71. d->key = QString();
  72. }
  73. bool
  74. SipInfo::isValid() const
  75. {
  76. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << d->visible << d->host << d->port << d->nodeId << d->key;
  77. if ( !d->visible.isNull() )
  78. {
  79. // visible and all data available
  80. if ( d->visible.toBool() && !d->host.isEmpty() && ( d->port > 0 ) && !d->nodeId.isNull() && !d->key.isNull() )
  81. return true;
  82. // invisible and no data available
  83. if ( !d->visible.toBool() && d->host.isEmpty() && ( d->port < 0 ) && d->nodeId.isNull() && d->key.isNull() )
  84. return true;
  85. // invisible and but nodeId and key available
  86. if ( !d->visible.toBool() && d->host.isEmpty() && ( d->port < 0 ) && !d->nodeId.isNull() && !d->key.isNull() )
  87. return true;
  88. }
  89. return false;
  90. }
  91. void
  92. SipInfo::setVisible( bool visible )
  93. {
  94. d->visible.setValue( visible );
  95. }
  96. bool
  97. SipInfo::isVisible() const
  98. {
  99. Q_ASSERT( isValid() );
  100. return d->visible.toBool();
  101. }
  102. void
  103. SipInfo::setHost( const QString& host )
  104. {
  105. d->host = host;
  106. }
  107. const QString
  108. SipInfo::host() const
  109. {
  110. Q_ASSERT( isValid() );
  111. return d->host;
  112. }
  113. void
  114. SipInfo::setPort( int port )
  115. {
  116. d->port = port;
  117. }
  118. int
  119. SipInfo::port() const
  120. {
  121. Q_ASSERT( isValid() );
  122. return d->port;
  123. }
  124. void
  125. SipInfo::setNodeId( const QString& nodeId )
  126. {
  127. d->nodeId = nodeId;
  128. }
  129. const QString
  130. SipInfo::nodeId() const
  131. {
  132. Q_ASSERT( isValid() );
  133. return d->nodeId;
  134. }
  135. void
  136. SipInfo::setKey( const QString& key )
  137. {
  138. d->key = key;
  139. }
  140. const QString
  141. SipInfo::key() const
  142. {
  143. Q_ASSERT( isValid() );
  144. return d->key;
  145. }
  146. const QString
  147. SipInfo::toJson() const
  148. {
  149. // build variant map
  150. QVariantMap m;
  151. m["visible"] = isVisible();
  152. if ( isVisible() )
  153. {
  154. m["ip"] = host();
  155. m["port"] = port();
  156. m["key"] = key();
  157. m["uniqname"] = nodeId();
  158. }
  159. // serialize
  160. QByteArray ba = TomahawkUtils::toJson( m );
  161. return QString::fromLatin1( ba );
  162. }
  163. const SipInfo
  164. SipInfo::fromJson( QString json )
  165. {
  166. SipInfo info;
  167. bool ok;
  168. QVariant v = TomahawkUtils::parseJson( json.toLatin1(), &ok );
  169. if ( !ok || v.type() != QVariant::Map )
  170. {
  171. qDebug() << Q_FUNC_INFO << "Invalid JSON: " << json;
  172. return info;
  173. }
  174. QVariantMap m = v.toMap();
  175. info.setVisible( m["visible"].toBool() );
  176. if ( m["visible"].toBool() )
  177. {
  178. info.setHost( m["host"].toString() );
  179. info.setPort( m["port"].toInt() );
  180. info.setNodeId( m["uniqname"].toString() );
  181. info.setKey( m["key"].toString() );
  182. }
  183. return info;
  184. }
  185. QDebug
  186. operator<< ( QDebug dbg, const SipInfo& info )
  187. {
  188. if ( !info.isValid() )
  189. dbg.nospace() << "info is invalid";
  190. else
  191. dbg.nospace() << info.toJson();
  192. return dbg.maybeSpace();
  193. }
  194. bool
  195. operator==( const SipInfo& one, const SipInfo& two )
  196. {
  197. // check valid/invalid combinations first, so we don't try to access any invalid sipInfos (->assert)
  198. if ( ( one.isValid() && !two.isValid() ) || ( !one.isValid() && two.isValid() ) )
  199. {
  200. return false;
  201. }
  202. else if ( one.isValid() && two.isValid() )
  203. {
  204. if ( one.isVisible() == two.isVisible()
  205. && one.host() == two.host()
  206. && one.port() == two.port()
  207. && one.nodeId() == two.nodeId()
  208. && one.key() == two.key() )
  209. {
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. const QString
  216. SipInfo::debugString() const
  217. {
  218. QString debugString( "SIP INFO: visible: %1 host: host %2 port: %3 nodeid: %4 key: %5" );
  219. return debugString.arg( d->visible.toBool() )
  220. .arg( d->host )
  221. .arg( d->port )
  222. .arg( d->nodeId )
  223. .arg( d->key );
  224. }