/src/libtomahawk/database/DatabaseCommand_LoadDynamicPlaylistEntries.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 112 lines · 69 code · 16 blank · 27 comment · 6 complexity · 554d44770309841527aa3850506b2e0d 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 "DatabaseCommand_LoadDynamicPlaylistEntries.h"
  19. #include "playlist/dynamic/DynamicControl.h"
  20. #include "playlist/dynamic/GeneratorInterface.h"
  21. #include "playlist/dynamic/GeneratorFactory.h"
  22. #include "utils/Json.h"
  23. #include "utils/Logger.h"
  24. #include "DatabaseImpl.h"
  25. #include "PlaylistEntry.h"
  26. #include "Source.h"
  27. #include "TomahawkSqlQuery.h"
  28. #include <QSqlQuery>
  29. #include <QString>
  30. using namespace Tomahawk;
  31. void
  32. DatabaseCommand_LoadDynamicPlaylistEntries::exec( DatabaseImpl* dbi )
  33. {
  34. // qDebug() << "Loading dynamic playlist guid" << guid();
  35. // load the entries first
  36. generateEntries( dbi );
  37. // now load the controls etc
  38. TomahawkSqlQuery controlsQuery = dbi->newquery();
  39. controlsQuery.prepare( "SELECT playlist_revision.playlist, controls, plmode, pltype "
  40. "FROM dynamic_playlist_revision, playlist_revision "
  41. "WHERE dynamic_playlist_revision.guid = ? AND playlist_revision.guid = dynamic_playlist_revision.guid" );
  42. controlsQuery.addBindValue( revisionGuid() );
  43. controlsQuery.exec();
  44. QString type;
  45. GeneratorMode mode;
  46. QList< QVariantMap > controls;
  47. QString playlist_guid;
  48. // qDebug() << "Loading controls..." << revisionGuid();
  49. // qDebug() << "SELECT playlist_revision.playlist, controls, plmode, pltype "
  50. // "FROM dynamic_playlist_revision, playlist_revision "
  51. // "WHERE dynamic_playlist_revision.guid = "<< revisionGuid() << " AND playlist_revision.guid = dynamic_playlist_revision.guid";
  52. if ( controlsQuery.first() )
  53. {
  54. playlist_guid = controlsQuery.value( 0 ).toString();
  55. bool ok;
  56. QVariant v = TomahawkUtils::parseJson( controlsQuery.value(1).toByteArray(), &ok );
  57. Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
  58. type = controlsQuery.value( 3 ).toString();
  59. mode = static_cast<GeneratorMode>( controlsQuery.value( 2 ).toInt() );
  60. QStringList controlIds = v.toStringList();
  61. // qDebug() << "Got controls in dynamic playlist, loading:" << controlIds << controlsQuery.value(1);
  62. foreach ( const QString& controlId, controlIds )
  63. {
  64. TomahawkSqlQuery controlQuery = dbi->newquery();
  65. controlQuery.prepare( "SELECT selectedType, match, input "
  66. "FROM dynamic_playlist_controls "
  67. "WHERE id = :id" );
  68. controlQuery.bindValue( ":id", controlId );
  69. controlQuery.exec();
  70. if ( controlQuery.next() )
  71. {
  72. QVariantMap c;
  73. c[ "type" ] = type;
  74. c[ "id" ] = controlId;
  75. c[ "selectedType" ] = controlQuery.value( 0 ).toString();
  76. c[ "match" ] = controlQuery.value( 1 ).toString();
  77. c[ "input" ] = controlQuery.value( 2 ).toString();
  78. controls << c;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. // No controls or plguid is null, but that's okay. We'll get a setdynrevision command with a proper revision some point later
  85. return;
  86. }
  87. if ( mode == OnDemand )
  88. {
  89. // Q_ASSERT( m_entrymap.isEmpty() ); // ondemand should have no entry
  90. emit done( revisionGuid(), m_islatest, type, controls, true );
  91. }
  92. else
  93. {
  94. emit done( revisionGuid(), m_guids, m_oldentries, type, controls, m_islatest, m_entrymap, true );
  95. }
  96. }