forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbaseplaylistfeature.cpp
More file actions
871 lines (774 loc) · 31.3 KB
/
Copy pathbaseplaylistfeature.cpp
File metadata and controls
871 lines (774 loc) · 31.3 KB
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
#include "library/trackset/baseplaylistfeature.h"
#include <QAction>
#include <QFileInfo>
#include <QInputDialog>
#include <QList>
#include "library/export/trackexportwizard.h"
#include "library/library.h"
#include "library/library_prefs.h"
#include "library/parser.h"
#include "library/parsercsv.h"
#include "library/playlisttablemodel.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "library/treeitem.h"
#include "library/treeitemmodel.h"
#include "moc_baseplaylistfeature.cpp"
#include "track/track.h"
#include "track/trackid.h"
#include "util/assert.h"
#include "util/defs.h"
#include "util/file.h"
#include "widget/wlibrary.h"
#include "widget/wlibrarysidebar.h"
#include "widget/wlibrarytextbrowser.h"
namespace {
constexpr QChar kUnsafeFilenameReplacement = '-';
const ConfigKey kConfigKeyLastImportExportPlaylistDirectory(
"[Library]", "LastImportExportPlaylistDirectory");
} // anonymous namespace
using namespace mixxx::library::prefs;
BasePlaylistFeature::BasePlaylistFeature(
Library* pLibrary,
UserSettingsPointer pConfig,
PlaylistTableModel* pModel,
const QString& rootViewName,
const QString& iconName,
const QString& countsDurationTableName,
bool keepHiddenTracks)
: BaseTrackSetFeature(pLibrary, pConfig, rootViewName, iconName),
m_playlistDao(pLibrary->trackCollectionManager()
->internalCollection()
->getPlaylistDAO()),
m_pPlaylistTableModel(pModel),
m_countsDurationTableName(countsDurationTableName),
m_keepHiddenTracks(keepHiddenTracks) {
pModel->setParent(this);
initActions();
connectPlaylistDAO();
connect(m_pLibrary,
&Library::trackSelected,
this,
[this](const TrackPointer& pTrack) {
const auto trackId = pTrack ? pTrack->getId() : TrackId{};
slotTrackSelected(trackId);
});
connect(m_pLibrary,
&Library::switchToView,
this,
&BasePlaylistFeature::slotResetSelectedTrack);
}
void BasePlaylistFeature::initActions() {
m_pCreatePlaylistAction = new QAction(tr("Create New Playlist"), this);
connect(m_pCreatePlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotCreatePlaylist);
m_pRenamePlaylistAction = new QAction(tr("Rename"), this);
m_pRenamePlaylistAction->setShortcut(kRenameSidebarItemShortcutKey);
connect(m_pRenamePlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotRenamePlaylist);
m_pDuplicatePlaylistAction = new QAction(tr("Duplicate"), this);
connect(m_pDuplicatePlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotDuplicatePlaylist);
m_pDeletePlaylistAction = new QAction(tr("Remove"), this);
const auto removeKeySequence =
// TODO(XXX): Qt6 replace enum | with QKeyCombination
QKeySequence(static_cast<int>(kHideRemoveShortcutModifier) |
kHideRemoveShortcutKey);
m_pDeletePlaylistAction->setShortcut(removeKeySequence);
connect(m_pDeletePlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotDeletePlaylist);
m_pLockPlaylistAction = new QAction(tr("Lock"), this);
connect(m_pLockPlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotTogglePlaylistLock);
m_pAddToAutoDJAction = new QAction(tr("Add to Auto DJ Queue (bottom)"), this);
connect(m_pAddToAutoDJAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotAddToAutoDJ);
m_pAddToAutoDJTopAction = new QAction(tr("Add to Auto DJ Queue (top)"), this);
connect(m_pAddToAutoDJTopAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotAddToAutoDJTop);
m_pAddToAutoDJReplaceAction = new QAction(tr("Add to Auto DJ Queue (replace)"), this);
connect(m_pAddToAutoDJReplaceAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotAddToAutoDJReplace);
m_pAnalyzePlaylistAction = new QAction(tr("Analyze entire Playlist"), this);
connect(m_pAnalyzePlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotAnalyzePlaylist);
m_pImportPlaylistAction = new QAction(tr("Import Playlist"), this);
connect(m_pImportPlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotImportPlaylist);
m_pCreateImportPlaylistAction = new QAction(tr("Import Playlist"), this);
connect(m_pCreateImportPlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotCreateImportPlaylist);
m_pExportPlaylistAction = new QAction(tr("Export Playlist"), this);
connect(m_pExportPlaylistAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotExportPlaylist);
m_pExportTrackFilesAction = new QAction(tr("Export Track Files"), this);
connect(m_pExportTrackFilesAction,
&QAction::triggered,
this,
&BasePlaylistFeature::slotExportTrackFiles);
}
void BasePlaylistFeature::connectPlaylistDAO() {
connect(&m_playlistDao,
&PlaylistDAO::added,
this,
&BasePlaylistFeature::slotPlaylistTableChangedAndScrollTo);
connect(&m_playlistDao,
&PlaylistDAO::lockChanged,
this,
&BasePlaylistFeature::slotPlaylistContentOrLockChanged);
connect(&m_playlistDao,
&PlaylistDAO::deleted,
this,
&BasePlaylistFeature::slotPlaylistTableChanged);
connect(&m_playlistDao,
&PlaylistDAO::playlistContentChanged,
this,
&BasePlaylistFeature::slotPlaylistContentOrLockChanged);
connect(&m_playlistDao,
&PlaylistDAO::renamed,
this,
// In "History" just the item is renamed, while in "Playlists" the
// entire sidebar model is rebuilt to re-sort items by name
&BasePlaylistFeature::slotPlaylistTableRenamed);
}
int BasePlaylistFeature::playlistIdFromIndex(const QModelIndex& index) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
if (item == nullptr) {
return kInvalidPlaylistId;
}
bool ok = false;
int playlistId = item->getData().toInt(&ok);
if (ok) {
return playlistId;
} else {
return kInvalidPlaylistId;
}
}
void BasePlaylistFeature::selectPlaylistInSidebar(int playlistId, bool select) {
if (!m_pSidebarWidget) {
return;
}
if (playlistId == kInvalidPlaylistId) {
return;
}
QModelIndex index = indexFromPlaylistId(playlistId);
if (index.isValid()) {
m_pSidebarWidget->selectChildIndex(index, select);
}
}
void BasePlaylistFeature::activateChild(const QModelIndex& index) {
//qDebug() << "BasePlaylistFeature::activateChild()" << index;
int playlistId = playlistIdFromIndex(index);
if (playlistId == kInvalidPlaylistId) {
// may happen during initialization
return;
}
m_lastClickedIndex = index;
m_lastRightClickedIndex = QModelIndex();
emit saveModelState();
m_pPlaylistTableModel->selectPlaylist(playlistId);
emit showTrackModel(m_pPlaylistTableModel);
emit enableCoverArtDisplay(true);
}
void BasePlaylistFeature::activatePlaylist(int playlistId) {
// qDebug() << "BasePlaylistFeature::activatePlaylist()" << playlistId << index;
VERIFY_OR_DEBUG_ASSERT(playlistId != kInvalidPlaylistId) {
return;
}
QModelIndex index = indexFromPlaylistId(playlistId);
VERIFY_OR_DEBUG_ASSERT(index.isValid()) {
return;
}
m_lastClickedIndex = index;
m_lastRightClickedIndex = QModelIndex();
emit saveModelState();
m_pPlaylistTableModel->selectPlaylist(playlistId);
emit showTrackModel(m_pPlaylistTableModel);
emit enableCoverArtDisplay(true);
// Update selection
emit featureSelect(this, m_lastClickedIndex);
}
void BasePlaylistFeature::renameItem(const QModelIndex& index) {
m_lastRightClickedIndex = index;
slotRenamePlaylist();
}
void BasePlaylistFeature::slotRenamePlaylist() {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
QString oldName = m_playlistDao.getPlaylistName(playlistId);
bool locked = m_playlistDao.isPlaylistLocked(playlistId);
if (locked) {
qDebug() << "Skipping playlist rename because playlist" << playlistId
<< oldName << "is locked.";
return;
}
QString newName;
bool validNameGiven = false;
while (!validNameGiven) {
bool ok = false;
newName = QInputDialog::getText(nullptr,
tr("Rename Playlist"),
tr("Enter new name for playlist:"),
QLineEdit::Normal,
oldName,
&ok)
.trimmed();
if (!ok || oldName == newName) {
return;
}
int existingId = m_playlistDao.getPlaylistIdFromName(newName);
if (existingId != kInvalidPlaylistId) {
QMessageBox::warning(nullptr,
tr("Renaming Playlist Failed"),
tr("A playlist by that name already exists."));
} else if (newName.isEmpty()) {
QMessageBox::warning(nullptr,
tr("Renaming Playlist Failed"),
tr("A playlist cannot have a blank name."));
} else {
validNameGiven = true;
}
}
m_playlistDao.renamePlaylist(playlistId, newName);
}
void BasePlaylistFeature::slotDuplicatePlaylist() {
int oldPlaylistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (oldPlaylistId == kInvalidPlaylistId) {
return;
}
QString oldName = m_playlistDao.getPlaylistName(oldPlaylistId);
QString name;
bool validNameGiven = false;
while (!validNameGiven) {
bool ok = false;
name = QInputDialog::getText(nullptr,
tr("Duplicate Playlist"),
tr("Enter name for new playlist:"),
QLineEdit::Normal,
//: Appendix to default name when duplicating a playlist
oldName + tr("_copy", "//:"),
&ok)
.trimmed();
if (!ok || oldName == name) {
return;
}
int existingId = m_playlistDao.getPlaylistIdFromName(name);
if (existingId != kInvalidPlaylistId) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("A playlist by that name already exists."));
} else if (name.isEmpty()) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("A playlist cannot have a blank name."));
} else {
validNameGiven = true;
}
}
int newPlaylistId = m_playlistDao.createPlaylist(name);
if (newPlaylistId != kInvalidPlaylistId) {
m_playlistDao.copyPlaylistTracks(oldPlaylistId, newPlaylistId);
}
}
void BasePlaylistFeature::slotTogglePlaylistLock() {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
bool locked = !m_playlistDao.isPlaylistLocked(playlistId);
if (!m_playlistDao.setPlaylistLocked(playlistId, locked)) {
qDebug() << "Failed to toggle lock of playlistId " << playlistId;
}
}
void BasePlaylistFeature::slotCreatePlaylist() {
QString name;
bool validNameGiven = false;
while (!validNameGiven) {
bool ok = false;
name = QInputDialog::getText(nullptr,
tr("Create New Playlist"),
tr("Enter name for new playlist:"),
QLineEdit::Normal,
tr("New Playlist"),
&ok)
.trimmed();
if (!ok) {
return;
}
int existingId = m_playlistDao.getPlaylistIdFromName(name);
if (existingId != kInvalidPlaylistId) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("A playlist by that name already exists."));
} else if (name.isEmpty()) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("A playlist cannot have a blank name."));
} else {
validNameGiven = true;
}
}
int playlistId = m_playlistDao.createPlaylist(name);
if (playlistId == kInvalidPlaylistId) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("An unknown error occurred while creating playlist: ") + name);
}
}
/// Returns a playlist that is a sibling inside the same parent
/// as the start index
int BasePlaylistFeature::getSiblingPlaylistIdOf(QModelIndex& start) {
QModelIndex nextIndex = start.sibling(start.row() + 1, start.column());
if (!nextIndex.isValid() && start.row() > 0) {
// No playlist below, looking above.
nextIndex = start.sibling(start.row() - 1, start.column());
}
if (nextIndex.isValid()) {
TreeItem* pTreeItem = m_pSidebarModel->getItem(nextIndex);
DEBUG_ASSERT(pTreeItem != nullptr);
if (!pTreeItem->hasChildren()) {
return playlistIdFromIndex(nextIndex);
}
}
return kInvalidPlaylistId;
}
void BasePlaylistFeature::deleteItem(const QModelIndex& index) {
m_lastRightClickedIndex = index;
slotDeletePlaylist();
}
void BasePlaylistFeature::slotDeletePlaylist() {
//qDebug() << "slotDeletePlaylist() row:" << m_lastRightClickedIndex.data();
if (!m_lastRightClickedIndex.isValid()) {
return;
}
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
bool locked = m_playlistDao.isPlaylistLocked(playlistId);
if (locked) {
qDebug() << "Skipping playlist deletion because playlist" << playlistId << "is locked.";
return;
}
QMessageBox::StandardButton btn = QMessageBox::question(nullptr,
tr("Confirm Deletion"),
tr("Do you really want to delete playlist <b>%1</b>?")
.arg(m_playlistDao.getPlaylistName(playlistId)),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (btn == QMessageBox::No) {
return;
}
m_playlistDao.deletePlaylist(playlistId);
}
void BasePlaylistFeature::slotImportPlaylist() {
//qDebug() << "slotImportPlaylist() row:" << m_lastRightClickedIndex.data();
const QString playlistFile = getPlaylistFile();
if (playlistFile.isEmpty()) {
return;
}
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
// Update the import/export playlist directory
QString fileDirectory(playlistFile);
fileDirectory.truncate(playlistFile.lastIndexOf("/"));
m_pConfig->set(kConfigKeyLastImportExportPlaylistDirectory,
ConfigValue(fileDirectory));
slotImportPlaylistFile(playlistFile, playlistId);
}
void BasePlaylistFeature::slotImportPlaylistFile(const QString& playlistFile,
int playlistId) {
if (playlistFile.isEmpty()) {
return;
}
// The user has picked a new directory via a file dialog. This means the
// system sandboxer (if we are sandboxed) has granted us permission to this
// folder. We don't need access to this file on a regular basis so we do not
// register a security bookmark.
// Create a temporary PlaylistTableModel for the Playlist the entries shall be imported to.
// This is used as a proxy object to write to the database.
// We cannot use m_pPlaylistTableModel since it might have another playlist selected which
// is not the playlist that received the right-click.
QScopedPointer<PlaylistTableModel> pPlaylistTableModel(
new PlaylistTableModel(this,
m_pLibrary->trackCollectionManager(),
"mixxx.db.model.playlist_export"));
pPlaylistTableModel->selectPlaylist(playlistId);
pPlaylistTableModel->setSort(
pPlaylistTableModel->fieldIndex(
ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION),
Qt::AscendingOrder);
pPlaylistTableModel->select();
QList<QString> locations = Parser::parse(playlistFile);
// Iterate over the List that holds locations of playlist entries
pPlaylistTableModel->addTracks(QModelIndex(), locations);
}
void BasePlaylistFeature::slotCreateImportPlaylist() {
// Get file to read
const QStringList playlistFiles = LibraryFeature::getPlaylistFiles();
if (playlistFiles.isEmpty()) {
return;
}
// Set last import directory
QString fileDirectory(playlistFiles.first());
fileDirectory.truncate(playlistFiles.first().lastIndexOf("/"));
m_pConfig->set(kConfigKeyLastImportExportPlaylistDirectory,
ConfigValue(fileDirectory));
int lastPlaylistId = kInvalidPlaylistId;
// For each selected element create a different playlist.
for (const QString& playlistFile : playlistFiles) {
const QFileInfo fileInfo(playlistFile);
// Get a valid name
const QString baseName = fileInfo.baseName();
QString name = baseName;
// Check if there already is a playlist by that name. If yes, add
// increasing suffix (1++) until we find an unused name.
int i = 1;
while (m_playlistDao.getPlaylistIdFromName(name) != kInvalidPlaylistId) {
name = baseName + QChar(' ') + QString::number(i);
++i;
}
lastPlaylistId = m_playlistDao.createPlaylist(name);
if (lastPlaylistId == kInvalidPlaylistId) {
QMessageBox::warning(nullptr,
tr("Playlist Creation Failed"),
tr("An unknown error occurred while creating playlist: ") + name);
return;
}
slotImportPlaylistFile(playlistFile, lastPlaylistId);
}
activatePlaylist(lastPlaylistId);
}
void BasePlaylistFeature::slotExportPlaylist() {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
QString playlistName = m_playlistDao.getPlaylistName(playlistId);
// replace separator character with something generic
playlistName = playlistName.replace(QDir::separator(), kUnsafeFilenameReplacement);
qDebug() << "Export playlist" << playlistName;
QString lastPlaylistDirectory = m_pConfig->getValue(
kConfigKeyLastImportExportPlaylistDirectory,
QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
// Open a dialog to let the user choose the file location for playlist export.
// The location is set to the last used directory for import/export and the file
// name to the playlist name.
const QString fileLocation = getFilePathWithVerifiedExtensionFromFileDialog(
tr("Export Playlist"),
lastPlaylistDirectory.append("/").append(playlistName).append(".m3u"),
tr("M3U Playlist (*.m3u);;M3U8 Playlist (*.m3u8);;"
"PLS Playlist (*.pls);;Text CSV (*.csv);;Readable Text (*.txt)"),
tr("M3U Playlist (*.m3u)"));
// Exit method if the file name is empty because the user cancelled the save dialog.
if (fileLocation.isEmpty()) {
return;
}
// Update the import/export playlist directory
QString fileDirectory(fileLocation);
fileDirectory.truncate(fileLocation.lastIndexOf("/"));
m_pConfig->set(kConfigKeyLastImportExportPlaylistDirectory,
ConfigValue(fileDirectory));
// The user has picked a new directory via a file dialog. This means the
// system sandboxer (if we are sandboxed) has granted us permission to this
// folder. We don't need access to this file on a regular basis so we do not
// register a security bookmark.
// Create a new table model since the main one might have an active search.
// This will only export songs that we think exist on default
QScopedPointer<PlaylistTableModel> pPlaylistTableModel(
new PlaylistTableModel(this,
m_pLibrary->trackCollectionManager(),
"mixxx.db.model.playlist_export",
m_keepHiddenTracks));
emit saveModelState();
pPlaylistTableModel->selectPlaylist(playlistId);
pPlaylistTableModel->setSort(
pPlaylistTableModel->fieldIndex(
ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION),
Qt::AscendingOrder);
pPlaylistTableModel->select();
// check config if relative paths are desired
bool useRelativePath = m_pConfig->getValue<bool>(
kUseRelativePathOnExportConfigKey);
if (fileLocation.endsWith(".csv", Qt::CaseInsensitive)) {
ParserCsv::writeCSVFile(fileLocation, pPlaylistTableModel.data(), useRelativePath);
} else if (fileLocation.endsWith(".txt", Qt::CaseInsensitive)) {
if (m_playlistDao.getHiddenType(pPlaylistTableModel->getPlaylist()) ==
PlaylistDAO::PLHT_SET_LOG) {
ParserCsv::writeReadableTextFile(fileLocation, pPlaylistTableModel.data(), true);
} else {
ParserCsv::writeReadableTextFile(fileLocation, pPlaylistTableModel.data(), false);
}
} else {
// Create and populate a list of files of the playlist
QList<QString> playlistItems;
int rows = pPlaylistTableModel->rowCount();
for (int i = 0; i < rows; ++i) {
QModelIndex index = pPlaylistTableModel->index(i, 0);
playlistItems << pPlaylistTableModel->getTrackLocation(index);
}
exportPlaylistItemsIntoFile(
fileLocation,
playlistItems,
useRelativePath);
}
}
void BasePlaylistFeature::slotExportTrackFiles() {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId == kInvalidPlaylistId) {
return;
}
QScopedPointer<PlaylistTableModel> pPlaylistTableModel(
new PlaylistTableModel(this,
m_pLibrary->trackCollectionManager(),
"mixxx.db.model.playlist_export"));
emit saveModelState();
pPlaylistTableModel->selectPlaylist(playlistId);
pPlaylistTableModel->setSort(pPlaylistTableModel->fieldIndex(
ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION),
Qt::AscendingOrder);
pPlaylistTableModel->select();
int rows = pPlaylistTableModel->rowCount();
TrackPointerList tracks;
for (int i = 0; i < rows; ++i) {
QModelIndex index = pPlaylistTableModel->index(i, 0);
tracks.push_back(pPlaylistTableModel->getTrack(index));
}
TrackExportWizard track_export(nullptr, m_pConfig, tracks);
track_export.exportTracks();
}
void BasePlaylistFeature::slotAddToAutoDJ() {
//qDebug() << "slotAddToAutoDJ() row:" << m_lastRightClickedIndex.data();
addToAutoDJ(PlaylistDAO::AutoDJSendLoc::BOTTOM);
}
void BasePlaylistFeature::slotAddToAutoDJTop() {
//qDebug() << "slotAddToAutoDJTop() row:" << m_lastRightClickedIndex.data();
addToAutoDJ(PlaylistDAO::AutoDJSendLoc::TOP);
}
void BasePlaylistFeature::slotAddToAutoDJReplace() {
//qDebug() << "slotAddToAutoDJReplace() row:" << m_lastRightClickedIndex.data();
addToAutoDJ(PlaylistDAO::AutoDJSendLoc::REPLACE);
}
void BasePlaylistFeature::addToAutoDJ(PlaylistDAO::AutoDJSendLoc loc) {
//qDebug() << "slotAddToAutoDJ() row:" << m_lastRightClickedIndex.data();
if (m_lastRightClickedIndex.isValid()) {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId >= 0) {
// Insert this playlist
m_playlistDao.addPlaylistToAutoDJQueue(playlistId, loc);
}
}
}
void BasePlaylistFeature::slotAnalyzePlaylist() {
if (m_lastRightClickedIndex.isValid()) {
int playlistId = playlistIdFromIndex(m_lastRightClickedIndex);
if (playlistId >= 0) {
QList<TrackId> ids = m_playlistDao.getTrackIds(playlistId);
QList<AnalyzerScheduledTrack> tracks;
for (auto id : ids) {
tracks.append(id);
}
emit analyzeTracks(tracks);
}
}
}
TreeItemModel* BasePlaylistFeature::sidebarModel() const {
return m_pSidebarModel;
}
void BasePlaylistFeature::bindLibraryWidget(WLibrary* libraryWidget,
KeyboardEventFilter* keyboard) {
Q_UNUSED(keyboard);
WLibraryTextBrowser* edit = new WLibraryTextBrowser(libraryWidget);
edit->setHtml(getRootViewHtml());
edit->setOpenLinks(false);
connect(edit,
&WLibraryTextBrowser::anchorClicked,
this,
&BasePlaylistFeature::htmlLinkClicked);
libraryWidget->registerView(m_rootViewName, edit);
}
void BasePlaylistFeature::bindSidebarWidget(WLibrarySidebar* pSidebarWidget) {
// store the sidebar widget pointer for later use in onRightClickChild
DEBUG_ASSERT(!m_pSidebarWidget);
m_pSidebarWidget = pSidebarWidget;
}
void BasePlaylistFeature::htmlLinkClicked(const QUrl& link) {
if (QString(link.path()) == "create") {
slotCreatePlaylist();
} else {
qDebug() << "Unknown playlist link clicked" << link.path();
}
}
QString BasePlaylistFeature::fetchPlaylistLabel(int playlistId) {
// This queries the temporary id/count/duration table that was has been created
// by the features' createPlaylistLabels() (updated each time playlists are added/removed)
QSqlDatabase database =
m_pLibrary->trackCollectionManager()->internalCollection()->database();
VERIFY_OR_DEBUG_ASSERT(database.tables(QSql::Views).contains(m_countsDurationTableName)) {
qWarning() << "BasePlaylistFeature: view" << m_countsDurationTableName
<< "does not exist! Can't fetch label for playlist" << playlistId;
return QString();
}
QSqlTableModel playlistTableModel(this, database);
playlistTableModel.setTable(m_countsDurationTableName);
const QString filter = "id=" + QString::number(playlistId);
playlistTableModel.setFilter(filter);
playlistTableModel.select();
while (playlistTableModel.canFetchMore()) {
playlistTableModel.fetchMore();
}
QSqlRecord record = playlistTableModel.record();
int nameColumn = record.indexOf("name");
int countColumn = record.indexOf("count");
int durationColumn = record.indexOf("durationSeconds");
DEBUG_ASSERT(playlistTableModel.rowCount() <= 1);
if (playlistTableModel.rowCount() > 0) {
QString name =
playlistTableModel.data(playlistTableModel.index(0, nameColumn))
.toString();
int count = playlistTableModel
.data(playlistTableModel.index(0, countColumn))
.toInt();
int duration =
playlistTableModel
.data(playlistTableModel.index(0, durationColumn))
.toInt();
return createPlaylistLabel(name, count, duration);
}
return QString();
}
void BasePlaylistFeature::updateChildModel(const QSet<int>& playlistIds) {
// qDebug() << "BasePlaylistFeature::updateChildModel() for"
// << playlistIds.count() << "playlist(s)";
if (playlistIds.isEmpty()) {
return;
}
int id = kInvalidPlaylistId;
QString label;
bool ok = false;
for (int row = 0; row < m_pSidebarModel->rowCount(); ++row) {
QModelIndex index = m_pSidebarModel->index(row, 0);
TreeItem* pTreeItem = m_pSidebarModel->getItem(index);
DEBUG_ASSERT(pTreeItem != nullptr);
if (pTreeItem->hasChildren()) {
for (TreeItem* pChild : pTreeItem->children()) {
id = pChild->getData().toInt(&ok);
if (ok && id != kInvalidPlaylistId && playlistIds.contains(id)) {
label = fetchPlaylistLabel(id);
pChild->setLabel(label);
decorateChild(pChild, id);
markTreeItem(pChild);
}
}
} else {
id = pTreeItem->getData().toInt(&ok);
if (ok && id != kInvalidPlaylistId && playlistIds.contains(id)) {
label = fetchPlaylistLabel(id);
pTreeItem->setLabel(label);
decorateChild(pTreeItem, id);
markTreeItem(pTreeItem);
}
}
}
m_pSidebarModel->triggerRepaint();
}
/// Clears the child model dynamically, but the invisible root item remains
void BasePlaylistFeature::clearChildModel() {
m_lastClickedIndex = QModelIndex();
m_lastRightClickedIndex = QModelIndex();
m_pSidebarModel->removeRows(0, m_pSidebarModel->rowCount());
}
QModelIndex BasePlaylistFeature::indexFromPlaylistId(int playlistId) {
QVariant variantId = QVariant(playlistId);
QModelIndexList results = m_pSidebarModel->match(
m_pSidebarModel->getRootIndex(),
TreeItemModel::kDataRole,
variantId,
1,
Qt::MatchWrap | Qt::MatchExactly | Qt::MatchRecursive);
if (!results.isEmpty()) {
return results.front();
}
return QModelIndex();
}
bool BasePlaylistFeature::isChildIndexSelectedInSidebar(const QModelIndex& index) {
return m_pSidebarWidget && m_pSidebarWidget->isChildIndexSelected(index);
};
void BasePlaylistFeature::slotTrackSelected(TrackId trackId) {
m_selectedTrackId = trackId;
m_playlistDao.getPlaylistsTrackIsIn(m_selectedTrackId, &m_playlistIdsOfSelectedTrack);
for (int row = 0; row < m_pSidebarModel->rowCount(); ++row) {
QModelIndex index = m_pSidebarModel->index(row, 0);
TreeItem* pTreeItem = m_pSidebarModel->getItem(index);
DEBUG_ASSERT(pTreeItem != nullptr);
markTreeItem(pTreeItem);
}
m_pSidebarModel->triggerRepaint();
}
void BasePlaylistFeature::markTreeItem(TreeItem* pTreeItem) {
bool ok;
int playlistId = pTreeItem->getData().toInt(&ok);
if (ok) {
bool shouldBold = m_playlistIdsOfSelectedTrack.contains(playlistId);
pTreeItem->setBold(shouldBold);
if (shouldBold && pTreeItem->hasParent()) {
TreeItem* item = pTreeItem;
// extra parents, because -Werror=parentheses
while ((item = item->parent())) {
item->setBold(true);
}
}
}
if (pTreeItem->hasChildren()) {
QList<TreeItem*> children = pTreeItem->children();
for (int i = 0; i < children.size(); i++) {
markTreeItem(children.at(i));
}
}
}
QString BasePlaylistFeature::createPlaylistLabel(const QString& name,
int count,
int duration) const {
// Show duration only if playlist has tracks
if (count > 0) {
return QStringLiteral("%1 (%2) %3")
.arg(name,
QString::number(count),
mixxx::Duration::formatTime(
duration, mixxx::Duration::Precision::SECONDS));
} else {
return QStringLiteral("%1 (%2)").arg(name,
QString::number(count));
}
}
void BasePlaylistFeature::slotResetSelectedTrack() {
slotTrackSelected(TrackId{});
}