Code

Removed nested functions
[ncmpc.git] / src / mpdclient.c
index c4a88ffe0b548e5385dac792559f908a950a64c0..98743a35d818a76c03cd4ebd9633bb28744ee743 100644 (file)
 #include "support.h"
 #include "mpdclient.h"
 #include "options.h"
+#include "strfsong.h"
 
 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
 #define ENABLE_SONG_ID
-#undef  ENABLE_PLCHANGES /* something is broken */
+#define ENABLE_PLCHANGES 
+
+#define BUFSIZE 1024
 
 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
 
+/* from utils.c */
+extern GList *string_list_free(GList *string_list);
+
+
+/* filelist sorting functions */
+static gint
+compare_filelistentry_dir(gconstpointer filelist_entry1, gconstpointer filelist_entry2)
+{
+  mpd_InfoEntity *e1, *e2;
+  char *key1, *key2;
+  int n = 0;
+
+  e1 = ((filelist_entry_t *)filelist_entry1)->entity;
+  e2 = ((filelist_entry_t *)filelist_entry2)->entity;
+  if (e1 && e2 &&
+      e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
+      e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
+    {
+      key1 = g_utf8_collate_key(e1->info.directory->path,-1);
+      key2 = g_utf8_collate_key(e2->info.directory->path,-1);
+      n = strcmp(key1,key2);
+      g_free(key1);
+      g_free(key2);
+    }
+  return n;
+}
+
+/* sort by list-format */
+gint
+compare_filelistentry_format(gconstpointer filelist_entry1, gconstpointer filelist_entry2)
+{
+  mpd_InfoEntity *e1, *e2;
+  char key1[BUFSIZE], key2[BUFSIZE];
+  int n = 0;
+
+  e1 = ((filelist_entry_t *)filelist_entry1)->entity;
+  e2 = ((filelist_entry_t *)filelist_entry2)->entity;
+  if (e1 && e2 &&
+      e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
+      e2->type == MPD_INFO_ENTITY_TYPE_SONG)
+    {
+      strfsong(key1, BUFSIZE, LIST_FORMAT, e1->info.song);
+      strfsong(key2, BUFSIZE, LIST_FORMAT, e2->info.song);
+      n = strcmp(key1,key2);
+    }
+  return n;
+}
+
 
 /* Error callbacks */
 static gint
@@ -61,6 +112,8 @@ error_cb(mpdclient_t *c, gint error, gchar *msg)
 }
 
 #ifdef DEBUG
+// Unused ath the moment
+/*
 #include "strfsong.h"
 
 static gchar *
@@ -71,7 +124,7 @@ get_song_name(mpd_Song *song)
   strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
   return name;
 }
-
+*/
 #endif
 
 /****************************************************************************/
@@ -87,10 +140,12 @@ mpdclient_finish_command(mpdclient_t *c)
     {
       gchar *msg = locale_to_utf8(c->connection->errorStr);
       gint error = c->connection->error;
-      
       if( error == MPD_ERROR_ACK )
        error = error | (c->connection->errorCode << 8);
-
+      if(  c->connection->errorCode == MPD_ACK_ERROR_PERMISSION )
+       {
+         if(screen_auth(c) == 0) return 0;
+       }
       error_cb(c, error, msg);
       g_free(msg);
       return error;
@@ -316,9 +371,9 @@ mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
 }
 
 gint 
-mpdclient_cmd_db_update(mpdclient_t *c)
+mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path)
 {
-  mpd_sendUpdateCommand(c->connection);
+  mpd_sendUpdateCommand(c->connection, path ? path : "");
   return mpdclient_finish_command(c);
 }
 
@@ -329,6 +384,24 @@ mpdclient_cmd_volume(mpdclient_t *c, gint value)
   return mpdclient_finish_command(c);
 }
 
+gint 
+mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8)
+{
+  mpd_sendAddCommand(c->connection, path_utf8);
+  return mpdclient_finish_command(c);
+}
+
+gint 
+mpdclient_cmd_add_path(mpdclient_t *c, gchar *path)
+{
+  gint retval;
+  gchar *path_utf8 = locale_to_utf8(path);
+
+  retval=mpdclient_cmd_add_path_utf8(c, path_utf8);
+  g_free(path_utf8);
+  return retval;
+}
+
 gint 
 mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song)
 { 
@@ -422,7 +495,7 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
 
   /* send the move command to mpd */  
 #ifdef ENABLE_SONG_ID
-  D("Swaping id:%d with id:%d\n", song1->id, song2->id);
+  D("Swapping id:%d with id:%d\n", song1->id, song2->id);
   mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
 #else
   D("Moving index %d to id:%d\n", old_index, new_index);
@@ -465,26 +538,36 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
 #endif 
 
   /* call playlist updated callback */
+  D("move> new_index=%d, old_index=%d\n", new_index, old_index);
   mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
 
   return 0;
 }
 
 gint 
-mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
+mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
 {
   gint retval = 0;
-  gchar *filename_utf8 = locale_to_utf8(filename);
 
   mpd_sendSaveCommand(c->connection, filename_utf8);
   if( (retval=mpdclient_finish_command(c)) == 0 )
     mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
+  return retval;
+}
+
+gint 
+mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename)
+{
+  gint retval = 0;
+  gchar *filename_utf8 = locale_to_utf8(filename);
+  
+  retval = mpdclient_cmd_save_playlist_utf8(c, filename);
   g_free(filename_utf8);
   return retval;
 }
 
 gint 
-mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
+mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
 {
   mpd_sendLoadCommand(c->connection, filename_utf8);
   c->need_update = TRUE;
@@ -492,7 +575,7 @@ mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
 }
 
 gint 
-mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
+mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8)
 {
   gint retval = 0;
 
@@ -502,6 +585,17 @@ mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
   return retval;
 }
 
+gint 
+mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename)
+{
+  gint retval = 0;
+  gchar *filename_utf8 = locale_to_utf8(filename);
+
+  retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8);
+  g_free(filename_utf8);
+  return retval;
+}
+
 
 /****************************************************************************/
 /*** Callback managment functions *******************************************/
@@ -624,8 +718,9 @@ mpdclient_playlist_update(mpdclient_t *c)
 }
 
 #ifdef ENABLE_PLCHANGES
-static gint 
-compare_songs(gconstpointer a, gconstpointer b)
+
+gint 
+mpdclient_compare_songs(gconstpointer a, gconstpointer b)
 {
   mpd_Song *song1 = (mpd_Song *) a; 
   mpd_Song *song2 = (mpd_Song *) b; 
@@ -633,8 +728,9 @@ compare_songs(gconstpointer a, gconstpointer b)
   return song1->pos - song2->pos;
 }
 
-/* update playlist (plchanges) */
 
+
+/* update playlist (plchanges) */
 gint 
 mpdclient_playlist_update_changes(mpdclient_t *c)
 {
@@ -650,75 +746,50 @@ mpdclient_playlist_update_changes(mpdclient_t *c)
 
   while( (entity=mpd_getNextInfoEntity(c->connection)) != NULL   ) 
     {
-      if(entity->type==MPD_INFO_ENTITY_TYPE_SONG) 
-       {
-         mpd_Song *song;
-         GList *item;
+      mpd_Song *song = entity->info.song;
 
-         if( (song=mpd_songDup(entity->info.song)) == NULL )
-           {
-             D("song==NULL => calling mpdclient_playlist_update()\n");
-             return mpdclient_playlist_update(c);
-           }
-
-         item =  playlist_lookup(c, song->id);
+      if( song->pos < c->playlist.length )
+       {
+         GList *item = g_list_nth(c->playlist.list, song->pos);
 
-         if( item && item->data)
-           {
-             GList *old;
-             gint index = g_list_position(c->playlist.list, item);
-
-             /* remove previous song at song->pos */
-             if( song->pos != index && 
-                 (old=g_list_nth(c->playlist.list, song->pos)) )
-               {
-                 D("Removing item with index %d [%p]\n", index, old);
-                 mpd_freeSong((mpd_Song *) old->data);
-                 old->data = NULL;
-                 c->playlist.list = g_list_delete_link(c->playlist.list, old);
-               }
-
-             /* Update playlist entry */
-             mpd_freeSong((mpd_Song *) item->data);                 
-             item->data = song;
-             if( c->song && c->song->id == song->id )
-               c->song = song;
-
-             D("Changing index:%d, pos:%d, id:%d => %s\n",
-               index, song->pos, song->id, get_song_name(song));
-           }
-         else
-           {
-             /* Add a new  playlist entry */
-             D("Adding pos:%d, id;%d - %s\n",
-               song->pos, song->id, get_song_name(song));
-             c->playlist.list = g_list_append(c->playlist.list, 
-                                         (gpointer) song);
-             c->playlist.length++;
-           }
+         /* update song */
+         D("updating pos:%d, id=%d [%p] - %s\n", 
+           song->pos, song->id, item, song->file);
+         mpd_freeSong((mpd_Song *) item->data);                     
+         item->data = mpd_songDup(song);
+       }
+      else
+       {
+         /* add a new song */
+         D("adding song at pos %d\n", song->pos);
+         c->playlist.list = g_list_append(c->playlist.list, 
+                                          (gpointer) mpd_songDup(song));
        }
-      mpd_freeInfoEntity(entity);      
+      
     }
-  mpd_finishCommand(c->connection);
-  
-  while( g_list_length(c->playlist.list) > c->status->playlistLength )
+
+  /* remove trailing songs */
+  while( c->status->playlistLength < c->playlist.length )
     {
       GList *item = g_list_last(c->playlist.list);
 
       /* Remove the last playlist entry */
+      D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
       mpd_freeSong((mpd_Song *) item->data);
       c->playlist.list = g_list_delete_link(c->playlist.list, item);
-      c->playlist.length--;      
-      D("Removed the last playlist entry\n");
+      c->playlist.length = g_list_length(c->playlist.list);   
     }
 
+  c->song = NULL;
   c->playlist.id = c->status->playlist;
   c->playlist.updated = TRUE;
+  c->playlist.length = g_list_length(c->playlist.list);
 
   mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
 
   return 0;
 }
+
 #else
 gint 
 mpdclient_playlist_update_changes(mpdclient_t *c)
@@ -734,14 +805,14 @@ playlist_get_song(mpdclient_t *c, gint index)
 }
 
 GList *
-playlist_lookup(mpdclient_t *c, gint id)
+playlist_lookup(mpdclient_t *c, int id)
 {
-  GList *list = c->playlist.list;
+  GList *list = g_list_first(c->playlist.list);
 
   while( list )
     {
       mpd_Song *song = (mpd_Song *) list->data;
-      if( (gint) song->id == id )
+      if( song->id == id )
        return list;
       list=list->next;
     }
@@ -803,6 +874,8 @@ 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;
@@ -829,6 +902,7 @@ mpdclient_filelist_get(mpdclient_t *c, gchar *path)
   mpdclient_filelist_t *filelist;
   mpd_InfoEntity *entity;
   gchar *path_utf8 = locale_to_utf8(path);
+  gboolean has_dirs_only = TRUE;
 
   D("mpdclient_filelist_get(%s)\n", path);
   mpd_sendLsInfoCommand(c->connection, path_utf8);
@@ -849,18 +923,77 @@ mpdclient_filelist_get(mpdclient_t *c, gchar *path)
       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( mpdclient_finish_command(c) )
-    {
-      g_free(path_utf8);
-      return mpdclient_filelist_free(filelist);
-    }
+   /* 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;
+}
+
+mpdclient_filelist_t *
+mpdclient_filelist_search_utf8(mpdclient_t *c, 
+                              int exact_match,
+                              int table, 
+                              gchar *filter_utf8)
+{
+  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));
+
+  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);
+
+  filelist->updated = TRUE;
+
+  return filelist;
+}
+
+
+mpdclient_filelist_t *
+mpdclient_filelist_search(mpdclient_t *c,
+                         int exact_match, 
+                         int table, 
+                         gchar *filter)
+{
+  mpdclient_filelist_t *filelist;
+  gchar *filter_utf8 = locale_to_utf8(filter);
+
+  D("mpdclient_filelist_search(%s)\n", filter);
+  filelist = mpdclient_filelist_search_utf8(c,exact_match,table,filter_utf8);
+  g_free(filter_utf8);
+
   return filelist;
 }
 
@@ -903,11 +1036,79 @@ mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
   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 )
+       {
+         mpd_Song *song = entity->info.song;
+
+         mpd_sendAddCommand(c->connection, song->file);
+       }
+      list = list->next;
+    }
+  mpd_sendCommandListEnd(c->connection);
+  return mpdclient_finish_command(c);
+}
+
+
+
 
 
 
 
 
+GList *
+mpdclient_get_artists_utf8(mpdclient_t *c)
+{
+  gchar *str = NULL; 
+  GList *list = NULL;
+
+  D("mpdclient_get_artists()\n");
+  mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
+  while( (str=mpd_getNextArtist(c->connection)) )
+    {
+      list = g_list_append(list, (gpointer) str);
+    }
+  if( mpdclient_finish_command(c) )
+    {
+      return string_list_free(list);
+    }  
+
+  return list;
+}
+
+GList *
+mpdclient_get_albums_utf8(mpdclient_t *c, gchar *artist_utf8)
+{
+  gchar *str = NULL; 
+  GList *list = NULL;
+
+  D("mpdclient_get_albums(%s)\n", artist_utf8);
+  mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
+  while( (str=mpd_getNextAlbum(c->connection)) )
+    {
+      list = g_list_append(list, (gpointer) str);
+    }
+  if( mpdclient_finish_command(c) )
+    {
+      return string_list_free(list);
+    }  
+  
+  return list;
+}
+