GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-owner-xml-v2.c
1 /********************************************************************\
2  * gnc-owner-xml-v2.c -- owner xml i/o implementation *
3  * *
4  * Copyright (C) 2002 Derek Atkins <[email protected]> *
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 [email protected] *
22  * *
23 \********************************************************************/
24 
25 #include "config.h"
26 
27 #include <glib.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "gnc-xml-helper.h"
32 
33 #include "sixtp.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"
39 
40 #include "gnc-xml.h"
41 #include "io-gncxml-gen.h"
42 #include "io-gncxml-v2.h"
43 
44 #include "gnc-owner-xml-v2.h"
45 #include "gncCustomerP.h"
46 #include "gncJobP.h"
47 #include "gncVendorP.h"
48 #include "gncEmployeeP.h"
49 
50 static QofLogModule log_module = GNC_MOD_IO;
51 
52 const gchar *owner_version_string = "2.0.0";
53 
54 /* ids */
55 #define owner_type_string "owner:type"
56 #define owner_id_string "owner:id"
57 
58 xmlNodePtr
59 gnc_owner_to_dom_tree (const char *tag, const GncOwner *owner)
60 {
61  xmlNodePtr ret;
62  const char *type_str;
63 
64  switch (gncOwnerGetType (owner))
65  {
66  case GNC_OWNER_CUSTOMER:
67  type_str = GNC_ID_CUSTOMER;
68  break;
69  case GNC_OWNER_JOB:
70  type_str = GNC_ID_JOB;
71  break;
72  case GNC_OWNER_VENDOR:
73  type_str = GNC_ID_VENDOR;
74  break;
75  case GNC_OWNER_EMPLOYEE:
76  type_str = GNC_ID_EMPLOYEE;
77  break;
78  default:
79  PWARN ("Invalid owner type: %d", gncOwnerGetType (owner));
80  return NULL;
81  }
82 
83  ret = xmlNewNode(NULL, BAD_CAST tag);
84  xmlSetProp(ret, BAD_CAST "version", BAD_CAST owner_version_string);
85 
86  xmlAddChild (ret, text_to_dom_tree (owner_type_string, type_str));
87  xmlAddChild (ret, guid_to_dom_tree (owner_id_string,
88  gncOwnerGetGUID (owner)));
89 
90  return ret;
91 }
92 
93 /***********************************************************************/
94 
96 {
97  GncOwner *owner;
98  QofBook *book;
99 };
100 
101 static gboolean
102 owner_type_handler (xmlNodePtr node, gpointer owner_pdata)
103 {
104  struct owner_pdata *pdata = owner_pdata;
105  char* txt = dom_tree_to_text(node);
106  g_return_val_if_fail(txt, FALSE);
107 
108  if (!g_strcmp0 (txt, GNC_ID_CUSTOMER))
109  gncOwnerInitCustomer (pdata->owner, NULL);
110  else if (!g_strcmp0 (txt, GNC_ID_JOB))
111  gncOwnerInitJob (pdata->owner, NULL);
112  else if (!g_strcmp0 (txt, GNC_ID_VENDOR))
113  gncOwnerInitVendor (pdata->owner, NULL);
114  else if (!g_strcmp0 (txt, GNC_ID_EMPLOYEE))
115  gncOwnerInitEmployee (pdata->owner, NULL);
116  else
117  {
118  PWARN ("Unknown owner type: %s", txt);
119  g_free(txt);
120  return FALSE;
121  }
122 
123  g_free(txt);
124  return TRUE;
125 }
126 
127 static gboolean
128 owner_id_handler (xmlNodePtr node, gpointer owner_pdata)
129 {
130  struct owner_pdata *pdata = owner_pdata;
131  GncGUID *guid;
132 
133  guid = dom_tree_to_guid(node);
134  g_return_val_if_fail (guid, FALSE);
135 
136  switch (gncOwnerGetType (pdata->owner))
137  {
138  case GNC_OWNER_CUSTOMER:
139  {
140  GncCustomer *cust = gncCustomerLookup (pdata->book, guid);
141  if (!cust)
142  {
143  cust = gncCustomerCreate (pdata->book);
144  gncCustomerSetGUID (cust, guid);
145  }
146  gncOwnerInitCustomer (pdata->owner, cust);
147  break;
148  }
149  case GNC_OWNER_JOB:
150  {
151  GncJob *job = gncJobLookup (pdata->book, guid);
152  if (!job)
153  {
154  job = gncJobCreate (pdata->book);
155  gncJobSetGUID (job, guid);
156  }
157  gncOwnerInitJob (pdata->owner, job);
158  break;
159  }
160  case GNC_OWNER_VENDOR:
161  {
162  GncVendor *vendor = gncVendorLookup (pdata->book, guid);
163  if (!vendor)
164  {
165  vendor = gncVendorCreate (pdata->book);
166  gncVendorSetGUID (vendor, guid);
167  }
168  gncOwnerInitVendor (pdata->owner, vendor);
169  break;
170  }
171  case GNC_OWNER_EMPLOYEE:
172  {
173  GncEmployee *employee = gncEmployeeLookup (pdata->book, guid);
174  if (!employee)
175  {
176  employee = gncEmployeeCreate (pdata->book);
177  gncEmployeeSetGUID (employee, guid);
178  }
179  gncOwnerInitEmployee (pdata->owner, employee);
180  break;
181  }
182  default:
183  PWARN ("Invalid owner type: %d\n", gncOwnerGetType (pdata->owner));
184  g_free (guid);
185  return FALSE;
186  }
187 
188  g_free (guid);
189  return TRUE;
190 }
191 
192 static struct dom_tree_handler owner_handlers_v2[] =
193 {
194  { owner_type_string, owner_type_handler, 1, 0 },
195  { owner_id_string, owner_id_handler, 1, 0 },
196  { NULL, 0, 0, 0 }
197 };
198 
199 gboolean
200 gnc_dom_tree_to_owner (xmlNodePtr node, GncOwner *owner, QofBook *book)
201 {
202  struct owner_pdata owner_pdata;
203  gboolean successful;
204 
205  owner_pdata.owner = owner;
206  owner_pdata.book = book;
207 
208  successful = dom_tree_generic_parse (node, owner_handlers_v2,
209  &owner_pdata);
210 
211  if (!successful)
212  {
213  PERR ("failed to parse owner tree");
214  }
215 
216  return successful;
217 }
218 
219 static gboolean
220 owner_ns(FILE *out)
221 {
222  g_return_val_if_fail(out, FALSE);
223  return gnc_xml2_write_namespace_decl(out, "owner");
224 }
225 
226 void
227 gnc_owner_xml_initialize (void)
228 {
229  static GncXmlDataType_t be_data =
230  {
231  GNC_FILE_BACKEND_VERS,
232  "gnc:Owner",
233  NULL, /* parser_create */
234  NULL, /* add_item */
235  NULL, /* get_count */
236  NULL, /* write */
237  NULL, /* scrub */
238  owner_ns,
239  };
240 
241  qof_object_register_backend ("gnc:Owner",
243  &be_data);
244 }
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Definition: gncOwner.c:496
#define GNC_FILE_BACKEND
Definition: io-gncxml-v2.h:99
#define PERR(format, args...)
Definition: qoflog.h:237
Definition: guid.h:65
#define PWARN(format, args...)
Definition: qoflog.h:243
Definition: gncJob.c:41
api for GnuCash version 2 XML-based file format
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Definition: gncOwner.c:201
gboolean gnc_xml2_write_namespace_decl(FILE *out, const char *name_space)
const gchar * QofLogModule
Definition: qofid.h:89