GnuCash  5.6-150-g038405b370+
gnc-owner-xml-v2.cpp
1 /********************************************************************\
2  * gnc-owner-xml-v2.c -- owner xml i/o implementation *
3  * *
4  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 #include <glib.h>
25 
26 #include <config.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gncCustomerP.h"
30 #include "gncJobP.h"
31 #include "gncVendorP.h"
32 #include "gncEmployeeP.h"
33 
34 #include "gnc-xml-helper.h"
35 #include "sixtp.h"
36 #include "sixtp-utils.h"
37 #include "sixtp-parsers.h"
38 #include "sixtp-utils.h"
39 #include "sixtp-dom-parsers.h"
40 #include "sixtp-dom-generators.h"
41 
42 #include "gnc-xml.h"
43 #include "io-gncxml-gen.h"
44 #include "io-gncxml-v2.h"
45 
46 #include "gnc-owner-xml-v2.h"
47 
48 static QofLogModule log_module = GNC_MOD_IO;
49 
50 const gchar* owner_version_string = "2.0.0";
51 
52 /* ids */
53 #define owner_type_string "owner:type"
54 #define owner_id_string "owner:id"
55 
56 xmlNodePtr
57 gnc_owner_to_dom_tree (const char* tag, const GncOwner* owner)
58 {
59  xmlNodePtr ret;
60  const char* type_str;
61 
62  switch (gncOwnerGetType (owner))
63  {
64  case GNC_OWNER_CUSTOMER:
65  type_str = GNC_ID_CUSTOMER;
66  break;
67  case GNC_OWNER_JOB:
68  type_str = GNC_ID_JOB;
69  break;
70  case GNC_OWNER_VENDOR:
71  type_str = GNC_ID_VENDOR;
72  break;
73  case GNC_OWNER_EMPLOYEE:
74  type_str = GNC_ID_EMPLOYEE;
75  break;
76  default:
77  PWARN ("Invalid owner type: %d", gncOwnerGetType (owner));
78  return NULL;
79  }
80 
81  ret = xmlNewNode (NULL, BAD_CAST tag);
82  xmlSetProp (ret, BAD_CAST "version", BAD_CAST owner_version_string);
83 
84  xmlAddChild (ret, text_to_dom_tree (owner_type_string, type_str));
85  xmlAddChild (ret, guid_to_dom_tree (owner_id_string,
86  gncOwnerGetGUID (owner)));
87 
88  return ret;
89 }
90 
91 /***********************************************************************/
92 
94 {
95  GncOwner* owner;
96  QofBook* book;
97 };
98 
99 static gboolean
100 owner_type_handler (xmlNodePtr node, gpointer owner_pdata)
101 {
102  struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
103  char* txt = dom_tree_to_text (node);
104  g_return_val_if_fail (txt, FALSE);
105 
106  if (!g_strcmp0 (txt, GNC_ID_CUSTOMER))
107  gncOwnerInitCustomer (pdata->owner, NULL);
108  else if (!g_strcmp0 (txt, GNC_ID_JOB))
109  gncOwnerInitJob (pdata->owner, NULL);
110  else if (!g_strcmp0 (txt, GNC_ID_VENDOR))
111  gncOwnerInitVendor (pdata->owner, NULL);
112  else if (!g_strcmp0 (txt, GNC_ID_EMPLOYEE))
113  gncOwnerInitEmployee (pdata->owner, NULL);
114  else
115  {
116  PWARN ("Unknown owner type: %s", txt);
117  g_free (txt);
118  return FALSE;
119  }
120 
121  g_free (txt);
122  return TRUE;
123 }
124 
125 static gboolean
126 owner_id_handler (xmlNodePtr node, gpointer owner_pdata)
127 {
128  struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
129  GncGUID* guid;
130 
131  guid = dom_tree_to_guid (node);
132  g_return_val_if_fail (guid, FALSE);
133 
134  switch (gncOwnerGetType (pdata->owner))
135  {
136  case GNC_OWNER_CUSTOMER:
137  {
138  GncCustomer* cust = gncCustomerLookup (pdata->book, guid);
139  if (!cust)
140  {
141  cust = gncCustomerCreate (pdata->book);
142  gncCustomerSetGUID (cust, guid);
143  }
144  gncOwnerInitCustomer (pdata->owner, cust);
145  break;
146  }
147  case GNC_OWNER_JOB:
148  {
149  GncJob* job = gncJobLookup (pdata->book, guid);
150  if (!job)
151  {
152  job = gncJobCreate (pdata->book);
153  gncJobSetGUID (job, guid);
154  }
155  gncOwnerInitJob (pdata->owner, job);
156  break;
157  }
158  case GNC_OWNER_VENDOR:
159  {
160  GncVendor* vendor = gncVendorLookup (pdata->book, guid);
161  if (!vendor)
162  {
163  vendor = gncVendorCreate (pdata->book);
164  gncVendorSetGUID (vendor, guid);
165  }
166  gncOwnerInitVendor (pdata->owner, vendor);
167  break;
168  }
169  case GNC_OWNER_EMPLOYEE:
170  {
171  GncEmployee* employee = gncEmployeeLookup (pdata->book, guid);
172  if (!employee)
173  {
174  employee = gncEmployeeCreate (pdata->book);
175  gncEmployeeSetGUID (employee, guid);
176  }
177  gncOwnerInitEmployee (pdata->owner, employee);
178  break;
179  }
180  default:
181  PWARN ("Invalid owner type: %d\n", gncOwnerGetType (pdata->owner));
182  guid_free (guid);
183  return FALSE;
184  }
185 
186  guid_free (guid);
187  return TRUE;
188 }
189 
190 static struct dom_tree_handler owner_handlers_v2[] =
191 {
192  { owner_type_string, owner_type_handler, 1, 0 },
193  { owner_id_string, owner_id_handler, 1, 0 },
194  { NULL, 0, 0, 0 }
195 };
196 
197 gboolean
198 gnc_dom_tree_to_owner (xmlNodePtr node, GncOwner* owner, QofBook* book)
199 {
200  struct owner_pdata owner_pdata;
201  gboolean successful;
202 
203  owner_pdata.owner = owner;
204  owner_pdata.book = book;
205 
206  successful = dom_tree_generic_parse (node, owner_handlers_v2,
207  &owner_pdata);
208 
209  if (!successful)
210  {
211  PERR ("failed to parse owner tree");
212  }
213 
214  return successful;
215 }
216 
217 static gboolean
218 owner_ns (FILE* out)
219 {
220  g_return_val_if_fail (out, FALSE);
221  return gnc_xml2_write_namespace_decl (out, "owner");
222 }
223 
224 void
225 gnc_owner_xml_initialize (void)
226 {
227  static GncXmlDataType_t be_data =
228  {
229  GNC_FILE_BACKEND_VERS,
230  "gnc:Owner",
231  NULL, /* parser_create */
232  NULL, /* add_item */
233  NULL, /* get_count */
234  NULL, /* write */
235  NULL, /* scrub */
236  owner_ns,
237  };
238 
239  gnc_xml_register_backend (be_data);
240 }
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Get the GncGUID of the immediate owner.
Definition: gncOwner.c:518
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
api for GnuCash version 2 XML-based file format
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr, currency, taxtable, taxtable_override taxincluded, active and jobs are identical to ::GncVendor.
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition: gncOwner.c:200
The type used to store guids in C.
Definition: guid.h:75