From 8b68e838276b81c13251f0cb9171da0fae3aaa16 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Fri, 7 Jun 2013 08:55:26 +0200
Subject: [PATCH] * Load artist-stats.

---
 src/libtomahawk/Artist.cpp                    | 52 ++++++++++++--
 src/libtomahawk/Artist.h                      |  9 ++-
 .../database/DatabaseCommand_ArtistStats.cpp  | 68 +++++++++++++++++++
 .../database/DatabaseCommand_ArtistStats.h    | 48 +++++++++++++
 4 files changed, 170 insertions(+), 7 deletions(-)
 create mode 100644 src/libtomahawk/database/DatabaseCommand_ArtistStats.cpp
 create mode 100644 src/libtomahawk/database/DatabaseCommand_ArtistStats.h

diff --git a/src/libtomahawk/Artist.cpp b/src/libtomahawk/Artist.cpp
index c2498ccf6..35b824f65 100644
--- a/src/libtomahawk/Artist.cpp
+++ b/src/libtomahawk/Artist.cpp
@@ -1,6 +1,6 @@
 /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  *
- *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
+ *   Copyright 2010-2013, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  *   Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  *   Copyright 2013,      Teo Mrnjavac <teo@kde.org>
  *
@@ -25,6 +25,7 @@
 #include "database/Database.h"
 #include "database/DatabaseImpl.h"
 #include "database/DatabaseCommand_AllAlbums.h"
+#include "database/DatabaseCommand_ArtistStats.h"
 #include "database/DatabaseCommand_TrackStats.h"
 #include "database/IdThreadWorker.h"
 #include "Source.h"
@@ -42,6 +43,7 @@ QHash< unsigned int, artist_wptr > Artist::s_artistsById = QHash< unsigned int,
 
 static QMutex s_nameCacheMutex;
 static QReadWriteLock s_idMutex;
+static QMutex s_memberMutex;
 
 
 Artist::~Artist()
@@ -132,6 +134,8 @@ Artist::Artist( unsigned int id, const QString& name )
     , m_simArtistsLoaded( false )
     , m_biographyLoaded( false )
     , m_infoJobs( 0 )
+    , m_chartPosition( 0 )
+    , m_chartCount( 0 )
 #ifndef ENABLE_HEADLESS
     , m_cover( 0 )
 #endif
@@ -185,6 +189,16 @@ Artist::deleteLater()
 }
 
 
+void
+Artist::onArtistStatsLoaded( unsigned int /* plays */, unsigned int chartPos, unsigned int chartCount )
+{
+    m_chartPosition = chartPos;
+    m_chartCount = chartCount;
+
+    emit statsLoaded();
+}
+
+
 void
 Artist::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection )
 {
@@ -380,8 +394,16 @@ Artist::loadStats()
 {
     artist_ptr a = m_ownRef.toStrongRef();
 
-    DatabaseCommand_TrackStats* cmd = new DatabaseCommand_TrackStats( a );
-    Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) );
+    {
+        DatabaseCommand_TrackStats* cmd = new DatabaseCommand_TrackStats( a );
+        Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) );
+    }
+
+    {
+        DatabaseCommand_ArtistStats* cmd = new DatabaseCommand_ArtistStats( a );
+        connect( cmd, SIGNAL( done( unsigned int, unsigned int, unsigned int ) ), SLOT( onArtistStatsLoaded( unsigned int, unsigned int, unsigned int ) ) );
+        Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) );
+    }
 }
 
 
@@ -405,14 +427,20 @@ Artist::playbackHistory( const Tomahawk::source_ptr& source ) const
 void
 Artist::setPlaybackHistory( const QList< Tomahawk::PlaybackLog >& playbackData )
 {
-    m_playbackHistory = playbackData;
+    {
+        QMutexLocker locker( &s_memberMutex );
+        m_playbackHistory = playbackData;
+    }
+
     emit statsLoaded();
 }
 
 
 unsigned int
-Artist::playbackCount( const source_ptr& source )
+Artist::playbackCount( const source_ptr& source ) const
 {
+    QMutexLocker locker( &s_memberMutex );
+
     unsigned int count = 0;
     foreach ( const PlaybackLog& log, m_playbackHistory )
     {
@@ -424,6 +452,20 @@ Artist::playbackCount( const source_ptr& source )
 }
 
 
+unsigned int
+Artist::chartPosition() const
+{
+    return m_chartPosition;
+}
+
+
+unsigned int
+Artist::chartCount() const
+{
+    return m_chartCount;
+}
+
+
 void
 Artist::onAlbumsFound( const QList< album_ptr >& albums, const QVariant& collectionIsNull )
 {
diff --git a/src/libtomahawk/Artist.h b/src/libtomahawk/Artist.h
index c9bd14fb7..825240c8f 100644
--- a/src/libtomahawk/Artist.h
+++ b/src/libtomahawk/Artist.h
@@ -61,7 +61,10 @@ public:
     void loadStats();
     QList< Tomahawk::PlaybackLog > playbackHistory( const Tomahawk::source_ptr& source = Tomahawk::source_ptr() ) const;
     void setPlaybackHistory( const QList< Tomahawk::PlaybackLog >& playbackData );
-    unsigned int playbackCount( const Tomahawk::source_ptr& source = Tomahawk::source_ptr() );
+    unsigned int playbackCount( const Tomahawk::source_ptr& source = Tomahawk::source_ptr() ) const;
+
+    unsigned int chartPosition() const;
+    unsigned int chartCount() const;
 
     QString biography() const;
 
@@ -91,6 +94,7 @@ signals:
     void statsLoaded();
 
 private slots:
+    void onArtistStatsLoaded( unsigned int plays, unsigned int chartPos, unsigned int chartCount );
     void onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection );
     void onAlbumsFound( const QList<Tomahawk::album_ptr>& albums, const QVariant& collectionIsNull = QVariant( false ) );
 
@@ -124,8 +128,9 @@ private:
     QList<Tomahawk::artist_ptr> m_similarArtists;
     QString m_biography;
 
-    bool m_playbackHistoryLoaded;
     QList< PlaybackLog > m_playbackHistory;
+    unsigned int m_chartPosition;
+    unsigned int m_chartCount;
 
     mutable QByteArray m_coverBuffer;
 #ifndef ENABLE_HEADLESS
diff --git a/src/libtomahawk/database/DatabaseCommand_ArtistStats.cpp b/src/libtomahawk/database/DatabaseCommand_ArtistStats.cpp
new file mode 100644
index 000000000..f7d2ebb11
--- /dev/null
+++ b/src/libtomahawk/database/DatabaseCommand_ArtistStats.cpp
@@ -0,0 +1,68 @@
+/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
+ *
+ *   Copyright 2013, Christian Muehlhaeuser <muesli@tomahawk-player.org>
+ *
+ *   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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "DatabaseCommand_ArtistStats.h"
+
+#include "Artist.h"
+#include "DatabaseImpl.h"
+#include "SourceList.h"
+#include "utils/Logger.h"
+
+using namespace Tomahawk;
+
+
+DatabaseCommand_ArtistStats::DatabaseCommand_ArtistStats( const artist_ptr& artist, QObject* parent )
+    : DatabaseCommand( parent )
+    , m_artist( artist )
+{
+}
+
+
+void
+DatabaseCommand_ArtistStats::exec( DatabaseImpl* dbi )
+{
+    TomahawkSqlQuery query = dbi->newquery();
+
+    query.prepare( "SELECT COUNT(*) AS counter, artist.id "
+                   "FROM playback_log, track, artist "
+                   "WHERE playback_log.source IS NULL AND track.id = playback_log.track AND artist.id = track.artist "
+                   "GROUP BY track.artist "
+                   "ORDER BY counter DESC" );
+    query.exec();
+
+    unsigned int plays = 0;
+    unsigned int chartPos = 0;
+    unsigned int chartCount = 0;
+
+    QHash< QString, unsigned int > charts;
+    while ( query.next() )
+    {
+        chartCount++;
+
+        if ( query.value( 1 ).toUInt() == m_artist->id() )
+        {
+            chartPos = chartCount;
+            plays = query.value( 0 ).toUInt();
+        }
+    }
+
+    if ( plays == 0 )
+        chartPos = chartCount;
+
+    emit done( plays, chartPos, chartCount );
+}
diff --git a/src/libtomahawk/database/DatabaseCommand_ArtistStats.h b/src/libtomahawk/database/DatabaseCommand_ArtistStats.h
new file mode 100644
index 000000000..eb509a4aa
--- /dev/null
+++ b/src/libtomahawk/database/DatabaseCommand_ArtistStats.h
@@ -0,0 +1,48 @@
+/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
+ *
+ *   Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
+ *
+ *   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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DATABASECOMMAND_ARTISTSTATS_H
+#define DATABASECOMMAND_ARTISTSTATS_H
+
+#include <QVariantMap>
+
+#include "DatabaseCommand.h"
+#include "Typedefs.h"
+#include "Track.h"
+#include "DllMacro.h"
+
+class DLLEXPORT DatabaseCommand_ArtistStats : public DatabaseCommand
+{
+Q_OBJECT
+
+public:
+    explicit DatabaseCommand_ArtistStats( const Tomahawk::artist_ptr& artist, QObject* parent = 0 );
+
+    virtual void exec( DatabaseImpl* lib );
+    virtual bool doesMutates() const { return false; }
+    virtual QString commandname() const { return "artiststats"; }
+
+signals:
+    void done( unsigned int totalPlays, unsigned int chartPosition, unsigned int chartCount );
+
+private:
+    Tomahawk::trackdata_ptr m_track;
+    Tomahawk::artist_ptr m_artist;
+};
+
+#endif // DATABASECOMMAND_ARTISTSTATS_H