Code

ec723e9e2ee98e5820c9c6adaf3ccf907e5261d3
[ncmpc.git] / src / screen_lyrics.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_lyrics.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "screen_file.h"
24 #include "screen_song.h"
25 #include "i18n.h"
26 #include "options.h"
27 #include "mpdclient.h"
28 #include "screen.h"
29 #include "lyrics.h"
30 #include "screen_text.h"
32 #include <assert.h>
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <unistd.h>
38 #include <stdio.h>
40 static struct screen_text text;
42 static struct mpd_song *next_song;
43 static bool follow = false;
45 static struct {
46         struct mpd_song *song;
48         char *artist, *title, *plugin_name;
50         struct plugin_cycle *loader;
51 } current;
53 static void
54 screen_lyrics_abort(void)
55 {
56         if (current.loader != NULL) {
57                 plugin_stop(current.loader);
58                 current.loader = NULL;
59         }
61         if (current.plugin_name != NULL) {
62                 g_free(current.plugin_name);
63                 current.plugin_name = NULL;
64         }
66         if (current.artist != NULL) {
67                 g_free(current.artist);
68                 current.artist = NULL;
69         }
71         if (current.title != NULL) {
72                 g_free(current.title);
73                 current.artist = NULL;
74         }
76         if (current.song != NULL) {
77                 mpd_song_free(current.song);
78                 current.song = NULL;
79         }
80 }
82 /**
83  * Repaint and update the screen, if it is currently active.
84  */
85 static void
86 lyrics_repaint_if_active(void)
87 {
88         if (screen_is_visible(&screen_lyrics)) {
89                 screen_text_repaint(&text);
91                 /* XXX repaint the screen title */
92         }
93 }
95 static void
96 path_lyr_file(char *path, size_t size,
97                 const char *artist, const char *title)
98 {
99         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
100                         getenv("HOME"), artist, title);
103 static bool
104 exists_lyr_file(const char *artist, const char *title)
106         char path[1024];
107         struct stat result;
109         path_lyr_file(path, 1024, artist, title);
111         return (stat(path, &result) == 0);
114 static FILE *
115 create_lyr_file(const char *artist, const char *title)
117         char path[1024];
119         snprintf(path, 1024, "%s/.lyrics",
120                  getenv("HOME"));
121         mkdir(path, S_IRWXU);
123         path_lyr_file(path, 1024, artist, title);
125         return fopen(path, "w");
128 static int
129 store_lyr_hd(void)
131         FILE *lyr_file;
132         unsigned i;
134         lyr_file = create_lyr_file(current.artist, current.title);
135         if (lyr_file == NULL)
136                 return -1;
138         for (i = 0; i < text.lines->len; ++i)
139                 fprintf(lyr_file, "%s\n",
140                         (const char*)g_ptr_array_index(text.lines, i));
142         fclose(lyr_file);
143         return 0;
146 static int
147 delete_lyr_hd(void)
149         char path[1024];
151         if (!exists_lyr_file(current.artist, current.title))
152                 return -1;
154         path_lyr_file(path, 1024, current.artist, current.title);
155         if (unlink(path) != 0)
156                 return -2;
158         return 0;
161 static void
162 screen_lyrics_set(const GString *str)
164         screen_text_set(&text, str);
166         /* paint new data */
168         lyrics_repaint_if_active();
171 static void
172 screen_lyrics_callback(const GString *result, const bool success,
173                        const char *plugin_name, G_GNUC_UNUSED void *data)
175         assert(current.loader != NULL);
177         current.plugin_name = g_strdup(plugin_name);
179         /* Display result, which may be lyrics or error messages */
180         if (result != NULL)
181                 screen_lyrics_set(result);
183         if (success == true) {
184                 if (options.lyrics_autosave &&
185                     !exists_lyr_file(current.artist, current.title))
186                         store_lyr_hd();
187         } else {
188                 /* translators: no lyrics were found for the song */
189                 screen_status_message (_("No lyrics"));
190         }
192         plugin_stop(current.loader);
193         current.loader = NULL;
196 static void
197 screen_lyrics_load(const struct mpd_song *song)
199         const char *artist, *title;
201         assert(song != NULL);
203         screen_lyrics_abort();
204         screen_text_clear(&text);
206         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
207         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
209         current.song = mpd_song_dup(song);
210         current.artist = g_strdup(artist);
211         current.title = g_strdup(title);
213         current.loader = lyrics_load(current.artist, current.title,
214                                      screen_lyrics_callback, NULL);
217 static void
218 lyrics_screen_init(WINDOW *w, int cols, int rows)
220         screen_text_init(&text, w, cols, rows);
223 static void
224 lyrics_resize(int cols, int rows)
226         screen_text_resize(&text, cols, rows);
229 static void
230 lyrics_exit(void)
232         screen_lyrics_abort();
234         screen_text_deinit(&text);
237 static void
238 lyrics_open(struct mpdclient *c)
240         const struct mpd_song *next_song_c =
241                 next_song != NULL ? next_song : c->song;
243         if (next_song_c != NULL &&
244             (current.song == NULL ||
245              strcmp(mpd_song_get_uri(next_song_c),
246                     mpd_song_get_uri(current.song)) != 0))
247                 screen_lyrics_load(next_song_c);
249         if (next_song != NULL)
250                 mpd_song_free(next_song);
251         next_song = NULL;
254 static void
255 lyrics_update(struct mpdclient *c)
257         if (!follow)
258                 return;
260         if (c->song != NULL &&
261             (current.song == NULL ||
262              strcmp(mpd_song_get_uri(c->song),
263                     mpd_song_get_uri(current.song)) != 0))
264                 screen_lyrics_load(c->song);
267 static const char *
268 lyrics_title(char *str, size_t size)
270         if (current.loader != NULL) {
271                 snprintf(str, size, "%s (%s)",
272                          _("Lyrics"),
273                          /* translators: this message is displayed
274                             while data is retrieved */
275                          _("loading..."));
276                 return str;
277         } else if (current.artist != NULL && current.title != NULL &&
278                    !screen_text_is_empty(&text)) {
279                 int n;
280                 n = snprintf(str, size, "%s: %s - %s",
281                              _("Lyrics"),
282                              current.artist, current.title);
283                 if (options.lyrics_show_plugin && current.plugin_name != NULL)
284                         snprintf(str + n, size - n, " (%s)",
285                                  current.plugin_name);
286                 return str;
287         } else
288                 return _("Lyrics");
291 static void
292 lyrics_paint(void)
294         screen_text_paint(&text);
297 static bool
298 lyrics_cmd(struct mpdclient *c, command_t cmd)
300         if (screen_text_cmd(&text, c, cmd))
301                 return true;
303         switch(cmd) {
304         case CMD_INTERRUPT:
305                 if (current.loader != NULL) {
306                         screen_lyrics_abort();
307                         screen_text_clear(&text);
308                 }
309                 return true;
310         case CMD_SAVE_PLAYLIST:
311                 if (current.loader == NULL && current.artist != NULL &&
312                     current.title != NULL && store_lyr_hd() == 0)
313                         /* lyrics for the song were saved on hard disk */
314                         screen_status_message (_("Lyrics saved"));
315                 return true;
316         case CMD_DELETE:
317                 if (current.loader == NULL && current.artist != NULL &&
318                     current.title != NULL) {
319                         switch (delete_lyr_hd()) {
320                         case 0:
321                                 screen_status_message (_("Lyrics deleted"));
322                                 break;
323                         case -1:
324                                 screen_status_message (_("No saved lyrics"));
325                                 break;
326                         }
327                 }
328                 return true;
329         case CMD_LYRICS_UPDATE:
330                 if (c->song != NULL) {
331                         screen_lyrics_load(c->song);
332                         screen_text_repaint(&text);
333                 }
334                 return true;
335         case CMD_SELECT:
336                 if (current.loader == NULL && current.artist != NULL &&
337                     current.title != NULL) {
338                         current.loader = lyrics_load(current.artist, current.title,
339                                                      screen_lyrics_callback, NULL);
340                         screen_text_repaint(&text);
341                 }
342                 return true;
344 #ifdef ENABLE_SONG_SCREEN
345         case CMD_SCREEN_SONG:
346                 if (current.song != NULL) {
347                         screen_song_switch(c, current.song);
348                         return true;
349                 }
351                 break;
352 #endif
353         case CMD_SCREEN_SWAP:
354                 screen_swap(c, current.song);
355                 return true;
357         case CMD_LOCATE:
358                 if (current.song != NULL) {
359                         screen_file_goto_song(c, current.song);
360                         return true;
361                 }
363                 return false;
365         default:
366                 break;
367         }
369         return false;
372 const struct screen_functions screen_lyrics = {
373         .init = lyrics_screen_init,
374         .exit = lyrics_exit,
375         .open = lyrics_open,
376         .update = lyrics_update,
377         .close = NULL,
378         .resize = lyrics_resize,
379         .paint = lyrics_paint,
380         .cmd = lyrics_cmd,
381         .get_title = lyrics_title,
382 };
384 void
385 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
387         assert(song != NULL);
389         follow = f;
390         next_song = mpd_song_dup(song);
391         screen_switch(&screen_lyrics, c);