Code

list_window: removed property "clear"
[ncmpc.git] / src / screen_lyrics.c
1 /*
2  * (c) 2006 by Kalle Wallin <kaw@linux.se>
3  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
20 #include "config.h"
21 #ifndef DISABLE_LYRICS_SCREEN
22 #include <sys/stat.h>
23 #include "ncmpc.h"
24 #include "options.h"
25 #include "mpdclient.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "strfsong.h"
30 #include "lyrics.h"
31 #include "gcc.h"
33 #define _GNU_SOURCE
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <ncurses.h>
38 #include <unistd.h>
39 #include <stdio.h>
41 static list_window_t *lw = NULL;
43 static struct {
44         const struct mpd_song *song;
46         char *artist, *title;
48         struct lyrics_loader *loader;
50         GPtrArray *lines;
51 } current;
53 static void
54 screen_lyrics_abort(void)
55 {
56         if (current.loader != NULL) {
57                 lyrics_free(current.loader);
58                 current.loader = NULL;
59         }
61         if (current.artist != NULL) {
62                 g_free(current.artist);
63                 current.artist = NULL;
64         }
66         if (current.title != NULL) {
67                 g_free(current.title);
68                 current.artist = NULL;
69         }
71         current.song = NULL;
72 }
74 static void
75 screen_lyrics_clear(void)
76 {
77         guint i;
79         for (i = 0; i < current.lines->len; ++i)
80                 g_free(g_ptr_array_index(current.lines, i));
82         g_ptr_array_set_size(current.lines, 0);
83 }
85 static const char *
86 list_callback(unsigned idx, int *highlight, void *data);
88 static void
89 screen_lyrics_set(const GString *str)
90 {
91         const char *p, *eol, *next;
93         screen_lyrics_clear();
95         p = str->str;
96         while ((eol = strchr(p, '\n')) != NULL) {
97                 char *line;
99                 next = eol + 1;
101                 /* strip whitespace at end */
103                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
104                         --eol;
106                 /* create copy and append it to current.lines*/
108                 line = g_malloc(eol - p + 1);
109                 memcpy(line, p, eol - p);
110                 line[eol - p] = 0;
112                 g_ptr_array_add(current.lines, line);
114                 /* reset control characters */
116                 for (eol = line + (eol - p); line < eol; ++line)
117                         if ((unsigned char)*line < 0x20)
118                                 *line = ' ';
120                 p = next;
121         }
123         if (*p != 0)
124                 g_ptr_array_add(current.lines, g_strdup(p));
126         /* paint new data */
128         if (get_cur_mode_id() == 104) { /* XXX don't use the literal number */
129                 list_window_paint(lw, list_callback, NULL);
130                 wrefresh(lw->w);
132                 /* XXX repaint the screen title */
133         }
136 static void
137 screen_lyrics_callback(const GString *result, mpd_unused void *data)
139         assert(current.loader != NULL);
141         if (result != NULL)
142                 screen_lyrics_set(result);
143         else
144                 screen_status_message (_("No lyrics"));
146         lyrics_free(current.loader);
147         current.loader = NULL;
150 static void
151 screen_lyrics_load(struct mpd_song *song)
153         char buffer[MAX_SONGNAME_LENGTH];
155         assert(song != NULL);
157         screen_lyrics_abort();
158         screen_lyrics_clear();
160         current.song = song;
162         strfsong(buffer, sizeof(buffer), "%artist%", song);
163         current.artist = g_strdup(buffer);
165         strfsong(buffer, sizeof(buffer), "%title%", song);
166         current.title = g_strdup(buffer);
168         current.loader = lyrics_load(current.artist, current.title,
169                                      screen_lyrics_callback, NULL);
172 static void lyrics_paint(screen_t *screen, mpdclient_t *c);
174 static FILE *create_lyr_file(const char *artist, const char *title)
176         char path[1024];
178         snprintf(path, 1024, "%s/.lyrics",
179                  getenv("HOME"));
180         mkdir(path, S_IRWXU);
182         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
183                  getenv("HOME"), artist, title);
185         return fopen(path, "w");
188 static int store_lyr_hd(void)
190         FILE *lyr_file;
191         unsigned i;
193         lyr_file = create_lyr_file(current.artist, current.title);
194         if (lyr_file == NULL)
195                 return -1;
197         for (i = 0; i < current.lines->len; ++i)
198                 fprintf(lyr_file, "%s\n",
199                         (const char*)g_ptr_array_index(current.lines, i));
201         fclose(lyr_file);
202         return 0;
205 static const char *
206 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
208         if (idx >= current.lines->len)
209                 return NULL;
211         return g_ptr_array_index(current.lines, idx);
215 static void
216 lyrics_screen_init(WINDOW *w, int cols, int rows)
218         current.lines = g_ptr_array_new();
219         lw = list_window_init(w, cols, rows);
220         lw->flags = LW_HIDE_CURSOR;
223 static void
224 lyrics_resize(int cols, int rows)
226         lw->cols = cols;
227         lw->rows = rows;
230 static void
231 lyrics_exit(void)
233         list_window_free(lw);
235         screen_lyrics_abort();
236         screen_lyrics_clear();
238         g_ptr_array_free(current.lines, TRUE);
239         current.lines = NULL;
242 static void
243 lyrics_open(mpd_unused screen_t *screen, mpdclient_t *c)
245         if (c->song != NULL && c->song != current.song)
246                 screen_lyrics_load(c->song);
250 static const char *
251 lyrics_title(char *str, size_t size)
253         if (current.loader != NULL)
254                 return "Lyrics (loading)";
255         else if (current.artist != NULL && current.title != NULL &&
256                  current.lines->len > 0) {
257                 snprintf(str, size, "Lyrics: %s - %s",
258                          current.artist, current.title);
259                 return str;
260         } else
261                 return "Lyrics";
264 static void
265 lyrics_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
267         list_window_paint(lw, list_callback, NULL);
268         wrefresh(lw->w);
272 static void
273 lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
275         if( lw->repaint ) {
276                 list_window_paint(lw, list_callback, NULL);
277                 wrefresh(lw->w);
278                 lw->repaint = 0;
279         }
283 static int
284 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
286         if (list_window_scroll_cmd(lw, current.lines->len, cmd))
287                 return 1;
289         switch(cmd) {
290         case CMD_INTERRUPT:
291                 if (current.loader != NULL) {
292                         screen_lyrics_abort();
293                         screen_lyrics_clear();
294                 }
295                 return 1;
296         case CMD_ADD:
297                 if (current.loader == NULL && current.artist != NULL &&
298                     current.title != NULL && store_lyr_hd() == 0)
299                         screen_status_message (_("Lyrics saved!"));
300                 return 1;
301         case CMD_LYRICS_UPDATE:
302                 if (c->song != NULL) {
303                         screen_lyrics_load(c->song);
304                         lyrics_paint(NULL, NULL);
305                 }
306                 return 1;
307         default:
308                 break;
309         }
311         lw->selected = lw->start+lw->rows;
312         if (screen_find(screen,
313                         lw, current.lines->len,
314                         cmd, list_callback, NULL)) {
315                 /* center the row */
316                 list_window_center(lw, current.lines->len, lw->selected);
317                 return 1;
318         }
320         return 0;
323 const struct screen_functions screen_lyrics = {
324         .init = lyrics_screen_init,
325         .exit = lyrics_exit,
326         .open = lyrics_open,
327         .close = NULL,
328         .resize = lyrics_resize,
329         .paint = lyrics_paint,
330         .update = lyrics_update,
331         .cmd = lyrics_cmd,
332         .get_title = lyrics_title,
333 };
335 #endif /* ENABLE_LYRICS_SCREEN */