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;
74 unsigned i;
75 char line_buf[1024];
77 get_text_line(&lyr_text, 0, artist, 512);
78 get_text_line(&lyr_text, 1, title, 512);
79 artist[strlen(artist)-1] = '\0';
80 title[strlen(title)-1] = '\0';
82 snprintf(path, 1024, "%s/.lyrics/%s/%s.lyric",
83 getenv("HOME"), artist, title);
84 lyr_file = create_lyr_file(artist, title);
85 if (lyr_file == NULL)
86 return -1;
88 for (i = 3; i <= lyr_text.text->len; i++) {
89 if(get_text_line(&lyr_text, i, line_buf, 1024) == -1);
90 fputs(line_buf, lyr_file);
91 }
93 fclose(lyr_file);
94 return 0;
95 }
98 static void check_repaint(void)
99 {
100 if(screen_get_id("lyrics") == get_cur_mode_id())lyrics_paint(NULL, NULL);
101 }
104 static gpointer get_lyr(void *c)
105 {
106 mpd_Status *status = ((retrieval_spec*)c)->client->status;
107 mpd_Song *cur = ((retrieval_spec*)c)->client->song;
108 char artist[MAX_SONGNAME_LENGTH];
109 char title[MAX_SONGNAME_LENGTH];
111 //mpdclient_update((mpdclient_t*)c);
113 if(!(IS_PAUSED(status->state)||IS_PLAYING(status->state))) {
114 formed_text_init(&lyr_text);
115 return NULL;
116 }
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;
152 }
154 static const char *
155 list_callback(unsigned idx, int *highlight, void *data)
156 {
157 static char buf[512];
159 //i think i'ts fine to write it into the 1st line...
160 if ((idx == lyr_text.lines->len && lyr_text.lines->len > 4) ||
161 ((lyr_text.lines->len == 0 || lyr_text.lines->len == 4) &&
162 idx == 0)) {
163 src_lyr* selected = g_array_index(src_lyr_stack, src_lyr*, src_selection);
164 *highlight=3;
165 if (selected != NULL)
166 return selected->description;
167 return "";
168 }
170 if (idx < 2 && lyr_text.lines->len > 4)
171 *highlight=3;
172 else if(idx >= lyr_text.lines->len ||
173 (idx < 4 && idx != 0 && lyr_text.lines->len < 5)) {
174 return "";
175 }
177 get_text_line(&lyr_text, idx, buf, 512);
178 return buf;
179 }
182 static void
183 lyrics_init(WINDOW *w, int cols, int rows)
184 {
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);
191 }
193 static void
194 lyrics_resize(int cols, int rows)
195 {
196 lw->cols = cols;
197 lw->rows = rows;
198 }
200 static void
201 lyrics_exit(void)
202 {
203 list_window_free(lw);
204 }
207 static const char *
208 lyrics_title(char *str, size_t size)
209 {
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 > (int)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;
244 }
246 static void
247 lyrics_paint(screen_t *screen, mpdclient_t *c)
248 {
249 lw->clear = 1;
250 list_window_paint(lw, list_callback, NULL);
251 wrefresh(lw->w);
252 }
255 static void
256 lyrics_update(screen_t *screen, mpdclient_t *c)
257 {
258 if( lw->repaint ) {
259 list_window_paint(lw, list_callback, NULL);
260 wrefresh(lw->w);
261 lw->repaint = 0;
262 }
263 }
266 static int
267 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
268 {
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 if ((unsigned)lyrics_text_rows > lw->rows)
286 lw->start = lyrics_text_rows - lw->rows;
287 else
288 lw->start = 0;
289 return 1;
290 case CMD_LIST_NEXT_PAGE:
291 lw->start = lw->start + lw->rows - 1;
292 if (lw->start + lw->rows >= (unsigned)lyrics_text_rows + 1) {
293 if ((unsigned)lyrics_text_rows + 1 > lw->rows)
294 lw->start = lyrics_text_rows + 1 - lw->rows;
295 else
296 lw->start = 0;
297 }
298 return 1;
299 case CMD_LIST_PREVIOUS_PAGE:
300 if (lw->start > lw->rows)
301 lw->start -= lw->rows;
302 else
303 lw->start = 0;
304 return 1;
305 case CMD_SELECT:
306 spec.client = c;
307 spec.way = 0;
308 g_thread_create(get_lyr, &spec, FALSE, NULL);
309 return 1;
310 case CMD_INTERRUPT:
311 if(lock > 1) lock = 4;
312 return 1;
313 case CMD_ADD:
314 if(lock > 0 && lock != 4) {
315 if(store_lyr_hd() == 0)
316 screen_status_message (_("Lyrics saved!"));
317 }
318 return 1;
319 case CMD_LYRICS_UPDATE:
320 spec.client = c;
321 spec.way = 1;
322 g_thread_create(get_lyr, &spec, FALSE, NULL);
323 return 1;
324 case CMD_SEARCH_MODE:
325 //while (0==0) fprintf (stderr, "%i", src_lyr_stack->len);
326 if (src_selection == (int)src_lyr_stack->len - 1)
327 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 /* center the row */
339 lw->start = lw->selected - (lw->rows / 2);
340 if (lw->start + lw->rows > (unsigned)lyrics_text_rows) {
341 if (lw->rows < (unsigned)lyrics_text_rows)
342 lw->start = lyrics_text_rows - lw->rows;
343 else
344 lw->start = 0;
345 }
346 return 1;
347 }
349 return 0;
350 }
352 static list_window_t *
353 lyrics_lw(void)
354 {
355 return lw;
356 }
358 screen_functions_t *
359 get_screen_lyrics(void)
360 {
361 static screen_functions_t functions;
363 memset(&functions, 0, sizeof(screen_functions_t));
364 functions.init = lyrics_init;
365 functions.exit = lyrics_exit;
366 functions.open = NULL;
367 functions.close = NULL;
368 functions.resize = lyrics_resize;
369 functions.paint = lyrics_paint;
370 functions.update = lyrics_update;
371 functions.cmd = lyrics_cmd;
372 functions.get_lw = lyrics_lw;
373 functions.get_title = lyrics_title;
375 return &functions;
376 }
377 #endif /* ENABLE_LYRICS_SCREEN */