PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 48ms app.codeStats 0ms

/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 139 lines | 86 code | 32 blank | 21 comment | 8 complexity | c7a0f3c4ca709376fef85d603710278d MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "PlaylistUpdaterInterface.h"
 20#include "TomahawkSettings.h"
 21#include "Source.h"
 22
 23namespace Tomahawk {
 24
 25bool
 26operator==( const SerializedUpdater& one, const SerializedUpdater& two )
 27{
 28    return one.type == two.type;
 29}
 30
 31}
 32
 33using namespace Tomahawk;
 34
 35QMap< QString, PlaylistUpdaterFactory* > PlaylistUpdaterInterface::s_factories = QMap< QString, PlaylistUpdaterFactory* >();
 36
 37void
 38PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f )
 39{
 40    s_factories[ f->type() ] = f;
 41}
 42
 43
 44
 45void
 46PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl )
 47{
 48    TomahawkSettings* s = TomahawkSettings::instance();
 49
 50    const SerializedUpdaters allUpdaters = s->playlistUpdaters();
 51    if ( allUpdaters.contains( pl->guid() ) )
 52    {
 53        // Ok, we have some we can try to load
 54        const SerializedUpdaterList updaters = allUpdaters.values( pl->guid() );
 55        foreach ( const SerializedUpdater& info, updaters )
 56        {
 57            if ( !s_factories.contains( info.type ) )
 58            {
 59//                Q_ASSERT( false );
 60                // You forgot to register your new updater type with the factory....
 61                continue;
 62            }
 63
 64            // Updaters register themselves in their constructor
 65            s_factories[ info.type ]->create( pl, info.customData );
 66        }
 67    }
 68}
 69
 70
 71PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl )
 72    : QObject( 0 )
 73    , m_playlist( pl )
 74{
 75    Q_ASSERT( !m_playlist.isNull() );
 76
 77    m_playlist->addUpdater( this );
 78
 79    QTimer::singleShot( 0, this, SLOT( save() ) );
 80}
 81
 82
 83PlaylistUpdaterInterface::~PlaylistUpdaterInterface()
 84{
 85    if ( !m_playlist.isNull() )
 86        m_playlist->removeUpdater( this );
 87}
 88
 89
 90void
 91PlaylistUpdaterInterface::save()
 92{
 93    if ( m_playlist.isNull() )
 94        return;
 95
 96    TomahawkSettings* s = TomahawkSettings::instance();
 97
 98    SerializedUpdaters allUpdaters = s->playlistUpdaters();
 99    if ( allUpdaters.contains( m_playlist->guid(), SerializedUpdater( type() ) ) )
100        allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) );
101
102    SerializedUpdater updater;
103    updater.type = type();
104    updater.customData = m_extraData;
105    allUpdaters.insert( m_playlist->guid(), updater );
106    s->setPlaylistUpdaters( allUpdaters );
107}
108
109
110void
111PlaylistUpdaterInterface::remove()
112{
113    if ( m_playlist.isNull() )
114        return;
115
116    TomahawkSettings* s = TomahawkSettings::instance();
117    SerializedUpdaters allUpdaters = s->playlistUpdaters();
118
119    if ( allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) ) )
120        s->setPlaylistUpdaters( allUpdaters );
121
122    aboutToDelete();
123    deleteLater();
124}
125
126
127QVariantHash
128PlaylistUpdaterInterface::settings() const
129{
130    return m_extraData;
131}
132
133
134void
135PlaylistUpdaterInterface::saveSettings( const QVariantHash& settings )
136{
137    m_extraData = settings;
138    save();
139}