2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
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 "magnatunesettingspage.h"
|
2011-03-18 15:39:29 +01:00
|
|
|
|
|
|
|
#include "core/network.h"
|
2010-06-09 00:56:31 +02:00
|
|
|
#include "magnatuneservice.h"
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internetmodel.h"
|
2011-08-14 01:07:10 +02:00
|
|
|
#include "ui_magnatunesettingspage.h"
|
2010-06-09 00:56:31 +02:00
|
|
|
|
2011-03-18 15:39:29 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2010-06-09 01:18:20 +02:00
|
|
|
#include <QSettings>
|
2011-03-18 15:39:29 +01:00
|
|
|
#include <QtDebug>
|
2010-06-09 01:18:20 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
MagnatuneSettingsPage::MagnatuneSettingsPage(SettingsDialog* dialog)
|
|
|
|
: SettingsPage(dialog),
|
2011-03-18 15:39:29 +01:00
|
|
|
network_(new NetworkAccessManager(this)),
|
2011-08-27 23:01:28 +02:00
|
|
|
ui_(new Ui_MagnatuneSettingsPage),
|
|
|
|
logged_in_(false)
|
2010-06-09 00:56:31 +02:00
|
|
|
{
|
|
|
|
ui_->setupUi(this);
|
2011-08-14 01:07:10 +02:00
|
|
|
setWindowIcon(QIcon(":/providers/magnatune.png"));
|
2010-06-09 00:56:31 +02:00
|
|
|
|
|
|
|
connect(ui_->membership, SIGNAL(currentIndexChanged(int)), SLOT(MembershipChanged(int)));
|
2011-03-18 15:39:29 +01:00
|
|
|
|
|
|
|
connect(network_, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
|
|
|
|
SLOT(AuthenticationRequired(QNetworkReply*, QAuthenticator*)));
|
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(LoginClicked()), SLOT(Login()));
|
|
|
|
connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(Logout()));
|
|
|
|
|
|
|
|
ui_->login_state->AddCredentialField(ui_->username);
|
|
|
|
ui_->login_state->AddCredentialField(ui_->password);
|
|
|
|
ui_->login_state->AddCredentialGroup(ui_->login_container);
|
|
|
|
|
|
|
|
ui_->login_state->SetAccountTypeText(tr(
|
|
|
|
"You can listen to Magnatune songs for free without an account. "
|
|
|
|
"Purchasing a membership removes the messages at the end of each track."));
|
2010-06-09 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
MagnatuneSettingsPage::~MagnatuneSettingsPage() {
|
2010-06-09 00:56:31 +02:00
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2011-03-18 15:39:29 +01:00
|
|
|
const char* kMagnatuneDownloadValidateUrl = "http://download.magnatune.com/";
|
|
|
|
const char* kMagnatuneStreamingValidateUrl = "http://streaming.magnatune.com/";
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
void MagnatuneSettingsPage::UpdateLoginState() {
|
|
|
|
ui_->login_state->SetLoggedIn(logged_in_ ? LoginStateWidget::LoggedIn
|
|
|
|
: LoginStateWidget::LoggedOut,
|
|
|
|
ui_->username->text());
|
|
|
|
ui_->login_state->SetAccountTypeVisible(!logged_in_);
|
|
|
|
}
|
2011-03-18 15:39:29 +01:00
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
void MagnatuneSettingsPage::Login() {
|
2011-03-21 16:07:41 +01:00
|
|
|
MagnatuneService::MembershipType type =
|
|
|
|
MagnatuneService::MembershipType(ui_->membership->currentIndex());
|
2011-03-18 15:39:29 +01:00
|
|
|
|
2011-03-21 16:07:41 +01:00
|
|
|
QUrl url(type == MagnatuneService::Membership_Streaming ?
|
|
|
|
kMagnatuneStreamingValidateUrl :
|
|
|
|
kMagnatuneDownloadValidateUrl,
|
|
|
|
QUrl::StrictMode);
|
2011-03-18 15:39:29 +01:00
|
|
|
|
|
|
|
url.setUserName(ui_->username->text());
|
|
|
|
// NOTE: Magnatune actually only checks the first 8 characters.
|
|
|
|
url.setPassword(ui_->password->text());
|
|
|
|
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
// Disable keep-alives and gzip compression as it's broken on magnatune.
|
|
|
|
req.setRawHeader("Connection", "Close");
|
|
|
|
req.setRawHeader("Accept-Encoding", "identity");
|
|
|
|
// Disable caching.
|
|
|
|
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
|
|
|
|
QNetworkRequest::AlwaysNetwork);
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
|
|
|
|
|
2011-03-18 15:39:29 +01:00
|
|
|
QNetworkReply* reply = network_->head(req);
|
2011-08-14 01:07:10 +02:00
|
|
|
connect(reply, SIGNAL(finished()), SLOT(LoginFinished()));
|
2011-03-18 15:39:29 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
void MagnatuneSettingsPage::Logout() {
|
|
|
|
logged_in_ = false;
|
|
|
|
UpdateLoginState();
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void MagnatuneSettingsPage::LoginFinished() {
|
2011-03-18 15:39:29 +01:00
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
Q_ASSERT(reply);
|
|
|
|
reply->deleteLater();
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
logged_in_ =
|
2011-03-18 15:39:29 +01:00
|
|
|
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200;
|
2011-08-27 23:01:28 +02:00
|
|
|
|
|
|
|
if (!logged_in_) {
|
2011-03-18 15:39:29 +01:00
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Authentication failed"), tr("Your Magnatune credentials were incorrect"));
|
2011-05-09 21:54:04 +02:00
|
|
|
} else {
|
|
|
|
Save();
|
2011-03-18 15:39:29 +01:00
|
|
|
}
|
2011-08-27 23:01:28 +02:00
|
|
|
|
|
|
|
UpdateLoginState();
|
2011-03-18 15:39:29 +01:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void MagnatuneSettingsPage::AuthenticationRequired(
|
2011-03-18 15:39:29 +01:00
|
|
|
QNetworkReply* reply, QAuthenticator*) {
|
|
|
|
// We send the authentication with the first request so this means we got
|
|
|
|
// a 401 Authentication Required, ie. the credentials are incorrect.
|
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void MagnatuneSettingsPage::Load() {
|
2010-06-09 01:18:20 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(MagnatuneService::kSettingsGroup);
|
|
|
|
|
|
|
|
ui_->membership->setCurrentIndex(s.value("membership", MagnatuneService::Membership_None).toInt());
|
|
|
|
ui_->username->setText(s.value("username").toString());
|
|
|
|
ui_->password->setText(s.value("password").toString());
|
|
|
|
ui_->format->setCurrentIndex(s.value("format", MagnatuneService::Format_Ogg).toInt());
|
2011-08-27 23:01:28 +02:00
|
|
|
logged_in_ = s.value("logged_in",
|
|
|
|
!ui_->username->text().isEmpty() &&
|
|
|
|
!ui_->password->text().isEmpty()).toBool();
|
2011-03-18 15:39:29 +01:00
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
UpdateLoginState();
|
2010-06-09 01:18:20 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void MagnatuneSettingsPage::Save() {
|
2010-06-09 01:18:20 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(MagnatuneService::kSettingsGroup);
|
|
|
|
|
|
|
|
s.setValue("membership", ui_->membership->currentIndex());
|
|
|
|
s.setValue("username", ui_->username->text());
|
|
|
|
s.setValue("password", ui_->password->text());
|
|
|
|
s.setValue("format", ui_->format->currentIndex());
|
2011-08-27 23:01:28 +02:00
|
|
|
s.setValue("logged_in", logged_in_);
|
2010-06-09 01:18:20 +02:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
InternetModel::Service<MagnatuneService>()->ReloadSettings();
|
2010-06-09 01:18:20 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void MagnatuneSettingsPage::MembershipChanged(int value) {
|
2010-06-09 00:56:31 +02:00
|
|
|
bool enabled = MagnatuneService::MembershipType(value) !=
|
|
|
|
MagnatuneService::Membership_None;
|
|
|
|
ui_->login_container->setEnabled(enabled);
|
|
|
|
ui_->preferences_group->setEnabled(enabled);
|
|
|
|
}
|