PageRenderTime 62ms CodeModel.GetById 24ms app.highlight 16ms RepoModel.GetById 14ms app.codeStats 6ms

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