Code

screen_lyrics: put code for reloading in a function
[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.
9  *
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.
14  *
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_status.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;
52         guint loader_timeout;
53 } current;
55 static void
56 screen_lyrics_abort(void)
57 {
58         if (current.loader != NULL) {
59                 plugin_stop(current.loader);
60                 current.loader = NULL;
61         }
63         if (current.loader_timeout != 0) {
64                 g_source_remove(current.loader_timeout);
65                 current.loader_timeout = 0;
66         }
68         if (current.plugin_name != NULL) {
69                 g_free(current.plugin_name);
70                 current.plugin_name = NULL;
71         }
73         if (current.artist != NULL) {
74                 g_free(current.artist);
75                 current.artist = NULL;
76         }
78         if (current.title != NULL) {
79                 g_free(current.title);
80                 current.title = NULL;
81         }
83         if (current.song != NULL) {
84                 mpd_song_free(current.song);
85                 current.song = NULL;
86         }
87 }
89 /**
90  * Repaint and update the screen, if it is currently active.
91  */
92 static void
93 lyrics_repaint_if_active(void)
94 {
95         if (screen_is_visible(&screen_lyrics)) {
96                 screen_text_repaint(&text);
98                 /* XXX repaint the screen title */
99         }
102 static void
103 path_lyr_file(char *path, size_t size,
104                 const char *artist, const char *title)
106         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
107                         getenv("HOME"), artist, title);
110 static bool
111 exists_lyr_file(const char *artist, const char *title)
113         char path[1024];
114         struct stat result;
116         path_lyr_file(path, 1024, artist, title);
118         return (stat(path, &result) == 0);
121 static FILE *
122 create_lyr_file(const char *artist, const char *title)
124         char path[1024];
126         snprintf(path, 1024, "%s/.lyrics",
127                  getenv("HOME"));
128         mkdir(path, S_IRWXU);
130         path_lyr_file(path, 1024, artist, title);
132         return fopen(path, "w");
135 static int
136 store_lyr_hd(void)
138         FILE *lyr_file;
139         unsigned i;
141         lyr_file = create_lyr_file(current.artist, current.title);
142         if (lyr_file == NULL)
143                 return -1;
145         for (i = 0; i < text.lines->len; ++i)
146                 fprintf(lyr_file, "%s\n",
147                         (const char*)g_ptr_array_index(text.lines, i));
149         fclose(lyr_file);
150         return 0;
153 static int
154 delete_lyr_hd(void)
156         char path[1024];
158         if (!exists_lyr_file(current.artist, current.title))
159                 return -1;
161         path_lyr_file(path, 1024, current.artist, current.title);
162         if (unlink(path) != 0)
163                 return -2;
165         return 0;
168 static void
169 screen_lyrics_set(const GString *str)
171         screen_text_set(&text, str);
173         /* paint new data */
175         lyrics_repaint_if_active();
178 static void
179 screen_lyrics_callback(const GString *result, const bool success,
180                        const char *plugin_name, G_GNUC_UNUSED void *data)
182         assert(current.loader != NULL);
184         current.plugin_name = g_strdup(plugin_name);
186         /* Display result, which may be lyrics or error messages */
187         if (result != NULL)
188                 screen_lyrics_set(result);
190         if (success == true) {
191                 if (options.lyrics_autosave &&
192                     !exists_lyr_file(current.artist, current.title))
193                         store_lyr_hd();
194         } else {
195                 /* translators: no lyrics were found for the song */
196                 screen_status_message (_("No lyrics"));
197         }
199         if (current.loader_timeout != 0) {
200                 g_source_remove(current.loader_timeout);
201                 current.loader_timeout = 0;
202         }
204         plugin_stop(current.loader);
205         current.loader = NULL;
208 static gboolean
209 screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
211         plugin_stop(current.loader);
212         current.loader = NULL;
214         screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
215                              options.lyrics_timeout);
217         current.loader_timeout = 0;
218         return FALSE;
221 static void
222 screen_lyrics_load(const struct mpd_song *song)
224         const char *artist, *title;
226         assert(song != NULL);
228         screen_lyrics_abort();
229         screen_text_clear(&text);
231         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
232         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
234         current.song = mpd_song_dup(song);
235         current.artist = g_strdup(artist);
236         current.title = g_strdup(title);
238         current.loader = lyrics_load(current.artist, current.title,
239                                      screen_lyrics_callback, NULL);
241         if (options.lyrics_timeout != 0) {
242                 current.loader_timeout =
243                         g_timeout_add_seconds(options.lyrics_timeout,
244                                               screen_lyrics_timeout_callback,
245                                               NULL);
246         }
249 static void
250 screen_lyrics_reload(void)
252         if (current.loader == NULL && current.artist != NULL &&
253             current.title != NULL) {
254                 current.loader = lyrics_load(current.artist, current.title,
255                                              screen_lyrics_callback, NULL);
256                 screen_text_repaint(&text);
257         }
260 static void
261 lyrics_screen_init(WINDOW *w, int cols, int rows)
263         screen_text_init(&text, w, cols, rows);
266 static void
267 lyrics_resize(int cols, int rows)
269         screen_text_resize(&text, cols, rows);
272 static void
273 lyrics_exit(void)
275         screen_lyrics_abort();
277         screen_text_deinit(&text);
280 static void
281 lyrics_open(struct mpdclient *c)
283         const struct mpd_song *next_song_c =
284                 next_song != NULL ? next_song : c->song;
286         if (next_song_c != NULL &&
287             (current.song == NULL ||
288              strcmp(mpd_song_get_uri(next_song_c),
289                     mpd_song_get_uri(current.song)) != 0))
290                 screen_lyrics_load(next_song_c);
292         if (next_song != NULL) {
293                 mpd_song_free(next_song);
294                 next_song = NULL;
295         }
298 static void
299 lyrics_update(struct mpdclient *c)
301         if (!follow)
302                 return;
304         if (c->song != NULL &&
305             (current.song == NULL ||
306              strcmp(mpd_song_get_uri(c->song),
307                     mpd_song_get_uri(current.song)) != 0))
308                 screen_lyrics_load(c->song);
311 static const char *
312 lyrics_title(char *str, size_t size)
314         if (current.loader != NULL) {
315                 snprintf(str, size, "%s (%s)",
316                          _("Lyrics"),
317                          /* translators: this message is displayed
318                             while data is retrieved */
319                          _("loading..."));
320                 return str;
321         } else if (current.artist != NULL && current.title != NULL &&
322                    !screen_text_is_empty(&text)) {
323                 int n;
324                 n = snprintf(str, size, "%s: %s - %s",
325                              _("Lyrics"),
326                              current.artist, current.title);
328                 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
329                     (unsigned int) n < size - 1)
330                         snprintf(str + n, size - n, " (%s)",
331                                  current.plugin_name);
333                 return str;
334         } else
335                 return _("Lyrics");
338 static void
339 lyrics_paint(void)
341         screen_text_paint(&text);
344 static bool
345 lyrics_cmd(struct mpdclient *c, command_t cmd)
347         if (screen_text_cmd(&text, c, cmd))
348                 return true;
350         switch(cmd) {
351         case CMD_INTERRUPT:
352                 if (current.loader != NULL) {
353                         screen_lyrics_abort();
354                         screen_text_clear(&text);
355                 }
356                 return true;
357         case CMD_SAVE_PLAYLIST:
358                 if (current.loader == NULL && current.artist != NULL &&
359                     current.title != NULL && store_lyr_hd() == 0)
360                         /* lyrics for the song were saved on hard disk */
361                         screen_status_message (_("Lyrics saved"));
362                 return true;
363         case CMD_DELETE:
364                 if (current.loader == NULL && current.artist != NULL &&
365                     current.title != NULL) {
366                         switch (delete_lyr_hd()) {
367                         case 0:
368                                 screen_status_message (_("Lyrics deleted"));
369                                 break;
370                         case -1:
371                                 screen_status_message (_("No saved lyrics"));
372                                 break;
373                         }
374                 }
375                 return true;
376         case CMD_LYRICS_UPDATE:
377                 if (c->song != NULL) {
378                         screen_lyrics_load(c->song);
379                         screen_text_repaint(&text);
380                 }
381                 return true;
382         case CMD_SELECT:
383                 screen_lyrics_reload();
384                 return true;
386 #ifdef ENABLE_SONG_SCREEN
387         case CMD_SCREEN_SONG:
388                 if (current.song != NULL) {
389                         screen_song_switch(c, current.song);
390                         return true;
391                 }
393                 break;
394 #endif
395         case CMD_SCREEN_SWAP:
396                 screen_swap(c, current.song);
397                 return true;
399         case CMD_LOCATE:
400                 if (current.song != NULL) {
401                         screen_file_goto_song(c, current.song);
402                         return true;
403                 }
405                 return false;
407         default:
408                 break;
409         }
411         return false;
414 const struct screen_functions screen_lyrics = {
415         .init = lyrics_screen_init,
416         .exit = lyrics_exit,
417         .open = lyrics_open,
418         .update = lyrics_update,
419         .close = NULL,
420         .resize = lyrics_resize,
421         .paint = lyrics_paint,
422         .cmd = lyrics_cmd,
423         .get_title = lyrics_title,
424 };
426 void
427 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
429         assert(song != NULL);
431         follow = f;
432         next_song = mpd_song_dup(song);
433         screen_switch(&screen_lyrics, c);