Skip to content

Commit

Permalink
- let o2 pass error code and -string
Browse files Browse the repository at this point in the history
- show message on refresh token error
- postpone starting librespot until we have a valdi token
  hopefully fixes the issue that it initially does not show up in
  the list of devices
  • Loading branch information
wdehoog committed Sep 30, 2018
1 parent b2c8434 commit 37dbff0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
7 changes: 4 additions & 3 deletions qml/components/SpotifyController.qml
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,15 @@ Item {
Spotify.getMyDevices(function(error, data) {
if (data) {
try {
console.log("reloadDevices() #devices: " + data.devices.length)
devicesModel.clear();
for (var i=0; i < data.devices.length; i++) {
devicesModel.append(data.devices[i]);
if (data.devices[i].is_active)
playbackState.device = data.devices[i]
}
} catch (err) {
console.log(err)
console.log("reloadDevices() error: " + err)
}
} else {
console.log("No Data for getMyDevices")
Expand Down Expand Up @@ -239,8 +240,8 @@ Item {
//playbackState.contextDetails = undefined
}
}
});
reloadDevices();
})
//reloadDevices() Why is this here? The info is not used.
}

function playTrack(track) {
Expand Down
31 changes: 23 additions & 8 deletions qml/hutspot.qml
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,25 @@ ApplicationWindow {
//serviceBrowser.browse("_spotify-connect._tcp")
}

Connections {
// Librespot must be started after we are logged in (have a valid token)
// otherwise it is not shown in the devices list
/*Connections {
target: librespot
onServiceEnabledChanged: {
if(start_stop_librespot.value) {
if(librespot.serviceEnabled)
librespot.start()
}
}
}*/
onHasValidTokenChanged: {
if(start_stop_librespot.value) {
if(librespot.serviceEnabled) {
if(hasValidToken)
librespot.start()
// ToDo: stop Librespot if the token becomes invalid?
}
}
}

// thanks to harbour-storeman.qml
Expand Down Expand Up @@ -377,13 +388,17 @@ ApplicationWindow {
}

onRefreshFinished: {
console.log("Connections.onRefreshFinished")
console.log("expires: " + tokenExpireTime)
tokenExpireTime = spotify.getExpires()
var expDate = new Date(tokenExpireTime*1000)
console.log("expires on: " + expDate.toDateString() + " " + expDate.toTimeString())
var now = new Date()
hasValidToken = expDate > now
console.log("Connections.onRefreshFinished error code: " + errorCode +", msg: " + errorString)
if(errorCode !== 0) {
showErrorMessage(errorString, qsTr("Failed to Refresh Authorization Token"))
} else {
console.log("expires: " + tokenExpireTime)
tokenExpireTime = spotify.getExpires()
var expDate = new Date(tokenExpireTime*1000)
console.log("expires on: " + expDate.toDateString() + " " + expDate.toTimeString())
var now = new Date()
hasValidToken = expDate > now
}
}

onOpenBrowser: {
Expand Down
4 changes: 0 additions & 4 deletions qml/pages/Devices.qml
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ Page {

Connections {
target: app
onLoggedInChanged: {
if(app.loggedIn)
app.controller.reloadDevices()
}
onHasValidTokenChanged: app.controller.reloadDevices()
}

Expand Down
4 changes: 2 additions & 2 deletions src/o2/o2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void O2::onRefreshFinished() {
timedReplies_.remove(refreshReply);
setLinked(true);
Q_EMIT linkingSucceeded();
Q_EMIT refreshFinished(QNetworkReply::NoError);
Q_EMIT refreshFinished(QNetworkReply::NoError, "");
qDebug() << " New token expires in" << tokens.value(O2_OAUTH2_EXPIRES_IN).toInt() << "seconds";
} else {
qDebug() << "O2::onRefreshFinished: Error" << (int)refreshReply->error() << refreshReply->errorString();
Expand All @@ -460,7 +460,7 @@ void O2::onRefreshError(QNetworkReply::NetworkError error) {
qWarning() << "O2::onRefreshError: " << error;
unlink();
timedReplies_.remove(refreshReply);
Q_EMIT refreshFinished(error);
Q_EMIT refreshFinished(error, refreshReply->errorString());
}

void O2::serverHasClosed(bool paramsfound)
Expand Down
2 changes: 1 addition & 1 deletion src/o2/o2.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Q_SLOTS:

Q_SIGNALS:
/// Emitted when a token refresh has been completed or failed.
void refreshFinished(QNetworkReply::NetworkError error);
void refreshFinished(QNetworkReply::NetworkError error, QString errorString);

// Property change signals
void grantFlowChanged();
Expand Down
6 changes: 3 additions & 3 deletions src/spotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Spotify::Spotify(QObject *parent) : QObject(parent)
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)), this, SLOT(onRefreshFinished(QNetworkReply::NetworkError)));
connect(o2Spotify, SIGNAL(refreshFinished(QNetworkReply::NetworkError, QString)), this, SLOT(onRefreshFinished(QNetworkReply::NetworkError, QString)));

}

Expand Down Expand Up @@ -99,11 +99,11 @@ void Spotify::onCloseBrowser() {
emit closeBrowser();
}

void Spotify::onRefreshFinished(QNetworkReply::NetworkError error) {
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();
emit refreshFinished(error, errorString);
}

void Spotify::onLinkedChanged() {
Expand Down
4 changes: 2 additions & 2 deletions src/spotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Spotify : public QObject
void linkingFailed();
void linkingSucceeded();
void linkedChanged();
void refreshFinished();
void refreshFinished(int errorCode, QString errorString);
void openBrowser(const QUrl &url);
void closeBrowser();

Expand All @@ -34,7 +34,7 @@ private slots:
void onLinkingFailed();
void onOpenBrowser(const QUrl &url);
void onCloseBrowser();
void onRefreshFinished(QNetworkReply::NetworkError error);
void onRefreshFinished(QNetworkReply::NetworkError error, QString errorString);

private:
O2Spotify * o2Spotify;
Expand Down
4 changes: 4 additions & 0 deletions translations/hutspot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,5 +947,9 @@
<source>View an Artist</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Failed to Refresh Authorization Token</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

0 comments on commit 37dbff0

Please sign in to comment.