From 53950b19fc05087cc90aef36ddc391d4c28eb169 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 9 Jul 2015 09:14:08 +0200 Subject: rework the notification string handling --- mpd-notification.c | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'mpd-notification.c') diff --git a/mpd-notification.c b/mpd-notification.c index e79bc68..862832a 100644 --- a/mpd-notification.c +++ b/mpd-notification.c @@ -171,9 +171,25 @@ found: return icon; } +/*** append_string ***/ +char * append_string(char * string, const char * format, const char * s) { + char * tmp; + + tmp = g_markup_escape_text(s, -1); + + string = realloc(string, strlen(string) + strlen(format) + strlen(tmp)); + + sprintf(string + strlen(string), format, tmp); + + free(tmp); + + return string; +} + /*** main ***/ int main(int argc, char ** argv) { - char * album = NULL, * artist = NULL, * icon = NULL, * notifystr = NULL, * title = NULL; + const char * title = NULL, * artist = NULL, * album = NULL; + char * icon = NULL, * notifystr = NULL; GError * error = NULL; unsigned short int errcount = 0, state = MPD_STATE_UNKNOWN; const char * mpd_host = MPD_HOST, * music_dir = NULL, * uri = NULL; @@ -296,6 +312,23 @@ int main(int argc, char ** argv) { song = mpd_recv_song(conn); + title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); + + /* ignore if we have no title */ + if (title == NULL) + continue; + + /* initial allocation and string termination */ + notifystr = strdup(""); + + notifystr = append_string(notifystr, TEXT_PLAY_TITLE, title); + + if ((artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)) != NULL) + notifystr = append_string(notifystr, TEXT_PLAY_ARTIST, artist); + + if ((album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)) != NULL) + notifystr = append_string(notifystr, TEXT_PLAY_ALBUM, album); + uri = mpd_song_get_uri(song); if (music_dir != NULL && uri != NULL) @@ -304,20 +337,6 @@ int main(int argc, char ** argv) { if (verbose > 0 && icon != NULL) printf("%s: found icon: %s\n", program, icon); - if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL) - title = strdup(TEXT_UNKNOWN); - if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL) - artist = strdup(TEXT_UNKNOWN); - if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL) - album = strdup(TEXT_UNKNOWN); - - notifystr = malloc(sizeof(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album)); - sprintf(notifystr, TEXT_PLAY, title, artist, album); - - free(title); - free(artist); - free(album); - mpd_song_free(song); } else if (state == MPD_STATE_PAUSE) notifystr = TEXT_PAUSE; @@ -356,8 +375,10 @@ int main(int argc, char ** argv) { } errcount = 0; - if (state == MPD_STATE_PLAY) + if (notifystr != NULL) { free(notifystr); + notifystr = NULL; + } if (icon != NULL) { free(icon); icon = NULL; -- cgit v1.2.3-54-g00ecf