/src/libtomahawk/database/DatabaseCommand_CreateDynamicPlaylist.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 124 lines · 87 code · 20 blank · 17 comment · 11 complexity · fad174fae64f1c211ae34836b7db001d 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_CreateDynamicPlaylist.h"
  19. #include "playlist/dynamic/DynamicPlaylist.h"
  20. #include "playlist/dynamic/DynamicControl.h"
  21. #include "playlist/dynamic/GeneratorInterface.h"
  22. #include "network/Servent.h"
  23. #include "utils/Json.h"
  24. #include "utils/Logger.h"
  25. #include "DatabaseImpl.h"
  26. #include "PlaylistEntry.h"
  27. #include "SourceList.h"
  28. #include "TomahawkSqlQuery.h"
  29. #include <QSqlQuery>
  30. #include <QSqlDriver>
  31. using namespace Tomahawk;
  32. DatabaseCommand_CreateDynamicPlaylist::DatabaseCommand_CreateDynamicPlaylist( QObject* parent )
  33. : DatabaseCommand_CreatePlaylist( parent )
  34. , m_autoLoad( true )
  35. {
  36. tLog( LOGVERBOSE ) << Q_FUNC_INFO << "creating dynamiccreatecommand 1";
  37. }
  38. DatabaseCommand_CreateDynamicPlaylist::DatabaseCommand_CreateDynamicPlaylist( const source_ptr& author,
  39. const dynplaylist_ptr& playlist, bool autoLoad )
  40. : DatabaseCommand_CreatePlaylist( author, playlist.staticCast<Playlist>() )
  41. , m_playlist( playlist )
  42. , m_autoLoad( autoLoad )
  43. {
  44. tLog( LOGVERBOSE ) << Q_FUNC_INFO << "creating dynamiccreatecommand 2";
  45. }
  46. DatabaseCommand_CreateDynamicPlaylist::~DatabaseCommand_CreateDynamicPlaylist()
  47. {}
  48. QVariant
  49. DatabaseCommand_CreateDynamicPlaylist::playlistV() const
  50. {
  51. if( m_v.isNull() )
  52. return TomahawkUtils::qobject2qvariant( (QObject*)m_playlist.data() );
  53. else
  54. return m_v;
  55. }
  56. void
  57. DatabaseCommand_CreateDynamicPlaylist::exec( DatabaseImpl* lib )
  58. {
  59. Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
  60. Q_ASSERT( !source().isNull() );
  61. DatabaseCommand_CreatePlaylist::createPlaylist( lib, true );
  62. tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Created normal playlist, now creating additional dynamic info!";
  63. tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Create dynamic execing!" << m_playlist << m_v;
  64. TomahawkSqlQuery cre = lib->newquery();
  65. cre.prepare( "INSERT INTO dynamic_playlist( guid, pltype, plmode, autoload ) "
  66. "VALUES( ?, ?, ?, ? )" );
  67. if( m_playlist.isNull() ) {
  68. QVariantMap m = m_v.toMap();
  69. cre.addBindValue( m.value( "guid" ) );
  70. cre.addBindValue( m.value( "type" ) );
  71. cre.addBindValue( m.value( "mode" ) );
  72. } else {
  73. cre.addBindValue( m_playlist->guid() );
  74. cre.addBindValue( m_playlist->type() );
  75. cre.addBindValue( m_playlist->mode() );
  76. }
  77. cre.addBindValue( m_autoLoad ? "true" : "false" );
  78. cre.exec();
  79. }
  80. void
  81. DatabaseCommand_CreateDynamicPlaylist::postCommitHook()
  82. {
  83. if ( source().isNull() || source()->dbCollection().isNull() )
  84. {
  85. tDebug() << "Source has gone offline, not emitting to GUI.";
  86. return;
  87. }
  88. if( !DatabaseCommand_CreatePlaylist::report() || report() == false )
  89. return;
  90. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "..reporting..";
  91. if( m_playlist.isNull() ) {
  92. QMetaObject::invokeMethod( SourceList::instance(),
  93. "createDynamicPlaylist",
  94. Qt::BlockingQueuedConnection,
  95. QGenericArgument( "Tomahawk::source_ptr", (const void*)&source() ),
  96. Q_ARG( QVariant, m_v ) );
  97. }
  98. else
  99. {
  100. m_playlist->reportCreated( m_playlist );
  101. }
  102. if( source()->isLocal() )
  103. Servent::instance()->triggerDBSync();
  104. }