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_file.h"
23 #include "screen_song.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "mpdclient.h"
27 #include "screen.h"
28 #include "lyrics.h"
29 #include "screen_text.h"
31 #include <assert.h>
32 #include <sys/stat.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <unistd.h>
37 #include <stdio.h>
39 static struct screen_text text;
41 static struct mpd_song *next_song;
42 static bool follow = false;
44 static struct {
45 struct mpd_song *song;
47 char *artist, *title;
49 struct plugin_cycle *loader;
50 } current;
52 static void
53 screen_lyrics_abort(void)
54 {
55 if (current.loader != NULL) {
56 plugin_stop(current.loader);
57 current.loader = NULL;
58 }
60 if (current.artist != NULL) {
61 g_free(current.artist);
62 current.artist = NULL;
63 }
65 if (current.title != NULL) {
66 g_free(current.title);
67 current.artist = NULL;
68 }
70 if (current.song != NULL) {
71 mpd_song_free(current.song);
72 current.song = NULL;
73 }
74 }
76 /**
77 * Repaint and update the screen, if it is currently active.
78 */
79 static void
80 lyrics_repaint_if_active(void)
81 {
82 if (screen_is_visible(&screen_lyrics)) {
83 screen_text_repaint(&text);
85 /* XXX repaint the screen title */
86 }
87 }
89 static bool
90 exists_lyr_file(const char *artist, const char *title)
91 {
92 char path[1024];
93 struct stat result;
95 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
96 getenv("HOME"), artist, title);
98 return (stat(path, &result) == 0);
99 }
101 static FILE *
102 create_lyr_file(const char *artist, const char *title)
103 {
104 char path[1024];
106 snprintf(path, 1024, "%s/.lyrics",
107 getenv("HOME"));
108 mkdir(path, S_IRWXU);
110 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
111 getenv("HOME"), artist, title);
113 return fopen(path, "w");
114 }
116 static int
117 store_lyr_hd(void)
118 {
119 FILE *lyr_file;
120 unsigned i;
122 lyr_file = create_lyr_file(current.artist, current.title);
123 if (lyr_file == NULL)
124 return -1;
126 for (i = 0; i < text.lines->len; ++i)
127 fprintf(lyr_file, "%s\n",
128 (const char*)g_ptr_array_index(text.lines, i));
130 fclose(lyr_file);
131 return 0;
132 }
134 static void
135 screen_lyrics_set(const GString *str)
136 {
137 screen_text_set(&text, str);
139 /* paint new data */
141 lyrics_repaint_if_active();
143 if (options.lyrics_autosave &&
144 !exists_lyr_file(current.artist, current.title))
145 store_lyr_hd();
146 }
148 static void
149 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
150 {
151 assert(current.loader != NULL);
153 if (result != NULL)
154 screen_lyrics_set(result);
155 else
156 /* translators: no lyrics were found for the song */
157 screen_status_message (_("No lyrics"));
159 plugin_stop(current.loader);
160 current.loader = NULL;
161 }
163 static void
164 screen_lyrics_load(const struct mpd_song *song)
165 {
166 const char *artist, *title;
168 assert(song != NULL);
170 screen_lyrics_abort();
171 screen_text_clear(&text);
173 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
174 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
176 current.song = mpd_song_dup(song);
177 current.artist = g_strdup(artist);
178 current.title = g_strdup(title);
180 current.loader = lyrics_load(current.artist, current.title,
181 screen_lyrics_callback, NULL);
182 }
184 static void
185 lyrics_screen_init(WINDOW *w, int cols, int rows)
186 {
187 screen_text_init(&text, w, cols, rows);
188 }
190 static void
191 lyrics_resize(int cols, int rows)
192 {
193 screen_text_resize(&text, cols, rows);
194 }
196 static void
197 lyrics_exit(void)
198 {
199 screen_lyrics_abort();
201 screen_text_deinit(&text);
202 }
204 static void
205 lyrics_open(struct mpdclient *c)
206 {
207 const struct mpd_song *next_song_c =
208 next_song != NULL ? next_song : c->song;
210 if (next_song_c != NULL &&
211 (current.song == NULL ||
212 strcmp(mpd_song_get_uri(next_song_c),
213 mpd_song_get_uri(current.song)) != 0))
214 screen_lyrics_load(next_song_c);
216 if (next_song != NULL)
217 mpd_song_free(next_song);
218 next_song = NULL;
219 }
221 static void
222 lyrics_update(struct mpdclient *c)
223 {
224 if (!follow)
225 return;
227 if (c->song != NULL &&
228 (current.song == NULL ||
229 strcmp(mpd_song_get_uri(c->song),
230 mpd_song_get_uri(current.song)) != 0))
231 screen_lyrics_load(c->song);
232 }
234 static const char *
235 lyrics_title(char *str, size_t size)
236 {
237 if (current.loader != NULL) {
238 snprintf(str, size, "%s (%s)",
239 _("Lyrics"),
240 /* translators: this message is displayed
241 while data is retrieved */
242 _("loading..."));
243 return str;
244 } else if (current.artist != NULL && current.title != NULL &&
245 !screen_text_is_empty(&text)) {
246 snprintf(str, size, "%s: %s - %s",
247 _("Lyrics"),
248 current.artist, current.title);
249 return str;
250 } else
251 return _("Lyrics");
252 }
254 static void
255 lyrics_paint(void)
256 {
257 screen_text_paint(&text);
258 }
260 static bool
261 lyrics_cmd(struct mpdclient *c, command_t cmd)
262 {
263 if (screen_text_cmd(&text, c, cmd))
264 return true;
266 switch(cmd) {
267 case CMD_INTERRUPT:
268 if (current.loader != NULL) {
269 screen_lyrics_abort();
270 screen_text_clear(&text);
271 }
272 return true;
273 case CMD_SAVE_PLAYLIST:
274 if (current.loader == NULL && current.artist != NULL &&
275 current.title != NULL && store_lyr_hd() == 0)
276 /* lyrics for the song were saved on hard disk */
277 screen_status_message (_("Lyrics saved"));
278 return true;
279 case CMD_LYRICS_UPDATE:
280 if (c->song != NULL) {
281 screen_lyrics_load(c->song);
282 screen_text_repaint(&text);
283 }
284 return true;
286 #ifdef ENABLE_SONG_SCREEN
287 case CMD_SCREEN_SONG:
288 if (current.song != NULL) {
289 screen_song_switch(c, current.song);
290 return true;
291 }
293 break;
294 #endif
295 case CMD_SCREEN_SWAP:
296 screen_swap(c, current.song);
297 return true;
299 case CMD_LOCATE:
300 if (current.song != NULL) {
301 screen_file_goto_song(c, current.song);
302 return true;
303 }
305 return false;
307 default:
308 break;
309 }
311 return false;
312 }
314 const struct screen_functions screen_lyrics = {
315 .init = lyrics_screen_init,
316 .exit = lyrics_exit,
317 .open = lyrics_open,
318 .update = lyrics_update,
319 .close = NULL,
320 .resize = lyrics_resize,
321 .paint = lyrics_paint,
322 .cmd = lyrics_cmd,
323 .get_title = lyrics_title,
324 };
326 void
327 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
328 {
329 assert(song != NULL);
331 follow = f;
332 next_song = mpd_song_dup(song);
333 screen_switch(&screen_lyrics, c);
334 }