summaryrefslogtreecommitdiff
path: root/examples/C/glib/list-connections-dbus-glib.c
blob: e17b8dc7d57bbd90fe0f11c3faf6e8f15caa182a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2011 Red Hat, Inc.
 */

/*
 * The example shows how to list connections from System Settings service using direct
 * D-Bus call of ListConnections method.
 * The example uses dbus-glib, libnm-util libraries.
 *
 * Compile with:
 *   gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` list-connections-dbus.c -o list-connections-dbus
 */

#include <glib.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>

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

#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH    (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))

static void
list_connections (DBusGProxy *proxy)
{
	int i;
	GError *error = NULL;
	GPtrArray *con_array;
	gboolean success;

	/* Call ListConnections D-Bus method */
	success = dbus_g_proxy_call (proxy, "ListConnections", &error,
	                             /* No input arguments */
	                             G_TYPE_INVALID,
	                             DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, &con_array, /* Return values */
	                             G_TYPE_INVALID);
	if (!success) {
		printf ("ListConnections failed: %s", error->message);
		g_error_free (error);
		return;
	}

	for (i = 0; con_array && i < con_array->len; i++) {
		char *connection_path = g_ptr_array_index (con_array, i);
		printf ("%s\n", connection_path);
		g_free (connection_path);
	}
	g_ptr_array_free (con_array, TRUE);
}

int main (int argc, char *argv[])
{
	DBusGConnection *bus;
	DBusGProxy *proxy;

#if !GLIB_CHECK_VERSION (2, 35, 0)
	/* Initialize GType system */
	g_type_init ();
#endif

	/* Get system bus */
	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);

	/* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */
	proxy = dbus_g_proxy_new_for_name (bus,
	                                   NM_DBUS_SERVICE,
	                                   NM_DBUS_PATH_SETTINGS,
	                                   NM_DBUS_IFACE_SETTINGS);

	/* List connections of system settings service */
	list_connections (proxy);

	g_object_unref (proxy);
	dbus_g_connection_unref (bus);

	return 0;
}