/src/libtomahawk/database/DatabaseCommand_SocialAction.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 100 lines · 62 code · 19 blank · 19 comment · 9 complexity · e8b0f9fd1452c6e66b4448cb8688c872 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_SocialAction.h"
  19. #include "collection/Collection.h"
  20. #include "database/Database.h"
  21. #include "network/Servent.h"
  22. #include "utils/Logger.h"
  23. #include "DatabaseImpl.h"
  24. #include "PlaylistEntry.h"
  25. #include <QSqlQuery>
  26. using namespace Tomahawk;
  27. void
  28. DatabaseCommand_SocialAction::postCommitHook()
  29. {
  30. qDebug() << Q_FUNC_INFO;
  31. if ( source()->isLocal() )
  32. {
  33. Servent::instance()->triggerDBSync();
  34. }
  35. trackdata_ptr trackData = TrackData::get( 0, m_artist, m_title );
  36. if ( trackData )
  37. trackData->loadSocialActions( true );
  38. source()->reportSocialAttributesChanged( this );
  39. }
  40. void
  41. DatabaseCommand_SocialAction::exec( DatabaseImpl* dbi )
  42. {
  43. qDebug() << Q_FUNC_INFO;
  44. Q_ASSERT( !source().isNull() );
  45. TomahawkSqlQuery query = dbi->newquery();
  46. QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id();
  47. if ( m_artist.isNull() || m_title.isEmpty() || m_action.isEmpty() )
  48. return;
  49. int artid = dbi->artistId( m_artist, true );
  50. if ( artid < 1 )
  51. return;
  52. int trkid = dbi->trackId( artid, m_title, true );
  53. if ( trkid < 1 )
  54. return;
  55. // update if it already exists
  56. TomahawkSqlQuery find = dbi->newquery();
  57. find.prepare( QString( "SELECT id, k, v FROM social_attributes WHERE social_attributes.id = ? AND social_attributes.source %1 AND social_attributes.k = ?" ).arg( source()->isLocal() ? "IS NULL" : QString( "=%1" ).arg( source()->id() ) ) );
  58. find.addBindValue( trkid );
  59. find.addBindValue( m_action );
  60. if ( find.exec() && find.next() )
  61. {
  62. // update
  63. query.prepare( QString( "UPDATE social_attributes SET v = '%1', timestamp = %2 WHERE social_attributes.id = %3 AND social_attributes.source %4 AND social_attributes.k = '%5'" )
  64. .arg( m_comment )
  65. .arg( m_timestamp )
  66. .arg( trkid )
  67. .arg( source()->isLocal() ? "IS NULL" : QString( "=%1" ).arg( source()->id() ) )
  68. .arg( m_action ) );
  69. }
  70. else
  71. {
  72. query.prepare( "INSERT INTO social_attributes(id, source, k, v, timestamp) "
  73. "VALUES (?, ?, ?, ?, ?)" );
  74. query.bindValue( 0, trkid );
  75. query.bindValue( 1, srcid );
  76. query.bindValue( 2, m_action );
  77. query.bindValue( 3, m_comment );
  78. query.bindValue( 4, m_timestamp );
  79. }
  80. query.exec();
  81. }