Code

screen_song: include cleanup
[ncmpc.git] / src / screen_song.c
1 /*
2  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "i18n.h"
20 #include "screen.h"
21 #include "screen_utils.h"
22 #include "charset.h"
24 static list_window_t *lw;
26 struct {
27         struct mpd_song *song;
28         GPtrArray *lines;
29 } current;
31 static void
32 screen_song_clear(void)
33 {
34         for (guint i = 0; i < current.lines->len; ++i)
35                 g_free(g_ptr_array_index(current.lines, i));
37         g_ptr_array_set_size(current.lines, 0);
38 }
40 static void
41 screen_song_paint(void);
43 /**
44  * Repaint and update the screen.
45  */
46 static void
47 screen_song_repaint(void)
48 {
49         screen_song_paint();
50         wrefresh(lw->w);
51 }
53 static const char *
54 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
55                           G_GNUC_UNUSED void *data)
56 {
57         static char buffer[256];
58         char *value;
60         if (idx >= current.lines->len)
61                 return NULL;
63         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
64         g_strlcpy(buffer, value, sizeof(buffer));
65         g_free(value);
67         return buffer;
68 }
71 static void
72 screen_song_init(WINDOW *w, int cols, int rows)
73 {
74         current.lines = g_ptr_array_new();
75         lw = list_window_init(w, cols, rows);
76         lw->flags = LW_HIDE_CURSOR;
77 }
79 static void
80 screen_song_exit(void)
81 {
82         list_window_free(lw);
84         screen_song_clear();
86         g_ptr_array_free(current.lines, TRUE);
87         current.lines = NULL;
88 }
90 static void
91 screen_song_resize(int cols, int rows)
92 {
93         lw->cols = cols;
94         lw->rows = rows;
95 }
97 static const char *
98 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
99 {
100         return _("Song viewer");
103 static void
104 screen_song_paint(void)
106         list_window_paint(lw, screen_song_list_callback, NULL);
109 static bool
110 screen_song_cmd(mpdclient_t *c, command_t cmd)
112         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
113                 screen_song_repaint();
114                 return true;
115         }
117         switch(cmd) {
118         case CMD_LOCATE:
119                 if (current.song != NULL) {
120                         screen_file_goto_song(c, current.song);
121                         return true;
122                 }
124                 return false;
126         default:
127                 break;
128         }
130         if (screen_find(lw, current.lines->len,
131                         cmd, screen_song_list_callback, NULL)) {
132                 /* center the row */
133                 list_window_center(lw, current.lines->len, lw->selected);
134                 screen_song_repaint();
135                 return true;
136         }
138         return false;
141 const struct screen_functions screen_song = {
142         .init = screen_song_init,
143         .exit = screen_song_exit,
144         .resize = screen_song_resize,
145         .paint = screen_song_paint,
146         .cmd = screen_song_cmd,
147         .get_title = screen_song_title,
148 };
150 static void
151 screen_song_append(const char *label, const char *value)
153         assert(label != NULL);
155         if (value != NULL)
156                 g_ptr_array_add(current.lines,
157                                 g_strdup_printf("%s: %s", label, value));
160 void
161 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
163         assert(song != NULL);
164         assert(song->file != NULL);
166         screen_song_clear();
168         current.song = mpd_songDup(song);
170         g_ptr_array_add(current.lines, g_strdup(song->file));
172         screen_song_append(_("Artist"), song->artist);
173         screen_song_append(_("Title"), song->title);
174         screen_song_append(_("Album"), song->album);
175         screen_song_append(_("Composer"), song->composer);
176         screen_song_append(_("Name"), song->name);
177         screen_song_append(_("Disc"), song->disc);
178         screen_song_append(_("Track"), song->track);
179         screen_song_append(_("Date"), song->date);
180         screen_song_append(_("Genre"), song->genre);
181         screen_song_append(_("Comment"), song->comment);
183         screen_switch(&screen_song, c);