1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "playlistitem.h"
19 #include "songplaylistitem.h"
20 #include "core/logging.h"
21 #include "core/song.h"
22 #include "internet/jamendo/jamendoplaylistitem.h"
23 #include "internet/jamendo/jamendoservice.h"
24 #include "internet/magnatune/magnatuneplaylistitem.h"
25 #include "internet/magnatune/magnatuneservice.h"
26 #include "internet/core/internetplaylistitem.h"
27 #include "library/library.h"
28 #include "library/libraryplaylistitem.h"
29 
30 #include <QSqlQuery>
31 #include <QtConcurrentRun>
32 #include <QtDebug>
33 
~PlaylistItem()34 PlaylistItem::~PlaylistItem() {}
35 
NewFromType(const QString & type)36 PlaylistItem* PlaylistItem::NewFromType(const QString& type) {
37   if (type == "Library") return new LibraryPlaylistItem(type);
38   if (type == "Magnatune") return new MagnatunePlaylistItem(type);
39   if (type == "Jamendo") return new JamendoPlaylistItem(type);
40   if (type == "Stream" || type == "File") return new SongPlaylistItem(type);
41   if (type == "Internet" || type == "Radio")
42     return new InternetPlaylistItem("Internet");
43 
44   qLog(Warning) << "Invalid PlaylistItem type:" << type;
45   return nullptr;
46 }
47 
NewFromSongsTable(const QString & table,const Song & song)48 PlaylistItem* PlaylistItem::NewFromSongsTable(const QString& table,
49                                               const Song& song) {
50   if (table == Library::kSongsTable) return new LibraryPlaylistItem(song);
51   if (table == MagnatuneService::kSongsTable)
52     return new MagnatunePlaylistItem(song);
53   if (table == JamendoService::kSongsTable)
54     return new JamendoPlaylistItem(song);
55 
56   qLog(Warning) << "Invalid PlaylistItem songs table:" << table;
57   return nullptr;
58 }
59 
BindToQuery(QSqlQuery * query) const60 void PlaylistItem::BindToQuery(QSqlQuery* query) const {
61   query->bindValue(":type", type());
62   query->bindValue(":library_id", DatabaseValue(Column_LibraryId));
63   query->bindValue(":radio_service", DatabaseValue(Column_InternetService));
64 
65   DatabaseSongMetadata().BindToQuery(query);
66 }
67 
SetTemporaryMetadata(const Song & metadata)68 void PlaylistItem::SetTemporaryMetadata(const Song& metadata) {
69   temp_metadata_ = metadata;
70   temp_metadata_.set_filetype(Song::Type_Stream);
71 }
72 
ClearTemporaryMetadata()73 void PlaylistItem::ClearTemporaryMetadata() { temp_metadata_ = Song(); }
74 
ReloadPlaylistItem(PlaylistItemPtr item)75 static void ReloadPlaylistItem(PlaylistItemPtr item) { item->Reload(); }
76 
BackgroundReload()77 QFuture<void> PlaylistItem::BackgroundReload() {
78   return QtConcurrent::run(ReloadPlaylistItem, shared_from_this());
79 }
80 
SetBackgroundColor(short priority,const QColor & color)81 void PlaylistItem::SetBackgroundColor(short priority, const QColor& color) {
82   background_colors_[priority] = color;
83 }
HasBackgroundColor(short priority) const84 bool PlaylistItem::HasBackgroundColor(short priority) const {
85   return background_colors_.contains(priority);
86 }
RemoveBackgroundColor(short priority)87 void PlaylistItem::RemoveBackgroundColor(short priority) {
88   background_colors_.remove(priority);
89 }
GetCurrentBackgroundColor() const90 QColor PlaylistItem::GetCurrentBackgroundColor() const {
91   return background_colors_.isEmpty()
92              ? QColor()
93              : background_colors_[background_colors_.keys().last()];
94 }
HasCurrentBackgroundColor() const95 bool PlaylistItem::HasCurrentBackgroundColor() const {
96   return !background_colors_.isEmpty();
97 }
98 
SetForegroundColor(short priority,const QColor & color)99 void PlaylistItem::SetForegroundColor(short priority, const QColor& color) {
100   foreground_colors_[priority] = color;
101 }
HasForegroundColor(short priority) const102 bool PlaylistItem::HasForegroundColor(short priority) const {
103   return foreground_colors_.contains(priority);
104 }
RemoveForegroundColor(short priority)105 void PlaylistItem::RemoveForegroundColor(short priority) {
106   foreground_colors_.remove(priority);
107 }
GetCurrentForegroundColor() const108 QColor PlaylistItem::GetCurrentForegroundColor() const {
109   return foreground_colors_.isEmpty()
110              ? QColor()
111              : foreground_colors_[foreground_colors_.keys().last()];
112 }
HasCurrentForegroundColor() const113 bool PlaylistItem::HasCurrentForegroundColor() const {
114   return !foreground_colors_.isEmpty();
115 }
SetShouldSkip(bool val)116 void PlaylistItem::SetShouldSkip(bool val) { should_skip_ = val; }
GetShouldSkip() const117 bool PlaylistItem::GetShouldSkip() const { return should_skip_; }
118