PageRenderTime 210ms CodeModel.GetById 60ms app.highlight 112ms RepoModel.GetById 29ms app.codeStats 1ms

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