/src/libtomahawk/database/DatabaseCommand_PlaybackHistory.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 95 lines · 60 code · 18 blank · 17 comment · 6 complexity · 0436a81f1381911b2fb56f4ff0b43bf1 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_PlaybackHistory.h"
  19. #include <QSqlQuery>
  20. #include "DatabaseImpl.h"
  21. #include "SourceList.h"
  22. #include "utils/Logger.h"
  23. namespace Tomahawk
  24. {
  25. void
  26. DatabaseCommand_PlaybackHistory::exec( DatabaseImpl* dbi )
  27. {
  28. TomahawkSqlQuery query = dbi->newquery();
  29. QString whereToken( "WHERE 1" );
  30. if ( !source().isNull() )
  31. {
  32. whereToken += QString( " AND source %1" ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
  33. }
  34. if ( m_dateFrom.year() > 1900 && m_dateTo.year() > 1900 )
  35. {
  36. whereToken += QString( " AND playtime >= %1 AND playtime <= %2" )
  37. .arg( QDateTime( m_dateFrom ).toUTC().toTime_t() )
  38. .arg( QDateTime( m_dateTo.addDays( 1 ) ).toUTC().toTime_t() );
  39. }
  40. QString sql = QString(
  41. "SELECT track, playtime, secs_played, source "
  42. "FROM playback_log "
  43. "%1 "
  44. "ORDER BY playtime DESC "
  45. "%2" ).arg( whereToken )
  46. .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
  47. query.prepare( sql );
  48. query.exec();
  49. QList<Tomahawk::track_ptr> tl;
  50. QList<Tomahawk::PlaybackLog> logs;
  51. while ( query.next() )
  52. {
  53. TomahawkSqlQuery query_track = dbi->newquery();
  54. QString sql = QString(
  55. "SELECT track.name, artist.name "
  56. "FROM track, artist "
  57. "WHERE artist.id = track.artist "
  58. "AND track.id = %1"
  59. ).arg( query.value( 0 ).toUInt() );
  60. query_track.prepare( sql );
  61. query_track.exec();
  62. if ( query_track.next() )
  63. {
  64. Tomahawk::track_ptr track = Tomahawk::Track::get( query_track.value( 1 ).toString(), query_track.value( 0 ).toString(), QString() );
  65. if ( !track )
  66. continue;
  67. Tomahawk::PlaybackLog log;
  68. log.timestamp = query.value( 1 ).toUInt();
  69. log.secsPlayed = query.value( 2 ).toUInt();
  70. log.source = SourceList::instance()->get( query.value( 3 ).toUInt() );
  71. logs << log;
  72. tl << track;
  73. }
  74. }
  75. emit tracks( tl, logs );
  76. }
  77. }