Code

use libmpdclient2
[ncmpc.git] / src / screen_lyrics.c
index ce11b4473e469fdf033719e513ef7e06d1014be7..5714258dbc3d7f5d8958d2edd951188e9befda2c 100644 (file)
@@ -1,21 +1,21 @@
-/*
- * (c) 2006 by Kalle Wallin <kaw@linux.se>
- * Copyright (C) 2008 Max Kellermann <max@duempel.org>
- *
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2009 The Music Player Daemon Project
+ * Project homepage: http://musicpd.org
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *
+
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- */
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
 
 #include <sys/stat.h>
 #include "i18n.h"
@@ -35,7 +35,8 @@
 
 static struct screen_text text;
 
-static const struct mpd_song *next_song;
+static struct mpd_song *next_song;
+static bool follow = false;
 
 static struct {
        struct mpd_song *song;
@@ -64,7 +65,7 @@ screen_lyrics_abort(void)
        }
 
        if (current.song != NULL) {
-               mpd_freeSong(current.song);
+               mpd_song_free(current.song);
                current.song = NULL;
        }
 }
@@ -82,6 +83,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 +136,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
@@ -117,7 +167,7 @@ screen_lyrics_load(const struct mpd_song *song)
        screen_lyrics_abort();
        screen_text_clear(&text);
 
-       current.song = mpd_songDup(song);
+       current.song = mpd_song_dup(song);
 
        strfsong(buffer, sizeof(buffer), "%artist%", song);
        current.artist = g_strdup(buffer);
@@ -129,37 +179,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)
 {
@@ -188,12 +207,31 @@ lyrics_open(mpdclient_t *c)
 
        if (next_song != NULL &&
            (current.song == NULL ||
-            strcmp(next_song->file, current.song->file) != 0))
+            strcmp(mpd_song_get_uri(next_song),
+                   mpd_song_get_uri(current.song)) != 0))
                screen_lyrics_load(next_song);
 
+       if (next_song != c->song)
+               mpd_song_free(next_song);
        next_song = NULL;
 }
 
+static void
+lyrics_update(mpdclient_t *c)
+{
+       if (!follow)
+               return;
+
+       next_song = c->song;
+
+       if (next_song != NULL &&
+           (current.song == NULL ||
+            strcmp(mpd_song_get_uri(next_song),
+                   mpd_song_get_uri(current.song)) != 0))
+               screen_lyrics_load(next_song);
+
+       next_song = NULL;
+}
 
 static const char *
 lyrics_title(char *str, size_t size)
@@ -234,7 +272,7 @@ lyrics_cmd(mpdclient_t *c, command_t cmd)
                        screen_text_clear(&text);
                }
                return true;
-       case CMD_ADD:
+       case CMD_SAVE_PLAYLIST:
                if (current.loader == NULL && current.artist != NULL &&
                    current.title != NULL && store_lyr_hd() == 0)
                        /* lyrics for the song were saved on hard disk */
@@ -248,7 +286,7 @@ lyrics_cmd(mpdclient_t *c, command_t cmd)
                return true;
 
 #ifdef ENABLE_SONG_SCREEN
-       case CMD_VIEW:
+       case CMD_SCREEN_SONG:
                if (current.song != NULL) {
                        screen_song_switch(c, current.song);
                        return true;
@@ -256,6 +294,9 @@ lyrics_cmd(mpdclient_t *c, command_t cmd)
 
                break;
 #endif
+       case CMD_SCREEN_SWAP:
+               screen_swap(c, current.song);
+               return true;
 
        case CMD_LOCATE:
                if (current.song != NULL) {
@@ -276,6 +317,7 @@ const struct screen_functions screen_lyrics = {
        .init = lyrics_screen_init,
        .exit = lyrics_exit,
        .open = lyrics_open,
+       .update = lyrics_update,
        .close = NULL,
        .resize = lyrics_resize,
        .paint = lyrics_paint,
@@ -284,10 +326,11 @@ const struct screen_functions screen_lyrics = {
 };
 
 void
-screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
+screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
 {
        assert(song != NULL);
 
-       next_song = song;
+       follow = f;
+       next_song = mpd_song_dup(song);
        screen_switch(&screen_lyrics, c);
 }