PageRenderTime 102ms CodeModel.GetById 50ms app.highlight 10ms RepoModel.GetById 40ms app.codeStats 1ms

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