PageRenderTime 51ms CodeModel.GetById 30ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/src/libtomahawk/playlist/XspfUpdater.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 178 lines | 118 code | 41 blank | 19 comment | 9 complexity | eb0dcec8bedaa3e1355a936cde6b187c 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 "XspfUpdater.h"
 20
 21#include "utils/XspfLoader.h"
 22#include "utils/TomahawkUtils.h"
 23#include "utils/Logger.h"
 24
 25#include "Pipeline.h"
 26#include "Playlist.h"
 27#include "PlaylistEntry.h"
 28#include "Source.h"
 29
 30#include <QCheckBox>
 31#include <QTimer>
 32
 33
 34using namespace Tomahawk;
 35
 36PlaylistUpdaterInterface*
 37XspfUpdaterFactory::create( const playlist_ptr &pl, const QVariantHash& settings )
 38{
 39    const bool autoUpdate = settings.value( "autoupdate" ).toBool();
 40    const int interval = settings.value( "interval" ).toInt();
 41    const QString url = settings.value( "xspfurl" ).toString();
 42
 43    XspfUpdater* updater = new XspfUpdater( pl, interval, autoUpdate, url );
 44
 45    return updater;
 46}
 47
 48
 49XspfUpdater::XspfUpdater( const playlist_ptr& pl, int interval, bool autoUpdate, const QString& xspfUrl )
 50    : PlaylistUpdaterInterface( pl )
 51    , m_timer( new QTimer( this ) )
 52    , m_autoUpdate( autoUpdate )
 53    , m_url( xspfUrl )
 54{
 55    m_timer->setInterval( interval );
 56
 57    connect( m_timer, SIGNAL( timeout() ), this, SLOT( updateNow() ) );
 58
 59    m_toggleCheckbox = new QCheckBox( );
 60    m_toggleCheckbox->setText( tr( "Automatically update from XSPF" ) );
 61    m_toggleCheckbox->setLayoutDirection( Qt::RightToLeft );
 62    m_toggleCheckbox->setChecked( m_autoUpdate );
 63    m_toggleCheckbox->hide();
 64
 65    connect( m_toggleCheckbox, SIGNAL( toggled( bool ) ), this, SLOT( setAutoUpdate( bool ) ) );
 66
 67    QVariantHash s = settings();
 68    s[ "autoupdate" ] = m_autoUpdate;
 69    s[ "interval" ] = interval;
 70    s[ "xspfurl" ] = xspfUrl;
 71    saveSettings( s );
 72
 73    // Force start
 74    setAutoUpdate( m_autoUpdate );
 75}
 76
 77
 78XspfUpdater::~XspfUpdater()
 79{
 80}
 81
 82
 83QWidget*
 84XspfUpdater::configurationWidget() const
 85{
 86    return m_toggleCheckbox;
 87}
 88
 89
 90void
 91XspfUpdater::updateNow()
 92{
 93    if ( m_url.isEmpty() )
 94    {
 95        qWarning() << "XspfUpdater not updating because we have an empty url...";
 96        return;
 97    }
 98    if ( !playlist()->loaded() )
 99    {
100        tDebug() << "XspfUpdater not updating because playlist wasn't loaded yet...";
101        return;
102    }
103
104    XSPFLoader* l = new XSPFLoader( false, false );
105    l->setAutoResolveTracks( false );
106    l->setErrorTitle( playlist()->title() );
107    l->load( m_url );
108    connect( l, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( playlistLoaded( QList<Tomahawk::query_ptr> ) ) );
109}
110
111
112void
113XspfUpdater::playlistLoaded( const QList<Tomahawk::query_ptr>& newEntries )
114{
115    XSPFLoader* loader = qobject_cast< XSPFLoader* >( sender() );
116    if ( loader )
117    {
118        const QString newTitle = loader->title();
119        if ( newTitle != playlist()->title() )
120            playlist()->rename( newTitle );
121    }
122
123    QList< query_ptr > tracks;
124    foreach ( const plentry_ptr ple, playlist()->entries() )
125        tracks << ple->query();
126
127    bool changed = false;
128    QList< query_ptr > mergedTracks = TomahawkUtils::mergePlaylistChanges( tracks, newEntries, changed );
129
130    if ( !changed )
131        return;
132
133    QList<Tomahawk::plentry_ptr> el = playlist()->entriesFromQueries( mergedTracks, true );
134    playlist()->createNewRevision( uuid(), playlist()->currentrevision(), el );
135}
136
137
138void
139XspfUpdater::setAutoUpdate( bool autoUpdate )
140{
141    m_autoUpdate = autoUpdate;
142
143    if ( m_autoUpdate )
144        m_timer->start();
145    else
146        m_timer->stop();
147
148    QVariantHash s = settings();
149    s[ "autoupdate" ] = m_autoUpdate;
150    saveSettings( s );
151
152    // Update immediately as well
153    if ( m_autoUpdate )
154        QTimer::singleShot( 0, this, SLOT( updateNow() ) );
155
156    emit changed();
157}
158
159
160void
161XspfUpdater::setInterval( int intervalMsecs )
162{
163    QVariantHash s = settings();
164    s[ "interval" ] = intervalMsecs;
165    saveSettings( s );
166
167    if ( !m_timer )
168        m_timer = new QTimer( this );
169
170    m_timer->setInterval( intervalMsecs );
171}
172
173
174void
175XspfUpdater::setSubscribed( bool subscribed )
176{
177    setAutoUpdate( subscribed );
178}