/src/libtomahawk/database/DatabaseCommand_LoadSocialActions.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 108 lines · 66 code · 23 blank · 19 comment · 8 complexity · d80e2cb74109d6e8f5b7716a8591849a MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christopher Reichert <creichert07@gmail.com>
  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_LoadSocialActions.h"
  19. #include <QSqlQuery>
  20. #include "collection/Collection.h"
  21. #include "database/Database.h"
  22. #include "network/Servent.h"
  23. #include "utils/Logger.h"
  24. #include "DatabaseImpl.h"
  25. #include "PlaylistEntry.h"
  26. #include "Result.h"
  27. #include "Track.h"
  28. using namespace Tomahawk;
  29. void
  30. DatabaseCommand_LoadSocialActions::exec( DatabaseImpl* dbi )
  31. {
  32. qDebug() << Q_FUNC_INFO;
  33. Q_ASSERT( !source().isNull() );
  34. TomahawkSqlQuery query = dbi->newquery();
  35. if ( m_actionOnly.isNull() )
  36. {
  37. // Load for just specified track
  38. if ( m_track->trackId() == 0 )
  39. return;
  40. QString whereToken;
  41. whereToken = QString( "WHERE id IS %1" ).arg( m_track->trackId() );
  42. QString sql = QString(
  43. "SELECT k, v, timestamp, source "
  44. "FROM social_attributes %1 "
  45. "ORDER BY timestamp ASC" ).arg( whereToken );
  46. query.prepare( sql );
  47. query.exec();
  48. QList< Tomahawk::SocialAction > allSocialActions;
  49. while ( query.next() )
  50. {
  51. Tomahawk::SocialAction action;
  52. action.action = query.value( 0 ); // action
  53. action.value = query.value( 1 ); // comment
  54. action.timestamp = query.value( 2 ); // timestamp
  55. action.source = SourceList::instance()->get( query.value( 3 ).toInt() ); // source
  56. if ( !action.source.isNull() )
  57. allSocialActions.append( action );
  58. }
  59. m_track->setAllSocialActions( allSocialActions );
  60. }
  61. else
  62. {
  63. // Load all tracks with this social action
  64. const QString srcStr = source()->isLocal() ? "IS NULL" : QString( "=%1" ).arg( source()->id() );
  65. query.prepare( QString( "SELECT id, v, timestamp FROM social_attributes WHERE source %1 AND k = ? " ).arg( srcStr ) );
  66. query.addBindValue( m_actionOnly );
  67. query.exec();
  68. DatabaseCommand_LoadSocialActions::TrackActions trackActions;
  69. while ( query.next() )
  70. {
  71. const QVariantMap track = dbi->track( query.value( 0 ).toInt() );
  72. if ( track.value( "artist" ).toString().isEmpty() || track.value( "name" ).toString().isEmpty() )
  73. continue;
  74. const QVariantMap artist = dbi->artist( track.value( "artist" ).toInt() );
  75. const track_ptr t = Track::get( artist.value( "name" ).toString(), track.value( "name" ).toString(), QString() );
  76. Tomahawk::SocialAction action;
  77. action.action = m_actionOnly; // action
  78. action.value = query.value( 1 ); // comment
  79. action.timestamp = query.value( 2 ); // timestamp
  80. action.source = source(); // source
  81. trackActions[ t ] = action;
  82. }
  83. emit done( trackActions );
  84. }
  85. }