-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtraktorfeature.cpp
646 lines (574 loc) · 24.3 KB
/
traktorfeature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "library/traktor/traktorfeature.h"
#include <QMap>
#include <QMessageBox>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QSettings>
#include <QXmlStreamReader>
#include <QtDebug>
#include "library/library.h"
#include "library/librarytablemodel.h"
#include "library/missingtablemodel.h"
#include "library/queryutil.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "library/treeitem.h"
#include "moc_traktorfeature.cpp"
#include "util/sandbox.h"
#include "util/semanticversion.h"
namespace {
QString fromTraktorSeparators(QString path) {
// Traktor uses /: instead of just / as delimiting character for some reasons
return path.replace("/:", "/");
}
} // anonymous namespace
TraktorTrackModel::TraktorTrackModel(QObject* parent,
TrackCollectionManager* pTrackCollectionManager,
QSharedPointer<BaseTrackCache> trackSource)
: BaseExternalTrackModel(parent, pTrackCollectionManager,
"mixxx.db.model.traktor_tablemodel",
"traktor_library",
trackSource) {
}
bool TraktorTrackModel::isColumnHiddenByDefault(int column) {
if (column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_BITRATE)) {
return true;
}
return BaseSqlTableModel::isColumnHiddenByDefault(column);
}
TraktorPlaylistModel::TraktorPlaylistModel(QObject* parent,
TrackCollectionManager* pTrackCollectionManager,
QSharedPointer<BaseTrackCache> trackSource)
: BaseExternalPlaylistModel(parent, pTrackCollectionManager,
"mixxx.db.model.traktor.playlistmodel",
"traktor_playlists",
"traktor_playlist_tracks",
trackSource) {
}
bool TraktorPlaylistModel::isColumnHiddenByDefault(int column) {
if (column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_BITRATE)) {
return true;
}
return BaseSqlTableModel::isColumnHiddenByDefault(column);
}
TraktorFeature::TraktorFeature(Library* pLibrary, UserSettingsPointer pConfig)
: BaseExternalLibraryFeature(pLibrary, pConfig, QStringLiteral("traktor")),
m_pSidebarModel(make_parented<TreeItemModel>(this)),
m_cancelImport(false) {
QString tableName = "traktor_library";
QString idColumn = "id";
QStringList columns;
columns << "id"
<< "artist"
<< "title"
<< "album"
<< "year"
<< "genre"
<< "tracknumber"
<< "location"
<< "comment"
<< "rating"
<< "duration"
<< "bitrate"
<< "bpm"
<< "key";
m_trackSource = QSharedPointer<BaseTrackCache>(new BaseTrackCache(
pLibrary->trackCollectionManager()->internalCollection(),
tableName,
idColumn,
columns,
false));
QStringList searchColumns;
searchColumns << "artist"
<< "album"
<< "location"
<< "comment"
<< "title"
<< "genre";
m_trackSource->setSearchColumns(searchColumns);
m_isActivated = false;
m_pTraktorTableModel = new TraktorTrackModel(
this, pLibrary->trackCollectionManager(), m_trackSource);
m_pTraktorPlaylistModel = new TraktorPlaylistModel(
this, pLibrary->trackCollectionManager(), m_trackSource);
m_title = tr("Traktor");
m_database =
QSqlDatabase::cloneDatabase(pLibrary->trackCollectionManager()
->internalCollection()
->database(),
"TRAKTOR_SCANNER");
//Open the database connection in this thread.
if (!m_database.open()) {
qDebug() << "Failed to open database for iTunes scanner."
<< m_database.lastError();
}
connect(&m_future_watcher,
&QFutureWatcher<TreeItem*>::finished,
this,
&TraktorFeature::onTrackCollectionLoaded);
}
TraktorFeature::~TraktorFeature() {
m_database.close();
m_cancelImport = true;
m_future.waitForFinished();
delete m_pTraktorTableModel;
delete m_pTraktorPlaylistModel;
}
BaseSqlTableModel* TraktorFeature::getPlaylistModelForPlaylist(const QString& playlist) {
TraktorPlaylistModel* pModel = new TraktorPlaylistModel(
this, m_pLibrary->trackCollectionManager(), m_trackSource);
pModel->setPlaylist(playlist);
return pModel;
}
QVariant TraktorFeature::title() {
return m_title;
}
bool TraktorFeature::isSupported() {
return (QFile::exists(getTraktorMusicDatabase()));
}
TreeItemModel* TraktorFeature::sidebarModel() const {
return m_pSidebarModel;
}
void TraktorFeature::refreshLibraryModels() {
}
void TraktorFeature::activate() {
qDebug() << "TraktorFeature::activate()";
if (!m_isActivated) {
m_isActivated = true;
// Let a worker thread do the XML parsing
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
m_future = QtConcurrent::run(&TraktorFeature::importLibrary,
this,
getTraktorMusicDatabase());
#else
m_future = QtConcurrent::run(this, &TraktorFeature::importLibrary,
getTraktorMusicDatabase());
#endif
m_future_watcher.setFuture(m_future);
m_title = tr("(loading) Traktor");
//calls a slot in the sidebar model such that 'iTunes (isLoading)' is displayed.
emit featureIsLoading(this, true);
}
emit saveModelState();
emit showTrackModel(m_pTraktorTableModel);
emit enableCoverArtDisplay(false);
}
void TraktorFeature::activateChild(const QModelIndex& index) {
if (!index.isValid()) {
return;
}
//access underlying TreeItem object
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (!item->hasChildren()) {
qDebug() << "Activate Traktor Playlist: " << item->getData().toString();
emit saveModelState();
m_pTraktorPlaylistModel->setPlaylist(item->getData().toString());
emit showTrackModel(m_pTraktorPlaylistModel);
emit enableCoverArtDisplay(false);
}
}
TreeItem* TraktorFeature::importLibrary(const QString& file) {
//Give thread a low priority
QThread* thisThread = QThread::currentThread();
thisThread->setPriority(QThread::LowPriority);
//Invisible root item of Traktor's child model
TreeItem* root = nullptr;
//Delete all table entries of Traktor feature
ScopedTransaction transaction(m_database);
clearTable("traktor_playlist_tracks");
clearTable("traktor_library");
clearTable("traktor_playlists");
transaction.commit();
transaction.transaction();
QSqlQuery query(m_database);
query.prepare("INSERT INTO traktor_library (artist, title, album, year,"
"genre,comment,tracknumber,bpm, bitrate,duration, location,"
"rating,key) VALUES (:artist, :title, :album, :year,:genre,"
":comment, :tracknumber,:bpm, :bitrate,:duration, :location,"
":rating,:key)");
//Parse Trakor XML file using SAX (for performance)
mixxx::FileInfo fileInfo(file);
QFile traktor_file(file);
if (!Sandbox::askForAccess(&fileInfo) || !traktor_file.open(QIODevice::ReadOnly)) {
qDebug() << "Cannot open Traktor music collection";
return nullptr;
}
QXmlStreamReader xml(&traktor_file);
bool inCollectionTag = false;
bool inPlaylistsTag = false;
bool isRootFolderParsed = false;
int nAudioFiles = 0;
while (!xml.atEnd() && !m_cancelImport) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("COLLECTION")) {
inCollectionTag = true;
}
// Each "ENTRY" tag in <COLLECTION> represents a track
if (inCollectionTag && xml.name() == QLatin1String("ENTRY")) {
//parse track
parseTrack(xml, query);
++nAudioFiles; //increment number of files in the music collection
}
if (xml.name() == QLatin1String("PLAYLISTS")) {
inPlaylistsTag = true;
}
if (inPlaylistsTag && !isRootFolderParsed && xml.name() == QLatin1String("NODE")) {
QXmlStreamAttributes attr = xml.attributes();
QString nodetype = attr.value("TYPE").toString();
QString name = attr.value("NAME").toString();
if (nodetype == "FOLDER" && name == "$ROOT") {
//process all playlists
root = parsePlaylists(xml);
isRootFolderParsed = true;
}
}
}
if (xml.isEndElement()) {
if (xml.name() == QLatin1String("COLLECTION")) {
inCollectionTag = false;
}
if (xml.name() == QLatin1String("PLAYLISTS") && inPlaylistsTag) {
inPlaylistsTag = false;
}
}
}
if (xml.hasError()) {
// do error handling
qDebug() << "Cannot process Traktor music collection";
if (root) {
delete root;
}
return nullptr;
}
qDebug() << "Found: " << nAudioFiles << " audio files in Traktor";
//initialize TraktorTableModel
transaction.commit();
return root;
}
void TraktorFeature::parseTrack(QXmlStreamReader &xml, QSqlQuery &query) {
QString title;
QString artist;
QString album;
QString year;
QString genre;
//drive letter
QString volume;
QString path;
QString filename;
QString location;
float bpm = 0.0;
int bitrate = 0;
QString key;
//duration of a track
int playtime = 0;
int rating = 0;
QString comment;
QString tracknumber;
//get XML attributes of starting ENTRY tag
QXmlStreamAttributes attr = xml.attributes ();
title = attr.value("TITLE").toString();
artist = attr.value("ARTIST").toString();
//read all sub tags of ENTRY until we reach the closing ENTRY tag
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("ALBUM")) {
QXmlStreamAttributes attr = xml.attributes ();
album = attr.value("TITLE").toString();
tracknumber = attr.value("TRACK").toString();
continue;
}
if (xml.name() == QLatin1String("LOCATION")) {
QXmlStreamAttributes attr = xml.attributes ();
volume = attr.value("VOLUME").toString();
path = attr.value("DIR").toString();
filename = attr.value("FILE").toString();
// compute the location, i.e, combining all the values
// On Windows the volume holds the drive letter e.g., d:
// On OS X, the volume is supposed to be "Macintosh HD" or "Macintosh SSD",
// which is a folder in /Volumes/ symlinked to root folder /
#if defined(__APPLE__)
location = "/Volumes/" + volume;
#else
location = volume;
#endif
location += fromTraktorSeparators(path);
location += filename;
continue;
}
if (xml.name() == QLatin1String("INFO")) {
QXmlStreamAttributes attr = xml.attributes();
key = attr.value("KEY").toString();
bitrate = attr.value("BITRATE").toString().toInt() / 1000;
playtime = attr.value("PLAYTIME").toString().toInt();
genre = attr.value("GENRE").toString();
year = attr.value("RELEASE_DATE").toString();
comment = attr.value("COMMENT").toString();
QString ranking_str = attr.value("RANKING").toString();
// A ranking in Traktor has ranges between 0 and 255 internally.
// This is same as the POPULARIMETER tag in IDv2,
// see http://help.mp3tag.de/main_tags.html
//
// Our rating values range from 1 to 5. The mapping is defined as follow
// ourRatingValue = TraktorRating / 51
bool ok = false;
int parsed_rating = ranking_str.toInt(&ok) / 51;
if (ok) {
rating = parsed_rating;
}
continue;
}
if (xml.name() == QLatin1String("TEMPO")) {
QXmlStreamAttributes attr = xml.attributes ();
bpm = attr.value("BPM").toString().toFloat();
continue;
}
}
//We leave the infinite loop, if twe have the closing tag "ENTRY"
if (xml.name() == QLatin1String("ENTRY") && xml.isEndElement()) {
break;
}
}
// If we reach the end of ENTRY within the COLLECTION tag
// Save parsed track to database
query.bindValue(":artist", artist);
query.bindValue(":title", title);
query.bindValue(":album", album);
query.bindValue(":genre", genre);
query.bindValue(":year", year);
query.bindValue(":duration", playtime);
query.bindValue(":location", location);
query.bindValue(":rating", rating);
query.bindValue(":comment", comment);
query.bindValue(":tracknumber", tracknumber);
query.bindValue(":key", key);
query.bindValue(":bpm", bpm);
query.bindValue(":bitrate", bitrate);
bool success = query.exec();
if (!success) {
qDebug() << "SQL Error in TraktorTableModel.cpp: line"
<< __LINE__ << " " << query.lastError();
return;
}
}
// Purpose: Parsing all the folder and playlists of Traktor
// This is a complex operation since Traktor uses the concept of folders and playlist.
// A folder can contain folders and playlists. A playlist contains entries but no folders.
// In other words, Traktor uses a tree structure to organize music.
// Inner nodes represent folders while leaves are playlists.
TreeItem* TraktorFeature::parsePlaylists(QXmlStreamReader &xml) {
qDebug() << "Process RootFolder";
//Each playlist is unique and can be identified by a path in the tree structure.
QString current_path = "";
QMap<QString,QString> map;
QString delimiter = "-->";
std::unique_ptr<TreeItem> rootItem = TreeItem::newRoot(this);
TreeItem* parent = rootItem.get();
QSqlQuery query_insert_to_playlists(m_database);
query_insert_to_playlists.prepare("INSERT INTO traktor_playlists (name) "
"VALUES (:name)");
QSqlQuery query_insert_to_playlist_tracks(m_database);
query_insert_to_playlist_tracks.prepare(
"INSERT INTO traktor_playlist_tracks (playlist_id, track_id, position) "
"VALUES (:playlist_id, :track_id, :position)");
while (!xml.atEnd() && !m_cancelImport) {
//read next XML element
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("NODE")) {
QXmlStreamAttributes attr = xml.attributes();
QString name = attr.value("NAME").toString();
QString type = attr.value("TYPE").toString();
//TODO: What happens if the folder node is a leaf (empty folder)
// Idea: Hide empty folders :-)
if (type == "FOLDER") {
current_path += delimiter;
current_path += name;
//qDebug() << "Folder: " +current_path << " has parent " << parent->getData().toString();
map.insert(current_path, "FOLDER");
parent = parent->appendChild(name, current_path);
} else if (type == "PLAYLIST") {
current_path += delimiter;
current_path += name;
//qDebug() << "Playlist: " +current_path << " has parent " << parent->getData().toString();
map.insert(current_path, "PLAYLIST");
parent->appendChild(name, current_path);
// process all the entries within the playlist 'name' having path 'current_path'
parsePlaylistEntries(xml,
current_path,
std::move(query_insert_to_playlists),
std::move(query_insert_to_playlist_tracks));
}
}
}
if (xml.isEndElement()) {
if (xml.name() == QLatin1String("NODE")) {
if (map.value(current_path) == "FOLDER") {
parent = parent->parent();
}
//Whenever we find a closing NODE, remove the last component of the path
int lastSlash = current_path.lastIndexOf (delimiter);
int path_length = current_path.size();
current_path.remove(lastSlash, path_length - lastSlash);
}
//We leave the infinite loop, if twe have the closing "PLAYLIST" tag
if (xml.name() == QLatin1String("PLAYLISTS")) {
break;
}
}
}
return rootItem.release();
}
void TraktorFeature::parsePlaylistEntries(
QXmlStreamReader& xml,
const QString& playlist_path,
QSqlQuery query_insert_into_playlist,
QSqlQuery query_insert_into_playlisttracks) {
// In the database, the name of a playlist is specified by the unique path,
// e.g., /someFolderA/someFolderB/playlistA"
query_insert_into_playlist.bindValue(":name", playlist_path);
if (!query_insert_into_playlist.exec()) {
LOG_FAILED_QUERY(query_insert_into_playlist)
<< "Failed to insert playlist in TraktorTableModel:"
<< playlist_path;
return;
}
// Get playlist id
QSqlQuery id_query(m_database);
id_query.prepare("select id from traktor_playlists where name=:path");
id_query.bindValue(":path", playlist_path);
if (!id_query.exec()) {
LOG_FAILED_QUERY(id_query) << "Could not get inserted playlist id for Traktor playlist::"
<< playlist_path;
return;
}
//playlist_id = id_query.lastInsertId().toInt();
int playlist_id = -1;
const int idColumn = id_query.record().indexOf("id");
while (id_query.next()) {
playlist_id = id_query.value(idColumn).toInt();
}
int playlist_position = 1;
while (!xml.atEnd() && !m_cancelImport) {
//read next XML element
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("PRIMARYKEY")) {
QXmlStreamAttributes attr = xml.attributes();
QString key = attr.value("KEY").toString();
QString type = attr.value("TYPE").toString();
if (type == "TRACK") {
key = fromTraktorSeparators(key);
#if defined(__APPLE__)
key.prepend("/Volumes/");
#endif
//insert to database
int track_id = -1;
QSqlQuery finder_query(m_database);
finder_query.prepare("select id from traktor_library where location=:path");
finder_query.bindValue(":path", key);
if (!finder_query.exec()) {
LOG_FAILED_QUERY(finder_query) << "Could not get track id:" << key;
continue;
}
if (finder_query.next()) {
track_id = finder_query.value(finder_query.record().indexOf("id")).toInt();
}
query_insert_into_playlisttracks.bindValue(":playlist_id", playlist_id);
query_insert_into_playlisttracks.bindValue(":track_id", track_id);
query_insert_into_playlisttracks.bindValue(":position", playlist_position++);
if (!query_insert_into_playlisttracks.exec()) {
LOG_FAILED_QUERY(query_insert_into_playlisttracks)
<< "trackid" << track_id << " with path " << key
<< "playlistname; " << playlist_path <<" with ID " << playlist_id;
}
}
}
}
if (xml.isEndElement()) {
//We leave the infinite loop, if twe have the closing "PLAYLIST" tag
if (xml.name() == QLatin1String("PLAYLIST")) {
break;
}
}
}
}
void TraktorFeature::clearTable(const QString& table_name) {
QSqlQuery query(m_database);
query.prepare("delete from "+table_name);
if (!query.exec()) {
qDebug() << "Could not delete remove old entries from table "
<< table_name << " : " << query.lastError();
} else {
qDebug() << "Traktor table entries of '" << table_name << "' have been cleared.";
}
}
QString TraktorFeature::getTraktorMusicDatabase() {
QString musicFolder = "";
// As of version 2, Traktor has changed the path of the collection.nml
// In general, the path is <Home>/Documents/Native Instruments/Traktor 2.x.y/collection.nml
// where x and y denote the bug fix release numbers. For example, Traktor 2.0.3 has the
// following path: <Home>/Documents/Native Instruments/Traktor 2.0.3/collection.nml
//Let's try to detect the latest Traktor version and its collection.nml
QString myDocuments = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QDir ni_directory(myDocuments +"/Native Instruments/");
ni_directory.setFilter(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
// We may not have access to this directory since it is in the user's
// Documents folder. Ask for access if we don't have it.
if (ni_directory.exists()) {
auto fileInfo = mixxx::FileInfo(ni_directory);
Sandbox::askForAccess(&fileInfo);
}
//Iterate over the subfolders
QFileInfoList list = ni_directory.entryInfoList();
QMap<mixxx::SemanticVersion, QString> installed_ts_map;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
QString folder_name = fileInfo.fileName();
if (folder_name == "Traktor") {
//We found a Traktor 1 installation
installed_ts_map.insert(mixxx::SemanticVersion(1, 0, 0), fileInfo.absoluteFilePath());
continue;
}
if (folder_name.contains("Traktor")) {
qDebug() << "Found " << folder_name;
auto semver = mixxx::SemanticVersion(folder_name);
if (semver.isValid()) {
installed_ts_map.insert(semver, fileInfo.absoluteFilePath());
}
}
}
//If no Traktor installation has been found, return some default string
if (installed_ts_map.isEmpty()) {
musicFolder = QDir::homePath() + "/collection.nml";
} else { //Select the folder with the highest version as default Traktor folder
QList<mixxx::SemanticVersion> versions = installed_ts_map.keys();
std::sort(versions.begin(), versions.end());
musicFolder = installed_ts_map.value(versions.last()) + "/collection.nml";
}
qDebug() << "Traktor Library Location=[" << musicFolder << "]";
return musicFolder;
}
void TraktorFeature::onTrackCollectionLoaded() {
std::unique_ptr<TreeItem> root(m_future.result());
if (root) {
m_pSidebarModel->setRootItem(std::move(root));
// Tell the traktor track source that it should re-build its index.
m_trackSource->buildIndex();
//m_pTraktorTableModel->select();
emit saveModelState();
emit showTrackModel(m_pTraktorTableModel);
qDebug() << "Traktor library loaded successfully";
} else {
QMessageBox::warning(
nullptr,
tr("Error Loading Traktor Library"),
tr("There was an error loading your Traktor library. Some of "
"your Traktor tracks or playlists may not have loaded."));
}
// calls a slot in the sidebarmodel such that 'isLoading' is removed from the feature title.
m_title = tr("Traktor");
emit featureLoadingFinished(this);
activate();
}