Line data Source code
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 :
49 : static QofLogModule log_module = GNC_MOD_IO;
50 :
51 : const 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 :
63 : static xmlNodePtr
64 0 : job_dom_tree_create (GncJob* job)
65 : {
66 : xmlNodePtr ret;
67 :
68 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_job_string);
69 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST job_version_string);
70 :
71 0 : xmlAddChild (ret, guid_to_dom_tree (job_guid_string,
72 0 : qof_instance_get_guid (QOF_INSTANCE (job))));
73 :
74 0 : xmlAddChild (ret, text_to_dom_tree (job_id_string,
75 : gncJobGetID (job)));
76 :
77 0 : xmlAddChild (ret, text_to_dom_tree (job_name_string,
78 : gncJobGetName (job)));
79 :
80 0 : maybe_add_string (ret, job_reference_string, gncJobGetReference (job));
81 :
82 0 : xmlAddChild (ret, gnc_owner_to_dom_tree (job_owner_string,
83 0 : gncJobGetOwner (job)));
84 :
85 0 : xmlAddChild (ret, int_to_dom_tree (job_active_string,
86 0 : gncJobGetActive (job)));
87 :
88 : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
89 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (job_slots_string,
90 0 : QOF_INSTANCE (job)));
91 :
92 0 : return ret;
93 : }
94 :
95 : /***********************************************************************/
96 :
97 : struct job_pdata
98 : {
99 : GncJob* job;
100 : QofBook* book;
101 : };
102 :
103 : static gboolean
104 1 : job_name_handler (xmlNodePtr node, gpointer job_pdata)
105 : {
106 1 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
107 :
108 1 : return apply_xmlnode_text (gncJobSetName, pdata->job, node);
109 : }
110 :
111 : static gboolean
112 1 : job_guid_handler (xmlNodePtr node, gpointer job_pdata)
113 : {
114 1 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
115 : GncJob* job;
116 :
117 1 : auto guid = dom_tree_to_guid (node);
118 1 : g_return_val_if_fail (guid, FALSE);
119 1 : job = gncJobLookup (pdata->book, &*guid);
120 1 : if (job)
121 : {
122 1 : gncJobDestroy (pdata->job);
123 1 : pdata->job = job;
124 1 : gncJobBeginEdit (job);
125 : }
126 : else
127 : {
128 0 : gncJobSetGUID (pdata->job, &*guid);
129 : }
130 :
131 1 : return TRUE;
132 : }
133 :
134 : static gboolean
135 1 : job_id_handler (xmlNodePtr node, gpointer job_pdata)
136 : {
137 1 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
138 :
139 1 : return apply_xmlnode_text (gncJobSetID, pdata->job, node);
140 : }
141 :
142 : static gboolean
143 0 : job_reference_handler (xmlNodePtr node, gpointer job_pdata)
144 : {
145 0 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
146 :
147 0 : return apply_xmlnode_text (gncJobSetReference, pdata->job, node);
148 : }
149 :
150 : static gboolean
151 1 : job_owner_handler (xmlNodePtr node, gpointer job_pdata)
152 : {
153 1 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
154 : GncOwner owner;
155 : gboolean ret;
156 :
157 1 : ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
158 1 : if (ret)
159 1 : gncJobSetOwner (pdata->job, &owner);
160 :
161 1 : return ret;
162 : }
163 :
164 : static gboolean
165 1 : job_active_handler (xmlNodePtr node, gpointer job_pdata)
166 : {
167 1 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
168 : gint64 val;
169 : gboolean ret;
170 :
171 1 : ret = dom_tree_to_integer (node, &val);
172 1 : if (ret)
173 1 : gncJobSetActive (pdata->job, (gboolean)val);
174 :
175 1 : return ret;
176 : }
177 :
178 : static gboolean
179 0 : job_slots_handler (xmlNodePtr node, gpointer job_pdata)
180 : {
181 0 : struct job_pdata* pdata = static_cast<decltype (pdata)> (job_pdata);
182 :
183 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->job));
184 : }
185 :
186 : static 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 :
198 : static GncJob*
199 1 : dom_tree_to_job (xmlNodePtr node, QofBook* book)
200 : {
201 : struct job_pdata job_pdata;
202 : gboolean successful;
203 :
204 1 : job_pdata.job = gncJobCreate (book);
205 1 : job_pdata.book = book;
206 1 : gncJobBeginEdit (job_pdata.job);
207 :
208 1 : successful = dom_tree_generic_parse (node, job_handlers_v2,
209 : &job_pdata);
210 :
211 1 : if (successful)
212 1 : gncJobCommitEdit (job_pdata.job);
213 : else
214 : {
215 0 : PERR ("failed to parse job tree");
216 0 : gncJobDestroy (job_pdata.job);
217 0 : job_pdata.job = NULL;
218 : }
219 :
220 1 : return job_pdata.job;
221 : }
222 :
223 : static gboolean
224 8 : gnc_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 8 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
231 8 : gxpf_data* gdata = (gxpf_data*)global_data;
232 8 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
233 :
234 8 : if (parent_data)
235 : {
236 7 : 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 1 : if (!tag)
242 : {
243 0 : return TRUE;
244 : }
245 :
246 1 : g_return_val_if_fail (tree, FALSE);
247 :
248 1 : job = dom_tree_to_job (tree, book);
249 1 : if (job != NULL)
250 : {
251 1 : gdata->cb (tag, gdata->parsedata, job);
252 : }
253 :
254 1 : xmlFreeNode (tree);
255 :
256 1 : return job != NULL;
257 : }
258 :
259 : static sixtp*
260 22 : job_sixtp_parser_create (void)
261 : {
262 22 : return sixtp_dom_parser_new (gnc_job_end_handler, NULL, NULL);
263 : }
264 :
265 : static gboolean
266 0 : job_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 0 : id = gncJobGetID (job);
272 0 : if (id == NULL || *id == '\0')
273 0 : return FALSE;
274 :
275 0 : return TRUE;
276 : }
277 :
278 : static void
279 0 : do_count (QofInstance* job_p, gpointer count_p)
280 : {
281 0 : int* count = static_cast<decltype (count)> (count_p);
282 0 : if (job_should_be_saved ((GncJob*)job_p))
283 0 : (*count)++;
284 0 : }
285 :
286 : static int
287 4 : job_get_count (QofBook* book)
288 : {
289 4 : int count = 0;
290 4 : qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
291 4 : return count;
292 : }
293 :
294 : static void
295 0 : xml_add_job (QofInstance* job_p, gpointer out_p)
296 : {
297 : xmlNodePtr node;
298 0 : GncJob* job = (GncJob*) job_p;
299 0 : FILE* out = static_cast<decltype (out)> (out_p);
300 :
301 0 : if (ferror (out))
302 0 : return;
303 0 : if (!job_should_be_saved (job))
304 0 : return;
305 :
306 0 : node = job_dom_tree_create (job);
307 0 : xmlElemDump (out, NULL, node);
308 0 : xmlFreeNode (node);
309 0 : if (ferror (out) || fprintf (out, "\n") < 0)
310 0 : return;
311 : }
312 :
313 : static gboolean
314 4 : job_write (FILE* out, QofBook* book)
315 : {
316 4 : qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_job, (gpointer) out);
317 4 : return ferror (out) == 0;
318 : }
319 :
320 : static gboolean
321 4 : job_ns (FILE* out)
322 : {
323 4 : g_return_val_if_fail (out, FALSE);
324 4 : return gnc_xml2_write_namespace_decl (out, "job");
325 : }
326 :
327 : void
328 15 : gnc_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 15 : gnc_xml_register_backend(be_data);
343 15 : }
|