Add source to songs and playlist_items

This commit is contained in:
Jonas Kvinge
2018-09-08 12:38:02 +02:00
parent 19b645d731
commit 6d686ee66a
48 changed files with 500 additions and 401 deletions

View File

@@ -32,13 +32,13 @@
#include "internetservice.h"
#include "tidal/tidalservice.h"
QMap<QString, InternetService*>* InternetModel::sServices = nullptr;
QMap<Song::Source, InternetService*>* InternetModel::sServices = nullptr;
InternetModel::InternetModel(Application *app, QObject *parent)
: QStandardItemModel(parent),
app_(app) {
if (!sServices) sServices = new QMap<QString, InternetService*>;
if (!sServices) sServices = new QMap<Song::Source, InternetService*>;
Q_ASSERT(sServices->isEmpty());
AddService(new TidalService(app, this));
@@ -47,7 +47,7 @@ InternetModel::InternetModel(Application *app, QObject *parent)
void InternetModel::AddService(InternetService *service) {
qLog(Debug) << "Adding internet service:" << service->name();
sServices->insert(service->name(), service);
sServices->insert(service->source(), service);
connect(service, SIGNAL(destroyed()), SLOT(ServiceDeleted()));
if (service->has_initial_load_settings()) service->InitialLoadSettings();
else service->ReloadSettings();
@@ -56,8 +56,8 @@ void InternetModel::AddService(InternetService *service) {
void InternetModel::RemoveService(InternetService *service) {
if (!sServices->contains(service->name())) return;
sServices->remove(service->name());
if (!sServices->contains(service->source())) return;
sServices->remove(service->source());
disconnect(service, 0, this, 0);
}
@@ -69,9 +69,9 @@ void InternetModel::ServiceDeleted() {
}
InternetService *InternetModel::ServiceByName(const QString &name) {
InternetService *InternetModel::ServiceBySource(const Song::Source &source) {
if (sServices->contains(name)) return sServices->value(name);
if (sServices->contains(source)) return sServices->value(source);
return nullptr;
}

View File

@@ -105,11 +105,13 @@ class InternetModel : public QStandardItemModel {
};
// Needs to be static for InternetPlaylistItem::restore
static InternetService *ServiceByName(const QString &name);
static InternetService *ServiceBySource(const Song::Source &source);
//static InternetService *ServiceByName(const QString &name);
template <typename T>
static T *Service() {
return static_cast<T*>(ServiceByName(T::kServiceName));
//return static_cast<T*>(ServiceByName(T::kServiceName));
return static_cast<T*>(ServiceBySource(T::kSource));
}
// Add and remove services. Ownership is not transferred and the service is not reparented.
@@ -124,7 +126,8 @@ class InternetModel : public QStandardItemModel {
void ServiceDeleted();
private:
static QMap<QString, InternetService*> *sServices;
//static QMap<QString, InternetService*> *sServices;
static QMap<Song::Source, InternetService*> *sServices;
Application *app_;
};

View File

@@ -2,6 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -33,72 +34,39 @@
#include "collection/sqlrow.h"
#include "playlist/playlistbackend.h"
InternetPlaylistItem::InternetPlaylistItem(const QString &type)
: PlaylistItem(type), set_service_icon_(false) {}
InternetPlaylistItem::InternetPlaylistItem(const Song::Source &source)
: PlaylistItem(source) {}
InternetPlaylistItem::InternetPlaylistItem(InternetService *service, const Song &metadata)
: PlaylistItem("Internet"),
service_name_(service->name()),
set_service_icon_(false),
: PlaylistItem(Song::Source_Stream),
source_(service->source()),
metadata_(metadata) {
InitMetadata();
}
bool InternetPlaylistItem::InitFromQuery(const SqlRow &query) {
// The song tables gets joined first, plus one each for the song ROWIDs
const int row = (Song::kColumns.count() + 1) * PlaylistBackend::kSongTableJoins;
service_name_ = query.value(row + 1).toString();
metadata_.InitFromQuery(query, false, (Song::kColumns.count() + 1) * 1);
InitMetadata();
return true;
}
InternetService *InternetPlaylistItem::service() const {
InternetService *ret = InternetModel::ServiceByName(service_name_);
if (ret && !set_service_icon_) {
const_cast<InternetPlaylistItem*>(this)->set_service_icon_ = true;
QString icon = ret->Icon();
if (!icon.isEmpty()) {
const_cast<InternetPlaylistItem*>(this)->metadata_.set_art_manual(icon);
}
}
InternetService *ret = InternetModel::ServiceBySource(source_);
return ret;
}
QVariant InternetPlaylistItem::DatabaseValue(DatabaseColumn column) const {
switch (column) {
case Column_InternetService:
return service_name_;
default:
return PlaylistItem::DatabaseValue(column);
}
return PlaylistItem::DatabaseValue(column);
}
void InternetPlaylistItem::InitMetadata() {
if (metadata_.title().isEmpty())
metadata_.set_title(metadata_.url().toString());
if (metadata_.filetype() == Song::Type_Unknown) metadata_.set_filetype(Song::Type_Stream);
if (metadata_.title().isEmpty()) metadata_.set_title(metadata_.url().toString());
if (metadata_.source() == Song::Source_Unknown) metadata_.set_source(Song::Source_Stream);
if (metadata_.filetype() == Song::FileType_Unknown) metadata_.set_filetype(Song::FileType_Stream);
metadata_.set_valid(true);
}
Song InternetPlaylistItem::Metadata() const {
if (!set_service_icon_) {
// Get the icon if we don't have it already
service();
}
if (HasTemporaryMetadata()) return temp_metadata_;
return metadata_;
}

View File

@@ -35,7 +35,7 @@ class InternetService;
class InternetPlaylistItem : public PlaylistItem {
public:
explicit InternetPlaylistItem(const QString &type);
explicit InternetPlaylistItem(const Song::Source &type);
InternetPlaylistItem(InternetService *service, const Song &metadata);
bool InitFromQuery(const SqlRow &query);
Song Metadata() const;
@@ -50,8 +50,7 @@ class InternetPlaylistItem : public PlaylistItem {
InternetService *service() const;
private:
QString service_name_;
bool set_service_icon_;
Song::Source source_;
Song metadata_;
};

View File

@@ -27,6 +27,6 @@
#include "internetmodel.h"
#include "internetservice.h"
InternetService::InternetService(const QString &name, Application *app, InternetModel *model, QObject *parent)
: QObject(parent), app_(app), model_(model), name_(name) {
InternetService::InternetService(Song::Source source, const QString &name, Application *app, InternetModel *model, QObject *parent)
: QObject(parent), app_(app), model_(model), source_(source), name_(name) {
}

View File

@@ -27,8 +27,10 @@
#include <QList>
#include <QString>
#include <QUrl>
#include <QIcon>
#include "core/song.h"
#include "core/iconloader.h"
#include "playlist/playlistitem.h"
#include "settings/settingsdialog.h"
@@ -40,14 +42,15 @@ class InternetService : public QObject {
Q_OBJECT
public:
InternetService(const QString &name, Application *app, InternetModel *model, QObject *parent = nullptr);
InternetService(Song::Source source, const QString &name, Application *app, InternetModel *model, QObject *parent = nullptr);
virtual ~InternetService() {}
Song::Source source() const { return source_; }
QString name() const { return name_; }
InternetModel *model() const { return model_; }
virtual bool has_initial_load_settings() const { return false; }
virtual void InitialLoadSettings() {}
virtual void ReloadSettings() {}
virtual QString Icon() { return QString(); }
virtual QIcon Icon() { return Song::IconForSource(source_); }
public slots:
virtual void ShowConfig() {}
@@ -56,6 +59,7 @@ class InternetService : public QObject {
Application *app_;
private:
InternetModel *model_;
Song::Source source_;
QString name_;
};