device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-object.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301 USA.
17  *
18  * Copyright 2007 - 2008 Novell, Inc.
19  * Copyright 2007 - 2012 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include "nm-object.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include "nm-utils.h"
31 #include "nm-dbus-interface.h"
32 #include "nm-object-cache.h"
33 #include "nm-object-private.h"
34 #include "nm-dbus-helpers.h"
35 #include "nm-client.h"
36 #include "nm-core-internal.h"
37
38 static gboolean debug = FALSE;
39 #define dbgmsg(f,...) if (G_UNLIKELY (debug)) { g_message (f, ## __VA_ARGS__ ); }
40
41 static void nm_object_initable_iface_init (GInitableIface *iface);
42 static void nm_object_async_initable_iface_init (GAsyncInitableIface *iface);
43
44 typedef struct {
45         NMObjectDecideTypeFunc type_func;
46         char *interface;
47         char *property;
48 } NMObjectTypeFuncData;
49
50 static GHashTable *type_funcs;
51
52 typedef struct {
53         GSList *interfaces;
54 } NMObjectClassPrivate;
55
56 #define NM_OBJECT_CLASS_GET_PRIVATE(k) (G_TYPE_CLASS_GET_PRIVATE ((k), NM_TYPE_OBJECT, NMObjectClassPrivate))
57
58 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (NMObject, nm_object, G_TYPE_OBJECT,
59                                   type_funcs = g_hash_table_new (NULL, NULL);
60                                   g_type_add_class_private (g_define_type_id, sizeof (NMObjectClassPrivate));
61                                   G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_object_initable_iface_init);
62                                   G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_object_async_initable_iface_init);
63                                   )
64
65 #define NM_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_OBJECT, NMObjectPrivate))
66
67 typedef struct {
68         PropertyMarshalFunc func;
69         GType object_type;
70         gpointer field;
71         const char *signal_prefix;
72 } PropertyInfo;
73
74 static void reload_complete (NMObject *object, gboolean emit_now);
75 static gboolean demarshal_generic (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field);
76
77 typedef struct {
78         GDBusConnection *connection;
79         gboolean nm_running;
80
81         char *path;
82         GHashTable *proxies;
83         GDBusProxy *properties_proxy;
84         GSList *property_tables;
85         NMObject *parent;
86         gboolean suppress_property_updates;
87
88         gboolean inited;        /* async init finished? */
89         GSList *waiters;        /* if async init did not finish, users of this object need
90                                  * to defer their notifications by adding themselves here. */
91
92         GSList *notify_items;
93         guint32 notify_id;
94
95         GSList *reload_results;
96         guint reload_remaining;
97         GError *reload_error;
98 } NMObjectPrivate;
99
100 enum {
101         PROP_0,
102         PROP_PATH,
103         PROP_DBUS_CONNECTION,
104         PROP_NM_RUNNING,
105
106         LAST_PROP
107 };
108
109 /**
110  * _nm_object_class_add_interface:
111  * @object_class: an #NMObjectClass
112  * @interface: a D-Bus interface name
113  *
114  * Registers that @object_class implements @interface. A proxy for that
115  * interface will automatically be created at construction time, and can
116  * be retrieved with _nm_object_get_proxy().
117  */
118 void
119 _nm_object_class_add_interface (NMObjectClass *object_class,
120                                 const char    *interface)
121 {
122         NMObjectClassPrivate *cpriv;
123
124         g_return_if_fail (NM_IS_OBJECT_CLASS (object_class));
125         g_return_if_fail (interface);
126
127         cpriv = NM_OBJECT_CLASS_GET_PRIVATE (object_class);
128
129         g_return_if_fail (g_slist_find_custom (cpriv->interfaces, interface, (GCompareFunc) g_strcmp0) == NULL);
130
131         cpriv->interfaces = g_slist_prepend (cpriv->interfaces, g_strdup (interface));
132 }
133
134 /**
135  * nm_object_get_path:
136  * @object: a #NMObject
137  *
138  * Gets the DBus path of the #NMObject.
139  *
140  * Returns: the object's path. This is the internal string used by the
141  * device, and must not be modified.
142  **/
143 const char *
144 nm_object_get_path (NMObject *object)
145 {
146         g_return_val_if_fail (NM_IS_OBJECT (object), NULL);
147
148         return NM_OBJECT_GET_PRIVATE (object)->path;
149 }
150
151 /**
152  * _nm_object_get_proxy:
153  * @object: an #NMObject
154  * @interface: a D-Bus interface implemented by @object
155  *
156  * Gets the D-Bus proxy for @interface on @object.
157  *
158  * Returns: (transfer none): a D-Bus proxy
159  */
160 GDBusProxy *
161 _nm_object_get_proxy (NMObject   *object,
162                       const char *interface)
163 {
164         GDBusProxy *proxy;
165
166         g_return_val_if_fail (NM_IS_OBJECT (object), NULL);
167
168         proxy = g_hash_table_lookup (NM_OBJECT_GET_PRIVATE (object)->proxies, interface);
169         g_return_val_if_fail (proxy != NULL, NULL);
170         return proxy;
171 }
172
173 typedef enum {
174         NOTIFY_SIGNAL_PENDING_NONE,
175         NOTIFY_SIGNAL_PENDING_ADDED,
176         NOTIFY_SIGNAL_PENDING_REMOVED,
177         NOTIFY_SIGNAL_PENDING_ADDED_REMOVED,
178 } NotifySignalPending;
179
180 typedef struct {
181         const char *property;
182         const char *signal_prefix;
183         NotifySignalPending pending;
184         NMObject *changed;
185 } NotifyItem;
186
187 static void
188 notify_item_free (NotifyItem *item)
189 {
190         g_clear_object (&item->changed);
191         g_slice_free (NotifyItem, item);
192 }
193
194 static gboolean
195 deferred_notify_cb (gpointer data)
196 {
197         NMObject *object = NM_OBJECT (data);
198         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
199         NMObjectClass *object_class = NM_OBJECT_GET_CLASS (object);
200         GSList *props, *iter;
201
202         priv->notify_id = 0;
203
204         /* Wait until all reloads are done before notifying */
205         if (priv->reload_remaining)
206                 return G_SOURCE_REMOVE;
207
208         /* Clear priv->notify_items early so that an NMObject subclass that
209          * listens to property changes can queue up other property changes
210          * during the g_object_notify() call separately from the property
211          * list we're iterating.
212          */
213         props = g_slist_reverse (priv->notify_items);
214         priv->notify_items = NULL;
215
216         g_object_ref (object);
217
218         /* Emit added/removed signals first since some of our internal objects
219          * use the added/removed signals for new object processing.
220          */
221         for (iter = props; iter; iter = g_slist_next (iter)) {
222                 NotifyItem *item = iter->data;
223                 char buf[50];
224                 gint ret = 0;
225
226                 switch (item->pending) {
227                 case NOTIFY_SIGNAL_PENDING_ADDED:
228                         ret = g_snprintf (buf, sizeof (buf), "%s-added", item->signal_prefix);
229                         break;
230                 case NOTIFY_SIGNAL_PENDING_REMOVED:
231                         ret = g_snprintf (buf, sizeof (buf), "%s-removed", item->signal_prefix);
232                         break;
233                 case NOTIFY_SIGNAL_PENDING_ADDED_REMOVED:
234                         if (object_class->object_creation_failed)
235                                 object_class->object_creation_failed (object, nm_object_get_path (item->changed));
236                         break;
237                 case NOTIFY_SIGNAL_PENDING_NONE:
238                 default:
239                         break;
240                 }
241                 if (ret > 0) {
242                         g_assert (ret < sizeof (buf));
243                         g_signal_emit_by_name (object, buf, item->changed);
244                 }
245         }
246
247         /* Emit property change notifications second */
248         for (iter = props; iter; iter = g_slist_next (iter)) {
249                 NotifyItem *item = iter->data;
250
251                 if (item->property)
252                         g_object_notify (G_OBJECT (object), item->property);
253         }
254
255         g_object_unref (object);
256
257         g_slist_free_full (props, (GDestroyNotify) notify_item_free);
258         return G_SOURCE_REMOVE;
259 }
260
261 static void
262 _nm_object_defer_notify (NMObject *object)
263 {
264         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
265
266         if (!priv->notify_id)
267                 priv->notify_id = g_idle_add_full (G_PRIORITY_LOW, deferred_notify_cb, object, NULL);
268 }
269
270 static void
271 _nm_object_queue_notify_full (NMObject *object,
272                               const char *property,
273                               const char *signal_prefix,
274                               gboolean added,
275                               NMObject *changed)
276 {
277         NMObjectPrivate *priv;
278         NotifyItem *item;
279         GSList *iter;
280
281         g_return_if_fail (NM_IS_OBJECT (object));
282         g_return_if_fail (!signal_prefix != !property);
283         g_return_if_fail (!signal_prefix == !changed);
284
285         priv = NM_OBJECT_GET_PRIVATE (object);
286         _nm_object_defer_notify (object);
287
288         property = g_intern_string (property);
289         signal_prefix = g_intern_string (signal_prefix);
290         for (iter = priv->notify_items; iter; iter = g_slist_next (iter)) {
291                 item = iter->data;
292
293                 if (property && (property == item->property))
294                         return;
295
296                 /* Collapse signals for the same object (such as "added->removed") to
297                  * ensure we don't emit signals when their sum should have no effect.
298                  * The "added->removed->removed" sequence requires special handling,
299                  * hence the addition of the ADDED_REMOVED state to ensure that no
300                  * signal is emitted in this case:
301                  *
302                  * Without the ADDED_REMOVED state:
303                  *     NONE          + added   -> ADDED
304                  *     ADDED         + removed -> NONE
305                  *     NONE          + removed -> REMOVED (would emit 'removed' signal)
306                  *
307                  * With the ADDED_REMOVED state:
308                  *     NONE | ADDED_REMOVED  + added   -> ADDED
309                  *     ADDED                 + removed -> ADDED_REMOVED
310                  *     ADDED_REMOVED         + removed -> ADDED_REMOVED (emits no signal)
311                  */
312                 if (signal_prefix && (changed == item->changed) && (item->signal_prefix == signal_prefix)) {
313                         switch (item->pending) {
314                         case NOTIFY_SIGNAL_PENDING_ADDED:
315                                 if (!added)
316                                         item->pending = NOTIFY_SIGNAL_PENDING_ADDED_REMOVED;
317                                 break;
318                         case NOTIFY_SIGNAL_PENDING_REMOVED:
319                                 if (added)
320                                         item->pending = NOTIFY_SIGNAL_PENDING_NONE;
321                                 break;
322                         case NOTIFY_SIGNAL_PENDING_ADDED_REMOVED:
323                                 if (added)
324                                         item->pending = NOTIFY_SIGNAL_PENDING_ADDED;
325                                 break;
326                         case NOTIFY_SIGNAL_PENDING_NONE:
327                                 item->pending = added ? NOTIFY_SIGNAL_PENDING_ADDED : NOTIFY_SIGNAL_PENDING_REMOVED;
328                                 break;
329                         default:
330                                 g_assert_not_reached ();
331                         }
332                         return;
333                 }
334         }
335
336         item = g_slice_new0 (NotifyItem);
337         item->property = property;
338         if (signal_prefix) {
339                 item->signal_prefix = signal_prefix;
340                 item->pending = added ? NOTIFY_SIGNAL_PENDING_ADDED : NOTIFY_SIGNAL_PENDING_REMOVED;
341                 item->changed = changed ? g_object_ref (changed) : NULL;
342         }
343         priv->notify_items = g_slist_prepend (priv->notify_items, item);
344 }
345
346 void
347 _nm_object_queue_notify (NMObject *object, const char *property)
348 {
349         _nm_object_queue_notify_full (object, property, NULL, FALSE, NULL);
350 }
351
352 void
353 _nm_object_register_type_func (GType base_type,
354                                NMObjectDecideTypeFunc type_func,
355                                const char *interface,
356                                const char *property)
357 {
358         NMObjectTypeFuncData *type_data;
359
360         g_return_if_fail (type_func != NULL);
361         g_return_if_fail (interface != NULL);
362         g_return_if_fail (property != NULL);
363
364         type_data = g_slice_new (NMObjectTypeFuncData);
365         type_data->type_func = type_func;
366         type_data->interface = g_strdup (interface);
367         type_data->property = g_strdup (property);
368
369         g_hash_table_insert (type_funcs,
370                              GSIZE_TO_POINTER (base_type),
371                              type_data);
372 }
373
374 static GObject *
375 _nm_object_create (GType type, GDBusConnection *connection, const char *path)
376 {
377         NMObjectTypeFuncData *type_data;
378         GObject *object;
379         GError *error = NULL;
380
381         type_data = g_hash_table_lookup (type_funcs, GSIZE_TO_POINTER (type));
382         if (type_data) {
383                 GDBusProxy *proxy;
384                 GVariant *ret, *value;
385
386                 proxy = _nm_dbus_new_proxy_for_connection (connection, path,
387                                                            DBUS_INTERFACE_PROPERTIES,
388                                                            NULL, &error);
389                 if (!proxy) {
390                         g_warning ("Could not create proxy for %s: %s.", path, error->message);
391                         g_error_free (error);
392                         return NULL;
393                 }
394
395                 ret = g_dbus_proxy_call_sync (proxy,
396                                               "Get",
397                                               g_variant_new ("(ss)",
398                                                              type_data->interface,
399                                                              type_data->property),
400                                               G_DBUS_CALL_FLAGS_NONE, -1,
401                                               NULL, &error);
402                 g_object_unref (proxy);
403                 if (!ret) {
404                         dbgmsg ("Could not fetch property '%s' of interface '%s' on %s: %s\n",
405                                    type_data->property, type_data->interface, path, error->message);
406                         g_error_free (error);
407                         return NULL;
408                 }
409
410                 g_variant_get (ret, "(v)", &value);
411                 type = type_data->type_func (value);
412                 g_variant_unref (value);
413                 g_variant_unref (ret);
414         }
415
416         if (type == G_TYPE_INVALID) {
417                 dbgmsg ("Could not create object for %s: unknown object type", path);
418                 return NULL;
419         }
420
421         object = g_object_new (type,
422                                NM_OBJECT_PATH, path,
423                                NM_OBJECT_DBUS_CONNECTION, connection,
424                                NULL);
425         /* Cache the object before initializing it (and in particular, loading its
426          * property values); this is necessary to make circular references work (eg,
427          * when creating an NMActiveConnection, it will create an NMDevice which
428          * will in turn try to create the parent NMActiveConnection). Since we don't
429          * support multi-threaded use, we know that we will have inited the object
430          * before any external code sees it.
431          */
432         _nm_object_cache_add (NM_OBJECT (object));
433         NM_OBJECT_GET_PRIVATE (object)->inited = TRUE;
434         if (!g_initable_init (G_INITABLE (object), NULL, &error)) {
435                 dbgmsg ("Could not create object for %s: %s", path, error->message);
436                 g_error_free (error);
437                 g_clear_object (&object);
438         }
439
440         return object;
441 }
442
443 typedef struct {
444         NMObject *self;
445         PropertyInfo *pi;
446
447         GObject **objects;
448         int length, remaining;
449
450         GPtrArray *array;
451         const char *property_name;
452 } ObjectCreatedData;
453
454 static void
455 odata_free (gpointer data)
456 {
457         ObjectCreatedData *odata = data;
458
459         g_object_unref (odata->self);
460         g_free (odata->objects);
461         if (odata->array)
462                 g_ptr_array_unref (odata->array);
463         g_slice_free (ObjectCreatedData, odata);
464 }
465
466 static void object_property_maybe_complete (ObjectCreatedData *odata);
467
468
469 typedef void (*NMObjectCreateCallbackFunc) (GObject *, const char *, gpointer);
470 typedef struct {
471         char *path;
472         NMObjectCreateCallbackFunc callback;
473         gpointer user_data;
474         NMObjectTypeFuncData *type_data;
475         GDBusConnection *connection;
476 } NMObjectTypeAsyncData;
477
478 static void
479 create_async_complete (GObject *object, NMObjectTypeAsyncData *async_data)
480 {
481         async_data->callback (object, async_data->path, async_data->user_data);
482
483         g_free (async_data->path);
484         g_object_unref (async_data->connection);
485         g_slice_free (NMObjectTypeAsyncData, async_data);
486 }
487
488 static void
489 create_async_inited (GObject *object, GAsyncResult *result, gpointer user_data)
490 {
491         NMObjectTypeAsyncData *async_data = user_data;
492         GError *error = NULL;
493
494         NM_OBJECT_GET_PRIVATE (object)->inited = TRUE;
495         if (!g_async_initable_init_finish (G_ASYNC_INITABLE (object), result, &error)) {
496                 dbgmsg ("Could not create object for %s: %s",
497                         nm_object_get_path (NM_OBJECT (object)),
498                         error->message);
499                 g_error_free (error);
500                 g_clear_object (&object);
501         }
502
503         create_async_complete (object, async_data);
504
505         if (object) {
506                 NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
507
508                 /* There are some object properties whose creation couldn't proceed
509                  * because it depended on this object. */
510                 while (priv->waiters) {
511                         ObjectCreatedData *odata = priv->waiters->data;
512
513                         priv->waiters = g_slist_remove (priv->waiters, odata);
514                         object_property_maybe_complete (odata);
515                 }
516         }
517 }
518
519 static void
520 create_async_got_type (NMObjectTypeAsyncData *async_data, GType type)
521 {
522         GObject *object;
523
524         /* Ensure we don't have the object already; we may get multiple type
525          * requests for the same object if there are multiple properties on
526          * other objects that refer to the object at this path.  One of those
527          * other requests may have already completed.
528          */
529         object = (GObject *) _nm_object_cache_get (async_data->path);
530         if (object) {
531                 create_async_complete (object, async_data);
532                 return;
533         }
534
535         if (type == G_TYPE_INVALID) {
536                 /* Don't know how to create this object */
537                 create_async_complete (NULL, async_data);
538                 return;
539         }
540
541         object = g_object_new (type,
542                                NM_OBJECT_PATH, async_data->path,
543                                NM_OBJECT_DBUS_CONNECTION, async_data->connection,
544                                NULL);
545         _nm_object_cache_add (NM_OBJECT (object));
546         g_async_initable_init_async (G_ASYNC_INITABLE (object), G_PRIORITY_DEFAULT,
547                                      NULL, create_async_inited, async_data);
548 }
549
550 static void
551 create_async_got_property (GObject *proxy, GAsyncResult *result, gpointer user_data)
552 {
553         NMObjectTypeAsyncData *async_data = user_data;
554         NMObjectTypeFuncData *type_data = async_data->type_data;
555         GVariant *ret, *value;
556         GError *error = NULL;
557         GType type;
558
559         ret = _nm_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), result,
560                                           G_VARIANT_TYPE ("(v)"), &error);
561         if (ret) {
562                 g_variant_get (ret, "(v)", &value);
563                 type = type_data->type_func (value);
564                 g_variant_unref (value);
565                 g_variant_unref (ret);
566         } else {
567                 dbgmsg ("Could not fetch property '%s' of interface '%s' on %s: %s\n",
568                         type_data->property, type_data->interface, async_data->path,
569                         error->message);
570                 g_clear_error (&error);
571                 type = G_TYPE_INVALID;
572         }
573
574         create_async_got_type (async_data, type);
575 }
576
577 static void
578 create_async_got_proxy (GObject *object, GAsyncResult *result, gpointer user_data)
579 {
580         NMObjectTypeAsyncData *async_data = user_data;
581         GDBusProxy *proxy;
582         GError *error = NULL;
583
584         proxy = _nm_dbus_new_proxy_for_connection_finish (result, &error);
585         if (!proxy) {
586                 g_warning ("Could not create proxy for %s: %s.", async_data->path, error->message);
587                 g_error_free (error);
588                 create_async_complete (NULL, async_data);
589                 return;
590         }
591
592         g_dbus_proxy_call (proxy,
593                            "Get",
594                            g_variant_new ("(ss)",
595                                           async_data->type_data->interface,
596                                           async_data->type_data->property),
597                            G_DBUS_CALL_FLAGS_NONE, -1,
598                            NULL,
599                            create_async_got_property, async_data);
600 }
601
602 static void
603 _nm_object_create_async (GType type, GDBusConnection *connection, const char *path,
604                          NMObjectCreateCallbackFunc callback, gpointer user_data)
605 {
606         NMObjectTypeAsyncData *async_data;
607
608         async_data = g_slice_new (NMObjectTypeAsyncData);
609         async_data->path = g_strdup (path);
610         async_data->callback = callback;
611         async_data->user_data = user_data;
612         async_data->connection = g_object_ref (connection);
613
614         async_data->type_data = g_hash_table_lookup (type_funcs, GSIZE_TO_POINTER (type));
615         if (async_data->type_data) {
616                 _nm_dbus_new_proxy_for_connection_async (connection, path,
617                                                          DBUS_INTERFACE_PROPERTIES,
618                                                          NULL,
619                                                          create_async_got_proxy, async_data);
620                 return;
621         }
622
623         create_async_got_type (async_data, type);
624 }
625
626 /* Stolen from dbus-glib */
627 static char*
628 wincaps_to_dash (const char *caps)
629 {
630         const char *p;
631         GString *str;
632
633         str = g_string_new (NULL);
634         p = caps;
635         while (*p) {
636                 if (g_ascii_isupper (*p)) {
637                         if (str->len > 0 && (str->len < 2 || str->str[str->len-2] != '-'))
638                                 g_string_append_c (str, '-');
639                         g_string_append_c (str, g_ascii_tolower (*p));
640                 } else
641                         g_string_append_c (str, *p);
642                 ++p;
643         }
644
645         return g_string_free (str, FALSE);
646 }
647
648 /* Adds object to array if it's not already there */
649 static void
650 add_to_object_array_unique (GPtrArray *array, GObject *obj)
651 {
652         guint i;
653
654         g_return_if_fail (array != NULL);
655
656         if (obj != NULL) {
657                 for (i = 0; i < array->len; i++) {
658                         if (g_ptr_array_index (array, i) == obj) {
659                                 g_object_unref (obj);
660                                 return;
661                         }
662                 }
663                 g_ptr_array_add (array, obj);
664         }
665 }
666
667 /* Places items from 'needles' that are not in 'haystack' into 'diff' */
668 static void
669 array_diff (GPtrArray *needles, GPtrArray *haystack, GPtrArray *diff)
670 {
671         guint i, j;
672         GObject *obj;
673
674         g_assert (needles);
675         g_assert (haystack);
676         g_assert (diff);
677
678         for (i = 0; i < needles->len; i++) {
679                 obj = g_ptr_array_index (needles, i);
680
681                 for (j = 0; j < haystack->len; j++) {
682                         if (g_ptr_array_index (haystack, j) == obj)
683                                 break;
684                 }
685
686                 if (j == haystack->len)
687                         g_ptr_array_add (diff, obj);
688         }
689 }
690
691 static void
692 queue_added_removed_signal (NMObject *self,
693                             const char *signal_prefix,
694                             NMObject *changed,
695                             gboolean added)
696 {
697         _nm_object_queue_notify_full (self, NULL, signal_prefix, added, changed);
698 }
699
700 static gboolean
701 already_awaits (ObjectCreatedData *odata, GObject *object)
702 {
703         NMObject *self = odata->self;
704         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
705         GSList *iter;
706
707         if ((GObject *)odata->self == object)
708                 return TRUE;
709
710         for (iter = priv->waiters; iter; iter = g_slist_next (iter)) {
711                 if (already_awaits (iter->data, object))
712                         return TRUE;
713         }
714
715         return FALSE;
716 }
717
718 static void
719 object_property_maybe_complete (ObjectCreatedData *odata)
720 {
721         NMObject *self = odata->self;
722         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
723         PropertyInfo *pi = odata->pi;
724         gboolean different = TRUE;
725         int i;
726
727         /* Only complete the array property load when all the objects are initialized. */
728         for (i = 0; i < odata->length; i++) {
729                 GObject *obj = odata->objects[i];
730                 NMObjectPrivate *obj_priv;
731
732                 /* Could not load the object. Perhaps it was removed. */
733                 if (!obj)
734                         continue;
735
736                 obj_priv = NM_OBJECT_GET_PRIVATE (obj);
737                 if (!obj_priv->inited) {
738
739                         /* The object is not finished because we block its creation. */
740                         if (already_awaits (odata, obj))
741                                 continue;
742
743                         if (!g_slist_find (obj_priv->waiters, odata))
744                                 obj_priv->waiters = g_slist_prepend (obj_priv->waiters, odata);
745                         return;
746                 }
747         }
748
749         if (odata->array) {
750                 GPtrArray *pi_old = *((GPtrArray **) pi->field);
751                 GPtrArray *old = odata->array;
752                 GPtrArray *new;
753
754                 /* Build up new array */
755                 new = g_ptr_array_new_full (odata->length, g_object_unref);
756                 for (i = 0; i < odata->length; i++)
757                         add_to_object_array_unique (new, odata->objects[i]);
758
759                 *((GPtrArray **) pi->field) = new;
760
761                 if (pi->signal_prefix) {
762                         GPtrArray *added = g_ptr_array_sized_new (3);
763                         GPtrArray *removed = g_ptr_array_sized_new (3);
764
765                         /* Find objects in 'old' that do not exist in 'new' */
766                         array_diff (old, new, removed);
767
768                         /* Find objects in 'new' that do not exist in old */
769                         array_diff (new, old, added);
770
771                         /* Emit added & removed */
772                         for (i = 0; i < removed->len; i++) {
773                                 queue_added_removed_signal (self,
774                                                             pi->signal_prefix,
775                                                             g_ptr_array_index (removed, i),
776                                                             FALSE);
777                         }
778
779                         for (i = 0; i < added->len; i++) {
780                                 queue_added_removed_signal (self,
781                                                             pi->signal_prefix,
782                                                             g_ptr_array_index (added, i),
783                                                             TRUE);
784                         }
785
786                         different = removed->len || added->len;
787                         g_ptr_array_unref (added);
788                         g_ptr_array_unref (removed);
789                 } else {
790                         /* No added/removed signals to send, just replace the property with
791                          * the new values.
792                          */
793                         different = TRUE;
794                 }
795
796                 /* Free old array last since it will release references, thus freeing
797                  * any objects in the 'removed' array.
798                  */
799                 if (pi_old)
800                         g_ptr_array_unref (pi_old);
801         } else {
802                 GObject **obj_p = pi->field;
803
804                 different = (*obj_p != odata->objects[0]);
805                 if (*obj_p)
806                         g_object_unref (*obj_p);
807                 *obj_p = odata->objects[0];
808         }
809
810         if (different && odata->property_name)
811                 _nm_object_queue_notify (self, odata->property_name);
812
813         if (--priv->reload_remaining == 0)
814                 reload_complete (self, FALSE);
815
816         odata_free (odata);
817 }
818
819 static void
820 object_created (GObject *obj, const char *path, gpointer user_data)
821 {
822         ObjectCreatedData *odata = user_data;
823
824         /* We assume that on error, the creator_func printed something */
825
826         if (obj == NULL && g_strcmp0 (path, "/") != 0 ) {
827                 NMObjectClass *object_class = NM_OBJECT_GET_CLASS (odata->self);
828
829                 if (object_class->object_creation_failed)
830                         object_class->object_creation_failed (odata->self, path);
831         }
832
833         odata->objects[--odata->remaining] = obj;
834         if (!odata->remaining)
835                 object_property_maybe_complete (odata);
836 }
837
838 static gboolean
839 handle_object_property (NMObject *self, const char *property_name, GVariant *value,
840                         PropertyInfo *pi, gboolean synchronously)
841 {
842         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
843         GObject *obj;
844         const char *path;
845         ObjectCreatedData *odata;
846
847         odata = g_slice_new (ObjectCreatedData);
848         odata->self = g_object_ref (self);
849         odata->pi = pi;
850         odata->objects = g_new (GObject *, 1);
851         odata->length = odata->remaining = 1;
852         odata->array = NULL;
853         odata->property_name = property_name;
854
855         priv->reload_remaining++;
856
857         path = g_variant_get_string (value, NULL);
858
859         if (!strcmp (path, "/")) {
860                 object_created (NULL, path, odata);
861                 return TRUE;
862         }
863
864         obj = G_OBJECT (_nm_object_cache_get (path));
865         if (obj) {
866                 object_created (obj, path, odata);
867                 return TRUE;
868         } else if (synchronously) {
869                 obj = _nm_object_create (pi->object_type, priv->connection, path);
870                 object_created (obj, path, odata);
871                 return obj != NULL;
872         } else {
873                 _nm_object_create_async (pi->object_type, priv->connection, path,
874                                          object_created, odata);
875                 /* Assume success */
876                 return TRUE;
877         }
878 }
879
880 static gboolean
881 handle_object_array_property (NMObject *self, const char *property_name, GVariant *value,
882                               PropertyInfo *pi, gboolean synchronously)
883 {
884         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
885         GObject *obj;
886         GVariantIter iter;
887         gsize npaths;
888         GPtrArray **array = pi->field;
889         const char *path;
890         ObjectCreatedData *odata;
891         guint i, len = *array ? (*array)->len : 0;
892
893         npaths = g_variant_n_children (value);
894
895         odata = g_slice_new (ObjectCreatedData);
896         odata->self = g_object_ref (self);
897         odata->pi = pi;
898         odata->objects = g_new0 (GObject *, npaths);
899         odata->length = odata->remaining = npaths;
900         odata->property_name = property_name;
901
902         /* Objects known at this point. */
903         odata->array = g_ptr_array_new_full (len, g_object_unref);
904         for (i = 0; i < len; i++)
905                 g_ptr_array_add (odata->array, g_object_ref (g_ptr_array_index (*array, i)));
906
907         priv->reload_remaining++;
908
909         if (npaths == 0) {
910                 object_property_maybe_complete (odata);
911                 return TRUE;
912         }
913
914         g_variant_iter_init (&iter, value);
915         while (g_variant_iter_next (&iter, "&o", &path)) {
916                 if (!strcmp (path, "/")) {
917                         /* FIXME: can't happen? */
918                         continue;
919                 }
920
921                 obj = G_OBJECT (_nm_object_cache_get (path));
922                 if (obj) {
923                         object_created (obj, path, odata);
924                 } else if (synchronously) {
925                         obj = _nm_object_create (pi->object_type, priv->connection, path);
926                         object_created (obj, path, odata);
927                 } else {
928                         _nm_object_create_async (pi->object_type, priv->connection, path,
929                                                  object_created, odata);
930                 }
931         }
932
933         if (!synchronously) {
934                 /* Assume success */
935                 return TRUE;
936         }
937
938         return *array && ((*array)->len == npaths);
939 }
940
941 static void
942 handle_property_changed (NMObject *self, const char *dbus_name,
943                          GVariant *value, gboolean synchronously)
944 {
945         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
946         char *prop_name;
947         PropertyInfo *pi;
948         GParamSpec *pspec;
949         gboolean success = FALSE, found = FALSE;
950         GSList *iter;
951
952         prop_name = wincaps_to_dash (dbus_name);
953
954         /* Iterate through the object and its parents to find the property */
955         for (iter = priv->property_tables; iter; iter = g_slist_next (iter)) {
956                 pi = g_hash_table_lookup ((GHashTable *) iter->data, prop_name);
957                 if (pi) {
958                         if (!pi->field) {
959                                 /* We know about this property but aren't tracking changes on it. */
960                                 goto out;
961                         }
962
963                         found = TRUE;
964                         break;
965                 }
966         }
967
968         if (!found) {
969                 dbgmsg ("Property '%s' unhandled.", prop_name);
970                 goto out;
971         }
972
973         pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (self)), prop_name);
974         if (!pspec && pi->func == demarshal_generic) {
975                 dbgmsg ("%s: property '%s' changed but wasn't defined by object type %s.",
976                         __func__,
977                         prop_name,
978                         G_OBJECT_TYPE_NAME (self));
979                 goto out;
980         }
981
982         if (G_UNLIKELY (debug)) {
983                 char *s;
984                 s = g_variant_print (value, FALSE);
985                 dbgmsg ("PC: (%p) %s:%s => '%s' (%s%s%s)",
986                         self, G_OBJECT_TYPE_NAME (self),
987                         prop_name,
988                         s,
989                         g_variant_get_type_string (value),
990                         pi->object_type ? " / " : "",
991                         pi->object_type ? g_type_name (pi->object_type) : "");
992                 g_free (s);
993         }
994
995         if (pspec && pi->object_type) {
996                 if (g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
997                         success = handle_object_property (self, pspec->name, value, pi, synchronously);
998                 else if (g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")))
999                         success = handle_object_array_property (self, pspec->name, value, pi, synchronously);
1000                 else {
1001                         g_warn_if_reached ();
1002                         goto out;
1003                 }
1004         } else
1005                 success = (*(pi->func)) (self, pspec, value, pi->field);
1006
1007         if (!success) {
1008                 dbgmsg ("%s: failed to update property '%s' of object type %s.",
1009                         __func__,
1010                         prop_name,
1011                         G_OBJECT_TYPE_NAME (self));
1012         }
1013
1014 out:
1015         g_free (prop_name);
1016 }
1017
1018 static void
1019 process_properties_changed (NMObject *self, GVariant *properties, gboolean synchronously)
1020 {
1021         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
1022         GVariantIter iter;
1023         const char *name;
1024         GVariant *value;
1025
1026         if (priv->suppress_property_updates)
1027                 return;
1028
1029         g_variant_iter_init (&iter, properties);
1030         while (g_variant_iter_next (&iter, "{&sv}", &name, &value)) {
1031                 handle_property_changed (self, name, value, synchronously);
1032                 g_variant_unref (value);
1033         }
1034 }
1035
1036 static void
1037 properties_changed (GDBusProxy *proxy,
1038                     GVariant   *properties,
1039                     gpointer    user_data)
1040 {
1041         process_properties_changed (NM_OBJECT (user_data), properties, FALSE);
1042 }
1043
1044 #define HANDLE_TYPE(vtype, ctype, getter) \
1045         G_STMT_START { \
1046                 if (g_variant_is_of_type (value, vtype)) { \
1047                         ctype *param = (ctype *) field; \
1048                         ctype newval = getter (value); \
1049                         different = *param != newval; \
1050                         *param = newval; \
1051                 } else { \
1052                         success = FALSE; \
1053                         goto done; \
1054                 } \
1055         } G_STMT_END
1056
1057 static gboolean
1058 demarshal_generic (NMObject *object,
1059                    GParamSpec *pspec,
1060                    GVariant *value,
1061                    gpointer field)
1062 {
1063         gboolean success = TRUE;
1064         gboolean different = FALSE;
1065
1066         if (pspec->value_type == G_TYPE_STRING) {
1067                 if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) {
1068                         char **param = (char **) field;
1069                         const char *newval = g_variant_get_string (value, NULL);
1070
1071                         different = !!g_strcmp0 (*param, newval);
1072                         if (different) {
1073                                 g_free (*param);
1074                                 *param = g_strdup (newval);
1075                         }
1076                 } else if (g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH)) {
1077                         char **param = (char **) field;
1078                         const char *newval = g_variant_get_string (value, NULL);
1079
1080                         /* Handle "NULL" object paths */
1081                         if (g_strcmp0 (newval, "/") == 0)
1082                                 newval = NULL;
1083                         different = !!g_strcmp0 (*param, newval);
1084                         if (different) {
1085                                 g_free (*param);
1086                                 *param = g_strdup (newval);
1087                         }
1088                 } else {
1089                         success = FALSE;
1090                         goto done;
1091                 }
1092         } else if (pspec->value_type == G_TYPE_STRV) {
1093                 char ***param = (char ***)field;
1094                 const char **newval;
1095                 gsize i;
1096
1097                 newval = g_variant_get_strv (value, NULL);
1098                 if (!*param)
1099                         different = TRUE;
1100                 else {
1101                         if (!_nm_utils_strv_equal ((char **) newval, *param)) {
1102                                 different = TRUE;
1103                                 g_strfreev (*param);
1104                         }
1105                 }
1106                 if (different) {
1107                         for (i = 0; newval[i]; i++)
1108                                 newval[i] = g_strdup (newval[i]);
1109                         *param = (char **) newval;
1110                 } else
1111                         g_free (newval);
1112         } else if (pspec->value_type == G_TYPE_BYTES) {
1113                 GBytes **param = (GBytes **)field;
1114                 gconstpointer val, old_val = NULL;
1115                 gsize length, old_length = 0;
1116
1117                 val = g_variant_get_fixed_array (value, &length, 1);
1118
1119                 if (*param)
1120                         old_val = g_bytes_get_data (*param, &old_length);
1121                 different =    old_length != length
1122                             || (   length > 0
1123                                 && memcmp (old_val, val, length) != 0);
1124                 if (different) {
1125                         if (*param)
1126                                 g_bytes_unref (*param);
1127                         *param = length > 0 ? g_bytes_new (val, length) : NULL;
1128                 }
1129         } else if (G_IS_PARAM_SPEC_ENUM (pspec)) {
1130                 int *param = (int *) field;
1131                 int newval = 0;
1132
1133                 if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32))
1134                         newval = g_variant_get_int32 (value);
1135                 else if (g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
1136                         newval = g_variant_get_uint32 (value);
1137                 else {
1138                         success = FALSE;
1139                         goto done;
1140                 }
1141                 different = *param != newval;
1142                 *param = newval;
1143         } else if (G_IS_PARAM_SPEC_FLAGS (pspec)) {
1144                 guint *param = (guint *) field;
1145                 guint newval = 0;
1146
1147                 if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32))
1148                         newval = g_variant_get_int32 (value);
1149                 else if (g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
1150                         newval = g_variant_get_uint32 (value);
1151                 else {
1152                         success = FALSE;
1153                         goto done;
1154                 }
1155                 different = *param != newval;
1156                 *param = newval;
1157         } else if (pspec->value_type == G_TYPE_BOOLEAN)
1158                 HANDLE_TYPE (G_VARIANT_TYPE_BOOLEAN, gboolean, g_variant_get_boolean);
1159         else if (pspec->value_type == G_TYPE_UCHAR)
1160                 HANDLE_TYPE (G_VARIANT_TYPE_BYTE, guchar, g_variant_get_byte);
1161         else if (pspec->value_type == G_TYPE_DOUBLE) {
1162                 NM_PRAGMA_WARNING_DISABLE("-Wfloat-equal")
1163                 HANDLE_TYPE (G_VARIANT_TYPE_DOUBLE, gdouble, g_variant_get_double);
1164                 NM_PRAGMA_WARNING_REENABLE
1165         } else if (pspec->value_type == G_TYPE_INT)
1166                 HANDLE_TYPE (G_VARIANT_TYPE_INT32, gint, g_variant_get_int32);
1167         else if (pspec->value_type == G_TYPE_UINT)
1168                 HANDLE_TYPE (G_VARIANT_TYPE_UINT32, guint, g_variant_get_uint32);
1169         else if (pspec->value_type == G_TYPE_INT64)
1170                 HANDLE_TYPE (G_VARIANT_TYPE_INT64, gint, g_variant_get_int64);
1171         else if (pspec->value_type == G_TYPE_UINT64)
1172                 HANDLE_TYPE (G_VARIANT_TYPE_UINT64, guint, g_variant_get_uint64);
1173         else if (pspec->value_type == G_TYPE_LONG)
1174                 HANDLE_TYPE (G_VARIANT_TYPE_INT64, glong, g_variant_get_int64);
1175         else if (pspec->value_type == G_TYPE_ULONG)
1176                 HANDLE_TYPE (G_VARIANT_TYPE_UINT64, gulong, g_variant_get_uint64);
1177         else {
1178                 g_warning ("%s: %s:%s unhandled type %s.",
1179                            __func__,
1180                            G_OBJECT_TYPE_NAME (object),
1181                            pspec->name,
1182                            g_type_name (pspec->value_type));
1183                 success = FALSE;
1184         }
1185
1186 done:
1187         if (success) {
1188                 if (different)
1189                         _nm_object_queue_notify (object, pspec->name);
1190         } else {
1191                 dbgmsg ("%s: %s:%s (type %s) couldn't be set from D-Bus type %s.",
1192                         __func__, G_OBJECT_TYPE_NAME (object), pspec->name,
1193                         g_type_name (pspec->value_type), g_variant_get_type_string (value));
1194         }
1195         return success;
1196 }
1197
1198 void
1199 _nm_object_register_properties (NMObject *object,
1200                                 const char *interface,
1201                                 const NMPropertiesInfo *info)
1202 {
1203         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1204         GDBusProxy *proxy;
1205         static gsize dval = 0;
1206         const char *debugstr;
1207         NMPropertiesInfo *tmp;
1208         GHashTable *instance;
1209
1210         g_return_if_fail (NM_IS_OBJECT (object));
1211         g_return_if_fail (interface != NULL);
1212         g_return_if_fail (info != NULL);
1213
1214         if (g_once_init_enter (&dval)) {
1215                 debugstr = getenv ("LIBNM_GLIB_DEBUG");
1216                 if (debugstr && strstr (debugstr, "properties-changed"))
1217                         debug = TRUE;
1218                 g_once_init_leave (&dval, 1);
1219         }
1220
1221         proxy = _nm_object_get_proxy (object, interface);
1222         g_return_if_fail (proxy != NULL);
1223
1224         _nm_dbus_signal_connect (proxy, "PropertiesChanged", G_VARIANT_TYPE ("(a{sv})"),
1225                                  G_CALLBACK (properties_changed), object);
1226
1227         instance = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1228         priv->property_tables = g_slist_prepend (priv->property_tables, instance);
1229
1230         for (tmp = (NMPropertiesInfo *) info; tmp->name; tmp++) {
1231                 PropertyInfo *pi;
1232
1233                 if (!tmp->name || (tmp->func && !tmp->field)) {
1234                         g_warning ("%s: missing field in NMPropertiesInfo", __func__);
1235                         continue;
1236                 }
1237
1238                 pi = g_malloc0 (sizeof (PropertyInfo));
1239                 pi->func = tmp->func ? tmp->func : demarshal_generic;
1240                 pi->object_type = tmp->object_type;
1241                 pi->field = tmp->field;
1242                 pi->signal_prefix = tmp->signal_prefix;
1243                 g_hash_table_insert (instance, g_strdup (tmp->name), pi);
1244         }
1245 }
1246
1247 static gboolean
1248 _nm_object_reload_properties (NMObject *object, GError **error)
1249 {
1250         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1251         GVariant *ret, *props;
1252         GHashTableIter iter;
1253         const char *interface;
1254         GDBusProxy *proxy;
1255
1256         if (!g_hash_table_size (priv->proxies) || !priv->nm_running)
1257                 return TRUE;
1258
1259         priv->reload_remaining++;
1260
1261         g_hash_table_iter_init (&iter, priv->proxies);
1262         while (g_hash_table_iter_next (&iter, (gpointer *) &interface, (gpointer *) &proxy)) {
1263                 ret = _nm_dbus_proxy_call_sync (priv->properties_proxy,
1264                                                 "GetAll",
1265                                                 g_variant_new ("(s)", interface),
1266                                                 G_VARIANT_TYPE ("(a{sv})"),
1267                                                 G_DBUS_CALL_FLAGS_NONE, -1,
1268                                                 NULL, error);
1269                 if (!ret) {
1270                         if (error && *error)
1271                                 g_dbus_error_strip_remote_error (*error);
1272                         return FALSE;
1273                 }
1274
1275                 g_variant_get (ret, "(@a{sv})", &props);
1276                 process_properties_changed (object, props, TRUE);
1277                 g_variant_unref (props);
1278                 g_variant_unref (ret);
1279         }
1280
1281         if (--priv->reload_remaining == 0)
1282                 reload_complete (object, TRUE);
1283
1284         return TRUE;
1285 }
1286
1287 void
1288 _nm_object_suppress_property_updates (NMObject *object, gboolean suppress)
1289 {
1290         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1291
1292         priv->suppress_property_updates = suppress;
1293 }
1294
1295
1296 void
1297 _nm_object_reload_property (NMObject *object,
1298                             const char *interface,
1299                             const char *prop_name)
1300 {
1301         GVariant *ret, *value;
1302         GError *err = NULL;
1303
1304         g_return_if_fail (NM_IS_OBJECT (object));
1305         g_return_if_fail (interface != NULL);
1306         g_return_if_fail (prop_name != NULL);
1307
1308         if (!NM_OBJECT_GET_PRIVATE (object)->nm_running)
1309                 return;
1310
1311         ret = _nm_dbus_proxy_call_sync (NM_OBJECT_GET_PRIVATE (object)->properties_proxy,
1312                                         "Get",
1313                                         g_variant_new ("(ss)", interface, prop_name),
1314                                         G_VARIANT_TYPE ("(v)"),
1315                                         G_DBUS_CALL_FLAGS_NONE, 15000,
1316                                         NULL, &err);
1317         if (!ret) {
1318                 dbgmsg ("%s: Error getting '%s' for %s: %s\n",
1319                         __func__,
1320                         prop_name,
1321                         nm_object_get_path (object),
1322                         err->message);
1323                 g_clear_error (&err);
1324                 return;
1325         }
1326
1327         g_variant_get (ret, "(v)", &value);
1328         handle_property_changed (object, prop_name, value, TRUE);
1329         g_variant_unref (value);
1330         g_variant_unref (ret);
1331 }
1332
1333 void
1334 _nm_object_set_property (NMObject *object,
1335                          const char *interface,
1336                          const char *prop_name,
1337                          const char *format_string,
1338                          ...)
1339 {
1340         GVariant *val, *ret;
1341         va_list ap;
1342
1343         g_return_if_fail (NM_IS_OBJECT (object));
1344         g_return_if_fail (interface != NULL);
1345         g_return_if_fail (prop_name != NULL);
1346         g_return_if_fail (format_string != NULL);
1347
1348         if (!NM_OBJECT_GET_PRIVATE (object)->nm_running)
1349                 return;
1350
1351         va_start (ap, format_string);
1352         val = g_variant_new_va (format_string, NULL, &ap);
1353         va_end (ap);
1354         g_return_if_fail (val != NULL);
1355
1356         ret = g_dbus_proxy_call_sync (NM_OBJECT_GET_PRIVATE (object)->properties_proxy,
1357                                       "Set",
1358                                       g_variant_new ("(ssv)", interface, prop_name, val),
1359                                       G_DBUS_CALL_FLAGS_NONE, 2000,
1360                                       NULL, NULL);
1361         /* Ignore errors. */
1362         if (ret)
1363                 g_variant_unref (ret);
1364 }
1365
1366 static void
1367 reload_complete (NMObject *object, gboolean emit_now)
1368 {
1369         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1370         GSimpleAsyncResult *simple;
1371         GSList *results, *iter;
1372         GError *error;
1373
1374         if (emit_now) {
1375                 nm_clear_g_source (&priv->notify_id);
1376                 deferred_notify_cb (object);
1377         } else
1378                 _nm_object_defer_notify (object);
1379
1380         results = priv->reload_results;
1381         priv->reload_results = NULL;
1382         error = priv->reload_error;
1383         priv->reload_error = NULL;
1384
1385         for (iter = results; iter; iter = iter->next) {
1386                 simple = iter->data;
1387
1388                 if (error)
1389                         g_simple_async_result_set_from_error (simple, error);
1390                 else
1391                         g_simple_async_result_set_op_res_gboolean (simple, TRUE);
1392
1393                 g_simple_async_result_complete (simple);
1394                 g_object_unref (simple);
1395         }
1396         g_slist_free (results);
1397         g_clear_error (&error);
1398 }
1399
1400 static void
1401 reload_got_properties (GObject *proxy,
1402                        GAsyncResult *result,
1403                        gpointer user_data)
1404 {
1405         NMObject *object = user_data;
1406         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1407         GVariant *ret, *props;
1408         GError *error = NULL;
1409
1410         ret = _nm_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), result,
1411                                           G_VARIANT_TYPE ("(a{sv})"),
1412                                           &error);
1413         if (ret) {
1414                 g_variant_get (ret, "(@a{sv})", &props);
1415                 process_properties_changed (object, props, FALSE);
1416                 g_variant_unref (props);
1417                 g_variant_unref (ret);
1418         } else {
1419                 g_dbus_error_strip_remote_error (error);
1420                 if (priv->reload_error)
1421                         g_error_free (error);
1422                 else
1423                         priv->reload_error = error;
1424         }
1425
1426         if (--priv->reload_remaining == 0)
1427                 reload_complete (object, FALSE);
1428 }
1429
1430 void
1431 _nm_object_reload_properties_async (NMObject *object,
1432                                     GCancellable *cancellable,
1433                                     GAsyncReadyCallback callback,
1434                                     gpointer user_data)
1435 {
1436         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1437         GSimpleAsyncResult *simple;
1438         GHashTableIter iter;
1439         const char *interface;
1440         GDBusProxy *proxy;
1441
1442         simple = g_simple_async_result_new (G_OBJECT (object), callback,
1443                                             user_data, _nm_object_reload_properties_async);
1444
1445         if (!g_hash_table_size (priv->proxies) || !priv->nm_running) {
1446                 g_simple_async_result_complete_in_idle (simple);
1447                 g_object_unref (simple);
1448                 return;
1449         }
1450
1451         priv->reload_results = g_slist_prepend (priv->reload_results, simple);
1452
1453         /* If there was already a reload happening, we don't need to
1454          * re-read the properties again, we just need to wait for the
1455          * existing reload to finish.
1456          */
1457         if (priv->reload_results->next)
1458                 return;
1459
1460         g_hash_table_iter_init (&iter, priv->proxies);
1461         while (g_hash_table_iter_next (&iter, (gpointer *) &interface, (gpointer *) &proxy)) {
1462                 priv->reload_remaining++;
1463                 g_dbus_proxy_call (priv->properties_proxy,
1464                                    "GetAll",
1465                                    g_variant_new ("(s)", interface),
1466                                    G_DBUS_CALL_FLAGS_NONE, -1,
1467                                    cancellable,
1468                                    reload_got_properties, object);
1469         }
1470 }
1471
1472 gboolean
1473 _nm_object_reload_properties_finish (NMObject *object, GAsyncResult *result, GError **error)
1474 {
1475         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1476         GSimpleAsyncResult *simple;
1477
1478         g_return_val_if_fail (NM_IS_OBJECT (object), FALSE);
1479         g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (object), _nm_object_reload_properties_async), FALSE);
1480
1481         /* NM might have disappeared meanwhile. That would cause a NoReply error to be emitted,
1482          * but we don't care if property updates were disabled. */
1483         if (priv->suppress_property_updates)
1484                 return TRUE;
1485
1486         simple = G_SIMPLE_ASYNC_RESULT (result);
1487         if (g_simple_async_result_propagate_error (simple, error))
1488                 return FALSE;
1489
1490         return g_simple_async_result_get_op_res_gboolean (simple);
1491 }
1492
1493 gboolean
1494 _nm_object_get_nm_running (NMObject *self)
1495 {
1496         return NM_OBJECT_GET_PRIVATE (self)->nm_running;
1497 }
1498
1499 /**************************************************************/
1500
1501 static void
1502 on_name_owner_changed (GObject    *proxy,
1503                        GParamSpec *pspec,
1504                        gpointer    user_data)
1505 {
1506         NMObject *self = NM_OBJECT (user_data);
1507         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
1508         gboolean now_running;
1509         char *owner;
1510
1511         now_running = ((owner = g_dbus_proxy_get_name_owner (priv->properties_proxy)) != NULL);
1512         g_free (owner);
1513         if (now_running != priv->nm_running) {
1514                 priv->nm_running = now_running;
1515                 g_object_notify (G_OBJECT (self), NM_OBJECT_NM_RUNNING);
1516         }
1517 }
1518
1519 static void
1520 init_dbus (NMObject *object)
1521 {
1522         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1523         char *owner;
1524
1525         if (_nm_dbus_is_connection_private (priv->connection))
1526                 priv->nm_running = TRUE;
1527         else {
1528                 priv->nm_running = ((owner = g_dbus_proxy_get_name_owner (priv->properties_proxy)) != NULL);
1529                 g_free (owner);
1530                 g_signal_connect (priv->properties_proxy, "notify::g-name-owner",
1531                                   G_CALLBACK (on_name_owner_changed), object);
1532         }
1533 }
1534
1535 static gboolean
1536 init_sync (GInitable *initable, GCancellable *cancellable, GError **error)
1537 {
1538         NMObject *self = NM_OBJECT (initable);
1539         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
1540         NMObjectClassPrivate *cpriv = NM_OBJECT_CLASS_GET_PRIVATE (NM_OBJECT_GET_CLASS (self));
1541         GSList *iter;
1542
1543         if (!priv->path) {
1544                 g_set_error_literal (error, NM_CLIENT_ERROR, NM_CLIENT_ERROR_OBJECT_CREATION_FAILED,
1545                                      _("Caller did not specify D-Bus path for object"));
1546                 return FALSE;
1547         }
1548
1549         if (!priv->connection)
1550                 priv->connection = _nm_dbus_new_connection (cancellable, error);
1551         if (!priv->connection)
1552                 return FALSE;
1553
1554         /* Create proxies */
1555         for (iter = cpriv->interfaces; iter; iter = iter->next) {
1556                 const char *interface = iter->data;
1557                 GDBusProxy *proxy;
1558
1559                 proxy = _nm_dbus_new_proxy_for_connection (priv->connection, priv->path, interface,
1560                                                            cancellable, error);
1561                 if (!proxy)
1562                         return FALSE;
1563                 g_hash_table_insert (priv->proxies, (char *) interface, proxy);
1564         }
1565
1566         priv->properties_proxy = _nm_dbus_new_proxy_for_connection (priv->connection,
1567                                                                     priv->path,
1568                                                                     DBUS_INTERFACE_PROPERTIES,
1569                                                                     cancellable, error);
1570         if (!priv->properties_proxy)
1571                 return FALSE;
1572
1573         NM_OBJECT_GET_CLASS (self)->init_dbus (self);
1574
1575         return _nm_object_reload_properties (self, error);
1576 }
1577
1578 /**************************************************************/
1579
1580 typedef struct {
1581         NMObject *object;
1582         GSimpleAsyncResult *simple;
1583         GCancellable *cancellable;
1584         int proxies_pending;
1585         GError *error;
1586 } NMObjectInitData;
1587
1588 static void
1589 init_async_complete (NMObjectInitData *init_data)
1590 {
1591         if (init_data->error)
1592                 g_simple_async_result_take_error (init_data->simple, init_data->error);
1593         else
1594                 g_simple_async_result_set_op_res_gboolean (init_data->simple, TRUE);
1595         g_simple_async_result_complete (init_data->simple);
1596         g_object_unref (init_data->simple);
1597         g_clear_object (&init_data->cancellable);
1598         g_slice_free (NMObjectInitData, init_data);
1599 }
1600
1601 static void
1602 init_async_got_properties (GObject *object, GAsyncResult *result, gpointer user_data)
1603 {
1604         NMObjectInitData *init_data = user_data;
1605
1606         _nm_object_reload_properties_finish (NM_OBJECT (object), result, &init_data->error);
1607         init_async_complete (init_data);
1608 }
1609
1610 static void
1611 init_async_got_proxy (GObject *object, GAsyncResult *result, gpointer user_data)
1612 {
1613         NMObjectInitData *init_data = user_data;
1614         NMObject *self = init_data->object;
1615         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
1616         GDBusProxy *proxy;
1617
1618         if (!init_data->error) {
1619                 proxy = _nm_dbus_new_proxy_for_connection_finish (result, &init_data->error);
1620                 if (proxy) {
1621                         const char *interface = g_dbus_proxy_get_interface_name (proxy);
1622
1623                         if (!strcmp (interface, DBUS_INTERFACE_PROPERTIES))
1624                                 priv->properties_proxy = proxy;
1625                         else
1626                                 g_hash_table_insert (priv->proxies, (char *) interface, proxy);
1627                 }
1628         }
1629
1630         init_data->proxies_pending--;
1631         if (init_data->proxies_pending)
1632                 return;
1633
1634         if (init_data->error) {
1635                 init_async_complete (init_data);
1636                 return;
1637         }
1638
1639         NM_OBJECT_GET_CLASS (self)->init_dbus (self);
1640
1641         _nm_object_reload_properties_async (init_data->object, init_data->cancellable, init_async_got_properties, init_data);
1642 }
1643
1644 static void
1645 init_async_got_bus (GObject *object, GAsyncResult *result, gpointer user_data)
1646 {
1647         NMObjectInitData *init_data = user_data;
1648         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (init_data->object);
1649         NMObjectClassPrivate *cpriv = NM_OBJECT_CLASS_GET_PRIVATE (NM_OBJECT_GET_CLASS (init_data->object));
1650         GSList *iter;
1651
1652         priv->connection = _nm_dbus_new_connection_finish (result, &init_data->error);
1653         if (!priv->connection) {
1654                 init_async_complete (init_data);
1655                 return;
1656         }
1657
1658         for (iter = cpriv->interfaces; iter; iter = iter->next) {
1659                 const char *interface = iter->data;
1660
1661                 _nm_dbus_new_proxy_for_connection_async (priv->connection,
1662                                                          priv->path, interface,
1663                                                          init_data->cancellable,
1664                                                          init_async_got_proxy, init_data);
1665                 init_data->proxies_pending++;
1666         }
1667
1668         _nm_dbus_new_proxy_for_connection_async (priv->connection,
1669                                                  priv->path,
1670                                                  DBUS_INTERFACE_PROPERTIES,
1671                                                  init_data->cancellable,
1672                                                  init_async_got_proxy, init_data);
1673         init_data->proxies_pending++;
1674 }
1675
1676 static void
1677 init_async (GAsyncInitable *initable, int io_priority,
1678             GCancellable *cancellable, GAsyncReadyCallback callback,
1679             gpointer user_data)
1680 {
1681         NMObject *self = NM_OBJECT (initable);
1682         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self);
1683         NMObjectInitData *init_data;
1684
1685         if (!priv->path) {
1686                 g_simple_async_report_error_in_idle (G_OBJECT (initable),
1687                                                      callback, user_data,
1688                                                      NM_CLIENT_ERROR,
1689                                                      NM_CLIENT_ERROR_OBJECT_CREATION_FAILED,
1690                                                      "%s",
1691                                                      _("Caller did not specify D-Bus path for object"));
1692                 return;
1693         }
1694
1695         init_data = g_slice_new0 (NMObjectInitData);
1696         init_data->object = self;
1697         init_data->simple = g_simple_async_result_new (G_OBJECT (initable), callback, user_data, init_async);
1698         init_data->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1699
1700         _nm_dbus_new_connection_async (cancellable, init_async_got_bus, init_data);
1701 }
1702
1703 static gboolean
1704 init_finish (GAsyncInitable *initable, GAsyncResult *result, GError **error)
1705 {
1706         GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1707
1708         if (g_simple_async_result_propagate_error (simple, error))
1709                 return FALSE;
1710         else
1711                 return TRUE;
1712 }
1713
1714 /**************************************************************/
1715
1716 static void
1717 nm_object_initable_iface_init (GInitableIface *iface)
1718 {
1719         iface->init = init_sync;
1720 }
1721
1722 static void
1723 nm_object_async_initable_iface_init (GAsyncInitableIface *iface)
1724 {
1725         iface->init_async = init_async;
1726         iface->init_finish = init_finish;
1727 }
1728
1729 static void
1730 nm_object_init (NMObject *object)
1731 {
1732         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1733
1734         priv->proxies = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
1735 }
1736
1737 static void
1738 set_property (GObject *object, guint prop_id,
1739               const GValue *value, GParamSpec *pspec)
1740 {
1741         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1742
1743         switch (prop_id) {
1744         case PROP_PATH:
1745                 /* Construct only */
1746                 priv->path = g_value_dup_string (value);
1747                 break;
1748         case PROP_DBUS_CONNECTION:
1749                 /* Construct only */
1750                 priv->connection = g_value_dup_object (value);
1751                 break;
1752         default:
1753                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1754                 break;
1755         }
1756 }
1757
1758 static void
1759 get_property (GObject *object, guint prop_id,
1760               GValue *value, GParamSpec *pspec)
1761 {
1762         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1763
1764         switch (prop_id) {
1765         case PROP_PATH:
1766                 g_value_set_string (value, priv->path);
1767                 break;
1768         case PROP_DBUS_CONNECTION:
1769                 g_value_set_object (value, priv->connection);
1770                 break;
1771         case PROP_NM_RUNNING:
1772                 g_value_set_boolean (value, priv->nm_running);
1773                 break;
1774         default:
1775                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1776                 break;
1777         }
1778 }
1779
1780 static void
1781 dispose (GObject *object)
1782 {
1783         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1784
1785         nm_clear_g_source (&priv->notify_id);
1786
1787         g_slist_free_full (priv->notify_items, (GDestroyNotify) notify_item_free);
1788         priv->notify_items = NULL;
1789
1790         g_slist_free_full (priv->waiters, odata_free);
1791         g_clear_pointer (&priv->proxies, g_hash_table_unref);
1792         g_clear_object (&priv->properties_proxy);
1793
1794         g_clear_object (&priv->connection);
1795
1796         G_OBJECT_CLASS (nm_object_parent_class)->dispose (object);
1797 }
1798
1799 static void
1800 finalize (GObject *object)
1801 {
1802         NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
1803
1804         g_slist_free_full (priv->property_tables, (GDestroyNotify) g_hash_table_destroy);
1805         g_free (priv->path);
1806
1807         G_OBJECT_CLASS (nm_object_parent_class)->finalize (object);
1808 }
1809
1810 static void
1811 nm_object_class_init (NMObjectClass *nm_object_class)
1812 {
1813         GObjectClass *object_class = G_OBJECT_CLASS (nm_object_class);
1814
1815         g_type_class_add_private (nm_object_class, sizeof (NMObjectPrivate));
1816
1817         /* virtual methods */
1818         object_class->set_property = set_property;
1819         object_class->get_property = get_property;
1820         object_class->dispose = dispose;
1821         object_class->finalize = finalize;
1822
1823         nm_object_class->init_dbus = init_dbus;
1824
1825         /* Properties */
1826
1827         /**
1828          * NMObject:path:
1829          *
1830          * The D-Bus object path.
1831          **/
1832         g_object_class_install_property
1833                 (object_class, PROP_PATH,
1834                  g_param_spec_string (NM_OBJECT_PATH, "", "",
1835                                       NULL,
1836                                       G_PARAM_READWRITE |
1837                                       G_PARAM_CONSTRUCT_ONLY |
1838                                       G_PARAM_STATIC_STRINGS));
1839
1840         /**
1841          * NMObject:dbus-connection: (skip)
1842          *
1843          * The #GDBusConnection of the object.
1844          **/
1845         g_object_class_install_property
1846             (object_class, PROP_DBUS_CONNECTION,
1847              g_param_spec_object (NM_OBJECT_DBUS_CONNECTION, "", "",
1848                                   G_TYPE_DBUS_CONNECTION,
1849                                   G_PARAM_READWRITE |
1850                                   G_PARAM_CONSTRUCT_ONLY |
1851                                   G_PARAM_STATIC_STRINGS));
1852
1853         /**
1854          * NMObject:manager-running: (skip)
1855          *
1856          * Internal use only.
1857          */
1858         g_object_class_install_property
1859                 (object_class, PROP_NM_RUNNING,
1860                  g_param_spec_boolean (NM_OBJECT_NM_RUNNING, "", "",
1861                                        FALSE,
1862                                        G_PARAM_READABLE |
1863                                        G_PARAM_STATIC_STRINGS));
1864 }
1865