/src/libtomahawk/database/DatabaseCommand_SetCollectionAttributes.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 88 lines · 51 code · 15 blank · 22 comment · 10 complexity · e81a4d1d3cd0369e3d52483bb1d58b5d 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_SetCollectionAttributes.h"
  19. #include "DatabaseImpl.h"
  20. #include "Source.h"
  21. #include "network/Servent.h"
  22. #include "SourceList.h"
  23. namespace Tomahawk
  24. {
  25. DatabaseCommand_SetCollectionAttributes::DatabaseCommand_SetCollectionAttributes( AttributeType type, const QByteArray& id )
  26. : DatabaseCommandLoggable( )
  27. , m_delete( false )
  28. , m_type( type )
  29. , m_id( id )
  30. {
  31. }
  32. DatabaseCommand_SetCollectionAttributes::DatabaseCommand_SetCollectionAttributes( DatabaseCommand_SetCollectionAttributes::AttributeType type, bool toDelete )
  33. : DatabaseCommandLoggable()
  34. , m_delete( toDelete )
  35. , m_type( type )
  36. {
  37. }
  38. void
  39. DatabaseCommand_SetCollectionAttributes::exec( DatabaseImpl *lib )
  40. {
  41. TomahawkSqlQuery query = lib->newquery();
  42. QString sourceStr;
  43. if ( source().isNull() )
  44. setSource( SourceList::instance()->getLocal() );
  45. if ( source().isNull() || source()->isLocal() )
  46. sourceStr = "NULL";
  47. else
  48. sourceStr = QString( "%1" ).arg( source()->id() );
  49. QString typeStr;
  50. if ( m_type == EchonestSongCatalog )
  51. typeStr = "echonest_song";
  52. else if ( m_type == EchonestArtistCatalog )
  53. typeStr = "echonest_artist";
  54. TomahawkSqlQuery delQuery = lib->newquery();
  55. delQuery.exec( QString( "DELETE FROM collection_attributes WHERE id %1" ).arg( source()->isLocal() ? QString("IS NULL") : QString( "= %1" ).arg( source()->id() )));
  56. if ( m_delete )
  57. return;
  58. QString queryStr = QString( "INSERT INTO collection_attributes ( id, k, v ) VALUES( %1, \"%2\", \"%3\" )" ).arg( sourceStr ).arg( typeStr ).arg( QString::fromUtf8( m_id ) );
  59. qDebug() << "Doing query:" << queryStr;
  60. query.exec( queryStr );
  61. }
  62. void
  63. DatabaseCommand_SetCollectionAttributes::postCommitHook()
  64. {
  65. /*
  66. if ( m_type == EchonestSongCatalog ||
  67. m_type == EchonestArtistCatalog )
  68. Tomahawk::EchonestCatalogSynchronizer::instance()->knownCatalogsChanged();
  69. */
  70. if ( source()->isLocal() )
  71. Servent::instance()->triggerDBSync();
  72. }
  73. }