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 "digitallyimportedclient.h"
19
#include "digitallyimportedservicebase.h"
20
#include "digitallyimportedsettingspage.h"
21
#include "ui_digitallyimportedsettingspage.h"
22
#include "core/closure.h"
24
#include <QMessageBox>
25
#include <QNetworkReply>
29
DigitallyImportedSettingsPage::DigitallyImportedSettingsPage(SettingsDialog* dialog)
30
: SettingsPage(dialog),
31
ui_(new Ui_DigitallyImportedSettingsPage),
32
client_(new DigitallyImportedClient("di", this))
35
setWindowIcon(QIcon(":/providers/digitallyimported-32.png"));
37
connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(Logout()));
38
connect(ui_->login_state, SIGNAL(LoginClicked()), SLOT(Login()));
39
connect(ui_->login, SIGNAL(clicked()), SLOT(Login()));
41
ui_->login_state->AddCredentialField(ui_->username);
42
ui_->login_state->AddCredentialField(ui_->password);
43
ui_->login_state->AddCredentialGroup(ui_->credential_group);
45
ui_->login_state->SetAccountTypeText(tr(
46
"You can listen for free without an account, but Premium members can "
47
"listen to higher quality streams without advertisements."));
48
ui_->login_state->SetAccountTypeVisible(true);
51
DigitallyImportedSettingsPage::~DigitallyImportedSettingsPage() {
55
void DigitallyImportedSettingsPage::Login() {
56
ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
58
QNetworkReply* reply = client_->Auth(ui_->username->text(), ui_->password->text());
59
NewClosure(reply, SIGNAL(finished()),
60
this, SLOT(LoginFinished(QNetworkReply*)), reply);
63
void DigitallyImportedSettingsPage::LoginFinished(QNetworkReply* reply) {
66
DigitallyImportedClient::AuthReply result = client_->ParseAuthReply(reply);
68
QString name = QString("%1 %2").arg(result.first_name_, result.last_name_);
69
UpdateLoginState(result.listen_hash_, name, result.expires_);
71
if (result.success_) {
72
// Clear the password field just to be sure
73
ui_->password->clear();
75
// Save the listen key and account information
77
s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
78
s.setValue("listen_hash", result.listen_hash_);
79
s.setValue("full_name", name);
80
s.setValue("expires", result.expires_);
82
QMessageBox::warning(this, tr("Authentication failed"),
83
result.error_reason_);
87
void DigitallyImportedSettingsPage::UpdateLoginState(
88
const QString& listen_hash, const QString& name, const QDateTime& expires) {
89
if (listen_hash.isEmpty()) {
90
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedOut);
91
ui_->login_state->SetExpires(QDate());
92
ui_->login_state->SetAccountTypeVisible(true);
94
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, name);
95
ui_->login_state->SetExpires(expires.date());
96
ui_->login_state->SetAccountTypeVisible(false);
99
ui_->premium_audio_type->setEnabled(!listen_hash.isEmpty());
100
ui_->premium_audio_label->setEnabled(!listen_hash.isEmpty());
103
void DigitallyImportedSettingsPage::Logout() {
105
s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
106
s.setValue("listen_hash", QString());
107
s.setValue("full_name", QString());
108
s.setValue("expires", QDateTime());
110
UpdateLoginState(QString(), QString(), QDateTime());
113
void DigitallyImportedSettingsPage::Load() {
115
s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
117
ui_->basic_audio_type->setCurrentIndex(s.value("basic_audio_type", 1).toInt());
118
ui_->premium_audio_type->setCurrentIndex(s.value("premium_audio_type", 2).toInt());
119
ui_->username->setText(s.value("username").toString());
121
UpdateLoginState(s.value("listen_hash").toString(),
122
s.value("full_name").toString(),
123
s.value("expires").toDateTime());
126
void DigitallyImportedSettingsPage::Save() {
128
s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
130
s.setValue("basic_audio_type", ui_->basic_audio_type->currentIndex());
131
s.setValue("premium_audio_type", ui_->premium_audio_type->currentIndex());
132
s.setValue("username", ui_->username->text());