2001-02-12 22:35:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
|
|
|
|
|
|
#include "sixtp.h"
|
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
|
#include "sixtp-parsers.h"
|
2001-02-15 00:17:57 +00:00
|
|
|
#include "sixtp-utils.h"
|
2001-02-22 07:29:05 +00:00
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
#include "sixtp-dom-generators.h"
|
|
|
|
|
|
|
|
|
|
#include "gnc-xml.h"
|
2001-02-12 22:35:20 +00:00
|
|
|
|
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
#include "AccountP.h"
|
|
|
|
|
#include "Account.h"
|
|
|
|
|
#include "Group.h"
|
|
|
|
|
|
2001-02-22 07:29:05 +00:00
|
|
|
const gchar *account_version_string = "2.0.0";
|
|
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
/* ids */
|
|
|
|
|
const char *gnc_account_string = "gnc:account";
|
|
|
|
|
const char *act_name_string = "act:name";
|
|
|
|
|
const char *act_id_string = "act:id";
|
|
|
|
|
const char *act_type_string = "act:type";
|
|
|
|
|
const char *act_currency_string = "act:currency";
|
|
|
|
|
const char *act_code_string = "act:code";
|
|
|
|
|
const char *act_description_string = "act:description";
|
|
|
|
|
const char *act_security_string = "act:security";
|
|
|
|
|
const char *act_slots_string = "act:slots";
|
|
|
|
|
const char *act_parent_string = "act:parent";
|
|
|
|
|
|
2001-02-12 22:35:20 +00:00
|
|
|
xmlNodePtr
|
|
|
|
|
gnc_account_dom_tree_create(Account *act)
|
|
|
|
|
{
|
2001-02-22 07:29:05 +00:00
|
|
|
xmlNodePtr ret;
|
|
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
ret = xmlNewNode(NULL, gnc_account_string);
|
2001-02-22 07:29:05 +00:00
|
|
|
xmlSetProp(ret, "version", account_version_string);
|
|
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlNewChild(ret, NULL, act_name_string, xaccAccountGetName(act));
|
2001-02-22 07:29:05 +00:00
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlAddChild(ret, guid_to_dom_tree(act_id_string, xaccAccountGetGUID(act)));
|
2001-02-22 07:29:05 +00:00
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlNewChild(ret, NULL, act_type_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountTypeEnumAsString(xaccAccountGetType(act)));
|
|
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlAddChild(ret, commodity_ref_to_dom_tree(act_currency_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountGetCurrency(act)));
|
|
|
|
|
|
|
|
|
|
if(xaccAccountGetCode(act))
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlNewChild(ret, NULL, act_code_string, xaccAccountGetCode(act));
|
2001-02-22 07:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xaccAccountGetDescription(act))
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlNewChild(ret, NULL, act_description_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountGetDescription(act));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xaccAccountGetSecurity(act))
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlAddChild(ret, commodity_ref_to_dom_tree(act_security_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountGetSecurity(act)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xaccAccountGetSlots(act))
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
xmlAddChild(ret, kvp_frame_to_dom_tree(act_slots_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountGetSlots(act)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xaccAccountGetParentAccount(act))
|
|
|
|
|
{
|
|
|
|
|
xmlAddChild(ret, guid_to_dom_tree(
|
2001-02-28 08:06:50 +00:00
|
|
|
act_parent_string,
|
2001-02-22 07:29:05 +00:00
|
|
|
xaccAccountGetGUID(xaccAccountGetParentAccount(act))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
2001-02-12 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
static gboolean
|
|
|
|
|
account_name_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
gchar* txt;
|
|
|
|
|
|
|
|
|
|
txt = dom_tree_to_text(node);
|
|
|
|
|
g_return_val_if_fail(txt, FALSE);
|
|
|
|
|
|
|
|
|
|
xaccAccountSetName(act, txt);
|
|
|
|
|
|
|
|
|
|
g_free(txt);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
2001-02-12 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_id_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
|
|
|
|
xaccAccountSetGUID(act, dom_tree_to_guid(node));
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_type_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
|
|
|
|
int type;
|
|
|
|
|
|
2001-02-27 20:15:11 +00:00
|
|
|
xaccAccountStringToType(node->xmlChildrenNode->content, &type);
|
2001-02-12 22:35:20 +00:00
|
|
|
xaccAccountSetType(act, type);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_currency_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
2001-02-15 00:17:57 +00:00
|
|
|
gnc_commodity *ref;
|
|
|
|
|
|
|
|
|
|
ref = dom_tree_to_commodity_ref(node);
|
|
|
|
|
xaccAccountSetCurrency(
|
|
|
|
|
act, associate_commodity_ref_with_engine_commodity(ref));
|
|
|
|
|
gnc_commodity_destroy(ref);
|
2001-02-12 22:35:20 +00:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_security_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
2001-02-15 00:17:57 +00:00
|
|
|
gnc_commodity *ref;
|
|
|
|
|
ref = dom_tree_to_commodity_ref(node);
|
|
|
|
|
xaccAccountSetSecurity(
|
|
|
|
|
act, associate_commodity_ref_with_engine_commodity(ref));
|
|
|
|
|
gnc_commodity_destroy(ref);
|
2001-02-12 22:35:20 +00:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_slots_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
kvp_frame *frm = dom_tree_to_kvp_frame(node);
|
|
|
|
|
g_return_val_if_fail(frm, FALSE);
|
|
|
|
|
|
|
|
|
|
xaccAccountSetSlots_nc(act, frm);
|
|
|
|
|
|
2001-02-22 07:29:05 +00:00
|
|
|
return TRUE;
|
2001-02-12 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_parent_handler (xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
|
|
|
|
Account *parent;
|
|
|
|
|
GUID *gid = dom_tree_to_guid(node);
|
|
|
|
|
|
|
|
|
|
parent = xaccAccountLookup(gid);
|
|
|
|
|
|
|
|
|
|
xaccAccountInsertSubAccount(parent, act);
|
|
|
|
|
|
|
|
|
|
xaccGUIDFree(gid);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-28 08:06:50 +00:00
|
|
|
static gboolean
|
|
|
|
|
account_code_handler(xmlNodePtr node, Account* act)
|
|
|
|
|
{
|
|
|
|
|
gchar* txt;
|
|
|
|
|
|
|
|
|
|
txt = dom_tree_to_text(node);
|
|
|
|
|
g_return_val_if_fail(txt, FALSE);
|
|
|
|
|
|
|
|
|
|
xaccAccountSetCode(act, txt);
|
|
|
|
|
|
|
|
|
|
g_free(txt);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
account_description_handler(xmlNodePtr node, Account *act)
|
|
|
|
|
{
|
|
|
|
|
gchar* txt;
|
|
|
|
|
|
|
|
|
|
txt = dom_tree_to_text(node);
|
|
|
|
|
g_return_val_if_fail(txt, FALSE);
|
|
|
|
|
|
|
|
|
|
xaccAccountSetDescription(act, txt);
|
|
|
|
|
|
|
|
|
|
g_free(txt);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-12 22:35:20 +00:00
|
|
|
struct dom_handlers
|
|
|
|
|
{
|
2001-02-28 08:06:50 +00:00
|
|
|
const char *tag;
|
2001-02-12 22:35:20 +00:00
|
|
|
|
|
|
|
|
gboolean (*handler) (xmlNodePtr, Account*);
|
|
|
|
|
|
|
|
|
|
int required;
|
|
|
|
|
int gotten;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct dom_handlers account_handlers_v2[] = {
|
|
|
|
|
{ "act:name", account_name_handler, 1, 0 },
|
|
|
|
|
{ "act:id", account_id_handler, 1, 0 },
|
|
|
|
|
{ "act:type", account_type_handler, 1, 0 },
|
|
|
|
|
{ "act:currency", account_currency_handler, 1, 0 },
|
2001-02-28 08:06:50 +00:00
|
|
|
{ "act:code", account_code_handler, 1, 0 },
|
|
|
|
|
{ "act:description", account_description_handler, 1, 0},
|
2001-02-12 22:35:20 +00:00
|
|
|
{ "act:security", account_security_handler, 0, 0 },
|
|
|
|
|
{ "act:slots", account_slots_handler, 0, 0 },
|
|
|
|
|
{ "act:parent", account_parent_handler, 0, 0 },
|
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_handlers(struct dom_handlers *handler_ptr)
|
|
|
|
|
{
|
|
|
|
|
while(handler_ptr->tag != NULL)
|
|
|
|
|
{
|
|
|
|
|
handler_ptr->gotten = 0;
|
|
|
|
|
|
|
|
|
|
handler_ptr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
all_required_gotten_p(struct dom_handlers *handler_ptr)
|
|
|
|
|
{
|
|
|
|
|
while(handler_ptr->tag != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(handler_ptr->required && ! handler_ptr->gotten)
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
handler_ptr++;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gnc_xml_set_account_data(const gchar* tag, xmlNodePtr node, Account *acc,
|
|
|
|
|
struct dom_handlers *handler_ptr)
|
|
|
|
|
{
|
|
|
|
|
while(handler_ptr->tag)
|
|
|
|
|
{
|
|
|
|
|
if(strcmp(tag, handler_ptr->tag) == 0)
|
|
|
|
|
{
|
|
|
|
|
(handler_ptr->handler)(node, acc);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handler_ptr++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!handler_ptr->tag)
|
|
|
|
|
{
|
|
|
|
|
g_warning("Unhandled account tag: %s\n", tag);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gnc_account_end_handler(gpointer data_for_children,
|
|
|
|
|
GSList* data_from_children, GSList* sibling_data,
|
|
|
|
|
gpointer parent_data, gpointer global_data,
|
|
|
|
|
gpointer *result, const gchar *tag)
|
|
|
|
|
{
|
|
|
|
|
int successful;
|
|
|
|
|
Account *acc;
|
|
|
|
|
xmlNodePtr achild;
|
|
|
|
|
xmlNodePtr tree = (xmlNodePtr)data_for_children;
|
|
|
|
|
|
|
|
|
|
successful = TRUE;
|
2001-02-22 07:29:05 +00:00
|
|
|
|
|
|
|
|
if(parent_data)
|
|
|
|
|
{
|
|
|
|
|
return successful;
|
|
|
|
|
}
|
2001-02-12 22:35:20 +00:00
|
|
|
|
|
|
|
|
acc = xaccMallocAccount();
|
|
|
|
|
g_return_val_if_fail(acc, FALSE);
|
|
|
|
|
xaccAccountBeginEdit(acc);
|
|
|
|
|
|
|
|
|
|
set_handlers(account_handlers_v2);
|
|
|
|
|
|
2001-02-22 07:29:05 +00:00
|
|
|
for(achild = tree->xmlChildrenNode; achild; achild = achild->next)
|
2001-02-12 22:35:20 +00:00
|
|
|
{
|
|
|
|
|
if(!gnc_xml_set_account_data(achild->name, achild, acc,
|
|
|
|
|
account_handlers_v2))
|
|
|
|
|
{
|
|
|
|
|
successful = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xaccAccountCommitEdit(acc);
|
|
|
|
|
|
|
|
|
|
if(!all_required_gotten_p(account_handlers_v2))
|
|
|
|
|
{
|
|
|
|
|
successful = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!successful)
|
|
|
|
|
{
|
|
|
|
|
xaccFreeAccount(acc);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(!xaccAccountGetParent(acc))
|
|
|
|
|
{
|
|
|
|
|
/* FIXME: something like this */
|
2001-02-22 07:29:05 +00:00
|
|
|
/* xaccGroupInsertAccount(global_data, acc); */
|
2001-02-12 22:35:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-25 00:42:44 +00:00
|
|
|
xmlFreeNode((xmlNodePtr) result);
|
2001-02-12 22:35:20 +00:00
|
|
|
|
|
|
|
|
return successful;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sixtp*
|
2001-02-22 07:29:05 +00:00
|
|
|
gnc_account_sixtp_parser_create(void)
|
2001-02-12 22:35:20 +00:00
|
|
|
{
|
2001-02-25 00:42:44 +00:00
|
|
|
return sixtp_dom_parser_new(gnc_account_end_handler, NULL, NULL);
|
2001-02-12 22:35:20 +00:00
|
|
|
}
|