/src/sip/zeroconf/zeroconf.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 157 lines · 108 code · 32 blank · 17 comment · 6 complexity · ee8a2a7efd2a29bc1e0c3ca1097c6599 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 "zeroconf.h"
  19. #include <QtPlugin>
  20. #include <QtCore/QTimer>
  21. #include "tomahawksettings.h"
  22. #include "utils/logger.h"
  23. SipPlugin*
  24. ZeroconfFactory::createPlugin( const QString& pluginId )
  25. {
  26. return new ZeroconfPlugin( pluginId.isEmpty() ? generateId() : pluginId );
  27. }
  28. ZeroconfPlugin::ZeroconfPlugin() : SipPlugin( "") {}
  29. ZeroconfPlugin::ZeroconfPlugin ( const QString& pluginId )
  30. : SipPlugin( pluginId )
  31. , m_zeroconf( 0 )
  32. , m_state( Disconnected )
  33. , m_cachedNodes()
  34. {
  35. qDebug() << Q_FUNC_INFO;
  36. m_advertisementTimer.setInterval( 60000 );
  37. m_advertisementTimer.setSingleShot( false );
  38. connect( &m_advertisementTimer, SIGNAL( timeout() ), this, SLOT( advertise() ) );
  39. }
  40. ZeroconfPlugin::~ZeroconfPlugin() {}
  41. const QString
  42. ZeroconfPlugin::name() const
  43. {
  44. return QString( MYNAME );
  45. }
  46. const QString
  47. ZeroconfPlugin::accountName() const
  48. {
  49. return QString( MYNAME );
  50. }
  51. const QString
  52. ZeroconfPlugin::friendlyName() const
  53. {
  54. return QString( MYNAME );
  55. }
  56. SipPlugin::ConnectionState
  57. ZeroconfPlugin::connectionState() const
  58. {
  59. return m_state;
  60. }
  61. #ifndef ENABLE_HEADLESS
  62. QIcon
  63. ZeroconfFactory::icon() const
  64. {
  65. return QIcon( ":/zeroconf-icon.png" );
  66. }
  67. #endif
  68. bool
  69. ZeroconfPlugin::connectPlugin( bool /*startup*/ )
  70. {
  71. delete m_zeroconf;
  72. m_zeroconf = new TomahawkZeroconf( Servent::instance()->port(), this );
  73. QObject::connect( m_zeroconf, SIGNAL( tomahawkHostFound( QString, int, QString, QString ) ),
  74. SLOT( lanHostFound( QString, int, QString, QString ) ) );
  75. advertise();
  76. m_state = Connected;
  77. foreach( const QStringList& nodeSet, m_cachedNodes )
  78. {
  79. if ( !Servent::instance()->connectedToSession( nodeSet[3] ) )
  80. Servent::instance()->connectToPeer( nodeSet[0], nodeSet[1].toInt(), "whitelist", nodeSet[2], nodeSet[3] );
  81. }
  82. m_cachedNodes.clear();
  83. m_advertisementTimer.start();
  84. return true;
  85. }
  86. void
  87. ZeroconfPlugin::disconnectPlugin()
  88. {
  89. m_advertisementTimer.stop();
  90. m_state = Disconnected;
  91. delete m_zeroconf;
  92. m_zeroconf = 0;
  93. }
  94. #ifndef ENABLE_HEADLESS
  95. QIcon
  96. ZeroconfPlugin::icon() const
  97. {
  98. return QIcon( ":/zeroconf-icon.png" );
  99. }
  100. #endif
  101. void
  102. ZeroconfPlugin::advertise()
  103. {
  104. m_zeroconf->advertise();
  105. }
  106. void
  107. ZeroconfPlugin::lanHostFound( const QString& host, int port, const QString& name, const QString& nodeid )
  108. {
  109. if ( sender() != m_zeroconf )
  110. return;
  111. qDebug() << "Found LAN host:" << host << port << nodeid;
  112. if ( m_state != Connected )
  113. {
  114. qDebug() << "Not online, so not connecting.";
  115. QStringList nodeSet;
  116. nodeSet << host << QString::number( port ) << name << nodeid;
  117. m_cachedNodes.append( nodeSet );
  118. return;
  119. }
  120. if ( !Servent::instance()->connectedToSession( nodeid ) )
  121. Servent::instance()->connectToPeer( host, port, "whitelist", name, nodeid );
  122. else
  123. qDebug() << "Already connected to" << host;
  124. }
  125. Q_EXPORT_PLUGIN2( sipfactory, ZeroconfFactory )