Code

screen_lyrics.c: wrapped some duplicate code
[ncmpc.git] / src / screen_lyrics.c
index abf1055661e827c8577c66bd65f2a9141873e97d..6cb7b46efddf71590f40d010936a701f18b80d46 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  
  * This program is free software; you can redistribute it and/or modify
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#include <sys/stat.h>
+#include "screen_lyrics.h"
+#include "screen_interface.h"
+#include "screen_message.h"
+#include "screen_file.h"
+#include "screen_song.h"
 #include "i18n.h"
 #include "options.h"
 #include "mpdclient.h"
-#include "command.h"
 #include "screen.h"
-#include "strfsong.h"
 #include "lyrics.h"
 #include "screen_text.h"
 
+#include <assert.h>
+#include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -83,14 +87,21 @@ lyrics_repaint_if_active(void)
        }
 }
 
+static void
+path_lyr_file(char *path, size_t size,
+               const char *artist, const char *title)
+{
+       snprintf(path, size, "%s/.lyrics/%s - %s.txt",
+                       getenv("HOME"), artist, title);
+}
+
 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);
+       path_lyr_file(path, 1024, artist, title);
 
        return (stat(path, &result) == 0);
 }
@@ -104,8 +115,7 @@ create_lyr_file(const char *artist, const char *title)
                 getenv("HOME"));
        mkdir(path, S_IRWXU);
 
-       snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
-                getenv("HOME"), artist, title);
+       path_lyr_file(path, 1024, artist, title);
 
        return fopen(path, "w");
 }
@@ -136,22 +146,26 @@ 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
-screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
+screen_lyrics_callback(const GString *result, const bool success,
+                      G_GNUC_UNUSED void *data)
 {
        assert(current.loader != NULL);
 
+       /* Display result, which may be lyrics or error messages */
        if (result != NULL)
                screen_lyrics_set(result);
-       else
+
+       if (success == true) {
+               if (options.lyrics_autosave &&
+                   !exists_lyr_file(current.artist, current.title))
+                       store_lyr_hd();
+       } else {
                /* translators: no lyrics were found for the song */
                screen_status_message (_("No lyrics"));
+       }
 
        plugin_stop(current.loader);
        current.loader = NULL;
@@ -160,20 +174,19 @@ screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
 static void
 screen_lyrics_load(const struct mpd_song *song)
 {
-       char buffer[MAX_SONGNAME_LENGTH];
+       const char *artist, *title;
 
        assert(song != NULL);
 
        screen_lyrics_abort();
        screen_text_clear(&text);
 
-       current.song = mpd_song_dup(song);
-
-       strfsong(buffer, sizeof(buffer), "%artist%", song);
-       current.artist = g_strdup(buffer);
+       artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
+       title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
 
-       strfsong(buffer, sizeof(buffer), "%title%", song);
-       current.title = g_strdup(buffer);
+       current.song = mpd_song_dup(song);
+       current.artist = g_strdup(artist);
+       current.title = g_strdup(title);
 
        current.loader = lyrics_load(current.artist, current.title,
                                     screen_lyrics_callback, NULL);