-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathplaylistmanager.cpp
More file actions
626 lines (494 loc) · 18.5 KB
/
playlistmanager.cpp
File metadata and controls
626 lines (494 loc) · 18.5 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
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "playlistmanager.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QFuture>
#include <QMessageBox>
#include <QtConcurrentRun>
#include <QtDebug>
#include "core/application.h"
#include "core/logging.h"
#include "core/player.h"
#include "core/songloader.h"
#include "core/utilities.h"
#include "library/librarybackend.h"
#include "library/libraryplaylistitem.h"
#include "playlistbackend.h"
#include "playlistcontainer.h"
#include "playlistparsers/playlistparser.h"
#include "playlistsaveoptionsdialog.h"
#include "playlistview.h"
#include "queue.h"
#include "smartplaylists/generator.h"
using smart_playlists::GeneratorPtr;
PlaylistManager::PlaylistManager(Application* app, QObject* parent)
: PlaylistManagerInterface(app, parent),
app_(app),
playlist_backend_(nullptr),
library_backend_(nullptr),
sequence_(nullptr),
parser_(nullptr),
playlist_container_(nullptr),
current_(-1),
active_(-1) {
connect(app_->player(), SIGNAL(Paused()), SLOT(SetActivePaused()));
connect(app_->player(), SIGNAL(Playing()), SLOT(SetActivePlaying()));
connect(app_->player(), SIGNAL(Stopped()), SLOT(SetActiveStopped()));
}
PlaylistManager::~PlaylistManager() {
for (const Data& data : playlists_.values()) {
delete data.p;
}
}
void PlaylistManager::Init(LibraryBackend* library_backend,
PlaylistBackend* playlist_backend,
PlaylistSequence* sequence,
PlaylistContainer* playlist_container) {
library_backend_ = library_backend;
playlist_backend_ = playlist_backend;
sequence_ = sequence;
parser_ = new PlaylistParser(library_backend, this);
playlist_container_ = playlist_container;
connect(library_backend_, SIGNAL(SongsDiscovered(SongList)),
SLOT(SongsDiscovered(SongList)));
connect(library_backend_, SIGNAL(SongsStatisticsChanged(SongList)),
SLOT(SongsDiscovered(SongList)));
connect(library_backend_, SIGNAL(SongsRatingChanged(SongList)),
SLOT(SongsDiscovered(SongList)));
connect(parser_, SIGNAL(Error(QString)), this, SIGNAL(Error(QString)));
for (const PlaylistBackend::Playlist& p :
playlist_backend->GetAllOpenPlaylists()) {
AddPlaylist(p.id, p.name, p.special_type, p.ui_path, p.favorite);
}
// If no playlist exists then make a new one
if (playlists_.isEmpty()) New(tr("Playlist"));
emit PlaylistManagerInitialized();
}
QList<Playlist*> PlaylistManager::GetAllPlaylists() const {
QList<Playlist*> result;
for (const Data& data : playlists_.values()) {
result.append(data.p);
}
return result;
}
QList<int> PlaylistManager::GetAllPlaylistIds() const {
QList<int> result;
for (const int data : playlists_.keys()) {
result.append(data);
}
return result;
}
QItemSelection PlaylistManager::selection(int id) const {
QMap<int, Data>::const_iterator it = playlists_.find(id);
return it->selection;
}
Playlist* PlaylistManager::AddPlaylist(int id, const QString& name,
const QString& special_type,
const QString& ui_path, bool favorite) {
Playlist* ret = new Playlist(playlist_backend_, app_->task_manager(),
library_backend_, id, special_type, favorite);
ret->set_sequence(sequence_);
ret->set_ui_path(ui_path);
connect(ret, SIGNAL(CurrentSongChanged(Song)),
SIGNAL(CurrentSongChanged(Song)));
connect(ret, SIGNAL(PlaylistChanged()), SLOT(OneOfPlaylistsChanged()));
connect(ret, SIGNAL(PlaylistChanged()), SLOT(UpdateSummaryText()));
connect(ret, SIGNAL(EditingFinished(QModelIndex)),
SIGNAL(EditingFinished(QModelIndex)));
connect(ret, SIGNAL(Error(QString)), SIGNAL(Error(QString)));
connect(ret, SIGNAL(PlayRequested(QModelIndex)),
SIGNAL(PlayRequested(QModelIndex)));
connect(playlist_container_->view(),
SIGNAL(ColumnAlignmentChanged(ColumnAlignmentMap)), ret,
SLOT(SetColumnAlignment(ColumnAlignmentMap)));
playlists_[id] = Data(ret, name);
emit PlaylistAdded(id, name, favorite);
if (current_ == -1) {
SetCurrentPlaylist(id);
}
if (active_ == -1) {
SetActivePlaylist(id);
}
return ret;
}
int PlaylistManager::New(const QString& name, const SongList& songs,
const QString& special_type) {
if (name.isNull()) return -1;
int id = playlist_backend_->CreatePlaylist(name, special_type);
if (id == -1) qFatal("Couldn't create playlist");
Playlist* playlist = AddPlaylist(id, name, special_type, QString(), false);
playlist->InsertSongsOrLibraryItems(songs);
SetCurrentPlaylist(id);
// If the name is just "Playlist", append the id
if (name == tr("Playlist")) {
Rename(id, QString("%1 %2").arg(name).arg(id));
}
return id;
}
void PlaylistManager::Load(const QString& filename) {
QFileInfo info(filename);
int id = playlist_backend_->CreatePlaylist(info.baseName(), QString());
if (id == -1) {
emit Error(tr("Couldn't create playlist"));
return;
}
Playlist* playlist =
AddPlaylist(id, info.baseName(), QString(), QString(), false);
QList<QUrl> urls;
playlist->InsertUrls(urls << QUrl::fromLocalFile(filename));
}
void PlaylistManager::Save(int id, const QString& filename,
Playlist::Path path_type) {
if (playlists_.contains(id)) {
parser_->Save(playlist(id)->GetAllSongs(), filename, path_type);
} else {
// Playlist is not in the playlist manager: probably save action was
// triggered
// from the left side bar and the playlist isn't loaded.
QFuture<QList<Song>> future = QtConcurrent::run(
playlist_backend_, &PlaylistBackend::GetPlaylistSongs, id);
NewClosure(future, this,
SLOT(ItemsLoadedForSavePlaylist(QFuture<SongList>, QString,
Playlist::Path)),
future, filename, path_type);
}
}
int PlaylistManager::LoadBulkPlaylists(const QString& filename, const QString& ui_path){
QFileInfo info(filename);
int id = playlist_backend_->CreatePlaylist(info.baseName(), QString());
if (id == -1) {
emit Error(tr("Couldn't create playlist"));
return id;
}
Playlist* playlist =
AddPlaylist(id, info.baseName(), QString(), ui_path, false);
QList<QUrl> urls;
playlist->InsertUrls(urls << QUrl::fromLocalFile(filename));
return id;
}
void PlaylistManager::BulkImportPlaylistsCallback(int id){
if (playlists_.contains(id)) {
// If playlists_ contains this playlist, its means it's opened: star or
// unstar it.
bool favorite = true;
playlist_backend_->FavoritePlaylist(id, favorite);
playlists_[id].p->set_favorite(favorite);
}
}
void PlaylistManager::ItemsLoadedForSavePlaylist(QFuture<SongList> future,
const QString& filename,
Playlist::Path path_type) {
parser_->Save(future.result(), filename, path_type);
}
void PlaylistManager::SaveWithUI(int id, const QString& playlist_name) {
QSettings settings;
settings.beginGroup(Playlist::kSettingsGroup);
QString filename = settings.value("last_save_playlist").toString();
QString extension =
settings.value("last_save_extension", parser()->default_extension())
.toString();
QString filter =
settings.value("last_save_filter", parser()->default_filter()).toString();
QString suggested_filename = playlist_name;
suggested_filename.replace(QRegExp("\\W"), "");
qLog(Debug) << "Using extension:" << extension;
// We want to use the playlist tab name (with disallowed characters removed)
// as a default filename, but in the same directory as the last saved file.
// Strip off filename components until we find something that's a folder
forever {
QFileInfo fileinfo(filename);
if (filename.isEmpty() || fileinfo.isDir()) break;
filename = filename.section('/', 0, -2);
}
// Use the home directory as a fallback in case the path is empty.
if (filename.isEmpty()) filename = QDir::homePath();
// Add the suggested filename
filename += "/" + suggested_filename + "." + extension;
qLog(Debug) << "Suggested filename:" << filename;
filename = QFileDialog::getSaveFileName(
nullptr, tr("Save playlist", "Title of the playlist save dialog."),
filename, parser()->filters(), &filter);
if (filename.isNull()) {
return;
}
// Check if the file extension is valid. Fallback to the default if not.
QFileInfo info(filename);
ParserBase* parser = parser_->ParserForExtension(info.suffix());
if (!parser) {
qLog(Warning) << "Unknown file extension:" << info.suffix();
filename = info.absolutePath() + "/" + info.fileName() + "." +
parser_->default_extension();
info.setFile(filename);
filter = info.suffix();
}
int p = settings.value(Playlist::kPathType, Playlist::Path_Automatic).toInt();
Playlist::Path path = static_cast<Playlist::Path>(p);
if (path == Playlist::Path_Ask_User) {
PlaylistSaveOptionsDialog optionsDialog(nullptr);
optionsDialog.setModal(true);
if (optionsDialog.exec() != QDialog::Accepted) {
return;
}
path = optionsDialog.path_type();
}
settings.setValue("last_save_playlist", filename);
settings.setValue("last_save_filter", filter);
settings.setValue("last_save_extension", info.suffix());
Save(id == -1 ? current_id() : id, filename, path);
}
void PlaylistManager::Rename(int id, const QString& new_name) {
Q_ASSERT(playlists_.contains(id));
playlist_backend_->RenamePlaylist(id, new_name);
playlists_[id].name = new_name;
emit PlaylistRenamed(id, new_name);
}
void PlaylistManager::Favorite(int id, bool favorite) {
if (playlists_.contains(id)) {
// If playlists_ contains this playlist, its means it's opened: star or
// unstar it.
playlist_backend_->FavoritePlaylist(id, favorite);
playlists_[id].p->set_favorite(favorite);
} else {
Q_ASSERT(!favorite);
// Otherwise it means user wants to remove this playlist from the left
// panel,
// while it's not visible in the playlist tabbar either, because it has been
// closed: delete it.
playlist_backend_->RemovePlaylist(id);
}
emit PlaylistFavorited(id, favorite);
}
void PlaylistManager::Clear(int id) {
if (playlists_.count() <= 1 || !playlists_.contains(id)) return;
playlists_[id].p->Clear();
}
bool PlaylistManager::Close(int id) {
// Won't allow removing the last playlist
if (playlists_.count() <= 1 || !playlists_.contains(id)) return false;
int next_id = -1;
for (int possible_next_id : playlists_.keys()) {
if (possible_next_id != id) {
next_id = possible_next_id;
break;
}
}
if (next_id == -1) return false;
if (id == active_) SetActivePlaylist(next_id);
if (id == current_) SetCurrentPlaylist(next_id);
Data data = playlists_.take(id);
emit PlaylistClosed(id);
if (!data.p->is_favorite()) {
playlist_backend_->RemovePlaylist(id);
emit PlaylistDeleted(id);
}
delete data.p;
return true;
}
void PlaylistManager::Delete(int id) {
if (!Close(id)) {
return;
}
playlist_backend_->RemovePlaylist(id);
emit PlaylistDeleted(id);
}
void PlaylistManager::OneOfPlaylistsChanged() {
emit PlaylistChanged(qobject_cast<Playlist*>(sender()));
}
void PlaylistManager::SetCurrentPlaylist(int id) {
Q_ASSERT(playlists_.contains(id));
current_ = id;
emit CurrentChanged(current());
UpdateSummaryText();
}
void PlaylistManager::SetActivePlaylist(int id) {
Q_ASSERT(playlists_.contains(id));
// Kinda a hack: unset the current item from the old active playlist before
// setting the new one
if (active_ != -1 && active_ != id) active()->set_current_row(-1);
active_ = id;
emit ActiveChanged(active());
sequence_->SetUsingDynamicPlaylist(active()->is_dynamic());
}
void PlaylistManager::SetActiveToCurrent() {
// Check if we need to update the active playlist.
// By calling SetActiveToCurrent, the playlist manager emits the signal
// "ActiveChanged". This signal causes the network remote module to
// send all playlists to the clients, even if no change happened.
if (current_id() != active_id()) {
SetActivePlaylist(current_id());
}
}
void PlaylistManager::ClearCurrent() { current()->Clear(); }
void PlaylistManager::ShuffleCurrent() { current()->Shuffle(); }
void PlaylistManager::RemoveDuplicatesCurrent() {
current()->RemoveDuplicateSongs();
}
void PlaylistManager::RemoveUnavailableCurrent() {
current()->RemoveUnavailableSongs();
}
void PlaylistManager::SetActivePlaying() { active()->Playing(); }
void PlaylistManager::SetActivePaused() { active()->Paused(); }
void PlaylistManager::SetActiveStopped() { active()->Stopped(); }
void PlaylistManager::SetActiveStreamMetadata(const QUrl& url,
const Song& song) {
active()->SetStreamMetadata(url, song);
}
void PlaylistManager::RateCurrentSong(double rating) {
active()->RateSong(active()->current_index(), rating);
}
void PlaylistManager::RateCurrentSong(int rating) {
RateCurrentSong(rating / 5.0);
}
void PlaylistManager::ChangePlaylistOrder(const QList<int>& ids) {
playlist_backend_->SetPlaylistOrder(ids);
}
void PlaylistManager::Enque(int id, int i) {
QModelIndexList dummyIndexList;
Q_ASSERT(playlists_.contains(id));
dummyIndexList.append(playlist(id)->index(i, 0));
playlist(id)->queue()->ToggleTracks(dummyIndexList);
}
void PlaylistManager::UpdateSummaryText() {
int tracks = current()->rowCount();
quint64 nanoseconds = 0;
int selected = 0;
// Get the length of the selected tracks
for (const QItemSelectionRange& range : playlists_[current_id()].selection) {
if (!range.isValid()) continue;
selected += range.bottom() - range.top() + 1;
for (int i = range.top(); i <= range.bottom(); ++i) {
qint64 length =
range.model()->index(i, Playlist::Column_Length).data().toLongLong();
if (length > 0) nanoseconds += length;
}
}
QString summary;
if (selected > 1) {
summary += tr("%L1 selected of").arg(selected) + " ";
} else {
nanoseconds = current()->GetTotalLength();
}
// TODO: Make the plurals translatable
summary += tracks == 1 ? tr("1 track") : tr("%L1 tracks").arg(tracks);
if (nanoseconds)
summary += " - [ " + Utilities::WordyTimeNanosec(nanoseconds) + " ]";
emit SummaryTextChanged(summary);
}
void PlaylistManager::SelectionChanged(const QItemSelection& selection) {
playlists_[current_id()].selection = selection;
UpdateSummaryText();
}
void PlaylistManager::SongsDiscovered(const SongList& songs) {
// Some songs might've changed in the library, let's update any playlist
// items we have that match those songs
for (const Song& song : songs) {
for (const Data& data : playlists_) {
PlaylistItemList items = data.p->library_items_by_id(song.id());
for (PlaylistItemPtr item : items) {
if (item->Metadata().directory_id() != song.directory_id()) continue;
static_cast<LibraryPlaylistItem*>(item.get())->SetMetadata(song);
data.p->ItemChanged(item);
}
}
}
}
void PlaylistManager::PlaySmartPlaylist(GeneratorPtr generator, bool as_new,
bool clear) {
if (as_new) {
New(generator->name());
}
if (clear) {
current()->Clear();
}
current()->InsertSmartPlaylist(generator);
}
// When Player has processed the new song chosen by the user...
void PlaylistManager::SongChangeRequestProcessed(const QUrl& url, bool valid) {
for (Playlist* playlist : GetAllPlaylists()) {
if (playlist->ApplyValidityOnCurrentSong(url, valid)) {
return;
}
}
}
void PlaylistManager::InsertUrls(int id, const QList<QUrl>& urls, int pos,
bool play_now, bool enqueue) {
Q_ASSERT(playlists_.contains(id));
if (play_now && active_ != id) SetActivePlaylist(id);
playlists_[id].p->InsertUrls(urls, pos, play_now, enqueue);
}
void PlaylistManager::InsertSongs(int id, const SongList& songs, int pos,
bool play_now, bool enqueue) {
Q_ASSERT(playlists_.contains(id));
if (play_now && active_ != id) SetActivePlaylist(id);
playlists_[id].p->InsertSongs(songs, pos, play_now, enqueue);
}
void PlaylistManager::RemoveItemsWithoutUndo(int id,
const QList<int>& indices) {
Q_ASSERT(playlists_.contains(id));
playlists_[id].p->RemoveItemsWithoutUndo(indices);
}
void PlaylistManager::RemoveCurrentSong() {
active()->removeRows(active()->current_index().row(), 1);
}
void PlaylistManager::InvalidateDeletedSongs() {
for (Playlist* playlist : GetAllPlaylists()) {
playlist->InvalidateDeletedSongs();
}
}
void PlaylistManager::RemoveDeletedSongs() {
for (Playlist* playlist : GetAllPlaylists()) {
playlist->RemoveDeletedSongs();
}
}
QString PlaylistManager::GetNameForNewPlaylist(const SongList& songs) {
if (songs.isEmpty()) {
return tr("Playlist");
}
QSet<QString> artists;
QSet<QString> albums;
for (const Song& song : songs) {
artists << (song.artist().isEmpty() ? tr("Unknown") : song.artist());
albums << (song.album().isEmpty() ? tr("Unknown") : song.album());
if (artists.size() > 1) {
break;
}
}
bool various_artists = artists.size() > 1;
QString result;
if (various_artists) {
result = tr("Various artists");
} else {
result = artists.values().first();
}
if (!various_artists && albums.size() == 1) {
result += " - " + albums.values().first();
}
return result;
}
void PlaylistManager::Open(int id) {
if (playlists_.contains(id)) {
return;
}
const PlaylistBackend::Playlist& p = playlist_backend_->GetPlaylist(id);
if (p.id != id) {
return;
}
AddPlaylist(p.id, p.name, p.special_type, p.ui_path, p.favorite);
}
void PlaylistManager::SetCurrentOrOpen(int id) {
Open(id);
SetCurrentPlaylist(id);
}
bool PlaylistManager::IsPlaylistOpen(int id) { return playlists_.contains(id); }