Code

include cleanup
[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  *
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 #include "config.h"
23 #ifndef DISABLE_LYRICS_SCREEN
24 #include <sys/stat.h>
25 #include "ncmpc.h"
26 #include "options.h"
27 #include "mpdclient.h"
28 #include "command.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "easy_download.h"
32 #include "strfsong.h"
33 #include "src_lyrics.h"
35 #define _GNU_SOURCE
36 #include <stdlib.h>
37 #include <string.h>
38 #include <glib.h>
39 #include <ncurses.h>
40 #include <expat.h>
41 #include <unistd.h>
42 #include <glib/gstdio.h>
43 #include <stdio.h>
45 int src_selection;
47 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
49 static 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 static int store_lyr_hd(void)
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)
84                 return -1;
86         int i;
87         char line_buf[1024];
89         for (i = 3; i <= lyr_text.text->len; i++) {
90                 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
91                 fputs(line_buf, lyr_file);
92         }
94         fclose(lyr_file);
95         return 0;
96 }
99 static void check_repaint(void)
101         if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
105 static gpointer get_lyr(void *c)
107         mpd_Status *status = ((retrieval_spec*)c)->client->status;
108         mpd_Song *cur = ((retrieval_spec*)c)->client->song;
109         //mpdclient_update((mpdclient_t*)c);
111         if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) {
112                 formed_text_init(&lyr_text);
113                 return NULL;
114         }
117         char artist[MAX_SONGNAME_LENGTH];
118         char title[MAX_SONGNAME_LENGTH];
119         lock=2;
120         result = 0;
122         formed_text_init(&lyr_text);
124         strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
125         strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
127         //write header..
128         formed_text_init(&lyr_text);
129         add_text_line(&lyr_text, artist, 0);
130         add_text_line(&lyr_text, title, 0);
131         add_text_line(&lyr_text, "", 0);
132         add_text_line(&lyr_text, "", 0);
134         if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ {
135                 if(get_lyr_by_src (src_selection, artist, title) != 0) {
136                         lock=0;
137                         return NULL;
138                 }
139         }
140         /*else{
141           if(get_lyr_hd(artist, title) != 0)
142           {
143           if(get_lyr_hd(artist, title) != 0) return NULL;
144           }
145           else result |= 1;
146           }*/
147         //return NULL;
148         lw->start = 0;
149         check_repaint();
150         lock = 1;
151         return &lyr_text;
154 static const char *
155 list_callback(int index, int *highlight, void *data)
157         static char buf[512];
159         //i think i'ts fine to write it into the 1st line...
160         if ((index == lyr_text.lines->len && lyr_text.lines->len > 4) ||
161             ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) &&
162              index == 0)) {
163                 *highlight=3;
164                 src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection);
165                 if (selected != NULL)
166                         return selected->description;
167                 return "";
168         }
170         if (index < 2 && lyr_text.lines->len > 4)
171                 *highlight=3;
172         else if(index >= lyr_text.lines->len ||
173                 (index < 4 && index != 0 && lyr_text.lines->len < 5)) {
174                 return "";
175         }
177         get_text_line(&lyr_text, index, buf, 512);
178         return buf;
182 static void
183 lyrics_init(WINDOW *w, int cols, int rows)
185         lw = list_window_init(w, cols, rows);
186         lw->flags = LW_HIDE_CURSOR;
187         //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
188         formed_text_init(&lyr_text);
189         if (!g_thread_supported())
190                 g_thread_init(NULL);
193 static void
194 lyrics_resize(int cols, int rows)
196         lw->cols = cols;
197         lw->rows = rows;
200 static void
201 lyrics_exit(void)
203         list_window_free(lw);
207 static const char *
208 lyrics_title(char *str, size_t size)
210         static GString *msg;
211         if (msg == NULL)
212                 msg = g_string_new ("");
213         else g_string_erase (msg, 0, -1);
215         g_string_append (msg, "Lyrics  [");
217         if (src_selection > src_lyr_stack->len - 1)
218                 g_string_append (msg, "No plugin available");
219         else {
220                 src_lyr* selected =  g_array_index (src_lyr_stack, src_lyr*, src_selection);
221                 if (selected != NULL)
222                         g_string_append (msg, selected->name);
223                 else
224                         g_string_append (msg, "NONE");
225         }
227         if(lyr_text.lines->len == 4) {
228                 if(lock == 1) {
229                         if(!(result & 1)) {
230                                 g_string_append (msg, " - ");
231                                 if(!(result & 2)) g_string_append (msg, _("No access"));
232                                 else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found"));
233                         }
234                 }
235                 if(lock == 2) {
236                         g_string_append (msg, " - ");
237                         g_string_append (msg, _("retrieving"));
238                 }
239         }
241         g_string_append_c (msg, ']');
243         return msg->str;
246 static void
247 lyrics_paint(screen_t *screen, mpdclient_t *c)
249         lw->clear = 1;
250         list_window_paint(lw, list_callback, NULL);
251         wrefresh(lw->w);
255 static void
256 lyrics_update(screen_t *screen, mpdclient_t *c)
258         if( lw->repaint ) {
259                 list_window_paint(lw, list_callback, NULL);
260                 wrefresh(lw->w);
261                 lw->repaint = 0;
262         }
266 static int
267 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
269         static retrieval_spec spec;
271         lw->repaint=1;
272         switch(cmd) {
273         case CMD_LIST_NEXT:
274                 if( lw->start+lw->rows < lyr_text.lines->len+1 )
275                         lw->start++;
276                 return 1;
277         case CMD_LIST_PREVIOUS:
278                 if( lw->start >0 )
279                         lw->start--;
280                 return 1;
281         case CMD_LIST_FIRST:
282                 lw->start = 0;
283                 return 1;
284         case CMD_LIST_LAST:
285                 lw->start = lyrics_text_rows-lw->rows;
286                 if( lw->start<0 )
287                         lw->start = 0;
288                 return 1;
289         case CMD_LIST_NEXT_PAGE:
290                 lw->start = lw->start + lw->rows-1;
291                 if( lw->start+lw->rows >= lyr_text.lines->len+1 )
292                         lw->start = lyr_text.lines->len-lw->rows+1;
293                 if( lw->start<0 )
294                         lw->start = 0;
295                 return 1;
296         case CMD_LIST_PREVIOUS_PAGE:
297                 lw->start = lw->start - lw->rows;
298                 if( lw->start<0 )
299                         lw->start = 0;
300                 return 1;
301         case CMD_SELECT:
302                 spec.client = c;
303                 spec.way = 0;
304                 g_thread_create(get_lyr, &spec, FALSE, NULL);
305                 return 1;
306         case CMD_INTERRUPT:
307                 if(lock > 1) lock = 4;
308                 return 1;
309         case CMD_ADD:
310                 if(lock > 0 && lock != 4) {
311                         if(store_lyr_hd() == 0)
312                                 screen_status_message (_("Lyrics saved!"));
313                 }
314                 return 1;
315         case CMD_LYRICS_UPDATE:
316                 spec.client = c;
317                 spec.way = 1;
318                 g_thread_create(get_lyr, &spec, FALSE, NULL);
319                 return 1;
320         case CMD_SEARCH_MODE:
321                 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
322                 if (src_selection == src_lyr_stack->len - 1)
323                         src_selection = -1;
324                 src_selection++;
325                 return 1;
326         default:
327                 break;
328         }
330         lw->selected = lw->start+lw->rows;
331         if (screen_find(screen,
332                         lw,  lyrics_text_rows,
333                         cmd, list_callback, NULL)) {
334                 /* center the row */
335                 lw->start = lw->selected - (lw->rows / 2);
336                 if (lw->start + lw->rows > lyrics_text_rows)
337                         lw->start = lyrics_text_rows - lw->rows;
338                 if (lw->start < 0)
339                         lw->start = 0;
340                 return 1;
341         }
343         return 0;
346 static list_window_t *
347 lyrics_lw(void)
349   return lw;
352 screen_functions_t *
353 get_screen_lyrics(void)
355   static screen_functions_t functions;
357   memset(&functions, 0, sizeof(screen_functions_t));
358   functions.init   = lyrics_init;
359   functions.exit   = lyrics_exit;
360   functions.open   = NULL;
361   functions.close  = NULL;
362   functions.resize = lyrics_resize;
363   functions.paint  = lyrics_paint;
364   functions.update = lyrics_update;
365   functions.cmd    = lyrics_cmd;
366   functions.get_lw = lyrics_lw;
367   functions.get_title = lyrics_title;
369   return &functions;
371 #endif /* ENABLE_LYRICS_SCREEN */