nm-client.c 73.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2007 - 2008 Novell, Inc.
19
 * Copyright 2007 - 2014 Red Hat, Inc.
20 21 22 23 24 25 26 27 28
 */

#include <string.h>
#include <nm-utils.h>

#include "nm-client.h"
#include "nm-device-ethernet.h"
#include "nm-device-wifi.h"
#include "nm-device-private.h"
29
#include "nm-core-internal.h"
30 31 32 33 34
#include "nm-object-private.h"
#include "nm-active-connection.h"
#include "nm-vpn-connection.h"
#include "nm-object-cache.h"
#include "nm-glib-compat.h"
Dan Winship's avatar
Dan Winship committed
35 36 37
#include "nm-dbus-helpers.h"

#include "nmdbus-manager.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

void _nm_device_wifi_set_wireless_enabled (NMDeviceWifi *device, gboolean enabled);

static void nm_client_initable_iface_init (GInitableIface *iface);
static void nm_client_async_initable_iface_init (GAsyncInitableIface *iface);
static GInitableIface *nm_client_parent_initable_iface;
static GAsyncInitableIface *nm_client_parent_async_initable_iface;

G_DEFINE_TYPE_WITH_CODE (NMClient, nm_client, NM_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_client_initable_iface_init);
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_client_async_initable_iface_init);
                         )

#define NM_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CLIENT, NMClientPrivate))

typedef struct {
Dan Winship's avatar
Dan Winship committed
54
	NMDBusManager *manager_proxy;
55 56 57 58 59 60 61 62 63
	char *version;
	NMState state;
	gboolean startup;
	GPtrArray *devices;
	GPtrArray *active_connections;
	NMConnectivityState connectivity;
	NMActiveConnection *primary_connection;
	NMActiveConnection *activating_connection;

Dan Winship's avatar
Dan Winship committed
64
	GCancellable *perm_call_cancellable;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	GHashTable *permissions;

	/* Activations waiting for their NMActiveConnection
	 * to appear and then their callback to be called.
	 */
	GSList *pending_activations;

	gboolean networking_enabled;
	gboolean wireless_enabled;
	gboolean wireless_hw_enabled;

	gboolean wwan_enabled;
	gboolean wwan_hw_enabled;

	gboolean wimax_enabled;
	gboolean wimax_hw_enabled;
} NMClientPrivate;

enum {
	PROP_0,
	PROP_VERSION,
	PROP_STATE,
	PROP_STARTUP,
88
	PROP_NM_RUNNING,
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	PROP_NETWORKING_ENABLED,
	PROP_WIRELESS_ENABLED,
	PROP_WIRELESS_HARDWARE_ENABLED,
	PROP_WWAN_ENABLED,
	PROP_WWAN_HARDWARE_ENABLED,
	PROP_WIMAX_ENABLED,
	PROP_WIMAX_HARDWARE_ENABLED,
	PROP_ACTIVE_CONNECTIONS,
	PROP_CONNECTIVITY,
	PROP_PRIMARY_CONNECTION,
	PROP_ACTIVATING_CONNECTION,
	PROP_DEVICES,

	LAST_PROP
};

enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
	PERMISSION_CHANGED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

115 116 117
static void nm_running_changed_cb (GObject *object,
                                   GParamSpec *pspec,
                                   gpointer user_data);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

/**********************************************************************/

/**
 * nm_client_error_quark:
 *
 * Registers an error quark for #NMClient if necessary.
 *
 * Returns: the error quark used for #NMClient errors.
 **/
GQuark
nm_client_error_quark (void)
{
	static GQuark quark;

	if (G_UNLIKELY (!quark))
		quark = g_quark_from_static_string ("nm-client-error-quark");
	return quark;
}

/**********************************************************************/

static void
nm_client_init (NMClient *client)
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);

	priv->state = NM_STATE_UNKNOWN;

	priv->permissions = g_hash_table_new (g_direct_hash, g_direct_equal);
}

static void
poke_wireless_devices_with_rf_status (NMClient *client)
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);
	int i;

156
	for (i = 0; i < priv->devices->len; i++) {
157 158 159 160 161 162 163 164 165 166 167 168 169
		NMDevice *device = g_ptr_array_index (priv->devices, i);

		if (NM_IS_DEVICE_WIFI (device))
			_nm_device_wifi_set_wireless_enabled (NM_DEVICE_WIFI (device), priv->wireless_enabled);
	}
}

static void
wireless_enabled_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
{
	poke_wireless_devices_with_rf_status (NM_CLIENT (object));
}

Dan Winship's avatar
Dan Winship committed
170
static void client_recheck_permissions (NMDBusManager *proxy, gpointer user_data);
171 172 173
static void active_connections_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data);
static void object_creation_failed_cb (GObject *object, GError *error, char *failed_path);

174
static void
175
init_dbus (NMObject *object)
176
{
177
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (object);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	const NMPropertiesInfo property_info[] = {
		{ NM_CLIENT_VERSION,                   &priv->version },
		{ NM_CLIENT_STATE,                     &priv->state },
		{ NM_CLIENT_STARTUP,                   &priv->startup },
		{ NM_CLIENT_NETWORKING_ENABLED,        &priv->networking_enabled },
		{ NM_CLIENT_WIRELESS_ENABLED,          &priv->wireless_enabled },
		{ NM_CLIENT_WIRELESS_HARDWARE_ENABLED, &priv->wireless_hw_enabled },
		{ NM_CLIENT_WWAN_ENABLED,              &priv->wwan_enabled },
		{ NM_CLIENT_WWAN_HARDWARE_ENABLED,     &priv->wwan_hw_enabled },
		{ NM_CLIENT_WIMAX_ENABLED,             &priv->wimax_enabled },
		{ NM_CLIENT_WIMAX_HARDWARE_ENABLED,    &priv->wimax_hw_enabled },
		{ NM_CLIENT_ACTIVE_CONNECTIONS,        &priv->active_connections, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_CLIENT_CONNECTIVITY,              &priv->connectivity },
		{ NM_CLIENT_PRIMARY_CONNECTION,        &priv->primary_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_CLIENT_ACTIVATING_CONNECTION,     &priv->activating_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_CLIENT_DEVICES,                   &priv->devices, NULL, NM_TYPE_DEVICE, "device" },
		{ NULL },
	};

197 198
	NM_OBJECT_CLASS (nm_client_parent_class)->init_dbus (object);

Dan Winship's avatar
Dan Winship committed
199
	priv->manager_proxy = NMDBUS_MANAGER (_nm_object_get_proxy (object, NM_DBUS_INTERFACE));
200
	_nm_object_register_properties (object,
201
	                                NM_DBUS_INTERFACE,
202
	                                property_info);
203 204

	/* Permissions */
Dan Winship's avatar
Dan Winship committed
205 206
	g_signal_connect (priv->manager_proxy, "check-permissions",
	                  G_CALLBACK (client_recheck_permissions), object);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

#define NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK     "org.freedesktop.NetworkManager.enable-disable-network"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI        "org.freedesktop.NetworkManager.enable-disable-wifi"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN        "org.freedesktop.NetworkManager.enable-disable-wwan"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX       "org.freedesktop.NetworkManager.enable-disable-wimax"
#define NM_AUTH_PERMISSION_SLEEP_WAKE                 "org.freedesktop.NetworkManager.sleep-wake"
#define NM_AUTH_PERMISSION_NETWORK_CONTROL            "org.freedesktop.NetworkManager.network-control"
#define NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED       "org.freedesktop.NetworkManager.wifi.share.protected"
#define NM_AUTH_PERMISSION_WIFI_SHARE_OPEN            "org.freedesktop.NetworkManager.wifi.share.open"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM     "org.freedesktop.NetworkManager.settings.modify.system"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN        "org.freedesktop.NetworkManager.settings.modify.own"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME   "org.freedesktop.NetworkManager.settings.modify.hostname"

static NMClientPermission
nm_permission_to_client (const char *nm)
{
	if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SLEEP_WAKE))
		return NM_CLIENT_PERMISSION_SLEEP_WAKE;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_NETWORK_CONTROL))
		return NM_CLIENT_PERMISSION_NETWORK_CONTROL;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED))
		return NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN))
		return NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME;

	return NM_CLIENT_PERMISSION_NONE;
}

static NMClientPermissionResult
nm_permission_result_to_client (const char *nm)
{
	if (!strcmp (nm, "yes"))
		return NM_CLIENT_PERMISSION_RESULT_YES;
	else if (!strcmp (nm, "no"))
		return NM_CLIENT_PERMISSION_RESULT_NO;
	else if (!strcmp (nm, "auth"))
		return NM_CLIENT_PERMISSION_RESULT_AUTH;
	return NM_CLIENT_PERMISSION_RESULT_UNKNOWN;
}

static void
Dan Winship's avatar
Dan Winship committed
263
update_permissions (NMClient *self, GVariant *permissions)
264 265 266 267 268 269 270 271 272 273 274 275 276
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);
	GHashTableIter iter;
	gpointer key, value;
	NMClientPermission perm;
	NMClientPermissionResult perm_result;
	GList *keys, *keys_iter;

	/* get list of old permissions for change notification */
	keys = g_hash_table_get_keys (priv->permissions);
	g_hash_table_remove_all (priv->permissions);

	if (permissions) {
Dan Winship's avatar
Dan Winship committed
277 278 279
		GVariantIter viter;
		const char *pkey, *pvalue;

280
		/* Process new permissions */
Dan Winship's avatar
Dan Winship committed
281 282 283 284
		g_variant_iter_init (&viter, permissions);
		while (g_variant_iter_next (&viter, "{&s&s}", &pkey, &pvalue)) {
			perm = nm_permission_to_client (pkey);
			perm_result = nm_permission_result_to_client (pvalue);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
			if (perm) {
				g_hash_table_insert (priv->permissions,
				                     GUINT_TO_POINTER (perm),
				                     GUINT_TO_POINTER (perm_result));

				/* Remove this permission from the list of previous permissions
				 * we'll be sending NM_CLIENT_PERMISSION_RESULT_UNKNOWN for
				 * in the change signal since it is still a known permission.
				 */
				keys = g_list_remove (keys, GUINT_TO_POINTER (perm));
			}
		}
	}

	/* Signal changes in all updated permissions */
	g_hash_table_iter_init (&iter, priv->permissions);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		g_signal_emit (self, signals[PERMISSION_CHANGED], 0,
		               GPOINTER_TO_UINT (key),
		               GPOINTER_TO_UINT (value));
	}

	/* And signal changes in all permissions that used to be valid but for
	 * some reason weren't received in the last request (if any).
	 */
	for (keys_iter = keys; keys_iter; keys_iter = g_list_next (keys_iter)) {
		g_signal_emit (self, signals[PERMISSION_CHANGED], 0,
		               GPOINTER_TO_UINT (keys_iter->data),
		               NM_CLIENT_PERMISSION_RESULT_UNKNOWN);
	}
	g_list_free (keys);
}

static gboolean
get_permissions_sync (NMClient *self, GError **error)
{
Dan Winship's avatar
Dan Winship committed
321 322
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);
	GVariant *permissions;
323

Dan Winship's avatar
Dan Winship committed
324 325 326 327 328 329 330 331 332 333
	if (nmdbus_manager_call_get_permissions_sync (priv->manager_proxy,
	                                              &permissions,
	                                              NULL, error)) {
		update_permissions (self, permissions);
		g_variant_unref (permissions);
		return TRUE;
	} else {
		update_permissions (self, NULL);
		return FALSE;
	}
334 335 336
}

static void
Dan Winship's avatar
Dan Winship committed
337 338
get_permissions_reply (GObject *object,
                       GAsyncResult *result,
339 340
                       gpointer user_data)
{
Dan Winship's avatar
Dan Winship committed
341 342 343
	NMClient *self;
	NMClientPrivate *priv;
	GVariant *permissions = NULL;
344 345
	GError *error = NULL;

Dan Winship's avatar
Dan Winship committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	/* WARNING: this may be called after the client is disposed, so we can't
	 * look at self/priv until after we've determined that that isn't the case.
	 */

	nmdbus_manager_call_get_permissions_finish (NMDBUS_MANAGER (object),
	                                            &permissions,
	                                            result, &error);
	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		/* @self has been disposed. */
		g_error_free (error);
		return;
	}

	self = user_data;
	priv = NM_CLIENT_GET_PRIVATE (self);

	update_permissions (self, permissions);

	g_clear_pointer (&permissions, g_variant_unref);
365
	g_clear_error (&error);
Dan Winship's avatar
Dan Winship committed
366
	g_clear_object (&priv->perm_call_cancellable);
367 368 369
}

static void
Dan Winship's avatar
Dan Winship committed
370
client_recheck_permissions (NMDBusManager *proxy, gpointer user_data)
371 372 373 374
{
	NMClient *self = NM_CLIENT (user_data);
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);

Dan Winship's avatar
Dan Winship committed
375 376 377 378 379 380 381 382
	if (priv->perm_call_cancellable)
		return;

	priv->perm_call_cancellable = g_cancellable_new ();
	nmdbus_manager_call_get_permissions (priv->manager_proxy,
	                                     priv->perm_call_cancellable,
	                                     get_permissions_reply,
	                                     self);
383 384 385
}

/**
386
 * nm_client_get_version:
387 388
 * @client: a #NMClient
 *
389
 * Gets NetworkManager version.
390
 *
391
 * Returns: string with the version
392
 **/
393 394
const char *
nm_client_get_version (NMClient *client)
395 396 397
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

398 399 400 401
	if (!nm_client_get_nm_running (client))
		return NULL;

	return NM_CLIENT_GET_PRIVATE (client)->version;
402 403 404
}

/**
405
 * nm_client_get_state:
406 407
 * @client: a #NMClient
 *
408
 * Gets the current daemon state.
409
 *
410
 * Returns: the current %NMState
411
 **/
412 413
NMState
nm_client_get_state (NMClient *client)
414
{
415
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
416

417
	return NM_CLIENT_GET_PRIVATE (client)->state;
418 419 420
}

/**
421
 * nm_client_get_startup:
422 423
 * @client: a #NMClient
 *
424 425
 * Tests whether the daemon is still in the process of activating
 * connections at startup.
426
 *
427
 * Returns: whether the daemon is still starting up
428
 **/
429 430
gboolean
nm_client_get_startup (NMClient *client)
431
{
432
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
433

434
	return NM_CLIENT_GET_PRIVATE (client)->startup;
435 436
}

437 438 439 440 441 442 443 444 445 446
/**
 * nm_client_get_nm_running:
 * @client: a #NMClient
 *
 * Determines whether the daemon is running.
 *
 * Returns: %TRUE if the daemon is running
 **/
gboolean
nm_client_get_nm_running (NMClient *client)
447
{
448
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
449

450
	return _nm_object_get_nm_running (NM_OBJECT (client));
451 452
}

453 454 455 456 457 458 459 460 461 462
/**
 * nm_client_networking_get_enabled:
 * @client: a #NMClient
 *
 * Whether networking is enabled or disabled.
 *
 * Returns: %TRUE if networking is enabled, %FALSE if networking is disabled
 **/
gboolean
nm_client_networking_get_enabled (NMClient *client)
463
{
464
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
465

466
	return NM_CLIENT_GET_PRIVATE (client)->networking_enabled;
467 468
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482
/**
 * nm_client_networking_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to set networking enabled, %FALSE to set networking disabled
 * @error: (allow-none): return location for a #GError, or %NULL
 *
 * Enables or disables networking.  When networking is disabled, all controlled
 * interfaces are disconnected and deactivated.  When networking is enabled,
 * all controlled interfaces are available for activation.
 *
 * Returns: %TRUE on success, %FALSE otherwise
 **/
gboolean
nm_client_networking_set_enabled (NMClient *client, gboolean enable, GError **error)
483
{
484
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
485

486 487 488 489 490 491 492
	if (!nm_client_get_nm_running (client)) {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
		return FALSE;
	}
493

494 495 496
	return nmdbus_manager_call_enable_sync (NM_CLIENT_GET_PRIVATE (client)->manager_proxy,
	                                        enable,
	                                        NULL, error);
497 498
}

499 500 501 502 503 504 505 506 507 508
/**
 * nm_client_wireless_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether the wireless is enabled.
 *
 * Returns: %TRUE if wireless is enabled
 **/
gboolean
nm_client_wireless_get_enabled (NMClient *client)
509
{
510
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
511

512
	return NM_CLIENT_GET_PRIVATE (client)->wireless_enabled;
513 514 515
}

/**
516
 * nm_client_wireless_set_enabled:
517
 * @client: a #NMClient
518
 * @enabled: %TRUE to enable wireless
519
 *
520
 * Enables or disables wireless devices.
521 522
 **/
void
523
nm_client_wireless_set_enabled (NMClient *client, gboolean enabled)
524 525 526
{
	g_return_if_fail (NM_IS_CLIENT (client));

527
	if (!nm_client_get_nm_running (client))
528
		return;
529

530 531 532 533
	_nm_object_set_property (NM_OBJECT (client),
	                         NM_DBUS_INTERFACE,
	                         "WirelessEnabled",
	                         "b", enabled);
534 535
}

536
/**
537 538
 * nm_client_wireless_hardware_get_enabled:
 * @client: a #NMClient
539
 *
540
 * Determines whether the wireless hardware is enabled.
541
 *
542
 * Returns: %TRUE if the wireless hardware is enabled
543
 **/
544 545
gboolean
nm_client_wireless_hardware_get_enabled (NMClient *client)
546
{
547
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
548

549
	return NM_CLIENT_GET_PRIVATE (client)->wireless_hw_enabled;
550 551
}

552 553 554 555 556 557 558 559 560 561
/**
 * nm_client_wwan_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether WWAN is enabled.
 *
 * Returns: %TRUE if WWAN is enabled
 **/
gboolean
nm_client_wwan_get_enabled (NMClient *client)
562
{
563
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
564

565
	return NM_CLIENT_GET_PRIVATE (client)->wwan_enabled;
566 567 568
}

/**
569
 * nm_client_wwan_set_enabled:
570
 * @client: a #NMClient
571
 * @enabled: %TRUE to enable WWAN
572
 *
573
 * Enables or disables WWAN devices.
574 575
 **/
void
576
nm_client_wwan_set_enabled (NMClient *client, gboolean enabled)
577 578
{
	g_return_if_fail (NM_IS_CLIENT (client));
579

580
	if (!nm_client_get_nm_running (client))
581
		return;
582

583 584 585 586
	_nm_object_set_property (NM_OBJECT (client),
	                         NM_DBUS_INTERFACE,
	                         "WwanEnabled",
	                         "b", enabled);
587
}
588

589
/**
590 591
 * nm_client_wwan_hardware_get_enabled:
 * @client: a #NMClient
592
 *
593
 * Determines whether the WWAN hardware is enabled.
594
 *
595
 * Returns: %TRUE if the WWAN hardware is enabled
596
 **/
597 598
gboolean
nm_client_wwan_hardware_get_enabled (NMClient *client)
599
{
600
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
601

602
	return NM_CLIENT_GET_PRIVATE (client)->wwan_hw_enabled;
603 604 605
}

/**
606
 * nm_client_wimax_get_enabled:
607 608
 * @client: a #NMClient
 *
609
 * Determines whether WiMAX is enabled.
610
 *
611
 * Returns: %TRUE if WiMAX is enabled
612
 **/
613
gboolean
614
nm_client_wimax_get_enabled (NMClient *client)
615
{
616
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
617

618
	return NM_CLIENT_GET_PRIVATE (client)->wimax_enabled;
619 620 621
}

/**
622
 * nm_client_wimax_set_enabled:
623
 * @client: a #NMClient
624
 * @enabled: %TRUE to enable WiMAX
625
 *
626
 * Enables or disables WiMAX devices.
627 628
 **/
void
629
nm_client_wimax_set_enabled (NMClient *client, gboolean enabled)
630 631 632
{
	g_return_if_fail (NM_IS_CLIENT (client));

633
	if (!nm_client_get_nm_running (client))
634 635
		return;

636 637 638 639
	_nm_object_set_property (NM_OBJECT (client),
	                         NM_DBUS_INTERFACE,
	                         "WimaxEnabled",
	                         "b", enabled);
640 641 642
}

/**
643
 * nm_client_wimax_hardware_get_enabled:
644 645
 * @client: a #NMClient
 *
646
 * Determines whether the WiMAX hardware is enabled.
647
 *
648
 * Returns: %TRUE if the WiMAX hardware is enabled
649 650
 **/
gboolean
651
nm_client_wimax_hardware_get_enabled (NMClient *client)
652
{
653
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
654

655
	return NM_CLIENT_GET_PRIVATE (client)->wimax_hw_enabled;
656 657
}

658
/**
659
 * nm_client_get_logging:
660
 * @client: a #NMClient
661 662 663 664
 * @level: (allow-none): return location for logging level string
 * @domains: (allow-none): return location for log domains string. The string is
 *   a list of domains separated by ","
 * @error: (allow-none): return location for a #GError, or %NULL
665
 *
666
 * Gets NetworkManager current logging level and domains.
667
 *
668
 * Returns: %TRUE on success, %FALSE otherwise
669
 **/
670 671
gboolean
nm_client_get_logging (NMClient *client, char **level, char **domains, GError **error)
672 673 674
{
	NMClientPrivate *priv;

675 676 677 678
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (level == NULL || *level == NULL, FALSE);
	g_return_val_if_fail (domains == NULL || *domains == NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
679 680

	priv = NM_CLIENT_GET_PRIVATE (client);
681 682 683 684 685 686 687
	if (!nm_client_get_nm_running (client)) {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
		return FALSE;
	}
688

689 690 691 692 693 694
	if (!level && !domains)
		return TRUE;

	return nmdbus_manager_call_get_logging_sync (priv->manager_proxy,
	                                             level, domains,
	                                             NULL, error);
695 696 697
}

/**
698
 * nm_client_set_logging:
699
 * @client: a #NMClient
700 701 702 703
 * @level: (allow-none): logging level to set (%NULL or an empty string for no change)
 * @domains: (allow-none): logging domains to set. The string should be a list of log
 *   domains separated by ",". (%NULL or an empty string for no change)
 * @error: (allow-none): return location for a #GError, or %NULL
704
 *
705
 * Sets NetworkManager logging level and/or domains.
706
 *
707
 * Returns: %TRUE on success, %FALSE otherwise
708 709
 **/
gboolean
710
nm_client_set_logging (NMClient *client, const char *level, const char *domains, GError **error)
711
{
712 713
	NMClientPrivate *priv;

714
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
715
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
716

717 718 719 720 721 722 723 724
	priv = NM_CLIENT_GET_PRIVATE (client);
	if (!nm_client_get_nm_running (client)) {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
		return FALSE;
	}
725

726 727
	if (!level && !domains)
		return TRUE;
728

729 730 731 732
	if (!level)
		level = "";
	if (!domains)
		domains = "";
733

734 735 736
	return nmdbus_manager_call_set_logging_sync (priv->manager_proxy,
	                                             level, domains,
	                                             NULL, error);
737 738 739
}

/**
740
 * nm_client_get_permission_result:
741
 * @client: a #NMClient
742
 * @permission: the permission for which to return the result, one of #NMClientPermission
743
 *
744 745
 * Requests the result of a specific permission, which indicates whether the
 * client can or cannot perform the action the permission represents
746
 *
747
 * Returns: the permission's result, one of #NMClientPermissionResult
748
 **/
749 750
NMClientPermissionResult
nm_client_get_permission_result (NMClient *client, NMClientPermission permission)
751
{
752
	gpointer result;
753

754 755 756 757 758
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CLIENT_PERMISSION_RESULT_UNKNOWN);

	result = g_hash_table_lookup (NM_CLIENT_GET_PRIVATE (client)->permissions,
	                              GUINT_TO_POINTER (permission));
	return GPOINTER_TO_UINT (result);
759 760 761
}

/**
762 763
 * nm_client_get_connectivity:
 * @client: an #NMClient
764
 *
765 766 767 768
 * Gets the current network connectivity state. Contrast
 * nm_client_check_connectivity() and
 * nm_client_check_connectivity_async(), which re-check the
 * connectivity state first before returning any information.
769
 *
770 771 772 773
 * Returns: the current connectivity state
 */
NMConnectivityState
nm_client_get_connectivity (NMClient *client)
774
{
775
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
776

777
	return NM_CLIENT_GET_PRIVATE (client)->connectivity;
778 779 780
}

/**
781 782 783 784
 * nm_client_check_connectivity:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @error: return location for a #GError
785
 *
786 787 788 789 790 791 792 793 794 795 796 797 798
 * Updates the network connectivity state and returns the (new)
 * current state. Contrast nm_client_get_connectivity(), which returns
 * the most recent known state without re-checking.
 *
 * This is a blocking call; use nm_client_check_connectivity_async()
 * if you do not want to block.
 *
 * Returns: the (new) current connectivity state
 */
NMConnectivityState
nm_client_check_connectivity (NMClient *client,
                              GCancellable *cancellable,
                              GError **error)
799
{
800 801
	NMClientPrivate *priv;
	guint32 connectivity;
802

803 804
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
	priv = NM_CLIENT_GET_PRIVATE (client);
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	if (nmdbus_manager_call_check_connectivity_sync (priv->manager_proxy,
	                                                 &connectivity,
	                                                 cancellable, error))
		return connectivity;
	else
		return NM_CONNECTIVITY_UNKNOWN;
}

static void
check_connectivity_cb (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	guint32 connectivity;
	GError *error = NULL;

	if (nmdbus_manager_call_check_connectivity_finish (NMDBUS_MANAGER (object),
	                                                   &connectivity,
	                                                   result, &error))
		g_simple_async_result_set_op_res_gssize (simple, connectivity);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
832 833 834
}

/**
835 836 837 838 839
 * nm_client_check_connectivity_async:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @callback: callback to call with the result
 * @user_data: data for @callback.
840
 *
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
 * Asynchronously updates the network connectivity state and invokes
 * @callback when complete. Contrast nm_client_get_connectivity(),
 * which (immediately) returns the most recent known state without
 * re-checking, and nm_client_check_connectivity(), which blocks.
 */
void
nm_client_check_connectivity_async (NMClient *client,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data)
{
	NMClientPrivate *priv;
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_CLIENT (client));
	priv = NM_CLIENT_GET_PRIVATE (client);

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_check_connectivity_async);
	nmdbus_manager_call_check_connectivity (priv->manager_proxy,
	                                        cancellable,
	                                        check_connectivity_cb, simple);
}

/**
 * nm_client_check_connectivity_finish:
 * @client: an #NMClient
 * @result: the #GAsyncResult
 * @error: return location for a #GError
870
 *
871 872 873 874 875 876 877 878 879
 * Retrieves the result of an nm_client_check_connectivity_async()
 * call.
 *
 * Returns: the (new) current connectivity state
 */
NMConnectivityState
nm_client_check_connectivity_finish (NMClient *client,
                                     GAsyncResult *result,
                                     GError **error)
880
{
881
	GSimpleAsyncResult *simple;
882

883 884 885 886 887 888 889
	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (client), nm_client_check_connectivity_async), NM_CONNECTIVITY_UNKNOWN);

	simple = G_SIMPLE_ASYNC_RESULT (result);

	if (g_simple_async_result_propagate_error (simple, error))
		return NM_CONNECTIVITY_UNKNOWN;
	return (NMConnectivityState) g_simple_async_result_get_op_res_gssize (simple);
890 891
}

892 893 894 895
/****************************************************************/
/* Devices                                                      */
/****************************************************************/

896
/**
897
 * nm_client_get_devices:
898 899
 * @client: a #NMClient
 *
900 901 902 903
 * Gets all the known network devices.  Use nm_device_get_type() or the
 * NM_IS_DEVICE_XXXX() functions to determine what kind of device member of the
 * returned array is, and then you may use device-specific methods such as
 * nm_device_ethernet_get_hw_address().
904
 *
905 906 907
 * Returns: (transfer none) (element-type NMDevice): a #GPtrArray
 * containing all the #NMDevices.  The returned array is owned by the
 * #NMClient object and should not be modified.
908
 **/
909 910
const GPtrArray *
nm_client_get_devices (NMClient *client)
911
{
912
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
913

914
	return NM_CLIENT_GET_PRIVATE (client)->devices;
915 916 917
}

/**
918
 * nm_client_get_device_by_path:
919
 * @client: a #NMClient
920
 * @object_path: the object path to search for
921
 *
922 923 924
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @object_path or %NULL if none is found.
925
 **/
926 927
NMDevice *
nm_client_get_device_by_path (NMClient *client, const char *object_path)
928
{
929 930 931
	const GPtrArray *devices;
	int i;
	NMDevice *device = NULL;
932

933 934
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (object_path, NULL);
935

936 937 938 939 940 941 942 943 944 945 946 947 948
	devices = nm_client_get_devices (client);
	if (!devices)
		return NULL;

	for (i = 0; i < devices->len; i++) {
		NMDevice *candidate = g_ptr_array_index (devices, i);
		if (!strcmp (nm_object_get_path (NM_OBJECT (candidate)), object_path)) {
			device = candidate;
			break;
		}
	}

	return device;
949 950 951
}

/**
952
 * nm_client_get_device_by_iface:
953
 * @client: a #NMClient
954
 * @iface: the interface name to search for
955
 *
956
 * Gets a #NMDevice from a #NMClient.
957
 *
958
 * Returns: (transfer none): the #NMDevice for the given @iface or %NULL if none is found.
959
 **/
960 961
NMDevice *
nm_client_get_device_by_iface (NMClient *client, const char *iface)
962
{
963 964 965
	const GPtrArray *devices;
	int i;
	NMDevice *device = NULL;
966

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (iface, NULL);

	devices = nm_client_get_devices (client);
	if (!devices)
		return NULL;

	for (i = 0; i < devices->len; i++) {
		NMDevice *candidate = g_ptr_array_index (devices, i);
		if (!strcmp (nm_device_get_iface (candidate), iface)) {
			device = candidate;
			break;
		}
	}

	return device;
983 984
}

985 986 987 988
/****************************************************************/
/* Active Connections                                           */
/****************************************************************/

989
/**
990
 * nm_client_get_active_connections:
991 992
 * @client: a #NMClient
 *
993
 * Gets the active connections.
994
 *
995 996 997
 * Returns: (transfer none) (element-type NMActiveConnection): a #GPtrArray
 *  containing all the active #NMActiveConnections.
 * The returned array is owned by the client and should not be modified.
998
 **/
999 1000
const GPtrArray *
nm_client_get_active_connections (NMClient *client)
1001
{
1002 1003
	NMClientPrivate *priv;

1004 1005
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

1006
	priv = NM_CLIENT_GET_PRIVATE (client);
1007 1008
	if (!nm_client_get_nm_running (client))
		return NULL;
1009

1010
	return priv->active_connections;
1011 1012 1013
}

/**
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
 * nm_client_get_primary_connection:
 * @client: an #NMClient
 *
 * Gets the #NMActiveConnection corresponding to the primary active
 * network device.
 *
 * In particular, when there is no VPN active, or the VPN does not
 * have the default route, this returns the active connection that has
 * the default route. If there is a VPN active with the default route,
 * then this function returns the active connection that contains the
 * route to the VPN endpoint.
 *
 * If there is no default route, or the default route is over a
 * non-NetworkManager-recognized device, this will return %NULL.
 *
 * Returns: (transfer none): the appropriate #NMActiveConnection, if
 * any
 */
NMActiveConnection *
nm_client_get_primary_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	return NM_CLIENT_GET_PRIVATE (client)->primary_connection;
}

/**
 * nm_client_get_activating_connection:
 * @client: an #NMClient
 *
 * Gets the #NMActiveConnection corresponding to a
 * currently-activating connection that is expected to become the new
 * #NMClient:primary-connection upon successful activation.
 *
 * Returns: (transfer none): the appropriate #NMActiveConnection, if
 * any.
 */
NMActiveConnection *
nm_client_get_activating_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	return NM_CLIENT_GET_PRIVATE (client)->activating_connection;
}

typedef struct {
	NMClient *client;
	GSimpleAsyncResult *simple;
	GCancellable *cancellable;
	gulong cancelled_id;
	char *active_path;
	char *new_connection_path;
} ActivateInfo;

static void
activate_info_complete (ActivateInfo *info,
                        NMActiveConnection *active,
                        GError *error)
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (info->client);

	if (active)
		g_simple_async_result_set_op_res_gpointer (info->simple, g_object_ref (active), g_object_unref);
	else
		g_simple_async_result_set_from_error (info->simple, error);
	g_simple_async_result_complete (info->simple);

	priv->pending_activations = g_slist_remove (priv->pending_activations, info);

	g_free (info->active_path);
	g_free (info->new_connection_path);
	g_object_unref (info->simple);
	if (info->cancellable) {
		if (info->cancelled_id)
			g_signal_handler_disconnect (info->cancellable, info->cancelled_id);
		g_object_unref (info->cancellable);
	}
	g_slice_free (ActivateInfo, info);
}

static void
recheck_pending_activations (NMClient *self, const char *failed_path, GError *error)
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);
	GSList *iter, *next;
	const GPtrArray *active_connections;
	gboolean found_in_active = FALSE;
	gboolean found_in_pending = FALSE;
	ActivateInfo *ainfo = NULL;
	int i;

	active_connections = nm_client_get_active_connections (self);

	/* For each pending activation, look for a active connection that has
	 * the pending activation's object path, and call pending connection's
	 * callback.
	 * If the connection to activate doesn't make it to active_connections,
	 * due to an error, we have to call the callback for failed_path.
	 */
	for (iter = priv->pending_activations; iter; iter = next) {
		ActivateInfo *info = iter->data;

		next = g_slist_next (iter);

		if (!found_in_pending && failed_path && g_strcmp0 (failed_path, info->active_path) == 0) {
			found_in_pending = TRUE;
			ainfo = info;
		}

		for (i = 0; i < active_connections->len; i++) {
			NMActiveConnection *active = g_ptr_array_index (active_connections, i);
			const char *active_path = nm_object_get_path (NM_OBJECT (active));

			if (!found_in_active && failed_path && g_strcmp0 (failed_path, active_path) == 0)
				found_in_active = TRUE;

			if (g_strcmp0 (info->active_path, active_path) == 0) {
				/* Call the pending activation's callback and it all up */
				activate_info_complete (info, active, NULL);
				break;
			}
		}
	}

	if (!found_in_active && found_in_pending) {
		/* A newly activated connection failed due to some immediate error
		 * and disappeared from active connection list.  Make sure the
		 * callback gets called.
		 */
		activate_info_complete (ainfo, NULL, error);
	}
}

static void
activation_cancelled (GCancellable *cancellable,
                      gpointer user_data)
{
	ActivateInfo *info = user_data;
	GError *error = NULL;

	if (!g_cancellable_set_error_if_cancelled (cancellable, &error))
		return;

	activate_info_complete (info, NULL, error);
	g_clear_error (&error);
}

static void
activate_cb (GObject *object,
             GAsyncResult *result,
             gpointer user_data)
{
	ActivateInfo *info = user_data;
	GError *error = NULL;

	if (nmdbus_manager_call_activate_connection_finish (NMDBUS_MANAGER (object),
	                                                    &info->active_path,
	                                                    result, &error)) {
		if (info->cancellable) {
			info->cancelled_id = g_signal_connect (info->cancellable, "cancelled",
			                                       G_CALLBACK (activation_cancelled), info);
		}

		recheck_pending_activations (info->client, NULL, NULL);
	} else {
		activate_info_complete (info, NULL, error);
		g_clear_error (&error);
	}
}

/**
 * nm_client_activate_connection_async:
1186
 * @client: a #NMClient
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
 * @connection: (allow-none): an #NMConnection
 * @device: (allow-none): the #NMDevice
 * @specific_object: (allow-none): the object path of a connection-type-specific
 *   object this activation should use. This parameter is currently ignored for
 *   wired and mobile broadband connections, and the value of %NULL should be used
 *   (ie, no specific object).  For Wi-Fi or WiMAX connections, pass the object
 *   path of a #NMAccessPoint or #NMWimaxNsp owned by @device, which you can
 *   get using nm_object_get_path(), and which will be used to complete the
 *   details of the newly added connection.
 * @cancellable: a #GCancellable, or %NULL
 * @callback: callback to be called when the activation has started
 * @user_data: caller-specific data passed to @callback
1199
 *
1200 1201 1202 1203 1204 1205 1206 1207
 * Asynchronously starts a connection to a particular network using the
 * configuration settings from @connection and the network device @device.
 * Certain connection types also take a "specific object" which is the object
 * path of a connection- specific object, like an #NMAccessPoint for Wi-Fi
 * connections, or an #NMWimaxNsp for WiMAX connections, to which you wish to
 * connect.  If the specific object is not given, NetworkManager can, in some
 * cases, automatically determine which network to connect to given the settings
 * in @connection.
1208
 *
1209 1210 1211 1212 1213 1214 1215
 * If @connection is not given for a device-based activation, NetworkManager
 * picks the best available connection for the device and activates it.
 *
 * Note that the callback is invoked when NetworkManager has started activating
 * the new connection, not when it finishes. You can used the returned
 * #NMActiveConnection object (in particular, #NMActiveConnection:state) to
 * track the activation to its completion.
1216
 **/
1217 1218 1219 1220 1221 1222 1223 1224
void
nm_client_activate_connection_async (NMClient *client,
                                     NMConnection *connection,
                                     NMDevice *device,
                                     const char *specific_object,
                                     GCancellable *cancellable,
                                     GAsyncReadyCallback callback,
                                     gpointer user_data)
1225
{
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	NMClientPrivate *priv;
	ActivateInfo *info;

	g_return_if_fail (NM_IS_CLIENT (client));
	if (device)
		g_return_if_fail (NM_IS_DEVICE (device));
	if (connection)
		g_return_if_fail (NM_IS_CONNECTION (connection));

	if (!nm_client_get_nm_running (client)) {
		g_simple_async_report_error_in_idle (G_OBJECT (client), callback, user_data,
		                                     NM_CLIENT_ERROR,
		                                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                                     "NetworkManager is not running");
		return;
	}

	info = g_slice_new0 (ActivateInfo);
	info->client = client;
	info->simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                          nm_client_activate_connection_async);
	info->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1248

1249 1250 1251 1252 1253 1254 1255 1256 1257
	priv = NM_CLIENT_GET_PRIVATE (client);
	priv->pending_activations = g_slist_prepend (priv->pending_activations, info);

	nmdbus_manager_call_activate_connection (priv->manager_proxy,
	                                         connection ? nm_connection_get_path (connection) : "/",
	                                         device ? nm_object_get_path (NM_OBJECT (device)) : "/",
	                                         specific_object ? specific_object : "/",
	                                         cancellable,
	                                         activate_cb, info);
1258 1259 1260
}

/**
1261 1262 1263 1264
 * nm_client_activate_connection_finish:
 * @client: an #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
1265
 *
1266
 * Gets the result of a call to nm_client_activate_connection_async().
1267
 *
1268 1269
 * Returns: (transfer full): the new #NMActiveConnection on success, %NULL on
 *   failure, in which case @error will be set.
1270
 **/
1271 1272 1273 1274
NMActiveConnection *
nm_client_activate_connection_finish (NMClient *client,
                                      GAsyncResult *result,
                                      GError **error)
1275
{
1276
	GSimpleAsyncResult *simple;
1277

1278 1279 1280 1281 1282 1283 1284
	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (client), nm_client_activate_connection_async), NULL);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return NULL;
	else
		return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1285 1286
}

1287 1288 1289 1290
static void
add_activate_cb (GObject *object,
                 GAsyncResult *result,
                 gpointer user_data)
1291
{
1292 1293
	ActivateInfo *info = user_data;
	GError *error = NULL;
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	if (nmdbus_manager_call_add_and_activate_connection_finish (NMDBUS_MANAGER (object),
	                                                            NULL,
	                                                            &info->active_path,
	                                                            result, &error)) {
		if (info->cancellable) {
			info->cancelled_id = g_signal_connect (info->cancellable, "cancelled",
			                                       G_CALLBACK (activation_cancelled), info);
		}

		recheck_pending_activations (info->client, NULL, NULL);
	} else {
		activate_info_complete (info, NULL, error);
		g_clear_error (&error);
	}
1309 1310 1311
}

/**
1312
 * nm_client_add_and_activate_connection_async:
1313
 * @client: a #NMClient
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
 * @partial: (allow-none): an #NMConnection to add; the connection may be
 *   partially filled (or even %NULL) and will be completed by NetworkManager
 *   using the given @device and @specific_object before being added
 * @device: the #NMDevice
 * @specific_object: (allow-none): the object path of a connection-type-specific
 *   object this activation should use. This parameter is currently ignored for
 *   wired and mobile broadband connections, and the value of %NULL should be used
 *   (ie, no specific object).  For Wi-Fi or WiMAX connections, pass the object
 *   path of a #NMAccessPoint or #NMWimaxNsp owned by @device, which you can
 *   get using nm_object_get_path(), and which will be used to complete the
 *   details of the newly added connection.
 * @cancellable: a #GCancellable, or %NULL
 * @callback: callback to be called when the activation has started
 * @user_data: caller-specific data passed to @callback
1328
 *
1329 1330 1331 1332 1333
 * Adds a new connection using the given details (if any) as a template,
 * automatically filling in missing settings with the capabilities of the given
 * device and specific object.  The new connection is then asynchronously
 * activated as with nm_client_activate_connection_async(). Cannot be used for
 * VPN connections at this time.
1334
 *
1335 1336 1337 1338
 * Note that the callback is invoked when NetworkManager has started activating
 * the new connection, not when it finishes. You can used the returned
 * #NMActiveConnection object (in particular, #NMActiveConnection:state) to
 * track the activation to its completion.
1339
 **/
1340 1341 1342 1343 1344 1345 1346 1347
void
nm_client_add_and_activate_connection_async (NMClient *client,
                                             NMConnection *partial,
                                             NMDevice *device,
                                             const char *specific_object,
                                             GCancellable *cancellable,
                                             GAsyncReadyCallback callback,
                                             gpointer user_data)
1348
{
1349 1350 1351 1352 1353 1354 1355 1356
	NMClientPrivate *priv;
	GVariant *dict = NULL;
	ActivateInfo *info;

	g_return_if_fail (NM_IS_CLIENT (client));
	g_return_if_fail (NM_IS_DEVICE (device));
	if (partial)
		g_return_if_fail (NM_IS_CONNECTION (partial));
1357

1358
	if (!nm_client_get_nm_running (client)) {
1359 1360 1361 1362 1363
		g_simple_async_report_error_in_idle (G_OBJECT (client), callback, user_data,
		                                     NM_CLIENT_ERROR,
		                                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                                     "NetworkManager is not running");
		return;
1364
	}
1365

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	info = g_slice_new0 (ActivateInfo);
	info->client = client;
	info->simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                          nm_client_add_and_activate_connection_async);
	info->cancellable = cancellable ? g_object_ref (cancellable) : NULL;

	priv = NM_CLIENT_GET_PRIVATE (client);
	priv->pending_activations = g_slist_prepend (priv->pending_activations, info);

	if (partial)
		dict = nm_connection_to_dbus (partial, NM_CONNECTION_SERIALIZE_ALL);
	if (!dict)
		dict = g_variant_new_array (G_VARIANT_TYPE ("{sa{sv}}"), NULL, 0);

	nmdbus_manager_call_add_and_activate_connection (priv->manager_proxy,
	                                                 dict,
	                                                 nm_object_get_path (NM_OBJECT (device)),
	                                                 specific_object ? specific_object : "/",
	                                                 cancellable,
	                                                 add_activate_cb, info);
1386 1387 1388
}

/**
1389 1390 1391 1392
 * nm_client_add_and_activate_connection_finish:
 * @client: an #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
1393
 *
1394
 * Gets the result of a call to nm_client_add_and_activate_connection_async().
1395
 *
1396 1397 1398 1399 1400
 * You can call nm_active_connection_get_connection() on the returned
 * #NMActiveConnection to find the path of the created #NMConnection.
 *
 * Returns: (transfer full): the new #NMActiveConnection on success, %NULL on
 *   failure, in which case @error will be set.
1401
 **/