X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fstrfsong.c;h=5107ba0bd2606f9a8cc28fecf9159701b0b3988a;hb=f06ea973f90093fed3cf81954698c4139818064b;hp=ff1d6ad6b31931bea0cb9ea22e38ee47e00df099;hpb=b39c1ef247a01c538bf61d8b2bbc0730b9198a9a;p=ncmpc.git diff --git a/src/strfsong.c b/src/strfsong.c index ff1d6ad..5107ba0 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -1,24 +1,25 @@ /* ncmpc (Ncurses MPD Client) - * (c) 2004-2009 The Music Player Daemon Project + * (c) 2004-2010 The Music Player Daemon Project * Project homepage: http://musicpd.org - + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ + */ #include "strfsong.h" #include "charset.h" +#include "utils.h" #include @@ -48,6 +49,35 @@ skip(const gchar * p) return p; } +#ifndef NCMPC_MINI + +static char * +concat_tag_values(const char *a, const char *b) +{ + return g_strconcat(a, ", ", b, NULL); +} + +static char * +song_more_tag_values(const struct mpd_song *song, enum mpd_tag_type tag, + const char *first) +{ + const char *p = mpd_song_get_tag(song, tag, 1); + if (p == NULL) + return NULL; + + char *buffer = concat_tag_values(first, p); + for (unsigned i = 2; (p = mpd_song_get_tag(song, tag, i)) != NULL; + ++i) { + char *prev = buffer; + buffer = concat_tag_values(buffer, p); + g_free(prev); + } + + return buffer; +} + +#endif /* !NCMPC_MINI */ + static char * song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag) { @@ -55,7 +85,19 @@ song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag) if (value == NULL) return NULL; - return utf8_to_locale(value); +#ifndef NCMPC_MINI + char *all = song_more_tag_values(song, tag, value); + if (all != NULL) + value = all; +#endif /* !NCMPC_MINI */ + + char *result = utf8_to_locale(value); + +#ifndef NCMPC_MINI + g_free(all); +#endif /* !NCMPC_MINI */ + + return result; } static gsize @@ -65,22 +107,26 @@ _strfsong(gchar *s, const struct mpd_song *song, const gchar **last) { - const gchar *p, *end; - gchar *temp; - gsize n, length = 0; - gboolean found = FALSE; + bool found = false; + /* "missed" helps handling the case of mere literal text like + found==true instead of found==false. */ + bool missed = false; + + s[0] = '\0'; - memset(s, 0, max); if (song == NULL) return 0; + const char *p; + size_t length = 0; for (p = format; *p != '\0' && length0 ) { g_strlcat(s, temp, max); length = strlen(s); - found = TRUE; + found = true; + } else { + missed = true; } g_free(temp); continue; @@ -113,8 +162,8 @@ _strfsong(gchar *s, /* EXPRESSION END */ if (p[0] == ']') { if(last) *last = p+1; - if(!found && length) { - memset(s, 0, max); + if(missed && !found && length) { + s[0] = '\0'; length = 0; } return length; @@ -123,6 +172,7 @@ _strfsong(gchar *s, /* pass-through non-escaped portions of the format string */ if (p[0] != '#' && p[0] != '%' && length= 'a' && *end <= 'z') { end++; } - n = end - p + 1; + size_t n = end - p + 1; if(*end != '%') n--; else if (strncmp("%file%", p, n) == 0) temp = utf8_to_locale(mpd_song_get_uri(song)); - else if (strncmp("%artist%", p, n) == 0) + else if (strncmp("%artist%", p, n) == 0) { temp = song_tag_locale(song, MPD_TAG_ARTIST); - else if (strncmp("%title%", p, n) == 0) + if (temp == NULL) { + temp = song_tag_locale(song, MPD_TAG_PERFORMER); + if (temp == NULL) + temp = song_tag_locale(song, MPD_TAG_COMPOSER); + } + } else if (strncmp("%albumartist", p, n) == 0) + temp = song_tag_locale(song, MPD_TAG_ALBUM_ARTIST); + else if (strncmp("%composer%", p, n) == 0) + temp = song_tag_locale(song, MPD_TAG_COMPOSER); + else if (strncmp("%performer%", p, n) == 0) + temp = song_tag_locale(song, MPD_TAG_PERFORMER); + else if (strncmp("%title%", p, n) == 0) { temp = song_tag_locale(song, MPD_TAG_TITLE); - else if (strncmp("%album%", p, n) == 0) + if (temp == NULL) + temp = song_tag_locale(song, MPD_TAG_NAME); + } else if (strncmp("%album%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_ALBUM); else if (strncmp("%shortalbum%", p, n) == 0) { temp = song_tag_locale(song, MPD_TAG_ALBUM); @@ -184,32 +248,28 @@ _strfsong(gchar *s, unsigned duration = mpd_song_get_duration(song); if (duration > 0) { - if (duration > 3600) { - temp = g_strdup_printf("%d:%02d:%02d", - duration / 3600, - (duration % 3600) / 60, - duration % 60); - } else { - temp = g_strdup_printf("%d:%02d", - duration / 60, - duration % 60); - } + char buffer[32]; + format_duration_short(buffer, sizeof(buffer), + duration); + temp = g_strdup(buffer); } } if( temp == NULL) { gsize templen=n; /* just pass-through any unknown specifiers (including esc) */ - /* drop a null char in so printf stops at the end of this specifier, - but put the real character back in (pseudo-const) */ if( length+templen > max ) templen = max-length; - g_strlcat(s, p,max); + gchar *ident = g_strndup(p, templen); + g_strlcat(s, ident, max); length+=templen; + g_free(ident); + + missed = true; } else { gsize templen = strlen(temp); - found = TRUE; + found = true; if( length+templen > max ) templen = max-length; g_strlcat(s, temp, max);