31 #include "gnc-xml-helper.h"
34 #include "sixtp-utils.h"
35 #include "sixtp-parsers.h"
36 #include "sixtp-utils.h"
37 #include "sixtp-dom-parsers.h"
38 #include "sixtp-dom-generators.h"
41 #include "io-gncxml-gen.h"
44 #include "gncOrderP.h"
46 #include "gnc-order-xml-v2.h"
47 #include "gnc-owner-xml-v2.h"
49 #define _GNC_MOD_NAME GNC_ID_ORDER
53 const gchar *order_version_string =
"2.0.0";
56 #define gnc_order_string "gnc:GncOrder"
57 #define order_guid_string "order:guid"
58 #define order_id_string "order:id"
59 #define order_owner_string "order:owner"
60 #define order_opened_string "order:opened"
61 #define order_closed_string "order:closed"
62 #define order_notes_string "order:notes"
63 #define order_reference_string "order:reference"
64 #define order_active_string "order:active"
65 #define order_slots_string "order:slots"
71 maybe_add_string (xmlNodePtr ptr,
const char *tag,
const char *str)
73 if (str && strlen(str) > 0)
74 xmlAddChild (ptr, text_to_dom_tree (tag, str));
78 order_dom_tree_create (
GncOrder *order)
84 ret = xmlNewNode(NULL, BAD_CAST gnc_order_string);
85 xmlSetProp(ret, BAD_CAST
"version", BAD_CAST order_version_string);
87 xmlAddChild(ret, guid_to_dom_tree(order_guid_string,
90 xmlAddChild(ret, text_to_dom_tree(order_id_string,
91 gncOrderGetID (order)));
93 xmlAddChild(ret, gnc_owner_to_dom_tree (order_owner_string,
94 gncOrderGetOwner (order)));
96 ts = gncOrderGetDateOpened (order);
97 xmlAddChild(ret, timespec_to_dom_tree (order_opened_string, &ts));
99 ts = gncOrderGetDateClosed (order);
100 if (ts.tv_sec || ts.tv_nsec)
101 xmlAddChild(ret, timespec_to_dom_tree (order_closed_string, &ts));
103 maybe_add_string (ret, order_notes_string, gncOrderGetNotes (order));
104 maybe_add_string (ret, order_reference_string, gncOrderGetReference (order));
106 xmlAddChild(ret, int_to_dom_tree(order_active_string,
107 gncOrderGetActive (order)));
109 kf = qof_instance_get_slots (QOF_INSTANCE(order));
112 xmlNodePtr kvpnode = kvp_frame_to_dom_tree(order_slots_string, kf);
115 xmlAddChild(ret, kvpnode);
130 static inline gboolean
131 set_string(xmlNodePtr node,
GncOrder* order,
132 void (*func)(
GncOrder *order,
const char *txt))
134 char* txt = dom_tree_to_text(node);
135 g_return_val_if_fail(txt, FALSE);
143 static inline gboolean
144 set_timespec(xmlNodePtr node,
GncOrder* order,
147 Timespec ts = dom_tree_to_timespec(node);
148 if (!dom_tree_valid_timespec(&ts, node->name))
return FALSE;
155 order_guid_handler (xmlNodePtr node, gpointer
order_pdata)
157 struct order_pdata *pdata = order_pdata;
161 guid = dom_tree_to_guid(node);
162 g_return_val_if_fail (guid, FALSE);
163 order = gncOrderLookup (pdata->book, guid);
166 gncOrderDestroy (pdata->order);
167 pdata->order = order;
168 gncOrderBeginEdit (order);
172 gncOrderSetGUID(pdata->order, guid);
181 order_id_handler (xmlNodePtr node, gpointer order_pdata)
183 struct order_pdata *pdata = order_pdata;
185 return set_string(node, pdata->order, gncOrderSetID);
189 order_owner_handler (xmlNodePtr node, gpointer order_pdata)
191 struct order_pdata *pdata = order_pdata;
195 ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
197 gncOrderSetOwner (pdata->order, &owner);
203 order_opened_handler (xmlNodePtr node, gpointer order_pdata)
205 struct order_pdata *pdata = order_pdata;
207 return set_timespec (node, pdata->order, gncOrderSetDateOpened);
211 order_closed_handler (xmlNodePtr node, gpointer order_pdata)
213 struct order_pdata *pdata = order_pdata;
215 return set_timespec (node, pdata->order, gncOrderSetDateClosed);
219 order_notes_handler (xmlNodePtr node, gpointer order_pdata)
221 struct order_pdata *pdata = order_pdata;
223 return set_string(node, pdata->order, gncOrderSetNotes);
227 order_reference_handler (xmlNodePtr node, gpointer order_pdata)
229 struct order_pdata *pdata = order_pdata;
231 return set_string(node, pdata->order, gncOrderSetReference);
235 order_active_handler (xmlNodePtr node, gpointer order_pdata)
237 struct order_pdata *pdata = order_pdata;
241 ret = dom_tree_to_integer(node, &val);
243 gncOrderSetActive(pdata->order, (gboolean)val);
249 order_slots_handler (xmlNodePtr node, gpointer order_pdata)
251 struct order_pdata *pdata = order_pdata;
253 return dom_tree_to_kvp_frame_given
254 (node, qof_instance_get_slots (QOF_INSTANCE (pdata->order)));
259 { order_guid_string, order_guid_handler, 1, 0 },
260 { order_id_string, order_id_handler, 1, 0 },
261 { order_owner_string, order_owner_handler, 1, 0 },
262 { order_opened_string, order_opened_handler, 1, 0 },
263 { order_closed_string, order_closed_handler, 0, 0 },
264 { order_notes_string, order_notes_handler, 0, 0 },
265 { order_reference_string, order_reference_handler, 0, 0 },
266 { order_active_string, order_active_handler, 1, 0 },
267 { order_slots_string, order_slots_handler, 0, 0 },
272 dom_tree_to_order (xmlNodePtr node,
QofBook *book)
274 struct order_pdata order_pdata;
277 order_pdata.order = gncOrderCreate(book);
278 order_pdata.book = book;
279 gncOrderBeginEdit (order_pdata.order);
281 successful = dom_tree_generic_parse (node, order_handlers_v2,
285 gncOrderCommitEdit (order_pdata.order);
288 PERR (
"failed to parse order tree");
289 gncOrderDestroy (order_pdata.order);
290 order_pdata.order = NULL;
293 return order_pdata.order;
297 gnc_order_end_handler(gpointer data_for_children,
298 GSList* data_from_children, GSList* sibling_data,
299 gpointer parent_data, gpointer global_data,
300 gpointer *result,
const gchar *tag)
303 xmlNodePtr tree = (xmlNodePtr)data_for_children;
305 QofBook *book = gdata->bookdata;
319 g_return_val_if_fail(tree, FALSE);
321 order = dom_tree_to_order(tree, book);
324 gdata->cb(tag, gdata->parsedata, order);
329 return order != NULL;
333 order_sixtp_parser_create(
void)
335 return sixtp_dom_parser_new(gnc_order_end_handler, NULL, NULL);
339 order_should_be_saved (
GncOrder *order)
344 id = gncOrderGetID (order);
345 if (
id == NULL || *
id ==
'\0')
354 int *count = count_p;
355 if (order_should_be_saved ((
GncOrder *) order_p))
360 order_get_count (
QofBook *book)
368 xml_add_order (
QofInstance * order_p, gpointer out_p)
376 if (!order_should_be_saved (order))
379 node = order_dom_tree_create (order);
380 xmlElemDump(out, NULL, node);
382 if (ferror(out) || fprintf(out,
"\n") < 0)
387 order_write (FILE *out,
QofBook *book)
390 return ferror(out) == 0;
396 g_return_val_if_fail(out, FALSE);
401 gnc_order_xml_initialize (
void)
405 GNC_FILE_BACKEND_VERS,
407 order_sixtp_parser_create,
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const GncGUID * qof_instance_get_guid(gconstpointer)
Use a 64-bit unsigned int timespec.
#define PERR(format, args...)
void qof_object_foreach_sorted(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
api for GnuCash version 2 XML-based file format
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
gboolean gnc_xml2_write_namespace_decl(FILE *out, const char *name_space)
struct KvpFrameImpl KvpFrame
const gchar * QofLogModule