libnm-glib: add "object-creation-failed" signal to NMObject
authorJiří Klimeš <jklimes@redhat.com>
Tue, 24 Apr 2012 14:23:27 +0000 (16:23 +0200)
committerJiří Klimeš <jklimes@redhat.com>
Wed, 25 Apr 2012 14:39:17 +0000 (16:39 +0200)
The signal is private for libnm-glib and should not be used externally.
It is emitted when there's an error while creating an object.
In addition, this commit makes use of the signal in NMClient to ensure
that the callbacks are always called for nm_client_activate_connection()
and nm_client_add_and_activate_connection().

libnm-glib/libnm-glib.ver
libnm-glib/nm-client.c
libnm-glib/nm-object.c
libnm-glib/nm-object.h

index 427e248..d4d678b 100644 (file)
@@ -180,6 +180,8 @@ global:
        nm_object_get_connection;
        nm_object_get_path;
        nm_object_get_type;
+       nm_object_error_get_type;
+       nm_object_error_quark;
        nm_remote_connection_commit_changes;
        nm_remote_connection_delete;
        nm_remote_connection_get_secrets;
index 311b1d2..a21b814 100644 (file)
@@ -457,35 +457,56 @@ activate_info_complete (ActivateInfo *info,
 }
 
 static void
-recheck_pending_activations (NMClient *self)
+recheck_pending_activations (NMClient *self, const char *failed_path, GError *error)
 {
        NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);
        GSList *iter;
        const GPtrArray *active_connections;
+       gboolean found_in_active = FALSE;
+       gboolean found_in_pending = FALSE;
+       ActivateInfo *ainfo = NULL;
        int i;
 
        active_connections = nm_client_get_active_connections (self);
-       if (!active_connections)
-               return;
 
-       /* For each active connection, look for a pending activation that has
-        * the active connection's object path, and call its callback.
+       /* For each pending activation, look for a active connection that has
+        * the pending activation's object path, and call pending connection's
+        * callback.
+        * If the connection to activate doesn't make it to active_connections,
+        * due to an error, we have to call the callback for failed_path.
         */
-       for (i = 0; i < active_connections->len; i++) {
-               NMActiveConnection *active = g_ptr_array_index (active_connections, i);
-               const char *active_path = nm_object_get_path (NM_OBJECT (active));
+       for (iter = priv->pending_activations; iter; iter = g_slist_next (iter)) {
+               ActivateInfo *info = iter->data;
+
+               if (!found_in_pending && failed_path && g_strcmp0 (failed_path, info->active_path) == 0) {
+                       found_in_pending = TRUE;
+                       ainfo = info;
+               }
 
-               for (iter = priv->pending_activations; iter; iter = g_slist_next (iter)) {
-                       ActivateInfo *info = iter->data;
+               for (i = 0; active_connections && i < active_connections->len; i++) {
+                       NMActiveConnection *active = g_ptr_array_index (active_connections, i);
+                       const char *active_path = nm_object_get_path (NM_OBJECT (active));
+
+                       if (!found_in_active && failed_path && g_strcmp0 (failed_path, active_path) == 0)
+                               found_in_active = TRUE;
 
                        if (g_strcmp0 (info->active_path, active_path) == 0) {
-                               /* Call the pending activation's callback and it all up*/
+                               /* Call the pending activation's callback and it all up */
                                activate_info_complete (info, active, NULL);
                                activate_info_free (info);
                                break;
                        }
                }
        }
+
+       if (!found_in_active && found_in_pending) {
+               /* A newly activated connection failed due to some immediate error
+                * and disappeared from active connection list.  Make sure the
+                * callback gets called.
+                */
+               activate_info_complete (ainfo, NULL, error);
+               activate_info_free (ainfo);
+       }
 }
 
 static void
@@ -506,7 +527,7 @@ activate_cb (DBusGProxy *proxy,
                g_clear_error (&error);
        } else {
                info->active_path = path;
-               recheck_pending_activations (info->client);
+               recheck_pending_activations (info->client, NULL, NULL);
        }
 }
 
@@ -585,7 +606,7 @@ add_activate_cb (DBusGProxy *proxy,
        } else {
                info->new_connection_path = connection_path;
                info->active_path = active_path;
-               recheck_pending_activations (info->client);
+               recheck_pending_activations (info->client, NULL, NULL);
        }
 }
 
@@ -651,7 +672,14 @@ nm_client_add_and_activate_connection (NMClient *client,
 static void
 active_connections_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
 {
-       recheck_pending_activations (NM_CLIENT (object));
+       recheck_pending_activations (NM_CLIENT (object), NULL, NULL);
+}
+
+static void
+object_creation_failed_cb (GObject *object, GError *error, char *failed_path)
+{
+       if (error)
+               recheck_pending_activations (NM_CLIENT (object), failed_path, error);
 }
 
 /**
@@ -1408,6 +1436,9 @@ constructed (GObject *object)
 
        g_signal_connect (object, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
                          G_CALLBACK (active_connections_changed_cb), NULL);
+
+       g_signal_connect (object, "object-creation-failed",
+                         G_CALLBACK (object_creation_failed_cb), NULL);
 }
 
 static gboolean
index e333f9f..756bfa7 100644 (file)
@@ -18,7 +18,7 @@
  * Boston, MA 02110-1301 USA.
  *
  * Copyright (C) 2007 - 2008 Novell, Inc.
- * Copyright (C) 2007 - 2011 Red Hat, Inc.
+ * Copyright (C) 2007 - 2012 Red Hat, Inc.
  */
 
 #include <string.h>
@@ -31,6 +31,7 @@
 #include "nm-dbus-glib-types.h"
 #include "nm-glib-compat.h"
 #include "nm-types.h"
+#include "nm-glib-marshal.h"
 
 #define DEBUG 0
 
@@ -93,6 +94,31 @@ enum {
        LAST_PROP
 };
 
+enum {
+       OBJECT_CREATION_FAILED,
+
+       LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+/**
+ * nm_object_error_quark:
+ *
+ * Registers an error quark for #NMObject if necessary.
+ *
+ * Returns: the error quark used for #NMObject errors.
+ **/
+GQuark
+nm_object_error_quark (void)
+{
+       static GQuark quark;
+
+       if (G_UNLIKELY (!quark))
+               quark = g_quark_from_static_string ("nm-object-error-quark");
+       return quark;
+}
+
 static void
 nm_object_init (NMObject *object)
 {
@@ -323,6 +349,29 @@ nm_object_class_init (NMObjectClass *nm_object_class)
                                                          "DBus Object Path",
                                                          NULL,
                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+       /* signals */
+
+       /**
+        * NMObject::object-creation-failed:
+        * @master_object: the object that received the signal
+        * @error: the error that occured while creating object
+        * @failed_path: object path of the failed object
+        *
+        * Indicates that an error occured while creating an #NMObject object
+        * during property handling of @master_object.
+        *
+        * Note: Be aware that the signal is private for libnm-glib's internal
+        *       use.
+        **/
+       signals[OBJECT_CREATION_FAILED] =
+               g_signal_new ("object-creation-failed",
+                             G_OBJECT_CLASS_TYPE (object_class),
+                             G_SIGNAL_RUN_FIRST,
+                             G_STRUCT_OFFSET (NMObjectClass, object_creation_failed),
+                             NULL, NULL,
+                             _nm_glib_marshal_VOID__POINTER_POINTER,
+                             G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
 }
 
 static void
@@ -463,7 +512,7 @@ _nm_object_create (GType type, DBusGConnection *connection, const char *path)
        return object;
 }
 
-typedef void (*NMObjectCreateCallbackFunc) (GObject *, gpointer);
+typedef void (*NMObjectCreateCallbackFunc) (GObject *, const char *, gpointer);
 typedef struct {
        DBusGConnection *connection;
        char *path;
@@ -474,7 +523,7 @@ typedef struct {
 static void
 create_async_complete (GObject *object, NMObjectTypeAsyncData *async_data)
 {
-       async_data->callback (object, async_data->user_data);
+       async_data->callback (object, async_data->path, async_data->user_data);
 
        g_free (async_data->path);
        g_slice_free (NMObjectTypeAsyncData, async_data);
@@ -643,12 +692,23 @@ object_property_complete (ObjectCreatedData *odata)
 }
 
 static void
-object_created (GObject *obj, gpointer user_data)
+object_created (GObject *obj, const char *path, gpointer user_data)
 {
        ObjectCreatedData *odata = user_data;
 
        /* We assume that on error, the creator_func printed something */
 
+       if (obj == NULL && g_strcmp0 (path, "/") != 0 ) {
+               GError *error;
+               error = g_error_new (NM_OBJECT_ERROR,
+                                    NM_OBJECT_ERROR_OBJECT_CREATION_FAILURE,
+                                    "Creating object for path '%s' failed in libnm-glib.",
+                                    path);
+               /* Emit a signal about the error. */
+               g_signal_emit (odata->self, signals[OBJECT_CREATION_FAILED], 0, error, path);
+               g_error_free (error);
+       }
+
        odata->objects[--odata->remaining] = obj;
        if (!odata->remaining)
                object_property_complete (odata);
@@ -675,18 +735,19 @@ handle_object_property (NMObject *self, const char *property_name, GValue *value
                priv->reload_remaining++;
 
        path = g_value_get_boxed (value);
+
        if (!strcmp (path, "/")) {
-               object_created (NULL, odata);
+               object_created (NULL, path, odata);
                return TRUE;
        }
 
        obj = G_OBJECT (_nm_object_cache_get (path));
        if (obj) {
-               object_created (obj, odata);
+               object_created (obj, path, odata);
                return TRUE;
        } else if (synchronously) {
                obj = _nm_object_create (pi->object_type, priv->connection, path);
-               object_created (obj, odata);
+               object_created (obj, path, odata);
                return obj != NULL;
        } else {
                _nm_object_create_async (pi->object_type, priv->connection, path,
@@ -735,10 +796,10 @@ handle_object_array_property (NMObject *self, const char *property_name, GValue
 
                obj = G_OBJECT (_nm_object_cache_get (path));
                if (obj) {
-                       object_created (obj, odata);
+                       object_created (obj, path, odata);
                } else if (synchronously) {
                        obj = _nm_object_create (pi->object_type, priv->connection, path);
-                       object_created (obj, odata);
+                       object_created (obj, path, odata);
                } else {
                        _nm_object_create_async (pi->object_type, priv->connection, path,
                                                 object_created, odata);
@@ -1091,7 +1152,7 @@ _nm_object_set_property (NMObject *object,
 }
 
 static void
-pseudo_property_object_created (GObject *obj, gpointer user_data)
+pseudo_property_object_created (GObject *obj, const char *path, gpointer user_data)
 {
        PseudoPropertyInfo *ppi = user_data;
 
@@ -1117,7 +1178,7 @@ pseudo_property_added (DBusGProxy *proxy, const char *path, gpointer user_data)
 
        obj = _nm_object_cache_get (path);
        if (obj)
-               pseudo_property_object_created (G_OBJECT (obj), ppi);
+               pseudo_property_object_created (G_OBJECT (obj), path, ppi);
        else {
                _nm_object_create_async (ppi->pi.object_type, priv->connection, path,
                                         pseudo_property_object_created, ppi);
index 3f7b36c..267c714 100644 (file)
@@ -18,7 +18,7 @@
  * Boston, MA 02110-1301 USA.
  *
  * Copyright (C) 2007 - 2008 Novell, Inc.
- * Copyright (C) 2007 - 2008 Red Hat, Inc.
+ * Copyright (C) 2007 - 2012 Red Hat, Inc.
  */
 
 #ifndef NM_OBJECT_H
@@ -37,6 +37,22 @@ G_BEGIN_DECLS
 #define NM_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_OBJECT))
 #define NM_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_OBJECT, NMObjectClass))
 
+/**
+ * NMObjectError:
+ * @NM_OBJECT_ERROR_UNKNOWN: unknown or unclassified error
+ * @NM_OBJECT_ERROR_OBJECT_CREATION_FAILURE: an error ocured while creating an #NMObject
+ *
+ * Describes errors that may result from operations involving a #NMObject.
+ *
+ **/
+typedef enum {
+       NM_OBJECT_ERROR_UNKNOWN = 0,
+       NM_OBJECT_ERROR_OBJECT_CREATION_FAILURE,
+} NMObjectError;
+
+#define NM_OBJECT_ERROR nm_object_error_quark ()
+GQuark nm_object_error_quark (void);
+
 #define NM_OBJECT_DBUS_CONNECTION "dbus-connection"
 #define NM_OBJECT_DBUS_PATH "dbus-path"
 
@@ -47,6 +63,15 @@ typedef struct {
 typedef struct {
        GObjectClass parent;
 
+       /* Signals */
+       /* The "object-creation-failed" signal is PRIVATE for libnm-glib and
+        * is not meant for any external usage.  It indicates that an error
+        * occured during creation of an object.
+        */
+       void (*object_creation_failed) (NMObject *master_object,
+                                       GError *error,
+                                       char *failed_path);
+
        /* Padding for future expansion */
        void (*_reserved1) (void);
        void (*_reserved2) (void);