Rename class InternetModel to InternetServices

This commit is contained in:
Jonas Kvinge
2018-10-23 23:25:02 +02:00
parent 0c10013858
commit f4dcf6821f
22 changed files with 75 additions and 136 deletions

View File

@@ -1,43 +0,0 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#ifndef INTERNETMIMEDATA_H
#define INTERNETMIMEDATA_H
#include "config.h"
#include <QObject>
#include <QModelIndexList>
#include "core/mimedata.h"
class InternetModel;
class InternetMimeData : public MimeData {
Q_OBJECT
public:
explicit InternetMimeData(const InternetModel *_model) : model(_model) {}
const InternetModel *model;
QModelIndexList indexes;
};
#endif

View File

@@ -28,8 +28,8 @@
#include <QtDebug>
#include "internetplaylistitem.h"
#include "internetservices.h"
#include "internetservice.h"
#include "internetmodel.h"
#include "core/settingsprovider.h"
#include "collection/sqlrow.h"
#include "playlist/playlistbackend.h"
@@ -50,11 +50,6 @@ bool InternetPlaylistItem::InitFromQuery(const SqlRow &query) {
return true;
}
InternetService *InternetPlaylistItem::service() const {
InternetService *ret = InternetModel::ServiceBySource(source_);
return ret;
}
QVariant InternetPlaylistItem::DatabaseValue(DatabaseColumn column) const {
return PlaylistItem::DatabaseValue(column);
}

View File

@@ -47,7 +47,6 @@ class InternetPlaylistItem : public PlaylistItem {
private:
void InitMetadata();
InternetService *service() const;
private:
Song::Source source_;

View File

@@ -48,7 +48,7 @@
#include "playlist/songmimedata.h"
#include "internetsearch.h"
#include "internetservice.h"
#include "internetmodel.h"
#include "internetservices.h"
using std::advance;
@@ -60,7 +60,7 @@ InternetSearch::InternetSearch(Application *app, Song::Source source, QObject *p
: QObject(parent),
app_(app),
source_(source),
service_(app->internet_model()->ServiceBySource(source)),
service_(app->internet_services()->ServiceBySource(source)),
searches_next_id_(1),
art_searches_next_id_(1) {

View File

@@ -24,9 +24,9 @@
#include "core/logging.h"
#include "core/mimedata.h"
#include "internetmodel.h"
#include "internetservices.h"
#include "internetservice.h"
InternetService::InternetService(Song::Source source, const QString &name, const QString &url_scheme, Application *app, InternetModel *model, QObject *parent)
: QObject(parent), app_(app), model_(model), source_(source), name_(name), url_scheme_(url_scheme) {
InternetService::InternetService(Song::Source source, const QString &name, const QString &url_scheme, Application *app, QObject *parent)
: QObject(parent), app_(app), source_(source), name_(name), url_scheme_(url_scheme) {
}

View File

@@ -36,19 +36,18 @@
#include "internetsearch.h"
class Application;
class InternetModel;
class InternetServices;
class CollectionFilterWidget;
class InternetService : public QObject {
Q_OBJECT
public:
InternetService(Song::Source source, const QString &name, const QString &url_scheme, Application *app, InternetModel *model, QObject *parent = nullptr);
InternetService(Song::Source source, const QString &name, const QString &url_scheme, Application *app, QObject *parent = nullptr);
virtual ~InternetService() {}
virtual Song::Source source() const { return source_; }
virtual QString name() const { return name_; }
virtual QString url_scheme() const { return url_scheme_; }
virtual InternetModel *model() const { return model_; }
virtual bool has_initial_load_settings() const { return false; }
virtual void InitialLoadSettings() {}
virtual void ReloadSettings() {}
@@ -62,7 +61,6 @@ class InternetService : public QObject {
protected:
Application *app_;
private:
InternetModel *model_;
Song::Source source_;
QString name_;
QString url_scheme_;

View File

@@ -24,70 +24,50 @@
#include <QObject>
#include <QMap>
#include <QString>
#include <QStandardItemModel>
#include <QtDebug>
#include "core/logging.h"
#include "internetmodel.h"
#include "internetservices.h"
#include "internetservice.h"
#ifdef HAVE_STREAM_TIDAL
# include "tidal/tidalservice.h"
#endif
#ifdef HAVE_STREAM_DEEZER
# include "deezer/deezerservice.h"
#endif
QMap<Song::Source, InternetService*>* InternetModel::sServices = nullptr;
InternetServices::InternetServices(QObject *parent) : QObject(parent) {}
InternetServices::~InternetServices() {}
InternetModel::InternetModel(Application *app, QObject *parent)
: QStandardItemModel(parent),
app_(app) {
void InternetServices::AddService(InternetService *service) {
if (!sServices) sServices = new QMap<Song::Source, InternetService*>;
Q_ASSERT(sServices->isEmpty());
#ifdef HAVE_STREAM_TIDAL
AddService(new TidalService(app, this));
#endif
#ifdef HAVE_STREAM_DEEZER
AddService(new DeezerService(app, this));
#endif
}
void InternetModel::AddService(InternetService *service) {
qLog(Debug) << "Adding internet service:" << service->name();
sServices->insert(service->source(), service);
services_.insert(service->source(), service);
connect(service, SIGNAL(destroyed()), SLOT(ServiceDeleted()));
if (service->has_initial_load_settings()) service->InitialLoadSettings();
else service->ReloadSettings();
qLog(Debug) << "Added internet service" << service->name();
}
void InternetModel::RemoveService(InternetService *service) {
void InternetServices::RemoveService(InternetService *service) {
if (!sServices->contains(service->source())) return;
sServices->remove(service->source());
if (!services_.contains(service->source())) return;
services_.remove(service->source());
disconnect(service, 0, this, 0);
}
void InternetModel::ServiceDeleted() {
void InternetServices::ServiceDeleted() {
InternetService *service = qobject_cast<InternetService*>(sender());
if (service) RemoveService(service);
}
InternetService *InternetModel::ServiceBySource(const Song::Source &source) {
InternetService *InternetServices::ServiceBySource(const Song::Source &source) {
if (sServices->contains(source)) return sServices->value(source);
if (services_.contains(source)) return services_.value(source);
return nullptr;
}
void InternetModel::ReloadSettings() {
for (InternetService *service : sServices->values()) {
void InternetServices::ReloadSettings() {
for (InternetService *service : services_.values()) {
service->ReloadSettings();
}
}

View File

@@ -19,8 +19,8 @@
*
*/
#ifndef INTERNETMODEL_H
#define INTERNETMODEL_H
#ifndef INTERNETSERVICES_H
#define INTERNETSERVICES_H
#include "config.h"
@@ -39,11 +39,12 @@
class Application;
class InternetService;
class InternetModel : public QStandardItemModel {
class InternetServices : public QObject {
Q_OBJECT
public:
explicit InternetModel(Application* app, QObject *parent = nullptr);
explicit InternetServices(QObject *parent = nullptr);
~InternetServices();
enum Role {
// Services can use this role to distinguish between different types of items that they add.
@@ -104,12 +105,10 @@ class InternetModel : public QStandardItemModel {
PlayBehaviour_DoubleClickAction,
};
// Needs to be static for InternetPlaylistItem::restore
static InternetService *ServiceBySource(const Song::Source &source);
InternetService *ServiceBySource(const Song::Source &source);
template <typename T>
static T *Service() {
return static_cast<T*>(ServiceBySource(T::kSource));
T *Service() {
return static_cast<T*>(this->ServiceBySource(T::kSource));
}
// Add and remove services. Ownership is not transferred and the service is not reparented.
@@ -118,14 +117,11 @@ class InternetModel : public QStandardItemModel {
void RemoveService(InternetService *service);
void ReloadSettings();
Application *app() const { return app_; }
private slots:
void ServiceDeleted();
private:
static QMap<Song::Source, InternetService*> *sServices;
Application *app_;
QMap<Song::Source, InternetService*> services_;
};