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_freeSong(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_songDup(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(mpdclient_t *c)
204 {
205 if (next_song == NULL)
206 next_song = c->song;
208 if (next_song != NULL &&
209 (current.song == NULL ||
210 strcmp(next_song->file, current.song->file) != 0))
211 screen_lyrics_load(next_song);
213 if (next_song != c->song)
214 mpd_freeSong(next_song);
215 next_song = NULL;
216 }
218 static void
219 lyrics_update(mpdclient_t *c)
220 {
221 if (!follow)
222 return;
224 next_song = c->song;
226 if (next_song != NULL &&
227 (current.song == NULL ||
228 strcmp(next_song->file, current.song->file) != 0))
229 screen_lyrics_load(next_song);
231 next_song = NULL;
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(mpdclient_t *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_songDup(song);
333 screen_switch(&screen_lyrics, c);
334 }