nm-dhcp4-config.c 4.99 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 -*- */
/* NetworkManager -- Network link manager
 *
 * 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.
 *
18
 * Copyright (C) 2008 Red Hat, Inc.
19 20
 */

21
#include "nm-default.h"
22

23 24
#include "nm-dhcp4-config.h"

25
#include "nm-dbus-interface.h"
26
#include "nm-utils.h"
27
#include "nm-dbus-object.h"
28
#include "nm-core-utils.h"
29

30
/*****************************************************************************/
31

32 33 34
NM_GOBJECT_PROPERTIES_DEFINE (NMDhcp4Config,
	PROP_OPTIONS,
);
35 36

typedef struct {
37
	GVariant *options;
38
} NMDhcp4ConfigPrivate;
39

40
struct _NMDhcp4Config {
41
	NMDBusObject parent;
42 43
	NMDhcp4ConfigPrivate _priv;
};
44

45
struct _NMDhcp4ConfigClass {
46
	NMDBusObjectClass parent;
47 48
};

49
G_DEFINE_TYPE (NMDhcp4Config, nm_dhcp4_config, NM_TYPE_DBUS_OBJECT)
50

51 52 53
#define NM_DHCP4_CONFIG_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDhcp4Config, NM_IS_DHCP4_CONFIG)

/*****************************************************************************/
54 55

void
56 57
nm_dhcp4_config_set_options (NMDhcp4Config *self,
                             GHashTable *options)
58
{
59
	NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
60
	GVariant *val;
61 62

	g_return_if_fail (NM_IS_DHCP4_CONFIG (self));
63
	g_return_if_fail (options);
64

65
	val = nm_utils_strdict_to_variant (options);
66
	g_variant_unref (priv->options);
67
	priv->options = g_variant_ref_sink (val);
68
	_notify (self, PROP_OPTIONS);
69 70 71
}

const char *
72
nm_dhcp4_config_get_option (NMDhcp4Config *self, const char *key)
73
{
74 75
	NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
	const char *value;
76 77 78 79

	g_return_val_if_fail (NM_IS_DHCP4_CONFIG (self), NULL);
	g_return_val_if_fail (key != NULL, NULL);

80 81 82 83
	if (g_variant_lookup (priv->options, key, "&s", &value))
		return value;
	else
		return NULL;
84 85
}

86 87
GVariant *
nm_dhcp4_config_get_options (NMDhcp4Config *self)
88 89 90
{
	g_return_val_if_fail (NM_IS_DHCP4_CONFIG (self), NULL);

91
	return g_variant_ref (NM_DHCP4_CONFIG_GET_PRIVATE (self)->options);
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/*****************************************************************************/

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE ((NMDhcp4Config *) object);

	switch (prop_id) {
	case PROP_OPTIONS:
		g_value_set_variant (value, priv->options);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

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

114
static void
115
nm_dhcp4_config_init (NMDhcp4Config *self)
116
{
117
	NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
118

119 120
	priv->options = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
	g_variant_ref_sink (priv->options);
121 122
}

123 124 125 126 127 128
NMDhcp4Config *
nm_dhcp4_config_new (void)
{
	return NM_DHCP4_CONFIG (g_object_new (NM_TYPE_DHCP4_CONFIG, NULL));
}

129 130 131
static void
finalize (GObject *object)
{
132
	NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE ((NMDhcp4Config *) object);
133

134
	g_variant_unref (priv->options);
135 136

	G_OBJECT_CLASS (nm_dhcp4_config_parent_class)->finalize (object);
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151
static const NMDBusInterfaceInfoExtended interface_info_dhcp4_config = {
	.parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT (
		NM_DBUS_INTERFACE_DHCP4_CONFIG,
		.signals = NM_DEFINE_GDBUS_SIGNAL_INFOS (
			&nm_signal_info_property_changed_legacy,
		),
		.properties = NM_DEFINE_GDBUS_PROPERTY_INFOS (
			NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("Options", "a{sv}",  NM_DHCP4_CONFIG_OPTIONS),
		),
	),
	.legacy_property_changed = TRUE,
};

152
static void
153
nm_dhcp4_config_class_init (NMDhcp4ConfigClass *config_class)
154 155
{
	GObjectClass *object_class = G_OBJECT_CLASS (config_class);
156
	NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS (config_class);
157

158 159
	object_class->get_property = get_property;
	object_class->finalize = finalize;
160

161
	dbus_object_class->export_path = NM_DBUS_EXPORT_PATH_NUMBERED (NM_DBUS_PATH"/DHCP4Config");
162 163
	dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS (&interface_info_dhcp4_config);
	dbus_object_class->export_on_construction = TRUE;
164

165 166 167 168 169 170
	obj_properties[PROP_OPTIONS] =
	     g_param_spec_variant (NM_DHCP4_CONFIG_OPTIONS, "", "",
	                           G_VARIANT_TYPE ("a{sv}"),
	                           NULL,
	                           G_PARAM_READABLE |
	                           G_PARAM_STATIC_STRINGS);
171

172
	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
173
}