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);
316 if (options.lyrics_show_plugin && current.plugin_name != NULL)
317 snprintf(str + n, size - n, " (%s)",
318 current.plugin_name);
319 return str;
320 } else
321 return _("Lyrics");
322 }
324 static void
325 lyrics_paint(void)
326 {
327 screen_text_paint(&text);
328 }
330 static bool
331 lyrics_cmd(struct mpdclient *c, command_t cmd)
332 {
333 if (screen_text_cmd(&text, c, cmd))
334 return true;
336 switch(cmd) {
337 case CMD_INTERRUPT:
338 if (current.loader != NULL) {
339 screen_lyrics_abort();
340 screen_text_clear(&text);
341 }
342 return true;
343 case CMD_SAVE_PLAYLIST:
344 if (current.loader == NULL && current.artist != NULL &&
345 current.title != NULL && store_lyr_hd() == 0)
346 /* lyrics for the song were saved on hard disk */
347 screen_status_message (_("Lyrics saved"));
348 return true;
349 case CMD_DELETE:
350 if (current.loader == NULL && current.artist != NULL &&
351 current.title != NULL) {
352 switch (delete_lyr_hd()) {
353 case 0:
354 screen_status_message (_("Lyrics deleted"));
355 break;
356 case -1:
357 screen_status_message (_("No saved lyrics"));
358 break;
359 }
360 }
361 return true;
362 case CMD_LYRICS_UPDATE:
363 if (c->song != NULL) {
364 screen_lyrics_load(c->song);
365 screen_text_repaint(&text);
366 }
367 return true;
368 case CMD_SELECT:
369 if (current.loader == NULL && current.artist != NULL &&
370 current.title != NULL) {
371 current.loader = lyrics_load(current.artist, current.title,
372 screen_lyrics_callback, NULL);
373 screen_text_repaint(&text);
374 }
375 return true;
377 #ifdef ENABLE_SONG_SCREEN
378 case CMD_SCREEN_SONG:
379 if (current.song != NULL) {
380 screen_song_switch(c, current.song);
381 return true;
382 }
384 break;
385 #endif
386 case CMD_SCREEN_SWAP:
387 screen_swap(c, current.song);
388 return true;
390 case CMD_LOCATE:
391 if (current.song != NULL) {
392 screen_file_goto_song(c, current.song);
393 return true;
394 }
396 return false;
398 default:
399 break;
400 }
402 return false;
403 }
405 const struct screen_functions screen_lyrics = {
406 .init = lyrics_screen_init,
407 .exit = lyrics_exit,
408 .open = lyrics_open,
409 .update = lyrics_update,
410 .close = NULL,
411 .resize = lyrics_resize,
412 .paint = lyrics_paint,
413 .cmd = lyrics_cmd,
414 .get_title = lyrics_title,
415 };
417 void
418 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
419 {
420 assert(song != NULL);
422 follow = f;
423 next_song = mpd_song_dup(song);
424 screen_switch(&screen_lyrics, c);
425 }