-
Notifications
You must be signed in to change notification settings - Fork 99
/
settingdialog.cpp
158 lines (144 loc) · 5.06 KB
/
settingdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "settingdialog.h"
#include "ui_settingdialog.h"
#include <QButtonGroup>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtXml/QDomDocument>
#include "libs/doubanplayer.h"
SettingDialog::SettingDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingDialog),
doubanfm(DoubanFM::getInstance()),
kbpsGroup(new QButtonGroup(this)),
userIconGetter(new QNetworkAccessManager(this)),
userInfoGetter(new QNetworkAccessManager(this))
{
ui->setupUi(this);
kbpsGroup->addButton(ui->kbps64);
kbpsGroup->addButton(ui->kbps128);
kbpsGroup->addButton(ui->kbps192);
connect(kbpsGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(kbps_radio_button_clicked(QAbstractButton*)));
if (doubanfm.hasLogin()) {
auto user = doubanfm.getUser();
ui->email->setEnabled(false);
ui->password->setEnabled(false);
ui->loginButton->setText(tr("Log out"));
ui->email->setText(user->email);
ui->password->setText(user->password);
ui->usernameLabel->setText(user->user_name);
userInfoGetter->get(QNetworkRequest(QUrl("http://api.douban.com/people/" + user->user_id)));
}
connect(userIconGetter, &QNetworkAccessManager::finished, [=] (QNetworkReply *reply) {
if (QNetworkReply::NoError != reply->error()) {
qDebug() << "Err: User icon image receive error";
reply->deleteLater();
return;
}
const QByteArray data(reply->readAll());
if (!data.size())
qDebug() << Q_FUNC_INFO << "received pixmap looks like nothing";
QImage image = QImage::fromData(data);
QPixmap pixmap = QPixmap::fromImage(image);
this->setUserIcon(pixmap);
reply->deleteLater();
});
connect(userInfoGetter, &QNetworkAccessManager::finished, [=] (QNetworkReply *reply) {
if (QNetworkReply::NoError != reply->error()) {
qDebug() << "Err: User info receive error";
reply->deleteLater();
return;
}
const QByteArray data(reply->readAll());
QDomDocument info;
if (!info.setContent(data)) {
qDebug() << "Err: User info receive error";
reply->deleteLater();
return;
}
QDomNodeList links = info.elementsByTagName("link");
for (int i = 0; i < links.size(); ++ i) {
QDomElement elem = links.at(i).toElement();
if (elem.attribute("rel") == "icon") {
userIconGetter->get(QNetworkRequest(QUrl(elem.attribute("href"))));
break;
}
}
reply->deleteLater();
});
connect(&doubanfm, &DoubanFM::loginSucceed, [=] (const DoubanUser &user) {
userInfoGetter->get(QNetworkRequest(QUrl("http://api.douban.com/people/" + user.user_id)));
ui->loginButton->setText(tr("Log out"));
ui->usernameLabel->setText(user.user_name);
ui->loginButton->setEnabled(true);
});
connect(&doubanfm, &DoubanFM::loginFailed, [=] (const QString& msg) {
ui->email->setEnabled(true);
ui->password->setEnabled(true);
ui->loginButton->setText(msg);
QTimer::singleShot(3000, this, SLOT(timer_event()));
});
QSettings settings("QDoubanFM", "QDoubanFM");
settings.beginGroup("General");
qint32 _kbps = settings.value("kbps", 64).toInt();
settings.endGroup();
switch (_kbps) {
case 64:
ui->kbps64->setChecked(true);
break;
case 128:
ui->kbps128->setChecked(true);
break;
case 192:
ui->kbps192->setChecked(true);
break;
}
}
SettingDialog::~SettingDialog()
{
delete kbpsGroup;
delete ui;
}
void SettingDialog::setUserIcon(const QPixmap &pixmap) {
ui->userIcon->setPixmap(pixmap);
}
void SettingDialog::on_loginButton_clicked()
{
if (!doubanfm.hasLogin()) {
QString email = ui->email->text();
QString pwd = ui->password->text();
if (email.size() == 0 || pwd.size() == 0) return;
doubanfm.userLogin(email, pwd);
ui->email->setEnabled(false);
ui->password->setEnabled(false);
ui->loginButton->setEnabled(false);
ui->loginButton->setText(tr("Logging in..."));
}
else {
doubanfm.userLogout();
ui->email->setText("");
ui->password->setText("");
ui->email->setEnabled(true);
ui->password->setEnabled(true);
ui->loginButton->setText(tr("Log in"));
ui->userIcon->setPixmap(QPixmap(":/img/user_man_circle.png"));
ui->usernameLabel->setText(tr("Not logged in"));
}
}
void SettingDialog::timer_event() {
ui->email->setEnabled(true);
ui->password->setEnabled(true);
ui->loginButton->setEnabled(true);
ui->loginButton->setText(tr("Log in"));
}
void SettingDialog::kbps_radio_button_clicked(QAbstractButton *button) {
auto& player = DoubanPlayer::getInstance();
if (button == ui->kbps64) {
player.setKbps(64);
}
else if (button == ui->kbps128) {
player.setKbps(128);
}
else if (button == ui->kbps192) {
player.setKbps(192);
}
}