/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 194 lines · 141 code · 36 blank · 17 comment · 9 complexity · 2c563f2da4ded9637f9c2637c8834ce1 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_AllAlbums.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 <QDateTime>
  26. #include <QSqlQuery>
  27. namespace Tomahawk
  28. {
  29. DatabaseCommand_AllAlbums::DatabaseCommand_AllAlbums( const Tomahawk::collection_ptr& collection, const Tomahawk::artist_ptr& artist, QObject* parent )
  30. : DatabaseCommand( parent )
  31. , m_collection( collection.objectCast< DatabaseCollection >() )
  32. , m_artist( artist )
  33. , m_amount( 0 )
  34. , m_sortOrder( DatabaseCommand_AllAlbums::None )
  35. , m_sortDescending( false )
  36. {
  37. }
  38. DatabaseCommand_AllAlbums::~DatabaseCommand_AllAlbums()
  39. {
  40. }
  41. void
  42. DatabaseCommand_AllAlbums::setArtist( const Tomahawk::artist_ptr& artist )
  43. {
  44. m_artist = artist;
  45. }
  46. void
  47. DatabaseCommand_AllAlbums::execForArtist( DatabaseImpl* dbi )
  48. {
  49. TomahawkSqlQuery query = dbi->newquery();
  50. QList<Tomahawk::album_ptr> al;
  51. QString orderToken, sourceToken, filterToken, timeToken, tables;
  52. switch ( m_sortOrder )
  53. {
  54. case 0:
  55. break;
  56. case ModificationTime:
  57. orderToken = "file.mtime";
  58. timeToken = QString( "AND file.mtime <= %1" ).arg( QDateTime::currentDateTimeUtc().toTime_t() );
  59. }
  60. if ( !m_collection.isNull() )
  61. sourceToken = QString( "AND file.source %1" ).arg( m_collection->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
  62. if ( !m_filter.isEmpty() )
  63. {
  64. QString filtersql;
  65. QStringList sl = m_filter.split( " ", QString::SkipEmptyParts );
  66. foreach( QString s, sl )
  67. {
  68. filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkSqlQuery::escape( s ) );
  69. }
  70. filterToken = QString( "AND artist.id = file_join.artist AND file_join.track = track.id %1" ).arg( filtersql );
  71. tables = "file, file_join, artist, track";
  72. }
  73. else
  74. tables = "file, file_join";
  75. QString sql = QString(
  76. "SELECT DISTINCT album.id, album.name "
  77. "FROM %1 "
  78. "LEFT OUTER JOIN album ON file_join.album = album.id "
  79. "WHERE file.id = file_join.file "
  80. "AND file_join.artist = %2 "
  81. "%3 %4 %5 %6 %7 %8"
  82. ).arg( tables )
  83. .arg( m_artist->id() )
  84. .arg( sourceToken )
  85. .arg( timeToken )
  86. .arg( filterToken )
  87. .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
  88. .arg( m_sortDescending ? "DESC" : QString() )
  89. .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
  90. query.prepare( sql );
  91. query.exec();
  92. while( query.next() )
  93. {
  94. unsigned int albumId = query.value( 0 ).toUInt();
  95. QString albumName = query.value( 1 ).toString();
  96. if ( query.value( 0 ).isNull() )
  97. {
  98. albumName = tr( "Unknown" );
  99. }
  100. Tomahawk::album_ptr album = Tomahawk::Album::get( albumId, albumName, m_artist );
  101. al << album;
  102. }
  103. emit albums( al, data() );
  104. emit albums( al );
  105. emit done();
  106. }
  107. void
  108. DatabaseCommand_AllAlbums::execForCollection( DatabaseImpl* dbi )
  109. {
  110. TomahawkSqlQuery query = dbi->newquery();
  111. QList<Tomahawk::album_ptr> al;
  112. QString orderToken, sourceToken;
  113. switch ( m_sortOrder )
  114. {
  115. case 0:
  116. break;
  117. case ModificationTime:
  118. orderToken = "file.mtime";
  119. }
  120. if ( !m_collection.isNull() )
  121. sourceToken = QString( "AND file.source %1 " ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
  122. QString sql = QString(
  123. "SELECT DISTINCT album.id, album.name, album.artist, artist.name "
  124. "FROM file_join, file, album "
  125. "LEFT OUTER JOIN artist ON album.artist = artist.id "
  126. "WHERE file.id = file_join.file "
  127. "AND file_join.album = album.id "
  128. "%1 "
  129. "%2 %3 %4"
  130. ).arg( sourceToken )
  131. .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
  132. .arg( m_sortDescending ? "DESC" : QString() )
  133. .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
  134. query.prepare( sql );
  135. query.exec();
  136. while( query.next() )
  137. {
  138. Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 2 ).toUInt(), query.value( 3 ).toString() );
  139. Tomahawk::album_ptr album = Tomahawk::Album::get( query.value( 0 ).toUInt(), query.value( 1 ).toString(), artist );
  140. al << album;
  141. }
  142. emit albums( al, data() );
  143. emit albums( al );
  144. emit done();
  145. }
  146. void
  147. DatabaseCommand_AllAlbums::exec( DatabaseImpl* dbi )
  148. {
  149. if ( !m_artist.isNull() )
  150. {
  151. execForArtist( dbi );
  152. }
  153. else
  154. {
  155. execForCollection( dbi );
  156. }
  157. }
  158. }