nm-client.c 75.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- 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.
 *
18 19
 * Copyright 2007 - 2008 Novell, Inc.
 * Copyright 2007 - 2013 Red Hat, Inc.
20
 */
21

22
#include "nm-default.h"
23

24 25
#include "nm-client.h"

26
#include <dbus/dbus-glib.h>
27
#include <string.h>
28 29

#include "nm-utils.h"
30

31
#include "nm-device-ethernet.h"
32
#include "nm-device-wifi.h"
33
#include "nm-device-private.h"
34 35 36
#include "nm-types-private.h"
#include "nm-object-private.h"
#include "nm-active-connection.h"
37
#include "nm-vpn-connection.h"
38 39
#include "nm-object-cache.h"
#include "nm-dbus-glib-types.h"
40

41
void _nm_device_wifi_set_wireless_enabled (NMDeviceWifi *device, gboolean enabled);
42

43 44 45 46 47 48 49 50 51
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);
                         )
52

53 54 55
#define NM_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CLIENT, NMClientPrivate))

typedef struct {
56
	DBusGProxy *client_proxy;
57 58
	DBusGProxy *bus_proxy;
	gboolean manager_running;
59
	char *version;
60
	NMState state;
61
	gboolean startup;
62
	GPtrArray *devices;
63
	GPtrArray *all_devices;
64
	GPtrArray *active_connections;
65
	NMConnectivityState connectivity;
66 67
	NMActiveConnection *primary_connection;
	NMActiveConnection *activating_connection;
68

69 70 71
	DBusGProxyCall *perm_call;
	GHashTable *permissions;

72 73 74 75 76
	/* Activations waiting for their NMActiveConnection
	 * to appear and then their callback to be called.
	 */
	GSList *pending_activations;

77
	gboolean networking_enabled;
78 79
	gboolean wireless_enabled;
	gboolean wireless_hw_enabled;
80 81 82

	gboolean wwan_enabled;
	gboolean wwan_hw_enabled;
Tambet Ingo's avatar
Tambet Ingo committed
83 84 85

	gboolean wimax_enabled;
	gboolean wimax_hw_enabled;
86 87
} NMClientPrivate;

88 89
enum {
	PROP_0,
90
	PROP_VERSION,
91
	PROP_STATE,
92
	PROP_STARTUP,
93
	PROP_MANAGER_RUNNING,
94
	PROP_NETWORKING_ENABLED,
95 96
	PROP_WIRELESS_ENABLED,
	PROP_WIRELESS_HARDWARE_ENABLED,
97 98
	PROP_WWAN_ENABLED,
	PROP_WWAN_HARDWARE_ENABLED,
Tambet Ingo's avatar
Tambet Ingo committed
99 100
	PROP_WIMAX_ENABLED,
	PROP_WIMAX_HARDWARE_ENABLED,
101
	PROP_ACTIVE_CONNECTIONS,
102
	PROP_CONNECTIVITY,
103 104
	PROP_PRIMARY_CONNECTION,
	PROP_ACTIVATING_CONNECTION,
105
	PROP_DEVICES,
106
	PROP_ALL_DEVICES,
107 108 109 110

	LAST_PROP
};

111 112 113
enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
114 115
	ANY_DEVICE_ADDED,
	ANY_DEVICE_REMOVED,
116
	PERMISSION_CHANGED,
117 118 119 120 121 122

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

123
static void proxy_name_owner_changed (DBusGProxy *proxy,
124 125 126 127
                                      const char *name,
                                      const char *old_owner,
                                      const char *new_owner,
                                      gpointer user_data);
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/**********************************************************************/

/**
 * nm_client_error_quark:
 *
 * Registers an error quark for #NMClient if necessary.
 *
 * Returns: the error quark used for #NMClient errors.
 *
 * Since: 0.9.10
 **/
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;
}

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

152 153 154
static void
nm_client_init (NMClient *client)
{
155 156 157
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);

	priv->state = NM_STATE_UNKNOWN;
158

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

162 163 164 165 166 167
static void
poke_wireless_devices_with_rf_status (NMClient *client)
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);
	int i;

168 169
	for (i = 0; priv->all_devices && (i < priv->all_devices->len); i++) {
		NMDevice *device = g_ptr_array_index (priv->all_devices, i);
170

171
		if (NM_IS_DEVICE_WIFI (device))
172
			_nm_device_wifi_set_wireless_enabled (NM_DEVICE_WIFI (device), priv->wireless_enabled);
173 174 175 176 177 178 179
	}
}

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

static void
183
register_properties (NMClient *client)
184 185
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);
186 187 188
	const NMPropertiesInfo property_info[] = {
		{ NM_CLIENT_VERSION,                   &priv->version },
		{ NM_CLIENT_STATE,                     &priv->state },
189
		{ NM_CLIENT_STARTUP,                   &priv->startup },
190 191 192 193 194 195 196
		{ 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 },
197
		{ NM_CLIENT_ACTIVE_CONNECTIONS,        &priv->active_connections, NULL, NM_TYPE_ACTIVE_CONNECTION },
198
		{ NM_CLIENT_CONNECTIVITY,              &priv->connectivity },
199 200
		{ NM_CLIENT_PRIMARY_CONNECTION,        &priv->primary_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_CLIENT_ACTIVATING_CONNECTION,     &priv->activating_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
201
		{ NM_CLIENT_DEVICES,                   &priv->devices, NULL, NM_TYPE_DEVICE, "device" },
202
		{ NM_CLIENT_ALL_DEVICES,               &priv->all_devices, NULL, NM_TYPE_DEVICE, "any-device" },
203 204 205
		{ NULL },
	};

206 207 208
	_nm_object_register_properties (NM_OBJECT (client),
	                                priv->client_proxy,
	                                property_info);
209 210
}

211 212 213
#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"
214
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX       "org.freedesktop.NetworkManager.enable-disable-wimax"
215 216 217 218
#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"
219
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM     "org.freedesktop.NetworkManager.settings.modify.system"
220
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN        "org.freedesktop.NetworkManager.settings.modify.own"
221
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME   "org.freedesktop.NetworkManager.settings.modify.hostname"
222 223 224 225 226 227 228 229 230 231

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;
232 233
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX;
234 235 236 237 238 239 240 241
	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;
242 243
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM;
244 245
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN;
246 247
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME;
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	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
265
update_permissions (NMClient *self, GHashTable *permissions)
266 267 268 269 270 271 272 273 274 275 276 277
{
	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);

278
	if (permissions) {
279 280 281 282 283 284 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
		/* Process new permissions */
		g_hash_table_iter_init (&iter, permissions);
		while (g_hash_table_iter_next (&iter, &key, &value)) {
			perm = nm_permission_to_client ((const char *) key);
			perm_result = nm_permission_result_to_client ((const char *) value);
			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);
}

317 318
static gboolean
get_permissions_sync (NMClient *self, GError **error)
319 320
{
	gboolean success;
321
	gs_unref_hashtable GHashTable *permissions = NULL;
322 323

	success = dbus_g_proxy_call_with_timeout (NM_CLIENT_GET_PRIVATE (self)->client_proxy,
324
	                                          "GetPermissions", 3000, error,
325 326 327
	                                          G_TYPE_INVALID,
	                                          DBUS_TYPE_G_MAP_OF_STRING, &permissions, G_TYPE_INVALID);
	update_permissions (self, success ? permissions : NULL);
328
	return success;
329 330 331 332
}

static void
get_permissions_reply (DBusGProxy *proxy,
333
                       DBusGProxyCall *call,
334
                       gpointer user_data)
335
{
336
	NMClient *self = NM_CLIENT (user_data);
337 338
	gs_unref_hashtable GHashTable *permissions = NULL;
	gs_free_error GError *error = NULL;
339

340 341 342
	dbus_g_proxy_end_call (proxy, call, &error,
	                       DBUS_TYPE_G_MAP_OF_STRING, &permissions,
	                       G_TYPE_INVALID);
343 344
	NM_CLIENT_GET_PRIVATE (self)->perm_call = NULL;
	update_permissions (NM_CLIENT (user_data), error ? NULL : permissions);
345 346 347 348 349 350 351 352
}

static void
client_recheck_permissions (DBusGProxy *proxy, gpointer user_data)
{
	NMClient *self = NM_CLIENT (user_data);
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);

353
	if (!priv->perm_call) {
354 355 356
		priv->perm_call = dbus_g_proxy_begin_call (NM_CLIENT_GET_PRIVATE (self)->client_proxy, "GetPermissions",
		                                           get_permissions_reply, self, NULL,
		                                           G_TYPE_INVALID);
357
	}
358 359
}

360 361 362 363
/**
 * nm_client_get_devices:
 * @client: a #NMClient
 *
364
 * Gets all the known network devices.  Use nm_device_get_type() or the
Dan Winship's avatar
Dan Winship committed
365 366 367
 * <literal>NM_IS_DEVICE_XXXX</literal> 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().
368
 *
369
 * Returns: (transfer none) (element-type NMDevice): a #GPtrArray
370
 * containing all the #NMDevices.  The returned array is owned by the
371
 * #NMClient object and should not be modified.
372 373 374
 **/
const GPtrArray *
nm_client_get_devices (NMClient *client)
375
{
376
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
377

378
	_nm_object_ensure_inited (NM_OBJECT (client));
379

380
	return handle_ptr_array_return (NM_CLIENT_GET_PRIVATE (client)->devices);
381
}
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/**
 * nm_client_get_all_devices:
 * @client: a #NMClient
 *
 * Gets both real devices and device placeholders (eg, software devices which
 * do not currently exist, but could be created automatically by NetworkManager
 * if one of their NMDevice::ActivatableConnections was activated).  Use
 * nm_device_is_real() to determine whether each device is a real device or
 * a placeholder.
 *
 * Use nm_device_get_type() or the NM_IS_DEVICE_XXXX() functions to determine
 * what kind of device each member of the returned array is, and then you may
 * use device-specific methods such as nm_device_ethernet_get_hw_address().
 *
 * 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.
 *
 * Since: 1.2
 **/
const GPtrArray *
nm_client_get_all_devices (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	return NM_CLIENT_GET_PRIVATE (client)->all_devices;
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/**
 * nm_client_get_device_by_path:
 * @client: a #NMClient
 * @object_path: the object path to search for
 *
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @object_path or %NULL if none is found.
 **/
NMDevice *
nm_client_get_device_by_path (NMClient *client, const char *object_path)
{
	const GPtrArray *devices;
	int i;
	NMDevice *device = NULL;
426

427 428
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (object_path, NULL);
429

430 431 432
	devices = nm_client_get_devices (client);
	if (!devices)
		return NULL;
433

434 435 436 437 438 439
	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;
		}
440 441
	}

442
	return device;
443 444
}

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/**
 * nm_client_get_device_by_iface:
 * @client: a #NMClient
 * @iface: the interface name to search for
 *
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @iface or %NULL if none is found.
 **/
NMDevice *
nm_client_get_device_by_iface (NMClient *client, const char *iface)
{
	const GPtrArray *devices;
	int i;
	NMDevice *device = NULL;

	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;
}

479 480
typedef struct {
	NMClient *client;
481
	NMClientActivateFn act_fn;
482
	NMClientAddActivateFn add_act_fn;
483 484
	char *active_path;
	char *new_connection_path;
485
	guint idle_id;
486
	gpointer user_data;
487 488 489 490 491
} ActivateInfo;

static void
activate_info_free (ActivateInfo *info)
{
492 493
	if (info->idle_id)
		g_source_remove (info->idle_id);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	g_free (info->active_path);
	g_free (info->new_connection_path);
	memset (info, 0, sizeof (*info));
	g_slice_free (ActivateInfo, info);
}

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

	if (info->act_fn)
		info->act_fn (info->client, error ? NULL : active, error, info->user_data);
	else if (info->add_act_fn) {
		info->add_act_fn (info->client,
		                  error ? NULL : active,
		                  error ? NULL : info->new_connection_path,
		                  error,
		                  info->user_data);
	} else if (error)
		g_warning ("Device activation failed: (%d) %s", error->code, error->message);

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

static void
522
recheck_pending_activations (NMClient *self, const char *failed_path, GError *error)
523 524
{
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (self);
Dan Winship's avatar
Dan Winship committed
525
	GSList *iter, *next;
526
	const GPtrArray *active_connections;
527 528 529
	gboolean found_in_active = FALSE;
	gboolean found_in_pending = FALSE;
	ActivateInfo *ainfo = NULL;
530 531 532 533
	int i;

	active_connections = nm_client_get_active_connections (self);

534 535 536 537 538
	/* 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.
539
	 */
Dan Winship's avatar
Dan Winship committed
540
	for (iter = priv->pending_activations; iter; iter = next) {
541 542
		ActivateInfo *info = iter->data;

Dan Winship's avatar
Dan Winship committed
543 544
		next = g_slist_next (iter);

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

550 551 552 553 554 555
		for (i = 0; active_connections && i < active_connections->len; i++) {
			NMActiveConnection *active = g_ptr_array_index (active_connections, i);
			const char *active_path = nm_object_get_path (NM_OBJECT (active));

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

557
			if (g_strcmp0 (info->active_path, active_path) == 0) {
558
				/* Call the pending activation's callback and it all up */
559 560 561 562 563 564
				activate_info_complete (info, active, NULL);
				activate_info_free (info);
				break;
			}
		}
	}
565 566 567 568 569 570 571 572 573

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

static void
577
activate_cb (DBusGProxy *proxy,
578
             DBusGProxyCall *call,
579
             gpointer user_data)
580
{
581
	ActivateInfo *info = user_data;
582 583
	char *path;
	GError *error = NULL;
584

585 586 587
	dbus_g_proxy_end_call (proxy, call, &error,
	                       DBUS_TYPE_G_OBJECT_PATH, &path,
	                       G_TYPE_INVALID);
588 589 590
	if (error) {
		activate_info_complete (info, NULL, error);
		activate_info_free (info);
591
		g_clear_error (&error);
592
	} else {
593
		info->active_path = path;
594
		recheck_pending_activations (info->client, NULL, NULL);
595
	}
596
}
597

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
static gboolean
activate_nm_not_running (gpointer user_data)
{
	ActivateInfo *info = user_data;
	GError *error;

	info->idle_id = 0;

	error = g_error_new_literal (NM_CLIENT_ERROR,
	                             NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
	                             "NetworkManager is not running");
	activate_info_complete (info, NULL, error);
	activate_info_free (info);
	g_clear_error (&error);
	return FALSE;
}

615 616 617
/**
 * nm_client_activate_connection:
 * @client: a #NMClient
618
 * @connection: (allow-none): an #NMConnection
619
 * @device: (allow-none): the #NMDevice
620 621
 * @specific_object: (allow-none): the object path of a connection-type-specific
 *   object this activation should use. This parameter is currently ignored for
Martin Pitt's avatar
Martin Pitt committed
622
 *   wired and mobile broadband connections, and the value of %NULL should be used
623
 *   (ie, no specific object).  For Wi-Fi or WiMAX connections, pass the object
624 625 626
 *   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.
627 628 629
 * @callback: (scope async) (allow-none): the function to call when the call is done
 * @user_data: (closure): user data to pass to the callback function
 *
630 631 632
 * 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-
633
 * specific object, like an #NMAccessPoint for Wi-Fi connections, or an
634 635 636
 * #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.
637 638 639
 *
 * If @connection is not given for a device-based activation, NetworkManager
 * picks the best available connection for the device and activates it.
640 641 642 643 644
 *
 * 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.
645 646 647
 **/
void
nm_client_activate_connection (NMClient *client,
648 649 650 651 652
                               NMConnection *connection,
                               NMDevice *device,
                               const char *specific_object,
                               NMClientActivateFn callback,
                               gpointer user_data)
653
{
654
	NMClientPrivate *priv;
655
	ActivateInfo *info;
656

657 658 659
	g_return_if_fail (NM_IS_CLIENT (client));
	if (device)
		g_return_if_fail (NM_IS_DEVICE (device));
660 661
	if (connection)
		g_return_if_fail (NM_IS_CONNECTION (connection));
662

663
	info = g_slice_new0 (ActivateInfo);
664 665 666
	info->act_fn = callback;
	info->user_data = user_data;
	info->client = client;
667

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

671 672 673 674 675
	if (priv->manager_running == FALSE) {
		info->idle_id = g_idle_add (activate_nm_not_running, info);
		return;
	}

676 677
	dbus_g_proxy_begin_call (priv->client_proxy, "ActivateConnection",
	                         activate_cb, info, NULL,
678
	                         DBUS_TYPE_G_OBJECT_PATH, connection ? nm_connection_get_path (connection) : "/",
679 680 681
	                         DBUS_TYPE_G_OBJECT_PATH, device ? nm_object_get_path (NM_OBJECT (device)) : "/",
	                         DBUS_TYPE_G_OBJECT_PATH, specific_object ? specific_object : "/",
	                         G_TYPE_INVALID);
682 683
}

684
static void
685
add_activate_cb (DBusGProxy *proxy,
686
                 DBusGProxyCall *call,
687
                 gpointer user_data)
688
{
689
	ActivateInfo *info = user_data;
690 691 692
	char *connection_path;
	char *active_path;
	GError *error = NULL;
693

694 695 696 697
	dbus_g_proxy_end_call (proxy, call, &error,
	                       DBUS_TYPE_G_OBJECT_PATH, &connection_path,
	                       DBUS_TYPE_G_OBJECT_PATH, &active_path,
	                       G_TYPE_INVALID);
698 699 700 701
	if (error) {
		activate_info_complete (info, NULL, error);
		activate_info_free (info);
	} else {
702 703
		info->new_connection_path = connection_path;
		info->active_path = active_path;
704
		recheck_pending_activations (info->client, NULL, NULL);
705
	}
706 707
}

708 709 710
/**
 * nm_client_add_and_activate_connection:
 * @client: a #NMClient
711
 * @partial: (allow-none): an #NMConnection to add; the connection may be
Martin Pitt's avatar
Martin Pitt committed
712
 *   partially filled (or even %NULL) and will be completed by NetworkManager
713
 *   using the given @device and @specific_object before being added
714 715 716
 * @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
Martin Pitt's avatar
Martin Pitt committed
717
 *   wired and mobile broadband connections, and the value of %NULL should be used
718
 *   (ie, no specific object).  For Wi-Fi or WiMAX connections, pass the object
719 720
 *   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
721 722 723 724
 *   details of the newly added connection.
 * @callback: (scope async) (allow-none): the function to call when the call is done
 * @user_data: (closure): user data to pass to the callback function
 *
725 726 727
 * 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 activated.
728
 * Cannot be used for VPN connections at this time.
729 730 731 732 733
 *
 * 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.
734 735 736 737 738 739 740 741 742
 **/
void
nm_client_add_and_activate_connection (NMClient *client,
                                       NMConnection *partial,
                                       NMDevice *device,
                                       const char *specific_object,
                                       NMClientAddActivateFn callback,
                                       gpointer user_data)
{
743
	NMClientPrivate *priv;
744
	ActivateInfo *info;
745 746 747 748 749
	GHashTable *hash = NULL;

	g_return_if_fail (NM_IS_CLIENT (client));
	g_return_if_fail (NM_IS_DEVICE (device));

750
	info = g_slice_new0 (ActivateInfo);
751 752 753 754 755 756
	info->add_act_fn = callback;
	info->user_data = user_data;
	info->client = client;

	if (partial)
		hash = nm_connection_to_hash (partial, NM_SETTING_HASH_FLAG_ALL);
757
	if (!hash)
758
		hash = g_hash_table_new (g_str_hash, g_str_equal);
759 760 761 762

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

763 764 765 766 767 768 769 770 771 772
	if (priv->manager_running) {
		dbus_g_proxy_begin_call (priv->client_proxy, "AddAndActivateConnection",
		                         add_activate_cb, info, NULL,
		                         DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash,
		                         DBUS_TYPE_G_OBJECT_PATH, nm_object_get_path (NM_OBJECT (device)),
		                         DBUS_TYPE_G_OBJECT_PATH, specific_object ? specific_object : "/",
		                         G_TYPE_INVALID);
	} else
		info->idle_id = g_idle_add (activate_nm_not_running, info);

773
	g_hash_table_unref (hash);
774 775
}

776 777 778
static void
active_connections_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
{
779 780 781 782 783 784 785 786
	recheck_pending_activations (NM_CLIENT (object), NULL, NULL);
}

static void
object_creation_failed_cb (GObject *object, GError *error, char *failed_path)
{
	if (error)
		recheck_pending_activations (NM_CLIENT (object), failed_path, error);
787 788
}

789 790 791 792 793 794 795 796 797
/**
 * nm_client_deactivate_connection:
 * @client: a #NMClient
 * @active: the #NMActiveConnection to deactivate
 *
 * Deactivates an active #NMActiveConnection.
 **/
void
nm_client_deactivate_connection (NMClient *client, NMActiveConnection *active)
798
{
799 800 801
	NMClientPrivate *priv;
	const char *path;
	GError *error = NULL;
802

803 804 805 806
	g_return_if_fail (NM_IS_CLIENT (client));
	g_return_if_fail (NM_IS_ACTIVE_CONNECTION (active));

	priv = NM_CLIENT_GET_PRIVATE (client);
807 808 809
	if (!priv->manager_running)
		return;

810
	path = nm_object_get_path (NM_OBJECT (active));
811 812 813 814
	if (!dbus_g_proxy_call (priv->client_proxy, "DeactivateConnection", &error,
	                        DBUS_TYPE_G_OBJECT_PATH, path,
	                        G_TYPE_INVALID,
	                        G_TYPE_INVALID)) {
Thomas Haller's avatar
Thomas Haller committed
815 816
		g_warning ("Could not deactivate connection '%s': %s",
		           path, NM_G_ERROR_MSG (error));
817
		g_clear_error (&error);
818 819 820
	}
}

821 822 823 824 825 826
/**
 * nm_client_get_active_connections:
 * @client: a #NMClient
 *
 * Gets the active connections.
 *
827
 * Returns: (transfer none) (element-type NMActiveConnection): a #GPtrArray
828
 *  containing all the active #NMActiveConnections.
829 830
 * The returned array is owned by the client and should not be modified.
 **/
831
const GPtrArray *
832
nm_client_get_active_connections (NMClient *client)
833
{
834
	NMClientPrivate *priv;
835

836
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
837

838 839
	_nm_object_ensure_inited (NM_OBJECT (client));

840 841 842
	priv = NM_CLIENT_GET_PRIVATE (client);
	if (!priv->manager_running)
		return NULL;
843

844 845
	return handle_ptr_array_return (priv->active_connections);
}
846

847 848 849 850 851 852 853 854 855 856 857 858
/**
 * 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)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
859

860
	_nm_object_ensure_inited (NM_OBJECT (client));
861 862
	return NM_CLIENT_GET_PRIVATE (client)->wireless_enabled;
}
863

864 865 866 867 868 869 870 871 872 873
/**
 * nm_client_wireless_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to enable wireless
 *
 * Enables or disables wireless devices.
 **/
void
nm_client_wireless_set_enabled (NMClient *client, gboolean enabled)
{
874
	GValue value = G_VALUE_INIT;
875

876
	g_return_if_fail (NM_IS_CLIENT (client));
877

878 879 880
	if (!NM_CLIENT_GET_PRIVATE (client)->manager_running)
		return;

881 882
	g_value_init (&value, G_TYPE_BOOLEAN);
	g_value_set_boolean (&value, enabled);
883

884
	_nm_object_set_property (NM_OBJECT (client),
885 886 887
	                         NM_DBUS_INTERFACE,
	                         "WirelessEnabled",
	                         &value);
888
}
Tambet Ingo's avatar
Tambet Ingo committed
889

890 891 892 893 894 895 896 897 898 899 900 901
/**
 * nm_client_wireless_hardware_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether the wireless hardware is enabled.
 *
 * Returns: %TRUE if the wireless hardware is enabled
 **/
gboolean
nm_client_wireless_hardware_get_enabled (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
Tambet Ingo's avatar
Tambet Ingo committed
902

903
	_nm_object_ensure_inited (NM_OBJECT (client));
904 905
	return NM_CLIENT_GET_PRIVATE (client)->wireless_hw_enabled;
}
906

907 908 909 910 911 912 913 914 915 916 917 918
/**
 * 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)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
919

920
	_nm_object_ensure_inited (NM_OBJECT (client));
921 922
	return NM_CLIENT_GET_PRIVATE (client)->wwan_enabled;
}
923

924 925 926 927 928 929 930 931 932 933
/**
 * nm_client_wwan_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to enable WWAN
 *
 * Enables or disables WWAN devices.
 **/
void
nm_client_wwan_set_enabled (NMClient *client, gboolean enabled)
{
934
	GValue value = G_VALUE_INIT;
935

936 937
	g_return_if_fail (NM_IS_CLIENT (client));

938 939 940
	if (!NM_CLIENT_GET_PRIVATE (client)->manager_running)
		return;

941 942 943 944 945 946 947
	g_value_init (&value, G_TYPE_BOOLEAN);
	g_value_set_boolean (&value, enabled);

	_nm_object_set_property (NM_OBJECT (client),
	                         NM_DBUS_INTERFACE,
	                         "WwanEnabled",
	                         &value);
948 949
}

950
/**
951 952
 * nm_client_wwan_hardware_get_enabled:
 * @client: a #NMClient
953
 *
954
 * Determines whether the WWAN hardware is enabled.
955
 *
956
 * Returns: %TRUE if the WWAN hardware is enabled
957
 **/
958 959
gboolean
nm_client_wwan_hardware_get_enabled (NMClient *client)
960
{
961
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
962

963
	_nm_object_ensure_inited (NM_OBJECT (client));
964 965
	return NM_CLIENT_GET_PRIVATE (client)->wwan_hw_enabled;
}
966

967 968 969 970 971 972 973 974 975 976 977 978 979
/**
 * nm_client_wimax_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether WiMAX is enabled.
 *
 * Returns: %TRUE if WiMAX is enabled
 **/
gboolean
nm_client_wimax_get_enabled (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

980
	_nm_object_ensure_inited (NM_OBJECT (client));
981
	return NM_CLIENT_GET_PRIVATE (client)->wimax_enabled;
982 983
}

984 985 986 987 988 989 990 991 992
/**
 * nm_client_wimax_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to enable WiMAX
 *
 * Enables or disables WiMAX devices.
 **/
void
nm_client_wimax_set_enabled (NMClient *client, gboolean enabled)
993
{
994
	GValue value = G_VALUE_INIT;
995

996 997
	g_return_if_fail (NM_IS_CLIENT (client));

998 999 1000
	if (!NM_CLIENT_GET_PRIVATE (client)->manager_running)
		return;

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	g_value_init (&value, G_TYPE_BOOLEAN);
	g_value_set_boolean (&value, enabled);

	_nm_object_set_property (NM_OBJECT (client),
	                         NM_DBUS_INTERFACE,
	                         "WimaxEnabled",
	                         &value);
}

/**
 * nm_client_wimax_hardware_get_enabled:
 * @client: a #NMClient
 *
 * Determines whether the WiMAX hardware is enabled.
 *
 * Returns: %TRUE if the WiMAX hardware is enabled
 **/
gboolean
nm_client_wimax_hardware_get_enabled (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

1023
	_nm_object_ensure_inited (NM_OBJECT (client));
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	return NM_CLIENT_GET_PRIVATE (client)->wimax_hw_enabled;
}

/**
 * nm_client_get_version:
 * @client: a #NMClient
 *
 * Gets NetworkManager version.
 *
 * Returns: string with the version
 **/
const char *
nm_client_get_version (NMClient *client)
{
	NMClientPrivate *priv;

	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	priv = NM_CLIENT_GET_PRIVATE (client);

1044
	_nm_object_ensure_inited (NM_OBJECT (client));
1045 1046

	return priv->manager_running ? priv->version : NULL;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
}

/**
 * nm_client_get_state:
 * @client: a #NMClient
 *
 * Gets the current daemon state.
 *
 * Returns: the current %NMState
 **/
NMState
nm_client_get_state (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);

1062
	_nm_object_ensure_inited (NM_OBJECT (client));
1063 1064

	return NM_CLIENT_GET_PRIVATE (client)->state;
1065 1066
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/**
 * nm_client_get_startup:
 * @client: a #NMClient
 *
 * Tests whether the daemon is still in the process of activating
 * connections at startup.
 *
 * Returns: whether the daemon is still starting up
 *
 * Since: 0.9.10
 **/
gboolean
nm_client_get_startup (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);

	_nm_object_ensure_inited (NM_OBJECT (client));

	return NM_CLIENT_GET_PRIVATE (client)->startup;
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/**
 * 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)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

1101 1102
	_nm_object_ensure_inited (NM_OBJECT (client));
	return NM_CLIENT_GET_PRIVATE (client)->networking_enabled;
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
}

/**
 * nm_client_networking_set_enabled:
 * @client: a #NMClient
 * @enabled: %TRUE to set networking enabled, %FALSE to set networking disabled
 *
 * 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.
 **/
void
nm_client_networking_set_enabled (NMClient *client, gboolean enable)
{
	GError *err = NULL;

	g_return_if_fail (NM_IS_CLIENT (client));

1121 1122 1123
	if (!NM_CLIENT_GET_PRIVATE (client)->manager_running)
		return;

1124 1125 1126 1127
	if (!dbus_g_proxy_call (NM_CLIENT_GET_PRIVATE (client)->client_proxy, "Enable", &err,
	                        G_TYPE_BOOLEAN, enable,
	                        G_TYPE_INVALID,
	                        G_TYPE_INVALID)) {
1128 1129 1130
		g_warning ("Error enabling/disabling networking: %s",
		           err ? err->message : "(unknown)");
		g_clear_error (&err);
1131 1132 1133 1134 1135 1136
	}
}

/**
 * nm_client_sleep:
 * @client: a #NMClient
1137
 * @sleep_: %TRUE to put the daemon to sleep
1138 1139 1140 1141
 *
 * Deprecated; use nm_client_networking_set_enabled() instead.
 **/
void
1142
nm_client_sleep (NMClient *client, gboolean sleep_)
1143
{
1144
	nm_client_networking_set_enabled (client, !sleep_);
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
}

/**
 * nm_client_get_manager_running:
 * @client: a #NMClient
 *
 * Determines whether the daemon is running.
 *
 * Returns: %TRUE if the daemon is running
 **/
gboolean
nm_client_get_manager_running (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

	return NM_CLIENT_GET_PRIVATE (client)->manager_running;
}

/**
 * nm_client_get_permission_result:
 * @client: a #NMClient
 * @permission: the permission for which to return the result, one of #NMClientPermission
 *
 * Requests the result of a specific permission, which indicates whether the
 * client can or cannot perform the action the permission represents
 *
 * Returns: the permission's result, one of #NMClientPermissionResult
 **/
NMClientPermissionResult
nm_client_get_permission_result (NMClient *client, NMClientPermission permission)
{
	gpointer result;

	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);
}

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
/**
 * nm_client_get_logging:
 * @client: a #NMClient
 * @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
 *
 * Gets NetworkManager current logging level and domains.
 *
 * Returns: %TRUE on success, %FALSE otherwise
1196 1197
 *
 * Since: 0.9.8
1198 1199 1200 1201
 **/
gboolean
nm_client_get_logging (NMClient *client, char **level, char **domains, GError **error)
{
1202
	NMClientPrivate *priv;
1203 1204 1205 1206 1207 1208

	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);

1209 1210 1211 1212 1213 1214
	priv = NM_CLIENT_GET_PRIVATE (client);
	if (!priv->manager_running) {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
1215 1216 1217
		return FALSE;
	}

1218 1219 1220 1221 1222 1223 1224 1225
	if (!level && !domains)
		return TRUE;

	return dbus_g_proxy_call (priv->client_proxy, "GetLogging", error,
	                          G_TYPE_INVALID,
	                          G_TYPE_STRING, level,
	                          G_TYPE_STRING, domains,
	                          G_TYPE_INVALID);
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
}

/**
 * nm_client_set_logging:
 * @client: a #NMClient
 * @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
 *
 * Sets NetworkManager logging level and/or domains.
 *
 * Returns: %TRUE on success, %FALSE otherwise
1239 1240
 *
 * Since: 0.9.8
1241 1242 1243 1244
 **/
gboolean
nm_client_set_logging (NMClient *client, const char *level, const char *domains, GError **error)
{
1245
	NMClientPrivate *priv;
1246 1247 1248 1249

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

1250 1251 1252 1253 1254 1255
	priv = NM_CLIENT_GET_PRIVATE (client);
	if (!priv->manager_running) {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
1256 1257 1258
		return FALSE;
	}

1259 1260 1261 1262 1263 1264 1265 1266
	if (!level && !domains)
		return TRUE;

	return dbus_g_proxy_call (priv->client_proxy, "SetLogging", error,
	                          G_TYPE_STRING, level ? level : "",
	                          G_TYPE_STRING, domains ? domains : "",
	                          G_TYPE_INVALID,
	                          G_TYPE_INVALID);
1267 1268
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
/**
 * 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
 *
1288
 * Since: 0.9.8.6
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
 */
NMActiveConnection *
nm_client_get_primary_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	_nm_object_ensure_inited (NM_OBJECT (client));
	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.
 *
1310
 * Since: 0.9.8.6
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
 */
NMActiveConnection *
nm_client_get_activating_connection (NMClient *client)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

	_nm_object_ensure_inited (NM_OBJECT (client));
	return NM_CLIENT_GET_PRIVATE (client)->activating_connection;
}

1321 1322 1323
/****************************************************************/

static void
1324
free_devices (NMClient *client, gboolean in_dispose)
1325
{
1326
	NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client);
1327 1328 1329 1330
	gs_unref_ptrarray GPtrArray *real_devices = NULL;
	gs_unref_ptrarray GPtrArray *all_devices = NULL;
	GPtrArray *devices = NULL;
	guint i, j;
1331

1332 1333
	real_devices = priv->devices;
	all_devices = priv->all_devices;
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	if (in_dispose) {
		priv->devices = NULL;
		priv->all_devices = NULL;
	} else {
		priv->devices = g_ptr_array_new ();
		priv->all_devices = g_ptr_array_new ();
	}

	if (all_devices && all_devices->len > 0)
		devices = all_devices;
	else if (devices && devices->len > 0)
		devices = real_devices;

	if (real_devices && devices != real_devices) {
		for (i = 0; i < real_devices->len; i++) {
			NMDevice *d = real_devices->pdata