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 "gncBillTermP.h"
45 #include "gncVendorP.h"
46 #include "gncTaxTableP.h"
47 #include "gnc-vendor-xml-v2.h"
48 #include "gnc-address-xml-v2.h"
49 #include "xml-helpers.h"
50 #include "gnc-bill-term-xml-v2.h"
52 #define _GNC_MOD_NAME GNC_ID_VENDOR
56 const gchar *vendor_version_string =
"2.0.0";
59 #define gnc_vendor_string "gnc:GncVendor"
60 #define vendor_name_string "vendor:name"
61 #define vendor_guid_string "vendor:guid"
62 #define vendor_id_string "vendor:id"
63 #define vendor_addr_string "vendor:addr"
64 #define vendor_notes_string "vendor:notes"
65 #define vendor_terms_string "vendor:terms"
66 #define vendor_taxincluded_string "vendor:taxincluded"
67 #define vendor_active_string "vendor:active"
68 #define vendor_currency_string "vendor:currency"
69 #define vendor_taxtable_string "vendor:taxtable"
70 #define vendor_taxtableoverride_string "vendor:use-tt"
71 #define vendor_slots_string "vendor:slots"
74 vendor_dom_tree_create (
GncVendor *vendor)
76 xmlNodePtr ret, kvpnode;
80 ret = xmlNewNode(NULL, BAD_CAST gnc_vendor_string);
81 xmlSetProp(ret, BAD_CAST
"version", BAD_CAST vendor_version_string);
83 xmlAddChild(ret, guid_to_dom_tree(vendor_guid_string,
86 xmlAddChild(ret, text_to_dom_tree(vendor_name_string,
87 gncVendorGetName (vendor)));
89 xmlAddChild(ret, text_to_dom_tree(vendor_id_string,
90 gncVendorGetID (vendor)));
92 xmlAddChild(ret, gnc_address_to_dom_tree(vendor_addr_string,
93 gncVendorGetAddr (vendor)));
95 maybe_add_string (ret, vendor_notes_string, gncVendorGetNotes (vendor));
97 term = gncVendorGetTerms (vendor);
99 xmlAddChild(ret, guid_to_dom_tree(vendor_terms_string,
102 xmlAddChild(ret, text_to_dom_tree(vendor_taxincluded_string,
103 gncTaxIncludedTypeToString (
104 gncVendorGetTaxIncluded (vendor))));
106 xmlAddChild(ret, int_to_dom_tree(vendor_active_string,
107 gncVendorGetActive (vendor)));
111 commodity_ref_to_dom_tree(vendor_currency_string,
112 gncVendorGetCurrency (vendor)));
114 xmlAddChild (ret, int_to_dom_tree (vendor_taxtableoverride_string,
115 gncVendorGetTaxTableOverride (vendor)));
116 taxtable = gncVendorGetTaxTable (vendor);
118 xmlAddChild (ret, guid_to_dom_tree (vendor_taxtable_string,
121 kvpnode = kvp_frame_to_dom_tree (vendor_slots_string,
122 qof_instance_get_slots (QOF_INSTANCE(vendor)));
123 if (kvpnode) xmlAddChild (ret, kvpnode);
137 set_string(xmlNodePtr node,
GncVendor* vendor,
138 void (*func)(
GncVendor *vendor,
const char *txt))
140 char* txt = dom_tree_to_text(node);
141 g_return_val_if_fail(txt, FALSE);
151 set_boolean(xmlNodePtr node,
GncVendor* vendor,
152 void (*func)(
GncVendor* vendor, gboolean b))
157 ret = dom_tree_to_integer(node, &val);
159 func(vendor, (gboolean)val);
165 vendor_name_handler (xmlNodePtr node, gpointer
vendor_pdata)
167 struct vendor_pdata *pdata = vendor_pdata;
169 return set_string(node, pdata->vendor, gncVendorSetName);
173 vendor_guid_handler (xmlNodePtr node, gpointer
vendor_pdata)
175 struct vendor_pdata *pdata = vendor_pdata;
179 guid = dom_tree_to_guid(node);
180 g_return_val_if_fail(guid, FALSE);
181 vendor = gncVendorLookup (pdata->book, guid);
184 gncVendorDestroy (pdata->vendor);
185 pdata->vendor = vendor;
186 gncVendorBeginEdit (vendor);
190 gncVendorSetGUID(pdata->vendor, guid);
199 vendor_id_handler (xmlNodePtr node, gpointer vendor_pdata)
201 struct vendor_pdata *pdata = vendor_pdata;
203 return set_string(node, pdata->vendor, gncVendorSetID);
207 vendor_notes_handler (xmlNodePtr node, gpointer vendor_pdata)
209 struct vendor_pdata *pdata = vendor_pdata;
211 return set_string(node, pdata->vendor, gncVendorSetNotes);
215 vendor_terms_handler (xmlNodePtr node, gpointer vendor_pdata)
217 struct vendor_pdata *pdata = vendor_pdata;
221 guid = dom_tree_to_guid(node);
222 g_return_val_if_fail (guid, FALSE);
223 term = gnc_billterm_xml_find_or_create(pdata->book, guid);
226 gncVendorSetTerms (pdata->vendor, term);
232 vendor_addr_handler (xmlNodePtr node, gpointer vendor_pdata)
234 struct vendor_pdata *pdata = vendor_pdata;
236 return gnc_dom_tree_to_address (node, gncVendorGetAddr(pdata->vendor));
240 vendor_taxincluded_handler (xmlNodePtr node, gpointer vendor_pdata)
242 struct vendor_pdata *pdata = vendor_pdata;
247 str = dom_tree_to_text (node);
248 g_return_val_if_fail (str, FALSE);
250 ret = gncTaxIncludedStringToType (str, &type);
254 gncVendorSetTaxIncluded(pdata->vendor, type);
260 vendor_active_handler (xmlNodePtr node, gpointer vendor_pdata)
262 struct vendor_pdata *pdata = vendor_pdata;
263 return set_boolean (node, pdata->vendor, gncVendorSetActive);
267 vendor_currency_handler (xmlNodePtr node, gpointer vendor_pdata)
269 struct vendor_pdata *pdata = vendor_pdata;
272 com = dom_tree_to_commodity_ref(node, pdata->book);
273 g_return_val_if_fail (com, FALSE);
275 gncVendorSetCurrency (pdata->vendor, com);
281 vendor_taxtable_handler (xmlNodePtr node, gpointer vendor_pdata)
283 struct vendor_pdata *pdata = vendor_pdata;
287 guid = dom_tree_to_guid (node);
288 g_return_val_if_fail (guid, FALSE);
289 taxtable = gncTaxTableLookup (pdata->book, guid);
292 taxtable = gncTaxTableCreate (pdata->book);
293 gncTaxTableBeginEdit (taxtable);
294 gncTaxTableSetGUID (taxtable, guid);
295 gncTaxTableCommitEdit (taxtable);
298 gncTaxTableDecRef (taxtable);
300 gncVendorSetTaxTable (pdata->vendor, taxtable);
306 vendor_taxtableoverride_handler (xmlNodePtr node, gpointer vendor_pdata)
308 struct vendor_pdata *pdata = vendor_pdata;
309 return set_boolean (node, pdata->vendor, gncVendorSetTaxTableOverride);
313 vendor_slots_handler (xmlNodePtr node, gpointer vendor_pdata)
315 struct vendor_pdata *pdata = vendor_pdata;
316 return dom_tree_to_kvp_frame_given (
317 node, qof_instance_get_slots (QOF_INSTANCE(pdata->vendor)));
323 { vendor_name_string, vendor_name_handler, 1, 0 },
324 { vendor_guid_string, vendor_guid_handler, 1, 0 },
325 { vendor_id_string, vendor_id_handler, 1, 0 },
326 { vendor_addr_string, vendor_addr_handler, 1, 0 },
327 { vendor_notes_string, vendor_notes_handler, 0, 0 },
328 { vendor_terms_string, vendor_terms_handler, 0, 0 },
329 { vendor_taxincluded_string, vendor_taxincluded_handler, 1, 0 },
330 { vendor_active_string, vendor_active_handler, 1, 0 },
331 { vendor_currency_string, vendor_currency_handler, 0, 0 },
332 {
"vendor:commodity", vendor_currency_handler, 0, 0 },
333 { vendor_taxtable_string, vendor_taxtable_handler, 0, 0 },
334 { vendor_taxtableoverride_string, vendor_taxtableoverride_handler, 0, 0 },
335 { vendor_slots_string, vendor_slots_handler, 0, 0 },
340 dom_tree_to_vendor (xmlNodePtr node,
QofBook *book)
342 struct vendor_pdata vendor_pdata;
345 vendor_pdata.vendor = gncVendorCreate(book);
346 vendor_pdata.book = book;
347 gncVendorBeginEdit (vendor_pdata.vendor);
349 successful = dom_tree_generic_parse (node, vendor_handlers_v2,
353 gncVendorCommitEdit (vendor_pdata.vendor);
356 PERR (
"failed to parse vendor tree");
357 gncVendorDestroy (vendor_pdata.vendor);
358 vendor_pdata.vendor = NULL;
361 return vendor_pdata.vendor;
365 gnc_vendor_end_handler(gpointer data_for_children,
366 GSList* data_from_children, GSList* sibling_data,
367 gpointer parent_data, gpointer global_data,
368 gpointer *result,
const gchar *tag)
371 xmlNodePtr tree = (xmlNodePtr)data_for_children;
373 QofBook *book = gdata->bookdata;
387 g_return_val_if_fail(tree, FALSE);
389 vendor = dom_tree_to_vendor(tree, book);
392 gdata->cb(tag, gdata->parsedata, vendor);
397 return vendor != NULL;
401 vendor_sixtp_parser_create(
void)
403 return sixtp_dom_parser_new(gnc_vendor_end_handler, NULL, NULL);
407 vendor_should_be_saved (
GncVendor *vendor)
412 id = gncVendorGetID (vendor);
413 if (
id == NULL || *
id ==
'\0')
420 do_count (
QofInstance * vendor_p, gpointer count_p)
422 int *count = count_p;
423 if (vendor_should_be_saved ((
GncVendor *)vendor_p))
428 vendor_get_count (
QofBook *book)
436 xml_add_vendor (
QofInstance * vendor_p, gpointer out_p)
444 if (!vendor_should_be_saved (vendor))
447 node = vendor_dom_tree_create (vendor);
448 xmlElemDump(out, NULL, node);
450 if (ferror(out) || fprintf(out,
"\n") < 0)
455 vendor_write (FILE *out,
QofBook *book)
458 return ferror(out) == 0;
464 g_return_val_if_fail(out, FALSE);
469 gnc_vendor_xml_initialize (
void)
473 GNC_FILE_BACKEND_VERS,
475 vendor_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)
#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)
const gchar * QofLogModule