From 817ff6a87020a55d7628d600e2ee09b947ee2f4e Mon Sep 17 00:00:00 2001 From: Michał Cichoń Date: Wed, 17 May 2017 04:07:07 +0200 Subject: Show status code text if no other source of error is available. --- src/http/http.c | 21 +++++++++++- src/main.c | 100 ++++++++++++++++++++++++++------------------------------ src/main.h | 4 ++- src/ui.c | 3 +- src/ui_act.c | 18 +++++----- 5 files changed, 81 insertions(+), 65 deletions(-) diff --git a/src/http/http.c b/src/http/http.c index dd64ad1..609317f 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -44,6 +44,7 @@ static wchar_t* HttpToWideString(const char* string, int size); static bool HttpCreateConnection (http_t http); static void HttpCloseConnection (http_t http); static void HttpSetLastError (http_t http, const char* message); +static void HttpSetLastErrorW (http_t http, const wchar_t* message); static void HttpSetLastErrorFromWinHttp (http_t http); static char* HttpFormatWinApiError (DWORD errorCode, HINSTANCE module); static char* HttpFormatWinHttpError (DWORD errorCode); @@ -118,11 +119,19 @@ static void HttpCloseConnection (http_t http) { static void HttpSetLastError (http_t http, const char* message) { free(http->error); http->error = NULL; - + if (message) http->error = strdup(message); } +static void HttpSetLastErrorW (http_t http, const wchar_t* message) { + free(http->error); + http->error = NULL; + + if (message) + http->error = HttpToString(message, wcslen(message)); +} + static void HttpSetLastErrorFromWinHttp (http_t http) { free(http->error); http->error = NULL; @@ -396,6 +405,13 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) { } if (succeeded && statusCode == 407) { + wchar_t statusText[256] = { 0 }; + DWORD statusTextSize = sizeof(statusText) - 1; + WinHttpQueryHeaders(handle, + WINHTTP_QUERY_STATUS_TEXT, + WINHTTP_HEADER_NAME_BY_INDEX, + statusText, &statusTextSize, WINHTTP_NO_HEADER_INDEX); + HttpSetLastErrorW (http, statusText); requestSent = false; retry = true; } @@ -467,6 +483,9 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) { HttpSetLastError (http, "Maximum retries count exceeded"); } + if (retryLimit == 0) + goto done; + complete = true; HttpSetLastError (http, NULL); diff --git a/src/main.c b/src/main.c index eca4705..6e99ada 100644 --- a/src/main.c +++ b/src/main.c @@ -158,29 +158,21 @@ static bool BarMainGetStations(BarApp_t *app) /* get initial station from autostart setting or user rl */ -static void BarMainGetInitialStation(BarApp_t *app) -{ - /* try to get autostart station */ - if (app->settings.autostartStation != NULL) - { - app->curStation = PianoFindStationById(app->ph.stations, - app->settings.autostartStation); - if (app->curStation == NULL) - { - BarUiMsg(&app->settings, MSG_ERR, - "Error: Autostart station not found.\n"); - } - } - /* no autostart? ask the user */ - if (app->curStation == NULL) - { - app->curStation = BarUiSelectStation(app, app->ph.stations, - "Select station: ", NULL, app->settings.autoselect); - } - if (app->curStation != NULL) - { - BarUiPrintStation(&app->settings, app->curStation); - } +static void BarMainGetInitialStation (BarApp_t *app) { + /* try to get autostart station */ + if (app->settings.autostartStation != NULL) { + app->nextStation = PianoFindStationById (app->ph.stations, + app->settings.autostartStation); + if (app->nextStation == NULL) { + BarUiMsg (&app->settings, MSG_ERR, + "Error: Autostart station not found.\n"); + } + } + /* no autostart? ask the user */ + if (app->nextStation == NULL) { + app->nextStation = BarUiSelectStation (app, app->ph.stations, + "Select station: ", NULL, app->settings.autoselect); + } } /* wait for user rl @@ -198,31 +190,27 @@ static void BarMainHandleUserInput(BarApp_t *app) /* fetch new playlist */ -static void BarMainGetPlaylist(BarApp_t *app) -{ - PianoReturn_t pRet; - PianoRequestDataGetPlaylist_t reqData; - reqData.station = app->curStation; - reqData.quality = app->settings.audioQuality; - - BarUiMsg(&app->settings, MSG_INFO, "Receiving new playlist... "); - if (!BarUiPianoCall(app, PIANO_REQUEST_GET_PLAYLIST, - &reqData, &pRet)) - { - app->curStation = NULL; - } - else - { - app->playlist = reqData.retPlaylist; - if (app->playlist == NULL) - { - BarUiMsg(&app->settings, MSG_INFO, "No tracks left.\n"); - app->curStation = NULL; - } - } - BarUiStartEventCmd(&app->settings, "stationfetchplaylist", - app->curStation, app->playlist, &app->player, app->ph.stations, - pRet); +static void BarMainGetPlaylist (BarApp_t *app) { + PianoReturn_t pRet; + PianoRequestDataGetPlaylist_t reqData; + reqData.station = app->nextStation; + reqData.quality = app->settings.audioQuality; + + BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... "); + if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST, + &reqData, &pRet)) { + app->nextStation = NULL; + } else { + app->playlist = reqData.retPlaylist; + if (app->playlist == NULL) { + BarUiMsg (&app->settings, MSG_INFO, "No tracks left.\n"); + app->nextStation = NULL; + } + } + app->curStation = app->nextStation; + BarUiStartEventCmd (&app->settings, "stationfetchplaylist", + app->curStation, app->playlist, &app->player, app->ph.stations, + pRet); } /* start new player thread @@ -276,7 +264,7 @@ static void BarMainPlayerCleanup(BarApp_t *app) if (app->playerErrors >= app->settings.maxPlayerErrors) { /* don't continue playback if thread reports too many error */ - app->curStation = NULL; + app->nextStation = NULL; app->playerErrors = 0; } } @@ -338,7 +326,7 @@ static void BarMainLoop(BarApp_t *app) /* check whether player finished playing and start playing new * song */ - if (BarPlayer2IsFinished(app->player) && app->curStation != NULL) + if (BarPlayer2IsFinished(app->player) && app->nextStation != NULL) { /* what's next? */ if (app->playlist != NULL) @@ -348,10 +336,14 @@ static void BarMainLoop(BarApp_t *app) histsong->head.next = NULL; BarUiHistoryPrepend(app, histsong); } - if (app->playlist == NULL) - { - BarMainGetPlaylist(app); - } + if (app->playlist == NULL && app->nextStation != NULL && !app->doQuit) + { + if (app->nextStation != app->curStation) + { + BarUiPrintStation (&app->settings, app->nextStation); + } + BarMainGetPlaylist (app); + } /* song ready to play */ if (app->playlist != NULL) { diff --git a/src/main.h b/src/main.h index 6c34d71..e5e988d 100644 --- a/src/main.h +++ b/src/main.h @@ -42,7 +42,9 @@ typedef struct { /* first item is current song */ PianoSong_t *playlist; PianoSong_t *songHistory; - PianoStation_t *curStation; + /* station of current song and station used to fetch songs from if playlist + * is empty */ + PianoStation_t *curStation, *nextStation; char doQuit; BarReadline_t rl; unsigned int playerErrors; diff --git a/src/ui.c b/src/ui.c index 8efc7aa..88ad4d7 100644 --- a/src/ui.c +++ b/src/ui.c @@ -715,7 +715,8 @@ void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, // pipeWriteFd = fdopen (pipeFd[1], "w"); - // if (curSong != NULL && stations != NULL && curStation->isQuickMix) { + // if (curSong != NULL && stations != NULL && curStation != NULL && + // curStation->isQuickMix) { // songStation = PianoFindStationById (stations, curSong->stationId); // } diff --git a/src/ui_act.c b/src/ui_act.c index 4206a33..cb1d7a8 100644 --- a/src/ui_act.c +++ b/src/ui_act.c @@ -224,10 +224,14 @@ BarUiActCallback(BarUiActDeleteStation) { if (BarUiActDefaultPianoCall (PIANO_REQUEST_DELETE_STATION, selStation) && selStation == app->curStation) { BarUiDoSkipSong (app->player); - PianoDestroyPlaylist (PianoListNextP (app->playlist)); - app->playlist->head.next = NULL; - BarUiHistoryPrepend (app, app->playlist); - app->playlist = NULL; + if (app->playlist != NULL) { + /* drain playlist */ + PianoDestroyPlaylist (PianoListNextP (app->playlist)); + app->playlist->head.next = NULL; + } + app->nextStation = NULL; + /* XXX: usually we shoudn’t touch cur*, but DELETE_STATION destroys + * station struct */ app->curStation = NULL; } BarUiActDefaultEventcmd ("stationdelete"); @@ -446,14 +450,12 @@ BarUiActCallback(BarUiActSelectStation) { PianoStation_t *newStation = BarUiSelectStation (app, app->ph.stations, "Select station: ", NULL, app->settings.autoselect); if (newStation != NULL) { - app->curStation = newStation; - BarUiPrintStation (&app->settings, app->curStation); + app->nextStation = newStation; BarUiDoSkipSong (app->player); if (app->playlist != NULL) { + /* drain playlist */ PianoDestroyPlaylist (PianoListNextP (app->playlist)); app->playlist->head.next = NULL; - BarUiHistoryPrepend (app, app->playlist); - app->playlist = NULL; } } } -- cgit v1.2.3