From 8dd3242cd68d14d02ba3b3753fac1d80be139a55 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Wed, 3 Feb 2010 18:32:48 +0000 Subject: [PATCH] Last.fm config --- src/lastfmconfig.cpp | 41 ++++++++++++------ src/lastfmconfig.h | 16 ++++--- src/lastfmconfig.ui | 55 +++--------------------- src/lastfmconfigdialog.cpp | 31 ++++++++++++++ src/lastfmconfigdialog.h | 23 ++++++++++ src/lastfmconfigdialog.ui | 86 ++++++++++++++++++++++++++++++++++++++ src/lastfmservice.cpp | 51 +++++++++++----------- src/lastfmservice.h | 8 ++-- src/mainwindow.cpp | 2 +- src/mainwindow.h | 3 +- src/radiomodel.cpp | 6 +++ src/radiomodel.h | 1 + src/radioservice.h | 2 + src/settingsdialog.cpp | 21 ++++++++++ src/settingsdialog.h | 1 + src/settingsdialog.ui | 39 ++++++++++++----- src/src.pro | 9 ++-- 17 files changed, 282 insertions(+), 113 deletions(-) create mode 100644 src/lastfmconfigdialog.cpp create mode 100644 src/lastfmconfigdialog.h create mode 100644 src/lastfmconfigdialog.ui diff --git a/src/lastfmconfig.cpp b/src/lastfmconfig.cpp index 47bf4cbc1..45fe3a314 100644 --- a/src/lastfmconfig.cpp +++ b/src/lastfmconfig.cpp @@ -1,13 +1,15 @@ #include "lastfmconfig.h" #include "lastfmservice.h" +#include "radiomodel.h" #include #include +#include -LastFMConfig::LastFMConfig(LastFMService* service, QWidget *parent) - : QDialog(parent), - service_(service) +LastFMConfig::LastFMConfig(QWidget *parent) + : QWidget(parent), + service_(static_cast(RadioModel::ServiceByName("Last.fm"))) { ui_.setupUi(this); ui_.busy->hide(); @@ -15,29 +17,42 @@ LastFMConfig::LastFMConfig(LastFMService* service, QWidget *parent) connect(service_, SIGNAL(AuthenticationComplete(bool)), SLOT(AuthenticationComplete(bool))); } -void LastFMConfig::accept() { - if (ui_.username->text().isEmpty() || ui_.password->text().isEmpty()) { - QDialog::accept(); - return; - } +bool LastFMConfig::NeedsValidation() const { + return !ui_.username->text().isEmpty() && !ui_.password->text().isEmpty(); +} +void LastFMConfig::Validate() { ui_.busy->show(); - ui_.button_box->setEnabled(false); service_->Authenticate(ui_.username->text(), ui_.password->text()); - - emit ScrobblingEnabledChanged(ui_.scrobble->isChecked()); } void LastFMConfig::AuthenticationComplete(bool success) { + if (!ui_.busy->isVisible()) + return; // Wasn't us that was waiting for auth + ui_.busy->hide(); - ui_.button_box->setEnabled(true); if (success) { ui_.username->setText(lastfm::ws::Username); ui_.password->clear(); - QDialog::accept(); } else { QMessageBox::warning(this, "Authentication failed", "Your Last.fm credentials were incorrect"); } + + emit ValidationComplete(success); +} + +void LastFMConfig::Load() { + ui_.username->setText(lastfm::ws::Username); + ui_.scrobble->setChecked(service_->IsScrobblingEnabled()); +} + +void LastFMConfig::Save() { + QSettings s; + s.beginGroup(LastFMService::kSettingsGroup); + s.setValue("ScrobblingEnabled", ui_.scrobble->isChecked()); + s.endGroup(); + + service_->ReloadSettings(); } diff --git a/src/lastfmconfig.h b/src/lastfmconfig.h index 04e9c74c7..f1143a758 100644 --- a/src/lastfmconfig.h +++ b/src/lastfmconfig.h @@ -1,30 +1,34 @@ #ifndef LASTFMCONFIG_H #define LASTFMCONFIG_H -#include +#include #include "ui_lastfmconfig.h" class LastFMService; -class LastFMConfig : public QDialog { +class LastFMConfig : public QWidget { Q_OBJECT public: - LastFMConfig(LastFMService* service, QWidget* parent = 0); + LastFMConfig(QWidget* parent = 0); - void accept(); + bool NeedsValidation() const; - Ui::LastFMConfig ui_; + public slots: + void Validate(); + void Load(); + void Save(); signals: - void ScrobblingEnabledChanged(bool value); + void ValidationComplete(bool success); private slots: void AuthenticationComplete(bool success); private: LastFMService* service_; + Ui::LastFMConfig ui_; }; #endif // LASTFMCONFIG_H diff --git a/src/lastfmconfig.ui b/src/lastfmconfig.ui index 5d09e11f3..e5940f8b7 100644 --- a/src/lastfmconfig.ui +++ b/src/lastfmconfig.ui @@ -1,19 +1,19 @@ LastFMConfig - + 0 0 385 - 245 + 213 - - Last.fm - + + 0 + @@ -115,16 +115,6 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - @@ -135,38 +125,5 @@ - - - button_box - accepted() - LastFMConfig - accept() - - - 248 - 254 - - - 157 - 274 - - - - - button_box - rejected() - LastFMConfig - reject() - - - 316 - 260 - - - 286 - 274 - - - - + diff --git a/src/lastfmconfigdialog.cpp b/src/lastfmconfigdialog.cpp new file mode 100644 index 000000000..7ba5d7daa --- /dev/null +++ b/src/lastfmconfigdialog.cpp @@ -0,0 +1,31 @@ +#include "lastfmconfigdialog.h" +#include "ui_lastfmconfigdialog.h" + +LastFMConfigDialog::LastFMConfigDialog(QWidget *parent) + : QDialog(parent) +{ + ui_.setupUi(this); + + connect(ui_.lastfm, SIGNAL(ValidationComplete(bool)), SLOT(ValidationComplete(bool))); +} + +void LastFMConfigDialog::showEvent(QShowEvent *) { + ui_.lastfm->Load(); +} + +void LastFMConfigDialog::accept() { + if (ui_.lastfm->NeedsValidation()) { + ui_.lastfm->Validate(); + ui_.buttonBox->setEnabled(false); + } else { + ui_.lastfm->Save(); + QDialog::accept(); + } +} + +void LastFMConfigDialog::ValidationComplete(bool success) { + ui_.buttonBox->setEnabled(true); + + if (success) + QDialog::accept(); +} diff --git a/src/lastfmconfigdialog.h b/src/lastfmconfigdialog.h new file mode 100644 index 000000000..0f60bb64f --- /dev/null +++ b/src/lastfmconfigdialog.h @@ -0,0 +1,23 @@ +#ifndef LASTFMCONFIGDIALOG_H +#define LASTFMCONFIGDIALOG_H + +#include + +#include "ui_lastfmconfigdialog.h" + +class LastFMConfigDialog : public QDialog { + Q_OBJECT + public: + LastFMConfigDialog(QWidget* parent = 0); + + void accept(); + void showEvent(QShowEvent *); + + private slots: + void ValidationComplete(bool success); + + private: + Ui::LastFMConfigDialog ui_; +}; + +#endif // LASTFMCONFIGDIALOG_H diff --git a/src/lastfmconfigdialog.ui b/src/lastfmconfigdialog.ui new file mode 100644 index 000000000..3f21a2451 --- /dev/null +++ b/src/lastfmconfigdialog.ui @@ -0,0 +1,86 @@ + + + LastFMConfigDialog + + + + 0 + 0 + 400 + 300 + + + + Last,fm + + + + :/last.fm/as.png:/last.fm/as.png + + + + + + + + + Qt::Horizontal + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + LastFMConfig + QWidget +
lastfmconfig.h
+ 1 +
+
+ + + + buttonBox + accepted() + LastFMConfigDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + LastFMConfigDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +
diff --git a/src/lastfmservice.cpp b/src/lastfmservice.cpp index ff4f96084..92f030b2c 100644 --- a/src/lastfmservice.cpp +++ b/src/lastfmservice.cpp @@ -1,8 +1,8 @@ #include "lastfmservice.h" -#include "lastfmconfig.h" #include "radioitem.h" #include "song.h" #include "lastfmstationdialog.h" +#include "lastfmconfigdialog.h" #include #include @@ -23,6 +23,7 @@ LastFMService::LastFMService(QObject* parent) : RadioService(kServiceName, parent), tuner_(NULL), scrobbler_(NULL), + config_(NULL), station_dialog_(new LastFMStationDialog), context_menu_(new QMenu), initial_tune_(false), @@ -35,17 +36,7 @@ LastFMService::LastFMService(QObject* parent) lastfm::ws::ApiKey = kApiKey; lastfm::ws::SharedSecret = kSecret; - QSettings settings; - settings.beginGroup(kSettingsGroup); - lastfm::ws::Username = settings.value("username").toString(); - lastfm::ws::SessionKey = settings.value("session").toString(); - scrobbling_enabled_ = settings.value("scrobbling_enabled", true).toBool(); - - config_ = new LastFMConfig(this); - connect(config_, SIGNAL(ScrobblingEnabledChanged(bool)), SLOT(ScrobblingEnabledChangedSlot(bool))); - - config_->ui_.username->setText(lastfm::ws::Username); - config_->ui_.scrobble->setEnabled(scrobbling_enabled_); + ReloadSettings(); play_action_ = context_menu_->addAction(QIcon(":media-playback-start.png"), "Add to playlist", this, SLOT(AddToPlaylist())); remove_action_ = context_menu_->addAction(QIcon(":list-remove.png"), "Remove", this, SLOT(Remove())); @@ -53,7 +44,7 @@ LastFMService::LastFMService(QObject* parent) add_artist_action_ = context_menu_->addAction(QIcon(":last.fm/icon_radio.png"), "Play artist radio...", this, SLOT(AddArtistRadio())); add_tag_action_ = context_menu_->addAction(QIcon(":last.fm/icon_tag.png"), "Play tag radio...", this, SLOT(AddTagRadio())); context_menu_->addAction(QIcon(":configure.png"), "Configure Last.fm...", - config_, SLOT(show())); + this, SLOT(ShowConfig())); remove_action_->setEnabled(false); add_artist_action_->setEnabled(false); @@ -66,18 +57,26 @@ LastFMService::~LastFMService() { delete context_menu_; } -bool LastFMService::IsAuthenticated() const { - return !lastfm::ws::SessionKey.isEmpty(); -} - -void LastFMService::ScrobblingEnabledChangedSlot(bool value) { - scrobbling_enabled_ = value; - +void LastFMService::ReloadSettings() { QSettings settings; settings.beginGroup(kSettingsGroup); - settings.setValue("scrobbling_enabled", scrobbling_enabled_); + lastfm::ws::Username = settings.value("Username").toString(); + lastfm::ws::SessionKey = settings.value("Session").toString(); + scrobbling_enabled_ = settings.value("ScrobblingEnabled", true).toBool(); - emit ScrobblingEnabledChanged(value); + emit ScrobblingEnabledChanged(scrobbling_enabled_); +} + +void LastFMService::ShowConfig() { + if (!config_) { + config_ = new LastFMConfigDialog; + } + + config_->show(); +} + +bool LastFMService::IsAuthenticated() const { + return !lastfm::ws::SessionKey.isEmpty(); } RadioItem* LastFMService::CreateRootItem(RadioItem* parent) { @@ -114,7 +113,7 @@ void LastFMService::LazyPopulate(RadioItem *item) { neighbours_list_->icon = QIcon(":last.fm/my_neighbours.png"); if (!IsAuthenticated()) - config_->show(); + ShowConfig(); add_artist_action_->setEnabled(true); add_tag_action_->setEnabled(true); @@ -186,8 +185,8 @@ void LastFMService::AuthenticateReplyFinished() { // Save the session key QSettings settings; settings.beginGroup(kSettingsGroup); - settings.setValue("username", lastfm::ws::Username); - settings.setValue("session", lastfm::ws::SessionKey); + settings.setValue("Username", lastfm::ws::Username); + settings.setValue("Session", lastfm::ws::SessionKey); // Invalidate the scrobbler - it will get recreated later delete scrobbler_; @@ -377,7 +376,7 @@ void LastFMService::Scrobble() { void LastFMService::Love() { if (!IsAuthenticated()) - config_->show(); + ShowConfig(); lastfm::MutableTrack mtrack(last_track_); mtrack.love(); diff --git a/src/lastfmservice.h b/src/lastfmservice.h index e29453f9a..f1c9c60e3 100644 --- a/src/lastfmservice.h +++ b/src/lastfmservice.h @@ -10,7 +10,7 @@ class QMenu; class QAction; -class LastFMConfig; +class LastFMConfigDialog; class LastFMService : public RadioService { Q_OBJECT @@ -58,6 +58,8 @@ class LastFMService : public RadioService { bool IsPauseAllowed() const { return false; } bool ShowLastFmControls() const { return true; } + void ReloadSettings(); + // Last.fm specific stuff bool IsAuthenticated() const; bool IsScrobblingEnabled() const { return scrobbling_enabled_; } @@ -76,9 +78,9 @@ class LastFMService : public RadioService { private slots: void AuthenticateReplyFinished(); - void ScrobblingEnabledChangedSlot(bool value); void RefreshFriendsFinished(); void RefreshNeighboursFinished(); + void ShowConfig(); void TunerTrackAvailable(); void TunerError(lastfm::ws::Error error); @@ -108,7 +110,7 @@ class LastFMService : public RadioService { lastfm::Audioscrobbler* scrobbler_; lastfm::Track last_track_; - LastFMConfig* config_; + LastFMConfigDialog* config_; LastFMStationDialog* station_dialog_; QMenu* context_menu_; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eeb320b28..eedcd12b4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -40,12 +40,12 @@ MainWindow::MainWindow(QWidget *parent) track_slider_(new TrackSlider(this)), edit_tag_dialog_(new EditTagDialog(this)), multi_loading_indicator_(new MultiLoadingIndicator(this)), - settings_dialog_(new SettingsDialog(this)), library_config_dialog_(new LibraryConfigDialog(this)), radio_model_(new RadioModel(this)), playlist_(new Playlist(this)), player_(new Player(playlist_, radio_model_->GetLastFMService(), this)), library_(new Library(player_->GetEngine(), this)), + settings_dialog_(new SettingsDialog(this)), playlist_menu_(new QMenu(this)), library_sort_model_(new QSortFilterProxyModel(this)), track_position_timer_(new QTimer(this)) diff --git a/src/mainwindow.h b/src/mainwindow.h index 1fddda5b4..bb8232b7a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -81,7 +81,6 @@ class MainWindow : public QMainWindow { TrackSlider* track_slider_; EditTagDialog* edit_tag_dialog_; MultiLoadingIndicator* multi_loading_indicator_; - SettingsDialog* settings_dialog_; LibraryConfigDialog* library_config_dialog_; RadioModel* radio_model_; @@ -89,6 +88,8 @@ class MainWindow : public QMainWindow { Player* player_; Library* library_; + SettingsDialog* settings_dialog_; + QMenu* playlist_menu_; QAction* playlist_play_pause_; QAction* playlist_stop_after_; diff --git a/src/radiomodel.cpp b/src/radiomodel.cpp index 96083acba..14d4af9e8 100644 --- a/src/radiomodel.cpp +++ b/src/radiomodel.cpp @@ -119,3 +119,9 @@ void RadioModel::ShowContextMenu(RadioItem* item, const QPoint& global_pos) { if (item->service) item->service->ShowContextMenu(item, global_pos); } + +void RadioModel::ReloadSettings() { + foreach (RadioService* service, sServices.values()) { + service->ReloadSettings(); + } +} diff --git a/src/radiomodel.h b/src/radiomodel.h index d2ac82e4b..2962a5b06 100644 --- a/src/radiomodel.h +++ b/src/radiomodel.h @@ -33,6 +33,7 @@ class RadioModel : public SimpleTreeModel { QMimeData* mimeData(const QModelIndexList& indexes) const; void ShowContextMenu(RadioItem* item, const QPoint& global_pos); + void ReloadSettings(); signals: void TaskStarted(const QString&); diff --git a/src/radioservice.h b/src/radioservice.h index 57bd4d0dc..f64ecd0d0 100644 --- a/src/radioservice.h +++ b/src/radioservice.h @@ -34,6 +34,8 @@ class RadioService : public QObject { virtual bool IsPauseAllowed() const { return true; } virtual bool ShowLastFmControls() const { return false; } + virtual void ReloadSettings() {} + signals: void TaskStarted(const QString& name); void TaskFinished(const QString& name); diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index de1efdcd9..ef57317ec 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -9,6 +9,9 @@ SettingsDialog::SettingsDialog(QWidget* parent) { ui_.setupUi(this); + // Last.fm + connect(ui_.lastfm, SIGNAL(ValidationComplete(bool)), SLOT(LastFMValidationComplete(bool))); + // List box connect(ui_.list, SIGNAL(currentTextChanged(QString)), SLOT(CurrentTextChanged(QString))); ui_.list->setCurrentRow(0); @@ -32,7 +35,22 @@ void SettingsDialog::SetLibraryDirectoryModel(LibraryDirectoryModel* model) { ui_.library_config->SetModel(model); } +void SettingsDialog::LastFMValidationComplete(bool success) { + ui_.buttonBox->setEnabled(true); + + if (success) + accept(); +} + void SettingsDialog::accept() { + if (ui_.lastfm->NeedsValidation()) { + ui_.lastfm->Validate(); + ui_.buttonBox->setEnabled(false); + return; + } else { + ui_.lastfm->Save(); + } + QSettings s; // Playback @@ -58,6 +76,9 @@ void SettingsDialog::accept() { void SettingsDialog::showEvent(QShowEvent*) { QSettings s; + // Last.fm + ui_.lastfm->Load(); + // Playback s.beginGroup(Engine::Base::kSettingsGroup); if (s.value("FadeoutEnabled", true).toBool()) diff --git a/src/settingsdialog.h b/src/settingsdialog.h index 7fe1ad274..0a8b91bef 100644 --- a/src/settingsdialog.h +++ b/src/settingsdialog.h @@ -24,6 +24,7 @@ class SettingsDialog : public QDialog { private slots: void CurrentTextChanged(const QString& text); void NotificationTypeChanged(); + void LastFMValidationComplete(bool success); private: Ui::SettingsDialog ui_; diff --git a/src/settingsdialog.ui b/src/settingsdialog.ui index 8ba14d357..505964c67 100644 --- a/src/settingsdialog.ui +++ b/src/settingsdialog.ui @@ -11,7 +11,7 @@ - Form + Settings @@ -51,7 +51,7 @@ Playback - + :/media-playback-start-32.png:/media-playback-start-32.png @@ -60,7 +60,7 @@ Notifications - + :/lightbulb.png:/lightbulb.png @@ -69,7 +69,7 @@ Music Library - + :/library.png:/library.png @@ -78,7 +78,7 @@ Last.fm - + :/last.fm/as.png:/last.fm/as.png @@ -99,9 +99,9 @@ - 1 + 3 - + 0 @@ -195,7 +195,7 @@ groupBox verticalSpacer - + 0 @@ -288,7 +288,7 @@ - + 0 @@ -298,7 +298,16 @@ - + + + + 0 + + + + + + @@ -326,6 +335,12 @@
libraryconfig.h
1 + + LastFMConfig + QWidget +
lastfmconfig.h
+ 1 +
list @@ -334,7 +349,9 @@ fadeout_duration buttonBox - + + + list diff --git a/src/src.pro b/src/src.pro index e01b2e99e..78859357a 100644 --- a/src/src.pro +++ b/src/src.pro @@ -51,7 +51,8 @@ SOURCES += main.cpp \ somafmservice.cpp \ settingsdialog.cpp \ librarydirectorymodel.cpp \ - libraryconfigdialog.cpp + libraryconfigdialog.cpp \ + lastfmconfigdialog.cpp HEADERS += mainwindow.h \ player.h \ library.h \ @@ -103,7 +104,8 @@ HEADERS += mainwindow.h \ somafmservice.h \ settingsdialog.h \ librarydirectorymodel.h \ - libraryconfigdialog.h + libraryconfigdialog.h \ + lastfmconfigdialog.h FORMS += mainwindow.ui \ libraryconfig.ui \ fileview.ui \ @@ -113,7 +115,8 @@ FORMS += mainwindow.ui \ edittagdialog.ui \ multiloadingindicator.ui \ settingsdialog.ui \ - libraryconfigdialog.ui + libraryconfigdialog.ui \ + lastfmconfigdialog.ui RESOURCES += ../data/data.qrc OTHER_FILES += ../data/schema.sql \ ../data/mainwindow.css