Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-order-xml-v2.c -- order xml i/o implementation *
3 : : * *
4 : : * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
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 : : \********************************************************************/
24 : : #include <glib.h>
25 : :
26 : : #include <config.h>
27 : : #include <stdlib.h>
28 : : #include <string.h>
29 : : #include "gncOrderP.h"
30 : :
31 : : #include "gnc-xml-helper.h"
32 : : #include "sixtp.h"
33 : : #include "sixtp-utils.h"
34 : : #include "sixtp-parsers.h"
35 : : #include "sixtp-utils.h"
36 : : #include "sixtp-dom-parsers.h"
37 : : #include "sixtp-dom-generators.h"
38 : :
39 : : #include "gnc-xml.h"
40 : : #include "io-gncxml-gen.h"
41 : : #include "io-gncxml-v2.h"
42 : :
43 : : #include "gnc-order-xml-v2.h"
44 : : #include "gnc-owner-xml-v2.h"
45 : :
46 : : #define _GNC_MOD_NAME GNC_ID_ORDER
47 : :
48 : : static QofLogModule log_module = GNC_MOD_IO;
49 : :
50 : : const gchar* order_version_string = "2.0.0";
51 : :
52 : : /* ids */
53 : : #define gnc_order_string "gnc:GncOrder"
54 : : #define order_guid_string "order:guid"
55 : : #define order_id_string "order:id"
56 : : #define order_owner_string "order:owner"
57 : : #define order_opened_string "order:opened"
58 : : #define order_closed_string "order:closed"
59 : : #define order_notes_string "order:notes"
60 : : #define order_reference_string "order:reference"
61 : : #define order_active_string "order:active"
62 : : #define order_slots_string "order:slots"
63 : :
64 : : static void
65 : 0 : maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
66 : : {
67 : 0 : if (str && *str)
68 : 0 : xmlAddChild (ptr, text_to_dom_tree (tag, str));
69 : 0 : }
70 : :
71 : : static xmlNodePtr
72 : 0 : order_dom_tree_create (GncOrder* order)
73 : : {
74 : : xmlNodePtr ret;
75 : : time64 tt;
76 : :
77 : 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_order_string);
78 : 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST order_version_string);
79 : :
80 : 0 : xmlAddChild (ret, guid_to_dom_tree (order_guid_string,
81 : 0 : qof_instance_get_guid (QOF_INSTANCE (order))));
82 : :
83 : 0 : xmlAddChild (ret, text_to_dom_tree (order_id_string,
84 : : gncOrderGetID (order)));
85 : :
86 : 0 : xmlAddChild (ret, gnc_owner_to_dom_tree (order_owner_string,
87 : 0 : gncOrderGetOwner (order)));
88 : :
89 : 0 : tt = gncOrderGetDateOpened (order);
90 : 0 : xmlAddChild (ret, time64_to_dom_tree (order_opened_string, tt));
91 : :
92 : 0 : tt = gncOrderGetDateClosed (order);
93 : 0 : if (tt != INT64_MAX)
94 : 0 : xmlAddChild (ret, time64_to_dom_tree (order_closed_string, tt));
95 : :
96 : 0 : maybe_add_string (ret, order_notes_string, gncOrderGetNotes (order));
97 : 0 : maybe_add_string (ret, order_reference_string, gncOrderGetReference (order));
98 : :
99 : 0 : xmlAddChild (ret, int_to_dom_tree (order_active_string,
100 : 0 : gncOrderGetActive (order)));
101 : :
102 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
103 : 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (order_slots_string,
104 : 0 : QOF_INSTANCE (order)));
105 : :
106 : 0 : return ret;
107 : : }
108 : :
109 : : /***********************************************************************/
110 : :
111 : : struct order_pdata
112 : : {
113 : : GncOrder* order;
114 : : QofBook* book;
115 : : };
116 : :
117 : : static inline gboolean
118 : 0 : set_time64 (xmlNodePtr node, GncOrder* order,
119 : : void (*func) (GncOrder* order, time64 tt))
120 : : {
121 : 0 : time64 time = dom_tree_to_time64 (node);
122 : 0 : if (!dom_tree_valid_time64 (time, node->name)) time = 0;
123 : 0 : func (order, time);
124 : 0 : return TRUE;
125 : : }
126 : :
127 : : static gboolean
128 : 0 : order_guid_handler (xmlNodePtr node, gpointer order_pdata)
129 : : {
130 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
131 : : GncOrder* order;
132 : :
133 : 0 : auto guid = dom_tree_to_guid (node);
134 : 0 : g_return_val_if_fail (guid, FALSE);
135 : 0 : order = gncOrderLookup (pdata->book, &*guid);
136 : 0 : if (order)
137 : : {
138 : 0 : gncOrderDestroy (pdata->order);
139 : 0 : pdata->order = order;
140 : 0 : gncOrderBeginEdit (order);
141 : : }
142 : : else
143 : : {
144 : 0 : gncOrderSetGUID (pdata->order, &*guid);
145 : : }
146 : :
147 : 0 : return TRUE;
148 : : }
149 : :
150 : : static gboolean
151 : 0 : order_id_handler (xmlNodePtr node, gpointer order_pdata)
152 : : {
153 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
154 : :
155 : 0 : return apply_xmlnode_text (gncOrderSetID, pdata->order, node);
156 : : }
157 : :
158 : : static gboolean
159 : 0 : order_owner_handler (xmlNodePtr node, gpointer order_pdata)
160 : : {
161 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
162 : : GncOwner owner;
163 : : gboolean ret;
164 : :
165 : 0 : ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
166 : 0 : if (ret)
167 : 0 : gncOrderSetOwner (pdata->order, &owner);
168 : :
169 : 0 : return ret;
170 : : }
171 : :
172 : : static gboolean
173 : 0 : order_opened_handler (xmlNodePtr node, gpointer order_pdata)
174 : : {
175 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
176 : :
177 : 0 : return set_time64 (node, pdata->order, gncOrderSetDateOpened);
178 : : }
179 : :
180 : : static gboolean
181 : 0 : order_closed_handler (xmlNodePtr node, gpointer order_pdata)
182 : : {
183 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
184 : :
185 : 0 : return set_time64 (node, pdata->order, gncOrderSetDateClosed);
186 : : }
187 : :
188 : : static gboolean
189 : 0 : order_notes_handler (xmlNodePtr node, gpointer order_pdata)
190 : : {
191 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
192 : :
193 : 0 : return apply_xmlnode_text (gncOrderSetNotes, pdata->order, node);
194 : : }
195 : :
196 : : static gboolean
197 : 0 : order_reference_handler (xmlNodePtr node, gpointer order_pdata)
198 : : {
199 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
200 : :
201 : 0 : return apply_xmlnode_text (gncOrderSetReference, pdata->order, node);
202 : : }
203 : :
204 : : static gboolean
205 : 0 : order_active_handler (xmlNodePtr node, gpointer order_pdata)
206 : : {
207 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
208 : : gint64 val;
209 : : gboolean ret;
210 : :
211 : 0 : ret = dom_tree_to_integer (node, &val);
212 : 0 : if (ret)
213 : 0 : gncOrderSetActive (pdata->order, (gboolean)val);
214 : :
215 : 0 : return ret;
216 : : }
217 : :
218 : : static gboolean
219 : 0 : order_slots_handler (xmlNodePtr node, gpointer order_pdata)
220 : : {
221 : 0 : struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
222 : :
223 : 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->order));
224 : : }
225 : :
226 : : static struct dom_tree_handler order_handlers_v2[] =
227 : : {
228 : : { order_guid_string, order_guid_handler, 1, 0 },
229 : : { order_id_string, order_id_handler, 1, 0 },
230 : : { order_owner_string, order_owner_handler, 1, 0 },
231 : : { order_opened_string, order_opened_handler, 1, 0 },
232 : : { order_closed_string, order_closed_handler, 0, 0 },
233 : : { order_notes_string, order_notes_handler, 0, 0 },
234 : : { order_reference_string, order_reference_handler, 0, 0 },
235 : : { order_active_string, order_active_handler, 1, 0 },
236 : : { order_slots_string, order_slots_handler, 0, 0 },
237 : : { NULL, 0, 0, 0 }
238 : : };
239 : :
240 : : static GncOrder*
241 : 0 : dom_tree_to_order (xmlNodePtr node, QofBook* book)
242 : : {
243 : : struct order_pdata order_pdata;
244 : : gboolean successful;
245 : :
246 : 0 : order_pdata.order = gncOrderCreate (book);
247 : 0 : order_pdata.book = book;
248 : 0 : gncOrderBeginEdit (order_pdata.order);
249 : :
250 : 0 : successful = dom_tree_generic_parse (node, order_handlers_v2,
251 : : &order_pdata);
252 : :
253 : 0 : if (successful)
254 : 0 : gncOrderCommitEdit (order_pdata.order);
255 : : else
256 : : {
257 : 0 : PERR ("failed to parse order tree");
258 : 0 : gncOrderDestroy (order_pdata.order);
259 : 0 : order_pdata.order = NULL;
260 : : }
261 : :
262 : 0 : return order_pdata.order;
263 : : }
264 : :
265 : : static gboolean
266 : 0 : gnc_order_end_handler (gpointer data_for_children,
267 : : GSList* data_from_children, GSList* sibling_data,
268 : : gpointer parent_data, gpointer global_data,
269 : : gpointer* result, const gchar* tag)
270 : : {
271 : : GncOrder* order;
272 : 0 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
273 : 0 : gxpf_data* gdata = (gxpf_data*)global_data;
274 : 0 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
275 : :
276 : 0 : if (parent_data)
277 : : {
278 : 0 : return TRUE;
279 : : }
280 : :
281 : : /* OK. For some messed up reason this is getting called again with a
282 : : NULL tag. So we ignore those cases */
283 : 0 : if (!tag)
284 : : {
285 : 0 : return TRUE;
286 : : }
287 : :
288 : 0 : g_return_val_if_fail (tree, FALSE);
289 : :
290 : 0 : order = dom_tree_to_order (tree, book);
291 : 0 : if (order != NULL)
292 : : {
293 : 0 : gdata->cb (tag, gdata->parsedata, order);
294 : : }
295 : :
296 : 0 : xmlFreeNode (tree);
297 : :
298 : 0 : return order != NULL;
299 : : }
300 : :
301 : : static sixtp*
302 : 22 : order_sixtp_parser_create (void)
303 : : {
304 : 22 : return sixtp_dom_parser_new (gnc_order_end_handler, NULL, NULL);
305 : : }
306 : :
307 : : static gboolean
308 : 0 : order_should_be_saved (GncOrder* order)
309 : : {
310 : : const char* id;
311 : :
312 : : /* make sure this is a valid order before we save it -- should have an ID */
313 : 0 : id = gncOrderGetID (order);
314 : 0 : if (id == NULL || *id == '\0')
315 : 0 : return FALSE;
316 : :
317 : 0 : return TRUE;
318 : : }
319 : :
320 : : static void
321 : 0 : do_count (QofInstance* order_p, gpointer count_p)
322 : : {
323 : 0 : int* count = static_cast<decltype (count)> (count_p);
324 : 0 : if (order_should_be_saved ((GncOrder*) order_p))
325 : 0 : (*count)++;
326 : 0 : }
327 : :
328 : : static int
329 : 4 : order_get_count (QofBook* book)
330 : : {
331 : 4 : int count = 0;
332 : 4 : qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
333 : 4 : return count;
334 : : }
335 : :
336 : : static void
337 : 0 : xml_add_order (QofInstance* order_p, gpointer out_p)
338 : : {
339 : : xmlNodePtr node;
340 : 0 : GncOrder* order = (GncOrder*) order_p;
341 : 0 : FILE* out = static_cast<decltype (out)> (out_p);
342 : :
343 : 0 : if (ferror (out))
344 : 0 : return;
345 : 0 : if (!order_should_be_saved (order))
346 : 0 : return;
347 : :
348 : 0 : node = order_dom_tree_create (order);
349 : 0 : xmlElemDump (out, NULL, node);
350 : 0 : xmlFreeNode (node);
351 : 0 : if (ferror (out) || fprintf (out, "\n") < 0)
352 : 0 : return;
353 : : }
354 : :
355 : : static gboolean
356 : 4 : order_write (FILE* out, QofBook* book)
357 : : {
358 : 4 : qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_order, (gpointer) out);
359 : 4 : return ferror (out) == 0;
360 : : }
361 : :
362 : : static gboolean
363 : 4 : order_ns (FILE* out)
364 : : {
365 : 4 : g_return_val_if_fail (out, FALSE);
366 : 4 : return gnc_xml2_write_namespace_decl (out, "order");
367 : : }
368 : :
369 : : void
370 : 15 : gnc_order_xml_initialize (void)
371 : : {
372 : : static GncXmlDataType_t be_data =
373 : : {
374 : : GNC_FILE_BACKEND_VERS,
375 : : gnc_order_string,
376 : : order_sixtp_parser_create,
377 : : NULL, /* add_item */
378 : : order_get_count,
379 : : order_write,
380 : : NULL, /* scrub */
381 : : order_ns,
382 : : };
383 : :
384 : 15 : gnc_xml_register_backend(be_data);
385 : 15 : }
|