/src/libtomahawk/database/DatabaseCommand_DeleteFiles.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 142 lines · 98 code · 23 blank · 21 comment · 17 complexity · 4ce46363c9355fda57fbd718ba9a9bc2 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. * Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.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_DeleteFiles.h"
  20. #include <QtSql/QSqlQuery>
  21. #include "collection/Collection.h"
  22. #include "database/Database.h"
  23. #include "database/DatabaseImpl.h"
  24. #include "network/Servent.h"
  25. #include "utils/Logger.h"
  26. #include "utils/TomahawkUtils.h"
  27. #include "Artist.h"
  28. #include "Album.h"
  29. #include "PlaylistEntry.h"
  30. #include "Source.h"
  31. using namespace Tomahawk;
  32. // After changing a collection, we need to tell other bits of the system:
  33. void
  34. DatabaseCommand_DeleteFiles::postCommitHook()
  35. {
  36. if ( m_idList.isEmpty() )
  37. return;
  38. // make the collection object emit its tracksAdded signal, so the
  39. // collection browser will update/fade in etc.
  40. Collection* coll = source()->dbCollection().data();
  41. connect( this, SIGNAL( notify( QList<unsigned int> ) ),
  42. coll, SLOT( delTracks( QList<unsigned int> ) ), Qt::QueuedConnection );
  43. tDebug() << "Notifying of deleted tracks:" << m_idList.size() << "from source" << source()->id();
  44. emit notify( m_idList );
  45. if ( source()->isLocal() )
  46. Servent::instance()->triggerDBSync();
  47. }
  48. void
  49. DatabaseCommand_DeleteFiles::exec( DatabaseImpl* dbi )
  50. {
  51. Q_ASSERT( !source().isNull() );
  52. int srcid = source()->isLocal() ? 0 : source()->id();
  53. TomahawkSqlQuery delquery = dbi->newquery();
  54. if ( m_deleteAll )
  55. {
  56. TomahawkSqlQuery dirquery = dbi->newquery();
  57. dirquery.prepare( QString( "SELECT id FROM file WHERE source %1" )
  58. .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) );
  59. dirquery.exec();
  60. while ( dirquery.next() )
  61. m_idList << dirquery.value( 0 ).toUInt();
  62. }
  63. else if ( source()->isLocal() )
  64. {
  65. if ( m_dir.path() != QString( "." ) )
  66. {
  67. tDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid;
  68. TomahawkSqlQuery dirquery = dbi->newquery();
  69. QString path( "file://" + m_dir.canonicalPath() + "/%" );
  70. dirquery.prepare( QString( "SELECT id FROM file WHERE source IS NULL AND url LIKE '%1'" ).arg( TomahawkSqlQuery::escape( path ) ) );
  71. dirquery.exec();
  72. while ( dirquery.next() )
  73. {
  74. m_ids << dirquery.value( 0 );
  75. m_idList << dirquery.value( 0 ).toUInt();
  76. }
  77. }
  78. else if ( !m_ids.isEmpty() )
  79. {
  80. tDebug() << Q_FUNC_INFO << "deleting given ids";
  81. foreach ( const QVariant& id, m_ids )
  82. m_idList << id.toUInt();
  83. }
  84. }
  85. if ( m_deleteAll )
  86. {
  87. delquery.prepare( QString( "DELETE FROM file WHERE source %1" )
  88. .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) );
  89. delquery.exec();
  90. }
  91. else if ( !m_ids.isEmpty() )
  92. {
  93. QString idstring;
  94. foreach ( const QVariant& id, m_ids )
  95. idstring.append( id.toString() + ", " );
  96. idstring.chop( 2 ); //remove the trailing ", "
  97. if ( !source()->isLocal() )
  98. {
  99. delquery.prepare( QString( "SELECT id FROM file WHERE source = %1 AND url IN ( %2 )" )
  100. .arg( source()->id() )
  101. .arg( idstring ) );
  102. delquery.exec();
  103. idstring = QString();
  104. while ( delquery.next() )
  105. {
  106. idstring.append( delquery.value( 0 ).toString() + ", " );
  107. m_idList << delquery.value( 0 ).toUInt();
  108. }
  109. idstring.chop( 2 ); //remove the trailing ", "
  110. }
  111. delquery.prepare( QString( "DELETE FROM file WHERE source %1 AND id IN ( %2 )" )
  112. .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) )
  113. .arg( idstring ) );
  114. delquery.exec();
  115. }
  116. if ( !m_idList.isEmpty() )
  117. source()->updateIndexWhenSynced();
  118. emit done( m_idList, source()->dbCollection() );
  119. }