Code

screen_lyrics.c: wrapped some duplicate code
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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 "screen_song.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_lyrics.h"
24 #include "screen_find.h"
25 #include "i18n.h"
26 #include "screen.h"
27 #include "charset.h"
28 #include "utils.h"
29 #include "mpdclient.h"
31 #include <mpd/client.h>
33 #include <glib/gprintf.h>
34 #include <assert.h>
35 #include <string.h>
37 enum {
38         LABEL_LENGTH = MPD_TAG_COUNT,
39         LABEL_BITRATE,
40 };
42 static const char *const tag_labels[] = {
43         [MPD_TAG_ARTIST] = N_("Artist"),
44         [MPD_TAG_TITLE] = N_("Title"),
45         [MPD_TAG_ALBUM] = N_("Album"),
46         [LABEL_LENGTH] = N_("Length"),
47         [MPD_TAG_COMPOSER] = N_("Composer"),
48         [MPD_TAG_NAME] = N_("Name"),
49         [MPD_TAG_DISC] = N_("Disc"),
50         [MPD_TAG_TRACK] = N_("Track"),
51         [MPD_TAG_DATE] = N_("Date"),
52         [MPD_TAG_GENRE] = N_("Genre"),
53         [MPD_TAG_COMMENT] = N_("Comment"),
54         [LABEL_BITRATE] = N_("Bitrate"),
55 };
57 static unsigned max_tag_label_width;
59 enum stats_label {
60         STATS_ARTISTS,
61         STATS_ALBUMS,
62         STATS_SONGS,
63         STATS_UPTIME,
64         STATS_DBUPTIME,
65         STATS_PLAYTIME,
66         STATS_DBPLAYTIME,
67 };
69 static const char *const stats_labels[] = {
70         [STATS_ARTISTS] = N_("Number of artists"),
71         [STATS_ALBUMS] = N_("Number of albums"),
72         [STATS_SONGS] = N_("Number of songs"),
73         [STATS_UPTIME] = N_("Uptime"),
74         [STATS_DBUPTIME] = N_("Most recent db update"),
75         [STATS_PLAYTIME] = N_("Playtime"),
76         [STATS_DBPLAYTIME] = N_("DB playtime"),
77 };
79 static unsigned max_stats_label_width;
81 static struct list_window *lw;
83 static struct mpd_song *next_song;
85 static struct {
86         struct mpd_song *selected_song;
87         struct mpd_song *played_song;
88         GPtrArray *lines;
89 } current;
91 static void
92 screen_song_clear(void)
93 {
94         for (guint i = 0; i < current.lines->len; ++i)
95                 g_free(g_ptr_array_index(current.lines, i));
97         g_ptr_array_set_size(current.lines, 0);
99         if (current.selected_song != NULL) {
100                 mpd_song_free(current.selected_song);
101                 current.selected_song = NULL;
102         }
103         if (current.played_song != NULL) {
104                 mpd_song_free(current.played_song);
105                 current.played_song = NULL;
106         }
109 static void
110 screen_song_paint(void);
112 /**
113  * Repaint and update the screen.
114  */
115 static void
116 screen_song_repaint(void)
118         screen_song_paint();
119         wrefresh(lw->w);
122 static const char *
123 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
125         assert(idx < current.lines->len);
127         return g_ptr_array_index(current.lines, idx);
131 static void
132 screen_song_init(WINDOW *w, int cols, int rows)
134         for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
135                 if (tag_labels[i] != NULL) {
136                         unsigned width = utf8_width(_(tag_labels[i]));
138                         if (width > max_tag_label_width)
139                                 max_tag_label_width = width;
140                 }
141         }
143         for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
144                 if (stats_labels[i] != NULL) {
145                         unsigned width = utf8_width(_(stats_labels[i]));
147                         if (width > max_stats_label_width)
148                                 max_stats_label_width = width;
149                 }
150         }
152         /* We will need at least 10 lines, so this saves 10 reallocations :) */
153         current.lines = g_ptr_array_sized_new(10);
154         lw = list_window_init(w, cols, rows);
155         lw->hide_cursor = true;
158 static void
159 screen_song_exit(void)
161         list_window_free(lw);
163         screen_song_clear();
165         g_ptr_array_free(current.lines, TRUE);
166         current.lines = NULL;
169 static void
170 screen_song_resize(int cols, int rows)
172         list_window_resize(lw, cols, rows);
175 static const char *
176 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
178         return _("Song viewer");
181 static void
182 screen_song_paint(void)
184         list_window_paint(lw, screen_song_list_callback, NULL);
187 /* Appends a line with a fixed width for the label column
188  * Handles NULL strings gracefully */
189 static void
190 screen_song_append(const char *label, const char *value, unsigned label_col)
192         unsigned label_width = locale_width(label) + 2;
193         int value_col, label_size;
194         gchar *entry, *entry_iter;
195         const gchar *value_iter;
196         char *p, *q;
197         unsigned width;
199         assert(label != NULL);
200         assert(value != NULL);
201         assert(g_utf8_validate(value, -1, NULL));
203         /* +2 for ': ' */
204         label_col += 2;
205         value_col = lw->cols - label_col;
206         /* calculate the number of required linebreaks */
207         value_iter = value;
208         label_size = strlen(label) + label_col;
210         while (*value_iter != 0) {
211                 entry = g_malloc(label_size);
212                 if (value_iter == value) {
213                         entry_iter = entry + g_sprintf(entry, "%s: ", label);
214                         /* fill the label column with whitespaces */
215                         memset(entry_iter, ' ', label_col - label_width);
216                         entry_iter += label_col - label_width;
217                 }
218                 else {
219                         /* fill the label column with whitespaces */
220                         memset(entry, ' ', label_col);
221                         entry_iter = entry + label_col;
222                 }
223                 /* skip whitespaces */
224                 while (g_ascii_isspace(*value_iter)) ++value_iter;
226                 p = g_strdup(value_iter);
227                 width = utf8_cut_width(p, value_col);
228                 if (width == 0)
229                         /* not enough room for anything - bail out */
230                         break;
232                 *entry_iter = 0;
234                 value_iter += strlen(p);
235                 p = replace_utf8_to_locale(p);
236                 q = g_strconcat(entry, p, NULL);
237                 g_free(entry);
238                 g_free(p);
240                 g_ptr_array_add(current.lines, q);
241         }
244 static void
245 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
247         const char *label = _(tag_labels[tag]);
248         unsigned i = 0;
249         const char *value;
251         assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
252         assert(label != NULL);
254         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
255                 screen_song_append(label, value, max_tag_label_width);
258 static void
259 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
261         assert(song != NULL);
263         screen_song_append_tag(song, MPD_TAG_ARTIST);
264         screen_song_append_tag(song, MPD_TAG_TITLE);
265         screen_song_append_tag(song, MPD_TAG_ALBUM);
267         /* create time string and add it */
268         if (mpd_song_get_duration(song) > 0) {
269                 char length[16];
270                 format_duration_short(length, sizeof(length),
271                                       mpd_song_get_duration(song));
272                 screen_song_append(_(tag_labels[LABEL_LENGTH]), length,
273                                    max_tag_label_width);
274         }
276         screen_song_append_tag(song, MPD_TAG_COMPOSER);
277         screen_song_append_tag(song, MPD_TAG_NAME);
278         screen_song_append_tag(song, MPD_TAG_DISC);
279         screen_song_append_tag(song, MPD_TAG_TRACK);
280         screen_song_append_tag(song, MPD_TAG_DATE);
281         screen_song_append_tag(song, MPD_TAG_GENRE);
282         screen_song_append_tag(song, MPD_TAG_COMMENT);
284         screen_song_append(_("Path"), mpd_song_get_uri(song),
285                            max_tag_label_width);
286         if (mpdclient_is_playing(c) && c->song != NULL &&
287             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
288                 char buf[16];
289                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
290                            mpd_status_get_kbit_rate(c->status));
291                 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
292                                    max_tag_label_width);
293         }
296 static void
297 screen_song_append_stats(enum stats_label label, const char *value)
299         screen_song_append(_(stats_labels[label]), value,
300                            max_stats_label_width);
303 static bool
304 screen_song_add_stats(struct mpd_connection *connection)
306         char buf[64];
307         GDate *date;
308         struct mpd_stats *mpd_stats;
310         mpd_stats = mpd_run_stats(connection);
311         if (mpd_stats == NULL)
312                 return false;
314         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
315         g_snprintf(buf, sizeof(buf), "%d",
316                    mpd_stats_get_number_of_artists(mpd_stats));
317         screen_song_append_stats(STATS_ARTISTS, buf);
318         g_snprintf(buf, sizeof(buf), "%d",
319                    mpd_stats_get_number_of_albums(mpd_stats));
320         screen_song_append_stats(STATS_ALBUMS, buf);
321         g_snprintf(buf, sizeof(buf), "%d",
322                    mpd_stats_get_number_of_songs(mpd_stats));
323         screen_song_append_stats(STATS_SONGS, buf);
325         format_duration_long(buf, sizeof(buf),
326                              mpd_stats_get_db_play_time(mpd_stats));
327         screen_song_append_stats(STATS_DBPLAYTIME, buf);
329         format_duration_long(buf, sizeof(buf),
330                              mpd_stats_get_play_time(mpd_stats));
331         screen_song_append_stats(STATS_PLAYTIME, buf);
333         format_duration_long(buf, sizeof(buf),
334                              mpd_stats_get_uptime(mpd_stats));
335         screen_song_append_stats(STATS_UPTIME, buf);
337         date = g_date_new();
338         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
339         g_date_strftime(buf, sizeof(buf), "%x", date);
340         screen_song_append_stats(STATS_DBUPTIME, buf);
341         g_date_free(date);
343         mpd_stats_free(mpd_stats);
344         return true;
347 static void
348 screen_song_update(struct mpdclient *c)
350         struct mpd_connection *connection;
352         /* Clear all lines */
353         for (guint i = 0; i < current.lines->len; ++i)
354                 g_free(g_ptr_array_index(current.lines, i));
355         g_ptr_array_set_size(current.lines, 0);
357         /* If a song was selected before the song screen was opened */
358         if (next_song != NULL) {
359                 assert(current.selected_song == NULL);
360                 current.selected_song = next_song;
361                 next_song = NULL;
362         }
364         if (current.selected_song != NULL &&
365                         (c->song == NULL ||
366                          strcmp(mpd_song_get_uri(current.selected_song),
367                                 mpd_song_get_uri(c->song)) != 0 ||
368                          !mpdclient_is_playing(c))) {
369                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
370                 screen_song_add_song(current.selected_song, c);
371                 g_ptr_array_add(current.lines, g_strdup("\0"));
372         }
374         if (c->song != NULL && mpdclient_is_playing(c)) {
375                 if (current.played_song != NULL) {
376                         mpd_song_free(current.played_song);
377                 }
378                 current.played_song = mpd_song_dup(c->song);
379                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
380                 screen_song_add_song(current.played_song, c);
381                 g_ptr_array_add(current.lines, g_strdup("\0"));
382         }
384         /* Add some statistics about mpd */
385         connection = mpdclient_get_connection(c);
386         if (connection != NULL && !screen_song_add_stats(connection))
387                 mpdclient_handle_error(c);
389         list_window_set_length(lw, current.lines->len);
390         screen_song_repaint();
393 static bool
394 screen_song_cmd(struct mpdclient *c, command_t cmd)
396         if (list_window_scroll_cmd(lw, cmd)) {
397                 screen_song_repaint();
398                 return true;
399         }
401         switch(cmd) {
402         case CMD_LOCATE:
403                 if (current.selected_song != NULL) {
404                         screen_file_goto_song(c, current.selected_song);
405                         return true;
406                 }
407                 if (current.played_song != NULL) {
408                         screen_file_goto_song(c, current.played_song);
409                         return true;
410                 }
412                 return false;
414 #ifdef ENABLE_LYRICS_SCREEN
415         case CMD_SCREEN_LYRICS:
416                 if (current.selected_song != NULL) {
417                         screen_lyrics_switch(c, current.selected_song, false);
418                         return true;
419                 }
420                 if (current.played_song != NULL) {
421                         screen_lyrics_switch(c, current.played_song, true);
422                         return true;
423                 }
424                 return false;
426 #endif
428         case CMD_SCREEN_SWAP:
429                 if (current.selected_song != NULL)
430                         screen_swap(c, current.selected_song);
431                 else
432                 // No need to check if this is null - we'd pass null anyway
433                         screen_swap(c, current.played_song);
434                 return true;
436         default:
437                 break;
438         }
440         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
441                 /* center the row */
442                 list_window_center(lw, lw->selected);
443                 screen_song_repaint();
444                 return true;
445         }
447         return false;
450 const struct screen_functions screen_song = {
451         .init = screen_song_init,
452         .exit = screen_song_exit,
453         .open = screen_song_update,
454         .close = screen_song_clear,
455         .resize = screen_song_resize,
456         .paint = screen_song_paint,
457         .update = screen_song_update,
458         .cmd = screen_song_cmd,
459         .get_title = screen_song_title,
460 };
462 void
463 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
465         assert(song != NULL);
466         assert(current.selected_song == NULL);
467         assert(current.played_song == NULL);
469         next_song = mpd_song_dup(song);
470         screen_switch(&screen_song, c);