From ae34932c79709af67026243896dafb53609262ff Mon Sep 17 00:00:00 2001 From: Patrick Hallen Date: Fri, 13 Mar 2009 21:40:37 +0100 Subject: [PATCH] screen_song: Added some statistics about mpd to the song screen. --- src/screen_song.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils.c | 54 +++++++++++++++++++++++++++++++++++++++++ src/utils.h | 3 +++ 3 files changed, 119 insertions(+) diff --git a/src/screen_song.c b/src/screen_song.c index d7e0869..3af2d8b 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -21,6 +21,7 @@ #include "screen.h" #include "screen_utils.h" #include "charset.h" +#include "utils.h" #include #include @@ -226,6 +227,63 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c) } } +static void +screen_song_add_stats(const mpdclient_t *c) +{ + unsigned i, max_label_width; + char buf[64]; + char *duration; + GDate *date; + enum label { + ARTISTS, ALBUMS, SONGS, UPTIME, + DBUPTIME, PLAYTIME, DBPLAYTIME + }; + const char *labels[] = { [ARTISTS] = _("Number of artists"), + [ALBUMS] = _("Number of albums"), + [SONGS] = _("Number of songs"), + [UPTIME] = _("Uptime"), + [DBUPTIME] =_("DB last updated"), + [PLAYTIME] = _("Playtime"), + [DBPLAYTIME] = _("DB playtime") + }; + mpd_Stats *mpd_stats = NULL; + if (c->connection != NULL) { + mpd_sendStatsCommand(c->connection); + mpd_stats = mpd_getStats(c->connection); + } + + if (mpd_stats != NULL) { + /* Determine the width of the longest label */ + max_label_width = utf8_width(labels[0]); + for (i = 1; i < G_N_ELEMENTS(labels); ++i) { + if (utf8_width(labels[i]) > max_label_width) + max_label_width = utf8_width(labels[i]); + } + + g_ptr_array_add(current.lines, g_strdup(_("MPD Statistics")) ); + g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists); + screen_song_append(labels[ARTISTS], buf, max_label_width); + g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums); + screen_song_append(labels[ALBUMS], buf, max_label_width); + g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs); + screen_song_append(labels[SONGS], buf, max_label_width); + duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime); + screen_song_append(labels[DBPLAYTIME], duration, max_label_width); + g_free(duration); + duration = time_seconds_to_durationstr(mpd_stats->playTime); + screen_song_append(labels[PLAYTIME], duration, max_label_width); + g_free(duration); + duration = time_seconds_to_durationstr(mpd_stats->uptime); + screen_song_append(labels[UPTIME], duration, max_label_width); + g_free(duration); + date = g_date_new(); + g_date_set_time_t(date, mpd_stats->dbUpdateTime); + g_date_strftime(buf, sizeof(buf), "%x", date); + screen_song_append(labels[DBUPTIME], buf, max_label_width); + g_date_free(date); + } +} + static void screen_song_update(mpdclient_t *c) { @@ -266,6 +324,10 @@ screen_song_update(mpdclient_t *c) g_ptr_array_add(current.lines, g_strdup("\0")); } + /* Add some statistics about mpd */ + if (c->connection != NULL) + screen_song_add_stats(c); + screen_song_repaint(); //} } diff --git a/src/utils.c b/src/utils.c index cd12509..32707eb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,6 +20,7 @@ #include "utils.h" #include "options.h" #include "charset.h" +#include "i18n.h" #include #include @@ -113,3 +114,56 @@ gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types) filelist_free(filelist); return list; } + +char * +time_seconds_to_durationstr(unsigned long time_seconds) +{ + const char *year = _("year"); + const char *years = _("years"); + const char *week = _("week"); + const char *weeks = _("weeks"); + const char *day = _("day"); + const char *days = _("days"); + char *duration; + char *iter; + unsigned bytes_written = 0; + unsigned length = utf8_width(years) + + utf8_width(weeks) + utf8_width(days) + 32; + + duration = g_malloc(length); + iter = duration; + if (time_seconds / 31536000 > 0) { + if (time_seconds % 31536000 == 1) + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, year); + else + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, years); + time_seconds %= 31536000; + length -= bytes_written; + iter += bytes_written; + } + if (time_seconds / 604800 > 0) { + if (time_seconds % 604800 == 1) + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, week); + else + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, weeks); + time_seconds %= 604800; + length -= bytes_written; + iter += bytes_written; + } + if (time_seconds / 86400 > 0) { + if (time_seconds % 86400 == 1) + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, day); + else + bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, days); + time_seconds %= 86400; + length -= bytes_written; + iter += bytes_written; + } + g_snprintf(iter, length, "%02lu:%02lu:%02lu", + time_seconds / 3600, + time_seconds % 3600 / 60, + time_seconds % 3600 % 60); + return duration; +} + + diff --git a/src/utils.h b/src/utils.h index a75ef8d..20077f0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -41,4 +41,7 @@ GList *gcmp_list_from_path(mpdclient_t *c, GList *list, gint types); +char * +time_seconds_to_durationstr(unsigned long time_seconds); + #endif -- 2.30.2