diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index abba94f30..35d33e356 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -267,6 +267,7 @@ set( libSources database/DatabaseCommand_CollectionAttributes.cpp database/DatabaseCommand_TrackAttributes.cpp database/DatabaseCommand_SetTrackAttributes.cpp + database/DatabaseCommand_PlaybackCharts.cpp database/Database.cpp database/TomahawkSqlQuery.cpp database/IdThreadWorker.cpp diff --git a/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.cpp b/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.cpp new file mode 100644 index 000000000..016c4f497 --- /dev/null +++ b/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.cpp @@ -0,0 +1,76 @@ +/* === This file is part of Tomahawk Player - === + * + * 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 + * (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_PlaybackCharts.h" + +#include + +#include "Artist.h" +#include "DatabaseImpl.h" +#include "Source.h" +#include "utils/TomahawkUtils.h" +#include "utils/Logger.h" + + +DatabaseCommand_PlaybackCharts::DatabaseCommand_PlaybackCharts( const Tomahawk::source_ptr& source, QObject* parent ) + : DatabaseCommand( parent ) + , m_amount( 0 ) +{ + setSource( source ); +} + + +DatabaseCommand_PlaybackCharts::~DatabaseCommand_PlaybackCharts() +{ +} + + +void +DatabaseCommand_PlaybackCharts::exec( DatabaseImpl* dbi ) +{ + TomahawkSqlQuery query = dbi->newquery(); + QString sourceToken; + + if ( source() ) + sourceToken = QString( "AND playback_log.source %1" ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ); + + QString sql = QString( + "SELECT artist.id, artist.name, COUNT(*) AS counter " + "FROM playback_log, artist, track " + "WHERE playback_log.track = track.id " + "AND artist.id = track.artist " + "%1 " + "GROUP BY artist.id " + "ORDER BY counter DESC " + "%2" + ).arg( sourceToken ) + .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() ); + + query.prepare( sql ); + query.exec(); + + QList al; + while ( query.next() ) + { + Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 0 ).toUInt(), query.value( 1 ).toString() ); + al << artist; + } + + emit artists( al ); + emit done(); +} diff --git a/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.h b/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.h new file mode 100644 index 000000000..b9084f1ef --- /dev/null +++ b/src/libtomahawk/database/DatabaseCommand_PlaybackCharts.h @@ -0,0 +1,54 @@ +/* === This file is part of Tomahawk Player - === + * + * 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 + * (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_PLAYBACKCHARTS_H +#define DATABASECOMMAND_PLAYBACKCHARTS_H + +#include +#include + +#include "Artist.h" +#include "Collection.h" +#include "Typedefs.h" +#include "DatabaseCommand.h" + +#include "DllMacro.h" + +class DLLEXPORT DatabaseCommand_PlaybackCharts : public DatabaseCommand +{ +Q_OBJECT +public: + explicit DatabaseCommand_PlaybackCharts( const Tomahawk::source_ptr& source, QObject* parent = 0 ); + virtual ~DatabaseCommand_PlaybackCharts(); + + virtual void exec( DatabaseImpl* ); + + virtual bool doesMutates() const { return false; } + virtual QString commandname() const { return "playbackcharts"; } + + void setLimit( unsigned int amount ) { m_amount = amount; } + +signals: + void artists( const QList& ); + void done(); + +private: + unsigned int m_amount; +}; + +#endif // DATABASECOMMAND_PLAYBACKCHARTS_H