Code

Updated error handling
authorKalle Wallin <kaw@linux.se>
Mon, 14 Jun 2004 23:05:24 +0000 (23:05 +0000)
committerKalle Wallin <kaw@linux.se>
Mon, 14 Jun 2004 23:05:24 +0000 (23:05 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@1492 09075e82-0dd4-0310-85a5-a0d7c8717e4f

src/main.c
src/mpdclient.c
src/mpdclient.h

index 8d3a82fd2f90e2f2703d0b3395469a18e2b5bc64..e996233a0f2e35d5366337ab11b5342948e3e6a4 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <signal.h>
+#include <string.h>
 #include <glib.h>
 
 #include "config.h"
@@ -37,22 +38,41 @@ static mpdclient_t   *mpd = NULL;
 static gboolean connected = FALSE;
 static GTimer      *timer = NULL;
 
+static gchar *
+error_msg(gchar *msg)
+{
+  gchar *p;
+
+  if( (p=strchr(msg, '}' )) == NULL )
+    return msg;
+  while( p && *p && (*p=='}' || *p==' ') )
+    p++;
+
+  return p;
+}
+
 static void
-error_callback(mpdclient_t *c, int error, char *msg)
+error_callback(mpdclient_t *c, gint error, gchar *msg)
 {
-  D("error_callback> error=%d errorCode=%d errorAt=%d\n", 
-    error, c->connection->errorCode, c->connection->errorAt);
-  D("error_callback> \"%s\"\n", msg);
+  gint code = GET_ACK_ERROR_CODE(error);
+
+  error = error & 0xFF;
+  D("Error [%d:%d]> \"%s\"\n", error, c->connection->errorCode, msg);
   switch(error)
     {
+    case MPD_ERROR_CONNPORT:
+    case MPD_ERROR_NORESPONSE:
+      break;
     case MPD_ERROR_ACK:
-      screen_status_printf("%s", msg);
+      screen_status_printf("%s", error_msg(msg));
+      beep();
       break;
     default:
-      screen_status_printf(_("Lost connection to %s"), options.host);
+      screen_status_printf("%s", msg);
+      doupdate();
+      beep();
       connected = FALSE;
     }
-  doupdate();
 }
 
 void
@@ -203,7 +223,6 @@ main(int argc, const char *argv[])
        {
          screen_status_printf(_("Connecting to %s...  [Press %s to abort]"), 
                               options->host, get_key_names(CMD_QUIT,0) );
-         doupdate();
 
          if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
            exit(EXIT_SUCCESS);
@@ -211,13 +230,13 @@ main(int argc, const char *argv[])
          if( mpdclient_connect(mpd,
                                options->host,
                                options->port, 
-                               1.0,
+                               1.5,
                                options->password) == 0 )
            {
              screen_status_printf(_("Connected to %s!"), options->host);
-             doupdate();
              connected = TRUE;
            }     
+         doupdate();
        }
 
       t = g_timer_elapsed(timer, NULL);
index 89ed29e2a39c787199ec9e0b61a3de7392fd5a50..5256413d5de6745683e92e10afceaf5b4b9ac064 100644 (file)
@@ -46,7 +46,7 @@ error_cb(mpdclient_t *c, gint error, gchar *msg)
   GList *list = c->error_callbacks;
   
   if( list==NULL )
-    fprintf(stderr, "error [%d]: %s\n", error, msg);
+    fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
 
   while(list)
     {
@@ -85,11 +85,14 @@ mpdclient_finish_command(mpdclient_t *c)
   if( c->connection->error )
     {
       gchar *msg = locale_to_utf8(c->connection->errorStr);
-      gint retval = c->connection->error;
+      gint error = c->connection->error;
+      
+      if( error == MPD_ERROR_ACK )
+       error = error | (c->connection->errorCode << 8);
 
-      error_cb(c, c->connection->error, msg);
+      error_cb(c, error, msg);
       g_free(msg);
-      return retval;
+      return error;
     }
 
   return 0;
@@ -120,7 +123,6 @@ mpdclient_free(mpdclient_t *c)
 gint
 mpdclient_disconnect(mpdclient_t *c)
 {
-  D("mpdclient_disconnect()...\n");
   if( c->connection )
     mpd_closeConnection(c->connection);
   c->connection = NULL;
@@ -152,7 +154,6 @@ mpdclient_connect(mpdclient_t *c,
     mpdclient_disconnect(c);
 
   /* connect to MPD */
-  D("mpdclient_connect(%s, %d)...\n", host, port);
   c->connection = mpd_newConnection(host, port, timeout);
   if( c->connection->error )
     return error_cb(c, c->connection->error, c->connection->errorStr);
@@ -163,6 +164,7 @@ mpdclient_connect(mpdclient_t *c,
       mpd_sendPasswordCommand(c->connection, password);
       retval = mpdclient_finish_command(c);
     }
+  c->need_update = TRUE;
 
   return retval;
 }
@@ -568,8 +570,7 @@ mpdclient_playlist_free(mpdclient_playlist_t *playlist)
       list=list->next;
     }
   g_list_free(playlist->list);
-  playlist->list   = NULL;
-  playlist->length = 0;
+  memset(playlist, 0, sizeof(mpdclient_playlist_t));
   return 0;
 }
 
@@ -587,9 +588,6 @@ mpdclient_playlist_update(mpdclient_t *c)
   if( c->playlist.list )
     mpdclient_playlist_free(&c->playlist);
 
-  c->song = NULL;
-  c->playlist.updated = TRUE;
-
   mpd_sendPlaylistInfoCommand(c->connection,-1);
   while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
     {
@@ -604,6 +602,7 @@ 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);
index 86f1b0ce810c14f09de6060c6a32420c2c962c03..391a5d224576aa7b9f71975ba18f7fd7a2d99883 100644 (file)
@@ -91,6 +91,10 @@ gint mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8);
 
 
 /*** error callbacks *****************************************************/ 
+
+#define IS_ACK_ERROR(n)       (n & MPD_ERROR_ACK)
+#define GET_ACK_ERROR_CODE(n) ((n & 0xFF00) >> 8)
+
 typedef void (*mpdc_error_cb_t) (mpdclient_t *c, gint error, gchar *msg);
 
 void mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb);