nm-client.c 76.3 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
#include "nm-default.h"
23

24 25
#include <string.h>

26
#include "nm-utils.h"
27
#include "nm-client.h"
28
#include "nm-manager.h"
29
#include "nm-remote-settings.h"
30 31 32
#include "nm-device-ethernet.h"
#include "nm-device-wifi.h"
#include "nm-device-private.h"
33
#include "nm-core-internal.h"
34 35
#include "nm-active-connection.h"
#include "nm-vpn-connection.h"
36
#include "nm-remote-connection.h"
37
#include "nm-object-cache.h"
Dan Winship's avatar
Dan Winship committed
38 39
#include "nm-dbus-helpers.h"

40 41 42 43 44
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);

45
G_DEFINE_TYPE_WITH_CODE (NMClient, nm_client, G_TYPE_OBJECT,
46 47 48 49 50 51 52
                         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 {
53
	NMManager *manager;
54
	NMRemoteSettings *settings;
55 56 57 58 59 60 61
} NMClientPrivate;

enum {
	PROP_0,
	PROP_VERSION,
	PROP_STATE,
	PROP_STARTUP,
62
	PROP_NM_RUNNING,
63 64 65 66 67 68 69 70 71 72 73 74
	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,
75
	PROP_ALL_DEVICES,
76 77 78
	PROP_CONNECTIONS,
	PROP_HOSTNAME,
	PROP_CAN_MODIFY,
79
	PROP_METERED,
80 81 82 83 84 85 86

	LAST_PROP
};

enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
87 88
	ANY_DEVICE_ADDED,
	ANY_DEVICE_REMOVED,
89
	PERMISSION_CHANGED,
90 91
	CONNECTION_ADDED,
	CONNECTION_REMOVED,
92 93
	ACTIVE_CONNECTION_ADDED,
	ACTIVE_CONNECTION_REMOVED,
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

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

/**
 * 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)
{
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
static gboolean
_nm_client_check_nm_running (NMClient *client, GError **error)
{
	if (nm_client_get_nm_running (client))
		return TRUE;
	else {
		g_set_error_literal (error,
		                     NM_CLIENT_ERROR,
		                     NM_CLIENT_ERROR_MANAGER_NOT_RUNNING,
		                     "NetworkManager is not running");
		return FALSE;
	}
}

140
/**
141
 * nm_client_get_version:
142 143
 * @client: a #NMClient
 *
144
 * Gets NetworkManager version.
145
 *
146
 * Returns: string with the version (or %NULL if NetworkManager is not running)
147
 **/
148 149
const char *
nm_client_get_version (NMClient *client)
150 151 152
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

153
	return nm_manager_get_version (NM_CLIENT_GET_PRIVATE (client)->manager);
154 155 156
}

/**
157
 * nm_client_get_state:
158 159
 * @client: a #NMClient
 *
160
 * Gets the current daemon state.
161
 *
162
 * Returns: the current %NMState
163
 **/
164 165
NMState
nm_client_get_state (NMClient *client)
166
{
167
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
168

169
	return nm_manager_get_state (NM_CLIENT_GET_PRIVATE (client)->manager);
170 171 172
}

/**
173
 * nm_client_get_startup:
174 175
 * @client: a #NMClient
 *
176 177
 * Tests whether the daemon is still in the process of activating
 * connections at startup.
178
 *
179
 * Returns: whether the daemon is still starting up
180
 **/
181 182
gboolean
nm_client_get_startup (NMClient *client)
183
{
184
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_STATE_UNKNOWN);
185

186
	return nm_manager_get_startup (NM_CLIENT_GET_PRIVATE (client)->manager);
187 188
}

189 190 191 192 193 194 195 196 197 198
/**
 * 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)
199
{
200
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
201

202
	return nm_manager_get_nm_running (NM_CLIENT_GET_PRIVATE (client)->manager);
203 204
}

205 206 207 208 209 210 211 212 213 214
/**
 * 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)
215
{
216
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
217

218
	return nm_manager_networking_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234
/**
 * 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)
235
{
236
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
237

238
	if (!_nm_client_check_nm_running (client, error))
239
		return FALSE;
240

241 242
	return nm_manager_networking_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                          enable, error);
243 244
}

245 246 247 248 249 250 251 252 253 254
/**
 * 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)
255
{
256
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
257

258
	return nm_manager_wireless_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
259 260 261
}

/**
262
 * nm_client_wireless_set_enabled:
263
 * @client: a #NMClient
264
 * @enabled: %TRUE to enable wireless
265
 *
266
 * Enables or disables wireless devices.
267 268
 **/
void
269
nm_client_wireless_set_enabled (NMClient *client, gboolean enabled)
270 271 272
{
	g_return_if_fail (NM_IS_CLIENT (client));

273
	if (!_nm_client_check_nm_running (client, NULL))
274
		return;
275

276
	nm_manager_wireless_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
277 278
}

279
/**
280 281
 * nm_client_wireless_hardware_get_enabled:
 * @client: a #NMClient
282
 *
283
 * Determines whether the wireless hardware is enabled.
284
 *
285
 * Returns: %TRUE if the wireless hardware is enabled
286
 **/
287 288
gboolean
nm_client_wireless_hardware_get_enabled (NMClient *client)
289
{
290
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
291

292
	return nm_manager_wireless_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
293 294
}

295 296 297 298 299 300 301 302 303 304
/**
 * 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)
305
{
306
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
307

308
	return nm_manager_wwan_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
309 310 311
}

/**
312
 * nm_client_wwan_set_enabled:
313
 * @client: a #NMClient
314
 * @enabled: %TRUE to enable WWAN
315
 *
316
 * Enables or disables WWAN devices.
317 318
 **/
void
319
nm_client_wwan_set_enabled (NMClient *client, gboolean enabled)
320 321
{
	g_return_if_fail (NM_IS_CLIENT (client));
322

323
	if (!_nm_client_check_nm_running (client, NULL))
324
		return;
325

326
	nm_manager_wwan_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
327
}
328

329
/**
330 331
 * nm_client_wwan_hardware_get_enabled:
 * @client: a #NMClient
332
 *
333
 * Determines whether the WWAN hardware is enabled.
334
 *
335
 * Returns: %TRUE if the WWAN hardware is enabled
336
 **/
337 338
gboolean
nm_client_wwan_hardware_get_enabled (NMClient *client)
339
{
340
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
341

342
	return nm_manager_wwan_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
343 344 345
}

/**
346
 * nm_client_wimax_get_enabled:
347 348
 * @client: a #NMClient
 *
349
 * Determines whether WiMAX is enabled.
350
 *
351
 * Returns: %TRUE if WiMAX is enabled
352
 **/
353
gboolean
354
nm_client_wimax_get_enabled (NMClient *client)
355
{
356
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
357

358
	return nm_manager_wimax_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
359 360 361
}

/**
362
 * nm_client_wimax_set_enabled:
363
 * @client: a #NMClient
364
 * @enabled: %TRUE to enable WiMAX
365
 *
366
 * Enables or disables WiMAX devices.
367 368
 **/
void
369
nm_client_wimax_set_enabled (NMClient *client, gboolean enabled)
370 371 372
{
	g_return_if_fail (NM_IS_CLIENT (client));

373
	if (!_nm_client_check_nm_running (client, NULL))
374 375
		return;

376
	nm_manager_wimax_set_enabled (NM_CLIENT_GET_PRIVATE (client)->manager, enabled);
377 378 379
}

/**
380
 * nm_client_wimax_hardware_get_enabled:
381 382
 * @client: a #NMClient
 *
383
 * Determines whether the WiMAX hardware is enabled.
384
 *
385
 * Returns: %TRUE if the WiMAX hardware is enabled
386 387
 **/
gboolean
388
nm_client_wimax_hardware_get_enabled (NMClient *client)
389
{
390
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
391

392
	return nm_manager_wimax_hardware_get_enabled (NM_CLIENT_GET_PRIVATE (client)->manager);
393 394
}

395
/**
396
 * nm_client_get_logging:
397
 * @client: a #NMClient
398 399 400 401
 * @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
402
 *
403
 * Gets NetworkManager current logging level and domains.
404
 *
405
 * Returns: %TRUE on success, %FALSE otherwise
406
 **/
407 408
gboolean
nm_client_get_logging (NMClient *client, char **level, char **domains, GError **error)
409
{
410 411 412 413
	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);
414

415
	if (!_nm_client_check_nm_running (client, error))
416
		return FALSE;
417

418 419
	return nm_manager_get_logging (NM_CLIENT_GET_PRIVATE (client)->manager,
	                               level, domains, error);
420 421 422
}

/**
423
 * nm_client_set_logging:
424
 * @client: a #NMClient
425 426 427 428
 * @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
429
 *
430
 * Sets NetworkManager logging level and/or domains.
431
 *
432
 * Returns: %TRUE on success, %FALSE otherwise
433 434
 **/
gboolean
435
nm_client_set_logging (NMClient *client, const char *level, const char *domains, GError **error)
436 437
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
438
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
439

440
	if (!_nm_client_check_nm_running (client, error))
441
		return FALSE;
442

443 444
	return nm_manager_set_logging (NM_CLIENT_GET_PRIVATE (client)->manager,
	                               level, domains, error);
445 446 447
}

/**
448
 * nm_client_get_permission_result:
449
 * @client: a #NMClient
450
 * @permission: the permission for which to return the result, one of #NMClientPermission
451
 *
452 453
 * Requests the result of a specific permission, which indicates whether the
 * client can or cannot perform the action the permission represents
454
 *
455
 * Returns: the permission's result, one of #NMClientPermissionResult
456
 **/
457 458
NMClientPermissionResult
nm_client_get_permission_result (NMClient *client, NMClientPermission permission)
459
{
460 461
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CLIENT_PERMISSION_RESULT_UNKNOWN);

462
	return nm_manager_get_permission_result (NM_CLIENT_GET_PRIVATE (client)->manager, permission);
463 464 465
}

/**
466 467
 * nm_client_get_connectivity:
 * @client: an #NMClient
468
 *
469 470 471 472
 * 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.
473
 *
474 475 476 477
 * Returns: the current connectivity state
 */
NMConnectivityState
nm_client_get_connectivity (NMClient *client)
478
{
479
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
480

481
	return nm_manager_get_connectivity (NM_CLIENT_GET_PRIVATE (client)->manager);
482 483 484
}

/**
485 486 487 488
 * nm_client_check_connectivity:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @error: return location for a #GError
489
 *
490 491 492 493 494 495 496 497 498 499 500 501 502
 * 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)
503
{
504
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
505

506 507 508
	if (!_nm_client_check_nm_running (client, error))
		return NM_CONNECTIVITY_UNKNOWN;

509 510
	return nm_manager_check_connectivity (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                      cancellable, error);
511 512 513 514 515 516 517 518
}

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

522 523 524
	connectivity = nm_manager_check_connectivity_finish (NM_MANAGER (object),
	                                                     result, &error);
	if (!error)
525 526 527 528 529 530
		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);
531 532 533
}

/**
534 535 536 537 538
 * nm_client_check_connectivity_async:
 * @client: an #NMClient
 * @cancellable: a #GCancellable
 * @callback: callback to call with the result
 * @user_data: data for @callback.
539
 *
540 541 542 543 544 545 546 547 548 549 550 551
 * 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)
{
	GSimpleAsyncResult *simple;
552
	GError *error = NULL;
553 554

	g_return_if_fail (NM_IS_CLIENT (client));
555 556 557 558 559

	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
		return;
	}
560 561 562

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_check_connectivity_async);
563 564
	nm_manager_check_connectivity_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                     cancellable, check_connectivity_cb, simple);
565 566 567 568 569 570 571
}

/**
 * nm_client_check_connectivity_finish:
 * @client: an #NMClient
 * @result: the #GAsyncResult
 * @error: return location for a #GError
572
 *
573 574 575 576 577 578 579 580 581
 * 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)
582
{
583
	GSimpleAsyncResult *simple;
584

585 586
	g_return_val_if_fail (NM_IS_CLIENT (client), NM_CONNECTIVITY_UNKNOWN);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NM_CONNECTIVITY_UNKNOWN);
587 588 589 590 591 592

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

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

/**
 * nm_client_save_hostname:
 * @client: the %NMClient
 * @hostname: (allow-none): the new persistent hostname to set, or %NULL to
 *   clear any existing persistent hostname
 * @cancellable: a #GCancellable, or %NULL
 * @error: return location for #GError
 *
 * Requests that the machine's persistent hostname be set to the specified value
 * or cleared.
 *
 * Returns: %TRUE if the request was successful, %FALSE if it failed
 **/
gboolean
nm_client_save_hostname (NMClient *client,
                         const char *hostname,
                         GCancellable *cancellable,
                         GError **error)
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);

	return nm_remote_settings_save_hostname (NM_CLIENT_GET_PRIVATE (client)->settings,
	                                         hostname, cancellable, error);
}

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

	if (nm_remote_settings_save_hostname_finish (NM_REMOTE_SETTINGS (object), result, &error))
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

/**
 * nm_client_save_hostname_async:
 * @client: the %NMClient
 * @hostname: (allow-none): the new persistent hostname to set, or %NULL to
 *   clear any existing persistent hostname
 * @cancellable: a #GCancellable, or %NULL
 * @callback: (scope async): callback to be called when the operation completes
 * @user_data: (closure): caller-specific data passed to @callback
 *
 * Requests that the machine's persistent hostname be set to the specified value
 * or cleared.
 **/
void
nm_client_save_hostname_async (NMClient *client,
                               const char *hostname,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	GSimpleAsyncResult *simple;
	GError *error = NULL;

	g_return_if_fail (NM_IS_CLIENT (client));

	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
		return;
	}

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_save_hostname_async);
	nm_remote_settings_save_hostname_async (NM_CLIENT_GET_PRIVATE (client)->settings,
	                                        hostname,
	                                        cancellable, save_hostname_cb, simple);
}

/**
 * nm_client_save_hostname_finish:
 * @client: the %NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: return location for #GError
 *
 * Gets the result of an nm_client_save_hostname_async() call.
 *
 * Returns: %TRUE if the request was successful, %FALSE if it failed
 **/
gboolean
nm_client_save_hostname_finish (NMClient *client,
                                GAsyncResult *result,
                                GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;
	else
		return g_simple_async_result_get_op_res_gboolean (simple);
}

701 702 703 704
/****************************************************************/
/* Devices                                                      */
/****************************************************************/

705
/**
706
 * nm_client_get_devices:
707 708
 * @client: a #NMClient
 *
709
 * Gets all the known network devices.  Use nm_device_get_type() or the
Dan Winship's avatar
Dan Winship committed
710 711 712
 * <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().
713
 *
714 715 716
 * 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.
717
 **/
718 719
const GPtrArray *
nm_client_get_devices (NMClient *client)
720
{
721
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
722

723
	return nm_manager_get_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/**
 * 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_manager_get_all_devices (NM_CLIENT_GET_PRIVATE (client)->manager);
}

754
/**
755
 * nm_client_get_device_by_path:
756
 * @client: a #NMClient
757
 * @object_path: the object path to search for
758
 *
759 760 761
 * Gets a #NMDevice from a #NMClient.
 *
 * Returns: (transfer none): the #NMDevice for the given @object_path or %NULL if none is found.
762
 **/
763 764
NMDevice *
nm_client_get_device_by_path (NMClient *client, const char *object_path)
765
{
766 767
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (object_path, NULL);
768

769
	return nm_manager_get_device_by_path (NM_CLIENT_GET_PRIVATE (client)->manager, object_path);
770 771 772
}

/**
773
 * nm_client_get_device_by_iface:
774
 * @client: a #NMClient
775
 * @iface: the interface name to search for
776
 *
777
 * Gets a #NMDevice from a #NMClient.
778
 *
779
 * Returns: (transfer none): the #NMDevice for the given @iface or %NULL if none is found.
780
 **/
781 782
NMDevice *
nm_client_get_device_by_iface (NMClient *client, const char *iface)
783
{
784 785 786
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (iface, NULL);

787
	return nm_manager_get_device_by_iface (NM_CLIENT_GET_PRIVATE (client)->manager, iface);
788 789
}

790 791 792 793
/****************************************************************/
/* Active Connections                                           */
/****************************************************************/

794
/**
795
 * nm_client_get_active_connections:
796 797
 * @client: a #NMClient
 *
798
 * Gets the active connections.
799
 *
800 801 802
 * 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.
803
 **/
804 805
const GPtrArray *
nm_client_get_active_connections (NMClient *client)
806 807 808
{
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);

809
	return nm_manager_get_active_connections (NM_CLIENT_GET_PRIVATE (client)->manager);
810 811 812
}

/**
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
 * 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);

836
	return nm_manager_get_primary_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
}

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

855
	return nm_manager_get_activating_connection (NM_CLIENT_GET_PRIVATE (client)->manager);
856 857 858 859 860 861 862
}

static void
activate_cb (GObject *object,
             GAsyncResult *result,
             gpointer user_data)
{
863 864
	GSimpleAsyncResult *simple = user_data;
	NMActiveConnection *ac;
865 866
	GError *error = NULL;

867 868 869 870 871 872 873 874
	ac = nm_manager_activate_connection_finish (NM_MANAGER (object), result, &error);
	if (ac)
		g_simple_async_result_set_op_res_gpointer (simple, ac, g_object_unref);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
875 876 877 878
}

/**
 * nm_client_activate_connection_async:
879
 * @client: a #NMClient
880 881 882 883 884 885 886 887 888 889 890 891
 * @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
892
 *
893 894 895 896 897 898 899 900
 * 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.
901
 *
902 903 904 905 906 907 908
 * 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.
909
 **/
910 911 912 913 914 915 916 917
void
nm_client_activate_connection_async (NMClient *client,
                                     NMConnection *connection,
                                     NMDevice *device,
                                     const char *specific_object,
                                     GCancellable *cancellable,
                                     GAsyncReadyCallback callback,
                                     gpointer user_data)
918
{
919
	GSimpleAsyncResult *simple;
920
	GError *error = NULL;
921 922 923 924 925 926 927

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

928 929
	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
930 931 932
		return;
	}

933 934 935 936 937
	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_activate_connection_async);
	nm_manager_activate_connection_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                      connection, device, specific_object,
	                                      cancellable, activate_cb, simple);
938 939 940
}

/**
941 942 943 944
 * nm_client_activate_connection_finish:
 * @client: an #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
945
 *
946
 * Gets the result of a call to nm_client_activate_connection_async().
947
 *
948 949
 * Returns: (transfer full): the new #NMActiveConnection on success, %NULL on
 *   failure, in which case @error will be set.
950
 **/
951 952 953 954
NMActiveConnection *
nm_client_activate_connection_finish (NMClient *client,
                                      GAsyncResult *result,
                                      GError **error)
955
{
956
	GSimpleAsyncResult *simple;
957

958 959
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
960 961 962 963 964 965

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

968 969 970 971
static void
add_activate_cb (GObject *object,
                 GAsyncResult *result,
                 gpointer user_data)
972
{
973 974
	GSimpleAsyncResult *simple = user_data;
	NMActiveConnection *ac;
975
	GError *error = NULL;
976

977 978 979 980 981 982 983 984
	ac = nm_manager_add_and_activate_connection_finish (NM_MANAGER (object), result, &error);
	if (ac)
		g_simple_async_result_set_op_res_gpointer (simple, ac, g_object_unref);
	else
		g_simple_async_result_take_error (simple, error);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
985 986 987
}

/**
988
 * nm_client_add_and_activate_connection_async:
989
 * @client: a #NMClient
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
 * @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
1004
 *
1005 1006 1007 1008 1009
 * 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.
1010
 *
1011 1012 1013 1014
 * 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.
1015
 **/
1016 1017 1018 1019 1020 1021 1022 1023
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)
1024
{
1025
	GSimpleAsyncResult *simple;
1026
	GError *error = NULL;
1027 1028 1029 1030 1031

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

1033 1034
	if (!_nm_client_check_nm_running (client, &error)) {
		g_simple_async_report_take_gerror_in_idle (G_OBJECT (client), callback, user_data, error);
1035
		return;
1036
	}
1037

1038 1039 1040 1041 1042
	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_add_and_activate_connection_async);
	nm_manager_add_and_activate_connection_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                              partial, device, specific_object,
	                                              cancellable, add_activate_cb, simple);
1043 1044 1045
}

/**
1046 1047 1048 1049
 * nm_client_add_and_activate_connection_finish:
 * @client: an #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
1050
 *
1051
 * Gets the result of a call to nm_client_add_and_activate_connection_async().
1052
 *
1053 1054 1055 1056 1057
 * 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.
1058
 **/
1059 1060 1061 1062
NMActiveConnection *
nm_client_add_and_activate_connection_finish (NMClient *client,
                                              GAsyncResult *result,
                                              GError **error)
1063
{
1064
	GSimpleAsyncResult *simple;
1065

1066 1067
	g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
1068 1069 1070 1071 1072 1073

	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));
1074 1075 1076
}

/**
1077
 * nm_client_deactivate_connection:
1078
 * @client: a #NMClient
1079 1080 1081
 * @active: the #NMActiveConnection to deactivate
 * @cancellable: a #GCancellable, or %NULL
 * @error: location for a #GError, or %NULL
1082
 *
1083
 * Deactivates an active #NMActiveConnection.
1084
 *
1085
 * Returns: success or failure
1086 1087
 **/
gboolean
1088 1089 1090 1091
nm_client_deactivate_connection (NMClient *client,
                                 NMActiveConnection *active,
                                 GCancellable *cancellable,
                                 GError **error)
1092 1093
{
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
1094
	g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (active), FALSE);
1095

1096
	if (!_nm_client_check_nm_running (client, NULL))
1097 1098
		return TRUE;

1099 1100
	return nm_manager_deactivate_connection (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                         active, cancellable, error);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
}

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

1111
	if (nm_manager_deactivate_connection_finish (NM_MANAGER (object), result, &error))
1112 1113 1114 1115 1116
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
	else
		g_simple_async_result_take_error (simple, error);
	g_simple_async_result_complete (simple);
	g_object_unref (simple);
1117 1118 1119
}

/**
1120
 * nm_client_deactivate_connection_async:
1121
 * @client: a #NMClient
1122 1123 1124 1125
 * @active: the #NMActiveConnection to deactivate
 * @cancellable: a #GCancellable, or %NULL
 * @callback: callback to be called when the deactivation has completed
 * @user_data: caller-specific data passed to @callback
1126
 *
1127
 * Asynchronously deactivates an active #NMActiveConnection.
1128
 **/
1129 1130 1131 1132 1133 1134
void
nm_client_deactivate_connection_async (NMClient *client,
                                       NMActiveConnection *active,
                                       GCancellable *cancellable,
                                       GAsyncReadyCallback callback,
                                       gpointer user_data)
1135
{
1136
	GSimpleAsyncResult *simple;
1137

1138 1139 1140 1141 1142
	g_return_if_fail (NM_IS_CLIENT (client));
	g_return_if_fail (NM_IS_ACTIVE_CONNECTION (active));

	simple = g_simple_async_result_new (G_OBJECT (client), callback, user_data,
	                                    nm_client_deactivate_connection_async);
1143

1144
	if (!_nm_client_check_nm_running (client, NULL)) {
1145 1146 1147 1148
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
		g_simple_async_result_complete_in_idle (simple);
		g_object_unref (simple);
		return;
1149 1150
	}

1151 1152 1153
	nm_manager_deactivate_connection_async (NM_CLIENT_GET_PRIVATE (client)->manager,
	                                        active,
	                                        cancellable, deactivated_cb, simple);
1154 1155 1156
}

/**
1157 1158 1159 1160
 * nm_client_deactivate_connection_finish:
 * @client: a #NMClient
 * @result: the result passed to the #GAsyncReadyCallback
 * @error: location for a #GError, or %NULL
1161
 *
1162
 * Gets the result of a call to nm_client_deactivate_connection_async().
1163
 *
1164 1165 1166 1167 1168 1169
 * Returns: success or failure
 **/
gboolean
nm_client_deactivate_connection_finish (NMClient *client,
                                        GAsyncResult *result,
                                        GError **error)
1170
{
1171
	GSimpleAsyncResult *simple;
1172

1173 1174
	g_return_val_if_fail (NM_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
1175

1176 1177 1178 1179 1180
	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;
	else
		return g_simple_async_result_get_op_res_gboolean (simple);