libnm-glib: allow non-verifiable NMRemoteConnection in libnm-glib
[NetworkManager.git] / libnm-glib / nm-remote-connection.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 - 2011 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "NetworkManager.h"
27 #include "nm-utils.h"
28 #include "nm-setting-connection.h"
29 #include "nm-remote-connection.h"
30 #include "nm-remote-connection-private.h"
31 #include "nm-object-private.h"
32 #include "nm-dbus-glib-types.h"
33 #include "nm-dbus-helpers-private.h"
34 #include "nm-setting-private.h"
35
36 #define NM_REMOTE_CONNECTION_BUS "bus"
37 #define NM_REMOTE_CONNECTION_DBUS_CONNECTION "dbus-connection"
38 #define NM_REMOTE_CONNECTION_DBUS_PATH "dbus-path"
39
40 static void nm_remote_connection_initable_iface_init (GInitableIface *iface);
41 static void nm_remote_connection_async_initable_iface_init (GAsyncInitableIface *iface);
42
43 G_DEFINE_TYPE_WITH_CODE (NMRemoteConnection, nm_remote_connection, NM_TYPE_CONNECTION,
44                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_remote_connection_initable_iface_init);
45                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_remote_connection_async_initable_iface_init);
46                          )
47
48 enum {
49         PROP_0,
50         PROP_BUS,
51         PROP_DBUS_CONNECTION,
52         PROP_DBUS_PATH,
53         PROP_UNSAVED,
54
55         LAST_PROP
56 };
57
58 enum {
59         UPDATED,
60         REMOVED,
61         VISIBLE,
62
63         LAST_SIGNAL
64 };
65 static guint signals[LAST_SIGNAL] = { 0 };
66
67 typedef struct RemoteCall RemoteCall;
68 typedef void (*RemoteCallFetchResultCb) (RemoteCall *call, DBusGProxyCall *proxy_call, GError *error);
69
70
71 struct RemoteCall {
72         NMRemoteConnection *self;
73         DBusGProxyCall *call;
74         RemoteCallFetchResultCb fetch_result_cb;
75         GFunc callback;
76         gpointer user_data;
77 };
78
79 typedef struct {
80         DBusGConnection *bus;
81         DBusGProxy *proxy;
82         DBusGProxy *props_proxy;
83         gboolean proxy_is_destroyed;
84         GSList *calls;
85
86         gboolean inited;
87         gboolean unsaved;
88
89         gboolean visible;
90 } NMRemoteConnectionPrivate;
91
92 #define NM_REMOTE_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_REMOTE_CONNECTION, NMRemoteConnectionPrivate))
93
94 /**
95  * nm_remote_connection_error_quark:
96  *
97  * Registers an error quark for #NMRemoteConnection if necessary.
98  *
99  * Returns: the error quark used for #NMRemoteConnection errors.
100  **/
101 GQuark
102 nm_remote_connection_error_quark (void)
103 {
104         static GQuark quark = 0;
105
106         if (G_UNLIKELY (quark == 0))
107                 quark = g_quark_from_static_string ("nm-remote-connection-error-quark");
108         return quark;
109 }
110
111 /****************************************************************/
112
113 static void
114 _nm_remote_connection_ensure_inited (NMRemoteConnection *self)
115 {
116         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
117         GError *error = NULL;
118
119         if (!priv->inited) {
120                 if (!g_initable_init (G_INITABLE (self), NULL, &error)) {
121                         /* Don't warn when the call times out because the settings service can't
122                          * be activated or whatever.
123                          */
124                         if (!g_error_matches (error, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
125                                 g_warning ("%s: (NMRemoteConnection) error initializing: %s\n",
126                                            __func__, error->message);
127                         }
128                         g_error_free (error);
129                 }
130                 priv->inited = TRUE;
131         }
132 }
133
134 /****************************************************************/
135
136 static void
137 remote_call_dbus_cb (DBusGProxy *proxy, DBusGProxyCall *proxy_call, gpointer user_data)
138 {
139         RemoteCall *call = user_data;
140         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (call->self);
141         GError *error = NULL;
142
143         g_assert ( (!proxy && !proxy_call &&  priv->proxy_is_destroyed) ||
144                    ( proxy &&  proxy_call && !priv->proxy_is_destroyed && proxy == priv->proxy) );
145
146         if (priv->proxy_is_destroyed) {
147                 error = g_error_new_literal (NM_REMOTE_CONNECTION_ERROR,
148                                              NM_REMOTE_CONNECTION_ERROR_DISCONNECTED,
149                                              _("Disconnected by D-Bus"));
150         }
151         call->fetch_result_cb (call, proxy_call, error);
152         g_clear_error (&error);
153
154         priv->calls = g_slist_remove (priv->calls, call);
155         g_object_unref (call->self);
156         g_free (call);
157 }
158
159 static gboolean
160 remote_call_cleanup_cb (void *user_data)
161 {
162         remote_call_dbus_cb (NULL, NULL, user_data);
163         return G_SOURCE_REMOVE;
164 }
165
166 static RemoteCall *
167 remote_call_new (NMRemoteConnection *self,
168                  RemoteCallFetchResultCb fetch_result_cb,
169                  GFunc callback,
170                  gpointer user_data)
171 {
172         RemoteCall *call;
173         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
174
175         g_assert (fetch_result_cb);
176
177         if (priv->proxy_is_destroyed && !callback)
178                 return NULL;
179
180         call = g_malloc0 (sizeof (RemoteCall));
181         call->self = g_object_ref (self);
182         call->fetch_result_cb = fetch_result_cb;
183         call->user_data = user_data;
184         call->callback = callback;
185
186         if (priv->proxy_is_destroyed) {
187                 g_idle_add (remote_call_cleanup_cb, call);
188                 return NULL;
189         }
190         priv->calls = g_slist_prepend (priv->calls, call);
191         return call;
192 }
193
194 static void
195 proxy_set_destroyed (NMRemoteConnection *self)
196 {
197         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
198
199         if (priv->proxy_is_destroyed) {
200                 g_assert (!priv->calls);
201                 return;
202         }
203
204         priv->proxy_is_destroyed = TRUE;
205
206         priv->calls = g_slist_reverse (priv->calls);
207         while (priv->calls)
208                 remote_call_dbus_cb (NULL, NULL, priv->calls->data);
209 }
210
211 static void
212 proxy_destroy_cb (DBusGProxy* proxy, gpointer user_data) {
213         proxy_set_destroyed (user_data);
214 }
215
216 /****************************************************************/
217
218 static void
219 result_cb (RemoteCall *call, DBusGProxyCall *proxy_call, GError *error)
220 {
221         NMRemoteConnectionResultFunc func = (NMRemoteConnectionResultFunc) call->callback;
222         GError *local_error = NULL;
223
224         if (!error) {
225                 dbus_g_proxy_end_call (NM_REMOTE_CONNECTION_GET_PRIVATE (call->self)->proxy,
226                                        proxy_call, &local_error, G_TYPE_INVALID);
227                 error = local_error;
228         }
229         if (func)
230                 (*func) (call->self, error, call->user_data);
231         g_clear_error (&local_error);
232 }
233
234 /**
235  * nm_remote_connection_commit_changes:
236  * @connection: the #NMRemoteConnection
237  * @callback: (scope async) (allow-none): a function to be called when the
238  * commit completes
239  * @user_data: (closure): caller-specific data to be passed to @callback
240  *
241  * Send any local changes to the settings and properties of this connection to
242  * NetworkManager, which will immediately save them to disk.
243  **/
244 void
245 nm_remote_connection_commit_changes (NMRemoteConnection *self,
246                                      NMRemoteConnectionResultFunc callback,
247                                      gpointer user_data)
248 {
249         NMRemoteConnectionPrivate *priv;
250         RemoteCall *call;
251         GHashTable *settings;
252
253         g_return_if_fail (NM_IS_REMOTE_CONNECTION (self));
254
255         priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
256
257         call = remote_call_new (self, result_cb, (GFunc) callback, user_data);
258         if (!call)
259                 return;
260
261         settings = nm_connection_to_hash (NM_CONNECTION (self), NM_SETTING_HASH_FLAG_ALL);
262         call->call = dbus_g_proxy_begin_call (priv->proxy, "Update",
263                                               remote_call_dbus_cb, call, NULL,
264                                               DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, settings,
265                                               G_TYPE_INVALID);
266         g_assert (call->call);
267         g_hash_table_destroy (settings);
268 }
269
270 /**
271  * nm_remote_connection_commit_changes_unsaved:
272  * @connection: the #NMRemoteConnection
273  * @callback: (scope async) (allow-none): a function to be called when the
274  * commit completes
275  * @user_data: (closure): caller-specific data to be passed to @callback
276  *
277  * Send any local changes to the settings and properties of this connection to
278  * NetworkManager.  The changes are not saved to disk until either
279  * nm_remote_connection_save() or nm_remote_connection_commit_changes() is
280  * called.
281  *
282  * Since: 0.9.10
283  **/
284 void
285 nm_remote_connection_commit_changes_unsaved (NMRemoteConnection *connection,
286                                              NMRemoteConnectionResultFunc callback,
287                                              gpointer user_data)
288 {
289         NMRemoteConnectionPrivate *priv;
290         GHashTable *settings = NULL;
291         RemoteCall *call;
292
293         g_return_if_fail (NM_IS_REMOTE_CONNECTION (connection));
294
295         priv = NM_REMOTE_CONNECTION_GET_PRIVATE (connection);
296
297         call = remote_call_new (connection, result_cb, (GFunc) callback, user_data);
298         if (!call)
299                 return;
300
301         settings = nm_connection_to_hash (NM_CONNECTION (connection), NM_SETTING_HASH_FLAG_ALL);
302         call->call = dbus_g_proxy_begin_call (priv->proxy, "UpdateUnsaved",
303                                               remote_call_dbus_cb, call, NULL,
304                                               DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, settings,
305                                               G_TYPE_INVALID);
306         g_assert (call->call);
307         g_hash_table_destroy (settings);
308 }
309
310 /**
311  * nm_remote_connection_save:
312  * @connection: the #NMRemoteConnection
313  * @callback: (scope async) (allow-none): a function to be called when the
314  * save completes
315  * @user_data: (closure): caller-specific data to be passed to @callback
316  *
317  * Saves the connection to disk if the connection has changes that have not yet
318  * been written to disk, or if the connection has never been saved.
319  *
320  * Since: 0.9.10
321  **/
322 void
323 nm_remote_connection_save (NMRemoteConnection *connection,
324                            NMRemoteConnectionResultFunc callback,
325                            gpointer user_data)
326 {
327         NMRemoteConnectionPrivate *priv;
328         RemoteCall *call;
329
330         g_return_if_fail (NM_IS_REMOTE_CONNECTION (connection));
331
332         priv = NM_REMOTE_CONNECTION_GET_PRIVATE (connection);
333
334         call = remote_call_new (connection, result_cb, (GFunc) callback, user_data);
335         if (!call)
336                 return;
337
338         call->call = dbus_g_proxy_begin_call (priv->proxy, "Save", remote_call_dbus_cb, call, NULL, G_TYPE_INVALID);
339         g_assert (call->call);
340 }
341
342 /**
343  * nm_remote_connection_delete:
344  * @connection: the #NMRemoteConnection
345  * @callback: (scope async) (allow-none): a function to be called when the delete completes
346  * @user_data: (closure): caller-specific data to be passed to @callback
347  *
348  * Delete the connection.
349  **/
350 void
351 nm_remote_connection_delete (NMRemoteConnection *self,
352                              NMRemoteConnectionResultFunc callback,
353                              gpointer user_data)
354 {
355         NMRemoteConnectionPrivate *priv;
356         RemoteCall *call;
357
358         g_return_if_fail (NM_IS_REMOTE_CONNECTION (self));
359
360         priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
361
362         call = remote_call_new (self, result_cb, (GFunc) callback, user_data);
363         if (!call)
364                 return;
365
366         call->call = dbus_g_proxy_begin_call (priv->proxy, "Delete",
367                                               remote_call_dbus_cb, call, NULL,
368                                               G_TYPE_INVALID);
369         g_assert (call->call);
370 }
371
372 static void
373 get_secrets_cb (RemoteCall *call, DBusGProxyCall *proxy_call, GError *error)
374 {
375         NMRemoteConnectionGetSecretsFunc func = (NMRemoteConnectionGetSecretsFunc) call->callback;
376         GHashTable *secrets = NULL;
377         GError *local_error = NULL;
378
379         if (!error) {
380                 dbus_g_proxy_end_call (NM_REMOTE_CONNECTION_GET_PRIVATE (call->self)->proxy,
381                                        proxy_call, &local_error,
382                                        DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &secrets,
383                                        G_TYPE_INVALID);
384                 error = local_error;
385         }
386         if (func)
387                 (*func) (call->self, error ? NULL : secrets, error, call->user_data);
388         g_clear_error (&local_error);
389         if (secrets)
390                 g_hash_table_destroy (secrets);
391 }
392
393
394 /**
395  * nm_remote_connection_get_secrets:
396  * @connection: the #NMRemoteConnection
397  * @setting_name: the #NMSetting object name to get secrets for
398  * @callback: (scope async): a function to be called when the update completes;
399  * must not be %NULL
400  * @user_data: (closure): caller-specific data to be passed to @callback
401  *
402  * Request the connection's secrets.
403  **/
404 void
405 nm_remote_connection_get_secrets (NMRemoteConnection *self,
406                                   const char *setting_name,
407                                   NMRemoteConnectionGetSecretsFunc callback,
408                                   gpointer user_data)
409 {
410         NMRemoteConnectionPrivate *priv;
411         RemoteCall *call;
412
413         g_return_if_fail (NM_IS_REMOTE_CONNECTION (self));
414         g_return_if_fail (callback != NULL);
415
416         priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
417
418         call = remote_call_new (self, get_secrets_cb, (GFunc) callback, user_data);
419         if (!call)
420                 return;
421
422         call->call = dbus_g_proxy_begin_call (priv->proxy, "GetSecrets",
423                                               remote_call_dbus_cb, call, NULL,
424                                               G_TYPE_STRING, setting_name,
425                                               G_TYPE_INVALID);
426         g_assert (call->call);
427 }
428
429 /**
430  * nm_remote_connection_get_unsaved:
431  * @connection: the #NMRemoteConnection
432  *
433  * Returns: %TRUE if the remote connection contains changes that have not
434  * been saved to disk, %FALSE if the connection is the same as its on-disk
435  * representation.
436  *
437  * Since: 0.9.10
438  **/
439 gboolean
440 nm_remote_connection_get_unsaved (NMRemoteConnection *connection)
441 {
442         g_return_val_if_fail (NM_IS_REMOTE_CONNECTION (connection), FALSE);
443
444         _nm_remote_connection_ensure_inited (connection);
445         return NM_REMOTE_CONNECTION_GET_PRIVATE (connection)->unsaved;
446 }
447
448 /****************************************************************/
449
450 static void
451 updated_get_settings_cb (DBusGProxy *proxy,
452                          DBusGProxyCall *call,
453                          gpointer user_data)
454 {
455         NMRemoteConnection *self = user_data;
456         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
457         GHashTable *new_settings;
458         GError *error = NULL;
459
460         dbus_g_proxy_end_call (proxy, call, &error,
461                                DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &new_settings,
462                                G_TYPE_INVALID);
463         if (error) {
464                 GHashTable *hash;
465
466                 g_error_free (error);
467
468                 /* Connection is no longer visible to this user.  Let the settings
469                  * service handle this via 'visible'.  The settings service will emit
470                  * the "removed" signal for us since it handles the lifetime of this
471                  * object.
472                  */
473                 hash = g_hash_table_new (g_str_hash, g_str_equal);
474                 _nm_connection_replace_settings (NM_CONNECTION (self), hash);
475                 g_hash_table_destroy (hash);
476
477                 priv->visible = FALSE;
478                 g_signal_emit (self, signals[VISIBLE], 0, FALSE);
479         } else {
480                 gs_unref_object NMConnection *self_alive = NULL;
481
482                 self_alive = g_object_ref (self);
483                 _nm_connection_replace_settings (NM_CONNECTION (self), new_settings);
484                 g_signal_emit (self, signals[UPDATED], 0, new_settings);
485                 g_hash_table_destroy (new_settings);
486
487                 /* Settings service will handle announcing the connection to clients */
488                 if (priv->visible == FALSE) {
489                         priv->visible = TRUE;
490                         g_signal_emit (self, signals[VISIBLE], 0, TRUE);
491                 }
492         }
493 }
494
495 static void
496 updated_cb (DBusGProxy *proxy, gpointer user_data)
497 {
498         NMRemoteConnection *self = NM_REMOTE_CONNECTION (user_data);
499         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
500
501         /* The connection got updated; request the replacement settings */
502         if (!priv->proxy_is_destroyed) {
503                 dbus_g_proxy_begin_call (priv->proxy, "GetSettings",
504                                          updated_get_settings_cb, self, NULL,
505                                          G_TYPE_INVALID);
506         }
507 }
508
509 static void
510 removed_cb (DBusGProxy *proxy, gpointer user_data)
511 {
512         g_signal_emit (G_OBJECT (user_data), signals[REMOVED], 0);
513 }
514
515 static void
516 properties_changed_cb (DBusGProxy *proxy,
517                        GHashTable *properties,
518                        gpointer user_data)
519 {
520         NMRemoteConnection *self = NM_REMOTE_CONNECTION (user_data);
521         GHashTableIter iter;
522         const char *key;
523         GValue *value;
524
525         g_hash_table_iter_init (&iter, properties);
526         while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
527                 if (!strcmp (key, "Unsaved")) {
528                         NM_REMOTE_CONNECTION_GET_PRIVATE (self)->unsaved = g_value_get_boolean (value);
529                         g_object_notify (G_OBJECT (self), NM_REMOTE_CONNECTION_UNSAVED);
530                 }
531         }
532 }
533
534 /****************************************************************/
535
536 /**
537  * nm_remote_connection_new:
538  * @bus: a valid and connected D-Bus connection
539  * @path: the D-Bus path of the connection as exported by the settings service
540  *
541  * Creates a new object representing the remote connection.
542  *
543  * Returns: the new remote connection object on success, or %NULL on failure
544  **/
545 NMRemoteConnection *
546 nm_remote_connection_new (DBusGConnection *bus,
547                           const char *path)
548 {
549         g_return_val_if_fail (bus != NULL, NULL);
550         g_return_val_if_fail (path != NULL, NULL);
551
552         return (NMRemoteConnection *) g_object_new (NM_TYPE_REMOTE_CONNECTION,
553                                                     NM_REMOTE_CONNECTION_BUS, bus,
554                                                     NM_CONNECTION_PATH, path,
555                                                     NULL);
556 }
557
558 static void
559 constructed (GObject *object)
560 {
561         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (object);
562
563         G_OBJECT_CLASS (nm_remote_connection_parent_class)->constructed (object);
564
565         g_assert (priv->bus);
566         g_assert (nm_connection_get_path (NM_CONNECTION (object)));
567
568         priv->proxy = _nm_dbus_new_proxy_for_connection (priv->bus,
569                                                          nm_connection_get_path (NM_CONNECTION (object)),
570                                                          NM_DBUS_IFACE_SETTINGS_CONNECTION);
571         g_assert (priv->proxy);
572         dbus_g_proxy_set_default_timeout (priv->proxy, G_MAXINT);
573
574         dbus_g_proxy_add_signal (priv->proxy, "Updated", G_TYPE_INVALID);
575         dbus_g_proxy_connect_signal (priv->proxy, "Updated", G_CALLBACK (updated_cb), object, NULL);
576
577         dbus_g_proxy_add_signal (priv->proxy, "Removed", G_TYPE_INVALID);
578         dbus_g_proxy_connect_signal (priv->proxy, "Removed", G_CALLBACK (removed_cb), object, NULL);
579
580         g_signal_connect (priv->proxy, "destroy", G_CALLBACK (proxy_destroy_cb), object);
581
582         /* Monitor properties */
583         dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__BOXED,
584                                            G_TYPE_NONE,
585                                            DBUS_TYPE_G_MAP_OF_VARIANT,
586                                            G_TYPE_INVALID);
587         dbus_g_proxy_add_signal (priv->proxy, "PropertiesChanged",
588                                  DBUS_TYPE_G_MAP_OF_VARIANT,
589                                  G_TYPE_INVALID);
590         dbus_g_proxy_connect_signal (priv->proxy, "PropertiesChanged",
591                                      G_CALLBACK (properties_changed_cb),
592                                      object,
593                                      NULL);
594
595         priv->props_proxy = _nm_dbus_new_proxy_for_connection (priv->bus,
596                                                                nm_connection_get_path (NM_CONNECTION (object)),
597                                                                DBUS_INTERFACE_PROPERTIES);
598         g_assert (priv->props_proxy);
599 }
600
601 static gboolean
602 init_sync (GInitable *initable, GCancellable *cancellable, GError **error)
603 {
604         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (initable);
605         GHashTable *hash;
606         gs_unref_object NMConnection *self_alive = NULL;
607
608         if (!dbus_g_proxy_call (priv->proxy, "GetSettings", error,
609                                 G_TYPE_INVALID,
610                                 DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &hash,
611                                 G_TYPE_INVALID))
612                 return FALSE;
613         priv->visible = TRUE;
614         self_alive = g_object_ref (initable);
615         _nm_connection_replace_settings (NM_CONNECTION (initable), hash);
616         g_signal_emit (initable, signals[UPDATED], 0, hash);
617         g_hash_table_destroy (hash);
618
619         /* Get properties */
620         hash = NULL;
621         if (!dbus_g_proxy_call (priv->props_proxy, "GetAll", error,
622                                 G_TYPE_STRING, NM_DBUS_IFACE_SETTINGS_CONNECTION,
623                                 G_TYPE_INVALID,
624                                 DBUS_TYPE_G_MAP_OF_VARIANT, &hash,
625                                 G_TYPE_INVALID))
626                 return FALSE;
627         properties_changed_cb (priv->props_proxy, hash, NM_REMOTE_CONNECTION (initable));
628         g_hash_table_destroy (hash);
629
630         return TRUE;
631 }
632
633 typedef struct {
634         NMRemoteConnection *connection;
635         GSimpleAsyncResult *result;
636 } NMRemoteConnectionInitData;
637
638 static void
639 init_async_complete (NMRemoteConnectionInitData *init_data, GError *error)
640 {
641         if (error)
642                 g_simple_async_result_take_error (init_data->result, error);
643         else {
644                 g_simple_async_result_set_op_res_gboolean (init_data->result, TRUE);
645                 NM_REMOTE_CONNECTION_GET_PRIVATE (init_data->connection)->inited = TRUE;
646         }
647
648         g_simple_async_result_complete (init_data->result);
649         g_object_unref (init_data->result);
650         g_slice_free (NMRemoteConnectionInitData, init_data);
651 }
652
653 static void
654 init_async_got_properties (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
655 {
656         NMRemoteConnectionInitData *init_data = user_data;
657         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (init_data->connection);
658         GHashTable *props;
659         GError *error = NULL;
660
661         if (dbus_g_proxy_end_call (proxy, call, &error,
662                                    DBUS_TYPE_G_MAP_OF_VARIANT, &props,
663                                    G_TYPE_INVALID)) {
664                 properties_changed_cb (priv->props_proxy, props, init_data->connection);
665                 g_hash_table_destroy (props);
666         }
667         init_async_complete (init_data, error);
668 }
669
670 static void
671 init_get_settings_cb (DBusGProxy *proxy,
672                       DBusGProxyCall *call,
673                       gpointer user_data)
674 {
675         NMRemoteConnectionInitData *init_data = user_data;
676         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (init_data->connection);
677         GHashTable *settings;
678         GError *error = NULL;
679         gs_unref_object NMConnection *self_alive = NULL;
680
681         dbus_g_proxy_end_call (proxy, call, &error,
682                                DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &settings,
683                                G_TYPE_INVALID);
684         if (error) {
685                 init_async_complete (init_data, error);
686                 return;
687         }
688
689         priv->visible = TRUE;
690         self_alive = g_object_ref (init_data->connection);
691         _nm_connection_replace_settings (NM_CONNECTION (init_data->connection), settings);
692         g_signal_emit (init_data->connection, signals[UPDATED], 0, settings);
693         g_hash_table_destroy (settings);
694
695         /* Grab properties */
696         dbus_g_proxy_begin_call (priv->props_proxy, "GetAll",
697                                  init_async_got_properties, init_data, NULL,
698                                  G_TYPE_STRING, NM_DBUS_IFACE_SETTINGS_CONNECTION,
699                                  G_TYPE_INVALID);
700 }
701
702 static void
703 init_async (GAsyncInitable *initable, int io_priority,
704             GCancellable *cancellable, GAsyncReadyCallback callback,
705             gpointer user_data)
706 {
707         NMRemoteConnectionInitData *init_data;
708         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (initable);
709
710
711         init_data = g_slice_new0 (NMRemoteConnectionInitData);
712         init_data->connection = NM_REMOTE_CONNECTION (initable);
713         init_data->result = g_simple_async_result_new (G_OBJECT (initable), callback,
714                                                        user_data, init_async);
715
716         dbus_g_proxy_begin_call (priv->proxy, "GetSettings",
717                                  init_get_settings_cb, init_data, NULL,
718                                  G_TYPE_INVALID);
719 }
720
721 static gboolean
722 init_finish (GAsyncInitable *initable, GAsyncResult *result, GError **error)
723 {
724         GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
725
726         if (g_simple_async_result_propagate_error (simple, error))
727                 return FALSE;
728         else
729                 return TRUE;
730 }
731
732 static void
733 nm_remote_connection_init (NMRemoteConnection *self)
734 {
735 }
736
737 static GObject *
738 constructor (GType type, guint n_construct_properties,
739              GObjectConstructParam *construct_properties)
740 {
741         static GParamSpec *nm_connection_path = NULL;
742         static GParamSpec *nm_remote_connection_dbus_path = NULL;
743         int i, path_index = -1, dbus_path_index = -1;
744
745         if (!nm_connection_path) {
746                 nm_connection_path =
747                         g_object_class_find_property (g_type_class_peek (NM_TYPE_CONNECTION),
748                                                       NM_CONNECTION_PATH);
749                 nm_remote_connection_dbus_path =
750                         g_object_class_find_property (g_type_class_peek (NM_TYPE_REMOTE_CONNECTION),
751                                                       NM_REMOTE_CONNECTION_DBUS_PATH);
752         }
753
754         /* Find the two properties */
755         for (i = 0; i < n_construct_properties; i++) {
756                 if (construct_properties[i].pspec == nm_connection_path)
757                         path_index = i;
758                 else if (construct_properties[i].pspec == nm_remote_connection_dbus_path)
759                         dbus_path_index = i;
760         }
761         g_assert (path_index != -1 && dbus_path_index != -1);
762
763         /* If NMRemoteConnection:dbus-path is set, and NMConnection:path
764          * is not, then copy the value of the former to the latter.
765          */
766         if (g_value_get_string (construct_properties[dbus_path_index].value) &&
767             !g_value_get_string (construct_properties[path_index].value))
768                 construct_properties[path_index].value = construct_properties[dbus_path_index].value;
769
770         return G_OBJECT_CLASS (nm_remote_connection_parent_class)->
771                 constructor (type, n_construct_properties, construct_properties);
772 }
773
774 static void
775 get_property (GObject *object, guint prop_id,
776               GValue *value, GParamSpec *pspec)
777 {
778         _nm_remote_connection_ensure_inited (NM_REMOTE_CONNECTION (object));
779
780         switch (prop_id) {
781         case PROP_UNSAVED:
782                 g_value_set_boolean (value, NM_REMOTE_CONNECTION_GET_PRIVATE (object)->unsaved);
783                 break;
784         default:
785                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
786                 break;
787         }
788 }
789
790 static void
791 set_property (GObject *object, guint prop_id,
792               const GValue *value, GParamSpec *pspec)
793 {
794         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (object);
795
796         switch (prop_id) {
797         case PROP_BUS:
798         case PROP_DBUS_CONNECTION:
799                 /* Construct only */
800                 /* priv->bus is set from either of two properties so that it (a) remains
801                  * backwards compatible with the previous "bus" property, and that (b)
802                  * it can be created just like an NMObject using the "dbus-connection",
803                  * even though it's not a subclass of NMObject.  So don't overwrite the
804                  * a valid value that the other property set with NULL, if one of the
805                  * properties isn't specified at construction time.
806                  */
807                 if (!priv->bus)
808                         priv->bus = g_value_dup_boxed (value);
809                 break;
810         case PROP_DBUS_PATH:
811                 /* Don't need to do anything; see constructor(). */
812                 break;
813         default:
814                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
815                 break;
816         }
817 }
818
819 static void
820 dispose (GObject *object)
821 {
822         NMRemoteConnection *self = NM_REMOTE_CONNECTION (object);
823         NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (self);
824
825         proxy_set_destroyed (self);
826
827         if (priv->proxy) {
828                 g_signal_handlers_disconnect_by_func (priv->proxy, proxy_destroy_cb, object);
829                 g_clear_object (&priv->proxy);
830         }
831         g_clear_object (&priv->props_proxy);
832
833         if (priv->bus) {
834                 dbus_g_connection_unref (priv->bus);
835                 priv->bus = NULL;
836         }
837
838         G_OBJECT_CLASS (nm_remote_connection_parent_class)->dispose (object);
839 }
840
841 static void
842 nm_remote_connection_class_init (NMRemoteConnectionClass *remote_class)
843 {
844         GObjectClass *object_class = G_OBJECT_CLASS (remote_class);
845
846         g_type_class_add_private (object_class, sizeof (NMRemoteConnectionPrivate));
847
848         /* virtual methods */
849         object_class->constructor = constructor;
850         object_class->get_property = get_property;
851         object_class->set_property = set_property;
852         object_class->dispose = dispose;
853         object_class->constructed = constructed;
854
855         /* Properties */
856         /**
857          * NMRemoteConnection:bus:
858          *
859          * The #DBusGConnection that the #NMRemoteConnection is connected to.
860          */
861         g_object_class_install_property
862                 (object_class, PROP_BUS,
863                  g_param_spec_boxed (NM_REMOTE_CONNECTION_BUS, "", "",
864                                      DBUS_TYPE_G_CONNECTION,
865                                      G_PARAM_WRITABLE |
866                                      G_PARAM_CONSTRUCT_ONLY |
867                                      G_PARAM_STATIC_STRINGS));
868
869         /* These are needed so _nm_object_create() can create NMRemoteConnections */
870         g_object_class_install_property
871                 (object_class, PROP_DBUS_CONNECTION,
872                  g_param_spec_boxed (NM_REMOTE_CONNECTION_DBUS_CONNECTION, "", "",
873                                      DBUS_TYPE_G_CONNECTION,
874                                      G_PARAM_WRITABLE |
875                                      G_PARAM_CONSTRUCT_ONLY |
876                                      G_PARAM_STATIC_STRINGS));
877         g_object_class_install_property
878                 (object_class, PROP_DBUS_PATH,
879                  g_param_spec_string (NM_REMOTE_CONNECTION_DBUS_PATH, "", "",
880                                       NULL,
881                                       G_PARAM_WRITABLE |
882                                       G_PARAM_CONSTRUCT_ONLY |
883                                       G_PARAM_STATIC_STRINGS));
884
885         /**
886          * NMRemoteConnection:unsaved:
887          *
888          * %TRUE if the remote connection contains changes that have not been saved
889          * to disk, %FALSE if the connection is the same as its on-disk representation.
890          *
891          * Since: 0.9.10
892          **/
893         g_object_class_install_property
894                 (object_class, PROP_UNSAVED,
895                  g_param_spec_boolean (NM_REMOTE_CONNECTION_UNSAVED, "", "",
896                                        FALSE,
897                                        G_PARAM_READABLE |
898                                        G_PARAM_STATIC_STRINGS));
899
900         /* Signals */
901         /**
902          * NMRemoteConnection::updated:
903          * @connection: a #NMConnection
904          *
905          * This signal is emitted when a connection changes, and it is
906          * still visible to the user.
907          */
908         signals[UPDATED] =
909                 g_signal_new (NM_REMOTE_CONNECTION_UPDATED,
910                               G_TYPE_FROM_CLASS (remote_class),
911                               G_SIGNAL_RUN_FIRST,
912                               G_STRUCT_OFFSET (NMRemoteConnectionClass, updated),
913                               NULL, NULL,
914                               g_cclosure_marshal_VOID__VOID,
915                               G_TYPE_NONE, 0);
916
917         /**
918          * NMRemoteConnection::removed:
919          * @connection: a #NMConnection
920          *
921          * This signal is emitted when a connection is either deleted or becomes
922          * invisible to the current user.
923          */
924         signals[REMOVED] =
925                 g_signal_new (NM_REMOTE_CONNECTION_REMOVED,
926                               G_TYPE_FROM_CLASS (remote_class),
927                               G_SIGNAL_RUN_FIRST,
928                               G_STRUCT_OFFSET (NMRemoteConnectionClass, removed),
929                               NULL, NULL,
930                               g_cclosure_marshal_VOID__VOID,
931                               G_TYPE_NONE, 0);
932
933         /* Private signal */
934         signals[VISIBLE] =
935                 g_signal_new ("visible",
936                               G_TYPE_FROM_CLASS (remote_class),
937                               G_SIGNAL_RUN_FIRST,
938                               0, NULL, NULL,
939                               g_cclosure_marshal_VOID__BOOLEAN,
940                               G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
941 }
942
943 static void
944 nm_remote_connection_initable_iface_init (GInitableIface *iface)
945 {
946         iface->init = init_sync;
947 }
948
949 static void
950 nm_remote_connection_async_initable_iface_init (GAsyncInitableIface *iface)
951 {
952         iface->init_async = init_async;
953         iface->init_finish = init_finish;
954 }