mirror of
https://github.com/clementine-player/Clementine
synced 2025-02-07 06:35:15 +01:00
Check last.fm subscriber status on config dialog load.
This commit is contained in:
parent
764b3b0429
commit
b9d35c29fe
@ -24,16 +24,19 @@
|
||||
#include <lastfm/ws.h>
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QMovie>
|
||||
#include <QSettings>
|
||||
|
||||
// Use Qt specific icons, since freedesktop doesn't seem to have suitable icons.
|
||||
const char* kSubscribedIcon = ":/trolltech/styles/commonstyle/images/standardbutton-apply-16.png";
|
||||
const char* kNotSubscribedIcon = ":/trolltech/styles/commonstyle/images/standardbutton-no-16.png";
|
||||
const char* kWaitingIcon = ":spinner.gif";
|
||||
|
||||
LastFMConfig::LastFMConfig(QWidget *parent)
|
||||
: QWidget(parent),
|
||||
service_(static_cast<LastFMService*>(RadioModel::ServiceByName("Last.fm"))),
|
||||
ui_(new Ui_LastFMConfig),
|
||||
loading_icon_(new QMovie(kWaitingIcon, QByteArray(), this)),
|
||||
waiting_for_auth_(false)
|
||||
{
|
||||
ui_->setupUi(this);
|
||||
@ -42,8 +45,10 @@ LastFMConfig::LastFMConfig(QWidget *parent)
|
||||
// Icons
|
||||
ui_->sign_out->setIcon(IconLoader::Load("list-remove"));
|
||||
ui_->label->setPixmap(IconLoader::Load("dialog-warning").pixmap(16));
|
||||
ui_->label->setMinimumSize(16, 16);
|
||||
|
||||
connect(service_, SIGNAL(AuthenticationComplete(bool)), SLOT(AuthenticationComplete(bool)));
|
||||
connect(service_, SIGNAL(UpdatedSubscriberStatus(bool)), SLOT(UpdatedSubscriberStatus(bool)));
|
||||
connect(ui_->sign_out, SIGNAL(clicked()), SLOT(SignOut()));
|
||||
|
||||
ui_->username->setMinimumWidth(QFontMetrics(QFont()).width("WWWWWWWWWWWW"));
|
||||
@ -81,6 +86,7 @@ void LastFMConfig::AuthenticationComplete(bool success) {
|
||||
}
|
||||
|
||||
emit ValidationComplete(success);
|
||||
service_->UpdateSubscriberStatus();
|
||||
}
|
||||
|
||||
void LastFMConfig::Load() {
|
||||
@ -88,9 +94,29 @@ void LastFMConfig::Load() {
|
||||
ui_->love_ban_->setChecked(service_->AreButtonsVisible());
|
||||
ui_->scrobble_button->setChecked(service_->IsScrobbleButtonVisible());
|
||||
|
||||
ui_->icon->setMovie(loading_icon_);
|
||||
loading_icon_->start();
|
||||
if (service_->IsAuthenticated()) {
|
||||
service_->UpdateSubscriberStatus();
|
||||
}
|
||||
|
||||
RefreshControls(service_->IsAuthenticated());
|
||||
}
|
||||
|
||||
void LastFMConfig::UpdatedSubscriberStatus(bool is_subscriber) {
|
||||
const char* icon_path = is_subscriber ? kSubscribedIcon : kNotSubscribedIcon;
|
||||
ui_->icon->setPixmap(QIcon(icon_path).pixmap(16));
|
||||
loading_icon_->stop();
|
||||
|
||||
if (is_subscriber) {
|
||||
ui_->subscriber_warning->hide();
|
||||
} else {
|
||||
ui_->subscriber_warning->setText(
|
||||
tr("You will not be able to play Last.fm radio stations "
|
||||
"as you are not a Last.fm subscriber."));
|
||||
}
|
||||
}
|
||||
|
||||
void LastFMConfig::Save() {
|
||||
QSettings s;
|
||||
s.beginGroup(LastFMService::kSettingsGroup);
|
||||
@ -114,18 +140,7 @@ void LastFMConfig::RefreshControls(bool authenticated) {
|
||||
ui_->groupBox->setVisible(!authenticated);
|
||||
ui_->sign_out->setVisible(authenticated);
|
||||
if (authenticated) {
|
||||
const bool is_subscriber = service_->IsSubscriber();
|
||||
const char* icon_path = is_subscriber ? kSubscribedIcon : kNotSubscribedIcon;
|
||||
ui_->icon->setPixmap(QIcon(icon_path).pixmap(16));
|
||||
ui_->status->setText(QString(tr("You're logged in as <b>%1</b>")).arg(lastfm::ws::Username));
|
||||
|
||||
if (is_subscriber) {
|
||||
ui_->subscriber_warning->hide();
|
||||
} else {
|
||||
ui_->subscriber_warning->setText(
|
||||
tr("You will not be able to play Last.fm radio stations "
|
||||
"as you are not a Last.fm subscriber."));
|
||||
}
|
||||
}
|
||||
else {
|
||||
ui_->icon->setPixmap(IconLoader::Load("dialog-question").pixmap(16));
|
||||
|
@ -43,10 +43,12 @@ class LastFMConfig : public QWidget {
|
||||
private slots:
|
||||
void AuthenticationComplete(bool success);
|
||||
void SignOut();
|
||||
void UpdatedSubscriberStatus(bool is_subscriber);
|
||||
|
||||
private:
|
||||
LastFMService* service_;
|
||||
Ui_LastFMConfig* ui_;
|
||||
QMovie* loading_icon_;
|
||||
|
||||
bool waiting_for_auth_;
|
||||
|
||||
|
@ -265,6 +265,7 @@ void LastFMService::AuthenticateReplyFinished() {
|
||||
emit AuthenticationComplete(false);
|
||||
return;
|
||||
}
|
||||
reply->deleteLater();
|
||||
|
||||
// Parse the reply
|
||||
try {
|
||||
@ -284,8 +285,6 @@ void LastFMService::AuthenticateReplyFinished() {
|
||||
settings.beginGroup(kSettingsGroup);
|
||||
settings.setValue("Username", lastfm::ws::Username);
|
||||
settings.setValue("Session", lastfm::ws::SessionKey);
|
||||
// TODO: Refresh this regularly either at startup or when the config
|
||||
// dialog is loaded. See user.getInfo in the last.fm API.
|
||||
settings.setValue("Subscriber", is_subscriber);
|
||||
} catch (std::runtime_error& e) {
|
||||
qDebug() << e.what();
|
||||
@ -300,6 +299,39 @@ void LastFMService::AuthenticateReplyFinished() {
|
||||
emit AuthenticationComplete(true);
|
||||
}
|
||||
|
||||
void LastFMService::UpdateSubscriberStatus() {
|
||||
QMap<QString, QString> params;
|
||||
params["method"] = "user.getInfo";
|
||||
params["user"] = lastfm::ws::Username;
|
||||
|
||||
QNetworkReply* reply = lastfm::ws::post(params);
|
||||
connect(reply, SIGNAL(finished()), SLOT(UpdateSubscriberStatusFinished()));
|
||||
}
|
||||
|
||||
void LastFMService::UpdateSubscriberStatusFinished() {
|
||||
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
||||
Q_ASSERT(reply);
|
||||
reply->deleteLater();
|
||||
|
||||
try {
|
||||
const lastfm::XmlQuery lfm = lastfm::ws::parse(reply);
|
||||
#ifdef Q_OS_WIN32
|
||||
if (lastfm::ws::last_parse_error != lastfm::ws::NoError)
|
||||
throw std::runtime_error("");
|
||||
#endif
|
||||
|
||||
QString subscriber = lfm["user"]["subscriber"].text();
|
||||
const bool is_subscriber = (subscriber.toInt() == 1);
|
||||
QSettings settings;
|
||||
settings.beginGroup(kSettingsGroup);
|
||||
settings.setValue("Subscriber", is_subscriber);
|
||||
qDebug() << lastfm::ws::Username << "Subscriber status:" << is_subscriber;
|
||||
emit UpdatedSubscriberStatus(is_subscriber);
|
||||
} catch (std::runtime_error& e) {
|
||||
qDebug() << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
QUrl LastFMService::FixupUrl(const QUrl& url) {
|
||||
QUrl ret;
|
||||
ret.setEncodedUrl(url.toEncoded().replace(
|
||||
|
@ -101,6 +101,7 @@ class LastFMService : public RadioService {
|
||||
|
||||
void Authenticate(const QString& username, const QString& password);
|
||||
void SignOut();
|
||||
void UpdateSubscriberStatus();
|
||||
|
||||
void FetchMoreTracks();
|
||||
|
||||
@ -120,12 +121,14 @@ class LastFMService : public RadioService {
|
||||
void ButtonVisibilityChanged(bool value);
|
||||
void ScrobbleButtonVisibilityChanged(bool value);
|
||||
void ScrobblerStatus(int value);
|
||||
void UpdatedSubscriberStatus(bool is_subscriber);
|
||||
|
||||
protected:
|
||||
QModelIndex GetCurrentIndex();
|
||||
|
||||
private slots:
|
||||
void AuthenticateReplyFinished();
|
||||
void UpdateSubscriberStatusFinished();
|
||||
void RefreshFriendsFinished();
|
||||
void RefreshNeighboursFinished();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user