Code

Removed nested functions
[ncmpc.git] / src / mpdclient.c
index 03c69f30ec2fc2c3e0c70f760d41e137bb3ed571..98743a35d818a76c03cd4ebd9633bb28744ee743 100644 (file)
@@ -30,6 +30,7 @@
 #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_SONG_ID
 #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
 error_cb(mpdclient_t *c, gint error, gchar *msg)
@@ -63,6 +112,8 @@ error_cb(mpdclient_t *c, gint error, gchar *msg)
 }
 
 #ifdef DEBUG
+// Unused ath the moment
+/*
 #include "strfsong.h"
 
 static gchar *
@@ -73,7 +124,7 @@ get_song_name(mpd_Song *song)
   strfsong(name, 256, "[%artist% - ]%title%|%file%", song);
   return name;
 }
-
+*/
 #endif
 
 /****************************************************************************/
@@ -89,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;
@@ -442,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);
@@ -849,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);
@@ -869,29 +923,44 @@ 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 table, gchar *filter_utf8)
+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);
-  mpd_sendSearchCommand(c->connection, table, 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)) ) 
@@ -913,13 +982,16 @@ mpdclient_filelist_search_utf8(mpdclient_t *c, int table, gchar *filter_utf8)
 
 
 mpdclient_filelist_t *
-mpdclient_filelist_search(mpdclient_t *c, int table, gchar *filter)
+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, table, filter_utf8);
+  filelist = mpdclient_filelist_search_utf8(c,exact_match,table,filter_utf8);
   g_free(filter_utf8);
 
   return filelist;
@@ -964,6 +1036,38 @@ 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)