Code

lyrics: missing "break"
[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"
34 #include "gcc.h"
36 #define _GNU_SOURCE
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <ncurses.h>
41 #include <expat.h>
42 #include <unistd.h>
43 #include <glib/gstdio.h>
44 #include <stdio.h>
46 static list_window_t *lw = NULL;
47 static int lyrics_text_rows = -1;
48 static int src_selection;
50 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
52 static FILE *create_lyr_file(char *artist, char *title)
53 {
54         char path[1024];
56         snprintf(path, 1024, "%s/.lyrics",
57                  getenv("HOME"));
58         if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
60         snprintf(path, 1024, "%s/.lyrics/%s",
61                  getenv("HOME"), artist);
62         if(g_access(path, W_OK) != 0) if(mkdir(path, S_IRWXU) != 0) return NULL;
64         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
65                  getenv("HOME"), artist, title);
67         return fopen(path, "w");
68 }
71 static int store_lyr_hd(void)
72 {
73         char artist[512];
74         char title[512];
75         static char path[1024];
76         FILE *lyr_file;
77         unsigned i;
78         char line_buf[1024];
80         get_text_line(&lyr_text, 0, artist, 512);
81         get_text_line(&lyr_text, 1, title, 512);
82         artist[strlen(artist)-1] = '\0';
83         title[strlen(title)-1] = '\0';
85         snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
86                  getenv("HOME"), artist, title);
87         lyr_file = create_lyr_file(artist, title);
88         if (lyr_file == NULL)
89                 return -1;
91         for (i = 3; i <= lyr_text.text->len; i++) {
92                 if (get_text_line(&lyr_text, i, line_buf, 1024) == -1)
93                         break;
94                 fputs(line_buf, lyr_file);
95         }
97         fclose(lyr_file);
98         return 0;
99 }
102 static void check_repaint(void)
104         if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
108 static gpointer get_lyr(void *c)
110         mpd_Status *status = ((retrieval_spec*)c)->client->status;
111         mpd_Song *cur = ((retrieval_spec*)c)->client->song;
112         char artist[MAX_SONGNAME_LENGTH];
113         char title[MAX_SONGNAME_LENGTH];
115         //mpdclient_update((mpdclient_t*)c);
117         if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) {
118                 formed_text_init(&lyr_text);
119                 return NULL;
120         }
123         lock=2;
124         result = 0;
126         formed_text_init(&lyr_text);
128         strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
129         strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
131         //write header..
132         formed_text_init(&lyr_text);
133         add_text_line(&lyr_text, artist, 0);
134         add_text_line(&lyr_text, title, 0);
135         add_text_line(&lyr_text, "", 0);
136         add_text_line(&lyr_text, "", 0);
138         if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ {
139                 if(get_lyr_by_src (src_selection, artist, title) != 0) {
140                         lock=0;
141                         return NULL;
142                 }
143         }
144         /*else{
145           if(get_lyr_hd(artist, title) != 0)
146           {
147           if(get_lyr_hd(artist, title) != 0) return NULL;
148           }
149           else result |= 1;
150           }*/
151         //return NULL;
152         lw->start = 0;
153         check_repaint();
154         lock = 1;
155         return &lyr_text;
158 static const char *
159 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
161         static char buf[512];
163         //i think i'ts fine to write it into the 1st line...
164         if ((idx == lyr_text.lines->len && lyr_text.lines->len > 4) ||
165             ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) &&
166              idx == 0)) {
167                 src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection);
168                 *highlight=3;
169                 if (selected != NULL)
170                         return selected->description;
171                 return "";
172         }
174         if (idx < 2 && lyr_text.lines->len > 4)
175                 *highlight=3;
176         else if(idx >= lyr_text.lines->len ||
177                 (idx < 4 && idx != 0 && lyr_text.lines->len < 5)) {
178                 return "";
179         }
181         get_text_line(&lyr_text, idx, buf, 512);
182         return buf;
186 static void
187 lyrics_init(WINDOW *w, int cols, int rows)
189         lw = list_window_init(w, cols, rows);
190         lw->flags = LW_HIDE_CURSOR;
191         //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
192         formed_text_init(&lyr_text);
193         if (!g_thread_supported())
194                 g_thread_init(NULL);
197 static void
198 lyrics_resize(int cols, int rows)
200         lw->cols = cols;
201         lw->rows = rows;
204 static void
205 lyrics_exit(void)
207         list_window_free(lw);
211 static const char *
212 lyrics_title(mpd_unused char *str, mpd_unused size_t size)
214         static GString *msg;
215         if (msg == NULL)
216                 msg = g_string_new ("");
217         else g_string_erase (msg, 0, -1);
219         g_string_append (msg, "Lyrics  [");
221         if (src_selection > (int)src_lyr_stack->len - 1)
222                 g_string_append (msg, "No plugin available");
223         else {
224                 src_lyr* selected =  g_array_index (src_lyr_stack, src_lyr*, src_selection);
225                 if (selected != NULL)
226                         g_string_append (msg, selected->name);
227                 else
228                         g_string_append (msg, "NONE");
229         }
231         if(lyr_text.lines->len == 4) {
232                 if(lock == 1) {
233                         if(!(result & 1)) {
234                                 g_string_append (msg, " - ");
235                                 if(!(result & 2)) g_string_append (msg, _("No access"));
236                                 else if(!(result & 4)||!(result & 16)) g_string_append (msg, _("Not found"));
237                         }
238                 }
239                 if(lock == 2) {
240                         g_string_append (msg, " - ");
241                         g_string_append (msg, _("retrieving"));
242                 }
243         }
245         g_string_append_c (msg, ']');
247         return msg->str;
250 static void
251 lyrics_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
253         lw->clear = 1;
254         list_window_paint(lw, list_callback, NULL);
255         wrefresh(lw->w);
259 static void
260 lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
262         if( lw->repaint ) {
263                 list_window_paint(lw, list_callback, NULL);
264                 wrefresh(lw->w);
265                 lw->repaint = 0;
266         }
270 static int
271 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
273         static retrieval_spec spec;
275         lw->repaint=1;
276         switch(cmd) {
277         case CMD_LIST_NEXT:
278                 if( lw->start+lw->rows < lyr_text.lines->len+1 )
279                         lw->start++;
280                 return 1;
281         case CMD_LIST_PREVIOUS:
282                 if( lw->start >0 )
283                         lw->start--;
284                 return 1;
285         case CMD_LIST_FIRST:
286                 lw->start = 0;
287                 return 1;
288         case CMD_LIST_LAST:
289                 if ((unsigned)lyrics_text_rows > lw->rows)
290                         lw->start = lyrics_text_rows - lw->rows;
291                 else
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 >= (unsigned)lyrics_text_rows + 1) {
297                         if ((unsigned)lyrics_text_rows + 1 > lw->rows)
298                                 lw->start = lyrics_text_rows + 1 - lw->rows;
299                         else
300                                 lw->start = 0;
301                 }
302                 return 1;
303         case CMD_LIST_PREVIOUS_PAGE:
304                 if (lw->start > lw->rows)
305                         lw->start -= lw->rows;
306                 else
307                         lw->start = 0;
308                 return 1;
309         case CMD_SELECT:
310                 spec.client = c;
311                 spec.way = 0;
312                 g_thread_create(get_lyr, &spec, FALSE, NULL);
313                 return 1;
314         case CMD_INTERRUPT:
315                 if(lock > 1) lock = 4;
316                 return 1;
317         case CMD_ADD:
318                 if(lock > 0 && lock != 4) {
319                         if(store_lyr_hd() == 0)
320                                 screen_status_message (_("Lyrics saved!"));
321                 }
322                 return 1;
323         case CMD_LYRICS_UPDATE:
324                 spec.client = c;
325                 spec.way = 1;
326                 g_thread_create(get_lyr, &spec, FALSE, NULL);
327                 return 1;
328         case CMD_SEARCH_MODE:
329                 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
330                 if (src_selection == (int)src_lyr_stack->len - 1)
331                         src_selection = -1;
332                 src_selection++;
333                 return 1;
334         default:
335                 break;
336         }
338         lw->selected = lw->start+lw->rows;
339         if (screen_find(screen,
340                         lw,  lyrics_text_rows,
341                         cmd, list_callback, NULL)) {
342                 /* center the row */
343                 lw->start = lw->selected - (lw->rows / 2);
344                 if (lw->start + lw->rows > (unsigned)lyrics_text_rows) {
345                         if (lw->rows < (unsigned)lyrics_text_rows)
346                                 lw->start = lyrics_text_rows - lw->rows;
347                         else
348                                 lw->start = 0;
349                 }
350                 return 1;
351         }
353         return 0;
356 static list_window_t *
357 lyrics_lw(void)
359   return lw;
362 screen_functions_t *
363 get_screen_lyrics(void)
365   static screen_functions_t functions;
367   memset(&functions, 0, sizeof(screen_functions_t));
368   functions.init   = lyrics_init;
369   functions.exit   = lyrics_exit;
370   functions.open   = NULL;
371   functions.close  = NULL;
372   functions.resize = lyrics_resize;
373   functions.paint  = lyrics_paint;
374   functions.update = lyrics_update;
375   functions.cmd    = lyrics_cmd;
376   functions.get_lw = lyrics_lw;
377   functions.get_title = lyrics_title;
379   return &functions;
381 #endif /* ENABLE_LYRICS_SCREEN */