~ubuntu-branches/ubuntu/trusty/nautilus-actions/trusty-proposed

« back to all changes in this revision

Viewing changes to libnautilus-actions/nautilus-actions-config-xml.c

  • Committer: Bazaar Package Importer
  • Author(s): Christine Spang
  • Date: 2009-05-30 10:15:52 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090530101552-5nicx1db3luh9n7e
Tags: 1.10.1-1
* New upstream release.
  - Do not search through actions when the Nautilus-provided list of
    selected files is empty
  - Remove deprecated functions
  - All sources are now ansi-compliant, though not (yet) pedantic
  - Double-click on an action opens the editor
  - Enable the OK button as soon as required fields are filled
  - Enable syslog'ed debug messages when in maintainer mode
  - Use NACT_GNOME_COMPILE_WARNINGS (see Gnome bugzilla bug #582860)
  - Replace configure.in with configure.ac, updating all Makefile.am
    accordingly
  - Fix make distcheck
  - create nautilus-actions.doap
  - Gnome bugzilla bug #574919 fixed (Closes: #523854)
  - Gnome bugzilla bugs #522605, #573365, and #568366 fixed
  - translations updated
* Added Homepage: field to debian/control
* Bump to Standards-Version 3.8.1
* update GNOME_DOWNLOAD_URL in debian/rules
* add full list of contributors to debian/copyright
* Bump debhelper compat to 7
* Fix categories in .desktop file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Nautilus Actions configuration tool
2
 
 * Copyright (C) 2005 The GNOME Foundation
3
 
 *
4
 
 * Authors:
5
 
 *  Frederic Ruaudel (grumz@grumz.net)
6
 
 *       Rodrigo Moya (rodrigo@gnome-db.org)
7
 
 *
8
 
 * This Program is free software; you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation; either version 2 of the
11
 
 * License, or (at your option) any later version.
12
 
 *
13
 
 * This Program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public
19
 
 * License along with this Library; see the file COPYING.  If not,
20
 
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
 
 * Boston, MA 02111-1307, USA.
22
 
 */
23
 
 
24
 
#include <config.h>
25
 
#include <string.h>
26
 
#include <libxml/parser.h>
27
 
#include <libxml/tree.h>
28
 
#include <uuid/uuid.h>
29
 
#include "nautilus-actions-config-xml.h"
30
 
#include "nautilus-actions-config.h"
31
 
 
32
 
#define ACTIONS_CONFIG_DIR                                      DEFAULT_CONFIG_PATH
33
 
#define ACTIONS_PER_USER_CONFIG_DIR             ".config/nautilus-actions"
34
 
#define ACTION_ROOT                                                     "nautilus-actions-config"
35
 
#define ACTION_VERSION                                          "version"
36
 
#define ACTION_ACTION                                           "action"
37
 
#define ACTION_ACTION_NAME                                      "name"
38
 
#define ACTION_MENU_ITEM                                        "menu-item"
39
 
#define ACTION_LANG                                                     "lang"
40
 
#define ACTION_LABEL                                                    "label"
41
 
#define ACTION_TOOLTIP                                          "tooltip"
42
 
#define ACTION_COMMAND                                          "command"
43
 
#define ACTION_PATH                                                     "path"
44
 
#define ACTION_PARAMS                                           "parameters"
45
 
#define ACTION_TEST                                                     "test"
46
 
#define ACTION_BASENAMES                                        "basename"
47
 
#define ACTION_BASENAMES_MATCH                  "match"
48
 
#define ACTION_ISFILE                                           "isfile"
49
 
#define ACTION_ISDIR                                                    "isdir"
50
 
#define ACTION_MULTIPLE                                         "accept-multiple-files"
51
 
#define ACTION_SCHEMES                                          "scheme"
52
 
#define ACTION_SCHEMES_TYPE                             "type"
53
 
 
54
 
static GObjectClass *parent_class = NULL;
55
 
 
56
 
static gboolean
57
 
save_action (NautilusActionsConfig *self, NautilusActionsConfigAction *action)
58
 
{
59
 
        return TRUE;
60
 
/*
61
 
        g_return_val_if_fail (NAUTILUS_ACTIONS_IS_CONFIG_XML (self), NULL);
62
 
 
63
 
        NautilusActionsConfigXml* config = NAUTILUS_ACTIONS_CONFIG_XML (self);
64
 
        gchar *key;
65
 
 
66
 
        // set the version on the action 
67
 
        if (action->version)
68
 
                g_free (action->version);
69
 
        action->version = g_strdup (NAUTILUS_ACTIONS_CONFIG_VERSION);
70
 
*/
71
 
}
72
 
 
73
 
static gboolean
74
 
remove_action (NautilusActionsConfig *self, NautilusActionsConfigAction* action)
75
 
{
76
 
        g_return_val_if_fail (NAUTILUS_ACTIONS_IS_CONFIG_XML (self), FALSE);
77
 
 
78
 
        //NautilusActionsConfigXml* config = NAUTILUS_ACTIONS_CONFIG_XML (self);
79
 
 
80
 
        return TRUE;
81
 
}
82
 
 
83
 
static GList *nautilus_actions_config_xml_get_config_files (void)
84
 
{
85
 
        GList* config_files = NULL;
86
 
        GDir* config_dir = NULL;
87
 
        const gchar* filename;
88
 
        gchar* path;
89
 
        gchar* per_user_dir = g_build_path ("/", g_get_home_dir (), ACTIONS_PER_USER_CONFIG_DIR, NULL);
90
 
 
91
 
        /* First get the per user files so they have priority because there are first parsed */
92
 
 
93
 
        if (g_file_test (per_user_dir, G_FILE_TEST_IS_DIR))
94
 
        {
95
 
                config_dir = g_dir_open (per_user_dir, 0, NULL);
96
 
                if (config_dir != NULL)
97
 
                {
98
 
                        filename = g_dir_read_name (config_dir);
99
 
                        while (filename != NULL)
100
 
                        {
101
 
                                path = g_build_path ("/", per_user_dir, filename, NULL);
102
 
                                if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
103
 
                                {
104
 
                                        // This is a regular file, we can add it 
105
 
                                        config_files = g_list_append (config_files, g_strdup (path));
106
 
                                }
107
 
                                g_free (path);
108
 
                                filename = g_dir_read_name (config_dir);
109
 
                        }
110
 
 
111
 
                        g_dir_close (config_dir);
112
 
                }
113
 
        }
114
 
        
115
 
        g_free (per_user_dir);
116
 
 
117
 
        /* Then get system-wide config files, if there are duplicate of above, they will be skipped during parsing */
118
 
        if (g_file_test (ACTIONS_CONFIG_DIR, G_FILE_TEST_IS_DIR))
119
 
        {
120
 
                config_dir = g_dir_open (ACTIONS_CONFIG_DIR, 0, NULL);
121
 
                if (config_dir != NULL)
122
 
                {
123
 
                        filename = g_dir_read_name (config_dir);
124
 
                        while (filename != NULL)
125
 
                        {
126
 
                                path = g_build_path ("/", ACTIONS_CONFIG_DIR, filename, NULL);
127
 
                                if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
128
 
                                {
129
 
                                        /* This is a regular file, we can add it */
130
 
                                        config_files = g_list_append (config_files, g_strdup (path));
131
 
                                }
132
 
                                g_free (path);
133
 
                                filename = g_dir_read_name (config_dir);
134
 
                        }
135
 
 
136
 
                        g_dir_close (config_dir);
137
 
                }
138
 
        }
139
 
 
140
 
        return config_files;
141
 
}
142
 
 
143
 
static void
144
 
nautilus_actions_config_xml_free_config_files (GList* config_files)
145
 
{
146
 
        g_list_foreach (config_files, (GFunc) g_free, NULL);
147
 
        g_list_free (config_files);
148
 
        config_files = NULL;
149
 
}
150
 
 
151
 
static gboolean
152
 
nautilus_actions_config_xml_action_fill_test_basenames (GSList** test_basenames, xmlNode* config_test_basename_node, const gchar* config_version)
153
 
{
154
 
        xmlNode *iter;
155
 
        gboolean retv = FALSE;
156
 
 
157
 
        if (g_ascii_strncasecmp (config_version, "0.1", strlen (config_version)) == 0)
158
 
        {
159
 
                //--> manage backward compatibility
160
 
                xmlChar* text = xmlNodeGetContent (config_test_basename_node);
161
 
                (*test_basenames) = g_slist_append ((*test_basenames), xmlStrdup (text));
162
 
                xmlFree (text);
163
 
                retv = TRUE;
164
 
        }
165
 
        else
166
 
        {
167
 
                for (iter = config_test_basename_node->children; iter; iter = iter->next)
168
 
                {
169
 
                        xmlChar* text;
170
 
                        
171
 
                        if (iter->type == XML_ELEMENT_NODE &&
172
 
                                        g_ascii_strncasecmp ((const gchar *) iter->name, 
173
 
                                                             ACTION_BASENAMES_MATCH,
174
 
                                                             strlen (ACTION_BASENAMES_MATCH)) == 0)
175
 
                        {
176
 
                                text = xmlNodeGetContent (iter);
177
 
                                (*test_basenames) = g_slist_append ((*test_basenames), xmlStrdup (text));
178
 
                                xmlFree (text);
179
 
                                retv = TRUE;
180
 
                        }
181
 
                }
182
 
        }
183
 
 
184
 
        return retv;
185
 
}
186
 
 
187
 
static gboolean
188
 
nautilus_actions_config_xml_action_fill_test_scheme (GSList** test_scheme, xmlNode* config_test_scheme_node)
189
 
{
190
 
        xmlNode *iter;
191
 
        gboolean retv = FALSE;
192
 
 
193
 
        for (iter = config_test_scheme_node->children; iter; iter = iter->next)
194
 
        {
195
 
                xmlChar* text;
196
 
                
197
 
                if (iter->type == XML_ELEMENT_NODE &&
198
 
                                g_ascii_strncasecmp ((const gchar *) iter->name, 
199
 
                                                     ACTION_SCHEMES_TYPE,
200
 
                                                     strlen (ACTION_SCHEMES_TYPE)) == 0)
201
 
                {
202
 
                        text = xmlNodeGetContent (iter);
203
 
                        (*test_scheme) = g_slist_append ((*test_scheme), xmlStrdup (text));
204
 
                        xmlFree (text);
205
 
                        retv = TRUE;
206
 
                }
207
 
        }
208
 
 
209
 
        return retv;
210
 
}
211
 
 
212
 
static gboolean nautilus_actions_config_xml_parse_boolean (const gchar* value2parse, gboolean *value2set)
213
 
{
214
 
        gboolean retv = FALSE;
215
 
 
216
 
        if (g_ascii_strncasecmp (value2parse, "true", strlen ("true")) == 0)
217
 
        {
218
 
                (*value2set) = TRUE;
219
 
                retv = TRUE;
220
 
        }
221
 
        else if (g_ascii_strncasecmp (value2parse, "false", strlen ("false")) == 0)
222
 
        {
223
 
                (*value2set) = FALSE;
224
 
                retv = TRUE;
225
 
        }
226
 
 
227
 
        return retv;
228
 
}
229
 
 
230
 
static gboolean nautilus_actions_config_xml_action_fill_test (NautilusActionsConfigAction *action, xmlNode* config_test_node)
231
 
{
232
 
        xmlNode *iter;
233
 
        gboolean retv = FALSE;
234
 
        gboolean basename_ok = FALSE;
235
 
        gboolean isfile_ok = FALSE;
236
 
        gboolean isdir_ok = FALSE;
237
 
        gboolean scheme_ok = FALSE;
238
 
        gboolean accept_multiple_file_ok = FALSE;
239
 
 
240
 
        for (iter = config_test_node->children; iter; iter = iter->next)
241
 
        {
242
 
                xmlChar* text;
243
 
                
244
 
                if (!basename_ok && iter->type == XML_ELEMENT_NODE &&
245
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
246
 
                                                                ACTION_BASENAMES,
247
 
                                                                strlen (ACTION_BASENAMES)) == 0)
248
 
                {
249
 
                        basename_ok = nautilus_actions_config_xml_action_fill_test_basenames (&(action->basenames), iter, action->version);
250
 
                }
251
 
                else if (!isfile_ok && iter->type == XML_ELEMENT_NODE &&
252
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
253
 
                                                                ACTION_ISFILE,
254
 
                                                                strlen (ACTION_ISFILE)) == 0)
255
 
                {
256
 
                        text = xmlNodeGetContent (iter);
257
 
                        isfile_ok = nautilus_actions_config_xml_parse_boolean ((char*)text, &(action->is_file));
258
 
                        xmlFree (text);
259
 
                }
260
 
                else if (!isdir_ok && iter->type == XML_ELEMENT_NODE &&
261
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
262
 
                                                                ACTION_ISDIR,
263
 
                                                                strlen (ACTION_ISDIR)) == 0)
264
 
                {
265
 
                        text = xmlNodeGetContent (iter);
266
 
                        isdir_ok = nautilus_actions_config_xml_parse_boolean ((char*)text, &(action->is_dir));
267
 
                        xmlFree (text);
268
 
                }
269
 
                else if (!accept_multiple_file_ok && iter->type == XML_ELEMENT_NODE &&
270
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
271
 
                                                                ACTION_MULTIPLE,
272
 
                                                                strlen (ACTION_MULTIPLE)) == 0)
273
 
                {
274
 
                        text = xmlNodeGetContent (iter);
275
 
                        accept_multiple_file_ok = nautilus_actions_config_xml_parse_boolean ((char*)text, &(action->accept_multiple_files));
276
 
                        xmlFree (text);
277
 
                }
278
 
                else if (!scheme_ok && iter->type == XML_ELEMENT_NODE &&
279
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
280
 
                                                                ACTION_SCHEMES,
281
 
                                                                strlen (ACTION_SCHEMES)) == 0)
282
 
                {
283
 
                        scheme_ok = nautilus_actions_config_xml_action_fill_test_scheme (&(action->schemes), iter);
284
 
                }
285
 
        }
286
 
        
287
 
        
288
 
        if (basename_ok && isfile_ok && isdir_ok && accept_multiple_file_ok && scheme_ok)
289
 
        {
290
 
                //--> manage backward compatibility
291
 
                action->match_case = TRUE;
292
 
                action->mimetypes = g_slist_append (action->mimetypes, g_strdup ("*/*"));
293
 
                retv = TRUE;
294
 
        }
295
 
 
296
 
        return retv;
297
 
}
298
 
 
299
 
static gboolean nautilus_actions_config_xml_action_fill_command (NautilusActionsConfigAction *action, xmlNode* config_command_node)
300
 
{
301
 
        xmlNode *iter;
302
 
        gboolean retv = FALSE;
303
 
        gboolean path_ok = FALSE;
304
 
        gboolean parameters_ok = FALSE;
305
 
 
306
 
        for (iter = config_command_node->children; iter; iter = iter->next)
307
 
        {
308
 
                xmlChar* text;
309
 
                
310
 
                if (!path_ok && iter->type == XML_ELEMENT_NODE &&
311
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
312
 
                                                                ACTION_PATH,
313
 
                                                                strlen (ACTION_PATH)) == 0)
314
 
                {
315
 
                        text = xmlNodeGetContent (iter);
316
 
                        action->path = (char*)xmlStrdup (text);
317
 
                        xmlFree (text);
318
 
                        path_ok = TRUE;
319
 
                }
320
 
                else if (!parameters_ok && iter->type == XML_ELEMENT_NODE &&
321
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
322
 
                                                                ACTION_PARAMS,
323
 
                                                                strlen (ACTION_PARAMS)) == 0)
324
 
                {
325
 
                        text = xmlNodeGetContent (iter);
326
 
                        action->parameters = (char*)xmlStrdup (text);
327
 
                        xmlFree (text);
328
 
                        parameters_ok = TRUE;
329
 
                }
330
 
        }
331
 
 
332
 
        if (path_ok && parameters_ok)
333
 
        {
334
 
                retv = TRUE;
335
 
        }
336
 
 
337
 
        return retv;
338
 
}
339
 
 
340
 
static gboolean nautilus_actions_config_xml_action_fill_menu_item (NautilusActionsConfigAction *action, xmlNode* config_menu_item_node)
341
 
{
342
 
        xmlNode *iter;
343
 
        gboolean retv = FALSE;
344
 
        gboolean label_ok = FALSE;
345
 
        gboolean label_lang_ok = FALSE;
346
 
        gboolean tooltip_ok = FALSE;
347
 
        gboolean tooltip_lang_ok = FALSE;
348
 
        gchar* lang = g_strdup (g_getenv ("LANG"));
349
 
        xmlChar* xmlLang;
350
 
 
351
 
        for (iter = config_menu_item_node->children; iter; iter = iter->next)
352
 
        {
353
 
                xmlChar* text;
354
 
                
355
 
                if (iter->type == XML_ELEMENT_NODE &&
356
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
357
 
                                                                ACTION_LABEL,
358
 
                                                                strlen (ACTION_LABEL)) == 0)
359
 
                {
360
 
                        xmlLang = xmlGetProp (iter, BAD_CAST ACTION_LANG);
361
 
                        text = xmlNodeGetContent (iter);
362
 
                        if (lang == NULL && xmlLang == NULL)
363
 
                        {
364
 
                                //--> No $LANG set, get the default one (no xml:lang)
365
 
                                action->label = (char*)xmlStrdup (text);
366
 
                                label_ok = TRUE;
367
 
                                label_lang_ok = TRUE;
368
 
                        }
369
 
                        else if (lang != NULL && xmlLang == NULL)
370
 
                        {
371
 
                                if (!label_lang_ok)
372
 
                                {
373
 
                                        //--> $LANG set, not found the good xml:lang yet, get the default one (no xml:lang)
374
 
                                        action->label = (char*)xmlStrdup (text);
375
 
                                        label_ok = TRUE;
376
 
                                }
377
 
                        }
378
 
                        else if (lang != NULL && (xmlLang != NULL && g_ascii_strncasecmp ((gchar*)xmlLang, lang, xmlStrlen (xmlLang)) == 0))
379
 
                        { 
380
 
                                //--> $LANG set, found the good xml:lang, free the default one if any and set the good one instead
381
 
                                if (action->label != NULL)
382
 
                                {
383
 
                                        g_free (action->label);
384
 
                                }
385
 
                                action->label = (char*)xmlStrdup (text);
386
 
                                label_ok = TRUE;
387
 
                                label_lang_ok = TRUE;
388
 
                        }
389
 
                        xmlFree (text);
390
 
                        xmlFree (xmlLang);
391
 
                }
392
 
                else if (iter->type == XML_ELEMENT_NODE &&
393
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
394
 
                                                                ACTION_TOOLTIP,
395
 
                                                                strlen (ACTION_TOOLTIP)) == 0)
396
 
                {
397
 
                        xmlLang = xmlGetProp (iter, BAD_CAST ACTION_LANG);
398
 
                        text = xmlNodeGetContent (iter);
399
 
                        if (lang == NULL && xmlLang == NULL)
400
 
                        {
401
 
                                //--> No $LANG set, get the default one (no xml:lang)
402
 
                                action->tooltip = (char*)xmlStrdup (text);
403
 
                                tooltip_ok = TRUE;
404
 
                                tooltip_lang_ok = TRUE;
405
 
                        }
406
 
                        else if (lang != NULL && xmlLang == NULL)
407
 
                        {
408
 
                                if (!tooltip_lang_ok)
409
 
                                {
410
 
                                        //--> $LANG set, not found the good xml:lang yet, get the default one (no xml:lang)
411
 
                                        action->tooltip = (char*)xmlStrdup (text);
412
 
                                        tooltip_ok = TRUE;
413
 
                                }
414
 
                        }
415
 
                        else if (lang != NULL && (xmlLang != NULL && g_ascii_strncasecmp ((gchar*)xmlLang, lang, xmlStrlen (xmlLang)) == 0))
416
 
                        { 
417
 
                                //--> $LANG set, found the good xml:lang, free the default one if any and set the good one instead
418
 
                                if (action->tooltip != NULL)
419
 
                                {
420
 
                                        g_free (action->tooltip);
421
 
                                }
422
 
                                action->tooltip = (char*)xmlStrdup (text);
423
 
                                tooltip_ok = TRUE;
424
 
                                tooltip_lang_ok = TRUE;
425
 
                        }
426
 
                        xmlFree (text);
427
 
                        xmlFree (xmlLang);
428
 
                }
429
 
        }
430
 
 
431
 
        if (label_ok && tooltip_ok)
432
 
        {
433
 
                action->icon = g_strdup ("");
434
 
                retv = TRUE;
435
 
        }
436
 
 
437
 
        g_free (lang);
438
 
 
439
 
        return retv;
440
 
}
441
 
 
442
 
static gboolean nautilus_actions_config_xml_action_fill (NautilusActionsConfigAction* action, xmlNode* config_node, GError** error)
443
 
{
444
 
        xmlNode *iter;
445
 
        gboolean retv = FALSE;
446
 
        gboolean test_ok = FALSE;
447
 
        gboolean command_ok = FALSE;
448
 
        gboolean menu_item_ok = FALSE;
449
 
        
450
 
        for (iter = config_node->children; iter; iter = iter->next)
451
 
        {
452
 
                if (!test_ok && iter->type == XML_ELEMENT_NODE &&
453
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
454
 
                                                                                        ACTION_TEST,
455
 
                                                                                        strlen (ACTION_TEST)) == 0)
456
 
                {
457
 
                        test_ok = nautilus_actions_config_xml_action_fill_test (action, iter);
458
 
                }
459
 
                else if (!command_ok && iter->type == XML_ELEMENT_NODE &&
460
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
461
 
                                                                                        ACTION_COMMAND,
462
 
                                                                                        strlen (ACTION_COMMAND)) == 0)
463
 
                {
464
 
                        command_ok = nautilus_actions_config_xml_action_fill_command (action, iter);
465
 
                }
466
 
                else if (!menu_item_ok && iter->type == XML_ELEMENT_NODE &&
467
 
                                g_ascii_strncasecmp ((gchar*)iter->name, 
468
 
                                                                                        ACTION_MENU_ITEM,
469
 
                                                                                        strlen (ACTION_MENU_ITEM)) == 0)
470
 
                {
471
 
                        menu_item_ok = nautilus_actions_config_xml_action_fill_menu_item (action, iter);
472
 
                }
473
 
        }
474
 
 
475
 
        //--> not used in old config files, so init to an empty string :
476
 
        action->conf_section = g_strdup ("");
477
 
 
478
 
        if (test_ok && command_ok && menu_item_ok)
479
 
        {
480
 
                retv = TRUE;
481
 
        }
482
 
        else if (!test_ok)
483
 
        {
484
 
                // i18n notes: will be displayed in an error dialog
485
 
                g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, _("This XML file is not a valid Nautilus-actions config file (Bad test section)"));
486
 
 
487
 
        }
488
 
        else if (!command_ok)
489
 
        {
490
 
                // i18n notes: will be displayed in an error dialog
491
 
                g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, _("This XML file is not a valid Nautilus-actions config file (Bad command section)"));
492
 
        }
493
 
        else if (!menu_item_ok)
494
 
        {
495
 
                // i18n notes: will be displayed in an error dialog
496
 
                g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, _("This XML file is not a valid Nautilus-actions config file (Bad menu item section)"));
497
 
        }
498
 
 
499
 
        return retv;
500
 
}
501
 
 
502
 
gboolean
503
 
nautilus_actions_config_xml_parse_file (NautilusActionsConfigXml* config, const gchar* filename, GError** error)
504
 
{
505
 
        xmlDoc *doc = NULL;
506
 
        xmlNode *root_node;
507
 
        xmlNode *iter;
508
 
        xmlChar* version;
509
 
        NautilusActionsConfigAction *action;
510
 
        uuid_t uuid;
511
 
        gchar uuid_str[64];
512
 
        gboolean retv = FALSE;
513
 
        gboolean no_error = TRUE;
514
 
        xmlError* xml_error = NULL;
515
 
 
516
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
517
 
 
518
 
        doc = xmlParseFile (filename);
519
 
        if (doc != NULL)
520
 
        {
521
 
                root_node = xmlDocGetRootElement (doc);
522
 
                if (g_ascii_strncasecmp ((gchar*)root_node->name, 
523
 
                                                                                ACTION_ROOT, 
524
 
                                                                                strlen (ACTION_ROOT)) == 0)
525
 
                {
526
 
                        version = xmlGetProp (root_node, BAD_CAST ACTION_VERSION);
527
 
                        
528
 
                        iter = root_node->children;
529
 
                        while (iter && no_error)
530
 
                        {
531
 
                                xmlChar *config_name;
532
 
 
533
 
                                if (iter->type == XML_ELEMENT_NODE)
534
 
                                {
535
 
                                        if (g_ascii_strncasecmp ((gchar*)iter->name, 
536
 
                                                                                                ACTION_ACTION, 
537
 
                                                                                                strlen (ACTION_ACTION)) == 0)
538
 
                                        {
539
 
                                                config_name = xmlGetProp (iter, BAD_CAST ACTION_ACTION_NAME);
540
 
                                                if (config_name != NULL)
541
 
                                                {
542
 
                                                        action = nautilus_actions_config_action_new ();
543
 
                                                        action->version = (char*)xmlStrdup (version);
544
 
                                                        uuid_generate (uuid);
545
 
                                                        uuid_unparse (uuid, uuid_str);
546
 
                                                        action->uuid = g_strdup (uuid_str);
547
 
                                                        if (nautilus_actions_config_xml_action_fill (action, iter, error))
548
 
                                                        {
549
 
                                                                g_hash_table_insert (NAUTILUS_ACTIONS_CONFIG (config)->actions, g_strdup (action->uuid), action);
550
 
                                                                retv = TRUE;
551
 
                                                        }
552
 
                                                        else
553
 
                                                        {
554
 
                                                                nautilus_actions_config_action_free (action);
555
 
                                                                no_error = FALSE;
556
 
                                                        }
557
 
                                                        xmlFree (config_name);
558
 
                                                }
559
 
                                        }
560
 
               else
561
 
               {
562
 
                  // i18n notes: will be displayed in an error dialog
563
 
                  g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, _("This XML file is not a valid Nautilus-actions config file (found <%s> element instead of <%s>)"), (gchar*)iter->name, ACTION_ACTION);
564
 
                  no_error = FALSE;
565
 
               }
566
 
                                }
567
 
                                iter = iter->next;
568
 
                        }
569
 
                        xmlFree (version);
570
 
                }
571
 
      else
572
 
      {
573
 
         // i18n notes: will be displayed in an error dialog
574
 
         g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, _("This XML file is not a valid Nautilus-actions config file (root node is <%s> instead of <%s>)"), (gchar*)iter->name, ACTION_ROOT);
575
 
      }
576
 
 
577
 
 
578
 
                xmlFreeDoc(doc);
579
 
        }
580
 
   else
581
 
   {
582
 
      xml_error = xmlGetLastError ();
583
 
      g_set_error (error, NAUTILUS_ACTIONS_XML_ERROR, NAUTILUS_ACTIONS_XML_ERROR_FAILED, "%s", xml_error->message);
584
 
      xmlResetError ((xmlErrorPtr)xml_error);
585
 
   }
586
 
 
587
 
        xmlCleanupParser();
588
 
 
589
 
        return retv;
590
 
}
591
 
 
592
 
void nautilus_actions_config_xml_load_list (NautilusActionsConfigXml* config)
593
 
{
594
 
        GList* config_files = NULL;
595
 
        GList* iter;
596
 
 
597
 
        config_files = nautilus_actions_config_xml_get_config_files ();
598
 
 
599
 
        for (iter = config_files; iter; iter = iter->next)
600
 
        {
601
 
                gchar* filename = (gchar*)iter->data;
602
 
                nautilus_actions_config_xml_parse_file (config, filename, NULL);
603
 
        }
604
 
 
605
 
        nautilus_actions_config_xml_free_config_files (config_files);
606
 
}
607
 
 
608
 
static void
609
 
nautilus_actions_config_xml_finalize (GObject *object)
610
 
{
611
 
        NautilusActionsConfig *config = (NautilusActionsConfig *) object;
612
 
 
613
 
        g_return_if_fail (NAUTILUS_ACTIONS_IS_CONFIG (config));
614
 
 
615
 
        /* chain call to parent class */
616
 
        if (parent_class->finalize)
617
 
        {
618
 
                parent_class->finalize (object);
619
 
        }
620
 
}
621
 
 
622
 
static void
623
 
nautilus_actions_config_xml_class_init (NautilusActionsConfigXmlClass *klass)
624
 
{
625
 
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
626
 
 
627
 
        parent_class = g_type_class_peek_parent (klass);
628
 
 
629
 
        object_class->finalize = nautilus_actions_config_xml_finalize;
630
 
        
631
 
        NAUTILUS_ACTIONS_CONFIG_CLASS (klass)->save_action = save_action;
632
 
        NAUTILUS_ACTIONS_CONFIG_CLASS (klass)->remove_action = remove_action;
633
 
}
634
 
 
635
 
static void
636
 
nautilus_actions_config_xml_init (NautilusActionsConfig *config, NautilusActionsConfigClass *klass)
637
 
{
638
 
}
639
 
 
640
 
GType
641
 
nautilus_actions_config_xml_get_type (void)
642
 
{
643
 
   static GType type = 0;
644
 
 
645
 
        if (type == 0) {
646
 
                static GTypeInfo info = {
647
 
                                        sizeof (NautilusActionsConfigXmlClass),
648
 
                                        (GBaseInitFunc) NULL,
649
 
                                        (GBaseFinalizeFunc) NULL,
650
 
                                        (GClassInitFunc) nautilus_actions_config_xml_class_init,
651
 
                                        NULL, NULL,
652
 
                                        sizeof (NautilusActionsConfigXml),
653
 
                                        0,
654
 
                                        (GInstanceInitFunc) nautilus_actions_config_xml_init
655
 
                };
656
 
                type = g_type_register_static (NAUTILUS_ACTIONS_TYPE_CONFIG, "NautilusActionsConfigXml", &info, 0);
657
 
        }
658
 
        return type;
659
 
}
660
 
 
661
 
NautilusActionsConfigXml *
662
 
nautilus_actions_config_xml_get (void)
663
 
{
664
 
        static NautilusActionsConfigXml *config = NULL;
665
 
 
666
 
        /* we share one NautilusActionsConfigXml object for all */
667
 
        if (!config) {
668
 
                config = g_object_new (NAUTILUS_ACTIONS_TYPE_CONFIG_XML, NULL);
669
 
                return config;
670
 
        }
671
 
 
672
 
        return NAUTILUS_ACTIONS_CONFIG_XML (g_object_ref (G_OBJECT (config)));
673
 
}
674
 
 
675
 
// vim:ts=3:sw=3:tw=1024:cin