4d1122b4b290e64ee9a73b6e22ab4a543a6adb28
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 void *data)
169 {
170 assert(current.loader != NULL);
172 /* Display result, which may be lyrics or error messages */
173 if (result != NULL)
174 screen_lyrics_set(result);
176 if (success == true) {
177 if (options.lyrics_autosave &&
178 !exists_lyr_file(current.artist, current.title))
179 store_lyr_hd();
180 } else {
181 /* translators: no lyrics were found for the song */
182 screen_status_message (_("No lyrics"));
183 }
185 plugin_stop(current.loader);
186 current.loader = NULL;
187 }
189 static void
190 screen_lyrics_load(const struct mpd_song *song)
191 {
192 const char *artist, *title;
194 assert(song != NULL);
196 screen_lyrics_abort();
197 screen_text_clear(&text);
199 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
200 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
202 current.song = mpd_song_dup(song);
203 current.artist = g_strdup(artist);
204 current.title = g_strdup(title);
206 current.loader = lyrics_load(current.artist, current.title,
207 screen_lyrics_callback, NULL);
208 }
210 static void
211 lyrics_screen_init(WINDOW *w, int cols, int rows)
212 {
213 screen_text_init(&text, w, cols, rows);
214 }
216 static void
217 lyrics_resize(int cols, int rows)
218 {
219 screen_text_resize(&text, cols, rows);
220 }
222 static void
223 lyrics_exit(void)
224 {
225 screen_lyrics_abort();
227 screen_text_deinit(&text);
228 }
230 static void
231 lyrics_open(struct mpdclient *c)
232 {
233 const struct mpd_song *next_song_c =
234 next_song != NULL ? next_song : c->song;
236 if (next_song_c != NULL &&
237 (current.song == NULL ||
238 strcmp(mpd_song_get_uri(next_song_c),
239 mpd_song_get_uri(current.song)) != 0))
240 screen_lyrics_load(next_song_c);
242 if (next_song != NULL)
243 mpd_song_free(next_song);
244 next_song = NULL;
245 }
247 static void
248 lyrics_update(struct mpdclient *c)
249 {
250 if (!follow)
251 return;
253 if (c->song != NULL &&
254 (current.song == NULL ||
255 strcmp(mpd_song_get_uri(c->song),
256 mpd_song_get_uri(current.song)) != 0))
257 screen_lyrics_load(c->song);
258 }
260 static const char *
261 lyrics_title(char *str, size_t size)
262 {
263 if (current.loader != NULL) {
264 snprintf(str, size, "%s (%s)",
265 _("Lyrics"),
266 /* translators: this message is displayed
267 while data is retrieved */
268 _("loading..."));
269 return str;
270 } else if (current.artist != NULL && current.title != NULL &&
271 !screen_text_is_empty(&text)) {
272 snprintf(str, size, "%s: %s - %s",
273 _("Lyrics"),
274 current.artist, current.title);
275 return str;
276 } else
277 return _("Lyrics");
278 }
280 static void
281 lyrics_paint(void)
282 {
283 screen_text_paint(&text);
284 }
286 static bool
287 lyrics_cmd(struct mpdclient *c, command_t cmd)
288 {
289 if (screen_text_cmd(&text, c, cmd))
290 return true;
292 switch(cmd) {
293 case CMD_INTERRUPT:
294 if (current.loader != NULL) {
295 screen_lyrics_abort();
296 screen_text_clear(&text);
297 }
298 return true;
299 case CMD_SAVE_PLAYLIST:
300 if (current.loader == NULL && current.artist != NULL &&
301 current.title != NULL && store_lyr_hd() == 0)
302 /* lyrics for the song were saved on hard disk */
303 screen_status_message (_("Lyrics saved"));
304 return true;
305 case CMD_DELETE:
306 {
307 int success;
309 if (current.loader == NULL && current.artist != NULL &&
310 current.title != NULL) {
311 success = delete_lyr_hd();
312 switch (success) {
313 case 0:
314 screen_status_message
315 (_("Lyrics deleted"));
316 break;
317 case -1:
318 screen_status_message
319 (_("No saved lyrics"));
320 break;
321 }
322 }
323 return true;
324 }
325 case CMD_LYRICS_UPDATE:
326 if (c->song != NULL) {
327 screen_lyrics_load(c->song);
328 screen_text_repaint(&text);
329 }
330 return true;
332 #ifdef ENABLE_SONG_SCREEN
333 case CMD_SCREEN_SONG:
334 if (current.song != NULL) {
335 screen_song_switch(c, current.song);
336 return true;
337 }
339 break;
340 #endif
341 case CMD_SCREEN_SWAP:
342 screen_swap(c, current.song);
343 return true;
345 case CMD_LOCATE:
346 if (current.song != NULL) {
347 screen_file_goto_song(c, current.song);
348 return true;
349 }
351 return false;
353 default:
354 break;
355 }
357 return false;
358 }
360 const struct screen_functions screen_lyrics = {
361 .init = lyrics_screen_init,
362 .exit = lyrics_exit,
363 .open = lyrics_open,
364 .update = lyrics_update,
365 .close = NULL,
366 .resize = lyrics_resize,
367 .paint = lyrics_paint,
368 .cmd = lyrics_cmd,
369 .get_title = lyrics_title,
370 };
372 void
373 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
374 {
375 assert(song != NULL);
377 follow = f;
378 next_song = mpd_song_dup(song);
379 screen_switch(&screen_lyrics, c);
380 }