2018-12-23 18:54:27 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-12-23 18:54:27 +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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-12-26 15:05:32 +01:00
|
|
|
#include <QString>
|
2018-12-23 18:54:27 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "settings/settingsdialog.h"
|
|
|
|
#include "settings/scrobblersettingspage.h"
|
|
|
|
|
|
|
|
#include "audioscrobbler.h"
|
|
|
|
#include "scrobblerservices.h"
|
|
|
|
#include "scrobblerservice.h"
|
|
|
|
#include "lastfmscrobbler.h"
|
|
|
|
#include "librefmscrobbler.h"
|
|
|
|
#include "listenbrainzscrobbler.h"
|
2020-09-23 18:55:22 +02:00
|
|
|
#ifdef HAVE_SUBSONIC
|
|
|
|
# include "subsonicscrobbler.h"
|
|
|
|
#endif
|
2018-12-23 18:54:27 +01:00
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
AudioScrobbler::AudioScrobbler(Application *app, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
app_(app),
|
|
|
|
scrobbler_services_(new ScrobblerServices(this)),
|
|
|
|
enabled_(false),
|
|
|
|
offline_(false),
|
|
|
|
scrobble_button_(false),
|
|
|
|
love_button_(false),
|
|
|
|
submit_delay_(0),
|
|
|
|
prefer_albumartist_(false),
|
|
|
|
show_error_dialog_(false) {
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
scrobbler_services_->AddService(new LastFMScrobbler(app_, scrobbler_services_));
|
|
|
|
scrobbler_services_->AddService(new LibreFMScrobbler(app_, scrobbler_services_));
|
|
|
|
scrobbler_services_->AddService(new ListenBrainzScrobbler(app_, scrobbler_services_));
|
2020-09-23 18:55:22 +02:00
|
|
|
#ifdef HAVE_SUBSONIC
|
2020-09-23 17:55:12 +02:00
|
|
|
scrobbler_services_->AddService(new SubsonicScrobbler(app_, scrobbler_services_));
|
2020-09-23 18:55:22 +02:00
|
|
|
#endif
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
ReloadSettings();
|
|
|
|
|
2020-04-25 00:07:42 +02:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(service, &ScrobblerService::ErrorMessage, this, &AudioScrobbler::ErrorReceived);
|
2020-04-25 00:07:42 +02:00
|
|
|
}
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::ReloadSettings() {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(ScrobblerSettingsPage::kSettingsGroup);
|
|
|
|
enabled_ = s.value("enabled", false).toBool();
|
|
|
|
offline_ = s.value("offline", false).toBool();
|
|
|
|
scrobble_button_ = s.value("scrobble_button", false).toBool();
|
2019-06-12 00:38:52 +02:00
|
|
|
love_button_ = s.value("love_button", false).toBool();
|
2018-12-25 23:12:46 +01:00
|
|
|
submit_delay_ = s.value("submit", 0).toInt();
|
2019-10-19 02:56:23 +02:00
|
|
|
prefer_albumartist_ = s.value("albumartist", false).toBool();
|
2020-05-26 17:51:23 +02:00
|
|
|
show_error_dialog_ = s.value("show_error_dialog", true).toBool();
|
2020-08-25 23:44:27 +02:00
|
|
|
QStringList sources = s.value("sources").toStringList();
|
2018-12-23 18:54:27 +01:00
|
|
|
s.endGroup();
|
|
|
|
|
2020-08-25 23:44:27 +02:00
|
|
|
sources_.clear();
|
|
|
|
|
|
|
|
if (sources.isEmpty()) {
|
|
|
|
sources_ << Song::Source_Unknown
|
|
|
|
<< Song::Source_LocalFile
|
|
|
|
<< Song::Source_Collection
|
|
|
|
<< Song::Source_CDDA
|
|
|
|
<< Song::Source_Device
|
|
|
|
<< Song::Source_Stream
|
|
|
|
<< Song::Source_Tidal
|
|
|
|
<< Song::Source_Subsonic
|
2021-07-11 05:18:56 +02:00
|
|
|
<< Song::Source_Qobuz
|
|
|
|
<< Song::Source_SomaFM
|
|
|
|
<< Song::Source_RadioParadise;
|
2020-08-25 23:44:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (const QString &source : sources) {
|
|
|
|
sources_ << Song::SourceFromText(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
emit ScrobblingEnabledChanged(enabled_);
|
|
|
|
emit ScrobbleButtonVisibilityChanged(scrobble_button_);
|
2019-06-12 00:38:52 +02:00
|
|
|
emit LoveButtonVisibilityChanged(love_button_);
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
service->ReloadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::ToggleScrobbling() {
|
|
|
|
|
|
|
|
bool enabled_old_ = enabled_;
|
|
|
|
enabled_ = !enabled_;
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(ScrobblerSettingsPage::kSettingsGroup);
|
|
|
|
s.setValue("enabled", enabled_);
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
if (enabled_ != enabled_old_) emit ScrobblingEnabledChanged(enabled_);
|
|
|
|
if (enabled_ && !offline_) { Submit(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::ToggleOffline() {
|
|
|
|
|
|
|
|
bool offline_old_ = offline_;
|
|
|
|
offline_ = !offline_;
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(ScrobblerSettingsPage::kSettingsGroup);
|
|
|
|
s.setValue("offline", offline_);
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
if (offline_ != offline_old_) { emit ScrobblingOfflineChanged(offline_); }
|
|
|
|
if (enabled_ && !offline_) { Submit(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::ShowConfig() {
|
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Scrobbler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::UpdateNowPlaying(const Song &song) {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2020-08-25 23:44:27 +02:00
|
|
|
if (!sources_.contains(song.source())) return;
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
qLog(Debug) << "Sending now playing for song" << song.artist() << song.album() << song.title();
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled()) continue;
|
|
|
|
service->UpdateNowPlaying(song);
|
|
|
|
}
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
void AudioScrobbler::ClearPlaying() {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled()) continue;
|
|
|
|
service->ClearPlaying();
|
|
|
|
}
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
void AudioScrobbler::Scrobble(const Song &song, const qint64 scrobble_point) {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2020-08-25 23:44:27 +02:00
|
|
|
if (!sources_.contains(song.source())) return;
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
qLog(Debug) << "Scrobbling song" << song.artist() << song.album() << song.title() << "at" << scrobble_point;
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled()) continue;
|
|
|
|
service->Scrobble(song);
|
|
|
|
}
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
void AudioScrobbler::Love() {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled() || !service->IsAuthenticated()) continue;
|
2019-06-12 00:38:52 +02:00
|
|
|
service->Love();
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::Submit() {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled() || !service->IsAuthenticated() || service->IsSubmitted()) continue;
|
2022-02-16 17:46:40 +01:00
|
|
|
service->StartSubmit();
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioScrobbler::WriteCache() {
|
2020-04-25 00:07:42 +02:00
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
for (ScrobblerService *service : scrobbler_services_->List()) {
|
|
|
|
if (!service->IsEnabled()) continue;
|
|
|
|
service->WriteCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void AudioScrobbler::ErrorReceived(const QString &error) {
|
2018-12-23 18:54:27 +01:00
|
|
|
emit ErrorMessage(error);
|
|
|
|
}
|