Code

strfsong: constant pointers
[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 <sys/stat.h>
21 #include "ncmpc.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "strfsong.h"
28 #include "lyrics.h"
29 #include "gcc.h"
31 #define _GNU_SOURCE
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <ncurses.h>
36 #include <unistd.h>
37 #include <stdio.h>
39 static list_window_t *lw = NULL;
41 static struct {
42         const struct mpd_song *song;
44         char *artist, *title;
46         struct lyrics_loader *loader;
48         GPtrArray *lines;
49 } current;
51 static void
52 screen_lyrics_abort(void)
53 {
54         if (current.loader != NULL) {
55                 lyrics_free(current.loader);
56                 current.loader = NULL;
57         }
59         if (current.artist != NULL) {
60                 g_free(current.artist);
61                 current.artist = NULL;
62         }
64         if (current.title != NULL) {
65                 g_free(current.title);
66                 current.artist = NULL;
67         }
69         current.song = NULL;
70 }
72 static void
73 screen_lyrics_clear(void)
74 {
75         guint i;
77         for (i = 0; i < current.lines->len; ++i)
78                 g_free(g_ptr_array_index(current.lines, i));
80         g_ptr_array_set_size(current.lines, 0);
81 }
83 static void
84 lyrics_paint(mpdclient_t *c);
86 /**
87  * Repaint and update the screen.
88  */
89 static void
90 lyrics_repaint(void)
91 {
92         lyrics_paint(NULL);
93         wrefresh(lw->w);
94 }
96 /**
97  * Repaint and update the screen, if it is currently active.
98  */
99 static void
100 lyrics_repaint_if_active(void)
102         if (screen_is_visible(&screen_lyrics)) {
103                 lyrics_repaint();
105                 /* XXX repaint the screen title */
106         }
109 static void
110 screen_lyrics_set(const GString *str)
112         const char *p, *eol, *next;
114         screen_lyrics_clear();
116         p = str->str;
117         while ((eol = strchr(p, '\n')) != NULL) {
118                 char *line;
120                 next = eol + 1;
122                 /* strip whitespace at end */
124                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
125                         --eol;
127                 /* create copy and append it to current.lines*/
129                 line = g_malloc(eol - p + 1);
130                 memcpy(line, p, eol - p);
131                 line[eol - p] = 0;
133                 g_ptr_array_add(current.lines, line);
135                 /* reset control characters */
137                 for (eol = line + (eol - p); line < eol; ++line)
138                         if ((unsigned char)*line < 0x20)
139                                 *line = ' ';
141                 p = next;
142         }
144         if (*p != 0)
145                 g_ptr_array_add(current.lines, g_strdup(p));
147         /* paint new data */
149         lyrics_repaint_if_active();
152 static void
153 screen_lyrics_callback(const GString *result, mpd_unused void *data)
155         assert(current.loader != NULL);
157         if (result != NULL)
158                 screen_lyrics_set(result);
159         else
160                 screen_status_message (_("No lyrics"));
162         lyrics_free(current.loader);
163         current.loader = NULL;
166 static void
167 screen_lyrics_load(const struct mpd_song *song)
169         char buffer[MAX_SONGNAME_LENGTH];
171         assert(song != NULL);
173         screen_lyrics_abort();
174         screen_lyrics_clear();
176         current.song = song;
178         strfsong(buffer, sizeof(buffer), "%artist%", song);
179         current.artist = g_strdup(buffer);
181         strfsong(buffer, sizeof(buffer), "%title%", song);
182         current.title = g_strdup(buffer);
184         current.loader = lyrics_load(current.artist, current.title,
185                                      screen_lyrics_callback, NULL);
188 static FILE *create_lyr_file(const char *artist, const char *title)
190         char path[1024];
192         snprintf(path, 1024, "%s/.lyrics",
193                  getenv("HOME"));
194         mkdir(path, S_IRWXU);
196         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
197                  getenv("HOME"), artist, title);
199         return fopen(path, "w");
202 static int store_lyr_hd(void)
204         FILE *lyr_file;
205         unsigned i;
207         lyr_file = create_lyr_file(current.artist, current.title);
208         if (lyr_file == NULL)
209                 return -1;
211         for (i = 0; i < current.lines->len; ++i)
212                 fprintf(lyr_file, "%s\n",
213                         (const char*)g_ptr_array_index(current.lines, i));
215         fclose(lyr_file);
216         return 0;
219 static const char *
220 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
222         if (idx >= current.lines->len)
223                 return NULL;
225         return g_ptr_array_index(current.lines, idx);
229 static void
230 lyrics_screen_init(WINDOW *w, int cols, int rows)
232         current.lines = g_ptr_array_new();
233         lw = list_window_init(w, cols, rows);
234         lw->flags = LW_HIDE_CURSOR;
237 static void
238 lyrics_resize(int cols, int rows)
240         lw->cols = cols;
241         lw->rows = rows;
244 static void
245 lyrics_exit(void)
247         list_window_free(lw);
249         screen_lyrics_abort();
250         screen_lyrics_clear();
252         g_ptr_array_free(current.lines, TRUE);
253         current.lines = NULL;
256 static void
257 lyrics_open(mpd_unused screen_t *screen, mpdclient_t *c)
259         if (c->song != NULL && c->song != current.song)
260                 screen_lyrics_load(c->song);
264 static const char *
265 lyrics_title(char *str, size_t size)
267         if (current.loader != NULL)
268                 return "Lyrics (loading)";
269         else if (current.artist != NULL && current.title != NULL &&
270                  current.lines->len > 0) {
271                 snprintf(str, size, "Lyrics: %s - %s",
272                          current.artist, current.title);
273                 return str;
274         } else
275                 return "Lyrics";
278 static void
279 lyrics_paint(mpd_unused mpdclient_t *c)
281         list_window_paint(lw, list_callback, NULL);
284 static int
285 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
287         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
288                 lyrics_repaint();
289                 return 1;
290         }
292         switch(cmd) {
293         case CMD_INTERRUPT:
294                 if (current.loader != NULL) {
295                         screen_lyrics_abort();
296                         screen_lyrics_clear();
297                 }
298                 return 1;
299         case CMD_ADD:
300                 if (current.loader == NULL && current.artist != NULL &&
301                     current.title != NULL && store_lyr_hd() == 0)
302                         screen_status_message (_("Lyrics saved!"));
303                 return 1;
304         case CMD_LYRICS_UPDATE:
305                 if (c->song != NULL) {
306                         screen_lyrics_load(c->song);
307                         lyrics_repaint();
308                 }
309                 return 1;
310         default:
311                 break;
312         }
314         lw->selected = lw->start+lw->rows;
315         if (screen_find(screen,
316                         lw, current.lines->len,
317                         cmd, list_callback, NULL)) {
318                 /* center the row */
319                 list_window_center(lw, current.lines->len, lw->selected);
320                 lyrics_repaint();
321                 return 1;
322         }
324         return 0;
327 const struct screen_functions screen_lyrics = {
328         .init = lyrics_screen_init,
329         .exit = lyrics_exit,
330         .open = lyrics_open,
331         .close = NULL,
332         .resize = lyrics_resize,
333         .paint = lyrics_paint,
334         .cmd = lyrics_cmd,
335         .get_title = lyrics_title,
336 };