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 const struct mpd_song *next_song;
40 static struct {
41 struct mpd_song *song;
43 char *artist, *title;
45 struct plugin_cycle *loader;
46 } current;
48 static void
49 screen_lyrics_abort(void)
50 {
51 if (current.loader != NULL) {
52 plugin_stop(current.loader);
53 current.loader = NULL;
54 }
56 if (current.artist != NULL) {
57 g_free(current.artist);
58 current.artist = NULL;
59 }
61 if (current.title != NULL) {
62 g_free(current.title);
63 current.artist = NULL;
64 }
66 if (current.song != NULL) {
67 mpd_freeSong(current.song);
68 current.song = NULL;
69 }
70 }
72 /**
73 * Repaint and update the screen, if it is currently active.
74 */
75 static void
76 lyrics_repaint_if_active(void)
77 {
78 if (screen_is_visible(&screen_lyrics)) {
79 screen_text_repaint(&text);
81 /* XXX repaint the screen title */
82 }
83 }
85 static void
86 screen_lyrics_set(const GString *str)
87 {
88 screen_text_set(&text, str);
90 /* paint new data */
92 lyrics_repaint_if_active();
93 }
95 static void
96 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
97 {
98 assert(current.loader != NULL);
100 if (result != NULL)
101 screen_lyrics_set(result);
102 else
103 /* translators: no lyrics were found for the song */
104 screen_status_message (_("No lyrics"));
106 plugin_stop(current.loader);
107 current.loader = NULL;
108 }
110 static void
111 screen_lyrics_load(const struct mpd_song *song)
112 {
113 char buffer[MAX_SONGNAME_LENGTH];
115 assert(song != NULL);
117 screen_lyrics_abort();
118 screen_text_clear(&text);
120 current.song = mpd_songDup(song);
122 strfsong(buffer, sizeof(buffer), "%artist%", song);
123 current.artist = g_strdup(buffer);
125 strfsong(buffer, sizeof(buffer), "%title%", song);
126 current.title = g_strdup(buffer);
128 current.loader = lyrics_load(current.artist, current.title,
129 screen_lyrics_callback, NULL);
130 }
132 static FILE *create_lyr_file(const char *artist, const char *title)
133 {
134 char path[1024];
136 snprintf(path, 1024, "%s/.lyrics",
137 getenv("HOME"));
138 mkdir(path, S_IRWXU);
140 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
141 getenv("HOME"), artist, title);
143 return fopen(path, "w");
144 }
146 static int store_lyr_hd(void)
147 {
148 FILE *lyr_file;
149 unsigned i;
151 lyr_file = create_lyr_file(current.artist, current.title);
152 if (lyr_file == NULL)
153 return -1;
155 for (i = 0; i < text.lines->len; ++i)
156 fprintf(lyr_file, "%s\n",
157 (const char*)g_ptr_array_index(text.lines, i));
159 fclose(lyr_file);
160 return 0;
161 }
163 static void
164 lyrics_screen_init(WINDOW *w, int cols, int rows)
165 {
166 screen_text_init(&text, w, cols, rows);
167 }
169 static void
170 lyrics_resize(int cols, int rows)
171 {
172 screen_text_resize(&text, cols, rows);
173 }
175 static void
176 lyrics_exit(void)
177 {
178 screen_lyrics_abort();
180 screen_text_deinit(&text);
181 }
183 static void
184 lyrics_open(mpdclient_t *c)
185 {
186 if (next_song == NULL)
187 next_song = c->song;
189 if (next_song != NULL &&
190 (current.song == NULL ||
191 strcmp(next_song->file, current.song->file) != 0))
192 screen_lyrics_load(next_song);
194 next_song = NULL;
195 }
198 static const char *
199 lyrics_title(char *str, size_t size)
200 {
201 if (current.loader != NULL) {
202 snprintf(str, size, "%s (%s)",
203 _("Lyrics"),
204 /* translators: this message is displayed
205 while data is retrieved */
206 _("loading..."));
207 return str;
208 } else if (current.artist != NULL && current.title != NULL &&
209 !screen_text_is_empty(&text)) {
210 snprintf(str, size, "%s: %s - %s",
211 _("Lyrics"),
212 current.artist, current.title);
213 return str;
214 } else
215 return _("Lyrics");
216 }
218 static void
219 lyrics_paint(void)
220 {
221 screen_text_paint(&text);
222 }
224 static bool
225 lyrics_cmd(mpdclient_t *c, command_t cmd)
226 {
227 if (screen_text_cmd(&text, c, cmd))
228 return true;
230 switch(cmd) {
231 case CMD_INTERRUPT:
232 if (current.loader != NULL) {
233 screen_lyrics_abort();
234 screen_text_clear(&text);
235 }
236 return true;
237 case CMD_SAVE_PLAYLIST:
238 if (current.loader == NULL && current.artist != NULL &&
239 current.title != NULL && store_lyr_hd() == 0)
240 /* lyrics for the song were saved on hard disk */
241 screen_status_message (_("Lyrics saved"));
242 return true;
243 case CMD_LYRICS_UPDATE:
244 if (c->song != NULL) {
245 screen_lyrics_load(c->song);
246 screen_text_repaint(&text);
247 }
248 return true;
250 #ifdef ENABLE_SONG_SCREEN
251 case CMD_SCREEN_SONG:
252 if (current.song != NULL) {
253 screen_song_switch(c, current.song);
254 return true;
255 }
257 break;
258 #endif
259 case CMD_SCREEN_SWAP:
260 screen_swap(c, current.song);
261 return true;
263 case CMD_LOCATE:
264 if (current.song != NULL) {
265 screen_file_goto_song(c, current.song);
266 return true;
267 }
269 return false;
271 default:
272 break;
273 }
275 return false;
276 }
278 const struct screen_functions screen_lyrics = {
279 .init = lyrics_screen_init,
280 .exit = lyrics_exit,
281 .open = lyrics_open,
282 .close = NULL,
283 .resize = lyrics_resize,
284 .paint = lyrics_paint,
285 .cmd = lyrics_cmd,
286 .get_title = lyrics_title,
287 };
289 void
290 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
291 {
292 assert(song != NULL);
294 next_song = song;
295 screen_switch(&screen_lyrics, c);
296 }