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