Code

Added support for (auto) center/focus playlists.
[ncmpc.git] / mpc.c
diff --git a/mpc.c b/mpc.c
index ccb08359009ff880587e52c25d665cf24cbeac3f..00a044462d3d0b0cd9568759373154c284c35252 100644 (file)
--- a/mpc.c
+++ b/mpc.c
@@ -1,8 +1,3 @@
-/* 
- * $Id: mpc.c,v 1.5 2004/03/17 23:19:21 kalle Exp $ 
- *
- */
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include "mpc.h"
 #include "options.h"
 
+#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);
@@ -28,6 +28,8 @@ mpc_update_song(mpd_client_t *c)
     }
 
   mpd_sendPlaylistInfoCommand(c->connection, c->status->song);
+  if( mpc_error(c) )
+    return;
   while( (entity=mpd_getNextInfoEntity(c->connection)) )
     {
       mpd_Song *song = entity->info.song;
@@ -55,13 +57,13 @@ mpc_close(mpd_client_t *c)
   if( c->connection )
     mpd_closeConnection(c->connection);
   if( c->cwd )
-    free( c->cwd );
+    g_free( c->cwd );
   
   return 0;
 }
 
 mpd_client_t *
-mpc_connect(char *host, int port)
+mpc_connect(char *host, int port, char *password)
 {
   mpd_Connection *connection;
   mpd_client_t *c;
@@ -73,21 +75,53 @@ mpc_connect(char *host, int port)
       exit(EXIT_FAILURE);
     }
   
-  c = malloc(sizeof(mpd_client_t));
+  c = g_malloc(sizeof(mpd_client_t));
   memset(c, 0, sizeof(mpd_client_t));
   c->connection = connection;
-  c->cwd = strdup("");
+  c->cwd = g_strdup("");
+
+  if( password )
+    {
+      mpd_sendPasswordCommand(connection, password);
+      mpd_finishCommand(connection);
+    }
 
   return c;
 }
 
+int
+mpc_reconnect(mpd_client_t *c, char *host, int port, char *password)
+{
+  mpd_Connection *connection;
+
+  connection =  mpd_newConnection(host, port, 10);
+  if( connection==NULL )
+    return -1;
+  if( connection->error )
+    {
+      mpd_closeConnection(connection);
+      return -1;
+    }
+  
+  c->connection = connection;
+
+  if( password )
+    {
+      mpd_sendPasswordCommand(connection, password);
+      mpd_finishCommand(connection);
+    }
+
+  return 0;
+}
+
+
 int
 mpc_error(mpd_client_t *c)
 {
   if( c == NULL || c->connection == NULL )
     return 1;
   if( c->connection->error )
-    return 1;
+    return c->connection->error;
 
   return 0;
 }
@@ -137,11 +171,16 @@ mpc_update_playlist(mpd_client_t *c)
 
   //  fprintf(stderr, "mpc_update_playlist(): status->playlist = %d\n",  c->status->playlist);
 
+  if( mpc_error(c) )
+    return -1;
+
   if( c->playlist )
     mpc_free_playlist(c);
 
   c->playlist_length=0;
   mpd_sendPlaylistInfoCommand(c->connection,-1);
+  if( mpc_error(c) )
+    return -1;
   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
     {
       if(entity->type==MPD_INFO_ENTITY_TYPE_SONG) 
@@ -186,38 +225,51 @@ mpc_playlist_get_song(mpd_client_t *c, int n)
 char *
 mpc_get_song_name(mpd_Song *song)
 {
+  static char buf[MAX_SONG_LENGTH];
+  char *name;
+  
   if( song->title )
     {
       if( song->artist )
        {
-         static char buf[512];
-
-         snprintf(buf, 512, "%s - %s", song->artist, song->title);
-         return utf8(buf);
+         snprintf(buf, MAX_SONG_LENGTH, "%s - %s", song->artist, song->title);
+         name = utf8_to_locale(buf);
+         strncpy(buf, name, MAX_SONG_LENGTH);
+         g_free(name);
+         return buf;
        }
       else
        {
-         return utf8(song->title);
+         name = utf8_to_locale(song->title);
+         strncpy(buf, name, MAX_SONG_LENGTH);
+         g_free(name);
+         return buf;
        }
     }
-  return utf8(basename(song->file));
+  name = utf8_to_locale(basename(song->file));
+  strncpy(buf, name, MAX_SONG_LENGTH);
+  g_free(name);
+  return buf;
 }
 
 int 
 mpc_update(mpd_client_t *c)
 {
+  if( mpc_error(c) )
+    return -1;
+
   if( c->status )
     {
       mpd_freeStatus(c->status);
     }
 
   c->status = mpd_getStatus(c->connection);
+  if( mpc_error(c) )
+    return -1;
   
-  //  if( c->playlist == NULL || c->playlist_id!=c->status->playlist )
   if( c->playlist_id!=c->status->playlist )
     mpc_update_playlist(c);
   
-  //  if( c->song == NULL || c->status->song != c->song_id )
   if( c->status->song != c->song_id )
     {
       c->song = mpc_playlist_get_song(c, c->status->song);
@@ -249,7 +301,7 @@ mpc_free_filelist(mpd_client_t *c)
 
       if( entry->entity )
        mpd_freeInfoEntity(entry->entity);
-      free(entry);
+      g_free(entry);
       list=list->next;
     }
   g_list_free(c->filelist);
@@ -266,6 +318,9 @@ mpc_update_filelist(mpd_client_t *c)
 {
   mpd_InfoEntity *entity;
 
+  if( mpc_error(c) )
+    return -1;
+
   if( c->filelist )
     mpc_free_filelist(c);
 
@@ -277,7 +332,7 @@ mpc_update_filelist(mpd_client_t *c)
   if( c->cwd && c->cwd[0] )
     {
       /* add a dummy entry for ./.. */
-      filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
+      filelist_entry_t *entry = g_malloc(sizeof(filelist_entry_t));
       memset(entry, 0, sizeof(filelist_entry_t));
       entry->entity = NULL;
       c->filelist = g_list_append(c->filelist, (gpointer) entry);
@@ -286,7 +341,7 @@ mpc_update_filelist(mpd_client_t *c)
 
   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
     {
-      filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
+      filelist_entry_t *entry = g_malloc(sizeof(filelist_entry_t));
       
       memset(entry, 0, sizeof(filelist_entry_t));
       entry->entity = entity;