/src/libtomahawk/database/DatabaseCommand_LoadAllPlaylists.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 147 lines · 101 code · 28 blank · 18 comment · 6 complexity · fd5c6bc2ee8cf651d0224bc27e41aa31 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2011, Leo Franchi <lfranchi@kde.org>
  5. *
  6. * Tomahawk is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Tomahawk is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "DatabaseCommand_LoadAllPlaylists_p.h"
  20. #include "utils/Json.h"
  21. #include "DatabaseImpl.h"
  22. #include "Playlist.h"
  23. #include "PlaylistEntry.h"
  24. #include "Source.h"
  25. #include <QSqlQuery>
  26. using namespace Tomahawk;
  27. DatabaseCommand_LoadAllPlaylists::DatabaseCommand_LoadAllPlaylists( const source_ptr& s, QObject* parent )
  28. : DatabaseCommand( parent, new DatabaseCommand_LoadAllPlaylistsPrivate( this, s ) )
  29. {
  30. qRegisterMetaType< QHash< Tomahawk::playlist_ptr, QStringList > >("QHash< Tomahawk::playlist_ptr, QStringList >");
  31. }
  32. void
  33. DatabaseCommand_LoadAllPlaylists::exec( DatabaseImpl* dbi )
  34. {
  35. Q_D( DatabaseCommand_LoadAllPlaylists );
  36. TomahawkSqlQuery query = dbi->newquery();
  37. QString orderToken, sourceToken;
  38. switch ( d->sortOrder )
  39. {
  40. case 0:
  41. break;
  42. case ModificationTime:
  43. orderToken = "ORDER BY playlist.createdOn";
  44. }
  45. if ( !source().isNull() )
  46. sourceToken = QString( "AND source %1 " ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
  47. QString trackIdJoin;
  48. QString trackIdFields;
  49. if ( d->returnPlEntryIds )
  50. {
  51. trackIdFields = ", pr.entries";
  52. trackIdJoin = "JOIN playlist_revision pr ON pr.playlist = p.guid AND pr.guid = p.currentrevision";
  53. }
  54. query.exec( QString( " SELECT p.guid, p.title, p.info, p.creator, p.lastmodified, p.shared, p.currentrevision, p.createdOn %6 "
  55. " FROM playlist p "
  56. " %5 "
  57. " WHERE ( ( dynplaylist = 'false' ) OR ( dynplaylist = 0 ) ) "
  58. " %1 "
  59. " %2 %3 %4 "
  60. )
  61. .arg( sourceToken )
  62. .arg( orderToken )
  63. .arg( d->sortDescending ? "DESC" : QString() )
  64. .arg( d->limitAmount > 0 ? QString( "LIMIT 0, %1" ).arg( d->limitAmount ) : QString() )
  65. .arg( trackIdJoin )
  66. .arg( trackIdFields )
  67. );
  68. QList<playlist_ptr> plists;
  69. QHash<playlist_ptr, QStringList> phash;
  70. while ( query.next() )
  71. {
  72. playlist_ptr p( new Playlist( source(), //src
  73. query.value(6).toString(), //current rev
  74. query.value(1).toString(), //title
  75. query.value(2).toString(), //info
  76. query.value(3).toString(), //creator
  77. query.value(7).toInt(), //lastmod / createdOn
  78. query.value(5).toBool(), //shared
  79. query.value(4).toInt(), //lastmod
  80. query.value(0).toString() //GUID
  81. ), &QObject::deleteLater );
  82. p->setWeakSelf( p.toWeakRef() );
  83. plists.append( p );
  84. if ( d->returnPlEntryIds )
  85. {
  86. QStringList trackIds = TomahawkUtils::parseJson( query.value( 8 ).toByteArray() ).toStringList();
  87. phash.insert( p, trackIds );
  88. }
  89. }
  90. emit done( plists );
  91. if ( d->returnPlEntryIds )
  92. {
  93. emit done( phash );
  94. }
  95. }
  96. void
  97. DatabaseCommand_LoadAllPlaylists::setLimit( unsigned int limit )
  98. {
  99. Q_D( DatabaseCommand_LoadAllPlaylists );
  100. d->limitAmount = limit;
  101. }
  102. void
  103. DatabaseCommand_LoadAllPlaylists::setSortOrder( DatabaseCommand_LoadAllPlaylists::SortOrder order )
  104. {
  105. Q_D( DatabaseCommand_LoadAllPlaylists );
  106. d->sortOrder = order;
  107. }
  108. void
  109. DatabaseCommand_LoadAllPlaylists::setSortDescending( bool descending )
  110. {
  111. Q_D( DatabaseCommand_LoadAllPlaylists );
  112. d->sortDescending = descending;
  113. }
  114. void
  115. DatabaseCommand_LoadAllPlaylists::setReturnPlEntryIds( bool returnPlEntryIds )
  116. {
  117. Q_D( DatabaseCommand_LoadAllPlaylists );
  118. d->returnPlEntryIds = returnPlEntryIds;
  119. }