Code

screen_lyrics, screen_song: duplicate "next_song"
[ncmpc.git] / src / screen_song.c
index d7e086928ccf34815bf360cc87edc37d99d7f3d7..a7d6f8ee4c087c4dbdae4b189c4c528465bd7985 100644 (file)
@@ -1,7 +1,7 @@
 /* ncmpc (Ncurses MPD Client)
  * (c) 2004-2009 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
 #include "screen.h"
 #include "screen_utils.h"
 #include "charset.h"
+#include "utils.h"
 
 #include <glib/gprintf.h>
 #include <string.h>
 
 static list_window_t *lw;
 
-static const struct mpd_song *next_song;
+static struct mpd_song *next_song;
 
 static struct {
        struct mpd_song *selected_song;
@@ -68,7 +69,7 @@ screen_song_repaint(void)
 
 static const char *
 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                         G_GNUC_UNUSED void *data)
+                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
 {
        static char buffer[256];
        char *value;
@@ -183,12 +184,13 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
 {
        unsigned i, max_label_width;
        enum label {
-               ARTIST, TITLE, ALBUM, COMPOSER, NAME, DISC, TRACK,
+               ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
                DATE, GENRE, COMMENT, PATH, BITRATE
        };
        const char *labels[] = { [ARTIST] = _("Artist"),
                [TITLE] = _("Title"),
                [ALBUM] = _("Album"),
+               [LENGTH] = _("Length"),
                [COMPOSER] = _("Composer"),
                [NAME] = _("Name"),
                [DISC] = _("Disc"),
@@ -211,6 +213,20 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
        screen_song_append(labels[ARTIST], song->artist, max_label_width);
        screen_song_append(labels[TITLE], song->title, max_label_width);
        screen_song_append(labels[ALBUM], song->album, max_label_width);
+       /* create time string and add it */
+       if (song->time != MPD_SONG_NO_TIME) {
+               char length[16];
+               /*write out the time, using hours if time over 60 minutes*/
+               if (song->time > 3600) {
+                       g_snprintf(length, sizeof(length),
+                                       "%i:%02i:%02i",
+                                       song->time/3600, (song->time%3600)/60, song->time%60);
+               } else {
+                       g_snprintf(length, sizeof(length),
+                                       "%i:%02i", song->time/60, song->time%60);
+               }
+               screen_song_append(labels[LENGTH], length, max_label_width);
+       }
        screen_song_append(labels[COMPOSER], song->composer, max_label_width);
        screen_song_append(labels[NAME], song->name, max_label_width);
        screen_song_append(labels[DISC], song->disc, max_label_width);
@@ -219,36 +235,94 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
        screen_song_append(labels[GENRE], song->genre, max_label_width);
        screen_song_append(labels[COMMENT], song->comment, max_label_width);
        screen_song_append(labels[PATH], song->file, max_label_width);
-       if (c->status != NULL && c->song != NULL && g_strcmp0(c->song->file, song->file) == 0) {
+       if (c->status != NULL && c->song != NULL &&
+                        g_strcmp0(c->song->file, song->file) == 0 &&
+                       (c->status->state == MPD_STATUS_STATE_PLAY ||
+                        c->status->state == MPD_STATUS_STATE_PAUSE) ) {
                char buf[16];
                g_snprintf(buf, sizeof(buf), _("%d kbps"), c->status->bitRate);
                screen_song_append(labels[BITRATE], buf, max_label_width);
        }
 }
 
+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] =_("Most recent db update"),
+               [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);
+
+               mpd_freeStats(mpd_stats);
+       }
+}
+
 static void
 screen_song_update(mpdclient_t *c)
 {
-       /* if any song changed */
-/*     if ((c->song != NULL &&
-                               (current.played_song == NULL ||
-                                g_strcmp0(c->song->file, current.played_song->file) != 0) ) ||
-                       next_song != NULL)
-       {*/
+       /* Clear all lines */
        for (guint i = 0; i < current.lines->len; ++i)
                g_free(g_ptr_array_index(current.lines, i));
        g_ptr_array_set_size(current.lines, 0);
 
-       /* if a song was selected before the song screen was opened */
+       /* If a song was selected before the song screen was opened */
        if (next_song != NULL) {
                assert(current.selected_song == NULL);
-               current.selected_song = mpd_songDup(next_song);
+               current.selected_song = next_song;
                next_song = NULL;
        }
 
        if (current.selected_song != NULL &&
                        (c->song == NULL ||
                         g_strcmp0(current.selected_song->file, c->song->file) != 0 ||
+                        c->status == NULL ||
                        (c->status->state != MPD_STATUS_STATE_PLAY &&
                         c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
                g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
@@ -256,7 +330,9 @@ screen_song_update(mpdclient_t *c)
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
-       if (c->song != NULL && (c->status->state == MPD_STATUS_STATE_PLAY || c->status->state == MPD_STATUS_STATE_PAUSE) ) {
+       if (c->song != NULL && c->status != NULL &&
+                       (c->status->state == MPD_STATUS_STATE_PLAY ||
+                        c->status->state == MPD_STATUS_STATE_PAUSE) ) {
                if (current.played_song != NULL) {
                        mpd_freeSong(current.played_song);
                }
@@ -266,8 +342,11 @@ 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();
-       //}
 }
 
 static bool
@@ -294,17 +373,25 @@ screen_song_cmd(mpdclient_t *c, command_t cmd)
 #ifdef ENABLE_LYRICS_SCREEN
        case CMD_SCREEN_LYRICS:
                if (current.selected_song != NULL) {
-                       screen_lyrics_switch(c, current.selected_song);
+                       screen_lyrics_switch(c, current.selected_song, false);
                        return true;
                }
                if (current.played_song != NULL) {
-                       screen_lyrics_switch(c, current.played_song);
+                       screen_lyrics_switch(c, current.played_song, true);
                        return true;
                }
-
                return false;
+
 #endif
 
+       case CMD_SCREEN_SWAP:
+               if (current.selected_song != NULL)
+                       screen_swap(c, current.selected_song);
+               else
+               // No need to check if this is null - we'd pass null anyway
+                       screen_swap(c, current.played_song);
+               return true;
+
        default:
                break;
        }
@@ -339,6 +426,6 @@ screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
        assert(current.selected_song == NULL);
        assert(current.played_song == NULL);
 
-       next_song = song;
+       next_song = mpd_songDup(song);
        screen_switch(&screen_song, c);
 }