1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-20 04:41:36 +02:00

* Added LovedTracksModel.

This commit is contained in:
Christian Muehlhaeuser
2012-07-14 11:13:22 +02:00
parent 077e9ab84a
commit 78aa6606f9
2 changed files with 191 additions and 0 deletions

View File

@@ -0,0 +1,133 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-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/>.
*/
#include "LovedTracksModel.h"
#include <QMimeData>
#include <QTreeView>
#include "Source.h"
#include "SourceList.h"
#include "database/Database.h"
#include "database/DatabaseCommand_GenericSelect.h"
#include "PlayableItem.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#define LOVED_TRACK_ITEMS 25
using namespace Tomahawk;
LovedTracksModel::LovedTracksModel( QObject* parent )
: PlaylistModel( parent )
, m_limit( LOVED_TRACK_ITEMS )
{
}
LovedTracksModel::~LovedTracksModel()
{
}
void
LovedTracksModel::loadTracks()
{
if ( rowCount( QModelIndex() ) )
{
clear();
}
startLoading();
QString sql;
if ( m_source.isNull() )
{
sql = QString( "SELECT track.name, artist.name, source, COUNT(*) as counter "
"FROM social_attributes, track, artist "
"WHERE social_attributes.id = track.id AND artist.id = track.artist AND social_attributes.k = 'Love' AND social_attributes.v = 'true' "
"GROUP BY track.id "
"ORDER BY counter DESC, social_attributes.timestamp DESC LIMIT 0, 50" );
}
else
{
sql = QString( "SELECT track.name, artist.name, COUNT(*) as counter "
"FROM social_attributes, track, artist "
"WHERE social_attributes.id = track.id AND artist.id = track.artist AND social_attributes.k = 'Love' AND social_attributes.v = 'true' AND social_attributes.source %1 "
"GROUP BY track.id "
"ORDER BY counter DESC, social_attributes.timestamp DESC " ).arg( m_source->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_source->id() ) );
}
DatabaseCommand_GenericSelect* cmd = new DatabaseCommand_GenericSelect( sql, DatabaseCommand_GenericSelect::Track, -1, 0 );
connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( appendQueries( QList<Tomahawk::query_ptr> ) ) );
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
}
void
LovedTracksModel::onSourcesReady()
{
Q_ASSERT( m_source.isNull() );
loadTracks();
foreach ( const source_ptr& source, SourceList::instance()->sources() )
onSourceAdded( source );
}
void
LovedTracksModel::setSource( const Tomahawk::source_ptr& source )
{
m_source = source;
if ( source.isNull() )
{
if ( SourceList::instance()->isReady() )
onSourcesReady();
else
connect( SourceList::instance(), SIGNAL( ready() ), SLOT( onSourcesReady() ) );
connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) );
}
else
{
onSourceAdded( source );
loadTracks();
}
}
void
LovedTracksModel::onSourceAdded( const Tomahawk::source_ptr& source )
{
connect( source.data(), SIGNAL( socialAttributesChanged( QString ) ), SLOT( onTrackLoved() ), Qt::UniqueConnection );
}
void
LovedTracksModel::onTrackLoved()
{
loadTracks();
}
bool
LovedTracksModel::isTemporary() const
{
return true;
}

View File

@@ -0,0 +1,58 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, 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 LOVEDTRACKSMODEL_H
#define LOVEDTRACKSMODEL_H
#include <QList>
#include <QHash>
#include "Typedefs.h"
#include "PlaylistModel.h"
#include "DllMacro.h"
class DLLEXPORT LovedTracksModel : public PlaylistModel
{
Q_OBJECT
public:
explicit LovedTracksModel( QObject* parent = 0 );
~LovedTracksModel();
unsigned int limit() const { return m_limit; }
void setLimit( unsigned int limit ) { m_limit = limit; }
bool isTemporary() const;
public slots:
void setSource( const Tomahawk::source_ptr& source );
private slots:
void onSourcesReady();
void onSourceAdded( const Tomahawk::source_ptr& source );
void onTrackLoved();
void loadTracks();
private:
Tomahawk::source_ptr m_source;
unsigned int m_limit;
};
#endif // LOVEDTRACKSMODEL_H