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;
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.artist != NULL) {
62 g_free(current.artist);
63 current.artist = NULL;
64 }
66 if (current.title != NULL) {
67 g_free(current.title);
68 current.artist = NULL;
69 }
71 if (current.song != NULL) {
72 mpd_song_free(current.song);
73 current.song = NULL;
74 }
75 }
77 /**
78 * Repaint and update the screen, if it is currently active.
79 */
80 static void
81 lyrics_repaint_if_active(void)
82 {
83 if (screen_is_visible(&screen_lyrics)) {
84 screen_text_repaint(&text);
86 /* XXX repaint the screen title */
87 }
88 }
90 static void
91 path_lyr_file(char *path, size_t size,
92 const char *artist, const char *title)
93 {
94 snprintf(path, size, "%s/.lyrics/%s - %s.txt",
95 getenv("HOME"), artist, title);
96 }
98 static bool
99 exists_lyr_file(const char *artist, const char *title)
100 {
101 char path[1024];
102 struct stat result;
104 path_lyr_file(path, 1024, artist, title);
106 return (stat(path, &result) == 0);
107 }
109 static FILE *
110 create_lyr_file(const char *artist, const char *title)
111 {
112 char path[1024];
114 snprintf(path, 1024, "%s/.lyrics",
115 getenv("HOME"));
116 mkdir(path, S_IRWXU);
118 path_lyr_file(path, 1024, artist, title);
120 return fopen(path, "w");
121 }
123 static int
124 store_lyr_hd(void)
125 {
126 FILE *lyr_file;
127 unsigned i;
129 lyr_file = create_lyr_file(current.artist, current.title);
130 if (lyr_file == NULL)
131 return -1;
133 for (i = 0; i < text.lines->len; ++i)
134 fprintf(lyr_file, "%s\n",
135 (const char*)g_ptr_array_index(text.lines, i));
137 fclose(lyr_file);
138 return 0;
139 }
141 static int
142 delete_lyr_hd(void)
143 {
144 char path[1024];
146 if (!exists_lyr_file(current.artist, current.title))
147 return -1;
149 path_lyr_file(path, 1024, current.artist, current.title);
150 if (unlink(path) != 0)
151 return -2;
153 return 0;
154 }
156 static void
157 screen_lyrics_set(const GString *str)
158 {
159 screen_text_set(&text, str);
161 /* paint new data */
163 lyrics_repaint_if_active();
164 }
166 static void
167 screen_lyrics_callback(const GString *result, const bool success,
168 G_GNUC_UNUSED const char *plugin_name,
169 G_GNUC_UNUSED void *data)
170 {
171 assert(current.loader != NULL);
173 /* Display result, which may be lyrics or error messages */
174 if (result != NULL)
175 screen_lyrics_set(result);
177 if (success == true) {
178 if (options.lyrics_autosave &&
179 !exists_lyr_file(current.artist, current.title))
180 store_lyr_hd();
181 } else {
182 /* translators: no lyrics were found for the song */
183 screen_status_message (_("No lyrics"));
184 }
186 plugin_stop(current.loader);
187 current.loader = NULL;
188 }
190 static void
191 screen_lyrics_load(const struct mpd_song *song)
192 {
193 const char *artist, *title;
195 assert(song != NULL);
197 screen_lyrics_abort();
198 screen_text_clear(&text);
200 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
201 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
203 current.song = mpd_song_dup(song);
204 current.artist = g_strdup(artist);
205 current.title = g_strdup(title);
207 current.loader = lyrics_load(current.artist, current.title,
208 screen_lyrics_callback, NULL);
209 }
211 static void
212 lyrics_screen_init(WINDOW *w, int cols, int rows)
213 {
214 screen_text_init(&text, w, cols, rows);
215 }
217 static void
218 lyrics_resize(int cols, int rows)
219 {
220 screen_text_resize(&text, cols, rows);
221 }
223 static void
224 lyrics_exit(void)
225 {
226 screen_lyrics_abort();
228 screen_text_deinit(&text);
229 }
231 static void
232 lyrics_open(struct mpdclient *c)
233 {
234 const struct mpd_song *next_song_c =
235 next_song != NULL ? next_song : c->song;
237 if (next_song_c != NULL &&
238 (current.song == NULL ||
239 strcmp(mpd_song_get_uri(next_song_c),
240 mpd_song_get_uri(current.song)) != 0))
241 screen_lyrics_load(next_song_c);
243 if (next_song != NULL)
244 mpd_song_free(next_song);
245 next_song = NULL;
246 }
248 static void
249 lyrics_update(struct mpdclient *c)
250 {
251 if (!follow)
252 return;
254 if (c->song != NULL &&
255 (current.song == NULL ||
256 strcmp(mpd_song_get_uri(c->song),
257 mpd_song_get_uri(current.song)) != 0))
258 screen_lyrics_load(c->song);
259 }
261 static const char *
262 lyrics_title(char *str, size_t size)
263 {
264 if (current.loader != NULL) {
265 snprintf(str, size, "%s (%s)",
266 _("Lyrics"),
267 /* translators: this message is displayed
268 while data is retrieved */
269 _("loading..."));
270 return str;
271 } else if (current.artist != NULL && current.title != NULL &&
272 !screen_text_is_empty(&text)) {
273 snprintf(str, size, "%s: %s - %s",
274 _("Lyrics"),
275 current.artist, current.title);
276 return str;
277 } else
278 return _("Lyrics");
279 }
281 static void
282 lyrics_paint(void)
283 {
284 screen_text_paint(&text);
285 }
287 static bool
288 lyrics_cmd(struct mpdclient *c, command_t cmd)
289 {
290 if (screen_text_cmd(&text, c, cmd))
291 return true;
293 switch(cmd) {
294 case CMD_INTERRUPT:
295 if (current.loader != NULL) {
296 screen_lyrics_abort();
297 screen_text_clear(&text);
298 }
299 return true;
300 case CMD_SAVE_PLAYLIST:
301 if (current.loader == NULL && current.artist != NULL &&
302 current.title != NULL && store_lyr_hd() == 0)
303 /* lyrics for the song were saved on hard disk */
304 screen_status_message (_("Lyrics saved"));
305 return true;
306 case CMD_DELETE:
307 if (current.loader == NULL && current.artist != NULL &&
308 current.title != NULL) {
309 switch (delete_lyr_hd()) {
310 case 0:
311 screen_status_message (_("Lyrics deleted"));
312 break;
313 case -1:
314 screen_status_message (_("No saved lyrics"));
315 break;
316 }
317 }
318 return true;
319 case CMD_LYRICS_UPDATE:
320 if (c->song != NULL) {
321 screen_lyrics_load(c->song);
322 screen_text_repaint(&text);
323 }
324 return true;
325 case CMD_SELECT:
326 if (current.loader == NULL && current.artist != NULL &&
327 current.title != NULL) {
328 current.loader = lyrics_load(current.artist, current.title,
329 screen_lyrics_callback, NULL);
330 screen_text_repaint(&text);
331 }
332 return true;
334 #ifdef ENABLE_SONG_SCREEN
335 case CMD_SCREEN_SONG:
336 if (current.song != NULL) {
337 screen_song_switch(c, current.song);
338 return true;
339 }
341 break;
342 #endif
343 case CMD_SCREEN_SWAP:
344 screen_swap(c, current.song);
345 return true;
347 case CMD_LOCATE:
348 if (current.song != NULL) {
349 screen_file_goto_song(c, current.song);
350 return true;
351 }
353 return false;
355 default:
356 break;
357 }
359 return false;
360 }
362 const struct screen_functions screen_lyrics = {
363 .init = lyrics_screen_init,
364 .exit = lyrics_exit,
365 .open = lyrics_open,
366 .update = lyrics_update,
367 .close = NULL,
368 .resize = lyrics_resize,
369 .paint = lyrics_paint,
370 .cmd = lyrics_cmd,
371 .get_title = lyrics_title,
372 };
374 void
375 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
376 {
377 assert(song != NULL);
379 follow = f;
380 next_song = mpd_song_dup(song);
381 screen_switch(&screen_lyrics, c);
382 }