Added About Dialog
[demorecorder] / src / XmlHelpers.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18
19 namespace IdWorks {
20
21 namespace XmlHelpers {
22   
23   public static bool parse_project_settings_file(string location, string version, ref SettingsStructures.ProjectSettings project, ref string errors) {
24     bool ret = false;
25     project = SettingsStructures.ProjectSettings();
26     errors = "";
27     // prepare the Xml Parser
28     Xml.Parser.init();
29     // load the document
30     Xml.Doc* doc = Xml.Parser.parse_file(location);
31     if (null != doc) {
32       // load the root node
33       Xml.Node* root = doc->get_root_element();
34       if (null != root) {
35         // check element is ProjectSettings
36         if ("ProjectSettings" == root->name) {
37           // check version
38           string? doc_version = get_node_attribute(root, "version");
39           if (null != doc_version && version == doc_version) {
40             // looking good let's get parsing.
41             try {
42               project.from_xml_node(root);
43               ret = true;
44             }
45             catch (GLib.Error ex) {
46               errors = "ProjectSettings parser threw an error.";
47               ret = false;
48             }
49             finally {
50               // no op
51             }
52           }
53           else {
54             errors = "Incorrect version or unversioned document.";
55           }
56         }
57         else {
58           errors = "Not a valid settings file.";
59         }
60       }
61       else {
62         errors = "No root node found.";
63       }
64       // clean up
65       delete doc;
66     }
67     else {
68       errors = "Couldn't parse file as valid XML.";
69     }
70     // clean up the parser
71     Xml.Parser.cleanup();
72     
73     return ret;
74   }
75   
76   private static string? get_node_attribute(Xml.Node* node, string key) {
77     string? ret = null;
78     // Loop over the passed node's attributes
79     for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next) {
80       string attr_name = prop->name;
81       // Notice the ->children which points to a Node*
82       // (Attr doesn't feature content)
83       string attr_content = prop->children->content;
84       if (key == attr_name) {
85         ret = attr_content;
86         break;
87       }
88     }
89     return ret;
90   }
91 }
92
93 }