diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 4f97ae05c..8ec12c389 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -280,6 +280,7 @@ list(APPEND libSources database/DatabaseCommand_ShareTrack.cpp database/DatabaseCommand_DeleteInboxEntry.cpp database/DatabaseCommand_ModifyInboxEntry.cpp + database/DatabaseCommand_NetworkCharts.cpp database/Database.cpp database/TomahawkSqlQuery.cpp database/IdThreadWorker.cpp diff --git a/src/libtomahawk/database/DatabaseCommand_NetworkCharts.cpp b/src/libtomahawk/database/DatabaseCommand_NetworkCharts.cpp new file mode 100644 index 000000000..e7a17ee30 --- /dev/null +++ b/src/libtomahawk/database/DatabaseCommand_NetworkCharts.cpp @@ -0,0 +1,74 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * 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_NetworkCharts.h" + +#include "Track.h" +#include "DatabaseImpl.h" +#include "TomahawkSqlQuery.h" + +DatabaseCommand_NetworkCharts::DatabaseCommand_NetworkCharts( const QDateTime &from, const QDateTime &to, QObject *parent ) + : DatabaseCommand( parent ) + , m_amount( 0 ) + , m_from( from ) + , m_to( to ) +{ +} + +DatabaseCommand_NetworkCharts::~DatabaseCommand_NetworkCharts() +{ +} + +void +DatabaseCommand_NetworkCharts::exec( DatabaseImpl * dbi ) +{ + TomahawkSqlQuery query = dbi->newquery(); + + QString limit; + if ( m_amount > 0 ) + { + limit = QString( "LIMIT 0, %1" ).arg( m_amount ); + } + + QString sql = QString( + "SELECT COUNT(*) as counter, track.name, artist.name " + " FROM playback_log, track, artist " + " WHERE track.id = playback_log.track AND artist.id = track.artist " + " AND playback_log.playtime >= %1 AND playback_log.playtime <= %2 " // incorportrate timespan + " AND playback_log.source IS NOT NULL " // exclude self + " GROUP BY playback_log.track " + " ORDER BY counter DESC " + " %3" + ).arg( m_from.toTime_t() ).arg( m_to.toTime_t() ).arg( limit ); + + query.prepare( sql ); + query.exec(); + + QList tracks; + while ( query.next() ) + { + Tomahawk::track_ptr track = Tomahawk::Track::get( query.value( 2 ).toString(), query.value( 1 ).toString() ); + if ( !track ) + continue; + + tracks << track; + } + + emit done( tracks ); +} + diff --git a/src/libtomahawk/database/DatabaseCommand_NetworkCharts.h b/src/libtomahawk/database/DatabaseCommand_NetworkCharts.h new file mode 100644 index 000000000..a9b7ec93d --- /dev/null +++ b/src/libtomahawk/database/DatabaseCommand_NetworkCharts.h @@ -0,0 +1,53 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * 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_NETWORKCHARTS_H +#define DATABASECOMMAND_NETWORKCHARTS_H + +#include +#include + +#include "Typedefs.h" +#include "DatabaseCommand.h" + +#include "DllMacro.h" + +class DLLEXPORT DatabaseCommand_NetworkCharts : public DatabaseCommand +{ +Q_OBJECT +public: + explicit DatabaseCommand_NetworkCharts( const QDateTime& from, const QDateTime& to, QObject* parent = 0 ); + virtual ~DatabaseCommand_NetworkCharts(); + + virtual void exec( DatabaseImpl* ); + + virtual bool doesMutates() const { return false; } + virtual QString commandname() const { return "networkcharts"; } + + void setLimit( unsigned int amount ) { m_amount = amount; } + +signals: + void done( const QList& tracks ); + +private: + unsigned int m_amount; + QDateTime m_from; + QDateTime m_to; +}; + +#endif // DATABASECOMMAND_NETWORKCHARTS_H