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

« back to all changes in this revision

Viewing changes to src/internet/spotifysettingspage.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 "spotifysettingspage.h"
 
19
 
 
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"
 
26
 
 
27
#include <QMessageBox>
 
28
#include <QNetworkReply>
 
29
#include <QNetworkRequest>
 
30
#include <QSettings>
 
31
#include <QtDebug>
 
32
 
 
33
SpotifySettingsPage::SpotifySettingsPage(SettingsDialog* dialog)
 
34
  : SettingsPage(dialog),
 
35
    network_(new NetworkAccessManager(this)),
 
36
    ui_(new Ui_SpotifySettingsPage),
 
37
    service_(InternetModel::Service<SpotifyService>()),
 
38
    validated_(false)
 
39
{
 
40
  ui_->setupUi(this);
 
41
 
 
42
  setWindowIcon(QIcon(":/icons/48x48/spotify.png"));
 
43
 
 
44
  QFont bold_font(font());
 
45
  bold_font.setBold(true);
 
46
  ui_->blob_status->setFont(bold_font);
 
47
 
 
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()));
 
52
 
 
53
  connect(service_, SIGNAL(LoginFinished(bool)), SLOT(LoginFinished(bool)));
 
54
  connect(service_, SIGNAL(BlobStateChanged()), SLOT(BlobStateChanged()));
 
55
 
 
56
  ui_->login_state->AddCredentialField(ui_->username);
 
57
  ui_->login_state->AddCredentialField(ui_->password);
 
58
  ui_->login_state->AddCredentialGroup(ui_->account_group);
 
59
 
 
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);
 
63
 
 
64
  BlobStateChanged();
 
65
}
 
66
 
 
67
SpotifySettingsPage::~SpotifySettingsPage() {
 
68
  delete ui_;
 
69
}
 
70
 
 
71
void SpotifySettingsPage::BlobStateChanged() {
 
72
  const bool installed = service_->IsBlobInstalled();
 
73
 
 
74
  ui_->account_group->setEnabled(installed);
 
75
  ui_->blob_status->setText(installed ? tr("Installed") : tr("Not installed"));
 
76
 
 
77
#ifdef Q_OS_LINUX
 
78
  ui_->download_blob->setEnabled(!installed);
 
79
#else
 
80
  ui_->download_blob->setEnabled(false);
 
81
#endif
 
82
}
 
83
 
 
84
void SpotifySettingsPage::DownloadBlob() {
 
85
  service_->InstallBlob();
 
86
}
 
87
 
 
88
void SpotifySettingsPage::Login() {
 
89
  if (!service_->IsBlobInstalled()) {
 
90
    return;
 
91
  }
 
92
 
 
93
  if (ui_->username->text() == original_username_ &&
 
94
      ui_->password->text() == original_password_ &&
 
95
      service_->login_state() == SpotifyService::LoginState_LoggedIn) {
 
96
    return;
 
97
  }
 
98
 
 
99
  ui_->login_state->SetLoggedIn(LoginStateWidget::LoginInProgress);
 
100
  service_->Login(ui_->username->text(), ui_->password->text());
 
101
}
 
102
 
 
103
void SpotifySettingsPage::Load() {
 
104
  QSettings s;
 
105
  s.beginGroup(SpotifyService::kSettingsGroup);
 
106
 
 
107
  original_username_ = s.value("username").toString();
 
108
 
 
109
  ui_->username->setText(original_username_);
 
110
  validated_ = false;
 
111
 
 
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());
 
116
 
 
117
  UpdateLoginState();
 
118
}
 
119
 
 
120
void SpotifySettingsPage::Save() {
 
121
  QSettings s;
 
122
  s.beginGroup(SpotifyService::kSettingsGroup);
 
123
 
 
124
  s.setValue("username", ui_->username->text());
 
125
  s.setValue("password", ui_->password->text());
 
126
 
 
127
  s.setValue("bitrate", ui_->bitrate->itemData(ui_->bitrate->currentIndex()).toInt());
 
128
  s.setValue("volume_normalisation", ui_->volume_normalisation->isChecked());
 
129
}
 
130
 
 
131
void SpotifySettingsPage::LoginFinished(bool success) {
 
132
  validated_ = success;
 
133
 
 
134
  Save();
 
135
  UpdateLoginState();
 
136
}
 
137
 
 
138
void SpotifySettingsPage::UpdateLoginState() {
 
139
  const bool logged_in =
 
140
      service_->login_state() == SpotifyService::LoginState_LoggedIn;
 
141
 
 
142
  ui_->login_state->SetLoggedIn(logged_in ? LoginStateWidget::LoggedIn
 
143
                                          : LoginStateWidget::LoggedOut,
 
144
                                ui_->username->text());
 
145
  ui_->login_state->SetAccountTypeVisible(!logged_in);
 
146
 
 
147
  switch (service_->login_state()) {
 
148
  case SpotifyService::LoginState_NoPremium:
 
149
    ui_->login_state->SetAccountTypeText(tr("You do not have a Spotify Premium account."));
 
150
    break;
 
151
 
 
152
  case SpotifyService::LoginState_Banned:
 
153
  case SpotifyService::LoginState_BadCredentials:
 
154
    ui_->login_state->SetAccountTypeText(tr("Your username or password was incorrect."));
 
155
    break;
 
156
 
 
157
  case SpotifyService::LoginState_ReloginFailed:
 
158
    ui_->login_state->SetAccountTypeText(tr("You have been logged out of Spotify, please re-enter your password."));
 
159
    break;
 
160
 
 
161
  default:
 
162
    ui_->login_state->SetAccountTypeText(tr("A Spotify Premium account is required."));
 
163
    break;
 
164
  }
 
165
}
 
166
 
 
167
void SpotifySettingsPage::Logout() {
 
168
  service_->Logout();
 
169
  UpdateLoginState();
 
170
 
 
171
  ui_->username->clear();
 
172
}