src/mn-xml.c (3344B) - raw
1 /*
2 * Mail Notification
3 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <string.h>
21 #include <glib/gi18n.h>
22 #include "mn-xml.h"
23 #include "mn-util.h"
24
25 void
26 mn_xml_import_properties (GObject *object, xmlNode *node)
27 {
28 GParamSpec **properties;
29 unsigned int n_properties;
30 int i;
31
32 g_return_if_fail(G_IS_OBJECT(object));
33 g_return_if_fail(node != NULL);
34
35 properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(object), &n_properties);
36 for (i = 0; i < n_properties; i++)
37 if ((properties[i]->flags & MN_XML_PARAM_IMPORT) != 0)
38 {
39 char *content;
40
41 content = xmlGetProp(node, g_param_spec_get_name(properties[i]));
42 if (content)
43 {
44 GValue value = { 0, };
45
46 g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(properties[i]));
47
48 if (mn_g_value_from_string(&value, content))
49 g_object_set_property(object, g_param_spec_get_name(properties[i]), &value);
50 else
51 g_warning(_("property \"%s\": unable to transform string \"%s\" into a value of type \"%s\""),
52 g_param_spec_get_name(properties[i]), content, G_VALUE_TYPE_NAME(&value));
53
54 g_value_unset(&value);
55 g_free(content);
56 }
57 }
58 g_free(properties);
59 }
60
61 void
62 mn_xml_export_properties (GObject *object, xmlNode *node)
63 {
64 GParamSpec **properties;
65 unsigned int n_properties;
66 int i;
67
68 g_return_if_fail(G_IS_OBJECT(object));
69 g_return_if_fail(node != NULL);
70
71 properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(object), &n_properties);
72 for (i = 0; i < n_properties; i++)
73 if ((properties[i]->flags & MN_XML_PARAM_EXPORT) != 0)
74 {
75 GValue value = { 0, };
76 gboolean is_default;
77
78 g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(properties[i]));
79 g_object_get_property(object, g_param_spec_get_name(properties[i]), &value);
80
81 if ((properties[i]->flags & MN_XML_PARAM_IGNORE_CASE) != 0)
82 {
83 GValue default_value = { 0, };
84 const char *str;
85 const char *default_str;
86
87 g_assert(G_IS_PARAM_SPEC_STRING(properties[i]));
88
89 g_value_init(&default_value, G_TYPE_STRING);
90 g_param_value_set_default(properties[i], &default_value);
91
92 str = g_value_get_string(&value);
93 default_str = g_value_get_string(&default_value);
94
95 is_default = str && default_str && ! mn_utf8_strcasecmp(str, default_str);
96
97 g_value_unset(&default_value);
98 }
99 else
100 is_default = g_param_value_defaults(properties[i], &value);
101
102 if (! is_default)
103 {
104 char *str;
105
106 str = mn_g_value_to_string(&value);
107 xmlSetProp(node, g_param_spec_get_name(properties[i]), str);
108 g_free(str);
109 }
110
111 g_value_unset(&value);
112 }
113 g_free(properties);
114 }