Minor code fixes to Subsonic

This commit is contained in:
Jonas Kvinge
2020-04-23 21:06:26 +02:00
parent 0be48f9f59
commit d024dd6563
2 changed files with 56 additions and 57 deletions

View File

@@ -197,7 +197,7 @@ void SubsonicRequest::AlbumsReplyReceived(QNetworkReply *reply, const int offset
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
} }
else { else {
Error("Json error object missing code or message.", json_obj); Error("Json error object is missing code or message.", json_obj);
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
return; return;
} }
@@ -209,15 +209,15 @@ void SubsonicRequest::AlbumsReplyReceived(QNetworkReply *reply, const int offset
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
return; return;
} }
QJsonValue json_albumlist; QJsonValue value_albumlist;
if (json_obj.contains("albumList")) json_albumlist = json_obj["albumList"]; if (json_obj.contains("albumList")) value_albumlist = json_obj["albumList"];
else if (json_obj.contains("albumList2")) json_albumlist = json_obj["albumList2"]; else if (json_obj.contains("albumList2")) value_albumlist = json_obj["albumList2"];
if (!json_albumlist.isObject()) { if (!value_albumlist.isObject()) {
Error("Json album list is not an object.", json_albumlist); Error("Json album list is not an object.", value_albumlist);
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
} }
json_obj = json_albumlist.toObject(); json_obj = value_albumlist.toObject();
if (json_obj.isEmpty()) { if (json_obj.isEmpty()) {
if (offset_requested == 0) no_results_ = true; if (offset_requested == 0) no_results_ = true;
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
@@ -238,44 +238,44 @@ void SubsonicRequest::AlbumsReplyReceived(QNetworkReply *reply, const int offset
Error("Json album is not an array.", json_album); Error("Json album is not an array.", json_album);
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
} }
QJsonArray json_albums = json_album.toArray(); QJsonArray array_albums = json_album.toArray();
if (json_albums.isEmpty()) { if (array_albums.isEmpty()) {
if (offset_requested == 0) no_results_ = true; if (offset_requested == 0) no_results_ = true;
AlbumsFinishCheck(offset_requested); AlbumsFinishCheck(offset_requested);
return; return;
} }
int albums_received = 0; int albums_received = 0;
for (const QJsonValue &value : json_albums) { for (const QJsonValue &value_album : array_albums) {
++albums_received; ++albums_received;
if (!value.isObject()) { if (!value_album.isObject()) {
Error("Invalid Json reply, album is not an object.", value); Error("Invalid Json reply, album is not an object.", value_album);
continue; continue;
} }
QJsonObject json_obj = value.toObject(); QJsonObject obj_album = value_album.toObject();
if (!json_obj.contains("id") || !json_obj.contains("artist")) { if (!obj_album.contains("id") || !obj_album.contains("artist")) {
Error("Invalid Json reply, album object is missing ID or artist.", json_obj); Error("Invalid Json reply, album object in array is missing ID or artist.", obj_album);
continue; continue;
} }
if (!json_obj.contains("album") && !json_obj.contains("name")) { if (!obj_album.contains("album") && !obj_album.contains("name")) {
Error("Invalid Json reply, album object is missing album or name.", json_obj); Error("Invalid Json reply, album object in array is missing album or name.", obj_album);
continue; continue;
} }
QString album_id = json_obj["id"].toString(); QString album_id = obj_album["id"].toString();
if (album_id.isEmpty()) { if (album_id.isEmpty()) {
album_id = QString::number(json_obj["id"].toInt()); album_id = QString::number(obj_album["id"].toInt());
} }
QString artist = json_obj["artist"].toString(); QString artist = obj_album["artist"].toString();
QString album; QString album;
if (json_obj.contains("album")) album = json_obj["album"].toString(); if (obj_album.contains("album")) album = obj_album["album"].toString();
else if (json_obj.contains("name")) album = json_obj["name"].toString(); else if (obj_album.contains("name")) album = obj_album["name"].toString();
if (album_songs_requests_pending_.contains(album_id)) continue; if (album_songs_requests_pending_.contains(album_id)) continue;
@@ -404,43 +404,43 @@ void SubsonicRequest::AlbumSongsReplyReceived(QNetworkReply *reply, const QStrin
SongsFinishCheck(); SongsFinishCheck();
return; return;
} }
QJsonValue json_album = json_obj["album"]; QJsonValue value_album = json_obj["album"];
if (!json_album.isObject()) { if (!value_album.isObject()) {
Error("Json album is not an object.", json_album); Error("Json album is not an object.", value_album);
SongsFinishCheck(); SongsFinishCheck();
return; return;
} }
QJsonObject json_album_obj = json_album.toObject(); QJsonObject obj_album = value_album.toObject();
if (!json_album_obj.contains("song")) { if (!obj_album.contains("song")) {
Error("Json album object does not contain song array.", json_obj); Error("Json album object does not contain song array.", json_obj);
SongsFinishCheck(); SongsFinishCheck();
return; return;
} }
QJsonValue json_song = json_album_obj["song"]; QJsonValue json_song = obj_album["song"];
if (!json_song.isArray()) { if (!json_song.isArray()) {
Error("Json song is not an array.", json_album_obj); Error("Json song is not an array.", obj_album);
SongsFinishCheck(); SongsFinishCheck();
return; return;
} }
QJsonArray json_array = json_song.toArray(); QJsonArray array_songs = json_song.toArray();
bool compilation = false; bool compilation = false;
bool multidisc = false; bool multidisc = false;
SongList songs; SongList songs;
int songs_received = 0; int songs_received = 0;
for (const QJsonValue &value : json_array) { for (const QJsonValue &value_song : array_songs) {
if (!value.isObject()) { if (!value_song.isObject()) {
Error("Invalid Json reply, track is not a object.", value); Error("Invalid Json reply, track is not a object.", value_song);
continue; continue;
} }
QJsonObject json_obj = value.toObject(); QJsonObject obj_song = value_song.toObject();
++songs_received; ++songs_received;
Song song(Song::Source_Subsonic); Song song(Song::Source_Subsonic);
ParseSong(song, json_obj, artist_id, album_id, album_artist); ParseSong(song, obj_song, artist_id, album_id, album_artist);
if (!song.is_valid()) continue; if (!song.is_valid()) continue;
if (song.disc() >= 2) multidisc = true; if (song.disc() >= 2) multidisc = true;
if (song.is_compilation()) compilation = true; if (song.is_compilation()) compilation = true;

View File

@@ -270,7 +270,7 @@ void SubsonicService::HandlePingReply(QNetworkReply *reply, const QUrl &url, con
return; return;
} }
if (json_doc.isNull() || json_doc.isEmpty()) { if (json_doc.isEmpty()) {
PingError("Ping reply from server has empty Json document."); PingError("Ping reply from server has empty Json document.");
return; return;
} }
@@ -290,39 +290,38 @@ void SubsonicService::HandlePingReply(QNetworkReply *reply, const QUrl &url, con
PingError("Ping reply from server is missing subsonic-response", json_obj); PingError("Ping reply from server is missing subsonic-response", json_obj);
return; return;
} }
QJsonValue json_response = json_obj["subsonic-response"]; QJsonValue value_response = json_obj["subsonic-response"];
if (!json_response.isObject()) { if (!value_response.isObject()) {
PingError("Ping reply from server subsonic-response is not an object", json_response); PingError("Ping reply from server subsonic-response is not an object", value_response);
return; return;
} }
QJsonObject obj_response = value_response.toObject();
json_obj = json_response.toObject(); if (obj_response.contains("error")) {
QJsonValue value_error = obj_response["error"];
if (json_obj.contains("error")) { if (!value_error.isObject()) {
QJsonValue json_error = json_obj["error"]; PingError("Authentication error reply from server is not an object", value_error);
if (!json_error.isObject()) {
PingError("Authentication error reply from server is not an object", json_response);
return; return;
} }
json_obj = json_error.toObject(); QJsonObject obj_error = value_error.toObject();
if (!json_obj.contains("code") || !json_obj.contains("message")) { if (!obj_error.contains("code") || !obj_error.contains("message")) {
PingError("Authentication error reply from server is missing status or message", json_obj); PingError("Authentication error reply from server is missing status or message", json_obj);
return; return;
} }
//int status = json_obj["code"].toInt(); //int status = obj_error["code"].toInt();
QString message = json_obj["message"].toString(); QString message = obj_error["message"].toString();
emit TestComplete(false, message); emit TestComplete(false, message);
emit TestFailure(message); emit TestFailure(message);
return; return;
} }
if (!json_obj.contains("status")) { if (!obj_response.contains("status")) {
PingError("Ping reply from server is missing status", json_obj); PingError("Ping reply from server is missing status", obj_response);
return; return;
} }
QString status = json_obj["status"].toString().toLower(); QString status = obj_response["status"].toString().toLower();
QString message = json_obj["message"].toString(); QString message = obj_response["message"].toString();
if (status == "failed") { if (status == "failed") {
emit TestComplete(false, message); emit TestComplete(false, message);
@@ -402,9 +401,9 @@ void SubsonicService::PingError(const QString &error, const QVariant &debug) {
if (!error.isEmpty()) errors_ << error; if (!error.isEmpty()) errors_ << error;
QString error_html; QString error_html;
for (const QString &error : errors_) { for (const QString &e : errors_) {
qLog(Error) << "Subsonic:" << error; qLog(Error) << "Subsonic:" << e;
error_html += error + "<br />"; error_html += e + "<br />";
} }
if (debug.isValid()) qLog(Debug) << debug; if (debug.isValid()) qLog(Debug) << debug;