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 <sys/stat.h>
21 #include "i18n.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "strfsong.h"
27 #include "lyrics.h"
28 #include "screen_text.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <glib.h>
33 #include <unistd.h>
34 #include <stdio.h>
36 static struct screen_text text;
38 static struct mpd_song *next_song;
39 static bool follow = false;
41 static struct {
42 struct mpd_song *song;
44 char *artist, *title;
46 struct plugin_cycle *loader;
47 } current;
49 static void
50 screen_lyrics_abort(void)
51 {
52 if (current.loader != NULL) {
53 plugin_stop(current.loader);
54 current.loader = NULL;
55 }
57 if (current.artist != NULL) {
58 g_free(current.artist);
59 current.artist = NULL;
60 }
62 if (current.title != NULL) {
63 g_free(current.title);
64 current.artist = NULL;
65 }
67 if (current.song != NULL) {
68 mpd_song_free(current.song);
69 current.song = NULL;
70 }
71 }
73 /**
74 * Repaint and update the screen, if it is currently active.
75 */
76 static void
77 lyrics_repaint_if_active(void)
78 {
79 if (screen_is_visible(&screen_lyrics)) {
80 screen_text_repaint(&text);
82 /* XXX repaint the screen title */
83 }
84 }
86 static bool
87 exists_lyr_file(const char *artist, const char *title)
88 {
89 char path[1024];
90 struct stat result;
92 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
93 getenv("HOME"), artist, title);
95 return (stat(path, &result) == 0);
96 }
98 static FILE *
99 create_lyr_file(const char *artist, const char *title)
100 {
101 char path[1024];
103 snprintf(path, 1024, "%s/.lyrics",
104 getenv("HOME"));
105 mkdir(path, S_IRWXU);
107 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
108 getenv("HOME"), artist, title);
110 return fopen(path, "w");
111 }
113 static int
114 store_lyr_hd(void)
115 {
116 FILE *lyr_file;
117 unsigned i;
119 lyr_file = create_lyr_file(current.artist, current.title);
120 if (lyr_file == NULL)
121 return -1;
123 for (i = 0; i < text.lines->len; ++i)
124 fprintf(lyr_file, "%s\n",
125 (const char*)g_ptr_array_index(text.lines, i));
127 fclose(lyr_file);
128 return 0;
129 }
131 static void
132 screen_lyrics_set(const GString *str)
133 {
134 screen_text_set(&text, str);
136 /* paint new data */
138 lyrics_repaint_if_active();
140 if (options.lyrics_autosave &&
141 !exists_lyr_file(current.artist, current.title))
142 store_lyr_hd();
143 }
145 static void
146 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
147 {
148 assert(current.loader != NULL);
150 if (result != NULL)
151 screen_lyrics_set(result);
152 else
153 /* translators: no lyrics were found for the song */
154 screen_status_message (_("No lyrics"));
156 plugin_stop(current.loader);
157 current.loader = NULL;
158 }
160 static void
161 screen_lyrics_load(const struct mpd_song *song)
162 {
163 char buffer[MAX_SONGNAME_LENGTH];
165 assert(song != NULL);
167 screen_lyrics_abort();
168 screen_text_clear(&text);
170 current.song = mpd_song_dup(song);
172 strfsong(buffer, sizeof(buffer), "%artist%", song);
173 current.artist = g_strdup(buffer);
175 strfsong(buffer, sizeof(buffer), "%title%", song);
176 current.title = g_strdup(buffer);
178 current.loader = lyrics_load(current.artist, current.title,
179 screen_lyrics_callback, NULL);
180 }
182 static void
183 lyrics_screen_init(WINDOW *w, int cols, int rows)
184 {
185 screen_text_init(&text, w, cols, rows);
186 }
188 static void
189 lyrics_resize(int cols, int rows)
190 {
191 screen_text_resize(&text, cols, rows);
192 }
194 static void
195 lyrics_exit(void)
196 {
197 screen_lyrics_abort();
199 screen_text_deinit(&text);
200 }
202 static void
203 lyrics_open(struct mpdclient *c)
204 {
205 const struct mpd_song *next_song_c =
206 next_song != NULL ? next_song : c->song;
208 if (next_song_c != NULL &&
209 (current.song == NULL ||
210 strcmp(mpd_song_get_uri(next_song_c),
211 mpd_song_get_uri(current.song)) != 0))
212 screen_lyrics_load(next_song_c);
214 if (next_song != NULL)
215 mpd_song_free(next_song);
216 next_song = NULL;
217 }
219 static void
220 lyrics_update(struct mpdclient *c)
221 {
222 if (!follow)
223 return;
225 if (c->song != NULL &&
226 (current.song == NULL ||
227 strcmp(mpd_song_get_uri(c->song),
228 mpd_song_get_uri(current.song)) != 0))
229 screen_lyrics_load(c->song);
230 }
232 static const char *
233 lyrics_title(char *str, size_t size)
234 {
235 if (current.loader != NULL) {
236 snprintf(str, size, "%s (%s)",
237 _("Lyrics"),
238 /* translators: this message is displayed
239 while data is retrieved */
240 _("loading..."));
241 return str;
242 } else if (current.artist != NULL && current.title != NULL &&
243 !screen_text_is_empty(&text)) {
244 snprintf(str, size, "%s: %s - %s",
245 _("Lyrics"),
246 current.artist, current.title);
247 return str;
248 } else
249 return _("Lyrics");
250 }
252 static void
253 lyrics_paint(void)
254 {
255 screen_text_paint(&text);
256 }
258 static bool
259 lyrics_cmd(struct mpdclient *c, command_t cmd)
260 {
261 if (screen_text_cmd(&text, c, cmd))
262 return true;
264 switch(cmd) {
265 case CMD_INTERRUPT:
266 if (current.loader != NULL) {
267 screen_lyrics_abort();
268 screen_text_clear(&text);
269 }
270 return true;
271 case CMD_SAVE_PLAYLIST:
272 if (current.loader == NULL && current.artist != NULL &&
273 current.title != NULL && store_lyr_hd() == 0)
274 /* lyrics for the song were saved on hard disk */
275 screen_status_message (_("Lyrics saved"));
276 return true;
277 case CMD_LYRICS_UPDATE:
278 if (c->song != NULL) {
279 screen_lyrics_load(c->song);
280 screen_text_repaint(&text);
281 }
282 return true;
284 #ifdef ENABLE_SONG_SCREEN
285 case CMD_SCREEN_SONG:
286 if (current.song != NULL) {
287 screen_song_switch(c, current.song);
288 return true;
289 }
291 break;
292 #endif
293 case CMD_SCREEN_SWAP:
294 screen_swap(c, current.song);
295 return true;
297 case CMD_LOCATE:
298 if (current.song != NULL) {
299 screen_file_goto_song(c, current.song);
300 return true;
301 }
303 return false;
305 default:
306 break;
307 }
309 return false;
310 }
312 const struct screen_functions screen_lyrics = {
313 .init = lyrics_screen_init,
314 .exit = lyrics_exit,
315 .open = lyrics_open,
316 .update = lyrics_update,
317 .close = NULL,
318 .resize = lyrics_resize,
319 .paint = lyrics_paint,
320 .cmd = lyrics_cmd,
321 .get_title = lyrics_title,
322 };
324 void
325 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
326 {
327 assert(song != NULL);
329 follow = f;
330 next_song = mpd_song_dup(song);
331 screen_switch(&screen_lyrics, c);
332 }