~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/DatabaseCommand_DeleteFiles.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, 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 "Artist.h"
 
25
#include "Album.h"
 
26
#include "Collection.h"
 
27
#include "Source.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"
 
33
 
 
34
using namespace Tomahawk;
 
35
 
 
36
 
 
37
// After changing a collection, we need to tell other bits of the system:
 
38
void
 
39
DatabaseCommand_DeleteFiles::postCommitHook()
 
40
{
 
41
    if ( !m_idList.count() )
 
42
        return;
 
43
 
 
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();
 
47
 
 
48
    connect( this, SIGNAL( notify( QList<unsigned int> ) ),
 
49
             coll,   SLOT( delTracks( QList<unsigned int> ) ), Qt::QueuedConnection );
 
50
 
 
51
    tDebug() << "Notifying of deleted tracks:" << m_idList.size() << "from source" << source()->id();
 
52
    emit notify( m_idList );
 
53
 
 
54
    if ( source()->isLocal() )
 
55
        Servent::instance()->triggerDBSync();
 
56
}
 
57
 
 
58
 
 
59
void
 
60
DatabaseCommand_DeleteFiles::exec( DatabaseImpl* dbi )
 
61
{
 
62
    Q_ASSERT( !source().isNull() );
 
63
 
 
64
    int srcid = source()->isLocal() ? 0 : source()->id();
 
65
    TomahawkSqlQuery delquery = dbi->newquery();
 
66
 
 
67
    if ( m_deleteAll )
 
68
    {
 
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() ) ) );
 
72
        dirquery.exec();
 
73
        
 
74
        while ( dirquery.next() )
 
75
            m_idList << dirquery.value( 0 ).toUInt();
 
76
    }
 
77
    else if ( source()->isLocal() )
 
78
    {
 
79
        if ( m_dir.path() != QString( "." ) )
 
80
        {
 
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 ) ) );
 
85
            dirquery.exec();
 
86
 
 
87
            while ( dirquery.next() )
 
88
            {
 
89
                m_ids << dirquery.value( 0 );
 
90
                m_idList << dirquery.value( 0 ).toUInt();
 
91
            }
 
92
        }
 
93
        else if ( !m_ids.isEmpty() )
 
94
        {
 
95
            tDebug() << Q_FUNC_INFO << "deleting given ids";
 
96
            foreach ( const QVariant& id, m_ids )
 
97
                m_idList << id.toUInt();
 
98
        }
 
99
    }
 
100
 
 
101
    if ( m_deleteAll )
 
102
    {
 
103
        delquery.prepare( QString( "DELETE FROM file WHERE source %1" )
 
104
                    .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) );
 
105
        delquery.exec();
 
106
    }
 
107
    else if ( !m_ids.isEmpty() )
 
108
    {
 
109
        QString idstring;
 
110
        foreach ( const QVariant& id, m_ids )
 
111
            idstring.append( id.toString() + ", " );
 
112
        idstring.chop( 2 ); //remove the trailing ", "
 
113
 
 
114
        if ( !source()->isLocal() )
 
115
        {
 
116
            delquery.prepare( QString( "SELECT id FROM file WHERE source = %1 AND url IN ( %2 )" )
 
117
                        .arg( source()->id() )
 
118
                        .arg( idstring ) );
 
119
            delquery.exec();
 
120
            
 
121
            idstring = QString();
 
122
            while ( delquery.next() )
 
123
            {
 
124
                idstring.append( delquery.value( 0 ).toString() + ", " );
 
125
                m_idList << delquery.value( 0 ).toUInt();
 
126
            }
 
127
            idstring.chop( 2 ); //remove the trailing ", "
 
128
        }
 
129
 
 
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() ) )
 
132
                             .arg( idstring ) );
 
133
        delquery.exec();
 
134
    }
 
135
 
 
136
    if ( m_idList.count() )
 
137
        source()->updateIndexWhenSynced();
 
138
 
 
139
    emit done( m_idList, source()->collection() );
 
140
}