diff --git a/ChangeLog b/ChangeLog index 826bba5e02..1f8b3fcc87 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,10 @@ * gw-gnome-utils-spec: wrap URLType type, #defines, and gnc_build_url() * get the rest of the C code to use URLTypes properly + * fix the business XML to use symbolic names for enums (instead of + their integer values). This is an incompatible change -- old + business XML objects will not load properly. + 2002-06-29 David Hampton * configure.in: diff --git a/src/business/business-core/file/gnc-customer-xml-v2.c b/src/business/business-core/file/gnc-customer-xml-v2.c index 9610e12c97..81aa39733e 100644 --- a/src/business/business-core/file/gnc-customer-xml-v2.c +++ b/src/business/business-core/file/gnc-customer-xml-v2.c @@ -109,8 +109,9 @@ customer_dom_tree_create (GncCustomer *cust) xmlAddChild(ret, guid_to_dom_tree(cust_terms_string, gncBillTermGetGUID (term))); - xmlAddChild(ret, int_to_dom_tree(cust_taxincluded_string, - gncCustomerGetTaxIncluded (cust))); + xmlAddChild(ret, text_to_dom_tree(cust_taxincluded_string, + gncTaxIncludedTypeToString ( + gncCustomerGetTaxIncluded (cust)))); xmlAddChild(ret, int_to_dom_tree(cust_active_string, gncCustomerGetActive (cust))); @@ -241,14 +242,18 @@ static gboolean customer_taxincluded_handler (xmlNodePtr node, gpointer cust_pdata) { struct customer_pdata *pdata = cust_pdata; - gint64 val; + GncTaxIncluded type; + char *str; gboolean ret; - ret = dom_tree_to_integer(node, &val); - if (ret) { - if (!val) val = GNC_TAXINCLUDED_USEGLOBAL; - gncCustomerSetTaxIncluded(pdata->customer, (GncTaxIncluded)val); - } + str = dom_tree_to_text (node); + g_return_val_if_fail (str, FALSE); + + ret = gncTaxIncludedStringToType (str, &type); + g_free (str); + + if (ret) + gncCustomerSetTaxIncluded(pdata->customer, type); return ret; } diff --git a/src/business/business-core/file/gnc-entry-xml-v2.c b/src/business/business-core/file/gnc-entry-xml-v2.c index a3efb3bdf5..c37596380b 100644 --- a/src/business/business-core/file/gnc-entry-xml-v2.c +++ b/src/business/business-core/file/gnc-entry-xml-v2.c @@ -122,10 +122,12 @@ entry_dom_tree_create (GncEntry *entry) maybe_add_numeric (ret, entry_price_string, gncEntryGetPrice (entry)); maybe_add_numeric (ret, entry_discount_string, gncEntryGetDiscount (entry)); - xmlAddChild(ret, int_to_dom_tree(entry_disctype_string, - gncEntryGetDiscountType (entry))); - xmlAddChild(ret, int_to_dom_tree(entry_dischow_string, - gncEntryGetDiscountHow (entry))); + xmlAddChild(ret, text_to_dom_tree(entry_disctype_string, + gncAmountTypeToString ( + gncEntryGetDiscountType (entry)))); + xmlAddChild(ret, text_to_dom_tree(entry_dischow_string, + gncEntryDiscountHowToString ( + gncEntryGetDiscountHow (entry)))); acc = gncEntryGetAccount (entry); if (acc) @@ -288,24 +290,40 @@ static gboolean entry_disctype_handler (xmlNodePtr node, gpointer entry_pdata) { struct entry_pdata *pdata = entry_pdata; - gint64 val; + GncAmountType type; + char *str; + gboolean ret; - dom_tree_to_integer(node, &val); - gncEntrySetDiscountType(pdata->entry, (gint)val); + str = dom_tree_to_text (node); + g_return_val_if_fail (str, FALSE); - return TRUE; + ret = gncAmountStringToType (str, &type); + g_free (str); + + if (ret) + gncEntrySetDiscountType(pdata->entry, type); + + return ret; } static gboolean entry_dischow_handler (xmlNodePtr node, gpointer entry_pdata) { struct entry_pdata *pdata = entry_pdata; - gint64 val; + GncDiscountHow how; + char *str; + gboolean ret; - dom_tree_to_integer(node, &val); - gncEntrySetDiscountHow(pdata->entry, (gint)val); + str = dom_tree_to_text (node); + g_return_val_if_fail (str, FALSE); - return TRUE; + ret = gncEntryDiscountStringToHow (str, &how); + g_free (str); + + if (ret) + gncEntrySetDiscountHow(pdata->entry, how); + + return ret; } static gboolean diff --git a/src/business/business-core/file/gnc-tax-table-xml-v2.c b/src/business/business-core/file/gnc-tax-table-xml-v2.c index a05f0e1e5c..e52696dd1c 100644 --- a/src/business/business-core/file/gnc-tax-table-xml-v2.c +++ b/src/business/business-core/file/gnc-tax-table-xml-v2.c @@ -92,8 +92,9 @@ ttentry_dom_tree_create (GncTaxTableEntry *entry) amount = gncTaxTableEntryGetAmount (entry); xmlAddChild (ret, gnc_numeric_to_dom_tree (ttentry_amount_string, &amount)); - xmlAddChild(ret, int_to_dom_tree (ttentry_type_string, - gncTaxTableEntryGetType (entry))); + xmlAddChild(ret, text_to_dom_tree (ttentry_type_string, + gncAmountTypeToString ( + gncTaxTableEntryGetType (entry)))); return ret; } @@ -157,11 +158,20 @@ static gboolean ttentry_type_handler (xmlNodePtr node, gpointer taxtable_pdata) { struct ttentry_pdata *pdata = taxtable_pdata; - gint64 val; + GncAmountType type; + char *str; + gboolean ret; - dom_tree_to_integer(node, &val); - gncTaxTableEntrySetType (pdata->ttentry, val); - return TRUE; + str = dom_tree_to_text (node); + g_return_val_if_fail (str, FALSE); + + ret = gncAmountStringToType (str, &type); + g_free (str); + + if (ret) + gncTaxTableEntrySetType (pdata->ttentry, type); + + return ret; } static gboolean diff --git a/src/business/business-core/file/gnc-vendor-xml-v2.c b/src/business/business-core/file/gnc-vendor-xml-v2.c index b2df5e527c..2aa9fa5204 100644 --- a/src/business/business-core/file/gnc-vendor-xml-v2.c +++ b/src/business/business-core/file/gnc-vendor-xml-v2.c @@ -102,8 +102,9 @@ vendor_dom_tree_create (GncVendor *vendor) xmlAddChild(ret, guid_to_dom_tree(vendor_terms_string, gncBillTermGetGUID (term))); - xmlAddChild(ret, int_to_dom_tree(vendor_taxincluded_string, - gncVendorGetTaxIncluded (vendor))); + xmlAddChild(ret, text_to_dom_tree(vendor_taxincluded_string, + gncTaxIncludedTypeToString ( + gncVendorGetTaxIncluded (vendor)))); xmlAddChild(ret, int_to_dom_tree(vendor_active_string, gncVendorGetActive (vendor))); @@ -218,14 +219,18 @@ static gboolean vendor_taxincluded_handler (xmlNodePtr node, gpointer vendor_pdata) { struct vendor_pdata *pdata = vendor_pdata; - gint64 val; + GncTaxIncluded type; + char *str; gboolean ret; - ret = dom_tree_to_integer(node, &val); - if (ret) { - if (!val) val = GNC_TAXINCLUDED_USEGLOBAL; - gncVendorSetTaxIncluded(pdata->vendor, (GncTaxIncluded)val); - } + str = dom_tree_to_text (node); + g_return_val_if_fail (str, FALSE); + + ret = gncTaxIncludedStringToType (str, &type); + g_free (str); + + if (ret) + gncVendorSetTaxIncluded(pdata->vendor, type); return ret; } diff --git a/src/business/business-core/gncEntry.c b/src/business/business-core/gncEntry.c index 91361fc2ad..5728cb8dac 100644 --- a/src/business/business-core/gncEntry.c +++ b/src/business/business-core/gncEntry.c @@ -58,6 +58,39 @@ struct _gncEntry { gboolean dirty; }; +/* You must edit the functions in this block in tandem. KEEP THEM IN + SYNC! */ + +#define GNC_RETURN_ENUM_AS_STRING(x,s) case (x): return (s); +const char * +gncEntryDiscountHowToString (GncDiscountHow how) +{ + switch(how) + { + GNC_RETURN_ENUM_AS_STRING(GNC_DISC_PRETAX, "PRETAX"); + GNC_RETURN_ENUM_AS_STRING(GNC_DISC_SAMETIME, "SAMETIME"); + GNC_RETURN_ENUM_AS_STRING(GNC_DISC_POSTTAX, "POSTTAX"); + default: + g_warning ("asked to translate unknown discount-how %d.\n", how); + break; + } + return(NULL); +} +#undef GNC_RETURN_ENUM_AS_STRING +#define GNC_RETURN_ON_MATCH(s,x) \ + if(safe_strcmp((s), (str)) == 0) { *how = x; return(TRUE); } +gboolean gncEntryDiscountStringToHow (const char *str, GncDiscountHow *how) +{ + GNC_RETURN_ON_MATCH ("PRETAX", GNC_DISC_PRETAX); + GNC_RETURN_ON_MATCH ("SAMETIME", GNC_DISC_SAMETIME); + GNC_RETURN_ON_MATCH ("POSTTAX", GNC_DISC_POSTTAX); + g_warning ("asked to translate unknown discount-how string %s.\n", + str ? str : "(null)"); + + return(FALSE); +} +#undef GNC_RETURN_ON_MATCH + #define _GNC_MOD_NAME GNC_ENTRY_MODULE_NAME #define CACHE_INSERT(str) g_cache_insert(gnc_engine_get_string_cache(), (gpointer)(str)); diff --git a/src/business/business-core/gncEntry.h b/src/business/business-core/gncEntry.h index 5faa3bac85..ffc0b8410c 100644 --- a/src/business/business-core/gncEntry.h +++ b/src/business/business-core/gncEntry.h @@ -32,6 +32,9 @@ typedef enum { GNC_DISC_POSTTAX } GncDiscountHow; +const char * gncEntryDiscountHowToString (GncDiscountHow how); +gboolean gncEntryDiscountStringToHow (const char *str, GncDiscountHow *how); + /* Create/Destroy Functions */ GncEntry *gncEntryCreate (GNCBook *book); diff --git a/src/business/business-core/gncTaxTable.c b/src/business/business-core/gncTaxTable.c index c70ae8f478..59f9566f8d 100644 --- a/src/business/business-core/gncTaxTable.c +++ b/src/business/business-core/gncTaxTable.c @@ -46,6 +46,65 @@ struct _book_info { GList * tables; /* visible tables */ }; +/* You must edit the functions in this block in tandem. KEEP THEM IN + SYNC! */ + +#define GNC_RETURN_ENUM_AS_STRING(x,s) case (x): return (s); +const char * +gncAmountTypeToString (GncAmountType type) +{ + switch(type) + { + GNC_RETURN_ENUM_AS_STRING(GNC_AMT_TYPE_VALUE, "VALUE"); + GNC_RETURN_ENUM_AS_STRING(GNC_AMT_TYPE_PERCENT, "PERCENT"); + default: + g_warning ("asked to translate unknown amount type %d.\n", type); + break; + } + return(NULL); +} + +const char * +gncTaxIncludedTypeToString (GncTaxIncluded type) +{ + switch(type) + { + GNC_RETURN_ENUM_AS_STRING(GNC_TAXINCLUDED_YES, "YES"); + GNC_RETURN_ENUM_AS_STRING(GNC_TAXINCLUDED_NO, "NO"); + GNC_RETURN_ENUM_AS_STRING(GNC_TAXINCLUDED_USEGLOBAL, "USEGLOBAL"); + default: + g_warning ("asked to translate unknown taxincluded type %d.\n", type); + break; + } + return(NULL); +} +#undef GNC_RETURN_ENUM_AS_STRING +#define GNC_RETURN_ON_MATCH(s,x) \ + if(safe_strcmp((s), (str)) == 0) { *type = x; return(TRUE); } +gboolean +gncAmountStringToType (const char *str, GncAmountType *type) +{ + GNC_RETURN_ON_MATCH ("VALUE", GNC_AMT_TYPE_VALUE); + GNC_RETURN_ON_MATCH ("PERCENT", GNC_AMT_TYPE_PERCENT); + g_warning ("asked to translate unknown amount type string %s.\n", + str ? str : "(null)"); + + return(FALSE); +} + +gboolean +gncTaxIncludedStringToType (const char *str, GncTaxIncluded *type) +{ + GNC_RETURN_ON_MATCH ("YES", GNC_TAXINCLUDED_YES); + GNC_RETURN_ON_MATCH ("NO", GNC_TAXINCLUDED_NO); + GNC_RETURN_ON_MATCH ("USEGLOBAL", GNC_TAXINCLUDED_USEGLOBAL); + g_warning ("asked to translate unknown taxincluded type string %s.\n", + str ? str : "(null)"); + + return(FALSE); +} +#undef GNC_RETURN_ON_MATCH + #define _GNC_MOD_NAME GNC_TAXTABLE_MODULE_NAME #define CACHE_INSERT(str) g_cache_insert(gnc_engine_get_string_cache(), (gpointer)(str)); diff --git a/src/business/business-core/gncTaxTable.h b/src/business/business-core/gncTaxTable.h index 18bdc7d78f..68ee8d03ea 100644 --- a/src/business/business-core/gncTaxTable.h +++ b/src/business/business-core/gncTaxTable.h @@ -34,6 +34,12 @@ typedef enum { GNC_TAXINCLUDED_USEGLOBAL, } GncTaxIncluded; +const char * gncAmountTypeToString (GncAmountType type); +gboolean gncAmountStringToType (const char *str, GncAmountType *type); + +const char * gncTaxIncludedTypeToString (GncTaxIncluded type); +gboolean gncTaxIncludedStringToType (const char *str, GncTaxIncluded *type); + /* Create/Destroy Functions */ GncTaxTable * gncTaxTableCreate (GNCBook *book); void gncTaxTableDestroy (GncTaxTable *table);