From 422ea20cefd4eb9fa95d283f50049f45dbf10e84 Mon Sep 17 00:00:00 2001 From: Christian Soffke Date: Sun, 4 Jun 2023 00:43:56 +0200 Subject: Warn before overwriting another playlist on disk When saving a playlist to an existing file on disk, warn user, unless the playlist's file name remains unchanged. Change-Id: I10d82667de5fadb5323be4f981bea9263849f07a --- apps/menus/playlist_menu.c | 14 +++----------- apps/misc.c | 7 +++++++ apps/misc.h | 1 + apps/onplay.c | 9 +-------- apps/playlist_catalog.c | 48 ++++++++++++++++++++++++++++++++++++++-------- apps/playlist_catalog.h | 3 +++ 6 files changed, 55 insertions(+), 27 deletions(-) diff --git a/apps/menus/playlist_menu.c b/apps/menus/playlist_menu.c index 9db87aa7a5..0983f9c338 100644 --- a/apps/menus/playlist_menu.c +++ b/apps/menus/playlist_menu.c @@ -65,18 +65,10 @@ int save_playlist_screen(struct playlist_info* playlist) strlcat(temp, DEFAULT_DYNAMIC_PLAYLIST_NAME, sizeof(temp)); } - dot = strrchr(temp, '.'); - if (dot) /* remove extension */ - *dot = '\0'; - - if (!kbd_input(temp, sizeof(temp), NULL)) + if (catalog_pick_new_playlist_name(temp, sizeof(temp), + playlist ? playlist->filename : + playlist_get_current()->filename)) { - len = strlen(temp); - if(len > 4 && !strcasecmp(&temp[len-4], ".m3u")) - strlcat(temp, "8", sizeof(temp)); - else if(len <= 5 || strcasecmp(&temp[len-5], ".m3u8")) - strlcat(temp, ".m3u8", sizeof(temp)); - playlist_save(playlist, temp, NULL, 0); /* reload in case playlist was saved to cwd */ diff --git a/apps/misc.c b/apps/misc.c index 21a45037ad..a054cfa7b6 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1227,6 +1227,13 @@ int confirm_delete_yesno(const char *name) return gui_syncyesno_run(&message, &yes_message, NULL); } +int confirm_overwrite_yesno(void) +{ + static const char *lines[] = { ID2P(LANG_REALLY_OVERWRITE) }; + static const struct text_message message = { lines, 1 }; + return gui_syncyesno_run(&message, NULL, NULL); +} + /* time_split_units() split time values depending on base unit unit_idx: UNIT_HOUR, UNIT_MIN, UNIT_SEC, UNIT_MS diff --git a/apps/misc.h b/apps/misc.h index b7a9a5c42c..497aa04c78 100644 --- a/apps/misc.h +++ b/apps/misc.h @@ -159,6 +159,7 @@ int hex_to_rgb(const char* hex, int* color); #endif int confirm_delete_yesno(const char *name); +int confirm_overwrite_yesno(void); char* strrsplt(char* str, int c); char* skip_whitespace(char* const str); diff --git a/apps/onplay.c b/apps/onplay.c index 341da62840..a9cbc64598 100644 --- a/apps/onplay.c +++ b/apps/onplay.c @@ -948,13 +948,6 @@ static bool poll_cancel_action(const char *path) return ACTION_STD_CANCEL == get_action(CONTEXT_STD, TIMEOUT_NOBLOCK); } -static int confirm_overwrite(void) -{ - static const char *lines[] = { ID2P(LANG_REALLY_OVERWRITE) }; - static const struct text_message message = { lines, 1 }; - return gui_syncyesno_run(&message, NULL, NULL); -} - static bool check_new_name(const char *basename) { /* at least prevent escapes out of the base directory from keyboard- @@ -1426,7 +1419,7 @@ static int clipboard_paste(void) case RELATE_DIFFERENT: if (file_exists(target.path)) { /* If user chooses not to overwrite, cancel */ - if (confirm_overwrite() == YESNO_NO) { + if (confirm_overwrite_yesno() == YESNO_NO) { rc = OPRC_NOOVERWRT; break; } diff --git a/apps/playlist_catalog.c b/apps/playlist_catalog.c index b6939208ac..be992645ee 100644 --- a/apps/playlist_catalog.c +++ b/apps/playlist_catalog.c @@ -389,6 +389,43 @@ bool catalog_view_playlists(void) return (display_playlists(NULL, CATBROWSE_CATVIEW) >= 0); } +static void apply_playlist_extension(char* buf, size_t buf_size) +{ + size_t len = strlen(buf); + if(len > 4 && !strcasecmp(&buf[len-4], ".m3u")) + strlcat(buf, "8", buf_size); + else if(len <= 5 || strcasecmp(&buf[len-5], ".m3u8")) + strlcat(buf, ".m3u8", buf_size); +} + +static int remove_extension(char* path) +{ + char *dot = strrchr(path, '.'); + if (dot) + *dot = '\0'; + + return 0; +} + + +bool catalog_pick_new_playlist_name(char *pl_name, size_t buf_size, + const char* curr_pl_name) +{ + bool do_save = false; + while (!do_save && !remove_extension(pl_name) && + !kbd_input(pl_name, buf_size - 7, NULL)) + { + do_save = true; + apply_playlist_extension(pl_name, buf_size); + + /* warn before overwriting existing (different) playlist */ + if ((!curr_pl_name || strcmp(curr_pl_name, pl_name)) && + file_exists(pl_name)) + do_save = confirm_overwrite_yesno() == YESNO_YES; + } + return do_save; +} + static int (*ctx_add_to_playlist)(const char* playlist, bool new_playlist); bool catalog_add_to_a_playlist(const char* sel, int sel_attr, bool new_playlist, char *m3u8name, @@ -404,7 +441,6 @@ bool catalog_add_to_a_playlist(const char* sel, int sel_attr, if (new_playlist) { - size_t len; if (m3u8name == NULL) { const char *name; @@ -425,14 +461,10 @@ bool catalog_add_to_a_playlist(const char* sel, int sel_attr, else strmemccpy(playlist, m3u8name, MAX_PATH); - if (kbd_input(playlist, MAX_PATH, NULL)) + apply_playlist_extension(playlist, sizeof(playlist)); + + if (!catalog_pick_new_playlist_name(playlist, sizeof(playlist), NULL)) return false; - - len = strlen(playlist); - if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u")) - strlcat(playlist, "8", sizeof(playlist)); - else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8")) - strlcat(playlist, ".m3u8", sizeof(playlist)); } else { diff --git a/apps/playlist_catalog.h b/apps/playlist_catalog.h index fb71821093..756a1e1129 100644 --- a/apps/playlist_catalog.h +++ b/apps/playlist_catalog.h @@ -33,6 +33,9 @@ void catalog_set_directory(const char* directory); */ bool catalog_view_playlists(void); +bool catalog_pick_new_playlist_name(char *pl_name, size_t buf_size, + const char* curr_pl_name); + /* * Add something to a playlist (new or select from list of playlists in * catalog). -- cgit v1.2.3