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 fputs(line_buf, lyr_file);
94 }
96 fclose(lyr_file);
97 return 0;
98 }
101 static void check_repaint(void)
102 {
103 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
104 }
107 static gpointer get_lyr(void *c)
108 {
109 mpd_Status *status = ((retrieval_spec*)c)->client->status;
110 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
111 char artist[MAX_SONGNAME_LENGTH];
112 char title[MAX_SONGNAME_LENGTH];
114 //mpdclient_update((mpdclient_t*)c);
116 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) {
117 formed_text_init(&lyr_text);
118 return NULL;
119 }
122 lock=2;
123 result = 0;
125 formed_text_init(&lyr_text);
127 strfsong(artist, MAX_SONGNAME_LENGTH, "%artist%", cur);
128 strfsong(title, MAX_SONGNAME_LENGTH, "%title%", cur);
130 //write header..
131 formed_text_init(&lyr_text);
132 add_text_line(&lyr_text, artist, 0);
133 add_text_line(&lyr_text, title, 0);
134 add_text_line(&lyr_text, "", 0);
135 add_text_line(&lyr_text, "", 0);
137 if (((retrieval_spec*)c)->way != -1) /*till it'S of use*/ {
138 if(get_lyr_by_src (src_selection, artist, title) != 0) {
139 lock=0;
140 return NULL;
141 }
142 }
143 /*else{
144 if(get_lyr_hd(artist, title) != 0)
145 {
146 if(get_lyr_hd(artist, title) != 0) return NULL;
147 }
148 else result |= 1;
149 }*/
150 //return NULL;
151 lw->start = 0;
152 check_repaint();
153 lock = 1;
154 return &lyr_text;
155 }
157 static const char *
158 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
159 {
160 static char buf[512];
162 //i think i'ts fine to write it into the 1st line...
163 if ((idx == lyr_text.lines->len && lyr_text.lines->len > 4) ||
164 ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) &&
165 idx == 0)) {
166 src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection);
167 *highlight=3;
168 if (selected != NULL)
169 return selected->description;
170 return "";
171 }
173 if (idx < 2 && lyr_text.lines->len > 4)
174 *highlight=3;
175 else if(idx >= lyr_text.lines->len ||
176 (idx < 4 && idx != 0 && lyr_text.lines->len < 5)) {
177 return "";
178 }
180 get_text_line(&lyr_text, idx, buf, 512);
181 return buf;
182 }
185 static void
186 lyrics_init(WINDOW *w, int cols, int rows)
187 {
188 lw = list_window_init(w, cols, rows);
189 lw->flags = LW_HIDE_CURSOR;
190 //lyr_text.lines = g_array_new(FALSE, TRUE, 4);
191 formed_text_init(&lyr_text);
192 if (!g_thread_supported())
193 g_thread_init(NULL);
194 }
196 static void
197 lyrics_resize(int cols, int rows)
198 {
199 lw->cols = cols;
200 lw->rows = rows;
201 }
203 static void
204 lyrics_exit(void)
205 {
206 list_window_free(lw);
207 }
210 static const char *
211 lyrics_title(mpd_unused char *str, mpd_unused size_t size)
212 {
213 static GString *msg;
214 if (msg == NULL)
215 msg = g_string_new ("");
216 else g_string_erase (msg, 0, -1);
218 g_string_append (msg, "Lyrics [");
220 if (src_selection > (int)src_lyr_stack->len - 1)
221 g_string_append (msg, "No plugin available");
222 else {
223 src_lyr* selected = g_array_index (src_lyr_stack, src_lyr*, src_selection);
224 if (selected != NULL)
225 g_string_append (msg, selected->name);
226 else
227 g_string_append (msg, "NONE");
228 }
230 if(lyr_text.lines->len == 4) {
231 if(lock == 1) {
232 if(!(result & 1)) {
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 g_string_append (msg, " - ");
240 g_string_append (msg, _("retrieving"));
241 }
242 }
244 g_string_append_c (msg, ']');
246 return msg->str;
247 }
249 static void
250 lyrics_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
251 {
252 lw->clear = 1;
253 list_window_paint(lw, list_callback, NULL);
254 wrefresh(lw->w);
255 }
258 static void
259 lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
260 {
261 if( lw->repaint ) {
262 list_window_paint(lw, list_callback, NULL);
263 wrefresh(lw->w);
264 lw->repaint = 0;
265 }
266 }
269 static int
270 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
271 {
272 static retrieval_spec spec;
274 lw->repaint=1;
275 switch(cmd) {
276 case CMD_LIST_NEXT:
277 if( lw->start+lw->rows < lyr_text.lines->len+1 )
278 lw->start++;
279 return 1;
280 case CMD_LIST_PREVIOUS:
281 if( lw->start >0 )
282 lw->start--;
283 return 1;
284 case CMD_LIST_FIRST:
285 lw->start = 0;
286 return 1;
287 case CMD_LIST_LAST:
288 if ((unsigned)lyrics_text_rows > lw->rows)
289 lw->start = lyrics_text_rows - lw->rows;
290 else
291 lw->start = 0;
292 return 1;
293 case CMD_LIST_NEXT_PAGE:
294 lw->start = lw->start + lw->rows - 1;
295 if (lw->start + lw->rows >= (unsigned)lyrics_text_rows + 1) {
296 if ((unsigned)lyrics_text_rows + 1 > lw->rows)
297 lw->start = lyrics_text_rows + 1 - lw->rows;
298 else
299 lw->start = 0;
300 }
301 return 1;
302 case CMD_LIST_PREVIOUS_PAGE:
303 if (lw->start > lw->rows)
304 lw->start -= lw->rows;
305 else
306 lw->start = 0;
307 return 1;
308 case CMD_SELECT:
309 spec.client = c;
310 spec.way = 0;
311 g_thread_create(get_lyr, &spec, FALSE, NULL);
312 return 1;
313 case CMD_INTERRUPT:
314 if(lock > 1) lock = 4;
315 return 1;
316 case CMD_ADD:
317 if(lock > 0 && lock != 4) {
318 if(store_lyr_hd() == 0)
319 screen_status_message (_("Lyrics saved!"));
320 }
321 return 1;
322 case CMD_LYRICS_UPDATE:
323 spec.client = c;
324 spec.way = 1;
325 g_thread_create(get_lyr, &spec, FALSE, NULL);
326 return 1;
327 case CMD_SEARCH_MODE:
328 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
329 if (src_selection == (int)src_lyr_stack->len - 1)
330 src_selection = -1;
331 src_selection++;
332 return 1;
333 default:
334 break;
335 }
337 lw->selected = lw->start+lw->rows;
338 if (screen_find(screen,
339 lw, lyrics_text_rows,
340 cmd, list_callback, NULL)) {
341 /* center the row */
342 lw->start = lw->selected - (lw->rows / 2);
343 if (lw->start + lw->rows > (unsigned)lyrics_text_rows) {
344 if (lw->rows < (unsigned)lyrics_text_rows)
345 lw->start = lyrics_text_rows - lw->rows;
346 else
347 lw->start = 0;
348 }
349 return 1;
350 }
352 return 0;
353 }
355 static list_window_t *
356 lyrics_lw(void)
357 {
358 return lw;
359 }
361 screen_functions_t *
362 get_screen_lyrics(void)
363 {
364 static screen_functions_t functions;
366 memset(&functions, 0, sizeof(screen_functions_t));
367 functions.init = lyrics_init;
368 functions.exit = lyrics_exit;
369 functions.open = NULL;
370 functions.close = NULL;
371 functions.resize = lyrics_resize;
372 functions.paint = lyrics_paint;
373 functions.update = lyrics_update;
374 functions.cmd = lyrics_cmd;
375 functions.get_lw = lyrics_lw;
376 functions.get_title = lyrics_title;
378 return &functions;
379 }
380 #endif /* ENABLE_LYRICS_SCREEN */