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