1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* e-component-info.c - Load/save information about Evolution components. |
---|
3 | * |
---|
4 | * Copyright (C) 2002 Ximian, Inc. |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of version 2 of the GNU General Public |
---|
8 | * License as published by the Free Software Foundation. |
---|
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 GNU |
---|
13 | * General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public |
---|
16 | * License along with this program; if not, write to the |
---|
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | * Boston, MA 02111-1307, USA. |
---|
19 | * |
---|
20 | * Author: Ettore Perazzoli <ettore@ximian.com> |
---|
21 | */ |
---|
22 | |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include <config.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "e-component-info.h" |
---|
28 | |
---|
29 | #include "e-util/e-lang-utils.h" |
---|
30 | |
---|
31 | #include <gnome-xml/parser.h> |
---|
32 | #include <gnome-xml/xmlmemory.h> |
---|
33 | |
---|
34 | #include <string.h> |
---|
35 | #include <stdlib.h> |
---|
36 | |
---|
37 | |
---|
38 | static char * |
---|
39 | get_value_for_node (xmlNode *node) |
---|
40 | { |
---|
41 | xmlChar *xml_value; |
---|
42 | char *glib_value; |
---|
43 | |
---|
44 | xml_value = xmlNodeGetContent (node); |
---|
45 | glib_value = g_strdup (xml_value); |
---|
46 | xmlFree (xml_value); |
---|
47 | |
---|
48 | return glib_value; |
---|
49 | } |
---|
50 | |
---|
51 | static xmlNode * |
---|
52 | lookup_node (xmlNode *parent_node, |
---|
53 | const char *node_name) |
---|
54 | { |
---|
55 | xmlNode *p; |
---|
56 | |
---|
57 | for (p = parent_node->childs; p != NULL; p = p->next) { |
---|
58 | if (strcmp ((const char *) p->name, node_name) == 0) |
---|
59 | return p; |
---|
60 | } |
---|
61 | |
---|
62 | return NULL; |
---|
63 | } |
---|
64 | |
---|
65 | static char * |
---|
66 | get_value (xmlNode *parent_node, |
---|
67 | const char *node_name) |
---|
68 | { |
---|
69 | xmlNode *node; |
---|
70 | |
---|
71 | node = lookup_node (parent_node, node_name); |
---|
72 | if (node == NULL) |
---|
73 | return NULL; |
---|
74 | |
---|
75 | return get_value_for_node (node); |
---|
76 | } |
---|
77 | |
---|
78 | static xmlNode * |
---|
79 | lookup_node_for_language (xmlNode *parent_node, |
---|
80 | const char *node_name, |
---|
81 | const char *language_id) |
---|
82 | { |
---|
83 | xmlNode *p; |
---|
84 | |
---|
85 | for (p = parent_node->childs; p != NULL; p = p->next) { |
---|
86 | xmlChar *node_language_id; |
---|
87 | |
---|
88 | if (strcmp ((const char *) p->name, node_name) != 0) |
---|
89 | continue; |
---|
90 | |
---|
91 | node_language_id = xmlNodeGetLang (p); |
---|
92 | if (node_language_id == NULL) |
---|
93 | continue; |
---|
94 | |
---|
95 | if (strcmp (node_language_id, language_id) == 0) { |
---|
96 | xmlFree (node_language_id); |
---|
97 | return p; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | |
---|
104 | static char * |
---|
105 | get_i18n_value (xmlNode *parent_node, |
---|
106 | const char *node_name, |
---|
107 | GSList *language_list) |
---|
108 | { |
---|
109 | GSList *p; |
---|
110 | |
---|
111 | for (p = language_list; p != NULL; p = p->next) { |
---|
112 | xmlNode *node; |
---|
113 | const char *language_id; |
---|
114 | |
---|
115 | language_id = (const char *) p->data; |
---|
116 | node = lookup_node_for_language (parent_node, node_name, language_id); |
---|
117 | |
---|
118 | if (node != NULL) { |
---|
119 | xmlChar *xml_value; |
---|
120 | char *glib_value; |
---|
121 | |
---|
122 | xml_value = xmlNodeGetContent (node); |
---|
123 | glib_value = g_strdup (xml_value); |
---|
124 | xmlFree (xml_value); |
---|
125 | |
---|
126 | return glib_value; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | return get_value (parent_node, node_name); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | static void |
---|
135 | add_folder_type (EComponentInfo *info, |
---|
136 | xmlNode *parent_node, |
---|
137 | GSList *language_list) |
---|
138 | { |
---|
139 | EComponentInfoFolderType *folder_type; |
---|
140 | char *user_creatable_string; |
---|
141 | |
---|
142 | folder_type = g_new (EComponentInfoFolderType, 1); |
---|
143 | |
---|
144 | folder_type->name = get_value (parent_node, "name"); |
---|
145 | folder_type->icon_file_name = get_value (parent_node, "icon_file_name"); |
---|
146 | folder_type->display_name = get_i18n_value (parent_node, "display_name", language_list); |
---|
147 | folder_type->description = get_i18n_value (parent_node, "description", language_list); |
---|
148 | |
---|
149 | /* FIXME dnd types. */ |
---|
150 | |
---|
151 | folder_type->accepted_dnd_types = NULL; |
---|
152 | folder_type->exported_dnd_types = NULL; |
---|
153 | |
---|
154 | user_creatable_string = get_value (parent_node, "user_creatable"); |
---|
155 | if (user_creatable_string == NULL || atoi (user_creatable_string) == 0) |
---|
156 | folder_type->is_user_creatable = FALSE; |
---|
157 | else |
---|
158 | folder_type->is_user_creatable = TRUE; |
---|
159 | |
---|
160 | info->folder_types = g_slist_prepend (info->folder_types, folder_type); |
---|
161 | } |
---|
162 | |
---|
163 | static void |
---|
164 | add_user_creatable_item_type (EComponentInfo *info, |
---|
165 | xmlNode *parent_node, |
---|
166 | GSList *language_list) |
---|
167 | { |
---|
168 | EComponentInfoUserCreatableItemType *type; |
---|
169 | |
---|
170 | type = g_new (EComponentInfoUserCreatableItemType, 1); |
---|
171 | |
---|
172 | type->id = get_value (parent_node, "id"); |
---|
173 | type->description = get_i18n_value (parent_node, "description", language_list); |
---|
174 | type->icon_file_name = get_value (parent_node, "icon_file_name"); |
---|
175 | type->menu_description = get_i18n_value (parent_node, "menu_description", language_list); |
---|
176 | type->menu_shortcut = get_value (parent_node, "menu_shortcut"); |
---|
177 | |
---|
178 | info->user_creatable_item_types = g_slist_prepend (info->user_creatable_item_types, type); |
---|
179 | } |
---|
180 | |
---|
181 | static void |
---|
182 | add_uri_schema (EComponentInfo *info, |
---|
183 | xmlNode *parent_node) |
---|
184 | { |
---|
185 | info->uri_schemas = g_slist_prepend (info->uri_schemas, get_value_for_node (parent_node)); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | EComponentInfo * |
---|
190 | e_component_info_load (const char *file_name) |
---|
191 | { |
---|
192 | EComponentInfo *new; |
---|
193 | xmlDoc *doc; |
---|
194 | xmlNode *root; |
---|
195 | xmlNode *p; |
---|
196 | GSList *language_list; |
---|
197 | |
---|
198 | g_return_val_if_fail (file_name != NULL, NULL); |
---|
199 | |
---|
200 | doc = xmlParseFile (file_name); |
---|
201 | if (doc == NULL) |
---|
202 | return NULL; |
---|
203 | |
---|
204 | root = xmlDocGetRootElement (doc); |
---|
205 | if (root == NULL || strcmp (root->name, "evolution_component") != 0) { |
---|
206 | xmlFreeDoc (doc); |
---|
207 | return NULL; |
---|
208 | } |
---|
209 | |
---|
210 | language_list = e_get_language_list (); |
---|
211 | |
---|
212 | new = g_new (EComponentInfo, 1); |
---|
213 | |
---|
214 | new->id = get_value (root, "id"); |
---|
215 | new->description = get_i18n_value (root, "description", language_list); |
---|
216 | new->icon_file_name = get_value (root, "icon_file_name"); |
---|
217 | |
---|
218 | new->folder_types = NULL; |
---|
219 | new->uri_schemas = NULL; |
---|
220 | new->user_creatable_item_types = NULL; |
---|
221 | |
---|
222 | for (p = root->childs; p != NULL; p = p->next) { |
---|
223 | if (strcmp ((char *) p->name, "folder_type") == 0) |
---|
224 | add_folder_type (new, p, language_list); |
---|
225 | else if (strcmp ((char *) p->name, "user_creatable_item_type") == 0) |
---|
226 | add_user_creatable_item_type (new, p, language_list); |
---|
227 | else if (strcmp ((char *) p->name, "uri_schema") == 0) |
---|
228 | add_uri_schema (new, p); |
---|
229 | } |
---|
230 | |
---|
231 | xmlFreeDoc (doc); |
---|
232 | e_free_language_list (language_list); |
---|
233 | |
---|
234 | return new; |
---|
235 | } |
---|
236 | |
---|
237 | void |
---|
238 | e_component_info_free (EComponentInfo *component_info) |
---|
239 | { |
---|
240 | GSList *p; |
---|
241 | |
---|
242 | g_return_if_fail (component_info != NULL); |
---|
243 | |
---|
244 | g_free (component_info->id); |
---|
245 | g_free (component_info->description); |
---|
246 | g_free (component_info->icon_file_name); |
---|
247 | |
---|
248 | for (p = component_info->folder_types; p != NULL; p = p->next) { |
---|
249 | EComponentInfoFolderType *folder_type; |
---|
250 | GSList *q; |
---|
251 | |
---|
252 | folder_type = (EComponentInfoFolderType *) p->data; |
---|
253 | g_free (folder_type->name); |
---|
254 | g_free (folder_type->icon_file_name); |
---|
255 | g_free (folder_type->display_name); |
---|
256 | g_free (folder_type->description); |
---|
257 | |
---|
258 | for (q = folder_type->accepted_dnd_types; q != NULL; q = q->next) |
---|
259 | g_free ((char *) q->data); |
---|
260 | g_slist_free (folder_type->accepted_dnd_types); |
---|
261 | |
---|
262 | for (q = folder_type->exported_dnd_types; q != NULL; q = q->next) |
---|
263 | g_free ((char *) q->data); |
---|
264 | g_slist_free (folder_type->exported_dnd_types); |
---|
265 | |
---|
266 | g_free (folder_type); |
---|
267 | } |
---|
268 | g_free (component_info->folder_types); |
---|
269 | |
---|
270 | for (p = component_info->uri_schemas; p != NULL; p = p->next) |
---|
271 | g_free ((char *) p->data); |
---|
272 | g_slist_free (component_info->uri_schemas); |
---|
273 | |
---|
274 | for (p = component_info->user_creatable_item_types; p != NULL; p = p->next) { |
---|
275 | EComponentInfoUserCreatableItemType *type; |
---|
276 | |
---|
277 | type = (EComponentInfoUserCreatableItemType *) p->data; |
---|
278 | |
---|
279 | g_free (type->id); |
---|
280 | g_free (type->description); |
---|
281 | g_free (type->icon_file_name); |
---|
282 | g_free (type->menu_description); |
---|
283 | g_free (type->menu_shortcut); |
---|
284 | } |
---|
285 | g_slist_free (component_info->user_creatable_item_types); |
---|
286 | |
---|
287 | g_free (component_info); |
---|
288 | } |
---|