33a0c3a2659e4ec0176ceb043029f4b924f46a59
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_file.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 <sys/stat.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <unistd.h>
35 #include <stdio.h>
37 static struct screen_text text;
39 static struct mpd_song *next_song;
40 static bool follow = false;
42 static struct {
43 struct mpd_song *song;
45 char *artist, *title;
47 struct plugin_cycle *loader;
48 } current;
50 static void
51 screen_lyrics_abort(void)
52 {
53 if (current.loader != NULL) {
54 plugin_stop(current.loader);
55 current.loader = NULL;
56 }
58 if (current.artist != NULL) {
59 g_free(current.artist);
60 current.artist = NULL;
61 }
63 if (current.title != NULL) {
64 g_free(current.title);
65 current.artist = NULL;
66 }
68 if (current.song != NULL) {
69 mpd_song_free(current.song);
70 current.song = NULL;
71 }
72 }
74 /**
75 * Repaint and update the screen, if it is currently active.
76 */
77 static void
78 lyrics_repaint_if_active(void)
79 {
80 if (screen_is_visible(&screen_lyrics)) {
81 screen_text_repaint(&text);
83 /* XXX repaint the screen title */
84 }
85 }
87 static bool
88 exists_lyr_file(const char *artist, const char *title)
89 {
90 char path[1024];
91 struct stat result;
93 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
94 getenv("HOME"), artist, title);
96 return (stat(path, &result) == 0);
97 }
99 static FILE *
100 create_lyr_file(const char *artist, const char *title)
101 {
102 char path[1024];
104 snprintf(path, 1024, "%s/.lyrics",
105 getenv("HOME"));
106 mkdir(path, S_IRWXU);
108 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
109 getenv("HOME"), artist, title);
111 return fopen(path, "w");
112 }
114 static int
115 store_lyr_hd(void)
116 {
117 FILE *lyr_file;
118 unsigned i;
120 lyr_file = create_lyr_file(current.artist, current.title);
121 if (lyr_file == NULL)
122 return -1;
124 for (i = 0; i < text.lines->len; ++i)
125 fprintf(lyr_file, "%s\n",
126 (const char*)g_ptr_array_index(text.lines, i));
128 fclose(lyr_file);
129 return 0;
130 }
132 static void
133 screen_lyrics_set(const GString *str)
134 {
135 screen_text_set(&text, str);
137 /* paint new data */
139 lyrics_repaint_if_active();
141 if (options.lyrics_autosave &&
142 !exists_lyr_file(current.artist, current.title))
143 store_lyr_hd();
144 }
146 static void
147 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
148 {
149 assert(current.loader != NULL);
151 if (result != NULL)
152 screen_lyrics_set(result);
153 else
154 /* translators: no lyrics were found for the song */
155 screen_status_message (_("No lyrics"));
157 plugin_stop(current.loader);
158 current.loader = NULL;
159 }
161 static void
162 screen_lyrics_load(const struct mpd_song *song)
163 {
164 char buffer[MAX_SONGNAME_LENGTH];
166 assert(song != NULL);
168 screen_lyrics_abort();
169 screen_text_clear(&text);
171 current.song = mpd_song_dup(song);
173 strfsong(buffer, sizeof(buffer), "%artist%", song);
174 current.artist = g_strdup(buffer);
176 strfsong(buffer, sizeof(buffer), "%title%", song);
177 current.title = g_strdup(buffer);
179 current.loader = lyrics_load(current.artist, current.title,
180 screen_lyrics_callback, NULL);
181 }
183 static void
184 lyrics_screen_init(WINDOW *w, int cols, int rows)
185 {
186 screen_text_init(&text, w, cols, rows);
187 }
189 static void
190 lyrics_resize(int cols, int rows)
191 {
192 screen_text_resize(&text, cols, rows);
193 }
195 static void
196 lyrics_exit(void)
197 {
198 screen_lyrics_abort();
200 screen_text_deinit(&text);
201 }
203 static void
204 lyrics_open(struct mpdclient *c)
205 {
206 const struct mpd_song *next_song_c =
207 next_song != NULL ? next_song : c->song;
209 if (next_song_c != NULL &&
210 (current.song == NULL ||
211 strcmp(mpd_song_get_uri(next_song_c),
212 mpd_song_get_uri(current.song)) != 0))
213 screen_lyrics_load(next_song_c);
215 if (next_song != NULL)
216 mpd_song_free(next_song);
217 next_song = NULL;
218 }
220 static void
221 lyrics_update(struct mpdclient *c)
222 {
223 if (!follow)
224 return;
226 if (c->song != NULL &&
227 (current.song == NULL ||
228 strcmp(mpd_song_get_uri(c->song),
229 mpd_song_get_uri(current.song)) != 0))
230 screen_lyrics_load(c->song);
231 }
233 static const char *
234 lyrics_title(char *str, size_t size)
235 {
236 if (current.loader != NULL) {
237 snprintf(str, size, "%s (%s)",
238 _("Lyrics"),
239 /* translators: this message is displayed
240 while data is retrieved */
241 _("loading..."));
242 return str;
243 } else if (current.artist != NULL && current.title != NULL &&
244 !screen_text_is_empty(&text)) {
245 snprintf(str, size, "%s: %s - %s",
246 _("Lyrics"),
247 current.artist, current.title);
248 return str;
249 } else
250 return _("Lyrics");
251 }
253 static void
254 lyrics_paint(void)
255 {
256 screen_text_paint(&text);
257 }
259 static bool
260 lyrics_cmd(struct mpdclient *c, command_t cmd)
261 {
262 if (screen_text_cmd(&text, c, cmd))
263 return true;
265 switch(cmd) {
266 case CMD_INTERRUPT:
267 if (current.loader != NULL) {
268 screen_lyrics_abort();
269 screen_text_clear(&text);
270 }
271 return true;
272 case CMD_SAVE_PLAYLIST:
273 if (current.loader == NULL && current.artist != NULL &&
274 current.title != NULL && store_lyr_hd() == 0)
275 /* lyrics for the song were saved on hard disk */
276 screen_status_message (_("Lyrics saved"));
277 return true;
278 case CMD_LYRICS_UPDATE:
279 if (c->song != NULL) {
280 screen_lyrics_load(c->song);
281 screen_text_repaint(&text);
282 }
283 return true;
285 #ifdef ENABLE_SONG_SCREEN
286 case CMD_SCREEN_SONG:
287 if (current.song != NULL) {
288 screen_song_switch(c, current.song);
289 return true;
290 }
292 break;
293 #endif
294 case CMD_SCREEN_SWAP:
295 screen_swap(c, current.song);
296 return true;
298 case CMD_LOCATE:
299 if (current.song != NULL) {
300 screen_file_goto_song(c, current.song);
301 return true;
302 }
304 return false;
306 default:
307 break;
308 }
310 return false;
311 }
313 const struct screen_functions screen_lyrics = {
314 .init = lyrics_screen_init,
315 .exit = lyrics_exit,
316 .open = lyrics_open,
317 .update = lyrics_update,
318 .close = NULL,
319 .resize = lyrics_resize,
320 .paint = lyrics_paint,
321 .cmd = lyrics_cmd,
322 .get_title = lyrics_title,
323 };
325 void
326 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
327 {
328 assert(song != NULL);
330 follow = f;
331 next_song = mpd_song_dup(song);
332 screen_switch(&screen_lyrics, c);
333 }