-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspotify.cpp
137 lines (116 loc) · 3.93 KB
/
spotify.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
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QDesktopServices>
#include <QDebug>
#include "spotify.h"
#include "o2/o0globals.h"
#include "o2/o2requestor.h"
#include "o2/o0settingsstore.h"
//#error Setup your Spotify application "https://beta.developer.spotify.com/dashboard/applications"
const char O2_CONSUMER_KEY[] = "388f2d2f105b45ef95e159ac87ef5733";
const char O2_CONSUMER_SECRET[] = "c926747234ef4fc8aefb2759f2c3d571";
const int localPort = 8888;
const char O2_REPLY_CONTENT[] =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<style type=\"text/css\">"
" h1 {text-align: center;}"
"</style>"
"</head>"
"<body>"
"<h1>Hutspot</h1>"
"<br>"
"<h1>Spotify authorization redirected</h1>"
"<br>"
"<h1>You can close this page and return to the App.</h1>"
"</body>"
"</html>";
Spotify::Spotify(QObject *parent) : QObject(parent)
{
o2Spotify = new O2Spotify(this);
o2Spotify->setClientId(O2_CONSUMER_KEY);
o2Spotify->setClientSecret(O2_CONSUMER_SECRET);
o2Spotify->setLocalPort(localPort);
// Create a store object for writing the received tokens
O0SettingsStore *store = new O0SettingsStore(O2_ENCRYPTION_KEY);
store->setGroupKey("spotify");
o2Spotify->setGrantFlow(O2::GrantFlowAuthorizationCode);
o2Spotify->setStore(store);
o2Spotify->setReplyContent(O2_REPLY_CONTENT);
// Connect signals
connect(o2Spotify, SIGNAL(linkedChanged()), this, SLOT(onLinkedChanged()));
connect(o2Spotify, SIGNAL(linkingFailed()), this, SLOT(onLinkingFailed()));
connect(o2Spotify, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded()));
connect(o2Spotify, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl)));
connect(o2Spotify, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser()));
connect(o2Spotify, SIGNAL(refreshFinished(QNetworkReply::NetworkError, QString)), this, SLOT(onRefreshFinished(QNetworkReply::NetworkError, QString)));
}
void Spotify::doO2Auth(const QString &scope) {
if (scope.length() > 0)
o2Spotify->setScope(scope);
qDebug() << "Starting OAuth...";
o2Spotify->unlink();
o2Spotify->link();
}
QString Spotify::getUserName() {
if(o2Spotify)
return o2Spotify->username();
return "";
}
QString Spotify::getToken() {
if(o2Spotify)
return o2Spotify->token();
return "";
}
void Spotify::refreshToken() {
if(o2Spotify)
o2Spotify->refresh();
}
int Spotify::getExpires() {
if(o2Spotify)
return o2Spotify->expires();
return -1;
}
void Spotify::onOpenBrowser(const QUrl &url) {
qDebug() << "Opening browser with URL" << url.toString();
//QDesktopServices::openUrl(url);
emit openBrowser(url);
}
void Spotify::onCloseBrowser() {
// don't know how to close the broser tab/window and switch to ourselves
qDebug() << "Spotify::onCloseBrowser()";
emit closeBrowser();
}
void Spotify::onRefreshFinished(QNetworkReply::NetworkError error, QString errorString) {
//QNetworkReply *tokenReply = qobject_cast<QNetworkReply *>(sender());
//qDebug() << "Spotify::onRefreshFinished(): " << error << ", " << tokenReply->errorString();
qDebug() << "Spotify::onRefreshFinished(): " << error;
emit refreshFinished(error, errorString);
}
void Spotify::onLinkedChanged() {
qDebug() << "Linked changed!";
emit linkedChanged();
}
void Spotify::onLinkingSucceeded() {
O2Spotify *o2s = qobject_cast<O2Spotify *>(sender());
if (!o2s->linked()) {
return;
}
QVariantMap extraTokens = o2s->extraTokens();
if (!extraTokens.isEmpty()) {
emit extraTokensReady(extraTokens);
qDebug() << "Extra tokens in response:";
foreach (QString key, extraTokens.keys()) {
qDebug() << "\t" << key << ":" << (extraTokens.value(key).toString().left(3) + "...");
}
}
emit linkingSucceeded();
}
void Spotify::onLinkingFailed() {
qDebug() << "Linking failed!";
emit linkingFailed();
}
bool Spotify::isLinked() {
return o2Spotify->linked();
}