Code

screen_lyrics: removed the "update" method
[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 void
86 lyrics_paint(mpdclient_t *c);
88 /**
89  * Repaint and update the screen.
90  */
91 static void
92 lyrics_repaint(void)
93 {
94         lyrics_paint(NULL);
95         wrefresh(lw->w);
96 }
98 /**
99  * Repaint and update the screen, if it is currently active.
100  */
101 static void
102 lyrics_repaint_if_active(void)
104         if (get_cur_mode_id() == 104) { /* XXX don't use the literal number */
105                 lyrics_repaint();
107                 /* XXX repaint the screen title */
108         }
111 static void
112 screen_lyrics_set(const GString *str)
114         const char *p, *eol, *next;
116         screen_lyrics_clear();
118         p = str->str;
119         while ((eol = strchr(p, '\n')) != NULL) {
120                 char *line;
122                 next = eol + 1;
124                 /* strip whitespace at end */
126                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
127                         --eol;
129                 /* create copy and append it to current.lines*/
131                 line = g_malloc(eol - p + 1);
132                 memcpy(line, p, eol - p);
133                 line[eol - p] = 0;
135                 g_ptr_array_add(current.lines, line);
137                 /* reset control characters */
139                 for (eol = line + (eol - p); line < eol; ++line)
140                         if ((unsigned char)*line < 0x20)
141                                 *line = ' ';
143                 p = next;
144         }
146         if (*p != 0)
147                 g_ptr_array_add(current.lines, g_strdup(p));
149         /* paint new data */
151         lyrics_repaint_if_active();
154 static void
155 screen_lyrics_callback(const GString *result, mpd_unused void *data)
157         assert(current.loader != NULL);
159         if (result != NULL)
160                 screen_lyrics_set(result);
161         else
162                 screen_status_message (_("No lyrics"));
164         lyrics_free(current.loader);
165         current.loader = NULL;
168 static void
169 screen_lyrics_load(struct mpd_song *song)
171         char buffer[MAX_SONGNAME_LENGTH];
173         assert(song != NULL);
175         screen_lyrics_abort();
176         screen_lyrics_clear();
178         current.song = song;
180         strfsong(buffer, sizeof(buffer), "%artist%", song);
181         current.artist = g_strdup(buffer);
183         strfsong(buffer, sizeof(buffer), "%title%", song);
184         current.title = g_strdup(buffer);
186         current.loader = lyrics_load(current.artist, current.title,
187                                      screen_lyrics_callback, NULL);
190 static FILE *create_lyr_file(const char *artist, const char *title)
192         char path[1024];
194         snprintf(path, 1024, "%s/.lyrics",
195                  getenv("HOME"));
196         mkdir(path, S_IRWXU);
198         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
199                  getenv("HOME"), artist, title);
201         return fopen(path, "w");
204 static int store_lyr_hd(void)
206         FILE *lyr_file;
207         unsigned i;
209         lyr_file = create_lyr_file(current.artist, current.title);
210         if (lyr_file == NULL)
211                 return -1;
213         for (i = 0; i < current.lines->len; ++i)
214                 fprintf(lyr_file, "%s\n",
215                         (const char*)g_ptr_array_index(current.lines, i));
217         fclose(lyr_file);
218         return 0;
221 static const char *
222 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
224         if (idx >= current.lines->len)
225                 return NULL;
227         return g_ptr_array_index(current.lines, idx);
231 static void
232 lyrics_screen_init(WINDOW *w, int cols, int rows)
234         current.lines = g_ptr_array_new();
235         lw = list_window_init(w, cols, rows);
236         lw->flags = LW_HIDE_CURSOR;
239 static void
240 lyrics_resize(int cols, int rows)
242         lw->cols = cols;
243         lw->rows = rows;
246 static void
247 lyrics_exit(void)
249         list_window_free(lw);
251         screen_lyrics_abort();
252         screen_lyrics_clear();
254         g_ptr_array_free(current.lines, TRUE);
255         current.lines = NULL;
258 static void
259 lyrics_open(mpd_unused screen_t *screen, mpdclient_t *c)
261         if (c->song != NULL && c->song != current.song)
262                 screen_lyrics_load(c->song);
266 static const char *
267 lyrics_title(char *str, size_t size)
269         if (current.loader != NULL)
270                 return "Lyrics (loading)";
271         else if (current.artist != NULL && current.title != NULL &&
272                  current.lines->len > 0) {
273                 snprintf(str, size, "Lyrics: %s - %s",
274                          current.artist, current.title);
275                 return str;
276         } else
277                 return "Lyrics";
280 static void
281 lyrics_paint(mpd_unused mpdclient_t *c)
283         list_window_paint(lw, list_callback, NULL);
286 static int
287 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
289         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
290                 lyrics_repaint();
291                 return 1;
292         }
294         switch(cmd) {
295         case CMD_INTERRUPT:
296                 if (current.loader != NULL) {
297                         screen_lyrics_abort();
298                         screen_lyrics_clear();
299                 }
300                 return 1;
301         case CMD_ADD:
302                 if (current.loader == NULL && current.artist != NULL &&
303                     current.title != NULL && store_lyr_hd() == 0)
304                         screen_status_message (_("Lyrics saved!"));
305                 return 1;
306         case CMD_LYRICS_UPDATE:
307                 if (c->song != NULL) {
308                         screen_lyrics_load(c->song);
309                         lyrics_repaint();
310                 }
311                 return 1;
312         default:
313                 break;
314         }
316         lw->selected = lw->start+lw->rows;
317         if (screen_find(screen,
318                         lw, current.lines->len,
319                         cmd, list_callback, NULL)) {
320                 /* center the row */
321                 list_window_center(lw, current.lines->len, lw->selected);
322                 lyrics_repaint();
323                 return 1;
324         }
326         return 0;
329 const struct screen_functions screen_lyrics = {
330         .init = lyrics_screen_init,
331         .exit = lyrics_exit,
332         .open = lyrics_open,
333         .close = NULL,
334         .resize = lyrics_resize,
335         .paint = lyrics_paint,
336         .cmd = lyrics_cmd,
337         .get_title = lyrics_title,
338 };
340 #endif /* ENABLE_LYRICS_SCREEN */