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

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/DatabaseCommand_AddFiles.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
 *
 
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
 
 
19
#include "DatabaseCommand_AddFiles.h"
 
20
 
 
21
#include <QSqlQuery>
 
22
 
 
23
#include "Artist.h"
 
24
#include "Album.h"
 
25
#include "Collection.h"
 
26
#include "database/Database.h"
 
27
#include "DatabaseImpl.h"
 
28
#include "network/DbSyncConnection.h"
 
29
#include "network/Servent.h"
 
30
#include "SourceList.h"
 
31
 
 
32
#include "utils/Logger.h"
 
33
 
 
34
using namespace Tomahawk;
 
35
 
 
36
 
 
37
// remove file paths when making oplog/for network transmission
 
38
QVariantList
 
39
DatabaseCommand_AddFiles::files() const
 
40
{
 
41
    QVariantList list;
 
42
    foreach ( const QVariant& v, m_files )
 
43
    {
 
44
        // replace url with the id, we don't leak file paths over the network.
 
45
        QVariantMap m = v.toMap();
 
46
        m.remove( "url" );
 
47
        m.insert( "url", QString::number( m.value( "id" ).toInt() ) );
 
48
        list.append( m );
 
49
    }
 
50
    return list;
 
51
}
 
52
 
 
53
 
 
54
// After changing a collection, we need to tell other bits of the system:
 
55
void
 
56
DatabaseCommand_AddFiles::postCommitHook()
 
57
{
 
58
    // make the collection object emit its tracksAdded signal, so the
 
59
    // collection browser will update/fade in etc.
 
60
    Collection* coll = source()->collection().data();
 
61
 
 
62
    connect( this, SIGNAL( notify( QList<unsigned int> ) ),
 
63
             coll,   SLOT( setTracks( QList<unsigned int> ) ), Qt::QueuedConnection );
 
64
 
 
65
    emit notify( m_ids );
 
66
 
 
67
    if ( source()->isLocal() )
 
68
        Servent::instance()->triggerDBSync();
 
69
}
 
70
 
 
71
 
 
72
void
 
73
DatabaseCommand_AddFiles::exec( DatabaseImpl* dbi )
 
74
{
 
75
    qDebug() << Q_FUNC_INFO;
 
76
    Q_ASSERT( !source().isNull() );
 
77
 
 
78
    TomahawkSqlQuery query_file = dbi->newquery();
 
79
    TomahawkSqlQuery query_filejoin = dbi->newquery();
 
80
    TomahawkSqlQuery query_trackattr = dbi->newquery();
 
81
 
 
82
    query_file.prepare( "INSERT INTO file(source, url, size, mtime, md5, mimetype, duration, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" );
 
83
    query_filejoin.prepare( "INSERT INTO file_join(file, artist, album, track, albumpos, composer, discnumber) VALUES (?, ?, ?, ?, ?, ?, ?)" );
 
84
    query_trackattr.prepare( "INSERT INTO track_attributes(id, k, v) VALUES (?, ?, ?)" );
 
85
 
 
86
    int added = 0;
 
87
    QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id();
 
88
    qDebug() << "Adding" << m_files.length() << "files to db for source" << srcid;
 
89
 
 
90
    QList<QVariant>::iterator it;
 
91
    for ( it = m_files.begin(); it != m_files.end(); ++it )
 
92
    {
 
93
        QVariant& v = *it;
 
94
        QVariantMap m = v.toMap();
 
95
 
 
96
        int fileid = 0, artistid = 0, albumid = 0, trackid = 0, composerid = 0;
 
97
 
 
98
        QString url      = m.value( "url" ).toString();
 
99
        int mtime        = m.value( "mtime" ).toInt();
 
100
        uint size        = m.value( "size" ).toUInt();
 
101
        QString hash     = m.value( "hash" ).toString();
 
102
        QString mimetype = m.value( "mimetype" ).toString();
 
103
        uint duration    = m.value( "duration" ).toUInt();
 
104
        uint bitrate     = m.value( "bitrate" ).toUInt();
 
105
        QString artist   = m.value( "artist" ).toString();
 
106
        QString album    = m.value( "album" ).toString();
 
107
        QString track    = m.value( "track" ).toString();
 
108
        uint albumpos    = m.value( "albumpos" ).toUInt();
 
109
        QString composer = m.value( "composer" ).toString();
 
110
        uint discnumber  = m.value( "discnumber" ).toUInt();
 
111
        int year         = m.value( "year" ).toInt();
 
112
 
 
113
        query_file.bindValue( 0, srcid );
 
114
        query_file.bindValue( 1, url );
 
115
        query_file.bindValue( 2, size );
 
116
        query_file.bindValue( 3, mtime );
 
117
        query_file.bindValue( 4, hash );
 
118
        query_file.bindValue( 5, mimetype );
 
119
        query_file.bindValue( 6, duration );
 
120
        query_file.bindValue( 7, bitrate );
 
121
        query_file.exec();
 
122
 
 
123
        if ( added % 1000 == 0 )
 
124
            qDebug() << "Inserted" << added;
 
125
 
 
126
        // get internal IDs for art/alb/trk
 
127
        fileid = query_file.lastInsertId().toInt();
 
128
        m.insert( "id", fileid );
 
129
        // this is the qvariant(map) the remote will get
 
130
        v = m;
 
131
 
 
132
        artistid = dbi->artistId( artist, true );
 
133
        if ( artistid < 1 )
 
134
            continue;
 
135
        trackid = dbi->trackId( artistid, track, true );
 
136
        if ( trackid < 1 )
 
137
            continue;
 
138
        albumid = dbi->albumId( artistid, album, true );
 
139
 
 
140
        if( !composer.trimmed().isEmpty() )
 
141
            composerid = dbi->artistId( composer, true );
 
142
 
 
143
        // Now add the association
 
144
        query_filejoin.bindValue( 0, fileid );
 
145
        query_filejoin.bindValue( 1, artistid );
 
146
        query_filejoin.bindValue( 2, albumid > 0 ? albumid : QVariant( QVariant::Int ) );
 
147
        query_filejoin.bindValue( 3, trackid );
 
148
        query_filejoin.bindValue( 4, albumpos );
 
149
        query_filejoin.bindValue( 5, composerid > 0 ? composerid : QVariant( QVariant::Int ) );
 
150
        query_filejoin.bindValue( 6, discnumber );
 
151
        if ( !query_filejoin.exec() )
 
152
        {
 
153
            qDebug() << "Error inserting into file_join table";
 
154
            continue;
 
155
        }
 
156
 
 
157
        query_trackattr.bindValue( 0, trackid );
 
158
        query_trackattr.bindValue( 1, "releaseyear" );
 
159
        query_trackattr.bindValue( 2, year );
 
160
        query_trackattr.exec();
 
161
 
 
162
        m_ids << fileid;
 
163
        added++;
 
164
    }
 
165
 
 
166
    qDebug() << "Inserted" << added << "tracks to database";
 
167
    tDebug() << "Committing" << added << "tracks...";
 
168
 
 
169
    emit done( m_files, source()->collection() );
 
170
}