PageRenderTime 92ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 31ms app.codeStats 0ms

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