From 210e6228bc0dad4486541fec6c9c8a24265ad3d9 Mon Sep 17 00:00:00 2001 From: chme Date: Mon, 18 Dec 2017 22:05:24 +0100 Subject: [PATCH] [spotify] Prevent duplicate scan on startup The logged_in callback is called during relogin in the initscan. Additionally initscan explicitly starts the scan leading to a duplicate scan run. To prevent this from happening removed the scan call in the logged_in callback and split the spotify_login_user function into a private function (without scan) and a public function (with scan). --- src/spotify.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/spotify.c b/src/spotify.c index b2f6f410..158c08da 100644 --- a/src/spotify.c +++ b/src/spotify.c @@ -1138,9 +1138,6 @@ logged_in(sp_session *sess, sp_error error) CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&login_lck)); listener_notify(LISTENER_SPOTIFY); - - // Trigger scan after successful login to libspotify - library_exec_async(webapi_rescan, NULL); } /** @@ -1571,10 +1568,11 @@ spotify_status_info_get(struct spotify_status_info *info) } /* Thread: library, httpd */ -int -spotify_login_user(const char *user, const char *password, char **errmsg) +static int +login_user(const char *user, const char *password, char **errmsg) { sp_error err; + int ret; if (!g_sess) { @@ -1643,7 +1641,30 @@ spotify_login_user(const char *user, const char *password, char **errmsg) CHECK_ERR(L_SPOTIFY, pthread_cond_wait(&login_cond, &login_lck)); CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&login_lck)); - return 0; + CHECK_ERR(L_SPOTIFY, pthread_mutex_lock(&status_lck)); + ret = spotify_status_info.libspotify_logged_in ? 0 : -1; + CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&status_lck)); + + if (ret < 0 && errmsg) + *errmsg = safe_asprintf("Login failed"); + + return ret; +} + +/* Thread: httpd, library */ +int +spotify_login_user(const char *user, const char *password, char **errmsg) +{ + int ret; + + ret = login_user(user, password, errmsg); + if (ret == 0) + { + // Trigger scan after successful login to libspotify + library_exec_async(webapi_rescan, NULL); + } + + return ret; } /* Thread: library */ @@ -2004,7 +2025,7 @@ initscan() * Login to spotify needs to be done before scanning tracks from the web api. * (Scanned tracks need to be registered with libspotify for playback) */ - spotify_login_user(NULL, NULL, NULL); + login_user(NULL, NULL, NULL); /* * Scan saved tracks from the web api