PageRenderTime 285ms CodeModel.GetById 100ms app.highlight 86ms RepoModel.GetById 95ms app.codeStats 0ms

/src/libtomahawk/SourceList.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 302 lines | 209 code | 74 blank | 19 comment | 12 complexity | c369ebf2853318fcff3126b8297b43c8 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 *   Copyright 2013,      Teo Mrnjavac <teo@kde.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
 20#include "SourceList.h"
 21
 22#include "database/Database.h"
 23#include "database/DatabaseImpl.h"
 24#include "database/DatabaseCommand_LoadAllSources.h"
 25#include "network/RemoteCollection.h"
 26#include "network/ControlConnection.h"
 27#include "infosystem/InfoSystemCache.h"
 28#include "resolvers/ExternalResolver.h"
 29#include "resolvers/ScriptCollection.h"
 30#include "utils/Json.h"
 31#include "utils/Logger.h"
 32
 33using namespace Tomahawk;
 34
 35SourceList* SourceList::s_instance = 0;
 36
 37
 38SourceList*
 39SourceList::instance()
 40{
 41    if ( !s_instance )
 42    {
 43        s_instance = new SourceList();
 44    }
 45
 46    return s_instance;
 47}
 48
 49
 50SourceList::SourceList( QObject* parent )
 51    : QObject( parent )
 52    , m_isReady( false )
 53{
 54}
 55
 56
 57const source_ptr&
 58SourceList::getLocal() const
 59{
 60    return m_local;
 61}
 62
 63
 64void
 65SourceList::setWebSource( const source_ptr& websrc )
 66{
 67    m_dummy = websrc;
 68}
 69
 70
 71const source_ptr
 72SourceList::webSource() const
 73{
 74    return m_dummy;
 75}
 76
 77
 78void
 79SourceList::loadSources()
 80{
 81    Tomahawk::DatabaseCommand_LoadAllSources* cmd = new Tomahawk::DatabaseCommand_LoadAllSources();
 82
 83    connect( cmd, SIGNAL( done( QList<Tomahawk::source_ptr> ) ),
 84                    SLOT( setSources( QList<Tomahawk::source_ptr> ) ) );
 85
 86    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 87}
 88
 89
 90void
 91SourceList::setSources( const QList<Tomahawk::source_ptr>& sources )
 92{
 93    {
 94        QMutexLocker lock( &m_mut );
 95
 96        m_isReady = true;
 97        foreach( const source_ptr& src, sources )
 98        {
 99            add( src );
100        }
101
102        tLog() << Q_FUNC_INFO << "- Total sources now:" << m_sources.size();
103    }
104
105    emit ready();
106}
107
108
109void
110SourceList::setLocal( const Tomahawk::source_ptr& localSrc )
111{
112    Q_ASSERT( localSrc->isLocal() );
113    Q_ASSERT( m_local.isNull() );
114
115    {
116        QMutexLocker lock( &m_mut );
117        m_sources.insert( localSrc->nodeId(), localSrc );
118        m_local = localSrc;
119    }
120
121
122    connect( localSrc.data(), SIGNAL( latchedOn( Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr ) ) );
123    connect( localSrc.data(), SIGNAL( latchedOff( Tomahawk::source_ptr ) ), this, SLOT( latchedOff( Tomahawk::source_ptr ) ) );
124    emit sourceAdded( localSrc );
125}
126
127
128void
129SourceList::add( const source_ptr& source )
130{
131    Q_ASSERT( m_isReady );
132
133//    qDebug() << "Adding to sources:" << source->nodeId() << source->id();
134    m_sources.insert( source->nodeId(), source );
135
136    if ( source->id() > 0 )
137        m_sources_id2name.insert( source->id(), source->nodeId() );
138    connect( source.data(), SIGNAL( syncedWithDatabase() ), SLOT( sourceSynced() ) );
139
140    collection_ptr coll( new RemoteCollection( source ) );
141    coll->setWeakRef( coll.toWeakRef() );
142    source->addCollection( coll );
143
144    connect( source.data(), SIGNAL( latchedOn( Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr ) ) );
145    connect( source.data(), SIGNAL( latchedOff( Tomahawk::source_ptr ) ), this, SLOT( latchedOff( Tomahawk::source_ptr ) ) );
146    emit sourceAdded( source );
147}
148
149
150void
151SourceList::removeAllRemote()
152{
153    foreach( const source_ptr& s, m_sources )
154    {
155        qDebug() << "Disconnecting" << s->friendlyName() << s->isLocal() << s->controlConnection() << s->isOnline();
156        if ( !s->isLocal() && s->controlConnection() )
157        {
158            s->controlConnection()->shutdown( true );
159        }
160    }
161}
162
163
164QList<source_ptr>
165SourceList::sources( bool onlyOnline ) const
166{
167    QMutexLocker lock( &m_mut );
168
169    QList< source_ptr > sources;
170    foreach( const source_ptr& src, m_sources )
171    {
172        if ( !onlyOnline || src->controlConnection() )
173            sources << src;
174    }
175
176    return sources;
177}
178
179
180source_ptr
181SourceList::get( int id ) const
182{
183    QMutexLocker lock( &m_mut );
184
185    if ( id == 0 )
186        return m_local;
187    else
188        return m_sources.value( m_sources_id2name.value( id ) );
189}
190
191
192source_ptr
193SourceList::get( const QString& username, const QString& friendlyName, bool autoCreate )
194{
195    QMutexLocker lock( &m_mut );
196
197    source_ptr source;
198    if ( Database::instance()->impl()->dbid() == username )
199    {
200        return m_local;
201    }
202
203    if ( !m_sources.contains( username ) )
204    {
205        if ( autoCreate )
206        {
207            Q_ASSERT( !friendlyName.isEmpty() );
208            source = source_ptr( new Source( -1, username ) );
209            source->setDbFriendlyName( friendlyName );
210            add( source );
211        }
212    }
213    else
214    {
215        source = m_sources.value( username );
216        source->setDbFriendlyName( friendlyName );
217    }
218
219    return source;
220}
221
222
223void
224SourceList::createPlaylist( const Tomahawk::source_ptr& src, const QVariant& contents )
225{
226    Tomahawk::playlist_ptr p = Tomahawk::playlist_ptr( new Tomahawk::Playlist( src ) );
227    TomahawkUtils::qvariant2qobject( contents.toMap(), p.data() );
228    p->reportCreated( p );
229}
230
231
232void
233SourceList::createDynamicPlaylist( const Tomahawk::source_ptr& src, const QVariant& contents )
234{
235    Tomahawk::dynplaylist_ptr p = Tomahawk::dynplaylist_ptr( new Tomahawk::DynamicPlaylist( src, contents.toMap().value( "type", QString() ).toString()  ) );
236    TomahawkUtils::qvariant2qobject( contents.toMap(), p.data() );
237    p->reportCreated( p );
238}
239
240
241void
242SourceList::sourceSynced()
243{
244    Source* src = qobject_cast< Source* >( sender() );
245
246    m_sources_id2name.insert( src->id(), src->nodeId() );
247}
248
249
250unsigned int
251SourceList::count() const
252{
253    QMutexLocker lock( &m_mut );
254    return m_sources.size();
255}
256
257
258QList<collection_ptr>
259SourceList::scriptCollections() const
260{
261    return m_scriptCollections;
262}
263
264
265void
266SourceList::latchedOff( const source_ptr& to )
267{
268    Source* s = qobject_cast< Source* >( sender() );
269    const source_ptr source = m_sources[ s->nodeId() ];
270
271    emit sourceLatchedOff( source, to );
272}
273
274
275void
276SourceList::addScriptCollection( const collection_ptr& collection )
277{
278    m_scriptCollections.append( collection );
279
280    emit scriptCollectionAdded( collection );
281}
282
283
284void
285SourceList::removeScriptCollection( const collection_ptr& collection )
286{
287    emit scriptCollectionRemoved( collection );
288
289    m_scriptCollections.removeAll( collection );
290}
291
292
293void
294SourceList::latchedOn( const source_ptr& to )
295{
296
297    Source* s = qobject_cast< Source* >( sender() );
298    const source_ptr source = m_sources[ s->nodeId() ];
299
300    emit sourceLatchedOn( source, to );
301}
302