Code

2d8af072fa9d4749988606f8fd11dd91779433da
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.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.
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.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "i18n.h"
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "charset.h"
25 static list_window_t *lw;
27 static struct {
28         struct mpd_song *song;
29         GPtrArray *lines;
30 } current;
32 static void
33 screen_song_clear(void)
34 {
35         for (guint i = 0; i < current.lines->len; ++i)
36                 g_free(g_ptr_array_index(current.lines, i));
38         g_ptr_array_set_size(current.lines, 0);
40         if (current.song != NULL) {
41                 mpd_freeSong(current.song);
42                 current.song = NULL;
43         }
44 }
46 static void
47 screen_song_paint(void);
49 /**
50  * Repaint and update the screen.
51  */
52 static void
53 screen_song_repaint(void)
54 {
55         screen_song_paint();
56         wrefresh(lw->w);
57 }
59 static const char *
60 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
61                           G_GNUC_UNUSED void *data)
62 {
63         static char buffer[256];
64         char *value;
66         if (idx >= current.lines->len)
67                 return NULL;
69         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
70         g_strlcpy(buffer, value, sizeof(buffer));
71         g_free(value);
73         return buffer;
74 }
77 static void
78 screen_song_init(WINDOW *w, int cols, int rows)
79 {
80         current.lines = g_ptr_array_new();
81         lw = list_window_init(w, cols, rows);
82         lw->hide_cursor = true;
83 }
85 static void
86 screen_song_exit(void)
87 {
88         list_window_free(lw);
90         screen_song_clear();
92         g_ptr_array_free(current.lines, TRUE);
93         current.lines = NULL;
94 }
96 static void
97 screen_song_resize(int cols, int rows)
98 {
99         lw->cols = cols;
100         lw->rows = rows;
103 static const char *
104 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
106         return _("Song viewer");
109 static void
110 screen_song_paint(void)
112         list_window_paint(lw, screen_song_list_callback, NULL);
115 static bool
116 screen_song_cmd(mpdclient_t *c, command_t cmd)
118         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
119                 screen_song_repaint();
120                 return true;
121         }
123         switch(cmd) {
124         case CMD_LOCATE:
125                 if (current.song != NULL) {
126                         screen_file_goto_song(c, current.song);
127                         return true;
128                 }
130                 return false;
132 #ifdef ENABLE_LYRICS_SCREEN
133         case CMD_SCREEN_LYRICS:
134                 if (current.song != NULL) {
135                         screen_lyrics_switch(c, current.song);
136                         return true;
137                 }
139                 return false;
140 #endif
142         default:
143                 break;
144         }
146         if (screen_find(lw, current.lines->len,
147                         cmd, screen_song_list_callback, NULL)) {
148                 /* center the row */
149                 list_window_center(lw, current.lines->len, lw->selected);
150                 screen_song_repaint();
151                 return true;
152         }
154         return false;
157 const struct screen_functions screen_song = {
158         .init = screen_song_init,
159         .exit = screen_song_exit,
160         .resize = screen_song_resize,
161         .paint = screen_song_paint,
162         .cmd = screen_song_cmd,
163         .get_title = screen_song_title,
164 };
166 static void
167 screen_song_append(const char *label, const char *value)
169         assert(label != NULL);
171         if (value != NULL)
172                 g_ptr_array_add(current.lines,
173                                 g_strdup_printf("%s: %s", label, value));
176 void
177 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
179         assert(song != NULL);
180         assert(song->file != NULL);
182         screen_song_clear();
184         current.song = mpd_songDup(song);
186         g_ptr_array_add(current.lines, g_strdup(song->file));
188         screen_song_append(_("Artist"), song->artist);
189         screen_song_append(_("Title"), song->title);
190         screen_song_append(_("Album"), song->album);
191         screen_song_append(_("Composer"), song->composer);
192         screen_song_append(_("Name"), song->name);
193         screen_song_append(_("Disc"), song->disc);
194         screen_song_append(_("Track"), song->track);
195         screen_song_append(_("Date"), song->date);
196         screen_song_append(_("Genre"), song->genre);
197         screen_song_append(_("Comment"), song->comment);
199         screen_switch(&screen_song, c);