/src/libtomahawk/playlist/collectionflatmodel.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 150 lines · 97 code · 34 blank · 19 comment · 13 complexity · 991f7dde0dfa279f77c0d195a90c4a84 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. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "collectionflatmodel.h"
  19. #include "database/database.h"
  20. #include "sourcelist.h"
  21. #include "utils/logger.h"
  22. using namespace Tomahawk;
  23. CollectionFlatModel::CollectionFlatModel( QObject* parent )
  24. : TrackModel( parent )
  25. {
  26. }
  27. CollectionFlatModel::~CollectionFlatModel()
  28. {
  29. }
  30. void
  31. CollectionFlatModel::addCollections( const QList< collection_ptr >& collections )
  32. {
  33. foreach( const collection_ptr& col, collections )
  34. {
  35. addCollection( col );
  36. }
  37. // we are waiting for some to load
  38. if( !m_loadingCollections.isEmpty() )
  39. emit loadingStarted();
  40. }
  41. void
  42. CollectionFlatModel::addCollection( const collection_ptr& collection, bool sendNotifications )
  43. {
  44. qDebug() << Q_FUNC_INFO << collection->name()
  45. << collection->source()->id()
  46. << collection->source()->userName();
  47. if ( sendNotifications )
  48. emit loadingStarted();
  49. DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( collection );
  50. connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr>, QVariant ) ),
  51. SLOT( onTracksAdded( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
  52. Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
  53. m_loadingCollections << collection.data();
  54. if ( collection->source()->isLocal() )
  55. setTitle( tr( "My Collection" ) );
  56. else
  57. setTitle( tr( "Collection of %1" ).arg( collection->source()->friendlyName() ) );
  58. }
  59. void
  60. CollectionFlatModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllTracks::SortOrder order )
  61. {
  62. qDebug() << Q_FUNC_INFO << collection->name()
  63. << collection->source()->id()
  64. << collection->source()->userName()
  65. << amount << order;
  66. DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( collection );
  67. cmd->setLimit( amount );
  68. cmd->setSortOrder( order );
  69. cmd->setSortDescending( true );
  70. connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr>, QVariant ) ),
  71. SLOT( onTracksAdded( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
  72. Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
  73. }
  74. void
  75. CollectionFlatModel::onTracksAdded( const QList<Tomahawk::query_ptr>& tracks )
  76. {
  77. qDebug() << Q_FUNC_INFO << tracks.count() << rowCount( QModelIndex() );
  78. if ( !m_loadingCollections.isEmpty() && sender() && qobject_cast< Collection* >( sender() ) )
  79. {
  80. // we are keeping track and are called as a slot
  81. m_loadingCollections.removeAll( qobject_cast< Collection* >( sender() ) );
  82. }
  83. append( tracks );
  84. if ( m_loadingCollections.isEmpty() )
  85. emit loadingFinished();
  86. }
  87. void
  88. CollectionFlatModel::onTracksRemoved( const QList<Tomahawk::query_ptr>& tracks )
  89. {
  90. QList<Tomahawk::query_ptr> t = tracks;
  91. for ( int i = rowCount( QModelIndex() ); i >= 0 && t.count(); i-- )
  92. {
  93. QModelIndex idx = index( i, 0, QModelIndex() );
  94. TrackModelItem* item = itemFromIndex( idx );
  95. if ( !item )
  96. continue;
  97. int j = 0;
  98. foreach ( const query_ptr& query, t )
  99. {
  100. if ( item->query().data() == query.data() )
  101. {
  102. remove( idx );
  103. t.removeAt( j );
  104. break;
  105. }
  106. j++;
  107. }
  108. }
  109. }
  110. void
  111. CollectionFlatModel::onDataChanged()
  112. {
  113. TrackModelItem* p = (TrackModelItem*)sender();
  114. if ( p )
  115. emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount( QModelIndex() ) - 1 ) );
  116. }