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 "scrobblersettingspage.h"
# include "ui_scrobblersettingspage.h"
2018-12-26 15:05:32 +01:00
# include <QObject>
2018-12-23 18:54:27 +01:00
# include <QMessageBox>
# include <QSettings>
2020-02-09 02:29:35 +01:00
# include <QCheckBox>
# include <QLabel>
# include <QLineEdit>
# include <QPushButton>
# include <QSpinBox>
2018-12-23 18:54:27 +01:00
2020-02-09 02:29:35 +01:00
# include "settingsdialog.h"
# include "settingspage.h"
2018-12-23 18:54:27 +01:00
# include "core/application.h"
# include "core/iconloader.h"
2020-08-25 23:44:27 +02:00
# include "core/song.h"
2020-02-09 02:29:35 +01:00
# include "widgets/loginstatewidget.h"
2018-12-23 18:54:27 +01:00
# include "scrobbler/audioscrobbler.h"
# include "scrobbler/lastfmscrobbler.h"
# include "scrobbler/librefmscrobbler.h"
# include "scrobbler/listenbrainzscrobbler.h"
const char * ScrobblerSettingsPage : : kSettingsGroup = " Scrobbler " ;
2021-06-20 19:04:08 +02:00
ScrobblerSettingsPage : : ScrobblerSettingsPage ( SettingsDialog * dialog , QWidget * parent )
: SettingsPage ( dialog , parent ) ,
scrobbler_ ( dialog - > app ( ) - > scrobbler ( ) ) ,
lastfmscrobbler_ ( dialog - > app ( ) - > scrobbler ( ) - > Service < LastFMScrobbler > ( ) ) ,
librefmscrobbler_ ( dialog - > app ( ) - > scrobbler ( ) - > Service < LibreFMScrobbler > ( ) ) ,
listenbrainzscrobbler_ ( dialog - > app ( ) - > scrobbler ( ) - > Service < ListenBrainzScrobbler > ( ) ) ,
2018-12-23 18:54:27 +01:00
ui_ ( new Ui_ScrobblerSettingsPage ) ,
lastfm_waiting_for_auth_ ( false ) ,
librefm_waiting_for_auth_ ( false ) ,
2021-07-11 07:40:57 +02:00
listenbrainz_waiting_for_auth_ ( false ) {
2018-12-23 18:54:27 +01:00
ui_ - > setupUi ( this ) ;
setWindowIcon ( IconLoader : : Load ( " scrobble " ) ) ;
// Last.fm
2021-01-26 16:48:04 +01:00
QObject : : connect ( lastfmscrobbler_ , & LastFMScrobbler : : AuthenticationComplete , this , & ScrobblerSettingsPage : : LastFM_AuthenticationComplete ) ;
QObject : : connect ( ui_ - > button_lastfm_login , & QPushButton : : clicked , this , & ScrobblerSettingsPage : : LastFM_Login ) ;
QObject : : connect ( ui_ - > widget_lastfm_login_state , & LoginStateWidget : : LoginClicked , this , & ScrobblerSettingsPage : : LastFM_Login ) ;
QObject : : connect ( ui_ - > widget_lastfm_login_state , & LoginStateWidget : : LogoutClicked , this , & ScrobblerSettingsPage : : LastFM_Logout ) ;
2018-12-23 18:54:27 +01:00
ui_ - > widget_lastfm_login_state - > AddCredentialGroup ( ui_ - > widget_lastfm_login ) ;
// Libre.fm
2021-01-26 16:48:04 +01:00
QObject : : connect ( librefmscrobbler_ , & LibreFMScrobbler : : AuthenticationComplete , this , & ScrobblerSettingsPage : : LibreFM_AuthenticationComplete ) ;
QObject : : connect ( ui_ - > button_librefm_login , & QPushButton : : clicked , this , & ScrobblerSettingsPage : : LibreFM_Login ) ;
QObject : : connect ( ui_ - > widget_librefm_login_state , & LoginStateWidget : : LoginClicked , this , & ScrobblerSettingsPage : : LibreFM_Login ) ;
QObject : : connect ( ui_ - > widget_librefm_login_state , & LoginStateWidget : : LogoutClicked , this , & ScrobblerSettingsPage : : LibreFM_Logout ) ;
2018-12-23 18:54:27 +01:00
ui_ - > widget_librefm_login_state - > AddCredentialGroup ( ui_ - > widget_librefm_login ) ;
// ListenBrainz
2021-01-26 16:48:04 +01:00
QObject : : connect ( listenbrainzscrobbler_ , & ListenBrainzScrobbler : : AuthenticationComplete , this , & ScrobblerSettingsPage : : ListenBrainz_AuthenticationComplete ) ;
QObject : : connect ( ui_ - > button_listenbrainz_login , & QPushButton : : clicked , this , & ScrobblerSettingsPage : : ListenBrainz_Login ) ;
QObject : : connect ( ui_ - > widget_listenbrainz_login_state , & LoginStateWidget : : LoginClicked , this , & ScrobblerSettingsPage : : ListenBrainz_Login ) ;
QObject : : connect ( ui_ - > widget_listenbrainz_login_state , & LoginStateWidget : : LogoutClicked , this , & ScrobblerSettingsPage : : ListenBrainz_Logout ) ;
2018-12-23 18:54:27 +01:00
ui_ - > widget_listenbrainz_login_state - > AddCredentialGroup ( ui_ - > widget_listenbrainz_login ) ;
2019-12-29 23:37:48 +01:00
ui_ - > label_listenbrainz_token - > setText ( " <html><head/><body><p> " + tr ( " Enter your user token from " ) + " " + " <a href= \" https://listenbrainz.org/profile/ \" ><span style= \" text-decoration: underline; color:#0000ff; \" >https://listenbrainz.org/profile/</span></a></p></body></html> " ) ;
2018-12-23 18:54:27 +01:00
resize ( sizeHint ( ) ) ;
}
ScrobblerSettingsPage : : ~ ScrobblerSettingsPage ( ) { delete ui_ ; }
void ScrobblerSettingsPage : : Load ( ) {
2020-10-01 22:04:38 +02:00
QSettings s ;
if ( ! s . contains ( kSettingsGroup ) ) set_changed ( ) ;
2018-12-23 18:54:27 +01:00
ui_ - > checkbox_enable - > setChecked ( scrobbler_ - > IsEnabled ( ) ) ;
ui_ - > checkbox_scrobble_button - > setChecked ( scrobbler_ - > ScrobbleButton ( ) ) ;
2019-06-12 00:38:52 +02:00
ui_ - > checkbox_love_button - > setChecked ( scrobbler_ - > LoveButton ( ) ) ;
2018-12-23 18:54:27 +01:00
ui_ - > checkbox_offline - > setChecked ( scrobbler_ - > IsOffline ( ) ) ;
2018-12-25 22:58:34 +01:00
ui_ - > spinbox_submit - > setValue ( scrobbler_ - > SubmitDelay ( ) ) ;
2019-10-19 02:56:23 +02:00
ui_ - > checkbox_albumartist - > setChecked ( scrobbler_ - > PreferAlbumArtist ( ) ) ;
2020-05-26 17:51:23 +02:00
ui_ - > checkbox_show_error_dialog - > setChecked ( scrobbler_ - > ShowErrorDialog ( ) ) ;
2018-12-23 18:54:27 +01:00
2020-08-25 23:44:27 +02:00
ui_ - > checkbox_source_collection - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Collection ) ) ;
ui_ - > checkbox_source_local - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_LocalFile ) ) ;
ui_ - > checkbox_source_cdda - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_CDDA ) ) ;
ui_ - > checkbox_source_device - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Device ) ) ;
ui_ - > checkbox_source_subsonic - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Subsonic ) ) ;
2020-09-22 00:00:02 +02:00
ui_ - > checkbox_source_tidal - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Tidal ) ) ;
ui_ - > checkbox_source_qobuz - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Qobuz ) ) ;
2020-08-25 23:44:27 +02:00
ui_ - > checkbox_source_stream - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Stream ) ) ;
2021-07-11 05:18:56 +02:00
ui_ - > checkbox_source_somafm - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_SomaFM ) ) ;
ui_ - > checkbox_source_radioparadise - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_RadioParadise ) ) ;
2020-08-25 23:44:27 +02:00
ui_ - > checkbox_source_unknown - > setChecked ( scrobbler_ - > sources ( ) . contains ( Song : : Source_Unknown ) ) ;
2018-12-23 18:54:27 +01:00
ui_ - > checkbox_lastfm_enable - > setChecked ( lastfmscrobbler_ - > IsEnabled ( ) ) ;
2019-04-15 22:17:40 +02:00
ui_ - > checkbox_lastfm_https - > setChecked ( lastfmscrobbler_ - > IsUseHTTPS ( ) ) ;
2018-12-23 18:54:27 +01:00
LastFM_RefreshControls ( lastfmscrobbler_ - > IsAuthenticated ( ) ) ;
ui_ - > checkbox_librefm_enable - > setChecked ( librefmscrobbler_ - > IsEnabled ( ) ) ;
LibreFM_RefreshControls ( librefmscrobbler_ - > IsAuthenticated ( ) ) ;
ui_ - > checkbox_listenbrainz_enable - > setChecked ( listenbrainzscrobbler_ - > IsEnabled ( ) ) ;
ui_ - > lineedit_listenbrainz_user_token - > setText ( listenbrainzscrobbler_ - > user_token ( ) ) ;
ListenBrainz_RefreshControls ( listenbrainzscrobbler_ - > IsAuthenticated ( ) ) ;
2020-05-25 23:56:54 +02:00
Init ( ui_ - > layout_scrobblersettingspage - > parentWidget ( ) ) ;
2020-10-12 17:20:18 +02:00
if ( ! QSettings ( ) . childGroups ( ) . contains ( kSettingsGroup ) ) set_changed ( ) ;
2018-12-23 18:54:27 +01:00
}
void ScrobblerSettingsPage : : Save ( ) {
QSettings s ;
s . beginGroup ( kSettingsGroup ) ;
s . setValue ( " enabled " , ui_ - > checkbox_enable - > isChecked ( ) ) ;
s . setValue ( " scrobble_button " , ui_ - > checkbox_scrobble_button - > isChecked ( ) ) ;
2019-06-12 00:38:52 +02:00
s . setValue ( " love_button " , ui_ - > checkbox_love_button - > isChecked ( ) ) ;
2018-12-23 18:54:27 +01:00
s . setValue ( " offline " , ui_ - > checkbox_offline - > isChecked ( ) ) ;
2018-12-25 22:58:34 +01:00
s . setValue ( " submit " , ui_ - > spinbox_submit - > value ( ) ) ;
2019-10-19 02:56:23 +02:00
s . setValue ( " albumartist " , ui_ - > checkbox_albumartist - > isChecked ( ) ) ;
2020-05-26 17:51:23 +02:00
s . setValue ( " show_error_dialog " , ui_ - > checkbox_show_error_dialog - > isChecked ( ) ) ;
2020-08-25 23:44:27 +02:00
QStringList sources ;
if ( ui_ - > checkbox_source_collection - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Collection ) ;
if ( ui_ - > checkbox_source_local - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_LocalFile ) ;
if ( ui_ - > checkbox_source_cdda - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_CDDA ) ;
if ( ui_ - > checkbox_source_device - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Device ) ;
if ( ui_ - > checkbox_source_subsonic - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Subsonic ) ;
2020-09-22 00:00:02 +02:00
if ( ui_ - > checkbox_source_tidal - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Tidal ) ;
if ( ui_ - > checkbox_source_qobuz - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Qobuz ) ;
2020-08-25 23:44:27 +02:00
if ( ui_ - > checkbox_source_stream - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Stream ) ;
2021-07-11 05:18:56 +02:00
if ( ui_ - > checkbox_source_somafm - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_SomaFM ) ;
if ( ui_ - > checkbox_source_radioparadise - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_RadioParadise ) ;
2020-08-25 23:44:27 +02:00
if ( ui_ - > checkbox_source_unknown - > isChecked ( ) ) sources < < Song : : TextForSource ( Song : : Source_Unknown ) ;
s . setValue ( " sources " , sources ) ;
2018-12-23 18:54:27 +01:00
s . endGroup ( ) ;
s . beginGroup ( LastFMScrobbler : : kSettingsGroup ) ;
s . setValue ( " enabled " , ui_ - > checkbox_lastfm_enable - > isChecked ( ) ) ;
2019-04-15 22:17:40 +02:00
s . setValue ( " https " , ui_ - > checkbox_lastfm_https - > isChecked ( ) ) ;
2018-12-23 18:54:27 +01:00
s . endGroup ( ) ;
s . beginGroup ( LibreFMScrobbler : : kSettingsGroup ) ;
s . setValue ( " enabled " , ui_ - > checkbox_librefm_enable - > isChecked ( ) ) ;
s . endGroup ( ) ;
s . beginGroup ( ListenBrainzScrobbler : : kSettingsGroup ) ;
s . setValue ( " enabled " , ui_ - > checkbox_listenbrainz_enable - > isChecked ( ) ) ;
s . setValue ( " user_token " , ui_ - > lineedit_listenbrainz_user_token - > text ( ) ) ;
s . endGroup ( ) ;
scrobbler_ - > ReloadSettings ( ) ;
}
void ScrobblerSettingsPage : : LastFM_Login ( ) {
lastfm_waiting_for_auth_ = true ;
ui_ - > widget_lastfm_login_state - > SetLoggedIn ( LoginStateWidget : : LoginInProgress ) ;
2019-04-15 22:17:40 +02:00
lastfmscrobbler_ - > Authenticate ( ui_ - > checkbox_lastfm_https - > isChecked ( ) ) ;
2018-12-23 18:54:27 +01:00
}
void ScrobblerSettingsPage : : LastFM_Logout ( ) {
lastfmscrobbler_ - > Logout ( ) ;
LastFM_RefreshControls ( false ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : LastFM_AuthenticationComplete ( const bool success , const QString & error ) {
2018-12-23 18:54:27 +01:00
if ( ! lastfm_waiting_for_auth_ ) return ;
lastfm_waiting_for_auth_ = false ;
if ( success ) {
Save ( ) ;
}
else {
2019-04-15 22:17:40 +02:00
if ( ! error . isEmpty ( ) ) QMessageBox : : warning ( this , " Authentication failed " , error ) ;
2018-12-23 18:54:27 +01:00
}
LastFM_RefreshControls ( success ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : LastFM_RefreshControls ( const bool authenticated ) {
2018-12-23 18:54:27 +01:00
ui_ - > widget_lastfm_login_state - > SetLoggedIn ( authenticated ? LoginStateWidget : : LoggedIn : LoginStateWidget : : LoggedOut , lastfmscrobbler_ - > username ( ) ) ;
}
void ScrobblerSettingsPage : : LibreFM_Login ( ) {
librefm_waiting_for_auth_ = true ;
ui_ - > widget_librefm_login_state - > SetLoggedIn ( LoginStateWidget : : LoginInProgress ) ;
librefmscrobbler_ - > Authenticate ( ) ;
}
void ScrobblerSettingsPage : : LibreFM_Logout ( ) {
librefmscrobbler_ - > Logout ( ) ;
LibreFM_RefreshControls ( false ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : LibreFM_AuthenticationComplete ( const bool success , const QString & error ) {
2018-12-23 18:54:27 +01:00
if ( ! librefm_waiting_for_auth_ ) return ;
librefm_waiting_for_auth_ = false ;
if ( success ) {
Save ( ) ;
}
else {
QMessageBox : : warning ( this , " Authentication failed " , error ) ;
}
LibreFM_RefreshControls ( success ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : LibreFM_RefreshControls ( const bool authenticated ) {
2018-12-23 18:54:27 +01:00
ui_ - > widget_librefm_login_state - > SetLoggedIn ( authenticated ? LoginStateWidget : : LoggedIn : LoginStateWidget : : LoggedOut , librefmscrobbler_ - > username ( ) ) ;
}
void ScrobblerSettingsPage : : ListenBrainz_Login ( ) {
listenbrainz_waiting_for_auth_ = true ;
ui_ - > widget_listenbrainz_login_state - > SetLoggedIn ( LoginStateWidget : : LoginInProgress ) ;
listenbrainzscrobbler_ - > Authenticate ( ) ;
}
void ScrobblerSettingsPage : : ListenBrainz_Logout ( ) {
listenbrainzscrobbler_ - > Logout ( ) ;
ListenBrainz_RefreshControls ( false ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : ListenBrainz_AuthenticationComplete ( const bool success , const QString & error ) {
2018-12-23 18:54:27 +01:00
if ( ! listenbrainz_waiting_for_auth_ ) return ;
listenbrainz_waiting_for_auth_ = false ;
if ( success ) {
Save ( ) ;
}
else {
QMessageBox : : warning ( this , " Authentication failed " , error ) ;
}
ListenBrainz_RefreshControls ( success ) ;
}
2021-06-20 19:04:08 +02:00
void ScrobblerSettingsPage : : ListenBrainz_RefreshControls ( const bool authenticated ) {
2018-12-23 18:54:27 +01:00
ui_ - > widget_listenbrainz_login_state - > SetLoggedIn ( authenticated ? LoginStateWidget : : LoggedIn : LoginStateWidget : : LoggedOut ) ;
}