diff --git a/src/strfsong.c b/src/strfsong.c
index fcaf4ada1673cbef53401b91f3527eb2186ea11c..3d5abd5797dfd78e50e9515f72763efabf5ee7cc 100644 (file)
--- a/src/strfsong.c
+++ b/src/strfsong.c
-/*
- * $Id$
- *
- * Based on mpc's songToFormatedString modified for glib and ncmpc
- *
- *
- * (c) 2003-2004 by normalperson and Warren Dukes (shank@mercury.chem.pitt.edu)
- * and Daniel Brown (danb@cs.utexas.edu)
- * and Kalle Wallin (kaw@linux.se)
+/* ncmpc (Ncurses MPD Client)
+ * (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
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * 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 "support.h"
#include "charset.h"
+#include "utils.h"
+
+#include <mpd/client.h>
#include <string.h>
gint stack = 0;
while (*p != '\0') {
- if(*p == '[') stack++;
- if(*p == '#' && p[1] != '\0') {
+ if (*p == '[')
+ stack++;
+ if (*p == '#' && p[1] != '\0') {
/* skip escaped stuff */
++p;
- } else if(stack) {
+ } else if (stack) {
if(*p == ']') stack--;
} else {
if(*p == '&' || *p == '|' || *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);
+ char *buffer, *prev;
+
+ if (p == NULL)
+ return NULL;
+
+ buffer = concat_tag_values(first, p);
+ for (unsigned i = 2; (p = mpd_song_get_tag(song, tag, i)) != NULL;
+ ++i) {
+ 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)
+{
+ const char *value = mpd_song_get_tag(song, tag, 0);
+ char *result;
+#ifndef NCMPC_MINI
+ char *all;
+#endif /* !NCMPC_MINI */
+
+ if (value == NULL)
+ return NULL;
+
+#ifndef NCMPC_MINI
+ all = song_more_tag_values(song, tag, value);
+ if (all != NULL)
+ value = all;
+#endif /* !NCMPC_MINI */
+
+ result = utf8_to_locale(value);
+
+#ifndef NCMPC_MINI
+ g_free(all);
+#endif /* !NCMPC_MINI */
+
+ return result;
+}
+
static gsize
_strfsong(gchar *s,
gsize max,
gboolean found = FALSE;
memset(s, 0, max);
- if( song==NULL )
+ if (song == NULL)
return 0;
for (p = format; *p != '\0' && length<max;) {
if(*end != '%')
n--;
else if (strncmp("%file%", p, n) == 0)
- temp = utf8_to_locale(song->file);
+ temp = utf8_to_locale(mpd_song_get_uri(song));
else if (strncmp("%artist%", p, n) == 0)
- temp = song->artist ? utf8_to_locale(song->artist) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_ARTIST);
else if (strncmp("%title%", p, n) == 0)
- temp = song->title ? utf8_to_locale(song->title) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_TITLE);
else if (strncmp("%album%", p, n) == 0)
- temp = song->album ? utf8_to_locale(song->album) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_ALBUM);
else if (strncmp("%shortalbum%", p, n) == 0) {
- temp = song->album ? utf8_to_locale(song->album) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_ALBUM);
if (temp) {
gchar *temp2 = g_strndup(temp, 25);
if (strlen(temp) > 25) {
}
}
else if (strncmp("%track%", p, n) == 0)
- temp = song->track ? utf8_to_locale(song->track) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_TRACK);
else if (strncmp("%name%", p, n) == 0)
- temp = song->name ? utf8_to_locale(song->name) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_NAME);
else if (strncmp("%date%", p, n) == 0)
- temp = song->date ? utf8_to_locale(song->date) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_DATE);
else if (strncmp("%genre%", p, n) == 0)
- temp = song->genre ? utf8_to_locale(song->genre) : NULL;
+ temp = song_tag_locale(song, MPD_TAG_GENRE);
else if (strncmp("%shortfile%", p, n) == 0) {
- if( strstr(song->file, "://") )
- temp = utf8_to_locale(song->file);
+ const char *uri = mpd_song_get_uri(song);
+ if (strstr(uri, "://") != NULL)
+ temp = utf8_to_locale(uri);
else
- temp = utf8_to_locale(basename(song->file));
+ temp = utf8_to_locale(g_basename(uri));
} else if (strncmp("%time%", p, n) == 0) {
- if (song->time != MPD_SONG_NO_TIME) {
- if (song->time > 3600) {
- temp = g_strdup_printf("%d:%02d:%02d",
- song->time / 3600,
- (song->time % 3600) / 60,
- song->time % 60);
- } else {
- temp = g_strdup_printf("%d:%02d",
- song->time / 60,
- song->time % 60);
- }
+ unsigned duration = mpd_song_get_duration(song);
+
+ if (duration > 0) {
+ char buffer[32];
+ format_duration_short(buffer, sizeof(buffer),
+ duration);
+ temp = g_strdup(buffer);
}
}
strfsong(gchar *s, gsize max, const gchar *format,
const struct mpd_song *song)
{
- return _strfsong(s, max, format, song, NULL);
+ return _strfsong(s, max, format, song, NULL);
}
-
+