1 /*
2 * $Id: screen_lyrics.c 3355 2006-09-1 17:44:04Z tradiaz $
3 *
4 * (c) 2006 by Kalle Wallin <kaw@linux.se>
5 * Tue Aug 1 23:17:38 2006
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
22 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <ncurses.h>
27 #include <expat.h>
28 #include <unistd.h>
29 #include <glib/gstdio.h>
30 #include <stdio.h>
32 #include "config.h"
33 #ifndef DISABLE_LYRICS_SCREEN
34 #include <sys/stat.h>
35 #include "ncmpc.h"
36 #include "options.h"
37 #include "mpdclient.h"
38 #include "command.h"
39 #include "screen.h"
40 #include "screen_utils.h"
41 #include "easy_download.h"
42 #include "strfsong.h"
43 #include "src_lyrics.h"
45 int src_selection;
47 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
49 FILE *create_lyr_file(char *artist, char *title)
50 {
51 char path[1024];
53 snprintf(path, 1024, "%s/.lyrics",
54 getenv("HOME"));
55 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
57 snprintf(path, 1024, "%s/.lyrics/%s",
58 getenv("HOME"), artist);
59 if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
61 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
62 getenv("HOME"), artist, title);
64 return fopen(path, "w");
65 }
68 int store_lyr_hd()
69 {
70 char artist[512];
71 char title[512];
72 static char path[1024];
73 FILE *lyr_file;
75 get_text_line(&lyr_text, 0, artist, 512);
76 get_text_line(&lyr_text, 1, title, 512);
77 artist[strlen(artist)-1] = '\0';
78 title[strlen(title)-1] = '\0';
80 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
81 getenv("HOME"), artist, title);
82 lyr_file = create_lyr_file(artist, title);
83 if(lyr_file == NULL) return -1;
84 int i;
85 char line_buf[1024];
87 for(i = 3; i <= lyr_text.text->len; i++)
88 {
89 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
90 fputs(line_buf, lyr_file);
91 }
92 fclose(lyr_file);
93 return 0;
94 }
97 void check_repaint()
98 {
99 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
100 }
103 gpointer get_lyr(void *c)
104 {
105 mpd_Status *status = ((retrieval_spec*)c)->client->status;
106 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
107 //mpdclient_update((mpdclient_t*)c);
109 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
110 {
111 formed_text_init(&lyr_text);
112 return NULL;
113 }
116 char artist[MAX_SONGNAME_LENGTH];
117 char title[MAX_SONGNAME_LENGTH];
118 lock=2;
119 result = 0;
121 formed_text_init(&lyr_text);
123 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
124 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
126 //write header..
127 formed_text_init(&lyr_text);
128 add_text_line(&lyr_text, artist, 0);
129 add_text_line(&lyr_text, title, 0);
130 add_text_line(&lyr_text, "", 0);
131 add_text_line(&lyr_text, "", 0);
133 if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/
134 {
135 if(get_lyr_by_src (src_selection, artist, title) != 0)
136 {
137 lock=0;
138 return NULL;
139 }
140 }
141 /*else{
142 if(get_lyr_hd(artist, title) != 0)
143 {
144 if(get_lyr_hd(artist, title) != 0) return NULL;
145 }
146 else result |= 1;
147 }*/
148 //return NULL;
149 lw->start = 0;
150 check_repaint();
151 lock = 1;
152 return &lyr_text;
153 }
155 static char *
156 list_callback(int index, int *highlight, void *data)
157 {
158 static char buf[512];
160 //i think i'ts fine to write it into the 1st line...
161 if((index == lyr_text.lines->len && lyr_text.lines->len > 4)||
162 ((lyr_text.lines->len == 0
163 ||lyr_text.lines->len == 4) && index == 0))
164 {
165 *highlight=3;
166 src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection);
167 if (selected != NULL) return selected->description;
168 return "";
169 }
171 if(index < 2 && lyr_text.lines->len > 4) *highlight=3;
172 else if(index >= lyr_text.lines->len ||
173 ( index < 4 && index != 0 && lyr_text.lines->len < 5))
174 {
175 return "";
176 }
178 get_text_line(&lyr_text, index, buf, 512);
179 return buf;
180 }
183 static void
184 lyrics_init(WINDOW *w, int cols, int rows)
185 {
186 lw = list_window_init(w, cols, rows);
187 lw->flags = LW_HIDE_CURSOR;
188 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
189 formed_text_init(&lyr_text);
190 if (!g_thread_supported()) g_thread_init(NULL);
192 }
194 static void
195 lyrics_resize(int cols, int rows)
196 {
197 lw->cols = cols;
198 lw->rows = rows;
199 }
201 static void
202 lyrics_exit(void)
203 {
204 list_window_free(lw);
205 }
208 static char *
209 lyrics_title(char *str, size_t size)
210 {
211 static GString *msg;
212 if (msg == NULL)
213 msg = g_string_new ("");
214 else g_string_erase (msg, 0, -1);
216 g_string_append (msg, "Lyrics [");
218 if (src_selection > src_lyr_stack->len-1)
219 g_string_append (msg, "No plugin available");
220 else
221 {
222 src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection);
223 if (selected != NULL)
224 g_string_append (msg, selected->name);
225 else g_string_append (msg, "NONE");
226 }
227 if(lyr_text.lines->len == 4)
228 {
229 if(lock == 1)
230 {
231 if(!(result & 1))
232 {
233 g_string_append (msg, " - ");
234 if(!(result & 2)) g_string_append (msg, _("No access"));
235 else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found"));
236 }
237 }
238 if(lock == 2)
239 {
240 g_string_append (msg, " - ");
241 g_string_append (msg, _("retrieving"));
242 }
243 }
245 g_string_append_c (msg, ']');
247 return msg->str;
248 }
250 static void
251 lyrics_paint(screen_t *screen, mpdclient_t *c)
252 {
253 lw->clear = 1;
254 list_window_paint(lw, list_callback, NULL);
255 wrefresh(lw->w);
256 }
259 static void
260 lyrics_update(screen_t *screen, mpdclient_t *c)
261 {
262 if( lw->repaint )
263 {
264 list_window_paint(lw, list_callback, NULL);
265 wrefresh(lw->w);
266 lw->repaint = 0;
267 }
268 }
271 static int
272 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
273 {
274 lw->repaint=1;
275 static retrieval_spec spec;
276 switch(cmd)
277 {
278 case CMD_LIST_NEXT:
279 if( lw->start+lw->rows < lyr_text.lines->len+1 )
280 lw->start++;
281 return 1;
282 case CMD_LIST_PREVIOUS:
283 if( lw->start >0 )
284 lw->start--;
285 return 1;
286 case CMD_LIST_FIRST:
287 lw->start = 0;
288 return 1;
289 case CMD_LIST_LAST:
290 lw->start = lyrics_text_rows-lw->rows;
291 if( lw->start<0 )
292 lw->start = 0;
293 return 1;
294 case CMD_LIST_NEXT_PAGE:
295 lw->start = lw->start + lw->rows-1;
296 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
297 lw->start = lyr_text.lines->len-lw->rows+1;
298 if( lw->start<0 )
299 lw->start = 0;
300 return 1;
301 case CMD_LIST_PREVIOUS_PAGE:
302 lw->start = lw->start - lw->rows;
303 if( lw->start<0 )
304 lw->start = 0;
305 return 1;
306 case CMD_SELECT:
307 spec.client = c;
308 spec.way = 0;
309 g_thread_create(get_lyr, &spec, FALSE, NULL);
310 return 1;
311 case CMD_INTERRUPT:
312 if(lock > 1) lock = 4;
313 return 1;
314 case CMD_ADD:
315 if(lock > 0 && lock != 4)
316 {
317 if(store_lyr_hd() == 0) screen_status_message (_("Lyrics saved!"));
318 }
319 return 1;
320 case CMD_LYRICS_UPDATE:
321 spec.client = c;
322 spec.way = 1;
323 g_thread_create(get_lyr, &spec, FALSE, NULL);
324 return 1;
325 case CMD_SEARCH_MODE:
326 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
327 if (src_selection == src_lyr_stack->len-1) src_selection = -1;
328 src_selection++;
329 return 1;
330 default:
331 break;
332 }
334 lw->selected = lw->start+lw->rows;
335 if( screen_find(screen,
336 lw, lyrics_text_rows,
337 cmd, list_callback, NULL) )
338 {
339 /* center the row */
340 lw->start = lw->selected-(lw->rows/2);
341 if( lw->start+lw->rows > lyrics_text_rows )
342 lw->start = lyrics_text_rows-lw->rows;
343 if( lw->start<0 )
344 lw->start=0;
345 return 1;
346 }
348 return 0;
349 }
351 static list_window_t *
352 lyrics_lw(void)
353 {
354 return lw;
355 }
357 screen_functions_t *
358 get_screen_lyrics(void)
359 {
360 static screen_functions_t functions;
362 memset(&functions, 0, sizeof(screen_functions_t));
363 functions.init = lyrics_init;
364 functions.exit = lyrics_exit;
365 functions.open = NULL;
366 functions.close = NULL;
367 functions.resize = lyrics_resize;
368 functions.paint = lyrics_paint;
369 functions.update = lyrics_update;
370 functions.cmd = lyrics_cmd;
371 functions.get_lw = lyrics_lw;
372 functions.get_title = lyrics_title;
374 return &functions;
375 }
376 #endif /* ENABLE_LYRICS_SCREEN */