/src/libtomahawk/collection.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 257 lines · 161 code · 57 blank · 39 comment · 6 complexity · 2f827e0e6a713d1b6aacaccbcfe7a98b 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 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 "Collection.h"
  20. #include "Source.h"
  21. #include "utils/Logger.h"
  22. #include "playlist/PlaylistUpdaterInterface.h"
  23. #include <QMetaObject>
  24. #include <QGenericArgument>
  25. using namespace Tomahawk;
  26. Collection::Collection( const source_ptr& source, const QString& name, QObject* parent )
  27. : QObject( parent )
  28. , m_name( name )
  29. , m_lastmodified( 0 )
  30. , m_changed( false )
  31. , m_source( source )
  32. {
  33. qDebug() << Q_FUNC_INFO << name << source->friendlyName();
  34. connect( source.data(), SIGNAL( synced() ), SLOT( onSynced() ) );
  35. }
  36. Collection::~Collection()
  37. {
  38. qDebug() << Q_FUNC_INFO;
  39. }
  40. QString
  41. Collection::name() const
  42. {
  43. return m_name;
  44. }
  45. const
  46. source_ptr& Collection::source() const
  47. {
  48. return m_source;
  49. }
  50. void
  51. Collection::addPlaylist( const Tomahawk::playlist_ptr& p )
  52. {
  53. if ( m_playlists.contains( p->guid() ) )
  54. return;
  55. QList<playlist_ptr> toadd;
  56. toadd << p;
  57. m_playlists.insert( p->guid(), p );
  58. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  59. << "from source id" << source()->id()
  60. << "numplaylists:" << m_playlists.count();*/
  61. emit playlistsAdded( toadd );
  62. }
  63. void
  64. Collection::addAutoPlaylist( const Tomahawk::dynplaylist_ptr& p )
  65. {
  66. QList<dynplaylist_ptr> toadd;
  67. toadd << p;
  68. m_autoplaylists.insert( p->guid(), p );
  69. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  70. << "from source id" << source()->id()
  71. << "numplaylists:" << m_playlists.count();*/
  72. emit autoPlaylistsAdded( toadd );
  73. }
  74. void
  75. Collection::addStation( const dynplaylist_ptr& s )
  76. {
  77. QList<dynplaylist_ptr> toadd;
  78. toadd << s;
  79. m_stations.insert( s->guid(), s );
  80. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  81. << "from source id" << source()->id()
  82. << "numplaylists:" << m_playlists.count();*/
  83. emit stationsAdded( toadd );
  84. }
  85. void
  86. Collection::deletePlaylist( const Tomahawk::playlist_ptr& p )
  87. {
  88. QList<playlist_ptr> todelete;
  89. todelete << p;
  90. m_playlists.remove( p->guid() );
  91. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  92. << "from source id" << source()->id()
  93. << "numplaylists:" << m_playlists.count();*/
  94. emit playlistsDeleted( todelete );
  95. }
  96. void
  97. Collection::deleteAutoPlaylist( const Tomahawk::dynplaylist_ptr& p )
  98. {
  99. QList<dynplaylist_ptr> todelete;
  100. todelete << p;
  101. m_autoplaylists.remove( p->guid() );
  102. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  103. << "from source id" << source()->id()
  104. << "numplaylists:" << m_playlists.count();*/
  105. emit autoPlaylistsDeleted( todelete );
  106. }
  107. void
  108. Collection::deleteStation( const dynplaylist_ptr& s )
  109. {
  110. QList<dynplaylist_ptr> todelete;
  111. todelete << s;
  112. m_stations.remove( s->guid() );
  113. /* qDebug() << Q_FUNC_INFO << "Collection name" << name()
  114. << "from source id" << source()->id()
  115. << "numplaylists:" << m_playlists.count();*/
  116. emit stationsDeleted( todelete );
  117. }
  118. Tomahawk::playlist_ptr
  119. Collection::playlist( const QString& guid )
  120. {
  121. return m_playlists.value( guid, Tomahawk::playlist_ptr() );
  122. }
  123. Tomahawk::dynplaylist_ptr
  124. Collection::autoPlaylist( const QString& guid )
  125. {
  126. return m_autoplaylists.value( guid, Tomahawk::dynplaylist_ptr() );
  127. }
  128. Tomahawk::dynplaylist_ptr
  129. Collection::station( const QString& guid )
  130. {
  131. return m_stations.value( guid, Tomahawk::dynplaylist_ptr() );
  132. }
  133. void
  134. Collection::setPlaylists( const QList<Tomahawk::playlist_ptr>& plists )
  135. {
  136. foreach ( const playlist_ptr& p, plists )
  137. {
  138. // qDebug() << "Batch inserting playlist:" << p->guid();
  139. m_playlists.insert( p->guid(), p );
  140. if ( !m_source.isNull() && m_source->isLocal() )
  141. PlaylistUpdaterInterface::loadForPlaylist( p );
  142. }
  143. emit playlistsAdded( plists );
  144. }
  145. void
  146. Collection::setAutoPlaylists( const QList< Tomahawk::dynplaylist_ptr >& plists )
  147. {
  148. foreach ( const dynplaylist_ptr& p, plists )
  149. {
  150. // qDebug() << "Batch inserting dynamic playlist:" << p->guid();
  151. m_autoplaylists.insert( p->guid(), p );
  152. }
  153. emit autoPlaylistsAdded( plists );
  154. }
  155. void
  156. Collection::setStations( const QList< dynplaylist_ptr >& stations )
  157. {
  158. foreach ( const dynplaylist_ptr& s, stations )
  159. {
  160. // qDebug() << "Batch inserting station:" << s->guid();
  161. m_stations.insert( s->guid(), s );
  162. }
  163. emit autoPlaylistsAdded( stations );
  164. }
  165. void
  166. Collection::setTracks( const QList<unsigned int>& ids )
  167. {
  168. tDebug() << Q_FUNC_INFO << ids.count() << name();
  169. m_changed = true;
  170. emit tracksAdded( ids );
  171. }
  172. void
  173. Collection::delTracks( const QList<unsigned int>& ids )
  174. {
  175. tDebug() << Q_FUNC_INFO << ids.count() << name();
  176. m_changed = true;
  177. emit tracksRemoved( ids );
  178. }
  179. void
  180. Collection::onSynced()
  181. {
  182. tDebug() << Q_FUNC_INFO << m_changed;
  183. if ( m_changed )
  184. {
  185. m_changed = false;
  186. emit changed();
  187. }
  188. }
  189. void
  190. Collection::moveAutoToStation( const QString& guid )
  191. {
  192. if ( m_autoplaylists.contains( guid ) )
  193. m_stations.insert( guid, m_autoplaylists.take( guid ) );
  194. }
  195. void
  196. Collection::moveStationToAuto( const QString& guid )
  197. {
  198. if ( m_stations.contains( guid ) )
  199. m_autoplaylists.insert( guid, m_stations.take( guid ) );
  200. }