Line data Source code
1 : /*
2 : * gnc-budget-xml-v2.c -- budget xml i/o implementation
3 : *
4 : * Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
5 : *
6 : * This program is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU General Public License as
8 : * published by the Free Software Foundation; either version 2 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, contact:
18 : *
19 : * Free Software Foundation Voice: +1-617-542-5942
20 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21 : * Boston, MA 02110-1301, USA gnu@gnu.org
22 : */
23 : #include <glib.h>
24 :
25 : #include <config.h>
26 : #include <stdlib.h>
27 : #include <string.h>
28 :
29 : #include "gnc-xml-helper.h"
30 : #include "sixtp.h"
31 : #include "sixtp-utils.h"
32 : #include "sixtp-parsers.h"
33 : #include "sixtp-utils.h"
34 : #include "sixtp-dom-parsers.h"
35 : #include "sixtp-dom-generators.h"
36 :
37 : #include "gnc-xml.h"
38 : #include "io-gncxml-gen.h"
39 : #include "io-gncxml-v2.h"
40 :
41 : static QofLogModule log_module = GNC_MOD_IO;
42 :
43 : const gchar* budget_version_string = "2.0.0";
44 :
45 : /* ids */
46 : #define gnc_budget_string "gnc:budget"
47 : #define bgt_id_string "bgt:id"
48 : #define bgt_name_string "bgt:name"
49 : #define bgt_description_string "bgt:description"
50 : #define bgt_num_periods_string "bgt:num-periods"
51 : #define bgt_recurrence_string "bgt:recurrence"
52 : #define bgt_slots_string "bgt:slots"
53 :
54 : xmlNodePtr
55 0 : gnc_budget_dom_tree_create (GncBudget* bgt)
56 : {
57 : xmlNodePtr ret;
58 :
59 0 : ENTER ("(budget=%p)", bgt);
60 :
61 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_budget_string);
62 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST budget_version_string);
63 :
64 : /* field: GncGUID */
65 0 : xmlAddChild (ret, guid_to_dom_tree (bgt_id_string,
66 : gnc_budget_get_guid (bgt)));
67 : /* field: char* name */
68 0 : xmlAddChild (ret, text_to_dom_tree (bgt_name_string,
69 : gnc_budget_get_name (bgt)));
70 : /* field: char* description */
71 0 : xmlAddChild (ret, text_to_dom_tree (bgt_description_string,
72 : gnc_budget_get_description (bgt)));
73 : /* field: guint num_periods */
74 0 : xmlAddChild (ret, guint_to_dom_tree (bgt_num_periods_string,
75 : gnc_budget_get_num_periods (bgt)));
76 : /* field: Recurrence* */
77 0 : xmlAddChild (ret, recurrence_to_dom_tree (bgt_recurrence_string,
78 : gnc_budget_get_recurrence (bgt)));
79 : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
80 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (bgt_slots_string,
81 0 : QOF_INSTANCE (bgt)));
82 :
83 0 : LEAVE (" ");
84 0 : return ret;
85 : }
86 :
87 : /***********************************************************************/
88 :
89 : static gboolean
90 1 : budget_id_handler (xmlNodePtr node, gpointer bgt)
91 : {
92 1 : auto guid = dom_tree_to_guid (node);
93 1 : g_return_val_if_fail (guid, FALSE);
94 1 : qof_instance_set_guid (QOF_INSTANCE (bgt), &*guid);
95 1 : return TRUE;
96 : }
97 :
98 : static gboolean
99 1 : budget_name_handler (xmlNodePtr node, gpointer bgt)
100 : {
101 1 : return apply_xmlnode_text (gnc_budget_set_name, GNC_BUDGET (bgt), node);
102 : }
103 :
104 : static gboolean
105 1 : budget_description_handler (xmlNodePtr node, gpointer bgt)
106 : {
107 1 : return apply_xmlnode_text (gnc_budget_set_description, GNC_BUDGET (bgt), node);
108 : }
109 :
110 : static gboolean
111 1 : budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
112 : {
113 : guint num_periods;
114 :
115 1 : if (dom_tree_to_guint (node, &num_periods))
116 : {
117 1 : gnc_budget_set_num_periods (GNC_BUDGET (bgt), num_periods);
118 1 : return TRUE;
119 : }
120 : else
121 0 : return FALSE;
122 : }
123 :
124 : static gboolean
125 1 : budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
126 : {
127 : Recurrence* r;
128 :
129 1 : if ((r = dom_tree_to_recurrence (node)) == NULL)
130 0 : return FALSE;
131 :
132 1 : gnc_budget_set_recurrence (GNC_BUDGET (bgt), r);
133 1 : g_free (r);
134 1 : return TRUE;
135 : }
136 :
137 : static gboolean
138 0 : budget_slots_handler (xmlNodePtr node, gpointer bgt)
139 : {
140 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (bgt));
141 : }
142 :
143 : static struct dom_tree_handler budget_handlers[] =
144 : {
145 : { bgt_id_string, budget_id_handler, 1, 0 },
146 : { bgt_name_string, budget_name_handler, 0, 0 },
147 : { bgt_description_string, budget_description_handler, 0, 0 },
148 : { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
149 : { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
150 : { bgt_slots_string, budget_slots_handler, 0, 0},
151 : { NULL, 0, 0, 0 }
152 : };
153 :
154 : static gboolean
155 10 : gnc_budget_end_handler (gpointer data_for_children,
156 : GSList* data_from_children, GSList* sibling_data,
157 : gpointer parent_data, gpointer global_data,
158 : gpointer* result, const gchar* tag)
159 : {
160 : GncBudget* bgt;
161 10 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
162 10 : gxpf_data* gdata = (gxpf_data*)global_data;
163 10 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
164 :
165 10 : if (parent_data)
166 : {
167 9 : return TRUE;
168 : }
169 :
170 : /* OK. For some messed up reason this is getting called again with a
171 : NULL tag. So we ignore those cases */
172 1 : if (!tag)
173 : {
174 0 : return TRUE;
175 : }
176 :
177 1 : g_return_val_if_fail (tree, FALSE);
178 :
179 1 : bgt = dom_tree_to_budget (tree, book);
180 1 : xmlFreeNode (tree);
181 1 : if (bgt != NULL)
182 : {
183 : /* ends up calling book_callback */
184 1 : gdata->cb (tag, gdata->parsedata, bgt);
185 : }
186 :
187 1 : return bgt != NULL;
188 : }
189 :
190 :
191 : GncBudget*
192 1 : dom_tree_to_budget (xmlNodePtr node, QofBook* book)
193 : {
194 : GncBudget* bgt;
195 :
196 1 : bgt = gnc_budget_new (book);
197 1 : if (!dom_tree_generic_parse (node, budget_handlers, bgt))
198 : {
199 0 : PERR ("failed to parse budget tree");
200 0 : gnc_budget_destroy (bgt);
201 0 : bgt = NULL;
202 : }
203 1 : return bgt;
204 : }
205 :
206 : sixtp*
207 22 : gnc_budget_sixtp_parser_create (void)
208 : {
209 22 : return sixtp_dom_parser_new (gnc_budget_end_handler, NULL, NULL);
210 : }
211 : /* ====================== END OF FILE ===================*/
|