From 993a8bacfd80d7afb7095a923b536d22e1336dc2 Mon Sep 17 00:00:00 2001 From: Thomas Jansen Date: Wed, 9 Sep 2009 09:51:35 +0200 Subject: [PATCH] automatically save lyrics Added the option "lyrics-autosave" to save lyrics automatically after receiving them. By default, the option is off. If the option is enabled, lyrics are only saved if no such file exists, so we don't accidentally overwrite lyrics. --- NEWS | 1 + doc/config.sample | 3 ++ doc/ncmpc.1 | 3 ++ src/conf.c | 7 ++++ src/options.c | 1 + src/options.h | 1 + src/screen_lyrics.c | 80 +++++++++++++++++++++++++++------------------ 7 files changed, 65 insertions(+), 31 deletions(-) diff --git a/NEWS b/NEWS index 33925b3..6f09d26 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,5 @@ ncmpc 0.15 - not yet released +* added the "lyrics-autosave" option * added CMD_SELECT_PLAYING * display song duration in the playlist * added the "hardware_cursor" option diff --git a/doc/config.sample b/doc/config.sample index ba37c98..e064ebb 100644 --- a/doc/config.sample +++ b/doc/config.sample @@ -98,6 +98,9 @@ ## The format used to for the xterm title when ncmpc is playing. #xterm-title-format = "ncmpc: [ %name%|[%artist% - ]%title%|%file%]" +## Automatically save the lyrics after receiving them. +#lyrics-autosave = no + ############## Colors ####################### ## ## base colors: black, red, green, yellow, blue, magenta, cyan, white diff --git a/doc/ncmpc.1 b/doc/ncmpc.1 index c000663..13f2eb2 100644 --- a/doc/ncmpc.1 +++ b/doc/ncmpc.1 @@ -120,6 +120,9 @@ Quits downloading lyrics of a song after the timeout of NUM seconds is reached. .TP .B jump\-prefix\-only = yes|no When using the jump command, search for the prefix of an entry. That means typing "m" will start to the first entry which begins with "m". +.TP +.B lyrics\-autosave = yes|no +Automatically save lyrics after receiving them. .SS Display .TP .B welcome\-screen\-list = yes|no diff --git a/src/conf.c b/src/conf.c index e60fb8f..a7d6ff0 100644 --- a/src/conf.c +++ b/src/conf.c @@ -76,6 +76,7 @@ #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list" #define CONF_DISPLAY_TIME "display-time" #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only" +#define CONF_LYRICS_AUTOSAVE "lyrics-autosave" static bool str2bool(char *str) @@ -514,6 +515,12 @@ parse_line(char *line) {} #else options.jump_prefix_only = str2bool(value); +#endif + else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name)) +#ifdef ENABLE_LYRICS_SCREEN + options.lyrics_autosave = str2bool(value); +#else + {} #endif else match_found = false; diff --git a/src/options.c b/src/options.c index 99ddded..05c91c9 100644 --- a/src/options.c +++ b/src/options.c @@ -54,6 +54,7 @@ options_t options = { .seek_time = 1, #ifdef ENABLE_LYRICS_SCREEN .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT, + .lyrics_autosave = false, #endif .find_wrap = true, .scroll_offset = 0, diff --git a/src/options.h b/src/options.h index 1cc2eb7..1fc0424 100644 --- a/src/options.h +++ b/src/options.h @@ -49,6 +49,7 @@ typedef struct { int seek_time; #ifdef ENABLE_LYRICS_SCREEN int lyrics_timeout; + bool lyrics_autosave; #endif bool find_wrap; bool find_show_last_pattern; diff --git a/src/screen_lyrics.c b/src/screen_lyrics.c index 221b7cd..51bb903 100644 --- a/src/screen_lyrics.c +++ b/src/screen_lyrics.c @@ -82,6 +82,51 @@ lyrics_repaint_if_active(void) } } +static bool +exists_lyr_file(const char *artist, const char *title) +{ + char path[1024]; + struct stat result; + + snprintf(path, 1024, "%s/.lyrics/%s - %s.txt", + getenv("HOME"), artist, title); + + return (stat(path, &result) == 0); +} + +static FILE * +create_lyr_file(const char *artist, const char *title) +{ + char path[1024]; + + snprintf(path, 1024, "%s/.lyrics", + getenv("HOME")); + mkdir(path, S_IRWXU); + + snprintf(path, 1024, "%s/.lyrics/%s - %s.txt", + getenv("HOME"), artist, title); + + return fopen(path, "w"); +} + +static int +store_lyr_hd(void) +{ + FILE *lyr_file; + unsigned i; + + lyr_file = create_lyr_file(current.artist, current.title); + if (lyr_file == NULL) + return -1; + + for (i = 0; i < text.lines->len; ++i) + fprintf(lyr_file, "%s\n", + (const char*)g_ptr_array_index(text.lines, i)); + + fclose(lyr_file); + return 0; +} + static void screen_lyrics_set(const GString *str) { @@ -90,6 +135,10 @@ screen_lyrics_set(const GString *str) /* paint new data */ lyrics_repaint_if_active(); + + if (options.lyrics_autosave && + !exists_lyr_file(current.artist, current.title)) + store_lyr_hd(); } static void @@ -129,37 +178,6 @@ screen_lyrics_load(const struct mpd_song *song) screen_lyrics_callback, NULL); } -static FILE *create_lyr_file(const char *artist, const char *title) -{ - char path[1024]; - - snprintf(path, 1024, "%s/.lyrics", - getenv("HOME")); - mkdir(path, S_IRWXU); - - snprintf(path, 1024, "%s/.lyrics/%s - %s.txt", - getenv("HOME"), artist, title); - - return fopen(path, "w"); -} - -static int store_lyr_hd(void) -{ - FILE *lyr_file; - unsigned i; - - lyr_file = create_lyr_file(current.artist, current.title); - if (lyr_file == NULL) - return -1; - - for (i = 0; i < text.lines->len; ++i) - fprintf(lyr_file, "%s\n", - (const char*)g_ptr_array_index(text.lines, i)); - - fclose(lyr_file); - return 0; -} - static void lyrics_screen_init(WINDOW *w, int cols, int rows) { -- 2.30.2