Code

automatically save lyrics
authorThomas Jansen <mithi@mithi.net>
Wed, 9 Sep 2009 07:51:35 +0000 (09:51 +0200)
committerThomas Jansen <mithi@mithi.net>
Wed, 9 Sep 2009 09:59:01 +0000 (11:59 +0200)
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
doc/config.sample
doc/ncmpc.1
src/conf.c
src/options.c
src/options.h
src/screen_lyrics.c

diff --git a/NEWS b/NEWS
index 33925b3dae102c1d73688da155fe8bf916d84bdc..6f09d2651a04a1f7f72a07df6ab84868a4cb9e13 100644 (file)
--- 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
index ba37c98fae04a4cd41dd5b22423163df68b4a656..e064ebbedbe72dd9fd8558247ae61291617ffc92 100644 (file)
@@ -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
index c0006635e114b4f16a55fbe697c2c0a774ae6089..13f2eb268118a92d6df9d3e728f4cd78c5286266 100644 (file)
@@ -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
index e60fb8f952de4cbbff24bd1fbb53055385206b91..a7d6ff0cf7609dfab786fdc71e943a627e03195e 100644 (file)
@@ -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;
index 99dddedd236f163348f9452220a2bdda181ae704..05c91c95fc719059ad24943634ed56dc3de77304 100644 (file)
@@ -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,
index 1cc2eb7f4f683abf9d9993fd0e6f40bfd4e385cf..1fc042455948be58082332541859a27b9165a044 100644 (file)
@@ -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;
index 221b7cdb4ce87f69425158f2ffd3ef51d5e7f0cb..51bb903a88ad108edb153d7a9f946433021646fd 100644 (file)
@@ -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)
 {