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 }
286 static void
287 lyrics_update(struct mpdclient *c)
288 {
289 if (!follow)
290 return;
292 if (c->song != NULL &&
293 (current.song == NULL ||
294 strcmp(mpd_song_get_uri(c->song),
295 mpd_song_get_uri(current.song)) != 0))
296 screen_lyrics_load(c->song);
297 }
299 static const char *
300 lyrics_title(char *str, size_t size)
301 {
302 if (current.loader != NULL) {
303 snprintf(str, size, "%s (%s)",
304 _("Lyrics"),
305 /* translators: this message is displayed
306 while data is retrieved */
307 _("loading..."));
308 return str;
309 } else if (current.artist != NULL && current.title != NULL &&
310 !screen_text_is_empty(&text)) {
311 int n;
312 n = snprintf(str, size, "%s: %s - %s",
313 _("Lyrics"),
314 current.artist, current.title);
315 if (options.lyrics_show_plugin && current.plugin_name != NULL)
316 snprintf(str + n, size - n, " (%s)",
317 current.plugin_name);
318 return str;
319 } else
320 return _("Lyrics");
321 }
323 static void
324 lyrics_paint(void)
325 {
326 screen_text_paint(&text);
327 }
329 static bool
330 lyrics_cmd(struct mpdclient *c, command_t cmd)
331 {
332 if (screen_text_cmd(&text, c, cmd))
333 return true;
335 switch(cmd) {
336 case CMD_INTERRUPT:
337 if (current.loader != NULL) {
338 screen_lyrics_abort();
339 screen_text_clear(&text);
340 }
341 return true;
342 case CMD_SAVE_PLAYLIST:
343 if (current.loader == NULL && current.artist != NULL &&
344 current.title != NULL && store_lyr_hd() == 0)
345 /* lyrics for the song were saved on hard disk */
346 screen_status_message (_("Lyrics saved"));
347 return true;
348 case CMD_DELETE:
349 if (current.loader == NULL && current.artist != NULL &&
350 current.title != NULL) {
351 switch (delete_lyr_hd()) {
352 case 0:
353 screen_status_message (_("Lyrics deleted"));
354 break;
355 case -1:
356 screen_status_message (_("No saved lyrics"));
357 break;
358 }
359 }
360 return true;
361 case CMD_LYRICS_UPDATE:
362 if (c->song != NULL) {
363 screen_lyrics_load(c->song);
364 screen_text_repaint(&text);
365 }
366 return true;
367 case CMD_SELECT:
368 if (current.loader == NULL && current.artist != NULL &&
369 current.title != NULL) {
370 current.loader = lyrics_load(current.artist, current.title,
371 screen_lyrics_callback, NULL);
372 screen_text_repaint(&text);
373 }
374 return true;
376 #ifdef ENABLE_SONG_SCREEN
377 case CMD_SCREEN_SONG:
378 if (current.song != NULL) {
379 screen_song_switch(c, current.song);
380 return true;
381 }
383 break;
384 #endif
385 case CMD_SCREEN_SWAP:
386 screen_swap(c, current.song);
387 return true;
389 case CMD_LOCATE:
390 if (current.song != NULL) {
391 screen_file_goto_song(c, current.song);
392 return true;
393 }
395 return false;
397 default:
398 break;
399 }
401 return false;
402 }
404 const struct screen_functions screen_lyrics = {
405 .init = lyrics_screen_init,
406 .exit = lyrics_exit,
407 .open = lyrics_open,
408 .update = lyrics_update,
409 .close = NULL,
410 .resize = lyrics_resize,
411 .paint = lyrics_paint,
412 .cmd = lyrics_cmd,
413 .get_title = lyrics_title,
414 };
416 void
417 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
418 {
419 assert(song != NULL);
421 follow = f;
422 next_song = mpd_song_dup(song);
423 screen_switch(&screen_lyrics, c);
424 }