1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
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.
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.
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/>.
19
#include "DatabaseCommand_AddFiles.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"
32
#include "utils/Logger.h"
34
using namespace Tomahawk;
37
// remove file paths when making oplog/for network transmission
39
DatabaseCommand_AddFiles::files() const
42
foreach ( const QVariant& v, m_files )
44
// replace url with the id, we don't leak file paths over the network.
45
QVariantMap m = v.toMap();
47
m.insert( "url", QString::number( m.value( "id" ).toInt() ) );
54
// After changing a collection, we need to tell other bits of the system:
56
DatabaseCommand_AddFiles::postCommitHook()
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();
62
connect( this, SIGNAL( notify( QList<unsigned int> ) ),
63
coll, SLOT( setTracks( QList<unsigned int> ) ), Qt::QueuedConnection );
67
if ( source()->isLocal() )
68
Servent::instance()->triggerDBSync();
73
DatabaseCommand_AddFiles::exec( DatabaseImpl* dbi )
75
qDebug() << Q_FUNC_INFO;
76
Q_ASSERT( !source().isNull() );
78
TomahawkSqlQuery query_file = dbi->newquery();
79
TomahawkSqlQuery query_filejoin = dbi->newquery();
80
TomahawkSqlQuery query_trackattr = dbi->newquery();
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 (?, ?, ?)" );
87
QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id();
88
qDebug() << "Adding" << m_files.length() << "files to db for source" << srcid;
90
QList<QVariant>::iterator it;
91
for ( it = m_files.begin(); it != m_files.end(); ++it )
94
QVariantMap m = v.toMap();
96
int fileid = 0, artistid = 0, albumid = 0, trackid = 0, composerid = 0;
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();
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 );
123
if ( added % 1000 == 0 )
124
qDebug() << "Inserted" << added;
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
132
artistid = dbi->artistId( artist, true );
135
trackid = dbi->trackId( artistid, track, true );
138
albumid = dbi->albumId( artistid, album, true );
140
if( !composer.trimmed().isEmpty() )
141
composerid = dbi->artistId( composer, true );
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() )
153
qDebug() << "Error inserting into file_join table";
157
query_trackattr.bindValue( 0, trackid );
158
query_trackattr.bindValue( 1, "releaseyear" );
159
query_trackattr.bindValue( 2, year );
160
query_trackattr.exec();
166
qDebug() << "Inserted" << added << "tracks to database";
167
tDebug() << "Committing" << added << "tracks...";
169
emit done( m_files, source()->collection() );