PageRenderTime 143ms CodeModel.GetById 40ms app.highlight 43ms RepoModel.GetById 57ms app.codeStats 1ms

/src/libtomahawk/database/DatabaseCommand_AddFiles.cpp

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