From 9cd22a2d5703c1f8159e0614a3c1c4dd086a1158 Mon Sep 17 00:00:00 2001 From: Kalle Wallin Date: Thu, 22 Apr 2004 20:34:52 +0000 Subject: [PATCH] Changed the way songs are added/deleted from playlist (#0000220). git-svn-id: https://svn.musicpd.org/ncmpc/trunk@875 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- mpc.c | 53 ++++++++-------------------- screen_file.c | 24 ++++++------- screen_file.h | 2 +- screen_play.c | 98 ++++++++++++++++++++++++++++++++++++++++++--------- screen_play.h | 3 ++ 5 files changed, 110 insertions(+), 70 deletions(-) diff --git a/mpc.c b/mpc.c index 00a0444..0a80af4 100644 --- a/mpc.c +++ b/mpc.c @@ -13,43 +13,11 @@ #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; diff --git a/screen_file.c b/screen_file.c index e57fc31..d2b1f78 100644 --- a/screen_file.c +++ b/screen_file.c @@ -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; diff --git a/screen_file.h b/screen_file.h index 1a48fc7..a7199a6 100644 --- a/screen_file.h +++ b/screen_file.h @@ -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); diff --git a/screen_play.c b/screen_play.c index eb10489..cfdfa41 100644 --- a/screen_play.c +++ b/screen_play.c @@ -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) { diff --git a/screen_play.h b/screen_play.h index f7c1018..535fc18 100644 --- a/screen_play.h +++ b/screen_play.h @@ -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); -- 2.30.2