Code

translation updates, template and german. another little build fix
[ncmpc.git] / src / screen_lyrics.c
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  * lyrics enhancement written by Andreas Obergrusberger <tradiaz@yahoo.de> 
7  * using www.leoslyrics.com XML API
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <ncurses.h>
28 #include <expat.h>
29 #include <unistd.h>
31 #include "config.h"
32 #ifndef DISABLE_LYRICS_SCREEN
33 #include "ncmpc.h"
34 #include "options.h"
35 #include "mpdclient.h"
36 #include "command.h"
37 #include "screen.h"
38 #include "screen_utils.h"
39 #include "easy_download.h"
40 #include "strfsong.h"
42 #define LEOSLYRICS_SEARCH_URL "http://api.leoslyrics.com/api_search.php?auth=QuodLibet&artist=%s&songtitle=%s"
43  
44 #define LEOSLYRICS_CONTENT_URL "http://api.leoslyrics.com/api_lyrics.php?auth=QuodLibet&hid=%s"
46 #define CREDITS "Lyrics provided by www.LeosLyrics.com"
47 typedef struct _formed_text
48 {
49         GString *text;
50         GArray *lines;
51         int val;
52 } formed_text;
55 XML_Parser parser, contentp;
56 static int lyrics_text_rows = -1;
57 static list_window_t *lw = NULL;
58 guint8 result;
59 char *hid;
60 GTimer *dltime;
61 short int lock;
62 //GString *lyr_text;
63 //char *lyr_text;       
64 //GArray *textline;
65 formed_text lyr_text;
66 /* result is a bitset in which the succes when searching 4 lyrics is logged
67 countend by position - backwards
68 0: lyrics in database
69 1: proper access  to the lyrics provider
70 2: lyrics found
71 3: exact match
72 4: lyrics downloaded
73 5: lyrics saved
74 wasting 3 bits doesn't mean being a fat memory hog like kde.... does it?
75 */
76 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
78 void get_text_line(formed_text *text, int num, char *dest, int len)
79 {
80         memset(dest, '\0', len*sizeof(char));
81     if(num >= text->lines->len-1) return;       
82         int linelen;
83         if(num == 0)
84         {
85                 linelen = g_array_index(text->lines, int, num);
86                 memcpy(dest, text->text->str, linelen*sizeof(char));    
87         }
88         else if(num == 1)
89         { //dont ask me why, but this is needed....
90                 linelen = g_array_index(text->lines, int, num)
91                         - g_array_index(text->lines, int, num-1);
92                 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
93                 linelen*sizeof(char));  
94         }       
95         else
96         {
97                 linelen = g_array_index(text->lines, int, num+1)
98                         - g_array_index(text->lines, int, num);
99                 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
100                 linelen*sizeof(char));  
101         }
102         dest[linelen] = '\n';
103         dest[linelen+1] = '\0';
105         
106 void add_text_line(formed_text *dest, const char *src, int len)
108         // need this because g_array_append_val doesnt work with literals
109         // and expat sends "\n" as an extra line everytime
110         if(len == 0)
111         {
112                 dest->val = strlen(src);
113                 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
114                                                                         dest->lines->len-1);
115                 g_string_append(dest->text, src);
116                 g_array_append_val(dest->lines, dest->val);
117                 return;
118         }
119         if(len > 1 || dest->val == 0) 
120         {
121                 dest->val = len;        
122                 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
123                                                                         dest->lines->len-1);
124         }
125         else if (len == 1 && dest->val != 0) dest->val = 0;
126                                 
127         if(dest->val > 0)
128         { 
129                 g_string_append_len(dest->text, src, len);
130                 g_array_append_val(dest->lines, dest->val);
131         }
134 void formed_text_init(formed_text *text)
136         if(text->text != NULL) g_string_free(text->text, TRUE); 
137         text->text = g_string_new("");
138         
139         if(text->lines != NULL) g_array_free(text->lines, TRUE);
140         text->lines = g_array_new(FALSE, TRUE, 4);
141         
142         text->val = 0;
144 /*
145 char *check_lyr_hd(char *artist, char *title, int how)
146 { //checking whether for lyrics file existence and proper access
147         static char path[1024];
148         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", 
149                         getenv("HOME"), artist, title);
150     
151     if(g_access(path, how) != 0) return NULL;
152         return path;
153 }               
155 int get_lyr_hd(char *artist, char *title)
157         char *path = check_lyr_hd(artist, title, R_OK);
158         if(path == NULL) return -1;
159         
160         FILE *lyr_file; 
161         lyr_file = fopen(path, "r");
162         if(lyr_file == NULL) return -1;
163         
164         char *buf = NULL;
165         char **line = &buf;
166         size_t n = 0;
167         
168         while(1)
169         {
170          n = getline(line, &n, lyr_file); 
171          if( n < 1 || *line == NULL || feof(lyr_file) != 0 ) return 0;
172          add_text_line(&lyr_text, *line, n+1);
173          free(*line);
174          *line = NULL; n = 0;
175         }
176         
177         return 0;
178 }       
179     
180 int store_lyr_hd()
182         char artist[512];
183         char title[512];
184         static char path[1024];
185         FILE *lyr_file;
186         
187         get_text_line(&lyr_text, 0, artist, 512);
188         get_text_line(&lyr_text, 1, title, 512);
189         artist[strlen(artist)-2] = '\0';
190         title[strlen(title)-2] = '\0';
191         
192         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric", 
193                         getenv("HOME"), artist, title);
194         
195         if (check_lyr_hd(artist, title, F_OK) == 0) lyr_file = fopen(path, "w");
196         else lyr_file = fopen(path, "xw");
197         if(lyr_file == NULL) return -1;
198         
199         int i;
200         char line_buf[1024];
201         
202         for(i = 2; i <= lyr_text.text->len; i++)
203         {
204                 get_text_line(&lyr_text, i, line_buf, 0);
205                 fputs(line_buf, lyr_file);
206         }
207         fclose(lyr_file);
208         return 0;
210                                 
211 */      
212 void check_repaint()
214         if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
217 int check_dl_progress(void *clientp, double dltotal, double dlnow,
218                         double ultotal, double ulnow)
220         if(g_timer_elapsed(dltime, NULL) >= options.lyrics_timeout)
221         {       
222                 formed_text_init(&lyr_text);
223                 return -1;
224         }
225         return 0;
226 }       
230 static void check_content(void *data, const char *name, const char **atts)
232         if(strstr(name, "text") != NULL)
233         {
235                 result |= 16;
236         }
238         
240 static void check_search_response(void *data, const char *name,
241                  const char **atts)
243         if(strstr(name, "response") != NULL)
244         {
245         result |=2;
246         return;
247         }  
248         
249         if(result & 4)
250         {
251                 if(strstr(name, "result") != NULL)
252                 {
253                         if(strstr(atts[2], "hid") != NULL)
254                         {
255                                 hid = atts[3];
256                         }
257         
258                         if(strstr(atts[2], "exactMatch") != NULL)
259                         {
260                                 result |= 8;
261                         }                       
262                 }
263         }
264                         
267 static void end_tag(void *data, const char *name)
269   //hmmmmmm             
272   static void check_search_success(void *userData, const XML_Char *s, int len)
273     {
274         if(result & 2)  //lets first check whether we're right
275         {               //we don't really want to search in the wrong string
276                 if(strstr((char*) s, "SUCCESS"))
277                 {
278                 result |=4;
279                 }
280         }       
281     }
283 static void fetch_text(void *userData, const XML_Char *s, int len) 
285         if(result & 16)
286         {
287                 add_text_line(&lyr_text, s, len); 
288         }
291 gpointer get_lyr(void *c)
293         mpd_Status *status = ((mpdclient_t*)c)->status;
294         mpd_Song *cur = ((mpdclient_t*)c)->song; 
295         mpdclient_update((mpdclient_t*)c);
296         
297         if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state)))
298         {
299                 formed_text_init(&lyr_text);                    
300                 return NULL;
301         }
302         
303         char url_avail[256];
304         char url_hid[256];
305         char artist[MAX_SONGNAME_LENGTH];
306         char title[MAX_SONGNAME_LENGTH];
307         lock = 2;
308         result = 0;
309         
310         if(dltime == NULL) dltime = g_timer_new();
311         
312         strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
313         strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
314         
315         formed_text_init(&lyr_text);
316         add_text_line(&lyr_text, artist, 0);
317         add_text_line(&lyr_text, title, 0);
318         
319         //if(get_lyr_hd(artist, title) == 0) return &lyr_text;
320         
321         //this replacess the whitespaces with '+'
322         g_strdelimit(artist, " ", '+');
323         g_strdelimit(title, " ", '+');
324         
325         //we insert the artist and the title into the url               
326         snprintf(url_avail, 512, LEOSLYRICS_SEARCH_URL, artist, title);
328         //download that xml!
329         easy_download_struct lyr_avail = {NULL, 0,-1};  
330         
331         g_timer_start(dltime);
332         if(!easy_download(url_avail, &lyr_avail, check_dl_progress)) return NULL;
333         g_timer_stop(dltime);
335         //we gotta parse that stuff with expat
336         parser = XML_ParserCreate(NULL);
337         XML_SetUserData(parser, NULL);
338         
339         int state = 0;
341         XML_SetElementHandler(parser, check_search_response, end_tag);
342         XML_SetCharacterDataHandler(parser, check_search_success);
343         XML_Parse(parser, lyr_avail.data, strlen(lyr_avail.data), state);       
344         XML_ParserFree(parser); 
346         if(!(result & 4)) return NULL; //check whether lyrics found
347         easy_download_struct lyr_content = {NULL, 0,-1};  
348         snprintf(url_hid, 512, LEOSLYRICS_CONTENT_URL, hid);
349         
350         g_timer_continue(dltime);               
351         if(!(easy_download(url_hid, &lyr_content, check_dl_progress))) return NULL;
352         g_timer_stop(dltime);
353         
354         contentp = XML_ParserCreate(NULL);
355         XML_SetUserData(contentp, NULL);
356         XML_SetElementHandler(contentp, check_content, end_tag);        
357         XML_SetCharacterDataHandler(contentp, fetch_text);
358         XML_Parse(contentp, lyr_content.data, strlen(lyr_content.data), state);
359         XML_ParserFree(contentp);
360         
361         lw->start = 0;
362         check_repaint();
363         
364     lock = 1;
365         return &lyr_text;
366 }       
368 static char *
369 list_callback(int index, int *highlight, void *data)
371         static char buf[512];
372         int linelen;
374     //i think i'ts fine to write it into the 1st line...
375   if((index == lyr_text.lines->len && lyr_text.lines->len != 2)||
376           ((lyr_text.lines->len == 0 
377           ||lyr_text.lines->len == 2) && index == 0))
378   {
379     *highlight=3; 
380         return CREDITS;
381   }
382     
383   if(index < 2 && lyr_text.lines->len > 2) *highlight=3;
384   else if(index >=  lyr_text.lines->len || index == 2)
385   {
386           return "";
387   }
388   if(index >1) index--;
389   get_text_line(&lyr_text, index, buf, 512);
390   return buf;
391
394 static void
395 lyrics_init(WINDOW *w, int cols, int rows)
397   lw = list_window_init(w, cols, rows);
398   lw->flags = LW_HIDE_CURSOR;
399   //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
400   formed_text_init(&lyr_text);
401   if (!g_thread_supported()) g_thread_init(NULL);
402   
405 static void
406 lyrics_resize(int cols, int rows)
408   lw->cols = cols;
409   lw->rows = rows;
412 static void
413 lyrics_exit(void)
415   list_window_free(lw);
419 static char *
420 lyrics_title(char *str, size_t size)
422         if(lyr_text.lines->len == 2){ 
423         if(lock == 1 && !(result & 2)) return _("Lyrics  [No connection]");
424     if(lock == 1 && !(result & 4)) return _("Lyrics  [Not found]");             
425         if(lock == 2) return _("Lyrics  [retrieving]");
426         }
427         /*if(lyr_text.lines->len > 2) 
428         {
429                 static char buf[512];
430                 char artist[512];
431                 char title[512];
432                 get_text_line(&lyr_text, 0, artist, 512);
433                 get_text_line(&lyr_text, 1, artist, 512);
434                 snprintf(buf, 512, "Lyrics  %s - %s", artist, title);
435                 return buf;
436         }*/
437         return "Lyrics";
440 static void 
441 lyrics_paint(screen_t *screen, mpdclient_t *c)
443   lw->clear = 1;
444   list_window_paint(lw, list_callback, NULL);
445   wrefresh(lw->w);
448 static void 
449 lyrics_update(screen_t *screen, mpdclient_t *c)
450 {  
451   if( lw->repaint )
452     {
453       list_window_paint(lw, list_callback, NULL);
454       wrefresh(lw->w);
455       lw->repaint = 0;
456     }
460 static int 
461 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
463   lw->repaint=1;
464   switch(cmd)
465     {
466     case CMD_LIST_NEXT:
467       if( lw->start+lw->rows < lyr_text.lines->len+1 )
468         lw->start++;
469       return 1;
470     case CMD_LIST_PREVIOUS:
471       if( lw->start >0 )
472         lw->start--;
473       return 1;
474     case CMD_LIST_FIRST:
475       lw->start = 0;
476       return 1;
477     case CMD_LIST_LAST:
478       lw->start = lyrics_text_rows-lw->rows;
479       if( lw->start<0 )
480         lw->start = 0;
481       return 1;
482     case CMD_LIST_NEXT_PAGE:
483       lw->start = lw->start + lw->rows-1;
484       if( lw->start+lw->rows >= lyr_text.lines->len+1 )
485         lw->start = lyr_text.lines->len-lw->rows+1;
486       if( lw->start<0 )
487         lw->start = 0;
488        return 1;
489     case CMD_LIST_PREVIOUS_PAGE:
490       lw->start = lw->start - lw->rows;
491       if( lw->start<0 )
492         lw->start = 0;
493       return 1;
494         case CMD_SELECT:
495           g_thread_create(get_lyr, c, FALSE, NULL);     
496           return 1;     
497         default:
498       break;
499     }
501   lw->selected = lw->start+lw->rows;
502   if( screen_find(screen, c, 
503                   lw,  lyrics_text_rows,
504                   cmd, list_callback, NULL) )
505     {
506       /* center the row */
507       lw->start = lw->selected-(lw->rows/2);
508       if( lw->start+lw->rows > lyrics_text_rows )
509         lw->start = lyrics_text_rows-lw->rows;
510       if( lw->start<0 )
511         lw->start=0;
512       return 1;
513     }
515   return 0;
518 static list_window_t *
519 lyrics_lw(void)
521   return lw;
524 screen_functions_t *
525 get_screen_lyrics(void)
527   static screen_functions_t functions;
529   memset(&functions, 0, sizeof(screen_functions_t));
530   functions.init   = lyrics_init;
531   functions.exit   = lyrics_exit;
532   functions.open   = NULL;
533   functions.close  = NULL;
534   functions.resize = lyrics_resize;
535   functions.paint  = lyrics_paint;
536   functions.update = lyrics_update;
537   functions.cmd    = lyrics_cmd;
538   functions.get_lw = lyrics_lw;
539   functions.get_title = lyrics_title;
541   return &functions;
543 #endif /* ENABLE_LYRICS_SCREEN */