~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/playlist/playlistmanager.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "playlist.h"
19
19
#include "playlistbackend.h"
 
20
#include "playlistcontainer.h"
20
21
#include "playlistmanager.h"
 
22
#include "playlistview.h"
 
23
#include "specialplaylisttype.h"
 
24
#include "core/logging.h"
21
25
#include "core/songloader.h"
22
26
#include "core/utilities.h"
23
27
#include "library/librarybackend.h"
37
41
    library_backend_(NULL),
38
42
    sequence_(NULL),
39
43
    parser_(NULL),
 
44
    default_playlist_type_(new DefaultPlaylistType),
40
45
    current_(-1),
41
46
    active_(-1)
42
47
{
46
51
  foreach (const Data& data, playlists_.values()) {
47
52
    delete data.p;
48
53
  }
 
54
 
 
55
  qDeleteAll(special_playlist_types_.values());
 
56
  delete default_playlist_type_;
49
57
}
50
58
 
51
59
void PlaylistManager::Init(LibraryBackend* library_backend,
52
60
                           PlaylistBackend* playlist_backend,
53
 
                           PlaylistSequence* sequence) {
 
61
                           PlaylistSequence* sequence,
 
62
                           PlaylistContainer* playlist_container) {
54
63
  library_backend_ = library_backend;
55
64
  playlist_backend_ = playlist_backend;
56
65
  sequence_ = sequence;
57
66
  parser_ = new PlaylistParser(library_backend, this);
 
67
  playlist_container_ = playlist_container;
58
68
 
59
69
  connect(library_backend_, SIGNAL(SongsDiscovered(SongList)), SLOT(SongsDiscovered(SongList)));
60
70
  connect(library_backend_, SIGNAL(SongsStatisticsChanged(SongList)), SLOT(SongsDiscovered(SongList)));
61
71
 
62
72
  foreach (const PlaylistBackend::Playlist& p, playlist_backend->GetAllPlaylists()) {
63
 
    AddPlaylist(p.id, p.name);
 
73
    AddPlaylist(p.id, p.name, p.special_type);
64
74
  }
65
75
 
66
76
  // If no playlist exists then make a new one
80
90
  return result;
81
91
}
82
92
 
83
 
const QItemSelection& PlaylistManager::selection(int id) const {
 
93
QItemSelection PlaylistManager::selection(int id) const {
84
94
  QMap<int, Data>::const_iterator it = playlists_.find(id);
85
95
  return it->selection;
86
96
}
87
97
 
88
 
Playlist* PlaylistManager::AddPlaylist(int id, const QString& name) {
89
 
  Playlist* ret = new Playlist(playlist_backend_, task_manager_, library_backend_, id);
 
98
Playlist* PlaylistManager::AddPlaylist(int id, const QString& name,
 
99
                                       const QString& special_type) {
 
100
  Playlist* ret = new Playlist(playlist_backend_, task_manager_, library_backend_, id, special_type);
90
101
  ret->set_sequence(sequence_);
91
102
 
92
103
  connect(ret, SIGNAL(CurrentSongChanged(Song)), SIGNAL(CurrentSongChanged(Song)));
95
106
  connect(ret, SIGNAL(EditingFinished(QModelIndex)), SIGNAL(EditingFinished(QModelIndex)));
96
107
  connect(ret, SIGNAL(LoadTracksError(QString)), SIGNAL(Error(QString)));
97
108
  connect(ret, SIGNAL(PlayRequested(QModelIndex)), SIGNAL(PlayRequested(QModelIndex)));
 
109
  connect(playlist_container_->view(), SIGNAL(ColumnAlignmentChanged(ColumnAlignmentMap)),
 
110
          ret, SLOT(SetColumnAlignment(ColumnAlignmentMap)));
98
111
 
99
112
  playlists_[id] = Data(ret, name);
100
113
 
110
123
  return ret;
111
124
}
112
125
 
113
 
void PlaylistManager::New(const QString& name, const SongList& songs) {
 
126
void PlaylistManager::New(const QString& name, const SongList& songs,
 
127
                          const QString& special_type) {
114
128
  if (name.isNull())
115
129
    return;
116
130
 
117
 
  int id = playlist_backend_->CreatePlaylist(name);
 
131
  int id = playlist_backend_->CreatePlaylist(name, special_type);
118
132
 
119
133
  if (id == -1)
120
134
    qFatal("Couldn't create playlist");
121
135
 
122
 
  Playlist* playlist = AddPlaylist(id, name);
 
136
  Playlist* playlist = AddPlaylist(id, name, special_type);
123
137
  playlist->InsertSongsOrLibraryItems(songs);
124
138
 
125
139
  SetCurrentPlaylist(id);
347
361
    playlist->InvalidateDeletedSongs();
348
362
  }
349
363
}
 
364
 
 
365
void PlaylistManager::RemoveDeletedSongs() {
 
366
  foreach(Playlist* playlist, GetAllPlaylists()) {
 
367
    playlist->RemoveDeletedSongs();
 
368
  }
 
369
}
 
370
 
 
371
QString PlaylistManager::GetNameForNewPlaylist(const SongList& songs) {
 
372
  if (songs.isEmpty()) {
 
373
    return tr("Playlist");
 
374
  }
 
375
 
 
376
  QSet<QString> artists;
 
377
  QSet<QString> albums;
 
378
 
 
379
  foreach(const Song& song, songs) {
 
380
    artists << (song.artist().isEmpty()
 
381
                    ? tr("Unknown")
 
382
                    : song.artist());
 
383
    albums << (song.album().isEmpty()
 
384
                    ? tr("Unknown")
 
385
                    : song.album());
 
386
 
 
387
    if(artists.size() > 1) {
 
388
      break;
 
389
    }
 
390
  }
 
391
 
 
392
  bool various_artists = artists.size() > 1;
 
393
 
 
394
  QString result;
 
395
  if(various_artists) {
 
396
    result = tr("Various artists");
 
397
  } else {
 
398
    result = artists.values().first();
 
399
  }
 
400
 
 
401
  if(!various_artists && albums.size() == 1) {
 
402
    result += " - " + albums.toList().first();
 
403
  }
 
404
 
 
405
  return result;
 
406
}
 
407
 
 
408
void PlaylistManager::RegisterSpecialPlaylistType(SpecialPlaylistType* type) {
 
409
  const QString name = type->name();
 
410
 
 
411
  if (special_playlist_types_.contains(name)) {
 
412
    qLog(Warning) << "Tried to register a special playlist type" << name
 
413
                  << "but one was already registered";
 
414
    return;
 
415
  }
 
416
 
 
417
  qLog(Info) << "Registered special playlist type" << name;
 
418
  special_playlist_types_.insert(name, type);
 
419
}
 
420
 
 
421
void PlaylistManager::UnregisterSpecialPlaylistType(SpecialPlaylistType* type) {
 
422
  const QString name = special_playlist_types_.key(type);
 
423
  if (name.isEmpty()) {
 
424
    qLog(Warning) << "Tried to unregister a special playlist type" << type->name()
 
425
                  << "that wasn't registered";
 
426
    return;
 
427
  }
 
428
 
 
429
  qLog(Info) << "Unregistered special playlist type" << name;
 
430
  special_playlist_types_.remove(name);
 
431
}
 
432
 
 
433
SpecialPlaylistType* PlaylistManager::GetPlaylistType(const QString& type) const {
 
434
  if (special_playlist_types_.contains(type)) {
 
435
    return special_playlist_types_[type];
 
436
  }
 
437
  return default_playlist_type_;
 
438
}