/src/libtomahawk/database/DatabaseCommand_LoadPlaylistEntries.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 142 lines · 97 code · 25 blank · 20 comment · 12 complexity · bd4ca5ac4f6ee021f6bf9adc8b05e876 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2015, Christian Muehlhaeuser <muesli@tomahawk-player.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_LoadPlaylistEntries.h"
  19. #include "utils/Json.h"
  20. #include "utils/Logger.h"
  21. #include "DatabaseImpl.h"
  22. #include "PlaylistEntry.h"
  23. #include "Query.h"
  24. #include "Source.h"
  25. #include <QSqlQuery>
  26. using namespace Tomahawk;
  27. void
  28. DatabaseCommand_LoadPlaylistEntries::exec( DatabaseImpl* dbi )
  29. {
  30. generateEntries( dbi );
  31. emit done( m_revguid, m_guids, m_oldentries, m_islatest, m_entrymap, true );
  32. }
  33. void
  34. DatabaseCommand_LoadPlaylistEntries::generateEntries( DatabaseImpl* dbi )
  35. {
  36. TomahawkSqlQuery query_entries = dbi->newquery();
  37. query_entries.prepare( "SELECT entries, playlist, author, timestamp, previous_revision "
  38. "FROM playlist_revision "
  39. "WHERE guid = :guid" );
  40. query_entries.bindValue( ":guid", m_revguid );
  41. query_entries.exec();
  42. tLog( LOGVERBOSE ) << "trying to load playlist entries for guid:" << m_revguid;
  43. QString prevrev;
  44. bool ok;
  45. if ( query_entries.next() )
  46. {
  47. if ( !query_entries.value( 0 ).isNull() )
  48. {
  49. // entries should be a list of strings:
  50. QVariant v = TomahawkUtils::parseJson( query_entries.value( 0 ).toByteArray(), &ok );
  51. Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
  52. m_guids = v.toStringList();
  53. QString inclause = QString( "('%1')" ).arg( m_guids.join( "', '" ) );
  54. TomahawkSqlQuery query = dbi->newquery();
  55. QString sql = QString( "SELECT guid, trackname, artistname, albumname, annotation, "
  56. "duration, addedon, addedby, result_hint "
  57. "FROM playlist_item "
  58. "WHERE guid IN %1" ).arg( inclause );
  59. query.exec( sql );
  60. while ( query.next() )
  61. {
  62. plentry_ptr e( new PlaylistEntry );
  63. e->setGuid( query.value( 0 ).toString() );
  64. e->setAnnotation( query.value( 4 ).toString() );
  65. e->setDuration( query.value( 5 ).toUInt() );
  66. e->setLastmodified( 0 ); // TODO e->lastmodified = query.value( 6 ).toInt();
  67. const QString resultHint = query.value( 8 ).toString();
  68. e->setResultHint( resultHint );
  69. Tomahawk::query_ptr q = Tomahawk::Query::get( query.value( 2 ).toString(), query.value( 1 ).toString(), query.value( 3 ).toString() );
  70. if ( q.isNull() )
  71. continue;
  72. q->setResultHint( resultHint );
  73. if ( resultHint.startsWith( "http" ) )
  74. q->setSaveHTTPResultHint( true );
  75. q->setProperty( "annotation", e->annotation() );
  76. e->setQuery( q );
  77. m_entrymap.insert( e->guid(), e );
  78. }
  79. }
  80. prevrev = query_entries.value( 4 ).toString();
  81. }
  82. else
  83. {
  84. // qDebug() << "Playlist has no current revision data";
  85. }
  86. if ( !prevrev.isEmpty() )
  87. {
  88. TomahawkSqlQuery query_entries_old = dbi->newquery();
  89. query_entries_old.prepare( "SELECT entries, "
  90. "(SELECT currentrevision = ? FROM playlist WHERE guid = ?) "
  91. "FROM playlist_revision "
  92. "WHERE guid = ?" );
  93. query_entries_old.addBindValue( m_revguid );
  94. query_entries_old.addBindValue( query_entries.value( 1 ).toString() );
  95. query_entries_old.addBindValue( prevrev );
  96. query_entries_old.exec();
  97. if ( !query_entries_old.next() )
  98. {
  99. return;
  100. Q_ASSERT( false );
  101. }
  102. if ( !query_entries_old.value( 0 ).isNull() )
  103. {
  104. QVariant v = TomahawkUtils::parseJson( query_entries_old.value( 0 ).toByteArray(), &ok );
  105. Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
  106. m_oldentries = v.toStringList();
  107. }
  108. m_islatest = query_entries_old.value( 1 ).toBool();
  109. }
  110. // qDebug() << Q_FUNC_INFO << "entrymap:" << m_entrymap;
  111. }
  112. DatabaseCommand_LoadPlaylistEntries::DatabaseCommand_LoadPlaylistEntries( QString revision_guid, QObject *parent )
  113. : DatabaseCommand( parent )
  114. , m_islatest( true )
  115. , m_revguid( revision_guid )
  116. {
  117. }