Code

po: update Galician translation
[ncmpc.git] / src / screen_queue.c
index 93b3cfe5498089967fee682ae998e5d3d6b675a7..67df4b53655b7f3603cf174ae48c1a88c724b266 100644 (file)
@@ -1,5 +1,5 @@
 /* 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
@@ -134,22 +134,19 @@ screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
 }
 
 static void
-center_playing_item(struct mpdclient *c, bool center_cursor)
+center_playing_item(const struct mpd_status *status, bool center_cursor)
 {
-       unsigned length = c->playlist.list->len;
        int idx;
 
-       /* try to center the song that are playing */
-       idx = playlist_get_index(&c->playlist, c->song);
-       if (idx < 0)
+       if (status == NULL ||
+           (mpd_status_get_state(status) != MPD_STATE_PLAY &&
+            mpd_status_get_state(status) != MPD_STATE_PAUSE))
                return;
 
-       if (length < lw->rows)
-       {
-               if (center_cursor)
-                       list_window_set_cursor(lw, idx);
+       /* try to center the song that are playing */
+       idx = mpd_status_get_song_pos(status);
+       if (idx < 0)
                return;
-       }
 
        list_window_center(lw, idx);
 
@@ -162,6 +159,32 @@ center_playing_item(struct mpdclient *c, bool center_cursor)
        list_window_fetch_cursor(lw);
 }
 
+G_GNUC_PURE
+static int
+get_current_song_id(const struct mpd_status *status)
+{
+       return status != NULL &&
+               (mpd_status_get_state(status) == MPD_STATE_PLAY ||
+                mpd_status_get_state(status) == MPD_STATE_PAUSE)
+               ? (int)mpd_status_get_song_id(status)
+               : -1;
+}
+
+static bool
+screen_queue_song_change(const struct mpd_status *status)
+{
+       if (get_current_song_id(status) == current_song_id)
+               return false;
+
+       current_song_id = get_current_song_id(status);
+
+       /* center the cursor */
+       if (options.auto_center && !lw->range_selection)
+               center_playing_item(status, false);
+
+       return true;
+}
+
 #ifndef NCMPC_MINI
 static void
 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
@@ -353,8 +376,8 @@ static int
 handle_add_to_playlist(struct mpdclient *c)
 {
        gchar *path;
-#ifndef NCMPC_MINI
        GCompletion *gcmp;
+#ifndef NCMPC_MINI
        GList *list = NULL;
        GList *dir_list = NULL;
        completion_callback_data_t data;
@@ -368,18 +391,15 @@ handle_add_to_playlist(struct mpdclient *c)
        wrln_completion_callback_data = &data;
        wrln_pre_completion_callback = add_pre_completion_cb;
        wrln_post_completion_callback = add_post_completion_cb;
+#else
+       gcmp = NULL;
 #endif
 
        /* get path */
        path = screen_readln(_("Add"),
                             NULL,
                             NULL,
-#ifdef NCMPC_MINI
-                            NULL
-#else
-                            gcmp
-#endif
-                            );
+                            gcmp);
 
        /* destroy completion data */
 #ifndef NCMPC_MINI
@@ -449,6 +469,7 @@ screen_queue_open(struct mpdclient *c)
        }
 
        screen_queue_restore_selection();
+       screen_queue_song_change(c->status);
 }
 
 static void
@@ -524,37 +545,22 @@ screen_queue_paint(void)
        list_window_paint2(lw, screen_queue_paint_callback, NULL);
 }
 
-G_GNUC_PURE
-static int
-get_current_song_id(const struct mpd_status *status)
-{
-       return status != NULL &&
-               (mpd_status_get_state(status) == MPD_STATE_PLAY ||
-                mpd_status_get_state(status) == MPD_STATE_PAUSE)
-               ? (int)mpd_status_get_song_id(status)
-               : -1;
-}
-
 static void
 screen_queue_update(struct mpdclient *c)
 {
-       if (c->events & MPD_IDLE_PLAYLIST)
+       if (c->events & MPD_IDLE_QUEUE)
                screen_queue_restore_selection();
+       else
+               /* the queue size may have changed, even if we havn't
+                  revceived the QUEUE idle event yet */
+               list_window_set_length(lw, playlist_length(playlist));
 
-       if ((c->events & MPD_IDLE_PLAYER) != 0 &&
-           get_current_song_id(c->status) != current_song_id) {
-               current_song_id = get_current_song_id(c->status);
-
-               /* center the cursor */
-               if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
-                       center_playing_item(c, false);
-
+       if (((c->events & MPD_IDLE_PLAYER) != 0 &&
+            screen_queue_song_change(c->status)) ||
+           c->events & MPD_IDLE_QUEUE)
+               /* the queue or the current song has changed, we must
+                  paint the new version */
                screen_queue_repaint();
-       } else if (c->events & MPD_IDLE_PLAYLIST) {
-               /* the playlist has changed, we must paint the new
-                  version */
-               screen_queue_repaint();
-       }
 }
 
 #ifdef HAVE_GETMOUSE
@@ -635,7 +641,7 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        switch(cmd) {
        case CMD_SCREEN_UPDATE:
-               center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
+               center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
                screen_queue_repaint();
                return false;
        case CMD_SELECT_PLAYING:
@@ -732,7 +738,7 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        case CMD_SHUFFLE:
                list_window_get_range(lw, &range);
-               if (range.end < range.start + 1)
+               if (range.end <= range.start + 1)
                        /* No range selection, shuffle all list. */
                        break;