PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 20ms RepoModel.GetById 41ms app.codeStats 0ms

/src/libtomahawk/collection.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 257 lines | 161 code | 57 blank | 39 comment | 6 complexity | 2f827e0e6a713d1b6aacaccbcfe7a98b MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "Collection.h"
 21
 22#include "Source.h"
 23
 24#include "utils/Logger.h"
 25#include "playlist/PlaylistUpdaterInterface.h"
 26
 27#include <QMetaObject>
 28#include <QGenericArgument>
 29
 30using namespace Tomahawk;
 31
 32
 33Collection::Collection( const source_ptr& source, const QString& name, QObject* parent )
 34    : QObject( parent )
 35    , m_name( name )
 36    , m_lastmodified( 0 )
 37    , m_changed( false )
 38    , m_source( source )
 39{
 40    qDebug() << Q_FUNC_INFO << name << source->friendlyName();
 41
 42    connect( source.data(), SIGNAL( synced() ), SLOT( onSynced() ) );
 43}
 44
 45
 46Collection::~Collection()
 47{
 48    qDebug() << Q_FUNC_INFO;
 49}
 50
 51
 52QString
 53Collection::name() const
 54{
 55    return m_name;
 56}
 57
 58
 59const
 60source_ptr& Collection::source() const
 61{
 62    return m_source;
 63}
 64
 65
 66void
 67Collection::addPlaylist( const Tomahawk::playlist_ptr& p )
 68{
 69    if ( m_playlists.contains( p->guid() ) )
 70        return;
 71
 72    QList<playlist_ptr> toadd;
 73    toadd << p;
 74    m_playlists.insert( p->guid(), p );
 75
 76/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
 77                            << "from source id" << source()->id()
 78                            << "numplaylists:" << m_playlists.count();*/
 79    emit playlistsAdded( toadd );
 80}
 81
 82
 83void
 84Collection::addAutoPlaylist( const Tomahawk::dynplaylist_ptr& p )
 85{
 86    QList<dynplaylist_ptr> toadd;
 87    toadd << p;
 88    m_autoplaylists.insert( p->guid(), p );
 89
 90/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
 91                            << "from source id" << source()->id()
 92                            << "numplaylists:" << m_playlists.count();*/
 93    emit autoPlaylistsAdded( toadd );
 94}
 95
 96
 97void
 98Collection::addStation( const dynplaylist_ptr& s )
 99{
100    QList<dynplaylist_ptr> toadd;
101    toadd << s;
102    m_stations.insert( s->guid(), s );
103
104/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
105                            << "from source id" << source()->id()
106                            << "numplaylists:" << m_playlists.count();*/
107    emit stationsAdded( toadd );
108}
109
110
111void
112Collection::deletePlaylist( const Tomahawk::playlist_ptr& p )
113{
114    QList<playlist_ptr> todelete;
115    todelete << p;
116    m_playlists.remove( p->guid() );
117
118/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
119                            << "from source id" << source()->id()
120                            << "numplaylists:" << m_playlists.count();*/
121    emit playlistsDeleted( todelete );
122}
123
124
125void
126Collection::deleteAutoPlaylist( const Tomahawk::dynplaylist_ptr& p )
127{
128    QList<dynplaylist_ptr> todelete;
129    todelete << p;
130    m_autoplaylists.remove( p->guid() );
131
132/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
133                            << "from source id" << source()->id()
134                            << "numplaylists:" << m_playlists.count();*/
135    emit autoPlaylistsDeleted( todelete );
136}
137
138
139void
140Collection::deleteStation( const dynplaylist_ptr& s )
141{
142    QList<dynplaylist_ptr> todelete;
143    todelete << s;
144    m_stations.remove( s->guid() );
145
146/*    qDebug() << Q_FUNC_INFO << "Collection name" << name()
147                            << "from source id" << source()->id()
148                            << "numplaylists:" << m_playlists.count();*/
149    emit stationsDeleted( todelete );
150}
151
152
153Tomahawk::playlist_ptr
154Collection::playlist( const QString& guid )
155{
156    return m_playlists.value( guid, Tomahawk::playlist_ptr() );
157}
158
159
160Tomahawk::dynplaylist_ptr
161Collection::autoPlaylist( const QString& guid )
162{
163    return m_autoplaylists.value( guid, Tomahawk::dynplaylist_ptr() );
164}
165
166
167Tomahawk::dynplaylist_ptr
168Collection::station( const QString& guid )
169{
170    return m_stations.value( guid, Tomahawk::dynplaylist_ptr() );
171}
172
173
174void
175Collection::setPlaylists( const QList<Tomahawk::playlist_ptr>& plists )
176{
177    foreach ( const playlist_ptr& p, plists )
178    {
179//        qDebug() << "Batch inserting playlist:" << p->guid();
180        m_playlists.insert( p->guid(), p );
181        if ( !m_source.isNull() && m_source->isLocal() )
182            PlaylistUpdaterInterface::loadForPlaylist( p );
183    }
184    emit playlistsAdded( plists );
185}
186
187
188void
189Collection::setAutoPlaylists( const QList< Tomahawk::dynplaylist_ptr >& plists )
190{
191    foreach ( const dynplaylist_ptr& p, plists )
192    {
193//        qDebug() << "Batch inserting dynamic playlist:" << p->guid();
194        m_autoplaylists.insert( p->guid(), p );
195    }
196    emit autoPlaylistsAdded( plists );
197}
198
199
200void
201Collection::setStations( const QList< dynplaylist_ptr >& stations )
202{
203    foreach ( const dynplaylist_ptr& s, stations )
204    {
205//        qDebug() << "Batch inserting station:" << s->guid();
206        m_stations.insert( s->guid(), s );
207    }
208    emit autoPlaylistsAdded( stations );
209}
210
211
212void
213Collection::setTracks( const QList<unsigned int>& ids )
214{
215    tDebug() << Q_FUNC_INFO << ids.count() << name();
216
217    m_changed = true;
218    emit tracksAdded( ids );
219}
220
221
222void
223Collection::delTracks( const QList<unsigned int>& ids )
224{
225    tDebug() << Q_FUNC_INFO << ids.count() << name();
226
227    m_changed = true;
228    emit tracksRemoved( ids );
229}
230
231
232void
233Collection::onSynced()
234{
235    tDebug() << Q_FUNC_INFO << m_changed;
236    if ( m_changed )
237    {
238        m_changed = false;
239        emit changed();
240    }
241}
242
243
244void
245Collection::moveAutoToStation( const QString& guid )
246{
247    if ( m_autoplaylists.contains( guid ) )
248        m_stations.insert( guid, m_autoplaylists.take( guid ) );
249}
250
251
252void
253Collection::moveStationToAuto( const QString& guid )
254{
255    if ( m_stations.contains( guid ) )
256        m_autoplaylists.insert( guid, m_stations.take( guid ) );
257}