1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "spotifysettingspage.h"
20
#include "spotifyservice.h"
21
#include "internetmodel.h"
22
#include "ui_spotifysettingspage.h"
23
#include "core/network.h"
24
#include "spotifyblob/common/spotifymessages.pb.h"
25
#include "ui/iconloader.h"
27
#include <QMessageBox>
28
#include <QNetworkReply>
29
#include <QNetworkRequest>
33
SpotifySettingsPage::SpotifySettingsPage(SettingsDialog* dialog)
34
: SettingsPage(dialog),
35
network_(new NetworkAccessManager(this)),
36
ui_(new Ui_SpotifySettingsPage),
37
service_(InternetModel::Service<SpotifyService>()),
42
setWindowIcon(QIcon(":/icons/48x48/spotify.png"));
44
QFont bold_font(font());
45
bold_font.setBold(true);
46
ui_->blob_status->setFont(bold_font);
48
connect(ui_->download_blob, SIGNAL(clicked()), SLOT(DownloadBlob()));
49
connect(ui_->login, SIGNAL(clicked()), SLOT(Login()));
50
connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(Logout()));
51
connect(ui_->login_state, SIGNAL(LoginClicked()), SLOT(Login()));
53
connect(service_, SIGNAL(LoginFinished(bool)), SLOT(LoginFinished(bool)));
54
connect(service_, SIGNAL(BlobStateChanged()), SLOT(BlobStateChanged()));
56
ui_->login_state->AddCredentialField(ui_->username);
57
ui_->login_state->AddCredentialField(ui_->password);
58
ui_->login_state->AddCredentialGroup(ui_->account_group);
60
ui_->bitrate->addItem("96 " + tr("kbps"), spotify_pb::Bitrate96k);
61
ui_->bitrate->addItem("160 " + tr("kbps"), spotify_pb::Bitrate160k);
62
ui_->bitrate->addItem("320 " + tr("kbps"), spotify_pb::Bitrate320k);
67
SpotifySettingsPage::~SpotifySettingsPage() {
71
void SpotifySettingsPage::BlobStateChanged() {
72
const bool installed = service_->IsBlobInstalled();
74
ui_->account_group->setEnabled(installed);
75
ui_->blob_status->setText(installed ? tr("Installed") : tr("Not installed"));
78
ui_->download_blob->setEnabled(!installed);
80
ui_->download_blob->setEnabled(false);
84
void SpotifySettingsPage::DownloadBlob() {
85
service_->InstallBlob();
88
void SpotifySettingsPage::Login() {
89
if (!service_->IsBlobInstalled()) {
93
if (ui_->username->text() == original_username_ &&
94
ui_->password->text() == original_password_ &&
95
service_->login_state() == SpotifyService::LoginState_LoggedIn) {
99
ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
100
service_->Login(ui_->username->text(), ui_->password->text());
103
void SpotifySettingsPage::Load() {
105
s.beginGroup(SpotifyService::kSettingsGroup);
107
original_username_ = s.value("username").toString();
109
ui_->username->setText(original_username_);
112
ui_->bitrate->setCurrentIndex(ui_->bitrate->findData(
113
s.value("bitrate", spotify_pb::Bitrate320k).toInt()));
114
ui_->volume_normalisation->setChecked(
115
s.value("volume_normalisation", false).toBool());
120
void SpotifySettingsPage::Save() {
122
s.beginGroup(SpotifyService::kSettingsGroup);
124
s.setValue("username", ui_->username->text());
125
s.setValue("password", ui_->password->text());
127
s.setValue("bitrate", ui_->bitrate->itemData(ui_->bitrate->currentIndex()).toInt());
128
s.setValue("volume_normalisation", ui_->volume_normalisation->isChecked());
131
void SpotifySettingsPage::LoginFinished(bool success) {
132
validated_ = success;
138
void SpotifySettingsPage::UpdateLoginState() {
139
const bool logged_in =
140
service_->login_state() == SpotifyService::LoginState_LoggedIn;
142
ui_->login_state->SetLoggedIn(logged_in ? LoginStateWidget::LoggedIn
143
: LoginStateWidget::LoggedOut,
144
ui_->username->text());
145
ui_->login_state->SetAccountTypeVisible(!logged_in);
147
switch (service_->login_state()) {
148
case SpotifyService::LoginState_NoPremium:
149
ui_->login_state->SetAccountTypeText(tr("You do not have a Spotify Premium account."));
152
case SpotifyService::LoginState_Banned:
153
case SpotifyService::LoginState_BadCredentials:
154
ui_->login_state->SetAccountTypeText(tr("Your username or password was incorrect."));
157
case SpotifyService::LoginState_ReloginFailed:
158
ui_->login_state->SetAccountTypeText(tr("You have been logged out of Spotify, please re-enter your password."));
162
ui_->login_state->SetAccountTypeText(tr("A Spotify Premium account is required."));
167
void SpotifySettingsPage::Logout() {
171
ui_->username->clear();