strawberry-audio-player-win.../src/core/application.cpp

262 lines
9.2 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, David Sansome <me@davidsansome.com>
2018-12-29 03:25:21 +01:00
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
2018-12-15 00:43:50 +01:00
* Copyright 2018, 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"
2018-02-27 18:06:05 +01:00
#include "application.h"
#include <functional>
2018-02-27 18:06:05 +01:00
#include <QObject>
#include <QThread>
2018-10-17 23:49:02 +02:00
#include <QVariant>
#include <QString>
#include "core/closure.h"
2018-02-27 18:06:05 +01:00
#include "core/lazy.h"
#include "core/tagreaderclient.h"
#include "core/song.h"
#include "database.h"
#include "taskmanager.h"
#include "player.h"
#include "appearance.h"
2018-02-27 18:06:05 +01:00
#include "engine/enginedevice.h"
2018-09-20 17:36:23 +02:00
#ifndef Q_OS_WIN
# include "device/devicemanager.h"
#endif
2018-02-27 18:06:05 +01:00
#include "collection/collection.h"
#include "playlist/playlistbackend.h"
#include "playlist/playlistmanager.h"
#include "covermanager/albumcoverloader.h"
#include "covermanager/coverproviders.h"
#include "covermanager/currentartloader.h"
2018-12-15 00:43:50 +01:00
#include "covermanager/lastfmcoverprovider.h"
2018-02-27 18:06:05 +01:00
#include "covermanager/discogscoverprovider.h"
#include "covermanager/musicbrainzcoverprovider.h"
2019-04-14 02:54:40 +02:00
#include "covermanager/deezercoverprovider.h"
2018-02-27 18:06:05 +01:00
#include "lyrics/lyricsproviders.h"
#include "lyrics/lyricsprovider.h"
#include "lyrics/auddlyricsprovider.h"
2019-04-14 18:02:51 +02:00
#include "lyrics/chartlyricsprovider.h"
2019-04-18 15:03:01 +02:00
#include "scrobbler/audioscrobbler.h"
#include "internet/internetservices.h"
#include "internet/internetsearch.h"
2018-08-09 18:10:03 +02:00
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
# include "tidal/tidalservice.h"
2019-04-14 18:02:51 +02:00
# include "covermanager/tidalcoverprovider.h"
#endif
2019-06-17 23:54:24 +02:00
#ifdef HAVE_SUBSONIC
# include "subsonic/subsonicservice.h"
#endif
2019-04-18 15:03:01 +02:00
#ifdef HAVE_MOODBAR
# include "moodbar/moodbarcontroller.h"
# include "moodbar/moodbarloader.h"
#endif
2018-02-27 18:06:05 +01:00
bool Application::kIsPortable = false;
class ApplicationImpl {
public:
2019-04-08 18:46:11 +02:00
explicit ApplicationImpl(Application *app) :
tag_reader_client_([=]() {
2018-02-27 18:06:05 +01:00
TagReaderClient *client = new TagReaderClient(app);
app->MoveToNewThread(client);
client->Start();
return client;
}),
database_([=]() {
2018-02-27 18:06:05 +01:00
Database *db = new Database(app, app);
app->MoveToNewThread(db);
DoInAMinuteOrSo(db, SLOT(DoBackup()));
return db;
}),
2018-02-27 18:06:05 +01:00
appearance_([=]() { return new Appearance(app); }),
task_manager_([=]() { return new TaskManager(app); }),
player_([=]() { return new Player(app, app); }),
enginedevice_([=]() { return new EngineDevice(app); }),
2018-09-20 17:36:23 +02:00
#ifndef Q_OS_WIN
2018-02-27 18:06:05 +01:00
device_manager_([=]() { return new DeviceManager(app, app); }),
2018-09-20 17:36:23 +02:00
#endif
2018-07-01 22:26:46 +02:00
collection_([=]() { return new SCollection(app, app); }),
2018-02-27 18:06:05 +01:00
playlist_backend_([=]() {
PlaylistBackend *backend = new PlaylistBackend(app, app);
app->MoveToThread(backend, database_->thread());
return backend;
}),
playlist_manager_([=]() { return new PlaylistManager(app); }),
cover_providers_([=]() {
CoverProviders *cover_providers = new CoverProviders(app);
// Initialize the repository of cover providers.
2019-04-14 16:40:05 +02:00
cover_providers->AddProvider(new LastFmCoverProvider(app, app));
cover_providers->AddProvider(new DiscogsCoverProvider(app, app));
cover_providers->AddProvider(new MusicbrainzCoverProvider(app, app));
cover_providers->AddProvider(new DeezerCoverProvider(app, app));
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
2019-04-14 16:40:05 +02:00
cover_providers->AddProvider(new TidalCoverProvider(app, app));
2019-04-14 18:02:51 +02:00
#endif
2018-02-27 18:06:05 +01:00
return cover_providers;
}),
album_cover_loader_([=]() {
AlbumCoverLoader *loader = new AlbumCoverLoader(app);
app->MoveToNewThread(loader);
return loader;
}),
2018-08-09 18:10:03 +02:00
current_art_loader_([=]() { return new CurrentArtLoader(app, app); }),
lyrics_providers_([=]() {
LyricsProviders *lyrics_providers = new LyricsProviders(app);
lyrics_providers->AddProvider(new AuddLyricsProvider(app));
2019-04-14 18:02:51 +02:00
lyrics_providers->AddProvider(new ChartLyricsProvider(app));
2018-10-14 00:08:33 +02:00
return lyrics_providers;
}),
internet_services_([=]() {
InternetServices *internet_services = new InternetServices(app);
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
internet_services->AddService(new TidalService(app, internet_services));
2019-06-17 23:54:24 +02:00
#endif
#ifdef HAVE_SUBSONIC
internet_services->AddService(new SubsonicService(app, internet_services));
#endif
return internet_services;
}),
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
tidal_search_([=]() { return new InternetSearch(app, Song::Source_Tidal, app); }),
2018-10-17 23:49:02 +02:00
#endif
2019-04-18 15:03:01 +02:00
scrobbler_([=]() { return new AudioScrobbler(app, app); }),
#ifdef HAVE_MOODBAR
moodbar_loader_([=]() { return new MoodbarLoader(app, app); }),
moodbar_controller_([=]() { return new MoodbarController(app, app); }),
#endif
dummy_([=]() { return nullptr; })
2018-10-14 00:08:33 +02:00
{}
2018-02-27 18:06:05 +01:00
Lazy<TagReaderClient> tag_reader_client_;
Lazy<Database> database_;
Lazy<Appearance> appearance_;
Lazy<TaskManager> task_manager_;
Lazy<Player> player_;
Lazy<EngineDevice> enginedevice_;
2018-09-20 17:36:23 +02:00
#ifndef Q_OS_WIN
2018-02-27 18:06:05 +01:00
Lazy<DeviceManager> device_manager_;
2018-09-20 17:36:23 +02:00
#endif
2018-07-01 22:26:46 +02:00
Lazy<SCollection> collection_;
2018-02-27 18:06:05 +01:00
Lazy<PlaylistBackend> playlist_backend_;
Lazy<PlaylistManager> playlist_manager_;
Lazy<CoverProviders> cover_providers_;
Lazy<AlbumCoverLoader> album_cover_loader_;
Lazy<CurrentArtLoader> current_art_loader_;
2018-10-14 00:08:33 +02:00
Lazy<LyricsProviders> lyrics_providers_;
Lazy<InternetServices> internet_services_;
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
Lazy<InternetSearch> tidal_search_;
2018-10-17 23:49:02 +02:00
#endif
Lazy<AudioScrobbler> scrobbler_;
2019-04-18 15:03:01 +02:00
#ifdef HAVE_MOODBAR
Lazy<MoodbarLoader> moodbar_loader_;
Lazy<MoodbarController> moodbar_controller_;
#endif
Lazy<QVariant> dummy_;
2018-02-27 18:06:05 +01:00
};
Application::Application(QObject *parent)
: QObject(parent), p_(new ApplicationImpl(this)) {
enginedevice()->Init();
collection()->Init();
tag_reader_client();
}
Application::~Application() {
// It's important that the device manager is deleted before the database.
// Deleting the database deletes all objects that have been created in its thread, including some device collection backends.
2018-09-20 17:36:23 +02:00
#ifndef Q_OS_WIN
2018-02-27 18:06:05 +01:00
p_->device_manager_.reset();
2018-09-20 17:36:23 +02:00
#endif
2018-02-27 18:06:05 +01:00
for (QThread *thread : threads_) {
thread->quit();
}
for (QThread *thread : threads_) {
thread->wait();
}
}
void Application::MoveToNewThread(QObject *object) {
QThread *thread = new QThread(this);
MoveToThread(object, thread);
thread->start();
threads_ << thread;
}
void Application::MoveToThread(QObject *object, QThread *thread) {
object->setParent(nullptr);
object->moveToThread(thread);
}
void Application::AddError(const QString& message) { emit ErrorAdded(message); }
void Application::ReloadSettings() { emit SettingsChanged(); }
2018-10-14 00:08:33 +02:00
void Application::OpenSettingsDialogAtPage(SettingsDialog::Page page) { emit SettingsDialogRequested(page); }
2018-02-27 18:06:05 +01:00
2018-10-14 00:08:33 +02:00
TagReaderClient *Application::tag_reader_client() const { return p_->tag_reader_client_.get(); }
2018-02-27 18:06:05 +01:00
Appearance *Application::appearance() const { return p_->appearance_.get(); }
Database *Application::database() const { return p_->database_.get(); }
2018-10-14 00:08:33 +02:00
TaskManager *Application::task_manager() const { return p_->task_manager_.get(); }
Player *Application::player() const { return p_->player_.get(); }
EngineDevice *Application::enginedevice() const { return p_->enginedevice_.get(); }
2018-09-20 17:36:23 +02:00
#ifndef Q_OS_WIN
2018-10-14 00:08:33 +02:00
DeviceManager *Application::device_manager() const { return p_->device_manager_.get(); }
2018-09-20 17:36:23 +02:00
#endif
2018-07-01 22:26:46 +02:00
SCollection *Application::collection() const { return p_->collection_.get(); }
2018-10-14 00:08:33 +02:00
CollectionBackend *Application::collection_backend() const { return collection()->backend(); }
2018-02-27 18:06:05 +01:00
CollectionModel *Application::collection_model() const { return collection()->model(); }
2018-10-14 00:08:33 +02:00
AlbumCoverLoader *Application::album_cover_loader() const { return p_->album_cover_loader_.get(); }
CoverProviders *Application::cover_providers() const { return p_->cover_providers_.get(); }
CurrentArtLoader *Application::current_art_loader() const { return p_->current_art_loader_.get(); }
LyricsProviders *Application::lyrics_providers() const { return p_->lyrics_providers_.get(); }
PlaylistBackend *Application::playlist_backend() const { return p_->playlist_backend_.get(); }
PlaylistManager *Application::playlist_manager() const { return p_->playlist_manager_.get(); }
InternetServices *Application::internet_services() const { return p_->internet_services_.get(); }
2019-04-14 18:02:51 +02:00
#ifdef HAVE_TIDAL
InternetSearch *Application::tidal_search() const { return p_->tidal_search_.get(); }
2018-10-17 23:49:02 +02:00
#endif
AudioScrobbler *Application::scrobbler() const { return p_->scrobbler_.get(); }
2019-04-18 15:03:01 +02:00
#ifdef HAVE_MOODBAR
MoodbarController *Application::moodbar_controller() const { return p_->moodbar_controller_.get(); }
MoodbarLoader *Application::moodbar_loader() const { return p_->moodbar_loader_.get(); }
#endif