/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 112 lines · 75 code · 20 blank · 17 comment · 4 complexity · fde1b874ca566f372f896d14aa01a211 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 "DatabaseCommand_AllArtists.h"
  19. #include "utils/TomahawkUtils.h"
  20. #include "utils/Logger.h"
  21. #include "Artist.h"
  22. #include "DatabaseImpl.h"
  23. #include "PlaylistEntry.h"
  24. #include "Source.h"
  25. #include <QSqlQuery>
  26. namespace Tomahawk
  27. {
  28. DatabaseCommand_AllArtists::DatabaseCommand_AllArtists( const Tomahawk::collection_ptr& collection, QObject* parent )
  29. : DatabaseCommand( parent )
  30. , m_collection( collection.objectCast< DatabaseCollection >() )
  31. , m_amount( 0 )
  32. , m_sortOrder( DatabaseCommand_AllArtists::None )
  33. , m_sortDescending( false )
  34. {
  35. }
  36. DatabaseCommand_AllArtists::~DatabaseCommand_AllArtists()
  37. {
  38. }
  39. void
  40. DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
  41. {
  42. TomahawkSqlQuery query = dbi->newquery();
  43. QString orderToken, sourceToken, filterToken, tables, joins;
  44. switch ( m_sortOrder )
  45. {
  46. case 0:
  47. break;
  48. case ModificationTime:
  49. orderToken = "file.mtime";
  50. }
  51. if ( !m_collection.isNull() )
  52. sourceToken = QString( "AND file.source %1" ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
  53. if ( !m_filter.isEmpty() )
  54. {
  55. QString filtersql;
  56. QStringList sl = m_filter.split( " ", QString::SkipEmptyParts );
  57. foreach( QString s, sl )
  58. {
  59. filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkSqlQuery::escape( s ) );
  60. }
  61. filterToken = QString( "AND file_join.track = track.id %1" ).arg( filtersql );
  62. joins = "LEFT JOIN album ON album.id = file_join.album";
  63. tables = "artist, track, file, file_join";
  64. }
  65. else
  66. tables = "artist, file, file_join";
  67. QString sql = QString(
  68. "SELECT DISTINCT artist.id, artist.name "
  69. "FROM %1 "
  70. "%2 "
  71. "WHERE file.id = file_join.file "
  72. "AND file_join.artist = artist.id "
  73. "%3 %4 %5 %6 %7"
  74. ).arg( tables )
  75. .arg( joins )
  76. .arg( sourceToken )
  77. .arg( filterToken )
  78. .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
  79. .arg( m_sortDescending ? "DESC" : QString() )
  80. .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
  81. query.prepare( sql );
  82. query.exec();
  83. QList<Tomahawk::artist_ptr> al;
  84. while ( query.next() )
  85. {
  86. Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 0 ).toUInt(), query.value( 1 ).toString() );
  87. al << artist;
  88. }
  89. emit artists( al );
  90. emit done();
  91. }
  92. }