Code

Adding crop feature in ncmpc
[ncmpc.git] / src / mpdclient.c
index ae4a7618faf8567d8589ee541936d635d11ebdf5..6d9d72b9c206789e6694400783397f8b13553ba1 100644 (file)
@@ -168,7 +168,7 @@ mpdclient_new(void)
        return c;
 }
 
-mpdclient_t *
+void
 mpdclient_free(mpdclient_t *c)
 {
        mpdclient_disconnect(c);
@@ -179,8 +179,6 @@ mpdclient_free(mpdclient_t *c)
        g_list_free(c->playlist_callbacks);
        g_list_free(c->browse_callbacks);
        g_free(c);
-
-       return NULL;
 }
 
 gint
@@ -301,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)
 {
@@ -461,7 +494,7 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx)
        c->playlist.id++;
 
        /* remove the song from the playlist */
-       playlist_remove(&c->playlist, idx);
+       playlist_remove_reuse(&c->playlist, idx);
 
        /* call playlist updated callback */
        mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
@@ -472,6 +505,8 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx)
                c->need_update = TRUE;
        }
 
+       mpd_freeSong(song);
+
 #else
        c->need_update = TRUE;
 #endif
@@ -666,7 +701,6 @@ mpdclient_playlist_update(mpdclient_t *c)
 
        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);
@@ -718,7 +752,6 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
 
        c->song = NULL;
        c->playlist.id = c->status->playlist;
-       c->playlist.updated = TRUE;
 
        mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
 
@@ -738,33 +771,6 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
 /*** Filelist functions *****************************************************/
 /****************************************************************************/
 
-mpdclient_filelist_t *
-mpdclient_filelist_free(mpdclient_filelist_t *filelist)
-{
-       GList *list = g_list_first(filelist->list);
-
-       D("mpdclient_filelist_free()\n");
-       if (list == NULL)
-               return NULL;
-       while (list != NULL) {
-               filelist_entry_t *entry = list->data;
-
-               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);
-
-       return NULL;
-}
-
-
 mpdclient_filelist_t *
 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
 {
@@ -775,21 +781,13 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
 
        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, "/")) {
+       filelist = filelist_new(path);
+       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, entry);
-               filelist->length++;
-       }
+               filelist_append(filelist, 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, entry);
-               filelist->length++;
+               filelist_append(filelist, entity);
 
                if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
                        has_dirs_only = FALSE;
@@ -800,13 +798,12 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
        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);
+               filelist_sort(filelist, compare_filelistentry_dir);
        }
 
        return filelist;
@@ -826,19 +823,16 @@ mpdclient_filelist_search_utf8(mpdclient_t *c,
                mpd_sendFindCommand(c->connection, table, filter_utf8);
        else
                mpd_sendSearchCommand(c->connection, table, filter_utf8);
-       filelist = g_malloc0(sizeof(mpdclient_filelist_t));
+       filelist = filelist_new(NULL);
 
-       while ((entity=mpd_getNextInfoEntity(c->connection))) {
-               filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
+       while ((entity=mpd_getNextInfoEntity(c->connection)))
+               filelist_append(filelist, entity);
 
-               entry->entity = entity;
-               filelist->list = g_list_append(filelist->list, entry);
-               filelist->length++;
+       if (mpdclient_finish_command(c)) {
+               filelist_free(filelist);
+               return NULL;
        }
 
-       if (mpdclient_finish_command(c))
-               return mpdclient_filelist_free(filelist);
-
        filelist->updated = TRUE;
        return filelist;
 }
@@ -868,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;
@@ -876,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)