/src/libtomahawk/database/DatabaseCommand_CreatePlaylist.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 143 lines · 102 code · 24 blank · 17 comment · 8 complexity · 39aa49ca1a774f687b26aef9585d4d10 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. *
  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_CreatePlaylist.h"
  19. #include "network/Servent.h"
  20. #include "utils/Json.h"
  21. #include "utils/Logger.h"
  22. #include "DatabaseImpl.h"
  23. #include "Playlist.h"
  24. #include "SourceList.h"
  25. #include "TomahawkSqlQuery.h"
  26. #include <QDateTime>
  27. #include <QSqlQuery>
  28. using namespace Tomahawk;
  29. DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( QObject* parent )
  30. : DatabaseCommandLoggable( parent )
  31. , m_report( true )
  32. {
  33. }
  34. DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( const source_ptr& author,
  35. const playlist_ptr& playlist )
  36. : DatabaseCommandLoggable( author )
  37. , m_playlist( playlist )
  38. , m_report( false ) //this ctor used when creating locally, reporting done elsewhere
  39. {
  40. }
  41. DatabaseCommand_CreatePlaylist::~DatabaseCommand_CreatePlaylist()
  42. {}
  43. void
  44. DatabaseCommand_CreatePlaylist::exec( DatabaseImpl* lib )
  45. {
  46. createPlaylist( lib, false );
  47. }
  48. QVariant
  49. DatabaseCommand_CreatePlaylist::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_CreatePlaylist::postCommitHook()
  58. {
  59. qDebug() << Q_FUNC_INFO;
  60. if ( source()->isLocal() )
  61. Servent::instance()->triggerDBSync();
  62. if ( m_report == false )
  63. return;
  64. tDebug() << Q_FUNC_INFO << "reporting...";
  65. if ( m_playlist.isNull() )
  66. {
  67. QMetaObject::invokeMethod( SourceList::instance(),
  68. "createPlaylist",
  69. Qt::BlockingQueuedConnection,
  70. QGenericArgument( "Tomahawk::source_ptr", (const void*)&source() ),
  71. Q_ARG( QVariant, m_v ) );
  72. }
  73. else
  74. {
  75. m_playlist->reportCreated( m_playlist );
  76. }
  77. }
  78. void
  79. DatabaseCommand_CreatePlaylist::createPlaylist( DatabaseImpl* lib, bool dynamic)
  80. {
  81. Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
  82. Q_ASSERT( !source().isNull() );
  83. uint now = 0;
  84. if ( m_playlist.isNull() )
  85. {
  86. now = m_v.toMap()[ "createdon" ].toUInt();
  87. }
  88. else
  89. {
  90. now = QDateTime::currentDateTime().toTime_t();
  91. m_playlist->setCreatedOn( now );
  92. }
  93. TomahawkSqlQuery cre = lib->newquery();
  94. cre.prepare( "INSERT INTO playlist( guid, source, shared, title, info, creator, lastmodified, dynplaylist, createdOn ) "
  95. "VALUES( :guid, :source, :shared, :title, :info, :creator, :lastmodified, :dynplaylist, :createdOn )" );
  96. cre.bindValue( ":source", source()->isLocal() ? QVariant(QVariant::Int) : source()->id() );
  97. cre.bindValue( ":dynplaylist", dynamic ? "true" : "false" );
  98. cre.bindValue( ":createdOn", now );
  99. if ( !m_playlist.isNull() )
  100. {
  101. cre.bindValue( ":guid", m_playlist->guid() );
  102. cre.bindValue( ":shared", m_playlist->shared() ? "true" : "false" );
  103. cre.bindValue( ":title", m_playlist->title() );
  104. cre.bindValue( ":info", m_playlist->info() );
  105. cre.bindValue( ":creator", m_playlist->creator() );
  106. cre.bindValue( ":lastmodified", m_playlist->lastmodified() );
  107. }
  108. else
  109. {
  110. QVariantMap m = m_v.toMap();
  111. cre.bindValue( ":guid", m.value( "guid" ) );
  112. cre.bindValue( ":shared", m.value( "shared" ) );
  113. cre.bindValue( ":title", m.value( "title" ) );
  114. cre.bindValue( ":info", m.value( "info" ) );
  115. cre.bindValue( ":creator", m.value( "creator" ) );
  116. cre.bindValue( ":lastmodified", m.value( "lastmodified", 0 ) );
  117. }
  118. cre.exec();
  119. }