~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/internet/digitallyimportedsettingspage.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "digitallyimportedclient.h"
 
19
#include "digitallyimportedservicebase.h"
 
20
#include "digitallyimportedsettingspage.h"
 
21
#include "ui_digitallyimportedsettingspage.h"
 
22
#include "core/closure.h"
 
23
 
 
24
#include <QMessageBox>
 
25
#include <QNetworkReply>
 
26
#include <QSettings>
 
27
 
 
28
 
 
29
DigitallyImportedSettingsPage::DigitallyImportedSettingsPage(SettingsDialog* dialog)
 
30
  : SettingsPage(dialog),
 
31
    ui_(new Ui_DigitallyImportedSettingsPage),
 
32
    client_(new DigitallyImportedClient("di", this))
 
33
{
 
34
  ui_->setupUi(this);
 
35
  setWindowIcon(QIcon(":/providers/digitallyimported-32.png"));
 
36
 
 
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()));
 
40
 
 
41
  ui_->login_state->AddCredentialField(ui_->username);
 
42
  ui_->login_state->AddCredentialField(ui_->password);
 
43
  ui_->login_state->AddCredentialGroup(ui_->credential_group);
 
44
 
 
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);
 
49
}
 
50
 
 
51
DigitallyImportedSettingsPage::~DigitallyImportedSettingsPage() {
 
52
  delete ui_;
 
53
}
 
54
 
 
55
void DigitallyImportedSettingsPage::Login() {
 
56
  ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
 
57
 
 
58
  QNetworkReply* reply = client_->Auth(ui_->username->text(), ui_->password->text());
 
59
  NewClosure(reply, SIGNAL(finished()),
 
60
             this, SLOT(LoginFinished(QNetworkReply*)), reply);
 
61
}
 
62
 
 
63
void DigitallyImportedSettingsPage::LoginFinished(QNetworkReply* reply) {
 
64
  reply->deleteLater();
 
65
 
 
66
  DigitallyImportedClient::AuthReply result = client_->ParseAuthReply(reply);
 
67
 
 
68
  QString name = QString("%1 %2").arg(result.first_name_, result.last_name_);
 
69
  UpdateLoginState(result.listen_hash_, name, result.expires_);
 
70
 
 
71
  if (result.success_) {
 
72
    // Clear the password field just to be sure
 
73
    ui_->password->clear();
 
74
 
 
75
    // Save the listen key and account information
 
76
    QSettings s;
 
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_);
 
81
  } else {
 
82
    QMessageBox::warning(this, tr("Authentication failed"),
 
83
                         result.error_reason_);
 
84
  }
 
85
}
 
86
 
 
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);
 
93
  } else {
 
94
    ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, name);
 
95
    ui_->login_state->SetExpires(expires.date());
 
96
    ui_->login_state->SetAccountTypeVisible(false);
 
97
  }
 
98
 
 
99
  ui_->premium_audio_type->setEnabled(!listen_hash.isEmpty());
 
100
  ui_->premium_audio_label->setEnabled(!listen_hash.isEmpty());
 
101
}
 
102
 
 
103
void DigitallyImportedSettingsPage::Logout() {
 
104
  QSettings s;
 
105
  s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
 
106
  s.setValue("listen_hash", QString());
 
107
  s.setValue("full_name", QString());
 
108
  s.setValue("expires", QDateTime());
 
109
 
 
110
  UpdateLoginState(QString(), QString(), QDateTime());
 
111
}
 
112
 
 
113
void DigitallyImportedSettingsPage::Load() {
 
114
  QSettings s;
 
115
  s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
 
116
 
 
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());
 
120
 
 
121
  UpdateLoginState(s.value("listen_hash").toString(),
 
122
                   s.value("full_name").toString(),
 
123
                   s.value("expires").toDateTime());
 
124
}
 
125
 
 
126
void DigitallyImportedSettingsPage::Save() {
 
127
  QSettings s;
 
128
  s.beginGroup(DigitallyImportedServiceBase::kSettingsGroup);
 
129
 
 
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());
 
133
}
 
134
 
 
135