GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-job-xml-v2.cpp
1/********************************************************************\
2 * gnc-job-xml-v2.c -- job 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 "gncJobP.h"
30
31#include "gnc-xml-helper.h"
32#include "sixtp.h"
33#include "sixtp-utils.h"
34#include "sixtp-parsers.h"
35#include "sixtp-utils.h"
36#include "sixtp-dom-parsers.h"
37#include "sixtp-dom-generators.h"
38
39#include "gnc-xml.h"
40#include "io-gncxml-gen.h"
41#include "io-gncxml-v2.h"
42
43#include "gnc-job-xml-v2.h"
44#include "gnc-owner-xml-v2.h"
45#include "xml-helpers.h"
46
47#define _GNC_MOD_NAME GNC_ID_JOB
48
49static QofLogModule log_module = GNC_MOD_IO;
50
51const gchar* job_version_string = "2.0.0";
52
53/* ids */
54#define gnc_job_string "gnc:GncJob"
55#define job_guid_string "job:guid"
56#define job_id_string "job:id"
57#define job_name_string "job:name"
58#define job_reference_string "job:reference"
59#define job_owner_string "job:owner"
60#define job_active_string "job:active"
61#define job_slots_string "job:slots"
62
63static xmlNodePtr
64job_dom_tree_create (GncJob* job)
65{
66 xmlNodePtr ret;
67
68 ret = xmlNewNode (NULL, BAD_CAST gnc_job_string);
69 xmlSetProp (ret, BAD_CAST "version", BAD_CAST job_version_string);
70
71 xmlAddChild (ret, guid_to_dom_tree (job_guid_string,
72 qof_instance_get_guid (QOF_INSTANCE (job))));
73
74 xmlAddChild (ret, text_to_dom_tree (job_id_string,
75 gncJobGetID (job)));
76
77 xmlAddChild (ret, text_to_dom_tree (job_name_string,
78 gncJobGetName (job)));
79
80 maybe_add_string (ret, job_reference_string, gncJobGetReference (job));
81
82 xmlAddChild (ret, gnc_owner_to_dom_tree (job_owner_string,
83 gncJobGetOwner (job)));
84
85 xmlAddChild (ret, int_to_dom_tree (job_active_string,
86 gncJobGetActive (job)));
87
88 /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
89 xmlAddChild (ret, qof_instance_slots_to_dom_tree (job_slots_string,
90 QOF_INSTANCE (job)));
91
92 return ret;
93}
94
95/***********************************************************************/
96
98{
99 GncJob* job;
100 QofBook* book;
101};
102
103static gboolean
104job_name_handler (xmlNodePtr node, gpointer job_pdata)
105{
106 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
107
108 return apply_xmlnode_text (gncJobSetName, pdata->job, node);
109}
110
111static gboolean
112job_guid_handler (xmlNodePtr node, gpointer job_pdata)
113{
114 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
115 GncJob* job;
116
117 auto guid = dom_tree_to_guid (node);
118 g_return_val_if_fail (guid, FALSE);
119 job = gncJobLookup (pdata->book, &*guid);
120 if (job)
121 {
122 gncJobDestroy (pdata->job);
123 pdata->job = job;
124 gncJobBeginEdit (job);
125 }
126 else
127 {
128 gncJobSetGUID (pdata->job, &*guid);
129 }
130
131 return TRUE;
132}
133
134static gboolean
135job_id_handler (xmlNodePtr node, gpointer job_pdata)
136{
137 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
138
139 return apply_xmlnode_text (gncJobSetID, pdata->job, node);
140}
141
142static gboolean
143job_reference_handler (xmlNodePtr node, gpointer job_pdata)
144{
145 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
146
147 return apply_xmlnode_text (gncJobSetReference, pdata->job, node);
148}
149
150static gboolean
151job_owner_handler (xmlNodePtr node, gpointer job_pdata)
152{
153 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
154 GncOwner owner;
155 gboolean ret;
156
157 ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
158 if (ret)
159 gncJobSetOwner (pdata->job, &owner);
160
161 return ret;
162}
163
164static gboolean
165job_active_handler (xmlNodePtr node, gpointer job_pdata)
166{
167 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
168 gint64 val;
169 gboolean ret;
170
171 ret = dom_tree_to_integer (node, &val);
172 if (ret)
173 gncJobSetActive (pdata->job, (gboolean)val);
174
175 return ret;
176}
177
178static gboolean
179job_slots_handler (xmlNodePtr node, gpointer job_pdata)
180{
181 struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
182
183 return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->job));
184}
185
186static struct dom_tree_handler job_handlers_v2[] =
187{
188 { job_guid_string, job_guid_handler, 1, 0 },
189 { job_id_string, job_id_handler, 1, 0 },
190 { job_name_string, job_name_handler, 1, 0 },
191 { job_reference_string, job_reference_handler, 0, 0 },
192 { job_owner_string, job_owner_handler, 1, 0 },
193 { job_active_string, job_active_handler, 1, 0 },
194 { job_slots_string, job_slots_handler, 0, 0 },
195 { NULL, 0, 0, 0 }
196};
197
198static GncJob*
199dom_tree_to_job (xmlNodePtr node, QofBook* book)
200{
201 struct job_pdata job_pdata;
202 gboolean successful;
203
204 job_pdata.job = gncJobCreate (book);
205 job_pdata.book = book;
206 gncJobBeginEdit (job_pdata.job);
207
208 successful = dom_tree_generic_parse (node, job_handlers_v2,
209 &job_pdata);
210
211 if (successful)
212 gncJobCommitEdit (job_pdata.job);
213 else
214 {
215 PERR ("failed to parse job tree");
216 gncJobDestroy (job_pdata.job);
217 job_pdata.job = NULL;
218 }
219
220 return job_pdata.job;
221}
222
223static gboolean
224gnc_job_end_handler (gpointer data_for_children,
225 GSList* data_from_children, GSList* sibling_data,
226 gpointer parent_data, gpointer global_data,
227 gpointer* result, const gchar* tag)
228{
229 GncJob* job;
230 xmlNodePtr tree = (xmlNodePtr)data_for_children;
231 gxpf_data* gdata = (gxpf_data*)global_data;
232 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
233
234 if (parent_data)
235 {
236 return TRUE;
237 }
238
239 /* OK. For some messed up reason this is getting called again with a
240 NULL tag. So we ignore those cases */
241 if (!tag)
242 {
243 return TRUE;
244 }
245
246 g_return_val_if_fail (tree, FALSE);
247
248 job = dom_tree_to_job (tree, book);
249 if (job != NULL)
250 {
251 gdata->cb (tag, gdata->parsedata, job);
252 }
253
254 xmlFreeNode (tree);
255
256 return job != NULL;
257}
258
259static sixtp*
260job_sixtp_parser_create (void)
261{
262 return sixtp_dom_parser_new (gnc_job_end_handler, NULL, NULL);
263}
264
265static gboolean
266job_should_be_saved (GncJob* job)
267{
268 const char* id;
269
270 /* make sure this is a valid job before we save it -- should have an ID */
271 id = gncJobGetID (job);
272 if (id == NULL || *id == '\0')
273 return FALSE;
274
275 return TRUE;
276}
277
278static void
279do_count (QofInstance* job_p, gpointer count_p)
280{
281 int* count = static_cast<decltype (count)> (count_p);
282 if (job_should_be_saved ((GncJob*)job_p))
283 (*count)++;
284}
285
286static int
287job_get_count (QofBook* book)
288{
289 int count = 0;
290 qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
291 return count;
292}
293
294static void
295xml_add_job (QofInstance* job_p, gpointer out_p)
296{
297 xmlNodePtr node;
298 GncJob* job = (GncJob*) job_p;
299 FILE* out = static_cast<decltype (out)> (out_p);
300
301 if (ferror (out))
302 return;
303 if (!job_should_be_saved (job))
304 return;
305
306 node = job_dom_tree_create (job);
307 xmlElemDump (out, NULL, node);
308 xmlFreeNode (node);
309 if (ferror (out) || fprintf (out, "\n") < 0)
310 return;
311}
312
313static gboolean
314job_write (FILE* out, QofBook* book)
315{
316 qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_job, (gpointer) out);
317 return ferror (out) == 0;
318}
319
320static gboolean
321job_ns (FILE* out)
322{
323 g_return_val_if_fail (out, FALSE);
324 return gnc_xml2_write_namespace_decl (out, "job");
325}
326
327void
328gnc_job_xml_initialize (void)
329{
330 static GncXmlDataType_t be_data =
331 {
332 GNC_FILE_BACKEND_VERS,
333 gnc_job_string,
334 job_sixtp_parser_create,
335 NULL, /* add_item */
336 job_get_count,
337 job_write,
338 NULL, /* scrub */
339 job_ns,
340 };
341
342 gnc_xml_register_backend(be_data);
343}
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback 'cb' on every instance ov a particular object type.
void qof_object_foreach_sorted(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke callback 'cb' on each instance in guid orted order.
api for GnuCash version 2 XML-based file format
QofBook reference.
Definition qofbook-p.hpp:47
Definition sixtp.h:130