1 /*
2 * (c) 2006 by Kalle Wallin <kaw@linux.se>
3 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
4 *
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.
9 *
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
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 screen_status_message (_("No lyrics"));
105 plugin_stop(current.loader);
106 current.loader = NULL;
107 }
109 static void
110 screen_lyrics_load(const struct mpd_song *song)
111 {
112 char buffer[MAX_SONGNAME_LENGTH];
114 assert(song != NULL);
116 screen_lyrics_abort();
117 screen_text_clear(&text);
119 current.song = mpd_songDup(song);
121 strfsong(buffer, sizeof(buffer), "%artist%", song);
122 current.artist = g_strdup(buffer);
124 strfsong(buffer, sizeof(buffer), "%title%", song);
125 current.title = g_strdup(buffer);
127 current.loader = lyrics_load(current.artist, current.title,
128 screen_lyrics_callback, NULL);
129 }
131 static FILE *create_lyr_file(const char *artist, const char *title)
132 {
133 char path[1024];
135 snprintf(path, 1024, "%s/.lyrics",
136 getenv("HOME"));
137 mkdir(path, S_IRWXU);
139 snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
140 getenv("HOME"), artist, title);
142 return fopen(path, "w");
143 }
145 static int store_lyr_hd(void)
146 {
147 FILE *lyr_file;
148 unsigned i;
150 lyr_file = create_lyr_file(current.artist, current.title);
151 if (lyr_file == NULL)
152 return -1;
154 for (i = 0; i < text.lines->len; ++i)
155 fprintf(lyr_file, "%s\n",
156 (const char*)g_ptr_array_index(text.lines, i));
158 fclose(lyr_file);
159 return 0;
160 }
162 static void
163 lyrics_screen_init(WINDOW *w, int cols, int rows)
164 {
165 screen_text_init(&text, w, cols, rows);
166 }
168 static void
169 lyrics_resize(int cols, int rows)
170 {
171 screen_text_resize(&text, cols, rows);
172 }
174 static void
175 lyrics_exit(void)
176 {
177 screen_lyrics_abort();
179 screen_text_deinit(&text);
180 }
182 static void
183 lyrics_open(mpdclient_t *c)
184 {
185 if (next_song == NULL)
186 next_song = c->song;
188 if (next_song != NULL &&
189 (current.song == NULL ||
190 strcmp(next_song->file, current.song->file) != 0))
191 screen_lyrics_load(next_song);
193 next_song = NULL;
194 }
197 static const char *
198 lyrics_title(char *str, size_t size)
199 {
200 if (current.loader != NULL) {
201 snprintf(str, size, "%s (%s)",
202 _("Lyrics"), _("loading..."));
203 return str;
204 } else if (current.artist != NULL && current.title != NULL &&
205 !screen_text_is_empty(&text)) {
206 snprintf(str, size, "%s: %s - %s",
207 _("Lyrics"),
208 current.artist, current.title);
209 return str;
210 } else
211 return _("Lyrics");
212 }
214 static void
215 lyrics_paint(void)
216 {
217 screen_text_paint(&text);
218 }
220 static bool
221 lyrics_cmd(mpdclient_t *c, command_t cmd)
222 {
223 if (screen_text_cmd(&text, c, cmd))
224 return true;
226 switch(cmd) {
227 case CMD_INTERRUPT:
228 if (current.loader != NULL) {
229 screen_lyrics_abort();
230 screen_text_clear(&text);
231 }
232 return true;
233 case CMD_ADD:
234 if (current.loader == NULL && current.artist != NULL &&
235 current.title != NULL && store_lyr_hd() == 0)
236 screen_status_message (_("Lyrics saved"));
237 return true;
238 case CMD_LYRICS_UPDATE:
239 if (c->song != NULL) {
240 screen_lyrics_load(c->song);
241 screen_text_repaint(&text);
242 }
243 return true;
245 #ifdef ENABLE_SONG_SCREEN
246 case CMD_VIEW:
247 if (current.song != NULL) {
248 screen_song_switch(c, current.song);
249 return true;
250 }
252 break;
253 #endif
255 case CMD_LOCATE:
256 if (current.song != NULL) {
257 screen_file_goto_song(c, current.song);
258 return true;
259 }
261 return false;
263 default:
264 break;
265 }
267 return false;
268 }
270 const struct screen_functions screen_lyrics = {
271 .init = lyrics_screen_init,
272 .exit = lyrics_exit,
273 .open = lyrics_open,
274 .close = NULL,
275 .resize = lyrics_resize,
276 .paint = lyrics_paint,
277 .cmd = lyrics_cmd,
278 .get_title = lyrics_title,
279 };
281 void
282 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
283 {
284 assert(song != NULL);
286 next_song = song;
287 screen_switch(&screen_lyrics, c);
288 }