strawberry-audio-player-win.../src/playlist/playlistitem.cpp

178 lines
5.3 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:10:03 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2021-07-11 19:57:18 +02:00
#include <memory>
2018-02-27 18:06:05 +01:00
#include <QtConcurrentRun>
#include <QFuture>
#include <QColor>
2018-02-27 18:06:05 +01:00
#include "core/sqlquery.h"
#include "core/song.h"
#include "collection/collectionplaylistitem.h"
#include "playlistitem.h"
#include "songplaylistitem.h"
2018-02-27 18:06:05 +01:00
2024-06-12 22:23:05 +02:00
#include "streaming/streamplaylistitem.h"
2021-07-11 07:39:27 +02:00
#include "radios/radioplaylistitem.h"
2018-08-09 18:10:03 +02:00
using std::make_shared;
2021-07-11 19:57:18 +02:00
PlaylistItemPtr PlaylistItem::NewFromSource(const Song::Source source) {
2018-02-27 18:06:05 +01:00
2018-09-08 12:38:02 +02:00
switch (source) {
2023-02-18 14:09:27 +01:00
case Song::Source::Collection:
return make_shared<CollectionPlaylistItem>();
2023-02-18 14:09:27 +01:00
case Song::Source::Subsonic:
case Song::Source::Tidal:
2022-06-04 15:51:35 +02:00
case Song::Source::Spotify:
2023-02-18 14:09:27 +01:00
case Song::Source::Qobuz:
2024-06-12 22:23:05 +02:00
return make_shared<StreamPlaylistItem>(source);
2023-02-18 14:09:27 +01:00
case Song::Source::Stream:
case Song::Source::RadioParadise:
case Song::Source::SomaFM:
return make_shared<RadioPlaylistItem>(source);
2023-02-18 14:09:27 +01:00
case Song::Source::LocalFile:
case Song::Source::CDDA:
case Song::Source::Device:
case Song::Source::Unknown:
break;
2018-09-08 12:38:02 +02:00
}
2018-08-09 18:10:03 +02:00
return make_shared<SongPlaylistItem>(source);
2018-02-27 18:06:05 +01:00
}
2021-07-11 19:57:18 +02:00
PlaylistItemPtr PlaylistItem::NewFromSong(const Song &song) {
2020-09-10 22:04:11 +02:00
switch (song.source()) {
2023-02-18 14:09:27 +01:00
case Song::Source::Collection:
return make_shared<CollectionPlaylistItem>(song);
2023-02-18 14:09:27 +01:00
case Song::Source::Subsonic:
case Song::Source::Tidal:
2022-06-04 15:51:35 +02:00
case Song::Source::Spotify:
2023-02-18 14:09:27 +01:00
case Song::Source::Qobuz:
2024-06-12 22:23:05 +02:00
return make_shared<StreamPlaylistItem>(song);
2023-02-18 14:09:27 +01:00
case Song::Source::Stream:
case Song::Source::RadioParadise:
case Song::Source::SomaFM:
return make_shared<RadioPlaylistItem>(song);
2023-02-18 14:09:27 +01:00
case Song::Source::LocalFile:
case Song::Source::CDDA:
case Song::Source::Device:
case Song::Source::Unknown:
2020-09-10 22:04:11 +02:00
break;
}
return make_shared<SongPlaylistItem>(song);
2020-09-10 22:04:11 +02:00
}
2021-06-21 15:38:58 +02:00
PlaylistItem::~PlaylistItem() = default;
2020-09-10 22:04:11 +02:00
void PlaylistItem::BindToQuery(SqlQuery *query) const {
2018-02-27 18:06:05 +01:00
2024-04-09 23:20:26 +02:00
query->BindValue(QStringLiteral(":type"), static_cast<int>(source_));
query->BindValue(QStringLiteral(":collection_id"), DatabaseValue(Column_CollectionId));
2018-02-27 18:06:05 +01:00
DatabaseSongMetadata().BindToQuery(query);
}
void PlaylistItem::SetTemporaryMetadata(const Song &metadata) {
temp_metadata_ = metadata;
}
void PlaylistItem::UpdateTemporaryMetadata(const Song &metadata) {
if (!temp_metadata_.is_valid()) return;
Song old_metadata = temp_metadata_;
temp_metadata_ = metadata;
// Keep samplerate, bitdepth and bitrate from the old metadata if it's not present in the new.
if (temp_metadata_.samplerate() <= 0 && old_metadata.samplerate() > 0) temp_metadata_.set_samplerate(old_metadata.samplerate());
if (temp_metadata_.bitdepth() <= 0 && old_metadata.bitdepth() > 0) temp_metadata_.set_bitdepth(old_metadata.bitdepth());
if (temp_metadata_.bitrate() <= 0 && old_metadata.bitrate() > 0) temp_metadata_.set_bitrate(old_metadata.bitrate());
}
2018-02-27 18:06:05 +01:00
void PlaylistItem::ClearTemporaryMetadata() {
temp_metadata_ = Song();
}
static void ReloadPlaylistItem(PlaylistItemPtr item) {
item->Reload();
}
QFuture<void> PlaylistItem::BackgroundReload() {
return QtConcurrent::run(ReloadPlaylistItem, shared_from_this());
}
void PlaylistItem::SetBackgroundColor(short priority, const QColor &color) {
background_colors_[priority] = color;
}
bool PlaylistItem::HasBackgroundColor(short priority) const {
return background_colors_.contains(priority);
}
void PlaylistItem::RemoveBackgroundColor(short priority) {
background_colors_.remove(priority);
}
QColor PlaylistItem::GetCurrentBackgroundColor() const {
2021-03-21 04:47:11 +01:00
2021-08-23 21:21:08 +02:00
if (background_colors_.isEmpty()) {
return QColor();
}
2024-06-12 17:38:58 +02:00
QList<short> background_colors_keys = background_colors_.keys();
return background_colors_[background_colors_keys.last()];
2021-03-21 04:47:11 +01:00
2018-02-27 18:06:05 +01:00
}
bool PlaylistItem::HasCurrentBackgroundColor() const {
return !background_colors_.isEmpty();
}
2021-06-20 19:04:08 +02:00
void PlaylistItem::SetForegroundColor(const short priority, const QColor &color) {
2018-02-27 18:06:05 +01:00
foreground_colors_[priority] = color;
}
2021-06-20 19:04:08 +02:00
bool PlaylistItem::HasForegroundColor(const short priority) const {
2018-02-27 18:06:05 +01:00
return foreground_colors_.contains(priority);
}
2021-06-20 19:04:08 +02:00
void PlaylistItem::RemoveForegroundColor(const short priority) {
2018-02-27 18:06:05 +01:00
foreground_colors_.remove(priority);
}
QColor PlaylistItem::GetCurrentForegroundColor() const {
2021-03-21 04:47:11 +01:00
if (foreground_colors_.isEmpty()) return QColor();
2024-06-12 17:38:58 +02:00
QList<short> foreground_colors_keys = foreground_colors_.keys();
return foreground_colors_[foreground_colors_keys.last()];
2021-03-21 04:47:11 +01:00
2018-02-27 18:06:05 +01:00
}
bool PlaylistItem::HasCurrentForegroundColor() const {
return !foreground_colors_.isEmpty();
}
2021-06-20 19:04:08 +02:00
void PlaylistItem::SetShouldSkip(const bool val) { should_skip_ = val; }
2018-02-27 18:06:05 +01:00
bool PlaylistItem::GetShouldSkip() const { return should_skip_; }