2002-03-04 13:34:52 -06:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-address-xml-v2.c -- address xml i/o implementation *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2002-03-04 13:34:52 -06:00
|
|
|
* *
|
|
|
|
\********************************************************************/
|
2015-11-29 19:11:29 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2002-03-04 13:34:52 -06:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-11-29 19:11:29 -06:00
|
|
|
}
|
2002-03-04 13:34:52 -06:00
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
|
|
|
|
#include "sixtp.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
#include "sixtp-dom-generators.h"
|
|
|
|
|
|
|
|
#include "gnc-xml.h"
|
|
|
|
#include "io-gncxml-gen.h"
|
|
|
|
#include "io-gncxml-v2.h"
|
|
|
|
|
|
|
|
#include "gnc-address-xml-v2.h"
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
static QofLogModule log_module = GNC_MOD_IO;
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
const gchar* address_version_string = "2.0.0";
|
2002-03-04 13:34:52 -06:00
|
|
|
|
|
|
|
/* ids */
|
2016-03-12 16:04:40 -06:00
|
|
|
#define addr_name_string "addr:name"
|
|
|
|
#define addr_addr1_string "addr:addr1"
|
|
|
|
#define addr_addr2_string "addr:addr2"
|
|
|
|
#define addr_addr3_string "addr:addr3"
|
|
|
|
#define addr_addr4_string "addr:addr4"
|
|
|
|
#define addr_phone_string "addr:phone"
|
|
|
|
#define addr_fax_string "addr:fax"
|
|
|
|
#define addr_email_string "addr:email"
|
|
|
|
#define addr_slots_string "addr:slots"
|
2002-03-04 13:34:52 -06:00
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
|
2002-03-04 13:34:52 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (str && strlen (str) > 0)
|
2010-03-02 15:41:05 -06:00
|
|
|
xmlAddChild (ptr, text_to_dom_tree (tag, str));
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_address_to_dom_tree (const char* tag, GncAddress* addr)
|
2002-03-04 13:34:52 -06:00
|
|
|
{
|
|
|
|
xmlNodePtr ret;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
ret = xmlNewNode (NULL, BAD_CAST tag);
|
|
|
|
xmlSetProp (ret, BAD_CAST "version", BAD_CAST address_version_string);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
|
|
|
maybe_add_string (ret, addr_name_string, gncAddressGetName (addr));
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
maybe_add_string (ret, addr_addr1_string, gncAddressGetAddr1 (addr));
|
|
|
|
maybe_add_string (ret, addr_addr2_string, gncAddressGetAddr2 (addr));
|
|
|
|
maybe_add_string (ret, addr_addr3_string, gncAddressGetAddr3 (addr));
|
|
|
|
maybe_add_string (ret, addr_addr4_string, gncAddressGetAddr4 (addr));
|
|
|
|
|
|
|
|
maybe_add_string (ret, addr_phone_string, gncAddressGetPhone (addr));
|
|
|
|
maybe_add_string (ret, addr_fax_string, gncAddressGetFax (addr));
|
|
|
|
maybe_add_string (ret, addr_email_string, gncAddressGetEmail (addr));
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
/* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
|
|
|
|
xmlAddChild (ret, qof_instance_slots_to_dom_tree (addr_slots_string,
|
|
|
|
QOF_INSTANCE (addr)));
|
2002-03-04 13:34:52 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
struct address_pdata
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GncAddress* address;
|
2002-03-04 13:34:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
set_string (xmlNodePtr node, GncAddress* addr,
|
|
|
|
void (*func) (GncAddress* addr, const char* txt))
|
2002-03-04 13:34:52 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar* txt = dom_tree_to_text (node);
|
|
|
|
g_return_val_if_fail (txt, FALSE);
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
func (addr, txt);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_free (txt);
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_name_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetName);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_addr1_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetAddr1);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_addr2_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetAddr2);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_addr3_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetAddr3);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_addr4_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetAddr4);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_phone_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetPhone);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_fax_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetFax);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
address_email_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2002-03-04 13:34:52 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
return set_string (node, pdata->address, gncAddressSetEmail);
|
2002-03-04 13:34:52 -06:00
|
|
|
}
|
|
|
|
|
2003-09-11 12:37:01 -05:00
|
|
|
static gboolean
|
|
|
|
address_slots_handler (xmlNodePtr node, gpointer addr_pdata)
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
|
2015-06-13 19:28:11 -05:00
|
|
|
return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->address));
|
2003-09-11 12:37:01 -05:00
|
|
|
}
|
|
|
|
|
2010-03-02 15:41:05 -06:00
|
|
|
static struct dom_tree_handler address_handlers_v2[] =
|
|
|
|
{
|
2002-03-04 13:34:52 -06:00
|
|
|
{ addr_name_string, address_name_handler, 0, 0 },
|
|
|
|
{ addr_addr1_string, address_addr1_handler, 0, 0 },
|
|
|
|
{ addr_addr2_string, address_addr2_handler, 0, 0 },
|
|
|
|
{ addr_addr3_string, address_addr3_handler, 0, 0 },
|
|
|
|
{ addr_addr4_string, address_addr4_handler, 0, 0 },
|
|
|
|
{ addr_phone_string, address_phone_handler, 0, 0 },
|
|
|
|
{ addr_fax_string, address_fax_handler, 0, 0 },
|
|
|
|
{ addr_email_string, address_email_handler, 0, 0 },
|
2003-09-11 12:37:01 -05:00
|
|
|
{ addr_slots_string, address_slots_handler, 0, 0 },
|
2002-03-04 13:34:52 -06:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_dom_tree_to_address (xmlNodePtr node, GncAddress* address)
|
2002-03-04 13:34:52 -06:00
|
|
|
{
|
|
|
|
struct address_pdata addr_pdata;
|
|
|
|
gboolean successful;
|
|
|
|
|
|
|
|
addr_pdata.address = address;
|
|
|
|
|
|
|
|
successful = dom_tree_generic_parse (node, address_handlers_v2,
|
|
|
|
&addr_pdata);
|
|
|
|
|
|
|
|
if (!successful)
|
|
|
|
{
|
|
|
|
PERR ("failed to parse address tree");
|
|
|
|
}
|
|
|
|
|
|
|
|
return successful;
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2010-01-31 12:37:28 -06:00
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
address_ns (FILE* out)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (out, FALSE);
|
|
|
|
return gnc_xml2_write_namespace_decl (out, "addr");
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_address_xml_initialize (void)
|
|
|
|
{
|
2010-03-02 15:41:05 -06:00
|
|
|
static GncXmlDataType_t be_data =
|
|
|
|
{
|
|
|
|
GNC_FILE_BACKEND_VERS,
|
|
|
|
"gnc:Address",
|
2016-03-12 16:04:40 -06:00
|
|
|
NULL, /* parser_create */
|
|
|
|
NULL, /* add_item */
|
|
|
|
NULL, /* get_count */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* scrub */
|
2010-03-02 15:41:05 -06:00
|
|
|
address_ns,
|
|
|
|
};
|
|
|
|
|
2016-08-04 16:41:40 -05:00
|
|
|
gnc_xml_register_backend (be_data);
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|