nm-dhcp4-config.c 6.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * libnm_glib -- Access network status & information from glib applications
 *
 * 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.
 *
20
 * Copyright (C) 2008 - 2011 Red Hat, Inc.
21 22 23
 * Copyright (C) 2008 Novell, Inc.
 */

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
#include <string.h>

#include "nm-dhcp4-config.h"
#include "NetworkManager.h"
#include "nm-types-private.h"
#include "nm-object-private.h"
#include "nm-utils.h"

G_DEFINE_TYPE (NMDHCP4Config, nm_dhcp4_config, NM_TYPE_OBJECT)

#define NM_DHCP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP4_CONFIG, NMDHCP4ConfigPrivate))

typedef struct {
	DBusGProxy *proxy;

	GHashTable *options;
} NMDHCP4ConfigPrivate;

enum {
	PROP_0,
	PROP_OPTIONS,

	LAST_PROP
};

static void
nm_dhcp4_config_init (NMDHCP4Config *config)
{
}

static void
copy_options (gpointer key, gpointer data, gpointer user_data)
{
	GHashTable *options = (GHashTable *) user_data;
	GValue *value = (GValue *) data;

	g_hash_table_insert (options, g_strdup (key), g_value_dup_string (value));
}

static gboolean
demarshal_dhcp4_options (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
{
	NMDHCP4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object);
	GHashTable *new_options;

	g_hash_table_remove_all (priv->options);

	new_options = g_value_get_boxed (value);
	if (new_options)
		g_hash_table_foreach (new_options, copy_options, priv->options);

75
	_nm_object_queue_notify (object, NM_DHCP4_CONFIG_OPTIONS);
76 77 78 79
	return TRUE;
}

static void
80
register_properties (NMDHCP4Config *config)
81 82
{
	NMDHCP4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (config);
83 84
	const NMPropertiesInfo property_info[] = {
		{ NM_DHCP4_CONFIG_OPTIONS,   &priv->options, demarshal_dhcp4_options },
85 86 87
		{ NULL },
	};

88 89 90
	_nm_object_register_properties (NM_OBJECT (config),
	                                priv->proxy,
	                                property_info);
91 92
}

93 94
static void
constructed (GObject *object)
95 96 97 98
{
	DBusGConnection *connection;
	NMDHCP4ConfigPrivate *priv;

99
	G_OBJECT_CLASS (nm_dhcp4_config_parent_class)->constructed (object);
100 101 102 103

	priv = NM_DHCP4_CONFIG_GET_PRIVATE (object);
	priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

104
	connection = nm_object_get_connection (NM_OBJECT (object));
105 106 107

	priv->proxy = dbus_g_proxy_new_for_name (connection,
										   NM_DBUS_SERVICE,
108
										   nm_object_get_path (NM_OBJECT (object)),
109 110
										   NM_DBUS_INTERFACE_DHCP4_CONFIG);

111
	register_properties (NM_DHCP4_CONFIG (object));
112 113 114 115 116 117 118 119 120 121
}

static void
finalize (GObject *object)
{
	NMDHCP4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object);

	if (priv->options)
		g_hash_table_destroy (priv->options);

122 123
	g_object_unref (priv->proxy);

124 125 126 127 128 129 130 131 132 133 134
	G_OBJECT_CLASS (nm_dhcp4_config_parent_class)->finalize (object);
}

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
	NMDHCP4Config *self = NM_DHCP4_CONFIG (object);

135 136
	_nm_object_ensure_inited (NM_OBJECT (object));

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	switch (prop_id) {
	case PROP_OPTIONS:
		g_value_set_boxed (value, nm_dhcp4_config_get_options (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_dhcp4_config_class_init (NMDHCP4ConfigClass *config_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (config_class);

	g_type_class_add_private (config_class, sizeof (NMDHCP4ConfigPrivate));

	/* virtual methods */
155
	object_class->constructed = constructed;
156 157 158 159
	object_class->get_property = get_property;
	object_class->finalize = finalize;

	/* properties */
160 161 162 163 164

	/**
	 * NMDHCP4Config:options:
	 *
	 * The #GHashTable containing options of the configuration.
165
	 *
166
	 * Type: GLib.HashTable(utf8,GObject.Value)
167
	 **/
168 169 170 171 172 173 174 175 176
	g_object_class_install_property
		(object_class, PROP_OPTIONS,
		 g_param_spec_boxed (NM_DHCP4_CONFIG_OPTIONS,
						       "Options",
						       "Options",
						       G_TYPE_HASH_TABLE,
						       G_PARAM_READABLE));
}

177 178 179 180 181 182 183
/**
 * nm_dhcp4_config_new:
 * @connection: the #DBusGConnection
 * @object_path: the DBus object path of the device
 *
 * Creates a new #NMDHCP4Config.
 *
Dan Williams's avatar
Dan Williams committed
184
 * Returns: (transfer full): a new configuration
185
 **/
186 187 188 189 190 191 192 193 194
GObject *
nm_dhcp4_config_new (DBusGConnection *connection, const char *object_path)
{
	return (GObject *) g_object_new (NM_TYPE_DHCP4_CONFIG,
									 NM_OBJECT_DBUS_CONNECTION, connection,
									 NM_OBJECT_DBUS_PATH, object_path,
									 NULL);
}

195 196
/**
 * nm_dhcp4_config_get_options:
Dan Williams's avatar
Dan Williams committed
197
 * @config: a #NMDHCP4Config
198 199 200
 *
 * Gets all the options contained in the configuration.
 *
201
 * Returns: (transfer none) (element-type utf8 GObject.Value): the #GHashTable containing strings for keys and values.
202 203
 * This is the internal copy used by the configuration, and must not be modified.
 **/
204
GHashTable *
Dan Williams's avatar
Dan Williams committed
205
nm_dhcp4_config_get_options (NMDHCP4Config *config)
206
{
207
	g_return_val_if_fail (NM_IS_DHCP4_CONFIG (config), NULL);
208

209 210
	_nm_object_ensure_inited (NM_OBJECT (config));
	return NM_DHCP4_CONFIG_GET_PRIVATE (config)->options;
211 212
}

213 214
/**
 * nm_dhcp4_config_get_one_option:
Dan Williams's avatar
Dan Williams committed
215
 * @config: a #NMDHCP4Config
216 217 218 219 220 221 222
 * @option: the option to retrieve
 *
 * Gets one option by option name.
 *
 * Returns: the configuration option's value. This is the internal string used by the
 * configuration, and must not be modified.
 **/
223
const char *
Dan Williams's avatar
Dan Williams committed
224
nm_dhcp4_config_get_one_option (NMDHCP4Config *config, const char *option)
225
{
Dan Williams's avatar
Dan Williams committed
226
	g_return_val_if_fail (NM_IS_DHCP4_CONFIG (config), NULL);
227

Dan Williams's avatar
Dan Williams committed
228
	return g_hash_table_lookup (nm_dhcp4_config_get_options (config), option);
229 230
}