1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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 bool
91 exists_lyr_file(const char *artist, const char *title)
92 {
93 char path[1024];
94 struct stat result;
96 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
97 getenv("HOME"), artist, title);
99 return (stat(path, &result) == 0);
100 }
102 static FILE *
103 create_lyr_file(const char *artist, const char *title)
104 {
105 char path[1024];
107 snprintf(path, 1024, "%s/.lyrics",
108 getenv("HOME"));
109 mkdir(path, S_IRWXU);
111 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
112 getenv("HOME"), artist, title);
114 return fopen(path, "w");
115 }
117 static int
118 store_lyr_hd(void)
119 {
120 FILE *lyr_file;
121 unsigned i;
123 lyr_file = create_lyr_file(current.artist, current.title);
124 if (lyr_file == NULL)
125 return -1;
127 for (i = 0; i < text.lines->len; ++i)
128 fprintf(lyr_file, "%s\n",
129 (const char*)g_ptr_array_index(text.lines, i));
131 fclose(lyr_file);
132 return 0;
133 }
135 static void
136 screen_lyrics_set(const GString *str)
137 {
138 screen_text_set(&text, str);
140 /* paint new data */
142 lyrics_repaint_if_active();
143 }
145 static void
146 screen_lyrics_callback(const GString *result, const bool success,
147 G_GNUC_UNUSED void *data)
148 {
149 assert(current.loader != NULL);
151 /* Display result, which may be lyrics or error messages */
152 if (result != NULL)
153 screen_lyrics_set(result);
155 if (success == true) {
156 if (options.lyrics_autosave &&
157 !exists_lyr_file(current.artist, current.title))
158 store_lyr_hd();
159 } else {
160 /* translators: no lyrics were found for the song */
161 screen_status_message (_("No lyrics"));
162 }
164 plugin_stop(current.loader);
165 current.loader = NULL;
166 }
168 static void
169 screen_lyrics_load(const struct mpd_song *song)
170 {
171 const char *artist, *title;
173 assert(song != NULL);
175 screen_lyrics_abort();
176 screen_text_clear(&text);
178 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
179 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
181 current.song = mpd_song_dup(song);
182 current.artist = g_strdup(artist);
183 current.title = g_strdup(title);
185 current.loader = lyrics_load(current.artist, current.title,
186 screen_lyrics_callback, NULL);
187 }
189 static void
190 lyrics_screen_init(WINDOW *w, int cols, int rows)
191 {
192 screen_text_init(&text, w, cols, rows);
193 }
195 static void
196 lyrics_resize(int cols, int rows)
197 {
198 screen_text_resize(&text, cols, rows);
199 }
201 static void
202 lyrics_exit(void)
203 {
204 screen_lyrics_abort();
206 screen_text_deinit(&text);
207 }
209 static void
210 lyrics_open(struct mpdclient *c)
211 {
212 const struct mpd_song *next_song_c =
213 next_song != NULL ? next_song : c->song;
215 if (next_song_c != NULL &&
216 (current.song == NULL ||
217 strcmp(mpd_song_get_uri(next_song_c),
218 mpd_song_get_uri(current.song)) != 0))
219 screen_lyrics_load(next_song_c);
221 if (next_song != NULL)
222 mpd_song_free(next_song);
223 next_song = NULL;
224 }
226 static void
227 lyrics_update(struct mpdclient *c)
228 {
229 if (!follow)
230 return;
232 if (c->song != NULL &&
233 (current.song == NULL ||
234 strcmp(mpd_song_get_uri(c->song),
235 mpd_song_get_uri(current.song)) != 0))
236 screen_lyrics_load(c->song);
237 }
239 static const char *
240 lyrics_title(char *str, size_t size)
241 {
242 if (current.loader != NULL) {
243 snprintf(str, size, "%s (%s)",
244 _("Lyrics"),
245 /* translators: this message is displayed
246 while data is retrieved */
247 _("loading..."));
248 return str;
249 } else if (current.artist != NULL && current.title != NULL &&
250 !screen_text_is_empty(&text)) {
251 snprintf(str, size, "%s: %s - %s",
252 _("Lyrics"),
253 current.artist, current.title);
254 return str;
255 } else
256 return _("Lyrics");
257 }
259 static void
260 lyrics_paint(void)
261 {
262 screen_text_paint(&text);
263 }
265 static bool
266 lyrics_cmd(struct mpdclient *c, command_t cmd)
267 {
268 if (screen_text_cmd(&text, c, cmd))
269 return true;
271 switch(cmd) {
272 case CMD_INTERRUPT:
273 if (current.loader != NULL) {
274 screen_lyrics_abort();
275 screen_text_clear(&text);
276 }
277 return true;
278 case CMD_SAVE_PLAYLIST:
279 if (current.loader == NULL && current.artist != NULL &&
280 current.title != NULL && store_lyr_hd() == 0)
281 /* lyrics for the song were saved on hard disk */
282 screen_status_message (_("Lyrics saved"));
283 return true;
284 case CMD_LYRICS_UPDATE:
285 if (c->song != NULL) {
286 screen_lyrics_load(c->song);
287 screen_text_repaint(&text);
288 }
289 return true;
291 #ifdef ENABLE_SONG_SCREEN
292 case CMD_SCREEN_SONG:
293 if (current.song != NULL) {
294 screen_song_switch(c, current.song);
295 return true;
296 }
298 break;
299 #endif
300 case CMD_SCREEN_SWAP:
301 screen_swap(c, current.song);
302 return true;
304 case CMD_LOCATE:
305 if (current.song != NULL) {
306 screen_file_goto_song(c, current.song);
307 return true;
308 }
310 return false;
312 default:
313 break;
314 }
316 return false;
317 }
319 const struct screen_functions screen_lyrics = {
320 .init = lyrics_screen_init,
321 .exit = lyrics_exit,
322 .open = lyrics_open,
323 .update = lyrics_update,
324 .close = NULL,
325 .resize = lyrics_resize,
326 .paint = lyrics_paint,
327 .cmd = lyrics_cmd,
328 .get_title = lyrics_title,
329 };
331 void
332 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
333 {
334 assert(song != NULL);
336 follow = f;
337 next_song = mpd_song_dup(song);
338 screen_switch(&screen_lyrics, c);
339 }