1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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;
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 }
100 }
102 static void
103 path_lyr_file(char *path, size_t size,
104 const char *artist, const char *title)
105 {
106 snprintf(path, size, "%s/.lyrics/%s - %s.txt",
107 getenv("HOME"), artist, title);
108 }
110 static bool
111 exists_lyr_file(const char *artist, const char *title)
112 {
113 char path[1024];
114 struct stat result;
116 path_lyr_file(path, 1024, artist, title);
118 return (stat(path, &result) == 0);
119 }
121 static FILE *
122 create_lyr_file(const char *artist, const char *title)
123 {
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");
133 }
135 static int
136 store_lyr_hd(void)
137 {
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;
151 }
153 static int
154 delete_lyr_hd(void)
155 {
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;
166 }
168 static void
169 screen_lyrics_set(const GString *str)
170 {
171 screen_text_set(&text, str);
173 /* paint new data */
175 lyrics_repaint_if_active();
176 }
178 static void
179 screen_lyrics_callback(const GString *result, const bool success,
180 const char *plugin_name, G_GNUC_UNUSED void *data)
181 {
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;
206 }
208 static gboolean
209 screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
210 {
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;
219 }
221 static void
222 screen_lyrics_load(const struct mpd_song *song)
223 {
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 }
247 }
249 static void
250 lyrics_screen_init(WINDOW *w, int cols, int rows)
251 {
252 screen_text_init(&text, w, cols, rows);
253 }
255 static void
256 lyrics_resize(int cols, int rows)
257 {
258 screen_text_resize(&text, cols, rows);
259 }
261 static void
262 lyrics_exit(void)
263 {
264 screen_lyrics_abort();
266 screen_text_deinit(&text);
267 }
269 static void
270 lyrics_open(struct mpdclient *c)
271 {
272 const struct mpd_song *next_song_c =
273 next_song != NULL ? next_song : c->song;
275 if (next_song_c != NULL &&
276 (current.song == NULL ||
277 strcmp(mpd_song_get_uri(next_song_c),
278 mpd_song_get_uri(current.song)) != 0))
279 screen_lyrics_load(next_song_c);
281 if (next_song != NULL) {
282 mpd_song_free(next_song);
283 next_song = NULL;
284 }
285 }
287 static void
288 lyrics_update(struct mpdclient *c)
289 {
290 if (!follow)
291 return;
293 if (c->song != NULL &&
294 (current.song == NULL ||
295 strcmp(mpd_song_get_uri(c->song),
296 mpd_song_get_uri(current.song)) != 0))
297 screen_lyrics_load(c->song);
298 }
300 static const char *
301 lyrics_title(char *str, size_t size)
302 {
303 if (current.loader != NULL) {
304 snprintf(str, size, "%s (%s)",
305 _("Lyrics"),
306 /* translators: this message is displayed
307 while data is retrieved */
308 _("loading..."));
309 return str;
310 } else if (current.artist != NULL && current.title != NULL &&
311 !screen_text_is_empty(&text)) {
312 int n;
313 n = snprintf(str, size, "%s: %s - %s",
314 _("Lyrics"),
315 current.artist, current.title);
317 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
318 (unsigned int) n < size - 1)
319 snprintf(str + n, size - n, " (%s)",
320 current.plugin_name);
322 return str;
323 } else
324 return _("Lyrics");
325 }
327 static void
328 lyrics_paint(void)
329 {
330 screen_text_paint(&text);
331 }
333 static bool
334 lyrics_cmd(struct mpdclient *c, command_t cmd)
335 {
336 if (screen_text_cmd(&text, c, cmd))
337 return true;
339 switch(cmd) {
340 case CMD_INTERRUPT:
341 if (current.loader != NULL) {
342 screen_lyrics_abort();
343 screen_text_clear(&text);
344 }
345 return true;
346 case CMD_SAVE_PLAYLIST:
347 if (current.loader == NULL && current.artist != NULL &&
348 current.title != NULL && store_lyr_hd() == 0)
349 /* lyrics for the song were saved on hard disk */
350 screen_status_message (_("Lyrics saved"));
351 return true;
352 case CMD_DELETE:
353 if (current.loader == NULL && current.artist != NULL &&
354 current.title != NULL) {
355 switch (delete_lyr_hd()) {
356 case 0:
357 screen_status_message (_("Lyrics deleted"));
358 break;
359 case -1:
360 screen_status_message (_("No saved lyrics"));
361 break;
362 }
363 }
364 return true;
365 case CMD_LYRICS_UPDATE:
366 if (c->song != NULL) {
367 screen_lyrics_load(c->song);
368 screen_text_repaint(&text);
369 }
370 return true;
371 case CMD_SELECT:
372 if (current.loader == NULL && current.artist != NULL &&
373 current.title != NULL) {
374 current.loader = lyrics_load(current.artist, current.title,
375 screen_lyrics_callback, NULL);
376 screen_text_repaint(&text);
377 }
378 return true;
380 #ifdef ENABLE_SONG_SCREEN
381 case CMD_SCREEN_SONG:
382 if (current.song != NULL) {
383 screen_song_switch(c, current.song);
384 return true;
385 }
387 break;
388 #endif
389 case CMD_SCREEN_SWAP:
390 screen_swap(c, current.song);
391 return true;
393 case CMD_LOCATE:
394 if (current.song != NULL) {
395 screen_file_goto_song(c, current.song);
396 return true;
397 }
399 return false;
401 default:
402 break;
403 }
405 return false;
406 }
408 const struct screen_functions screen_lyrics = {
409 .init = lyrics_screen_init,
410 .exit = lyrics_exit,
411 .open = lyrics_open,
412 .update = lyrics_update,
413 .close = NULL,
414 .resize = lyrics_resize,
415 .paint = lyrics_paint,
416 .cmd = lyrics_cmd,
417 .get_title = lyrics_title,
418 };
420 void
421 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
422 {
423 assert(song != NULL);
425 follow = f;
426 next_song = mpd_song_dup(song);
427 screen_switch(&screen_lyrics, c);
428 }