From bef7a63732131dc9f084250a99dec3e3c67a19b8 Mon Sep 17 00:00:00 2001 From: Christopher Reichert Date: Fri, 10 Jun 2011 15:38:59 -0500 Subject: [PATCH] Added database commands for Social action. Love button in audiocontrols sends message through info system to sync love status with local database. The only social action that is currently setup is lastfm's "love". --- src/audiocontrols.cpp | 21 ++++- src/audiocontrols.h | 1 + src/libtomahawk/CMakeLists.txt | 2 + src/libtomahawk/database/databasecommand.cpp | 8 ++ .../database/databasecommand_socialaction.cpp | 71 +++++++++++++++ .../database/databasecommand_socialaction.h | 88 +++++++++++++++++++ .../infosystem/infoplugins/lastfmplugin.cpp | 38 +++++++- .../infosystem/infoplugins/lastfmplugin.h | 4 +- src/libtomahawk/infosystem/infosystem.h | 4 +- 9 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 src/libtomahawk/database/databasecommand_socialaction.cpp create mode 100644 src/libtomahawk/database/databasecommand_socialaction.h diff --git a/src/audiocontrols.cpp b/src/audiocontrols.cpp index 7b060b327..5247e38be 100644 --- a/src/audiocontrols.cpp +++ b/src/audiocontrols.cpp @@ -25,6 +25,8 @@ #include "viewmanager.h" #include "utils/imagebutton.h" #include "utils/tomahawkutils.h" +#include "database/database.h" +#include "database/databasecommand_socialaction.h" #include "album.h" @@ -137,7 +139,6 @@ AudioControls::AudioControls( QWidget* parent ) connect( ui->volumeLowButton, SIGNAL( clicked() ), AudioEngine::instance(), SLOT( lowerVolume() ) ); connect( ui->volumeHighButton, SIGNAL( clicked() ), AudioEngine::instance(), SLOT( raiseVolume() ) ); - connect( ui->playPauseButton, SIGNAL( clicked() ), this, SIGNAL( playPressed() ) ); connect( ui->pauseButton, SIGNAL( clicked() ), this, SIGNAL( pausePressed() ) ); @@ -478,3 +479,21 @@ AudioControls::onTrackClicked() { ViewManager::instance()->showCurrentTrack(); } + + +void +AudioControls::onLoveButtonClicked() +{ + Tomahawk::InfoSystem::InfoCriteriaHash trackInfo; + trackInfo["title"] = m_currentTrack->track(); + trackInfo["artist"] = m_currentTrack->artist()->name(); + trackInfo["album"] = m_currentTrack->album()->name(); + + Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( + s_acInfoIdentifier, Tomahawk::InfoSystem::InfoLove, + QVariant::fromValue< Tomahawk::InfoSystem::InfoCriteriaHash >( trackInfo ) ); + + DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction( m_currentTrack, QString( "Love" ) ); + Database::instance()->enqueue( QSharedPointer(cmd) ); +} + diff --git a/src/audiocontrols.h b/src/audiocontrols.h index 82f2ee623..90c154c73 100644 --- a/src/audiocontrols.h +++ b/src/audiocontrols.h @@ -65,6 +65,7 @@ private slots: void onArtistClicked(); void onAlbumClicked(); void onTrackClicked(); + void onLoveButtonClicked(); void infoSystemInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); void infoSystemFinished( QString target ); diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 6cef087ca..1eaff36d8 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -79,6 +79,7 @@ set( libSources database/databasecommand_deletedynamicplaylist.cpp database/databasecommand_addclientauth.cpp database/databasecommand_clientauthvalid.cpp + database/databasecommand_socialaction.cpp database/database.cpp infosystem/infosystemcache.cpp @@ -245,6 +246,7 @@ set( libHeaders database/databasecommand_loadallstations.h database/databasecommand_addclientauth.h database/databasecommand_clientauthvalid.h + database/databasecommand_socialaction.h infosystem/infosystem.h infosystem/infosystemworker.h diff --git a/src/libtomahawk/database/databasecommand.cpp b/src/libtomahawk/database/databasecommand.cpp index 41824c87c..d90d90b09 100644 --- a/src/libtomahawk/database/databasecommand.cpp +++ b/src/libtomahawk/database/databasecommand.cpp @@ -30,6 +30,7 @@ #include "databasecommand_createdynamicplaylist.h" #include "databasecommand_deletedynamicplaylist.h" #include "databasecommand_setdynamicplaylistrevision.h" +#include "databasecommand_socialaction.h" DatabaseCommand::DatabaseCommand( QObject* parent ) @@ -142,6 +143,13 @@ DatabaseCommand::factory( const QVariant& op, const source_ptr& source ) QJson::QObjectHelper::qvariant2qobject( op.toMap(), cmd ); return cmd; } + else if( name == "socialaction" ) + { + DatabaseCommand_SocialAction * cmd = new DatabaseCommand_SocialAction; + cmd->setSource( source ); + QJson::QObjectHelper::qvariant2qobject( op.toMap(), cmd ); + return cmd; + } qDebug() << "ERROR in" << Q_FUNC_INFO << name; // Q_ASSERT( false ); diff --git a/src/libtomahawk/database/databasecommand_socialaction.cpp b/src/libtomahawk/database/databasecommand_socialaction.cpp new file mode 100644 index 000000000..850cfbc5f --- /dev/null +++ b/src/libtomahawk/database/databasecommand_socialaction.cpp @@ -0,0 +1,71 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christopher Reichert + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "databasecommand_socialaction.h" + +#include + +#include "database/database.h" +#include "databaseimpl.h" +#include "network/servent.h" + +using namespace Tomahawk; + + +void +DatabaseCommand_SocialAction::postCommitHook() +{ + qDebug() << Q_FUNC_INFO; + if ( source()->isLocal() ) + { + Servent::instance()->triggerDBSync(); + } +} + + +void +DatabaseCommand_SocialAction::exec( DatabaseImpl* dbi ) +{ + qDebug() << Q_FUNC_INFO; + Q_ASSERT( !source().isNull() ); + + TomahawkSqlQuery query = dbi->newquery(); + + query.prepare( "INSERT INTO social_attributes(id, source, k, v, timestamp) " + "VALUES (?, ?, ?, ?, ?)" ); + + QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id(); + + bool isnew; + int artid = dbi->artistId( m_artist, isnew ); + if( artid < 1 ) + return; + + int trkid = dbi->trackId( artid, m_track, isnew ); + if( trkid < 1 ) + return; + + query.bindValue( 0, trkid ); + query.bindValue( 1, srcid ); + query.bindValue( 2, m_action ); + query.bindValue( 3, m_comment ); + query.bindValue( 4, m_timestamp ); + + query.exec(); +} + diff --git a/src/libtomahawk/database/databasecommand_socialaction.h b/src/libtomahawk/database/databasecommand_socialaction.h new file mode 100644 index 000000000..070cb38bb --- /dev/null +++ b/src/libtomahawk/database/databasecommand_socialaction.h @@ -0,0 +1,88 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2011, Christopher Reichert + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef DATABASECOMMAND_SOCIALACTION_H +#define DATABASECOMMAND_SOCIALACTION_H + +#include +#include "database/databasecommand.h" + +#include "sourcelist.h" +#include "typedefs.h" +#include "artist.h" + +#include "dllmacro.h" + + +class DLLEXPORT DatabaseCommand_SocialAction : public DatabaseCommand +{ +Q_OBJECT +Q_PROPERTY( QString action READ action WRITE setAction ) +Q_PROPERTY( QString comment READ comment WRITE setComment ) +Q_PROPERTY( int timestamp READ timestamp WRITE setTimestamp ) + +public: + + explicit DatabaseCommand_SocialAction( QObject* parent = 0 ) + : DatabaseCommand( parent ) + {} + + explicit DatabaseCommand_SocialAction( const Tomahawk::result_ptr& result, QString action, QString comment="", QObject* parent = 0 ) + : DatabaseCommand( parent ), m_result( result ), m_action( action ) + { + setSource( SourceList::instance()->getLocal() ); + + setArtist( result->artist()->name() ); + setTrack( result->track() ); + setComment( comment ); + setTimestamp( QDateTime::currentDateTime().toTime_t() ); + } + + virtual QString commandname() const { return "socialaction"; } + + virtual void exec( DatabaseImpl* ); + virtual void postCommitHook(); + + QString artist() const { return m_artist; } + void setArtist( const QString& s ) { m_artist = s; } + + QString track() const { return m_track; } + void setTrack( const QString& s ) { m_track = s; } + + // key + QString action() const { return m_action; } + void setAction( QString a ) { m_action = a; } + + // value + QString comment() const { return m_comment; } + void setComment( const QString& com ) { m_comment = com; } + + int timestamp() const { return m_timestamp; } + void setTimestamp( const int ts ) { m_timestamp = ts; } + +private: + Tomahawk::result_ptr m_result; + + QString m_artist; + QString m_track; + int m_timestamp; + QString m_comment; + QString m_action; +}; + +#endif // DATABASECOMMAND_SOCIALACTION_H diff --git a/src/libtomahawk/infosystem/infoplugins/lastfmplugin.cpp b/src/libtomahawk/infosystem/infoplugins/lastfmplugin.cpp index 92dbfaafa..a483c4565 100644 --- a/src/libtomahawk/infosystem/infoplugins/lastfmplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/lastfmplugin.cpp @@ -45,8 +45,8 @@ LastFmPlugin::LastFmPlugin() : InfoPlugin() , m_scrobbler( 0 ) { - m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages; - m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying; + m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoLove; + m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove; /* Your API Key is 7194b85b6d1f424fe1668173a78c0c4a @@ -145,11 +145,16 @@ LastFmPlugin::pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoTy scrobble(); break; + case InfoLove: + sendLoveSong( input ); + break; + default: return; } } + void LastFmPlugin::nowPlaying( const QVariant &input ) { @@ -195,6 +200,34 @@ LastFmPlugin::scrobble() } +void +LastFmPlugin::sendLoveSong( QVariant input ) +{ + qDebug() << Q_FUNC_INFO; + + if ( !input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() ) + { + qDebug() << "LastFmPlugin::nowPlaying cannot convert input!"; + return; + } + + InfoCriteriaHash hash = input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); + if ( !hash.contains( "title" ) || !hash.contains( "artist" ) || !hash.contains( "album" ) || !hash.contains( "duration" ) ) + return; + + lastfm::MutableTrack track; + track.stamp(); + + track.setTitle( hash["title"] ); + track.setArtist( hash["artist"] ); + track.setAlbum( hash["album"] ); + bool ok; + track.setDuration( hash["duration"].toUInt( &ok ) ); + track.setSource( lastfm::Track::Player ); + track.love(); +} + + void LastFmPlugin::fetchCoverArt( const QString &caller, const InfoType type, const QVariant &input, const Tomahawk::InfoSystem::InfoCustomData &customData ) { @@ -504,3 +537,4 @@ LastFmPlugin::createScrobbler() m_scrobbler = new lastfm::Audioscrobbler( "thk" ); } } + diff --git a/src/libtomahawk/infosystem/infoplugins/lastfmplugin.h b/src/libtomahawk/infosystem/infoplugins/lastfmplugin.h index 123f5c4bb..ab194a5b1 100644 --- a/src/libtomahawk/infosystem/infoplugins/lastfmplugin.h +++ b/src/libtomahawk/infosystem/infoplugins/lastfmplugin.h @@ -3,8 +3,7 @@ * Copyright 2010-2011, Christian Muehlhaeuser * * Tomahawk is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Tomahawk is distributed in the hope that it will be useful, @@ -68,6 +67,7 @@ private: void scrobble(); void dataError( const QString &caller, const Tomahawk::InfoSystem::InfoType type, const QVariant &input, const Tomahawk::InfoSystem::InfoCustomData &customData ); + void sendLoveSong( QVariant input ); lastfm::MutableTrack m_track; lastfm::Audioscrobbler* m_scrobbler; diff --git a/src/libtomahawk/infosystem/infosystem.h b/src/libtomahawk/infosystem/infosystem.h index d96657d1f..12d6bce85 100644 --- a/src/libtomahawk/infosystem/infosystem.h +++ b/src/libtomahawk/infosystem/infosystem.h @@ -100,7 +100,9 @@ enum InfoType { // as items are saved in cache, mark them here to not change the InfoNowResumed = 50, InfoNowStopped = 51, - InfoNoInfo = 52 + InfoNoInfo = 52, + InfoLove = 53, + InfoUnLove = 54 }; typedef QMap< InfoType, QVariant > InfoMap;