1 /***************************************************************************
4 * property.c : HalProperty methods
6 * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
7 * Copyright (C) 2004 Novell, Inc.
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 **************************************************************************/
43 dbus_int32_t int_value
;
44 dbus_uint64_t uint64_value
;
45 dbus_bool_t bool_value
;
47 GSList
*strlist_value
;
55 hal_property_free (HalProperty
*prop
)
60 if (prop
->type
== HAL_PROPERTY_TYPE_STRING
) {
61 g_free (prop
->v
.str_value
);
62 } else if (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
) {
64 for (i
= prop
->v
.strlist_value
; i
!= NULL
; i
= g_slist_next (i
)) {
67 g_slist_free (prop
->v
.strlist_value
);
74 hal_property_new_string (const char *key
, const char *value
)
78 gboolean validated
= TRUE
;
80 prop
= g_new0 (HalProperty
, 1);
82 prop
->type
= HAL_PROPERTY_TYPE_STRING
;
83 prop
->key
= g_strdup (key
);
84 prop
->v
.str_value
= g_strdup (value
!= NULL
? value
: "");
86 while (!g_utf8_validate (prop
->v
.str_value
, -1,
87 (const char **) &endchar
)) {
93 HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
94 key
, prop
->v
.str_value
));
101 hal_property_new_int (const char *key
, dbus_int32_t value
)
105 prop
= g_new0 (HalProperty
, 1);
107 prop
->type
= HAL_PROPERTY_TYPE_INT32
;
108 prop
->key
= g_strdup (key
);
109 prop
->v
.int_value
= value
;
115 hal_property_new_uint64 (const char *key
, dbus_uint64_t value
)
119 prop
= g_new0 (HalProperty
, 1);
121 prop
->type
= HAL_PROPERTY_TYPE_UINT64
;
122 prop
->key
= g_strdup (key
);
123 prop
->v
.uint64_value
= value
;
129 hal_property_new_bool (const char *key
, dbus_bool_t value
)
133 prop
= g_new0 (HalProperty
, 1);
135 prop
->type
= HAL_PROPERTY_TYPE_BOOLEAN
;
136 prop
->key
= g_strdup (key
);
137 prop
->v
.bool_value
= value
;
143 hal_property_new_double (const char *key
, double value
)
147 prop
= g_new0 (HalProperty
, 1);
149 prop
->type
= HAL_PROPERTY_TYPE_DOUBLE
;
150 prop
->key
= g_strdup (key
);
151 prop
->v
.double_value
= value
;
157 hal_property_get_key (HalProperty
*prop
)
159 g_return_val_if_fail (prop
!= NULL
, NULL
);
165 hal_property_get_type (HalProperty
*prop
)
167 g_return_val_if_fail (prop
!= NULL
, HAL_PROPERTY_TYPE_INVALID
);
173 hal_property_get_string (HalProperty
*prop
)
175 g_return_val_if_fail (prop
!= NULL
, NULL
);
176 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRING
, NULL
);
178 return prop
->v
.str_value
;
182 hal_property_get_int (HalProperty
*prop
)
184 g_return_val_if_fail (prop
!= NULL
, -1);
185 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_INT32
, -1);
187 return prop
->v
.int_value
;
191 hal_property_get_uint64 (HalProperty
*prop
)
193 g_return_val_if_fail (prop
!= NULL
, -1);
194 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_UINT64
, -1);
196 return prop
->v
.uint64_value
;
200 hal_property_get_bool (HalProperty
*prop
)
202 g_return_val_if_fail (prop
!= NULL
, FALSE
);
203 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_BOOLEAN
, FALSE
);
205 return prop
->v
.bool_value
;
209 hal_property_to_string (HalProperty
*prop
)
211 g_return_val_if_fail (prop
!= NULL
, NULL
);
213 switch (prop
->type
) {
214 case HAL_PROPERTY_TYPE_STRING
:
215 return g_strdup (prop
->v
.str_value
);
216 case HAL_PROPERTY_TYPE_INT32
:
217 return g_strdup_printf ("%d", prop
->v
.int_value
);
218 case HAL_PROPERTY_TYPE_UINT64
:
219 return g_strdup_printf ("%llu", (long long unsigned int) prop
->v
.uint64_value
);
220 case HAL_PROPERTY_TYPE_BOOLEAN
:
221 /* FIXME: Maybe use 1 and 0 here instead? */
222 return g_strdup (prop
->v
.bool_value
? "true" : "false");
223 case HAL_PROPERTY_TYPE_DOUBLE
:
224 return g_strdup_printf ("%f", prop
->v
.double_value
);
225 case HAL_PROPERTY_TYPE_STRLIST
:
233 for (iter
= hal_property_get_strlist (prop
);
234 iter
!= NULL
&& i
< sizeof(buf
);
235 iter
= g_slist_next (iter
)) {
239 str
= (const char *) iter
->data
;
241 strncpy (buf
+ i
, str
, sizeof(buf
) - i
);
244 if (g_slist_next (iter
) != NULL
&& i
< sizeof(buf
)) {
249 return g_strdup (buf
);
258 hal_property_get_double (HalProperty
*prop
)
260 g_return_val_if_fail (prop
!= NULL
, -1.0);
261 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_DOUBLE
, -1.0);
263 return prop
->v
.double_value
;
267 hal_property_set_string (HalProperty
*prop
, const char *value
)
270 gboolean validated
= TRUE
;
272 g_return_if_fail (prop
!= NULL
);
273 g_return_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRING
||
274 prop
->type
== HAL_PROPERTY_TYPE_INVALID
);
276 prop
->type
= HAL_PROPERTY_TYPE_STRING
;
277 if (prop
->v
.str_value
!= NULL
)
278 g_free (prop
->v
.str_value
);
279 prop
->v
.str_value
= g_strdup (value
);
281 while (!g_utf8_validate (prop
->v
.str_value
, -1,
282 (const char **) &endchar
)) {
288 HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
294 hal_property_set_int (HalProperty
*prop
, dbus_int32_t value
)
296 g_return_if_fail (prop
!= NULL
);
297 g_return_if_fail (prop
->type
== HAL_PROPERTY_TYPE_INT32
||
298 prop
->type
== HAL_PROPERTY_TYPE_INVALID
);
300 prop
->type
= HAL_PROPERTY_TYPE_INT32
;
301 prop
->v
.int_value
= value
;
305 hal_property_set_uint64 (HalProperty
*prop
, dbus_uint64_t value
)
307 g_return_if_fail (prop
!= NULL
);
308 g_return_if_fail (prop
->type
== HAL_PROPERTY_TYPE_UINT64
||
309 prop
->type
== HAL_PROPERTY_TYPE_INVALID
);
311 prop
->type
= HAL_PROPERTY_TYPE_UINT64
;
312 prop
->v
.uint64_value
= value
;
316 hal_property_set_bool (HalProperty
*prop
, dbus_bool_t value
)
318 g_return_if_fail (prop
!= NULL
);
319 g_return_if_fail (prop
->type
== HAL_PROPERTY_TYPE_BOOLEAN
||
320 prop
->type
== HAL_PROPERTY_TYPE_INVALID
);
322 prop
->type
= HAL_PROPERTY_TYPE_BOOLEAN
;
323 prop
->v
.bool_value
= value
;
327 hal_property_set_double (HalProperty
*prop
, double value
)
329 g_return_if_fail (prop
!= NULL
);
330 g_return_if_fail (prop
->type
== HAL_PROPERTY_TYPE_DOUBLE
||
331 prop
->type
== HAL_PROPERTY_TYPE_INVALID
);
333 prop
->type
= HAL_PROPERTY_TYPE_DOUBLE
;
334 prop
->v
.double_value
= value
;
338 hal_property_set_attribute (HalProperty
*prop
,
339 enum PropertyAttribute attr
,
342 g_return_if_fail (prop
!= NULL
);
346 prop
->readonly
= val
;
349 prop
->persistence
= val
;
358 hal_property_get_attribute (HalProperty
*prop
,
359 enum PropertyAttribute attr
)
361 g_return_val_if_fail (prop
!= NULL
, -1);
365 return prop
->readonly
;
367 return prop
->persistence
;
369 return prop
->callout
;
376 hal_property_new_strlist (const char *key
)
380 prop
= g_new0 (HalProperty
, 1);
382 prop
->type
= HAL_PROPERTY_TYPE_STRLIST
;
383 prop
->key
= g_strdup (key
);
384 prop
->v
.strlist_value
= NULL
;
390 hal_property_get_strlist (HalProperty
*prop
)
392 g_return_val_if_fail (prop
!= NULL
, NULL
);
393 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, NULL
);
395 return prop
->v
.strlist_value
;
399 hal_property_strlist_append (HalProperty
*prop
, const char *value
)
401 g_return_val_if_fail (prop
!= NULL
, FALSE
);
402 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
404 prop
->v
.strlist_value
= g_slist_append (prop
->v
.strlist_value
, g_strdup (value
));
410 hal_property_strlist_prepend (HalProperty
*prop
, const char *value
)
412 g_return_val_if_fail (prop
!= NULL
, FALSE
);
413 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
415 prop
->v
.strlist_value
= g_slist_prepend (prop
->v
.strlist_value
, g_strdup (value
));
421 hal_property_strlist_remove_elem (HalProperty
*prop
, guint index
)
425 g_return_val_if_fail (prop
!= NULL
, FALSE
);
426 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
428 if (prop
->v
.strlist_value
== NULL
)
431 elem
= g_slist_nth (prop
->v
.strlist_value
, index
);
436 prop
->v
.strlist_value
= g_slist_delete_link (prop
->v
.strlist_value
, elem
);
442 hal_property_strlist_add (HalProperty
*prop
, const char *value
)
446 g_return_val_if_fail (prop
!= NULL
, FALSE
);
447 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
449 for (elem
= prop
->v
.strlist_value
; elem
!= NULL
; elem
= g_slist_next (elem
)) {
450 if (strcmp (elem
->data
, value
) == 0) {
455 return hal_property_strlist_append (prop
, value
);
459 hal_property_strlist_remove (HalProperty
*prop
, const char *value
)
464 g_return_val_if_fail (prop
!= NULL
, FALSE
);
465 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
467 for (elem
= prop
->v
.strlist_value
, i
= 0; elem
!= NULL
; elem
= g_slist_next (elem
), i
++) {
468 if (strcmp (elem
->data
, value
) == 0) {
469 return hal_property_strlist_remove_elem (prop
, i
);
477 hal_property_strlist_clear (HalProperty
*prop
)
481 g_return_val_if_fail (prop
!= NULL
, FALSE
);
482 g_return_val_if_fail (prop
->type
== HAL_PROPERTY_TYPE_STRLIST
, FALSE
);
484 for (elem
= prop
->v
.strlist_value
; elem
!= NULL
; elem
= g_slist_next (elem
)) {
487 g_slist_free (prop
->v
.strlist_value
);