Code

Adding crop feature in ncmpc
[ncmpc.git] / src / mpdclient.c
index c570051657443aaaa6b65138a9e450c37c8892a8..6d9d72b9c206789e6694400783397f8b13553ba1 100644 (file)
@@ -163,21 +163,22 @@ mpdclient_new(void)
        mpdclient_t *c;
 
        c = g_malloc0(sizeof(mpdclient_t));
-       c->playlist.list = g_array_sized_new(FALSE, FALSE, sizeof(struct mpd_song *), 1024);
+       playlist_init(&c->playlist);
 
        return c;
 }
 
-mpdclient_t *
+void
 mpdclient_free(mpdclient_t *c)
 {
        mpdclient_disconnect(c);
+
+       mpdclient_playlist_free(&c->playlist);
+
        g_list_free(c->error_callbacks);
        g_list_free(c->playlist_callbacks);
        g_list_free(c->browse_callbacks);
        g_free(c);
-
-       return NULL;
 }
 
 gint
@@ -191,8 +192,7 @@ mpdclient_disconnect(mpdclient_t *c)
                mpd_freeStatus(c->status);
        c->status = NULL;
 
-       if (c->playlist.list)
-               mpdclient_playlist_free(&c->playlist);
+       playlist_clear(&c->playlist);
 
        if (c->song)
                c->song = NULL;
@@ -253,7 +253,7 @@ mpdclient_update(mpdclient_t *c)
 
        /* check if the playlist needs an update */
        if (c->playlist.id != c->status->playlist) {
-               if (c->playlist.list)
+               if (playlist_is_empty(&c->playlist))
                        retval = mpdclient_playlist_update_changes(c);
                else
                        retval = mpdclient_playlist_update(c);
@@ -299,6 +299,41 @@ mpdclient_cmd_pause(mpdclient_t *c, gint value)
        return mpdclient_finish_command(c);
 }
 
+gint
+mpdclient_cmd_crop(mpdclient_t *c)
+{
+       mpd_Status *status;
+       int length;
+
+       mpd_sendStatusCommand(c->connection);
+       status = mpd_getStatus(c->connection);
+       length = status->playlistLength - 1;
+
+       if (length <= 0) {
+               mpd_freeStatus(status);
+               screen_status_message("You have to have a playlist longer than 1 song in length to crop");
+       } else if (status->state == 3 || status->state == 2) {
+               /* If playing or paused */
+
+               mpd_sendCommandListBegin( c->connection );
+
+               while (length >= 0) {
+                       if (length != status->song)
+                               mpd_sendDeleteCommand(c->connection, length);
+
+                       length--;
+               }
+
+               mpd_sendCommandListEnd(c->connection);
+               mpd_freeStatus(status);
+       } else {
+               mpd_freeStatus(status);
+               screen_status_message("You need to be playing to crop the playlist\n");
+       }
+
+       return mpdclient_finish_command(c);
+}
+
 gint
 mpdclient_cmd_stop(mpdclient_t *c)
 {
@@ -419,8 +454,7 @@ mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
 
 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
        /* add the song to playlist */
-       c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song));
-       c->playlist.length++;
+       playlist_append(&c->playlist, song);
 
        /* increment the playlist id, so we dont retrives a new playlist */
        c->playlist.id++;
@@ -438,11 +472,13 @@ gint
 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
 {
        gint retval = 0;
-       struct mpd_song *song = playlist_get_song(c, idx);
+       struct mpd_song *song;
 
-       if( !song )
+       if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
                return -1;
 
+       song = playlist_get(&c->playlist, idx);
+
        /* send the delete command to mpd */
 #ifdef ENABLE_SONG_ID
        D("Delete id:%d\n", song->id);
@@ -458,7 +494,7 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx)
        c->playlist.id++;
 
        /* remove the song from the playlist */
-       g_array_remove_index(c->playlist.list, idx);
+       playlist_remove_reuse(&c->playlist, idx);
 
        /* call playlist updated callback */
        mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
@@ -469,7 +505,6 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx)
                c->need_update = TRUE;
        }
 
-       /* free song */
        mpd_freeSong(song);
 
 #else
@@ -489,8 +524,8 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
            (guint)new_index >= c->playlist.list->len)
                return -1;
 
-       song1 = playlist_get_song(c, old_index);
-       song2 = playlist_get_song(c, new_index);
+       song1 = playlist_get(&c->playlist, old_index);
+       song2 = playlist_get(&c->playlist, new_index);
 
        /* send the move command to mpd */
 #ifdef ENABLE_SONG_ID
@@ -504,13 +539,8 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
                return n;
 
 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
-       /* update the songs position field */
-       n = song1->pos;
-       song1->pos = song2->pos;
-       song2->pos = n;
-       /* update the array */
-       g_array_index(c->playlist.list, struct mpd_song *, old_index) = song2;
-       g_array_index(c->playlist.list, struct mpd_song *, new_index) = song1;
+       /* update the playlist */
+       playlist_swap(&c->playlist, old_index, new_index);
 
        /* increment the playlist id, so we dont retrives a new playlist */
        c->playlist.id++;
@@ -647,20 +677,6 @@ mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
 /*** Playlist managment functions *******************************************/
 /****************************************************************************/
 
-gint
-mpdclient_playlist_free(mpdclient_playlist_t *playlist)
-{
-       guint i;
-
-       for (i = 0; i < playlist->list->len; ++i) {
-               struct mpd_song *song = g_array_index(playlist->list, struct mpd_song *, i);
-               mpd_freeSong(song);
-       }
-
-       g_array_free(playlist->list, TRUE);
-       memset(playlist, 0, sizeof(mpdclient_playlist_t));
-       return 0;
-}
 
 /* update playlist */
 gint
@@ -673,21 +689,18 @@ mpdclient_playlist_update(mpdclient_t *c)
        if (MPD_ERROR(c))
                return -1;
 
-       if (c->playlist.list)
-               mpdclient_playlist_free(&c->playlist);
+       playlist_clear(&c->playlist);
 
        mpd_sendPlaylistInfoCommand(c->connection,-1);
        while ((entity = mpd_getNextInfoEntity(c->connection))) {
-               if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
-                       struct mpd_song *song = mpd_songDup(entity->info.song);
-                       g_array_append_val(c->playlist.list, song);
-               }
+               if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
+                       playlist_append(&c->playlist, entity->info.song);
+
                mpd_freeInfoEntity(entity);
        }
 
        c->playlist.id = c->status->playlist;
        c->song = NULL;
-       c->playlist.updated = TRUE;
 
        /* call playlist updated callbacks */
        mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
@@ -712,20 +725,17 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
        mpd_sendPlChangesCommand(c->connection, c->playlist.id);
 
        while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
-               struct mpd_song *song = mpd_songDup(entity->info.song);
+               struct mpd_song *song = entity->info.song;
 
                if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
                        /* update song */
                        D("updating pos:%d, id=%d - %s\n",
                          song->pos, song->id, song->file);
-                       mpd_freeSong(g_array_index(c->playlist.list,
-                                                  struct mpd_song *, song->pos));
-                       g_array_index(c->playlist.list,
-                                     struct mpd_song *, song->pos) = song;
+                       playlist_replace(&c->playlist, song->pos, song);
                } else {
                        /* add a new song */
                        D("adding song at pos %d\n", song->pos);
-                       g_array_append_val(c->playlist.list, song);
+                       playlist_append(&c->playlist, song);
                }
 
                mpd_freeInfoEntity(entity);
@@ -734,17 +744,14 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
        /* remove trailing songs */
        while ((guint)c->status->playlistLength < c->playlist.list->len) {
                guint pos = c->playlist.list->len - 1;
-               struct mpd_song *song = g_array_index(c->playlist.list, struct mpd_song *, pos);
 
                /* Remove the last playlist entry */
                D("removing song at pos %d\n", pos);
-               mpd_freeSong(song);
-               g_array_remove_index(c->playlist.list, pos);
+               playlist_remove(&c->playlist, pos);
        }
 
        c->song = NULL;
        c->playlist.id = c->status->playlist;
-       c->playlist.updated = TRUE;
 
        mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
 
@@ -759,189 +766,75 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
 }
 #endif
 
-struct mpd_song *
-playlist_get_song(mpdclient_t *c, gint idx)
-{
-       if (idx < 0 || (guint)idx >= c->playlist.list->len)
-               return NULL;
-
-       return g_array_index(c->playlist.list, struct mpd_song *, idx);
-}
-
-struct mpd_song *
-playlist_lookup_song(mpdclient_t *c, gint id)
-{
-       guint i;
-
-       for (i = 0; i < c->playlist.list->len; ++i) {
-               struct mpd_song *song = g_array_index(c->playlist.list,
-                                                     struct mpd_song *, i);
-               if (song->id == id)
-                       return song;
-       }
-
-       return NULL;
-}
-
-gint
-playlist_get_index(mpdclient_t *c, struct mpd_song *song)
-{
-       guint i;
-
-       for (i = 0; i < c->playlist.list->len; ++i) {
-               if (g_array_index(c->playlist.list, struct mpd_song *, i)
-                   == song)
-                       return (gint)i;
-       }
-
-       return -1;
-}
-
-gint
-playlist_get_index_from_id(mpdclient_t *c, gint id)
-{
-       guint i;
-
-       for (i = 0; i < c->playlist.list->len; ++i) {
-               struct mpd_song *song = g_array_index(c->playlist.list,
-                                                     struct mpd_song *, i);
-               if (song->id == id)
-                       return (gint)i;
-       }
-
-       return -1;
-}
-
-gint
-playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
-{
-       guint i;
-
-       for (i = 0; i < c->playlist.list->len; ++i) {
-               struct mpd_song *song = g_array_index(c->playlist.list,
-                                                     struct mpd_song *, i);
-               if(strcmp(song->file, filename) == 0)
-                       return (gint)i;
-       }
-
-       return -1;
-}
-
 
 /****************************************************************************/
 /*** Filelist functions *****************************************************/
 /****************************************************************************/
 
 mpdclient_filelist_t *
-mpdclient_filelist_free(mpdclient_filelist_t *filelist)
+mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
 {
-  GList *list = g_list_first(filelist->list);
+       mpdclient_filelist_t *filelist;
+       mpd_InfoEntity *entity;
+       gchar *path_utf8 = locale_to_utf8(path);
+       gboolean has_dirs_only = TRUE;
 
-  D("mpdclient_filelist_free()\n");
-  if( list == NULL )
-    return NULL;
-  while( list!=NULL )
-    {
-      filelist_entry_t *entry = list->data;
+       D("mpdclient_filelist_get(%s)\n", path);
+       mpd_sendLsInfoCommand(c->connection, path_utf8);
+       filelist = filelist_new(path);
+       if (path && path[0] && strcmp(path, "/"))
+               /* add a dummy entry for ./.. */
+               filelist_append(filelist, NULL);
 
-      if( entry->entity )
-       mpd_freeInfoEntity(entry->entity);
-      g_free(entry);
-      list=list->next;
-    }
-  g_list_free(filelist->list);
-  g_free(filelist->path);
-  filelist->path = NULL;
-  filelist->list = NULL;
-  filelist->length = 0;
-  g_free(filelist);
+       while ((entity=mpd_getNextInfoEntity(c->connection))) {
+               filelist_append(filelist, entity);
 
-  return NULL;
-}
+               if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
+                       has_dirs_only = FALSE;
+               }
+       }
 
+       /* If there's an error, ignore it.  We'll return an empty filelist. */
+       mpdclient_finish_command(c);
 
-mpdclient_filelist_t *
-mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
-{
-  mpdclient_filelist_t *filelist;
-  mpd_InfoEntity *entity;
-  gchar *path_utf8 = locale_to_utf8(path);
-  gboolean has_dirs_only = TRUE;
+       g_free(path_utf8);
+       filelist->updated = TRUE;
 
-  D("mpdclient_filelist_get(%s)\n", path);
-  mpd_sendLsInfoCommand(c->connection, path_utf8);
-  filelist = g_malloc0(sizeof(mpdclient_filelist_t));
-  if( path && path[0] && strcmp(path, "/") )
-    {
-      /* add a dummy entry for ./.. */
-      filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
-      entry->entity = NULL;
-      filelist->list = g_list_append(filelist->list, (gpointer) entry);
-      filelist->length++;
-    }
-
-  while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
-    {
-      filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
-      
-      entry->entity = entity;
-      filelist->list = g_list_append(filelist->list, (gpointer) entry);
-      filelist->length++;
-
-      if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
-       {
-         has_dirs_only = FALSE;
+       // If there are only directory entities in the filelist, we sort it
+       if (has_dirs_only) {
+               D("mpdclient_filelist_get: only dirs; sorting!\n");
+               filelist_sort(filelist, compare_filelistentry_dir);
        }
-    }
-  
-   /* If there's an error, ignore it.  We'll return an empty filelist. */
-   mpdclient_finish_command(c);
-  
-  g_free(path_utf8);
-  filelist->path = g_strdup(path);
-  filelist->updated = TRUE;
 
-  // If there are only directory entities in the filelist, we sort it
-  if (has_dirs_only)
-    {
-      D("mpdclient_filelist_get: only dirs; sorting!\n");
-      filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir);
-    }
-
-  return filelist;
+       return filelist;
 }
 
 mpdclient_filelist_t *
-mpdclient_filelist_search_utf8(mpdclient_t *c, 
+mpdclient_filelist_search_utf8(mpdclient_t *c,
                               int exact_match,
-                              int table, 
+                              int table,
                               gchar *filter_utf8)
 {
-  mpdclient_filelist_t *filelist;
-  mpd_InfoEntity *entity;
+       mpdclient_filelist_t *filelist;
+       mpd_InfoEntity *entity;
 
-  D("mpdclient_filelist_search(%s)\n", filter_utf8);
-  if( exact_match )
-    mpd_sendFindCommand(c->connection, table, filter_utf8);
-  else
-    mpd_sendSearchCommand(c->connection, table, filter_utf8);
-  filelist = g_malloc0(sizeof(mpdclient_filelist_t));
+       D("mpdclient_filelist_search(%s)\n", filter_utf8);
+       if (exact_match)
+               mpd_sendFindCommand(c->connection, table, filter_utf8);
+       else
+               mpd_sendSearchCommand(c->connection, table, filter_utf8);
+       filelist = filelist_new(NULL);
 
-  while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
-    {
-      filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
-      
-      entry->entity = entity;
-      filelist->list = g_list_append(filelist->list, (gpointer) entry);
-      filelist->length++;
-    }
-  
-  if( mpdclient_finish_command(c) )
-    return mpdclient_filelist_free(filelist);
+       while ((entity=mpd_getNextInfoEntity(c->connection)))
+               filelist_append(filelist, entity);
 
-  filelist->updated = TRUE;
+       if (mpdclient_finish_command(c)) {
+               filelist_free(filelist);
+               return NULL;
+       }
 
-  return filelist;
+       filelist->updated = TRUE;
+       return filelist;
 }
 
 
@@ -969,7 +862,7 @@ mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
     {    
       gchar *path = g_strdup(filelist->path);
 
-      filelist = mpdclient_filelist_free(filelist);
+      filelist_free(filelist);
       filelist = mpdclient_filelist_get(c, path);
       g_free(path);
       return filelist;
@@ -977,62 +870,30 @@ mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
   return NULL;
 }
 
-filelist_entry_t *
-mpdclient_filelist_find_song(mpdclient_filelist_t *fl, struct mpd_song *song)
-{
-  GList *list = g_list_first(fl->list);
-
-  while( list && song)
-    {
-      filelist_entry_t *entry = list->data;
-      mpd_InfoEntity *entity  = entry->entity;
-
-      if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
-       {
-         struct mpd_song *song2 = entity->info.song;
-
-         if( strcmp(song->file, song2->file) == 0 )
-           {
-             return entry;
-           }
-       }
-      list = list->next;
-    }
-  return NULL;
-}
-
 int
 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
 {
-  GList *list = g_list_first(fl->list);
-
-  if( fl->list==NULL || fl->length<1 )
-    return 0;
-
-  mpd_sendCommandListBegin(c->connection);
-  while( list )
-    {
-      filelist_entry_t *entry = list->data;
-      mpd_InfoEntity *entity  = entry->entity;
-
-      if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
-       {
-         struct mpd_song *song = entity->info.song;
-
-         mpd_sendAddCommand(c->connection, song->file);
-       }
-      list = list->next;
-    }
-  mpd_sendCommandListEnd(c->connection);
-  return mpdclient_finish_command(c);
-}
-
+       guint i;
 
+       if (filelist_is_empty(fl))
+               return 0;
 
+       mpd_sendCommandListBegin(c->connection);
 
+       for (i = 0; i < filelist_length(fl); ++i) {
+               filelist_entry_t *entry = filelist_get(fl, i);
+               mpd_InfoEntity *entity  = entry->entity;
 
+               if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
+                       struct mpd_song *song = entity->info.song;
 
+                       mpd_sendAddCommand(c->connection, song->file);
+               }
+       }
 
+       mpd_sendCommandListEnd(c->connection);
+       return mpdclient_finish_command(c);
+}
 
 GList *
 mpdclient_get_artists_utf8(mpdclient_t *c)