/src/libtomahawk/database/DatabaseCollection.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 288 lines · 177 code · 67 blank · 44 comment · 8 complexity · d81565e98b58210f062c9b897cd7febf 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, Leo Franchi <lfranchi@kde.org>
  5. * Copyright 2013, Teo Mrnjavac <teo@kde.org>
  6. *
  7. * Tomahawk is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tomahawk is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "DatabaseCollection.h"
  21. #include "database/Database.h"
  22. #include "database/DatabaseCommand_AllArtists.h"
  23. #include "database/DatabaseCommand_AllAlbums.h"
  24. #include "database/DatabaseCommand_AllTracks.h"
  25. #include "database/DatabaseCommand_AddFiles.h"
  26. #include "database/DatabaseCommand_DeleteFiles.h"
  27. #include "database/DatabaseCommand_LoadAllPlaylists.h"
  28. #include "database/DatabaseCommand_LoadAllAutoPlaylists.h"
  29. #include "database/DatabaseCommand_LoadAllStations.h"
  30. #include "utils/Logger.h"
  31. #include "PlaylistEntry.h"
  32. using namespace Tomahawk;
  33. DatabaseCollection::DatabaseCollection( const source_ptr& src, QObject* parent )
  34. : Collection( src, QString( "dbcollection:%1" ).arg( src->nodeId() ), parent )
  35. {
  36. m_browseCapabilities
  37. << CapabilityBrowseArtists
  38. << CapabilityBrowseAlbums
  39. << CapabilityBrowseTracks;
  40. connect( source().data(), SIGNAL( online() ), SIGNAL( online() ) );
  41. connect( source().data(), SIGNAL( offline() ), SIGNAL( offline() ) );
  42. }
  43. bool
  44. DatabaseCollection::isOnline() const
  45. {
  46. return source()->isOnline();
  47. }
  48. void
  49. DatabaseCollection::loadPlaylists()
  50. {
  51. DatabaseCommand_LoadAllPlaylists* cmd = new DatabaseCommand_LoadAllPlaylists( source() );
  52. connect( cmd, SIGNAL( done( const QList<Tomahawk::playlist_ptr>& ) ),
  53. SLOT( setPlaylists( const QList<Tomahawk::playlist_ptr>& ) ) );
  54. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  55. }
  56. void
  57. DatabaseCollection::loadAutoPlaylists()
  58. {
  59. DatabaseCommand_LoadAllAutoPlaylists* cmd = new DatabaseCommand_LoadAllAutoPlaylists( source() );
  60. connect( cmd, SIGNAL( autoPlaylistLoaded( Tomahawk::source_ptr, QVariantList ) ),
  61. SLOT( autoPlaylistCreated( const Tomahawk::source_ptr&, const QVariantList& ) ) );
  62. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  63. }
  64. void
  65. DatabaseCollection::loadStations()
  66. {
  67. DatabaseCommand_LoadAllStations* cmd = new DatabaseCommand_LoadAllStations( source() );
  68. connect( cmd, SIGNAL( stationLoaded( Tomahawk::source_ptr, QVariantList ) ),
  69. SLOT( stationCreated( const Tomahawk::source_ptr&, const QVariantList& ) ) );
  70. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  71. }
  72. void
  73. DatabaseCollection::addTracks( const QList<QVariant>& newitems )
  74. {
  75. qDebug() << Q_FUNC_INFO << newitems.length();
  76. DatabaseCommand_AddFiles* cmd = new DatabaseCommand_AddFiles( newitems, source() );
  77. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  78. }
  79. void
  80. DatabaseCollection::removeTracks( const QDir& dir )
  81. {
  82. qDebug() << Q_FUNC_INFO << dir;
  83. DatabaseCommand_DeleteFiles* cmd = new DatabaseCommand_DeleteFiles( dir, source() );
  84. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  85. }
  86. QList< Tomahawk::playlist_ptr >
  87. DatabaseCollection::playlists()
  88. {
  89. if ( Collection::playlists().isEmpty() )
  90. {
  91. loadPlaylists();
  92. }
  93. return Collection::playlists();
  94. }
  95. QList< dynplaylist_ptr >
  96. DatabaseCollection::autoPlaylists()
  97. {
  98. // echonest is dead, disable all echonest code
  99. /*
  100. if ( Collection::autoPlaylists().isEmpty() )
  101. {
  102. loadAutoPlaylists();
  103. }
  104. */
  105. return Collection::autoPlaylists();
  106. }
  107. QList< dynplaylist_ptr >
  108. DatabaseCollection::stations()
  109. {
  110. // echonest is dead, disable all echonest code
  111. /*
  112. if ( Collection::stations().isEmpty() )
  113. {
  114. loadStations();
  115. }
  116. */
  117. return Collection::stations();
  118. }
  119. Tomahawk::ArtistsRequest*
  120. DatabaseCollection::requestArtists()
  121. {
  122. //FIXME: assuming there's only one dbcollection per source, and that this is the one
  123. Tomahawk::collection_ptr thisCollection = source()->dbCollection();
  124. if ( thisCollection->name() != this->name() )
  125. return 0;
  126. Tomahawk::ArtistsRequest* cmd = new DatabaseCommand_AllArtists( thisCollection );
  127. return cmd;
  128. }
  129. Tomahawk::AlbumsRequest*
  130. DatabaseCollection::requestAlbums( const Tomahawk::artist_ptr& artist )
  131. {
  132. //FIXME: assuming there's only one dbcollection per source, and that this is the one
  133. Tomahawk::collection_ptr thisCollection = source()->dbCollection();
  134. if ( thisCollection->name() != this->name() )
  135. return 0;
  136. Tomahawk::AlbumsRequest* cmd = new DatabaseCommand_AllAlbums( thisCollection, artist );
  137. return cmd;
  138. }
  139. Tomahawk::TracksRequest*
  140. DatabaseCollection::requestTracks( const Tomahawk::album_ptr& album )
  141. {
  142. //FIXME: assuming there's only one dbcollection per source, and that this is the one
  143. Tomahawk::collection_ptr thisCollection = source()->dbCollection();
  144. if ( thisCollection->name() != this->name() )
  145. return 0;
  146. DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( thisCollection );
  147. if ( album )
  148. {
  149. cmd->setAlbum( album->weakRef() );
  150. cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition );
  151. }
  152. return cmd;
  153. }
  154. int
  155. DatabaseCollection::trackCount() const
  156. {
  157. return source()->trackCount();
  158. }
  159. QPixmap
  160. DatabaseCollection::icon( const QSize& size ) const
  161. {
  162. return source()->avatar( TomahawkUtils::RoundedCorners, size, true );
  163. }
  164. void
  165. DatabaseCollection::autoPlaylistCreated( const source_ptr& source, const QVariantList& data )
  166. {
  167. dynplaylist_ptr p( new DynamicPlaylist( source, //src
  168. data[0].toString(), //current rev
  169. data[1].toString(), //title
  170. data[2].toString(), //info
  171. data[3].toString(), //creator
  172. data[4].toUInt(), // createdOn
  173. data[5].toString(), // dynamic type
  174. static_cast<GeneratorMode>(data[6].toInt()), // dynamic mode
  175. data[7].toBool(), //shared
  176. data[8].toInt(), //lastmod
  177. data[9].toString() ), &QObject::deleteLater ); //GUID
  178. p->setWeakSelf( p.toWeakRef() );
  179. addAutoPlaylist( p );
  180. }
  181. void
  182. DatabaseCollection::stationCreated( const source_ptr& source, const QVariantList& data )
  183. {
  184. dynplaylist_ptr p( new DynamicPlaylist( source, //src
  185. data[0].toString(), //current rev
  186. data[1].toString(), //title
  187. data[2].toString(), //info
  188. data[3].toString(), //creator
  189. data[4].toUInt(), // createdOn
  190. data[5].toString(), // dynamic type
  191. static_cast<GeneratorMode>(data[6].toInt()), // dynamic mode
  192. data[7].toBool(), //shared
  193. data[8].toInt(), //lastmod
  194. data[9].toString() ), &QObject::deleteLater ); //GUID
  195. p->setWeakSelf( p.toWeakRef() );
  196. addStation( p );
  197. }
  198. /*
  199. * Resolver interface
  200. *
  201. * We implement searching the database in the DatabaseResolver which avoids a n+1 query here.
  202. * We can't simply let ScriptCollection inherit Collection and Resolver because both are QObjects,
  203. * although Resolver doesn't need to be a QObject atm, blocking adding signals/slots to Resolver
  204. * in future seems to me worse than violating Liskov's law here. ~ domme
  205. */
  206. unsigned int
  207. DatabaseCollection::timeout() const
  208. {
  209. return 0;
  210. }
  211. unsigned int
  212. DatabaseCollection::weight() const
  213. {
  214. return 0;
  215. }
  216. void
  217. DatabaseCollection::resolve( const Tomahawk::query_ptr& query )
  218. {
  219. Q_UNUSED( query );
  220. Q_ASSERT(false);
  221. }