Code

po: update Brazilian Portuguese translation
[ncmpc.git] / src / gidle.c
index 9c720866770f415bfe504470c57dbf2cd2d39941..415fb17f005fbbacdfc90d9f4662b50709695309 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
-   (c) 2004-2009 The Music Player Daemon Project
+   (c) 2004-2010 The Music Player Daemon Project
    Project homepage: http://musicpd.org
 
    Redistribution and use in source and binary forms, with or without
@@ -52,6 +52,22 @@ struct mpd_glib_source {
        guint id;
 
        enum mpd_idle idle_events;
+
+       /**
+        * This flag is a hack: it is set while mpd_glib_leave() is
+        * executed.  mpd_glib_leave() might invoke the callback, and
+        * the callback might invoke mpd_glib_enter(), awkwardly
+        * leaving mpd_glib_leave() in idle mode.  As long as this
+        * flag is set, mpd_glib_enter() is a no-op to prevent this.
+        */
+       bool leaving;
+
+       /**
+        * This flag is true when mpd_glib_free() has been called
+        * during a callback invoked from mpd_glib_leave().
+        * mpd_glib_leave() will do the real g_free() call then.
+        */
+       bool destroyed;
 };
 
 struct mpd_glib_source *
@@ -71,6 +87,8 @@ mpd_glib_new(struct mpd_connection *connection,
        source->channel = g_io_channel_unix_new(mpd_async_get_fd(source->async));
        source->io_events = 0;
        source->id = 0;
+       source->leaving = false;
+       source->destroyed = false;
 
        return source;
 }
@@ -78,16 +96,26 @@ mpd_glib_new(struct mpd_connection *connection,
 void
 mpd_glib_free(struct mpd_glib_source *source)
 {
+       assert(!source->destroyed);
+
        if (source->id != 0)
                g_source_remove(source->id);
 
        g_io_channel_unref(source->channel);
+
+       mpd_parser_free(source->parser);
+
+       if (source->leaving)
+               source->destroyed = true;
+       else
+               g_free(source);
 }
 
 static void
 mpd_glib_invoke(const struct mpd_glib_source *source)
 {
        assert(source->id == 0);
+       assert(!source->destroyed);
 
        if (source->idle_events != 0)
                source->callback(MPD_ERROR_SUCCESS, 0, NULL,
@@ -100,6 +128,7 @@ mpd_glib_invoke_error(const struct mpd_glib_source *source,
                      const char *message)
 {
        assert(source->id == 0);
+       assert(!source->destroyed);
 
        source->callback(error, server_error, message,
                         0, source->callback_ctx);
@@ -308,33 +337,40 @@ mpd_glib_add_watch(struct mpd_glib_source *source)
        source->io_events = events;
 }
 
-void
+bool
 mpd_glib_enter(struct mpd_glib_source *source)
 {
        bool success;
 
        assert(source->io_events == 0);
        assert(source->id == 0);
+       assert(!source->destroyed);
+
+       if (source->leaving)
+               return false;
 
        source->idle_events = 0;
 
        success = mpd_async_send_command(source->async, "idle", NULL);
        if (!success) {
                mpd_glib_invoke_async_error(source);
-               return;
+               return false;
        }
 
        mpd_glib_add_watch(source);
+       return true;
 }
 
-void
+bool
 mpd_glib_leave(struct mpd_glib_source *source)
 {
        enum mpd_idle events;
 
+       assert(!source->destroyed);
+
        if (source->id == 0)
                /* already left, callback was invoked */
-               return;
+               return true;
 
        g_source_remove(source->id);
        source->id = 0;
@@ -344,6 +380,8 @@ mpd_glib_leave(struct mpd_glib_source *source)
                ? mpd_run_noidle(source->connection)
                : mpd_recv_idle(source->connection, false);
 
+       source->leaving = true;
+
        if (events == 0 &&
            mpd_connection_get_error(source->connection) != MPD_ERROR_SUCCESS) {
                enum mpd_error error =
@@ -355,9 +393,24 @@ mpd_glib_leave(struct mpd_glib_source *source)
 
                mpd_glib_invoke_error(source, error, server_error,
                                      mpd_connection_get_error_message(source->connection));
-               return;
+
+               if (source->destroyed) {
+                       g_free(source);
+                       return false;
+               }
+
+               source->leaving = false;
+               return true;
        }
 
        source->idle_events |= events;
        mpd_glib_invoke(source);
+
+       if (source->destroyed) {
+               g_free(source);
+               return false;
+       }
+
+       source->leaving = false;
+       return true;
 }