From 9ee41307fc356e915be7a6dc4aea19034f0735a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Thu, 30 Sep 2010 21:09:01 +0200 Subject: [PATCH] examples: add example for adding a connection in C (using glib) --- configure.ac | 1 + examples/C/Makefile.am | 2 + examples/C/add-connection-glib.c | 112 +++++++++++++++++++++++++++++++ examples/Makefile.am | 4 +- 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 examples/C/Makefile.am create mode 100644 examples/C/add-connection-glib.c diff --git a/configure.ac b/configure.ac index db28815783..568359706f 100644 --- a/configure.ac +++ b/configure.ac @@ -560,6 +560,7 @@ docs/libnm-util/Makefile NetworkManager.pc examples/Makefile examples/python/Makefile +examples/C/Makefile ]) AC_OUTPUT diff --git a/examples/C/Makefile.am b/examples/C/Makefile.am new file mode 100644 index 0000000000..68c0697b0b --- /dev/null +++ b/examples/C/Makefile.am @@ -0,0 +1,2 @@ +EXTRA_DIST = \ + add-connection-glib.c diff --git a/examples/C/add-connection-glib.c b/examples/C/add-connection-glib.c new file mode 100644 index 0000000000..0e8dc18036 --- /dev/null +++ b/examples/C/add-connection-glib.c @@ -0,0 +1,112 @@ +/* + * 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 2010 Red Hat, Inc. + */ + +/* + * The example shows how to call AddConnection() D-Bus method to add + * a connection to system settings service. It uses dbus-glib and libnm-util + * libraries. + * + * Compile with: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` add-connection-glib.c -o add-connection-glib + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) +#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT)) + +void add_connection (DBusGProxy *proxy, const char *con_name) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingWired *s_wired; + NMSettingIP4Config *s_ip4; + char *uuid; + GHashTable *hash; + GError *error = NULL; + + /* Create a new connection object */ + connection = (NMConnection *) nm_connection_new (); + + /* Build up the 'connection' Setting */ + s_con = (NMSettingConnection *) nm_setting_connection_new (); + uuid = nm_utils_uuid_generate (); + g_object_set (G_OBJECT (s_con), + NM_SETTING_CONNECTION_UUID, uuid, + NM_SETTING_CONNECTION_ID, con_name, + NM_SETTING_CONNECTION_TYPE, "802-3-ethernet", + NULL); + g_free (uuid); + nm_connection_add_setting (connection, NM_SETTING (s_con)); + + /* Build up the 'wired' Setting */ + s_wired = (NMSettingWired *) nm_setting_wired_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wired)); + + /* Build up the 'ipv4' Setting */ + s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + g_object_set (G_OBJECT (s_ip4), + NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NULL); + nm_connection_add_setting (connection, NM_SETTING (s_ip4)); + + hash = nm_connection_to_hash (connection); + + /* Call AddConnection with the hash as argument */ + dbus_g_proxy_call (proxy, "AddConnection", &error, + DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, + G_TYPE_INVALID); + + g_hash_table_destroy (hash); + g_object_unref (connection); +} + + +int main (int argc, char *argv[]) +{ + DBusGConnection *bus; + DBusGProxy *proxy; + + /* Initialize GType system */ + g_type_init (); + + /* 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_SYSTEM_SETTINGS, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_IFACE_SETTINGS); + + /* Add a connection */ + add_connection (proxy, "__Test connection__"); + + g_object_unref (proxy); + dbus_g_connection_unref (bus); + + return 0; +} diff --git a/examples/Makefile.am b/examples/Makefile.am index c2ddf781cc..643959b143 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1 +1,3 @@ -SUBDIRS=python +SUBDIRS= \ + python \ + C