2011-04-25 21:16:26 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
#include "spotifysettingspage.h"
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
#include "spotifyservice.h"
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internetmodel.h"
|
2011-08-14 01:07:10 +02:00
|
|
|
#include "ui_spotifysettingspage.h"
|
2011-11-27 18:29:33 +01:00
|
|
|
#include "core/network.h"
|
|
|
|
#include "spotifyblob/common/spotifymessages.pb.h"
|
2011-05-09 22:03:53 +02:00
|
|
|
#include "ui/iconloader.h"
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
SpotifySettingsPage::SpotifySettingsPage(SettingsDialog* dialog)
|
|
|
|
: SettingsPage(dialog),
|
2011-04-25 21:16:26 +02:00
|
|
|
network_(new NetworkAccessManager(this)),
|
2011-08-14 01:07:10 +02:00
|
|
|
ui_(new Ui_SpotifySettingsPage),
|
2011-07-15 15:27:50 +02:00
|
|
|
service_(InternetModel::Service<SpotifyService>()),
|
2011-04-30 14:31:20 +02:00
|
|
|
validated_(false)
|
2011-04-25 21:16:26 +02:00
|
|
|
{
|
|
|
|
ui_->setupUi(this);
|
2011-04-30 14:31:20 +02:00
|
|
|
|
2011-11-05 17:08:56 +01:00
|
|
|
setWindowIcon(QIcon(":/icons/48x48/spotify.png"));
|
2011-08-14 01:07:10 +02:00
|
|
|
|
2011-04-30 14:31:20 +02:00
|
|
|
QFont bold_font(font());
|
|
|
|
bold_font.setBold(true);
|
|
|
|
ui_->blob_status->setFont(bold_font);
|
|
|
|
|
|
|
|
connect(ui_->download_blob, SIGNAL(clicked()), SLOT(DownloadBlob()));
|
2011-08-14 01:07:10 +02:00
|
|
|
connect(ui_->login, SIGNAL(clicked()), SLOT(Login()));
|
2011-08-27 23:01:28 +02:00
|
|
|
connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(Logout()));
|
|
|
|
connect(ui_->login_state, SIGNAL(LoginClicked()), SLOT(Login()));
|
2011-04-30 14:31:20 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
connect(service_, SIGNAL(LoginFinished(bool)), SLOT(LoginFinished(bool)));
|
2011-04-30 14:31:20 +02:00
|
|
|
connect(service_, SIGNAL(BlobStateChanged()), SLOT(BlobStateChanged()));
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
ui_->login_state->AddCredentialField(ui_->username);
|
|
|
|
ui_->login_state->AddCredentialField(ui_->password);
|
|
|
|
ui_->login_state->AddCredentialGroup(ui_->account_group);
|
|
|
|
|
2011-11-27 18:29:33 +01:00
|
|
|
ui_->bitrate->addItem("96 " + tr("kbps"), spotify_pb::Bitrate96k);
|
|
|
|
ui_->bitrate->addItem("160 " + tr("kbps"), spotify_pb::Bitrate160k);
|
|
|
|
ui_->bitrate->addItem("320 " + tr("kbps"), spotify_pb::Bitrate320k);
|
|
|
|
|
2011-04-30 14:31:20 +02:00
|
|
|
BlobStateChanged();
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
SpotifySettingsPage::~SpotifySettingsPage() {
|
2011-04-25 21:16:26 +02:00
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::BlobStateChanged() {
|
2011-04-30 14:31:20 +02:00
|
|
|
const bool installed = service_->IsBlobInstalled();
|
|
|
|
|
|
|
|
ui_->account_group->setEnabled(installed);
|
|
|
|
ui_->blob_status->setText(installed ? tr("Installed") : tr("Not installed"));
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
ui_->download_blob->setEnabled(!installed);
|
|
|
|
#else
|
|
|
|
ui_->download_blob->setEnabled(false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::DownloadBlob() {
|
2011-04-30 14:31:20 +02:00
|
|
|
service_->InstallBlob();
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::Login() {
|
2011-04-30 14:31:20 +02:00
|
|
|
if (!service_->IsBlobInstalled()) {
|
2011-08-14 01:07:10 +02:00
|
|
|
return;
|
2011-04-30 14:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ui_->username->text() == original_username_ &&
|
2011-08-27 23:01:28 +02:00
|
|
|
ui_->password->text() == original_password_ &&
|
|
|
|
service_->login_state() == SpotifyService::LoginState_LoggedIn) {
|
2011-05-09 21:54:04 +02:00
|
|
|
return;
|
2011-08-14 01:07:10 +02:00
|
|
|
}
|
2011-05-09 21:54:04 +02:00
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
|
2011-04-25 21:16:26 +02:00
|
|
|
service_->Login(ui_->username->text(), ui_->password->text());
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::Load() {
|
2011-04-25 21:16:26 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(SpotifyService::kSettingsGroup);
|
|
|
|
|
2011-04-30 14:31:20 +02:00
|
|
|
original_username_ = s.value("username").toString();
|
|
|
|
|
|
|
|
ui_->username->setText(original_username_);
|
|
|
|
validated_ = false;
|
2011-08-27 23:01:28 +02:00
|
|
|
|
2011-11-27 18:29:33 +01:00
|
|
|
ui_->bitrate->setCurrentIndex(ui_->bitrate->findData(
|
|
|
|
s.value("bitrate", spotify_pb::Bitrate320k).toInt()));
|
|
|
|
ui_->volume_normalisation->setChecked(
|
|
|
|
s.value("volume_normalisation", false).toBool());
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
UpdateLoginState();
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::Save() {
|
2011-04-25 21:16:26 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(SpotifyService::kSettingsGroup);
|
|
|
|
|
|
|
|
s.setValue("username", ui_->username->text());
|
|
|
|
s.setValue("password", ui_->password->text());
|
2011-11-27 18:29:33 +01:00
|
|
|
|
|
|
|
s.setValue("bitrate", ui_->bitrate->itemData(ui_->bitrate->currentIndex()).toInt());
|
|
|
|
s.setValue("volume_normalisation", ui_->volume_normalisation->isChecked());
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void SpotifySettingsPage::LoginFinished(bool success) {
|
2011-04-30 14:31:20 +02:00
|
|
|
validated_ = success;
|
2011-05-09 21:54:04 +02:00
|
|
|
|
|
|
|
Save();
|
2011-08-27 23:01:28 +02:00
|
|
|
UpdateLoginState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifySettingsPage::UpdateLoginState() {
|
|
|
|
const bool logged_in =
|
|
|
|
service_->login_state() == SpotifyService::LoginState_LoggedIn;
|
|
|
|
|
|
|
|
ui_->login_state->SetLoggedIn(logged_in ? LoginStateWidget::LoggedIn
|
|
|
|
: LoginStateWidget::LoggedOut,
|
|
|
|
ui_->username->text());
|
|
|
|
ui_->login_state->SetAccountTypeVisible(!logged_in);
|
|
|
|
|
|
|
|
switch (service_->login_state()) {
|
|
|
|
case SpotifyService::LoginState_NoPremium:
|
|
|
|
ui_->login_state->SetAccountTypeText(tr("You do not have a Spotify Premium account."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpotifyService::LoginState_Banned:
|
|
|
|
case SpotifyService::LoginState_BadCredentials:
|
|
|
|
ui_->login_state->SetAccountTypeText(tr("Your username or password was incorrect."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ui_->login_state->SetAccountTypeText(tr("A Spotify Premium account is required."));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifySettingsPage::Logout() {
|
|
|
|
service_->Logout();
|
|
|
|
UpdateLoginState();
|
2011-10-30 19:52:54 +01:00
|
|
|
|
|
|
|
ui_->username->clear();
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|