Code

Changed the way songs are added/deleted from playlist (#0000220).
authorKalle Wallin <kaw@linux.se>
Thu, 22 Apr 2004 20:34:52 +0000 (20:34 +0000)
committerKalle Wallin <kaw@linux.se>
Thu, 22 Apr 2004 20:34:52 +0000 (20:34 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@875 09075e82-0dd4-0310-85a5-a0d7c8717e4f

mpc.c
screen_file.c
screen_file.h
screen_play.c
screen_play.h

diff --git a/mpc.c b/mpc.c
index 00a044462d3d0b0cd9568759373154c284c35252..0a80af4dd1280f7c9b867153e8784ed93df16c06 100644 (file)
--- a/mpc.c
+++ b/mpc.c
 
 #define MAX_SONG_LENGTH 1024
 
-void 
-mpc_update_song(mpd_client_t *c)
-{
-  mpd_InfoEntity *entity;
-
-  if( mpc_error(c) )
-    return;
-
-  if( c->song )
-    {
-      mpd_freeSong(c->song);
-      c->song = NULL;
-    }
-
-  mpd_sendPlaylistInfoCommand(c->connection, c->status->song);
-  if( mpc_error(c) )
-    return;
-  while( (entity=mpd_getNextInfoEntity(c->connection)) )
-    {
-      mpd_Song *song = entity->info.song;
-
-      if(c->connection->error) 
-       {
-         fprintf(stderr,"error: %s\n",c->connection->errorStr);
-         exit(EXIT_FAILURE);
-       }
-      if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG) {
-       mpd_freeInfoEntity(entity);
-       fprintf(stderr,
-               "error: type != MPD_INFO_ENTITY_TYPE_SONG [%d]\n",
-               entity->type);
-       exit(EXIT_FAILURE);
-      }
-      c->song = mpd_songDup(song);
-      mpd_freeInfoEntity(entity);
-    }
-}
+#ifdef DEBUG
+#define D(x) x
+#else
+#define D(x)
+#endif
 
 int 
 mpc_close(mpd_client_t *c)
@@ -161,6 +129,9 @@ mpc_free_playlist(mpd_client_t *c)
   c->playlist=NULL;
   c->playlist_length=0;
 
+  c->song_id = -1;
+  c->song = NULL;
+
   return 0;
 }
 
@@ -169,7 +140,7 @@ mpc_update_playlist(mpd_client_t *c)
 {
   mpd_InfoEntity *entity;
 
-  //  fprintf(stderr, "mpc_update_playlist(): status->playlist = %d\n",  c->status->playlist);
+  D(fprintf(stderr, "mpc_update_playlist() [%d]\n",  c->status->playlist));
 
   if( mpc_error(c) )
     return -1;
@@ -195,6 +166,9 @@ mpc_update_playlist(mpd_client_t *c)
   c->playlist_id = c->status->playlist;
   c->playlist_updated = 1;
   c->song_id = -1;
+  c->song = NULL;
+
+  mpc_filelist_set_selected(c);
 
   return 0;
 }
@@ -222,6 +196,7 @@ mpc_playlist_get_song(mpd_client_t *c, int n)
   return (mpd_Song *) g_list_nth_data(c->playlist, n);
 }
 
+
 char *
 mpc_get_song_name(mpd_Song *song)
 {
@@ -270,7 +245,7 @@ mpc_update(mpd_client_t *c)
   if( c->playlist_id!=c->status->playlist )
     mpc_update_playlist(c);
   
-  if( c->status->song != c->song_id )
+  if( !c->song || c->status->song != c->song_id )
     {
       c->song = mpc_playlist_get_song(c, c->status->song);
       c->song_id = c->status->song;
index e57fc317f25d03478f9ff4f643c7d3e13440a1c0..d2b1f78449e10abd70ed3b54f0ca2a1ff261b210 100644 (file)
@@ -29,6 +29,7 @@
 #include "command.h"
 #include "screen.h"
 #include "screen_utils.h"
+#include "screen_play.h"
 #include "screen_file.h"
 
 #define BUFSIZE 1024
@@ -298,8 +299,7 @@ handle_select(screen_t *screen, mpd_client_t *c)
        {
          mpd_Song *song = entry->entity->info.song;
 
-         mpd_sendAddCommand(c->connection, song->file);
-         mpd_finishCommand(c->connection);
+         playlist_add_song(c, song);
 
          screen_status_printf("Adding \'%s\' to playlist\n", 
                               mpc_get_song_name(song));
@@ -307,19 +307,17 @@ handle_select(screen_t *screen, mpd_client_t *c)
     }
   else
     {
+      /* remove song from playlist */
       if( entry->entity->type==MPD_INFO_ENTITY_TYPE_SONG )
        {
-         int i;
          mpd_Song *song = entry->entity->info.song;
-         
-         i = mpc_playlist_get_song_index(c, song->file);
-         if( i>=0 )
-           {
-             mpd_sendDeleteCommand(c->connection, i);
-             mpd_finishCommand(c->connection);
-             screen_status_printf("Removed \'%s\' from playlist\n", 
-                                  mpc_get_song_name(song));
 
+         if( song )
+           {
+             int index = mpc_playlist_get_song_index(c, song->file);
+             
+             while( (index=mpc_playlist_get_song_index(c, song->file))>=0 )
+               playlist_delete_song(c, index);
            }
        }
     }
@@ -458,7 +456,7 @@ file_clear_highlights(mpd_client_t *c)
 }
 
 void
-file_clear_highlight(mpd_client_t *c, mpd_Song *song)
+file_set_highlight(mpd_client_t *c, mpd_Song *song, int highlight)
 {
   GList *list = g_list_first(c->filelist);
 
@@ -476,7 +474,7 @@ file_clear_highlight(mpd_client_t *c, mpd_Song *song)
 
          if( strcmp(song->file, song2->file) == 0 )
            {
-             entry->selected = 0;
+             entry->selected = highlight;
            }
        }
       list = list->next;
index 1a48fc7066f809caacbf80d4bfee481b742cf9ca..a7199a62e0c2fbeb53cc8e937fcba9f9fb44cd20 100644 (file)
@@ -1,5 +1,5 @@
 
-void file_clear_highlight(mpd_client_t *c, mpd_Song *song);
+void file_set_highlight(mpd_client_t *c, mpd_Song *song, int hightlight);
 void file_clear_highlights(mpd_client_t *c);
 
 list_window_t *get_filelist_window(void);
index eb1048951a5081f744bdae0d1427af99313238b5..cfdfa410af65aa525dfcbaababab74a007434835 100644 (file)
@@ -34,6 +34,8 @@
 
 #define BUFSIZE 256
 
+#define ENABLE_FANCY_PLAYLIST_MANAGMENT
+
 static list_window_t *lw = NULL;
 
 static char *
@@ -126,7 +128,6 @@ handle_save_playlist(screen_t *screen, mpd_client_t *c)
   return 0;
 }
 
-
 static void
 play_init(WINDOW *w, int cols, int rows)
 {
@@ -157,7 +158,7 @@ static void
 play_paint(screen_t *screen, mpd_client_t *c)
 { 
   lw->clear = 1;
-  
+
   list_window_paint(lw, list_callback, (void *) c);
   wnoutrefresh(lw->w);
 }
@@ -197,24 +198,10 @@ play_update(screen_t *screen, mpd_client_t *c)
 static int
 play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
 {
-  mpd_Song *song;
-
   switch(cmd)
     {
     case CMD_DELETE:
-      song = mpc_playlist_get_song(c, lw->selected);
-      if( song )
-       {
-         file_clear_highlight(c, song);
-         mpd_sendDeleteCommand(c->connection, lw->selected);
-         mpd_finishCommand(c->connection);
-         if( !mpc_error(c) )
-           {
-             screen_status_printf("Removed \'%s\' from playlist!",
-                                  mpc_get_song_name(song));
-
-           }
-       }
+      playlist_delete_song(c, lw->selected);
       return 1;
     case CMD_SAVE_PLAYLIST:
       handle_save_playlist(screen, c);
@@ -249,6 +236,83 @@ play_get_selected(void)
   return lw->selected;
 }
 
+int
+playlist_add_song(mpd_client_t *c, mpd_Song *song)
+{
+  if( !song || !song->file )
+    return -1;
+
+  /* send the add command to mpd */
+  mpd_sendAddCommand(c->connection, song->file);
+  mpd_finishCommand(c->connection);
+  if( mpc_error(c) )
+    return -1;
+
+#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT
+  /* add the song to playlist */
+  c->playlist = g_list_append(c->playlist, (gpointer) mpd_songDup(song));
+  c->playlist_length++;
+
+  /* increment the playlist id, so we dont retrives a new playlist */
+  c->playlist_id++;
+
+  /* make shure the playlist is repainted */
+  lw->clear = 1;
+  lw->repaint = 1;
+#endif
+
+  /* set selected highlight in the browse screen */
+  file_set_highlight(c, song, 1);
+
+  return 0;
+}
+
+int
+playlist_delete_song(mpd_client_t *c, int index)
+{
+  mpd_Song *song = mpc_playlist_get_song(c, index);
+
+  if( !song )
+    return -1;
+
+  /* send the delete command to mpd */
+  mpd_sendDeleteCommand(c->connection, index);
+  mpd_finishCommand(c->connection); 
+  if( mpc_error(c) )
+    return -1;
+
+  /* print a status message */
+  screen_status_printf("Removed \'%s\' from playlist!",
+                      mpc_get_song_name(song));
+  /* clear selected highlight in the browse screen */
+  file_set_highlight(c, song, 0);
+
+#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT 
+  /* increment the playlist id, so we dont retrives a new playlist */
+  c->playlist_id++;
+
+  /* remove references to the song */
+  if( c->song == song )
+    {
+      c->song = NULL;
+      c->song_id = -1;
+    }
+  
+  /* remove the song from the playlist */
+  c->playlist = g_list_remove(c->playlist, (gpointer) song);
+  c->playlist_length = g_list_length(c->playlist);
+  mpd_freeSong(song);
+
+  /* make shure the playlist is repainted */
+  lw->clear = 1;
+  lw->repaint = 1;
+  list_window_check_selected(lw, c->playlist_length);
+#endif
+
+  return 0;
+}
+
+
 screen_functions_t *
 get_screen_playlist(void)
 {
index f7c10185639b087399a9041560a0b994b8648267..535fc1887bca84c51237356cdfca746a69fdefb8 100644 (file)
@@ -1,5 +1,8 @@
 
 int play_get_selected(void);
 
+int playlist_add_song(mpd_client_t *c, mpd_Song *song);
+int playlist_delete_song(mpd_client_t *c, int index);
+
 screen_functions_t *get_screen_playlist(void);