Last.fm config

This commit is contained in:
David Sansome 2010-02-03 18:32:48 +00:00
parent 4aa1cdfa52
commit 8dd3242cd6
17 changed files with 282 additions and 113 deletions

View File

@ -1,13 +1,15 @@
#include "lastfmconfig.h"
#include "lastfmservice.h"
#include "radiomodel.h"
#include <lastfm/ws.h>
#include <QMessageBox>
#include <QSettings>
LastFMConfig::LastFMConfig(LastFMService* service, QWidget *parent)
: QDialog(parent),
service_(service)
LastFMConfig::LastFMConfig(QWidget *parent)
: QWidget(parent),
service_(static_cast<LastFMService*>(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();
}

View File

@ -1,30 +1,34 @@
#ifndef LASTFMCONFIG_H
#define LASTFMCONFIG_H
#include <QDialog>
#include <QWidget>
#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

View File

@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LastFMConfig</class>
<widget class="QDialog" name="LastFMConfig">
<widget class="QWidget" name="LastFMConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>385</width>
<height>245</height>
<height>213</height>
</rect>
</property>
<property name="windowTitle">
<string>Last.fm</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
@ -115,16 +115,6 @@
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
@ -135,38 +125,5 @@
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>button_box</sender>
<signal>accepted()</signal>
<receiver>LastFMConfig</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>button_box</sender>
<signal>rejected()</signal>
<receiver>LastFMConfig</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
<connections/>
</ui>

View File

@ -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();
}

23
src/lastfmconfigdialog.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef LASTFMCONFIGDIALOG_H
#define LASTFMCONFIGDIALOG_H
#include <QDialog>
#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

86
src/lastfmconfigdialog.ui Normal file
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LastFMConfigDialog</class>
<widget class="QDialog" name="LastFMConfigDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Last,fm</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>:/last.fm/as.png</normaloff>:/last.fm/as.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="LastFMConfig" name="lastfm" native="true"/>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>LastFMConfig</class>
<extends>QWidget</extends>
<header>lastfmconfig.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>LastFMConfigDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>LastFMConfigDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1,8 +1,8 @@
#include "lastfmservice.h"
#include "lastfmconfig.h"
#include "radioitem.h"
#include "song.h"
#include "lastfmstationdialog.h"
#include "lastfmconfigdialog.h"
#include <lastfm/ws.h>
#include <lastfm/misc.h>
@ -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();

View File

@ -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_;

View File

@ -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))

View File

@ -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_;

View File

@ -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();
}
}

View File

@ -33,6 +33,7 @@ class RadioModel : public SimpleTreeModel<RadioItem> {
QMimeData* mimeData(const QModelIndexList& indexes) const;
void ShowContextMenu(RadioItem* item, const QPoint& global_pos);
void ReloadSettings();
signals:
void TaskStarted(const QString&);

View File

@ -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);

View File

@ -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())

View File

@ -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_;

View File

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
<string>Settings</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
@ -51,7 +51,7 @@
<string>Playback</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-playback-start-32.png</normaloff>:/media-playback-start-32.png</iconset>
</property>
</item>
@ -60,7 +60,7 @@
<string>Notifications</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/lightbulb.png</normaloff>:/lightbulb.png</iconset>
</property>
</item>
@ -69,7 +69,7 @@
<string>Music Library</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/library.png</normaloff>:/library.png</iconset>
</property>
</item>
@ -78,7 +78,7 @@
<string>Last.fm</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/last.fm/as.png</normaloff>:/last.fm/as.png</iconset>
</property>
</item>
@ -99,9 +99,9 @@
<item>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>1</number>
<number>3</number>
</property>
<widget class="QWidget" name="page">
<widget class="QWidget" name="playback_page">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="margin">
<number>0</number>
@ -195,7 +195,7 @@
<zorder>groupBox</zorder>
<zorder>verticalSpacer</zorder>
</widget>
<widget class="QWidget" name="page_4">
<widget class="QWidget" name="notifications_page">
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="margin">
<number>0</number>
@ -288,7 +288,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<widget class="QWidget" name="library_page">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="margin">
<number>0</number>
@ -298,7 +298,16 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="page_3"/>
<widget class="QWidget" name="lastfm_page">
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="LastFMConfig" name="lastfm" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
@ -326,6 +335,12 @@
<header>libraryconfig.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>LastFMConfig</class>
<extends>QWidget</extends>
<header>lastfmconfig.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>list</tabstop>
@ -334,7 +349,9 @@
<tabstop>fadeout_duration</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<resources>
<include location="../data/data.qrc"/>
</resources>
<connections>
<connection>
<sender>list</sender>

View File

@ -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