1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
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.
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.
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/>.
20
#include "DatabaseCommand_DeleteFiles.h"
22
#include <QtSql/QSqlQuery>
26
#include "Collection.h"
28
#include "database/Database.h"
29
#include "database/DatabaseImpl.h"
30
#include "network/Servent.h"
31
#include "utils/Logger.h"
32
#include "utils/TomahawkUtils.h"
34
using namespace Tomahawk;
37
// After changing a collection, we need to tell other bits of the system:
39
DatabaseCommand_DeleteFiles::postCommitHook()
41
if ( !m_idList.count() )
44
// make the collection object emit its tracksAdded signal, so the
45
// collection browser will update/fade in etc.
46
Collection* coll = source()->collection().data();
48
connect( this, SIGNAL( notify( QList<unsigned int> ) ),
49
coll, SLOT( delTracks( QList<unsigned int> ) ), Qt::QueuedConnection );
51
tDebug() << "Notifying of deleted tracks:" << m_idList.size() << "from source" << source()->id();
52
emit notify( m_idList );
54
if ( source()->isLocal() )
55
Servent::instance()->triggerDBSync();
60
DatabaseCommand_DeleteFiles::exec( DatabaseImpl* dbi )
62
Q_ASSERT( !source().isNull() );
64
int srcid = source()->isLocal() ? 0 : source()->id();
65
TomahawkSqlQuery delquery = dbi->newquery();
69
TomahawkSqlQuery dirquery = dbi->newquery();
70
dirquery.prepare( QString( "SELECT id FROM file WHERE source %1" )
71
.arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) );
74
while ( dirquery.next() )
75
m_idList << dirquery.value( 0 ).toUInt();
77
else if ( source()->isLocal() )
79
if ( m_dir.path() != QString( "." ) )
81
tDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid;
82
TomahawkSqlQuery dirquery = dbi->newquery();
83
QString path( "file://" + m_dir.canonicalPath() + "/%" );
84
dirquery.prepare( QString( "SELECT id FROM file WHERE source IS NULL AND url LIKE '%1'" ).arg( TomahawkSqlQuery::escape( path ) ) );
87
while ( dirquery.next() )
89
m_ids << dirquery.value( 0 );
90
m_idList << dirquery.value( 0 ).toUInt();
93
else if ( !m_ids.isEmpty() )
95
tDebug() << Q_FUNC_INFO << "deleting given ids";
96
foreach ( const QVariant& id, m_ids )
97
m_idList << id.toUInt();
103
delquery.prepare( QString( "DELETE FROM file WHERE source %1" )
104
.arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) );
107
else if ( !m_ids.isEmpty() )
110
foreach ( const QVariant& id, m_ids )
111
idstring.append( id.toString() + ", " );
112
idstring.chop( 2 ); //remove the trailing ", "
114
if ( !source()->isLocal() )
116
delquery.prepare( QString( "SELECT id FROM file WHERE source = %1 AND url IN ( %2 )" )
117
.arg( source()->id() )
121
idstring = QString();
122
while ( delquery.next() )
124
idstring.append( delquery.value( 0 ).toString() + ", " );
125
m_idList << delquery.value( 0 ).toUInt();
127
idstring.chop( 2 ); //remove the trailing ", "
130
delquery.prepare( QString( "DELETE FROM file WHERE source %1 AND id IN ( %2 )" )
131
.arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) )
136
if ( m_idList.count() )
137
source()->updateIndexWhenSynced();
139
emit done( m_idList, source()->collection() );