/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. #include "PlaylistUpdaterInterface.h"
  19. #include "TomahawkSettings.h"
  20. #include "Source.h"
  21. namespace Tomahawk {
  22. bool
  23. operator==( const SerializedUpdater& one, const SerializedUpdater& two )
  24. {
  25. return one.type == two.type;
  26. }
  27. }
  28. using namespace Tomahawk;
  29. QMap< QString, PlaylistUpdaterFactory* > PlaylistUpdaterInterface::s_factories = QMap< QString, PlaylistUpdaterFactory* >();
  30. void
  31. PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f )
  32. {
  33. s_factories[ f->type() ] = f;
  34. }
  35. void
  36. PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl )
  37. {
  38. TomahawkSettings* s = TomahawkSettings::instance();
  39. const SerializedUpdaters allUpdaters = s->playlistUpdaters();
  40. if ( allUpdaters.contains( pl->guid() ) )
  41. {
  42. // Ok, we have some we can try to load
  43. const SerializedUpdaterList updaters = allUpdaters.values( pl->guid() );
  44. foreach ( const SerializedUpdater& info, updaters )
  45. {
  46. if ( !s_factories.contains( info.type ) )
  47. {
  48. // Q_ASSERT( false );
  49. // You forgot to register your new updater type with the factory....
  50. continue;
  51. }
  52. // Updaters register themselves in their constructor
  53. s_factories[ info.type ]->create( pl, info.customData );
  54. }
  55. }
  56. }
  57. PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl )
  58. : QObject( 0 )
  59. , m_playlist( pl )
  60. {
  61. Q_ASSERT( !m_playlist.isNull() );
  62. m_playlist->addUpdater( this );
  63. QTimer::singleShot( 0, this, SLOT( save() ) );
  64. }
  65. PlaylistUpdaterInterface::~PlaylistUpdaterInterface()
  66. {
  67. if ( !m_playlist.isNull() )
  68. m_playlist->removeUpdater( this );
  69. }
  70. void
  71. PlaylistUpdaterInterface::save()
  72. {
  73. if ( m_playlist.isNull() )
  74. return;
  75. TomahawkSettings* s = TomahawkSettings::instance();
  76. SerializedUpdaters allUpdaters = s->playlistUpdaters();
  77. if ( allUpdaters.contains( m_playlist->guid(), SerializedUpdater( type() ) ) )
  78. allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) );
  79. SerializedUpdater updater;
  80. updater.type = type();
  81. updater.customData = m_extraData;
  82. allUpdaters.insert( m_playlist->guid(), updater );
  83. s->setPlaylistUpdaters( allUpdaters );
  84. }
  85. void
  86. PlaylistUpdaterInterface::remove()
  87. {
  88. if ( m_playlist.isNull() )
  89. return;
  90. TomahawkSettings* s = TomahawkSettings::instance();
  91. SerializedUpdaters allUpdaters = s->playlistUpdaters();
  92. if ( allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) ) )
  93. s->setPlaylistUpdaters( allUpdaters );
  94. aboutToDelete();
  95. deleteLater();
  96. }
  97. QVariantHash
  98. PlaylistUpdaterInterface::settings() const
  99. {
  100. return m_extraData;
  101. }
  102. void
  103. PlaylistUpdaterInterface::saveSettings( const QVariantHash& settings )
  104. {
  105. m_extraData = settings;
  106. save();
  107. }