/src/libtomahawk/playlist/AlbumModel.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 267 lines · 189 code · 60 blank · 18 comment · 23 complexity · a2199e05b148d08876da546e480a8c55 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2015, 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 "AlbumModel.h"
  20. #include <QListView>
  21. #include <QMimeData>
  22. #include <QNetworkReply>
  23. #include "Artist.h"
  24. #include "PlayableItem.h"
  25. #include "Source.h"
  26. #include "SourceList.h"
  27. #include "database/Database.h"
  28. #include "utils/TomahawkUtils.h"
  29. #include "utils/Logger.h"
  30. using namespace Tomahawk;
  31. AlbumModel::AlbumModel( QObject* parent )
  32. : PlayableModel( parent )
  33. , m_overwriteOnAdd( false )
  34. {
  35. }
  36. AlbumModel::~AlbumModel()
  37. {
  38. }
  39. void
  40. AlbumModel::addCollection( const collection_ptr& collection, bool overwrite )
  41. {
  42. DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection );
  43. m_overwriteOnAdd = overwrite;
  44. m_collection = collection;
  45. connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ),
  46. SLOT( addAlbums( QList<Tomahawk::album_ptr> ) ) );
  47. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  48. setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) );
  49. if ( collection.isNull() )
  50. {
  51. connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ), Qt::UniqueConnection );
  52. QList<Tomahawk::source_ptr> sources = SourceList::instance()->sources();
  53. foreach ( const source_ptr& source, sources )
  54. {
  55. connect( source->dbCollection().data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
  56. }
  57. }
  58. else
  59. {
  60. connect( collection.data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
  61. }
  62. emit loadingStarted();
  63. }
  64. void
  65. AlbumModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllAlbums::SortOrder order, bool overwrite )
  66. {
  67. DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection );
  68. cmd->setLimit( amount );
  69. cmd->setSortOrder( order );
  70. cmd->setSortDescending( true );
  71. m_overwriteOnAdd = overwrite;
  72. m_collection = collection;
  73. connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ),
  74. SLOT( addAlbums( QList<Tomahawk::album_ptr> ) ) );
  75. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  76. if ( !collection.isNull() )
  77. setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) );
  78. else
  79. setTitle( tr( "All albums" ) );
  80. emit loadingStarted();
  81. }
  82. void
  83. AlbumModel::addAlbums( const QList<Tomahawk::album_ptr>& albums )
  84. {
  85. emit loadingFinished();
  86. if ( m_overwriteOnAdd )
  87. clear();
  88. QList<Tomahawk::album_ptr> trimmedAlbums;
  89. foreach ( const album_ptr& album, albums )
  90. {
  91. if ( !album.isNull() && !album->name().isEmpty() )
  92. {
  93. if ( findItem( album ) || trimmedAlbums.contains( album ) )
  94. continue;
  95. trimmedAlbums << album;
  96. }
  97. }
  98. if ( trimmedAlbums.isEmpty() )
  99. {
  100. emit itemCountChanged( rowCount( QModelIndex() ) );
  101. return;
  102. }
  103. int c = rowCount( QModelIndex() );
  104. QPair< int, int > crows;
  105. crows.first = c;
  106. crows.second = c + trimmedAlbums.count() - 1;
  107. emit beginInsertRows( QModelIndex(), crows.first, crows.second );
  108. foreach( const album_ptr& album, trimmedAlbums )
  109. {
  110. PlayableItem* albumitem = new PlayableItem( album, rootItem() );
  111. albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
  112. connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
  113. }
  114. emit endInsertRows();
  115. emit itemCountChanged( rowCount( QModelIndex() ) );
  116. }
  117. void
  118. AlbumModel::addArtists( const QList<Tomahawk::artist_ptr>& artists )
  119. {
  120. emit loadingFinished();
  121. if ( m_overwriteOnAdd )
  122. clear();
  123. QList<Tomahawk::artist_ptr> trimmedArtists;
  124. foreach ( const artist_ptr& artist, artists )
  125. {
  126. if ( !artist.isNull() && !artist->name().isEmpty() )
  127. {
  128. if ( findItem( artist ) || trimmedArtists.contains( artist ) )
  129. continue;
  130. trimmedArtists << artist;
  131. }
  132. }
  133. if ( trimmedArtists.isEmpty() )
  134. {
  135. emit itemCountChanged( rowCount( QModelIndex() ) );
  136. return;
  137. }
  138. int c = rowCount( QModelIndex() );
  139. QPair< int, int > crows;
  140. crows.first = c;
  141. crows.second = c + trimmedArtists.count() - 1;
  142. emit beginInsertRows( QModelIndex(), crows.first, crows.second );
  143. foreach ( const artist_ptr& artist, trimmedArtists )
  144. {
  145. PlayableItem* albumitem = new PlayableItem( artist, rootItem() );
  146. albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
  147. connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
  148. }
  149. emit endInsertRows();
  150. emit itemCountChanged( rowCount( QModelIndex() ) );
  151. }
  152. void
  153. AlbumModel::addQueries( const QList<Tomahawk::query_ptr>& queries )
  154. {
  155. emit loadingFinished();
  156. if ( m_overwriteOnAdd )
  157. clear();
  158. int c = rowCount( QModelIndex() );
  159. QPair< int, int > crows;
  160. crows.first = c;
  161. crows.second = c + queries.count() - 1;
  162. emit beginInsertRows( QModelIndex(), crows.first, crows.second );
  163. foreach ( const query_ptr& query, queries )
  164. {
  165. PlayableItem* albumitem = new PlayableItem( query, rootItem() );
  166. albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
  167. connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
  168. }
  169. emit endInsertRows();
  170. emit itemCountChanged( rowCount( QModelIndex() ) );
  171. }
  172. void
  173. AlbumModel::onSourceAdded( const Tomahawk::source_ptr& source )
  174. {
  175. connect( source->dbCollection().data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
  176. }
  177. void
  178. AlbumModel::onCollectionChanged()
  179. {
  180. addCollection( m_collection, true );
  181. }
  182. PlayableItem*
  183. AlbumModel::findItem( const artist_ptr& artist ) const
  184. {
  185. for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
  186. {
  187. PlayableItem* item = itemFromIndex( index( i, 0, QModelIndex() ) );
  188. if ( !item->artist().isNull() && item->artist() == artist )
  189. {
  190. return item;
  191. }
  192. }
  193. return 0;
  194. }
  195. PlayableItem*
  196. AlbumModel::findItem( const album_ptr& album ) const
  197. {
  198. for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
  199. {
  200. PlayableItem* item = itemFromIndex( index( i, 0, QModelIndex() ) );
  201. if ( !item->album().isNull() && item->album() == album )
  202. {
  203. return item;
  204. }
  205. }
  206. return 0;
  207. }