/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. #include "SourceList.h"
  20. #include "database/Database.h"
  21. #include "database/DatabaseImpl.h"
  22. #include "database/DatabaseCommand_LoadAllSources.h"
  23. #include "network/RemoteCollection.h"
  24. #include "network/ControlConnection.h"
  25. #include "infosystem/InfoSystemCache.h"
  26. #include "resolvers/ExternalResolver.h"
  27. #include "resolvers/ScriptCollection.h"
  28. #include "utils/Json.h"
  29. #include "utils/Logger.h"
  30. using namespace Tomahawk;
  31. SourceList* SourceList::s_instance = 0;
  32. SourceList*
  33. SourceList::instance()
  34. {
  35. if ( !s_instance )
  36. {
  37. s_instance = new SourceList();
  38. }
  39. return s_instance;
  40. }
  41. SourceList::SourceList( QObject* parent )
  42. : QObject( parent )
  43. , m_isReady( false )
  44. {
  45. }
  46. const source_ptr&
  47. SourceList::getLocal() const
  48. {
  49. return m_local;
  50. }
  51. void
  52. SourceList::setWebSource( const source_ptr& websrc )
  53. {
  54. m_dummy = websrc;
  55. }
  56. const source_ptr
  57. SourceList::webSource() const
  58. {
  59. return m_dummy;
  60. }
  61. void
  62. SourceList::loadSources()
  63. {
  64. Tomahawk::DatabaseCommand_LoadAllSources* cmd = new Tomahawk::DatabaseCommand_LoadAllSources();
  65. connect( cmd, SIGNAL( done( QList<Tomahawk::source_ptr> ) ),
  66. SLOT( setSources( QList<Tomahawk::source_ptr> ) ) );
  67. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  68. }
  69. void
  70. SourceList::setSources( const QList<Tomahawk::source_ptr>& sources )
  71. {
  72. {
  73. QMutexLocker lock( &m_mut );
  74. m_isReady = true;
  75. foreach( const source_ptr& src, sources )
  76. {
  77. add( src );
  78. }
  79. tLog() << Q_FUNC_INFO << "- Total sources now:" << m_sources.size();
  80. }
  81. emit ready();
  82. }
  83. void
  84. SourceList::setLocal( const Tomahawk::source_ptr& localSrc )
  85. {
  86. Q_ASSERT( localSrc->isLocal() );
  87. Q_ASSERT( m_local.isNull() );
  88. {
  89. QMutexLocker lock( &m_mut );
  90. m_sources.insert( localSrc->nodeId(), localSrc );
  91. m_local = localSrc;
  92. }
  93. connect( localSrc.data(), SIGNAL( latchedOn( Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr ) ) );
  94. connect( localSrc.data(), SIGNAL( latchedOff( Tomahawk::source_ptr ) ), this, SLOT( latchedOff( Tomahawk::source_ptr ) ) );
  95. emit sourceAdded( localSrc );
  96. }
  97. void
  98. SourceList::add( const source_ptr& source )
  99. {
  100. Q_ASSERT( m_isReady );
  101. // qDebug() << "Adding to sources:" << source->nodeId() << source->id();
  102. m_sources.insert( source->nodeId(), source );
  103. if ( source->id() > 0 )
  104. m_sources_id2name.insert( source->id(), source->nodeId() );
  105. connect( source.data(), SIGNAL( syncedWithDatabase() ), SLOT( sourceSynced() ) );
  106. collection_ptr coll( new RemoteCollection( source ) );
  107. coll->setWeakRef( coll.toWeakRef() );
  108. source->addCollection( coll );
  109. connect( source.data(), SIGNAL( latchedOn( Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr ) ) );
  110. connect( source.data(), SIGNAL( latchedOff( Tomahawk::source_ptr ) ), this, SLOT( latchedOff( Tomahawk::source_ptr ) ) );
  111. emit sourceAdded( source );
  112. }
  113. void
  114. SourceList::removeAllRemote()
  115. {
  116. foreach( const source_ptr& s, m_sources )
  117. {
  118. qDebug() << "Disconnecting" << s->friendlyName() << s->isLocal() << s->controlConnection() << s->isOnline();
  119. if ( !s->isLocal() && s->controlConnection() )
  120. {
  121. s->controlConnection()->shutdown( true );
  122. }
  123. }
  124. }
  125. QList<source_ptr>
  126. SourceList::sources( bool onlyOnline ) const
  127. {
  128. QMutexLocker lock( &m_mut );
  129. QList< source_ptr > sources;
  130. foreach( const source_ptr& src, m_sources )
  131. {
  132. if ( !onlyOnline || src->controlConnection() )
  133. sources << src;
  134. }
  135. return sources;
  136. }
  137. source_ptr
  138. SourceList::get( int id ) const
  139. {
  140. QMutexLocker lock( &m_mut );
  141. if ( id == 0 )
  142. return m_local;
  143. else
  144. return m_sources.value( m_sources_id2name.value( id ) );
  145. }
  146. source_ptr
  147. SourceList::get( const QString& username, const QString& friendlyName, bool autoCreate )
  148. {
  149. QMutexLocker lock( &m_mut );
  150. source_ptr source;
  151. if ( Database::instance()->impl()->dbid() == username )
  152. {
  153. return m_local;
  154. }
  155. if ( !m_sources.contains( username ) )
  156. {
  157. if ( autoCreate )
  158. {
  159. Q_ASSERT( !friendlyName.isEmpty() );
  160. source = source_ptr( new Source( -1, username ) );
  161. source->setDbFriendlyName( friendlyName );
  162. add( source );
  163. }
  164. }
  165. else
  166. {
  167. source = m_sources.value( username );
  168. source->setDbFriendlyName( friendlyName );
  169. }
  170. return source;
  171. }
  172. void
  173. SourceList::createPlaylist( const Tomahawk::source_ptr& src, const QVariant& contents )
  174. {
  175. Tomahawk::playlist_ptr p = Tomahawk::playlist_ptr( new Tomahawk::Playlist( src ) );
  176. TomahawkUtils::qvariant2qobject( contents.toMap(), p.data() );
  177. p->reportCreated( p );
  178. }
  179. void
  180. SourceList::createDynamicPlaylist( const Tomahawk::source_ptr& src, const QVariant& contents )
  181. {
  182. Tomahawk::dynplaylist_ptr p = Tomahawk::dynplaylist_ptr( new Tomahawk::DynamicPlaylist( src, contents.toMap().value( "type", QString() ).toString() ) );
  183. TomahawkUtils::qvariant2qobject( contents.toMap(), p.data() );
  184. p->reportCreated( p );
  185. }
  186. void
  187. SourceList::sourceSynced()
  188. {
  189. Source* src = qobject_cast< Source* >( sender() );
  190. m_sources_id2name.insert( src->id(), src->nodeId() );
  191. }
  192. unsigned int
  193. SourceList::count() const
  194. {
  195. QMutexLocker lock( &m_mut );
  196. return m_sources.size();
  197. }
  198. QList<collection_ptr>
  199. SourceList::scriptCollections() const
  200. {
  201. return m_scriptCollections;
  202. }
  203. void
  204. SourceList::latchedOff( const source_ptr& to )
  205. {
  206. Source* s = qobject_cast< Source* >( sender() );
  207. const source_ptr source = m_sources[ s->nodeId() ];
  208. emit sourceLatchedOff( source, to );
  209. }
  210. void
  211. SourceList::addScriptCollection( const collection_ptr& collection )
  212. {
  213. m_scriptCollections.append( collection );
  214. emit scriptCollectionAdded( collection );
  215. }
  216. void
  217. SourceList::removeScriptCollection( const collection_ptr& collection )
  218. {
  219. emit scriptCollectionRemoved( collection );
  220. m_scriptCollections.removeAll( collection );
  221. }
  222. void
  223. SourceList::latchedOn( const source_ptr& to )
  224. {
  225. Source* s = qobject_cast< Source* >( sender() );
  226. const source_ptr source = m_sources[ s->nodeId() ];
  227. emit sourceLatchedOn( source, to );
  228. }