xref: /titanic_51/usr/src/cmd/hal/hald/property.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
1*18c2aff7Sartem /***************************************************************************
2*18c2aff7Sartem  * CVSID: $Id$
3*18c2aff7Sartem  *
4*18c2aff7Sartem  * property.c : HalProperty methods
5*18c2aff7Sartem  *
6*18c2aff7Sartem  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
7*18c2aff7Sartem  * Copyright (C) 2004 Novell, Inc.
8*18c2aff7Sartem  *
9*18c2aff7Sartem  * Licensed under the Academic Free License version 2.1
10*18c2aff7Sartem  *
11*18c2aff7Sartem  * This program is free software; you can redistribute it and/or modify
12*18c2aff7Sartem  * it under the terms of the GNU General Public License as published by
13*18c2aff7Sartem  * the Free Software Foundation; either version 2 of the License, or
14*18c2aff7Sartem  * (at your option) any later version.
15*18c2aff7Sartem  *
16*18c2aff7Sartem  * This program is distributed in the hope that it will be useful,
17*18c2aff7Sartem  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*18c2aff7Sartem  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*18c2aff7Sartem  * GNU General Public License for more details.
20*18c2aff7Sartem  *
21*18c2aff7Sartem  * You should have received a copy of the GNU General Public License
22*18c2aff7Sartem  * along with this program; if not, write to the Free Software
23*18c2aff7Sartem  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24*18c2aff7Sartem  *
25*18c2aff7Sartem  **************************************************************************/
26*18c2aff7Sartem 
27*18c2aff7Sartem #ifdef HAVE_CONFIG_H
28*18c2aff7Sartem #  include <config.h>
29*18c2aff7Sartem #endif
30*18c2aff7Sartem 
31*18c2aff7Sartem #include <string.h>
32*18c2aff7Sartem #include <glib.h>
33*18c2aff7Sartem 
34*18c2aff7Sartem #include "logger.h"
35*18c2aff7Sartem #include "property.h"
36*18c2aff7Sartem 
37*18c2aff7Sartem struct _HalProperty {
38*18c2aff7Sartem 	char *key;
39*18c2aff7Sartem 
40*18c2aff7Sartem 	int type;
41*18c2aff7Sartem 	union {
42*18c2aff7Sartem 		char *str_value;
43*18c2aff7Sartem 		dbus_int32_t int_value;
44*18c2aff7Sartem  		dbus_uint64_t uint64_value;
45*18c2aff7Sartem 		dbus_bool_t bool_value;
46*18c2aff7Sartem 		double double_value;
47*18c2aff7Sartem 		GSList *strlist_value;
48*18c2aff7Sartem 	} v;
49*18c2aff7Sartem 	gboolean readonly;
50*18c2aff7Sartem 	gboolean persistence;
51*18c2aff7Sartem 	gboolean callout;
52*18c2aff7Sartem };
53*18c2aff7Sartem 
54*18c2aff7Sartem void
55*18c2aff7Sartem hal_property_free (HalProperty *prop)
56*18c2aff7Sartem {
57*18c2aff7Sartem 
58*18c2aff7Sartem 	g_free (prop->key);
59*18c2aff7Sartem 
60*18c2aff7Sartem 	if (prop->type == HAL_PROPERTY_TYPE_STRING) {
61*18c2aff7Sartem 		g_free (prop->v.str_value);
62*18c2aff7Sartem 	} else if (prop->type == HAL_PROPERTY_TYPE_STRLIST) {
63*18c2aff7Sartem 		GSList *i;
64*18c2aff7Sartem 		for (i = prop->v.strlist_value; i != NULL; i = g_slist_next (i)) {
65*18c2aff7Sartem 			g_free (i->data);
66*18c2aff7Sartem 		}
67*18c2aff7Sartem 		g_slist_free (prop->v.strlist_value);
68*18c2aff7Sartem 	}
69*18c2aff7Sartem 
70*18c2aff7Sartem 	g_free (prop);
71*18c2aff7Sartem }
72*18c2aff7Sartem 
73*18c2aff7Sartem HalProperty *
74*18c2aff7Sartem hal_property_new_string (const char *key, const char *value)
75*18c2aff7Sartem {
76*18c2aff7Sartem 	HalProperty *prop;
77*18c2aff7Sartem 	char *endchar;
78*18c2aff7Sartem 	gboolean validated = TRUE;
79*18c2aff7Sartem 
80*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
81*18c2aff7Sartem 
82*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_STRING;
83*18c2aff7Sartem 	prop->key = g_strdup (key);
84*18c2aff7Sartem 	prop->v.str_value = g_strdup (value != NULL ? value : "");
85*18c2aff7Sartem 
86*18c2aff7Sartem 	while (!g_utf8_validate (prop->v.str_value, -1,
87*18c2aff7Sartem 				 (const char **) &endchar)) {
88*18c2aff7Sartem 		validated = FALSE;
89*18c2aff7Sartem 		*endchar = '?';
90*18c2aff7Sartem 	}
91*18c2aff7Sartem 
92*18c2aff7Sartem 	if (!validated) {
93*18c2aff7Sartem 		HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
94*18c2aff7Sartem 			      key, prop->v.str_value));
95*18c2aff7Sartem 	}
96*18c2aff7Sartem 
97*18c2aff7Sartem 	return prop;
98*18c2aff7Sartem }
99*18c2aff7Sartem 
100*18c2aff7Sartem HalProperty *
101*18c2aff7Sartem hal_property_new_int (const char *key, dbus_int32_t value)
102*18c2aff7Sartem {
103*18c2aff7Sartem 	HalProperty *prop;
104*18c2aff7Sartem 
105*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
106*18c2aff7Sartem 
107*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_INT32;
108*18c2aff7Sartem 	prop->key = g_strdup (key);
109*18c2aff7Sartem 	prop->v.int_value = value;
110*18c2aff7Sartem 
111*18c2aff7Sartem 	return prop;
112*18c2aff7Sartem }
113*18c2aff7Sartem 
114*18c2aff7Sartem HalProperty *
115*18c2aff7Sartem hal_property_new_uint64 (const char *key, dbus_uint64_t value)
116*18c2aff7Sartem {
117*18c2aff7Sartem 	HalProperty *prop;
118*18c2aff7Sartem 
119*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
120*18c2aff7Sartem 
121*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_UINT64;
122*18c2aff7Sartem 	prop->key = g_strdup (key);
123*18c2aff7Sartem 	prop->v.uint64_value = value;
124*18c2aff7Sartem 
125*18c2aff7Sartem 	return prop;
126*18c2aff7Sartem }
127*18c2aff7Sartem 
128*18c2aff7Sartem HalProperty *
129*18c2aff7Sartem hal_property_new_bool (const char *key, dbus_bool_t value)
130*18c2aff7Sartem {
131*18c2aff7Sartem 	HalProperty *prop;
132*18c2aff7Sartem 
133*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
134*18c2aff7Sartem 
135*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_BOOLEAN;
136*18c2aff7Sartem 	prop->key = g_strdup (key);
137*18c2aff7Sartem 	prop->v.bool_value = value;
138*18c2aff7Sartem 
139*18c2aff7Sartem 	return prop;
140*18c2aff7Sartem }
141*18c2aff7Sartem 
142*18c2aff7Sartem HalProperty *
143*18c2aff7Sartem hal_property_new_double (const char *key, double value)
144*18c2aff7Sartem {
145*18c2aff7Sartem 	HalProperty *prop;
146*18c2aff7Sartem 
147*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
148*18c2aff7Sartem 
149*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_DOUBLE;
150*18c2aff7Sartem 	prop->key = g_strdup (key);
151*18c2aff7Sartem 	prop->v.double_value = value;
152*18c2aff7Sartem 
153*18c2aff7Sartem 	return prop;
154*18c2aff7Sartem }
155*18c2aff7Sartem 
156*18c2aff7Sartem const char *
157*18c2aff7Sartem hal_property_get_key (HalProperty *prop)
158*18c2aff7Sartem {
159*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, NULL);
160*18c2aff7Sartem 
161*18c2aff7Sartem 	return prop->key;
162*18c2aff7Sartem }
163*18c2aff7Sartem 
164*18c2aff7Sartem int
165*18c2aff7Sartem hal_property_get_type (HalProperty *prop)
166*18c2aff7Sartem {
167*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, HAL_PROPERTY_TYPE_INVALID);
168*18c2aff7Sartem 
169*18c2aff7Sartem 	return prop->type;
170*18c2aff7Sartem }
171*18c2aff7Sartem 
172*18c2aff7Sartem const char *
173*18c2aff7Sartem hal_property_get_string (HalProperty *prop)
174*18c2aff7Sartem {
175*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, NULL);
176*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRING, NULL);
177*18c2aff7Sartem 
178*18c2aff7Sartem 	return prop->v.str_value;
179*18c2aff7Sartem }
180*18c2aff7Sartem 
181*18c2aff7Sartem dbus_int32_t
182*18c2aff7Sartem hal_property_get_int (HalProperty *prop)
183*18c2aff7Sartem {
184*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, -1);
185*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_INT32, -1);
186*18c2aff7Sartem 
187*18c2aff7Sartem 	return prop->v.int_value;
188*18c2aff7Sartem }
189*18c2aff7Sartem 
190*18c2aff7Sartem dbus_uint64_t
191*18c2aff7Sartem hal_property_get_uint64 (HalProperty *prop)
192*18c2aff7Sartem {
193*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, -1);
194*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_UINT64, -1);
195*18c2aff7Sartem 
196*18c2aff7Sartem 	return prop->v.uint64_value;
197*18c2aff7Sartem }
198*18c2aff7Sartem 
199*18c2aff7Sartem dbus_bool_t
200*18c2aff7Sartem hal_property_get_bool (HalProperty *prop)
201*18c2aff7Sartem {
202*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
203*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_BOOLEAN, FALSE);
204*18c2aff7Sartem 
205*18c2aff7Sartem 	return prop->v.bool_value;
206*18c2aff7Sartem }
207*18c2aff7Sartem 
208*18c2aff7Sartem char *
209*18c2aff7Sartem hal_property_to_string (HalProperty *prop)
210*18c2aff7Sartem {
211*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, NULL);
212*18c2aff7Sartem 
213*18c2aff7Sartem 	switch (prop->type) {
214*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_STRING:
215*18c2aff7Sartem 		return g_strdup (prop->v.str_value);
216*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_INT32:
217*18c2aff7Sartem 		return g_strdup_printf ("%d", prop->v.int_value);
218*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_UINT64:
219*18c2aff7Sartem 		return g_strdup_printf ("%llu", (long long unsigned int) prop->v.uint64_value);
220*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_BOOLEAN:
221*18c2aff7Sartem 		/* FIXME: Maybe use 1 and 0 here instead? */
222*18c2aff7Sartem 		return g_strdup (prop->v.bool_value ? "true" : "false");
223*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_DOUBLE:
224*18c2aff7Sartem 		return g_strdup_printf ("%f", prop->v.double_value);
225*18c2aff7Sartem 	case HAL_PROPERTY_TYPE_STRLIST:
226*18c2aff7Sartem 	{
227*18c2aff7Sartem 		GSList *iter;
228*18c2aff7Sartem 		guint i;
229*18c2aff7Sartem 		char buf[256];
230*18c2aff7Sartem 
231*18c2aff7Sartem 		i = 0;
232*18c2aff7Sartem 		buf[0] = '\0';
233*18c2aff7Sartem 		for (iter = hal_property_get_strlist (prop);
234*18c2aff7Sartem 		     iter != NULL && i < sizeof(buf);
235*18c2aff7Sartem 		     iter = g_slist_next (iter)) {
236*18c2aff7Sartem 			guint len;
237*18c2aff7Sartem 			const char *str;
238*18c2aff7Sartem 
239*18c2aff7Sartem 			str = (const char *) iter->data;
240*18c2aff7Sartem 			len = strlen (str);
241*18c2aff7Sartem 			strncpy (buf + i, str, sizeof(buf) - i);
242*18c2aff7Sartem 			i += len;
243*18c2aff7Sartem 
244*18c2aff7Sartem 			if (g_slist_next (iter) != NULL && i < sizeof(buf)) {
245*18c2aff7Sartem 				buf[i] = '\t';
246*18c2aff7Sartem 				i++;
247*18c2aff7Sartem 			}
248*18c2aff7Sartem 		}
249*18c2aff7Sartem 		return g_strdup (buf);
250*18c2aff7Sartem 	}
251*18c2aff7Sartem 
252*18c2aff7Sartem 	default:
253*18c2aff7Sartem 		return NULL;
254*18c2aff7Sartem 	}
255*18c2aff7Sartem }
256*18c2aff7Sartem 
257*18c2aff7Sartem double
258*18c2aff7Sartem hal_property_get_double (HalProperty *prop)
259*18c2aff7Sartem {
260*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, -1.0);
261*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_DOUBLE, -1.0);
262*18c2aff7Sartem 
263*18c2aff7Sartem 	return prop->v.double_value;
264*18c2aff7Sartem }
265*18c2aff7Sartem 
266*18c2aff7Sartem void
267*18c2aff7Sartem hal_property_set_string (HalProperty *prop, const char *value)
268*18c2aff7Sartem {
269*18c2aff7Sartem 	char *endchar;
270*18c2aff7Sartem 	gboolean validated = TRUE;
271*18c2aff7Sartem 
272*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
273*18c2aff7Sartem 	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_STRING ||
274*18c2aff7Sartem 			  prop->type == HAL_PROPERTY_TYPE_INVALID);
275*18c2aff7Sartem 
276*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_STRING;
277*18c2aff7Sartem 	if (prop->v.str_value != NULL)
278*18c2aff7Sartem 		g_free (prop->v.str_value);
279*18c2aff7Sartem 	prop->v.str_value = g_strdup (value);
280*18c2aff7Sartem 
281*18c2aff7Sartem 	while (!g_utf8_validate (prop->v.str_value, -1,
282*18c2aff7Sartem 				 (const char **) &endchar)) {
283*18c2aff7Sartem 		validated = FALSE;
284*18c2aff7Sartem 		*endchar = '?';
285*18c2aff7Sartem 	}
286*18c2aff7Sartem 
287*18c2aff7Sartem 	if (!validated) {
288*18c2aff7Sartem 		HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
289*18c2aff7Sartem 			      prop->key, value));
290*18c2aff7Sartem 	}
291*18c2aff7Sartem }
292*18c2aff7Sartem 
293*18c2aff7Sartem void
294*18c2aff7Sartem hal_property_set_int (HalProperty *prop, dbus_int32_t value)
295*18c2aff7Sartem {
296*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
297*18c2aff7Sartem 	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_INT32 ||
298*18c2aff7Sartem 			  prop->type == HAL_PROPERTY_TYPE_INVALID);
299*18c2aff7Sartem 
300*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_INT32;
301*18c2aff7Sartem 	prop->v.int_value = value;
302*18c2aff7Sartem }
303*18c2aff7Sartem 
304*18c2aff7Sartem void
305*18c2aff7Sartem hal_property_set_uint64 (HalProperty *prop, dbus_uint64_t value)
306*18c2aff7Sartem {
307*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
308*18c2aff7Sartem 	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_UINT64 ||
309*18c2aff7Sartem 			  prop->type == HAL_PROPERTY_TYPE_INVALID);
310*18c2aff7Sartem 
311*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_UINT64;
312*18c2aff7Sartem 	prop->v.uint64_value = value;
313*18c2aff7Sartem }
314*18c2aff7Sartem 
315*18c2aff7Sartem void
316*18c2aff7Sartem hal_property_set_bool (HalProperty *prop, dbus_bool_t value)
317*18c2aff7Sartem {
318*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
319*18c2aff7Sartem 	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_BOOLEAN ||
320*18c2aff7Sartem 			  prop->type == HAL_PROPERTY_TYPE_INVALID);
321*18c2aff7Sartem 
322*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_BOOLEAN;
323*18c2aff7Sartem 	prop->v.bool_value = value;
324*18c2aff7Sartem }
325*18c2aff7Sartem 
326*18c2aff7Sartem void
327*18c2aff7Sartem hal_property_set_double (HalProperty *prop, double value)
328*18c2aff7Sartem {
329*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
330*18c2aff7Sartem 	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_DOUBLE ||
331*18c2aff7Sartem 			  prop->type == HAL_PROPERTY_TYPE_INVALID);
332*18c2aff7Sartem 
333*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_DOUBLE;
334*18c2aff7Sartem 	prop->v.double_value = value;
335*18c2aff7Sartem }
336*18c2aff7Sartem 
337*18c2aff7Sartem void
338*18c2aff7Sartem hal_property_set_attribute (HalProperty *prop,
339*18c2aff7Sartem 			    enum PropertyAttribute attr,
340*18c2aff7Sartem 			    gboolean val)
341*18c2aff7Sartem {
342*18c2aff7Sartem 	g_return_if_fail (prop != NULL);
343*18c2aff7Sartem 
344*18c2aff7Sartem 	switch (attr) {
345*18c2aff7Sartem 	case READONLY:
346*18c2aff7Sartem 		prop->readonly = val;
347*18c2aff7Sartem 		break;
348*18c2aff7Sartem 	case PERSISTENCE:
349*18c2aff7Sartem 		prop->persistence = val;
350*18c2aff7Sartem 		break;
351*18c2aff7Sartem 	case CALLOUT:
352*18c2aff7Sartem 		prop->callout = val;
353*18c2aff7Sartem 		break;
354*18c2aff7Sartem 	}
355*18c2aff7Sartem }
356*18c2aff7Sartem 
357*18c2aff7Sartem gboolean
358*18c2aff7Sartem hal_property_get_attribute (HalProperty *prop,
359*18c2aff7Sartem 			    enum PropertyAttribute attr)
360*18c2aff7Sartem {
361*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, -1);
362*18c2aff7Sartem 
363*18c2aff7Sartem 	switch (attr) {
364*18c2aff7Sartem 	case READONLY:
365*18c2aff7Sartem 		return prop->readonly;
366*18c2aff7Sartem 	case PERSISTENCE:
367*18c2aff7Sartem 		return prop->persistence;
368*18c2aff7Sartem 	case CALLOUT:
369*18c2aff7Sartem 		return prop->callout;
370*18c2aff7Sartem 	default:
371*18c2aff7Sartem 		return -1;
372*18c2aff7Sartem 	}
373*18c2aff7Sartem }
374*18c2aff7Sartem 
375*18c2aff7Sartem HalProperty *
376*18c2aff7Sartem hal_property_new_strlist (const char *key)
377*18c2aff7Sartem {
378*18c2aff7Sartem 	HalProperty *prop;
379*18c2aff7Sartem 
380*18c2aff7Sartem 	prop = g_new0 (HalProperty, 1);
381*18c2aff7Sartem 
382*18c2aff7Sartem 	prop->type = HAL_PROPERTY_TYPE_STRLIST;
383*18c2aff7Sartem 	prop->key = g_strdup (key);
384*18c2aff7Sartem 	prop->v.strlist_value = NULL;
385*18c2aff7Sartem 
386*18c2aff7Sartem 	return prop;
387*18c2aff7Sartem }
388*18c2aff7Sartem 
389*18c2aff7Sartem GSList *
390*18c2aff7Sartem hal_property_get_strlist (HalProperty *prop)
391*18c2aff7Sartem {
392*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, NULL);
393*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, NULL);
394*18c2aff7Sartem 
395*18c2aff7Sartem 	return prop->v.strlist_value;
396*18c2aff7Sartem }
397*18c2aff7Sartem 
398*18c2aff7Sartem gboolean
399*18c2aff7Sartem hal_property_strlist_append (HalProperty *prop, const char *value)
400*18c2aff7Sartem {
401*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
402*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
403*18c2aff7Sartem 
404*18c2aff7Sartem 	prop->v.strlist_value = g_slist_append (prop->v.strlist_value, g_strdup (value));
405*18c2aff7Sartem 
406*18c2aff7Sartem 	return TRUE;
407*18c2aff7Sartem }
408*18c2aff7Sartem 
409*18c2aff7Sartem gboolean
410*18c2aff7Sartem hal_property_strlist_prepend (HalProperty *prop, const char *value)
411*18c2aff7Sartem {
412*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
413*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
414*18c2aff7Sartem 
415*18c2aff7Sartem 	prop->v.strlist_value = g_slist_prepend (prop->v.strlist_value, g_strdup (value));
416*18c2aff7Sartem 
417*18c2aff7Sartem 	return TRUE;
418*18c2aff7Sartem }
419*18c2aff7Sartem 
420*18c2aff7Sartem gboolean
421*18c2aff7Sartem hal_property_strlist_remove_elem (HalProperty *prop, guint index)
422*18c2aff7Sartem {
423*18c2aff7Sartem 	GSList *elem;
424*18c2aff7Sartem 
425*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
426*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
427*18c2aff7Sartem 
428*18c2aff7Sartem 	if (prop->v.strlist_value == NULL)
429*18c2aff7Sartem 		return FALSE;
430*18c2aff7Sartem 
431*18c2aff7Sartem 	elem = g_slist_nth (prop->v.strlist_value, index);
432*18c2aff7Sartem 	if (elem == NULL)
433*18c2aff7Sartem 		return FALSE;
434*18c2aff7Sartem 
435*18c2aff7Sartem 	g_free (elem->data);
436*18c2aff7Sartem 	prop->v.strlist_value = g_slist_delete_link (prop->v.strlist_value, elem);
437*18c2aff7Sartem 	return TRUE;
438*18c2aff7Sartem }
439*18c2aff7Sartem 
440*18c2aff7Sartem 
441*18c2aff7Sartem gboolean
442*18c2aff7Sartem hal_property_strlist_add (HalProperty  *prop, const char *value)
443*18c2aff7Sartem {
444*18c2aff7Sartem 	GSList *elem;
445*18c2aff7Sartem 
446*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
447*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
448*18c2aff7Sartem 
449*18c2aff7Sartem 	for (elem = prop->v.strlist_value; elem != NULL; elem = g_slist_next (elem)) {
450*18c2aff7Sartem 		if (strcmp (elem->data, value) == 0) {
451*18c2aff7Sartem 			return FALSE;
452*18c2aff7Sartem 		}
453*18c2aff7Sartem 	}
454*18c2aff7Sartem 
455*18c2aff7Sartem 	return hal_property_strlist_append (prop, value);
456*18c2aff7Sartem }
457*18c2aff7Sartem 
458*18c2aff7Sartem gboolean
459*18c2aff7Sartem hal_property_strlist_remove (HalProperty *prop, const char *value)
460*18c2aff7Sartem {
461*18c2aff7Sartem 	guint i;
462*18c2aff7Sartem 	GSList *elem;
463*18c2aff7Sartem 
464*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
465*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
466*18c2aff7Sartem 
467*18c2aff7Sartem 	for (elem = prop->v.strlist_value, i = 0; elem != NULL; elem = g_slist_next (elem), i++) {
468*18c2aff7Sartem 		if (strcmp (elem->data, value) == 0) {
469*18c2aff7Sartem 			return hal_property_strlist_remove_elem (prop, i);
470*18c2aff7Sartem 		}
471*18c2aff7Sartem 	}
472*18c2aff7Sartem 
473*18c2aff7Sartem 	return FALSE;
474*18c2aff7Sartem }
475*18c2aff7Sartem 
476*18c2aff7Sartem gboolean
477*18c2aff7Sartem hal_property_strlist_clear (HalProperty *prop)
478*18c2aff7Sartem {
479*18c2aff7Sartem 	GSList *elem;
480*18c2aff7Sartem 
481*18c2aff7Sartem 	g_return_val_if_fail (prop != NULL, FALSE);
482*18c2aff7Sartem 	g_return_val_if_fail (prop->type == HAL_PROPERTY_TYPE_STRLIST, FALSE);
483*18c2aff7Sartem 
484*18c2aff7Sartem 	for (elem = prop->v.strlist_value; elem != NULL; elem = g_slist_next (elem)) {
485*18c2aff7Sartem 		g_free (elem->data);
486*18c2aff7Sartem 	}
487*18c2aff7Sartem 	g_slist_free (prop->v.strlist_value);
488*18c2aff7Sartem 
489*18c2aff7Sartem 	return FALSE;
490*18c2aff7Sartem }
491