Line data Source code
1 : /********************************************************************
2 : * gnc-freqspec-xml-v2.c -- xml routines for FreqSpecs *
3 : * Copyright (C) 2001 Joshua Sled <jsled@asynchronous.org> *
4 : * Copyright (C) 2001 Ben Stanley <bds02@uow.edu.au> *
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 :
25 : #include <glib.h>
26 :
27 : #include <config.h>
28 : #include <string.h>
29 : #include "qof.h"
30 : #include "SchedXaction.h"
31 : #include "FreqSpec.h"
32 :
33 : #include "gnc-xml-helper.h"
34 : #include "sixtp.h"
35 : #include "sixtp-utils.h"
36 : #include "sixtp-parsers.h"
37 : #include "sixtp-utils.h"
38 : #include "sixtp-dom-parsers.h"
39 : #include "sixtp-dom-generators.h"
40 :
41 : #include "gnc-xml.h"
42 :
43 : #include "io-gncxml-v2.h"
44 :
45 : #include "sixtp-dom-parsers.h"
46 :
47 : const gchar* freqspec_version_string = "1.0.0";
48 :
49 : struct freqTypeTuple
50 : {
51 : const char* str;
52 : FreqType ft;
53 : };
54 :
55 : struct freqTypeTuple freqTypeStrs[] =
56 : {
57 : { "none", INVALID },
58 : { "once", ONCE },
59 : { "daily", DAILY },
60 : { "weekly", WEEKLY },
61 : { "monthly", MONTHLY },
62 : { "month_relative", MONTH_RELATIVE },
63 : { "composite", COMPOSITE },
64 : { NULL, static_cast<FreqType> (-1) },
65 : };
66 :
67 : struct uiFreqTypeTuple
68 : {
69 : const char* str;
70 : UIFreqType uift;
71 : };
72 :
73 : struct uiFreqTypeTuple uiFreqTypeStrs[] =
74 : {
75 : { "none", UIFREQ_NONE },
76 : { "once", UIFREQ_ONCE },
77 : { "daily", UIFREQ_DAILY },
78 : { "daily_mf", UIFREQ_DAILY_MF },
79 : { "weekly", UIFREQ_WEEKLY },
80 : { "bi_weekly", UIFREQ_BI_WEEKLY },
81 : { "semi_monthly", UIFREQ_SEMI_MONTHLY },
82 : { "monthly", UIFREQ_MONTHLY },
83 : { "quarterly", UIFREQ_QUARTERLY },
84 : { "tri_anually", UIFREQ_TRI_ANUALLY },
85 : { "semi_yearly", UIFREQ_SEMI_YEARLY },
86 : { "yearly", UIFREQ_YEARLY },
87 : { NULL, static_cast<UIFreqType> (-1) }
88 : };
89 :
90 : /**
91 : * Struct passed around as user-data when parsing the FreqSpec.
92 : **/
93 : typedef struct
94 : {
95 : QofBook* book; /* Book we're loading into. */
96 :
97 : Recurrence* recurrence;
98 : GList* recurrence_list;
99 :
100 : /* fields used in the union of unions... :) */
101 : GDate once_day; /* once */
102 : gint64 interval; /* all [except once] */
103 : gint64 offset; /* all [except once] */
104 : gint64 day; /* monthly or month-relative */
105 : gint64 occurrence; /* month-relative */
106 : gint64 weekend_adj; /* monthly/yearly */
107 : GList* list; /* composite */
108 : UIFreqType uift;
109 : } fsParseData;
110 :
111 : static void
112 0 : fspd_init (fsParseData* fspd)
113 : {
114 0 : fspd->list = NULL;
115 0 : fspd->book = NULL;
116 0 : fspd->recurrence = g_new0 (Recurrence, 1);
117 0 : fspd->recurrence_list = NULL;
118 0 : fspd->uift = UIFREQ_NONE;
119 : fspd->interval
120 0 : = fspd->offset
121 0 : = fspd->day
122 0 : = fspd->occurrence
123 0 : = 0;
124 0 : fspd->weekend_adj = WEEKEND_ADJ_NONE;
125 0 : g_date_clear (&fspd->once_day, 1);
126 0 : }
127 :
128 : static
129 : gboolean
130 : gnc_fs_handler (xmlNodePtr node, gpointer d);
131 :
132 : static
133 : gboolean
134 0 : fs_uift_handler (xmlNodePtr node, gpointer data)
135 : {
136 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
137 : int i;
138 :
139 0 : auto nodeTxt = dom_tree_to_text (node);
140 :
141 0 : g_return_val_if_fail (nodeTxt, FALSE);
142 0 : for (i = 0; uiFreqTypeStrs[i].str != NULL; i++)
143 : {
144 0 : if (g_strcmp0 (nodeTxt->c_str(), uiFreqTypeStrs[i].str) == 0)
145 : {
146 0 : fspd->uift = uiFreqTypeStrs[i].uift;
147 0 : return TRUE;
148 : }
149 : }
150 0 : return FALSE;
151 0 : }
152 :
153 : static
154 : gboolean
155 0 : fs_date_handler (xmlNodePtr node, gpointer data)
156 : {
157 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
158 0 : auto foo = dom_tree_to_gdate (node);
159 0 : if (!foo)
160 0 : return FALSE;
161 0 : fspd->once_day = *foo;
162 0 : return TRUE;
163 : }
164 :
165 : static
166 : gboolean
167 0 : fs_interval_handler (xmlNodePtr node, gpointer data)
168 : {
169 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
170 : gboolean ret;
171 : gint64 foo;
172 :
173 0 : ret = dom_tree_to_integer (node, &foo);
174 0 : if (! ret)
175 : {
176 0 : return ret;
177 : }
178 0 : fspd->interval = foo;
179 0 : return TRUE;
180 : }
181 :
182 : static
183 : gboolean
184 0 : fs_offset_handler (xmlNodePtr node, gpointer data)
185 : {
186 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
187 : gboolean ret;
188 : gint64 foo;
189 :
190 0 : ret = dom_tree_to_integer (node, &foo);
191 0 : if (! ret)
192 0 : return ret;
193 0 : fspd->offset = foo;
194 0 : return TRUE;
195 : }
196 :
197 : static
198 : gboolean
199 0 : fs_day_handler (xmlNodePtr node, gpointer data)
200 : {
201 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
202 : gboolean ret;
203 : gint64 foo;
204 :
205 0 : ret = dom_tree_to_integer (node, &foo);
206 0 : if (! ret)
207 0 : return ret;
208 0 : fspd->day = foo;
209 0 : return TRUE;
210 : }
211 :
212 : static
213 : gboolean
214 0 : fs_weekday_handler (xmlNodePtr node, gpointer data)
215 : {
216 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
217 : gboolean ret;
218 : gint64 foo;
219 0 : ret = dom_tree_to_integer (node, &foo);
220 0 : if (!ret)
221 0 : return ret;
222 0 : fspd->day = foo;
223 0 : return TRUE;
224 : }
225 :
226 : static
227 : gboolean
228 0 : fs_occurrence_handler (xmlNodePtr node, gpointer data)
229 : {
230 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
231 : gboolean ret;
232 : gint64 foo;
233 0 : ret = dom_tree_to_integer (node, &foo);
234 0 : if (!ret)
235 0 : return ret;
236 0 : fspd->occurrence = foo;
237 0 : return TRUE;
238 : }
239 :
240 : static
241 : gboolean
242 0 : fs_weekend_adj_handler (xmlNodePtr node, gpointer data)
243 : {
244 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
245 : gboolean ret;
246 : gint64 foo;
247 0 : ret = dom_tree_to_integer (node, &foo);
248 0 : if (!ret)
249 0 : return ret;
250 0 : fspd->weekend_adj = foo;
251 0 : return TRUE;
252 : }
253 :
254 : static
255 : gboolean
256 0 : fs_subelement_handler (xmlNodePtr node, gpointer data)
257 : {
258 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
259 : GList* recurrences;
260 :
261 0 : recurrences = dom_tree_freqSpec_to_recurrences (node, fspd->book);
262 0 : if (recurrences == NULL)
263 0 : return FALSE;
264 :
265 : {
266 : GList* r_iter;
267 0 : for (r_iter = recurrences; r_iter != NULL; r_iter = r_iter->next)
268 : {
269 0 : Recurrence* r = (Recurrence*)r_iter->data;
270 : GDate recurrence_date;
271 0 : if (fspd->uift == UIFREQ_SEMI_MONTHLY)
272 : {
273 : // complementry hack around 'once' freqspects not being valid. :/
274 0 : recurrence_date = recurrenceGetDate (r);
275 0 : recurrenceSet (r, recurrenceGetMultiplier (r), PERIOD_MONTH, &recurrence_date,
276 : recurrenceGetWeekendAdjust (r));
277 : }
278 0 : fspd->recurrence_list = g_list_append (fspd->recurrence_list, r);
279 : }
280 : }
281 0 : return TRUE;
282 : }
283 :
284 : struct dom_tree_handler fs_union_dom_handlers[] =
285 : {
286 : { "fs:date", fs_date_handler, 0, 0 },
287 : { "fs:interval", fs_interval_handler, 0, 0 },
288 : { "fs:offset", fs_offset_handler, 0, 0 },
289 : { "fs:day", fs_day_handler, 0, 0 },
290 : { "fs:weekday", fs_weekday_handler, 0, 0 },
291 : { "fs:occurrence", fs_occurrence_handler, 0, 0 },
292 : { "fs:weekend_adj", fs_weekend_adj_handler, 0, 0 },
293 : { "gnc:freqspec", fs_subelement_handler, 0, 0 },
294 : { NULL, NULL, 0, 0 },
295 : };
296 :
297 : static gboolean
298 0 : fs_none_handler (xmlNodePtr node, gpointer data)
299 : {
300 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
301 : gboolean successful;
302 0 : successful = dom_tree_generic_parse (node,
303 : fs_union_dom_handlers,
304 : fspd);
305 0 : return successful;
306 : }
307 :
308 : static
309 : gboolean
310 0 : fs_once_handler (xmlNodePtr node, gpointer data)
311 : {
312 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
313 : gboolean successful;
314 :
315 0 : successful = dom_tree_generic_parse (node,
316 : fs_union_dom_handlers,
317 : fspd);
318 0 : if (!successful)
319 0 : return FALSE;
320 0 : recurrenceSet (fspd->recurrence, 0, PERIOD_ONCE, &fspd->once_day,
321 : WEEKEND_ADJ_NONE);
322 :
323 0 : return TRUE;
324 : }
325 :
326 : static gboolean
327 0 : fs_daily_handler (xmlNodePtr node, gpointer data)
328 : {
329 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
330 : GDate offset_date;
331 : gboolean successful;
332 0 : successful = dom_tree_generic_parse (node, fs_union_dom_handlers, fspd);
333 0 : if (!successful)
334 0 : return FALSE;
335 :
336 0 : g_date_clear (&offset_date, 1);
337 0 : g_date_set_julian (&offset_date, fspd->offset == 0 ? 7 : fspd->offset);
338 0 : recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_DAY, &offset_date,
339 : WEEKEND_ADJ_NONE);
340 :
341 0 : return TRUE;
342 : }
343 :
344 : static
345 : gboolean
346 0 : fs_weekly_handler (xmlNodePtr node, gpointer data)
347 : {
348 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
349 : GDate offset_date;
350 : gboolean successful;
351 0 : successful = dom_tree_generic_parse (node,
352 : fs_union_dom_handlers,
353 : fspd);
354 0 : if (!successful)
355 0 : return FALSE;
356 :
357 0 : g_date_clear (&offset_date, 1);
358 0 : g_date_set_julian (&offset_date, fspd->offset == 0 ? 7 : fspd->offset);
359 0 : recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_WEEK, &offset_date,
360 : WEEKEND_ADJ_NONE);
361 :
362 0 : return TRUE;
363 : }
364 :
365 : static
366 : gboolean
367 0 : fs_monthly_handler (xmlNodePtr node, gpointer data)
368 : {
369 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
370 : GDate offset_date;
371 : gboolean successful;
372 0 : successful = dom_tree_generic_parse (node,
373 : fs_union_dom_handlers,
374 : fspd);
375 0 : if (!successful)
376 0 : return FALSE;
377 :
378 :
379 0 : g_date_clear (&offset_date, 1);
380 0 : g_date_set_julian (&offset_date, 1);
381 0 : g_date_add_months (&offset_date, fspd->offset);
382 0 : g_date_set_day (&offset_date, fspd->day);
383 0 : if (fspd->uift == UIFREQ_ONCE)
384 : {
385 : // hack...
386 0 : recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_ONCE, &offset_date,
387 : WEEKEND_ADJ_NONE);
388 : }
389 : else
390 : {
391 0 : recurrenceSet (fspd->recurrence, fspd->interval,
392 : PERIOD_MONTH, &offset_date,
393 0 : static_cast<WeekendAdjust> (fspd->weekend_adj));
394 : }
395 :
396 0 : return successful;
397 : }
398 :
399 : static
400 : gboolean
401 0 : fs_month_relative_handler (xmlNodePtr node, gpointer data)
402 : {
403 0 : g_critical ("this was never supported, how is it in the datafile?");
404 0 : return FALSE;
405 : }
406 :
407 : static
408 : gboolean
409 0 : fs_guid_handler (xmlNodePtr node, gpointer data)
410 : {
411 0 : return TRUE;
412 : }
413 :
414 : static
415 : gboolean
416 0 : fs_composite_handler (xmlNodePtr node, gpointer data)
417 : {
418 0 : fsParseData* fspd = static_cast<decltype (fspd)> (data);
419 : gboolean successful;
420 0 : successful = dom_tree_generic_parse (node,
421 : fs_union_dom_handlers,
422 : fspd);
423 0 : return successful;
424 : }
425 :
426 : static struct dom_tree_handler fs_dom_handlers[] =
427 : {
428 : { "gnc:freqspec", gnc_fs_handler, 0, 0 },
429 : { "fs:ui_type", fs_uift_handler, 1, 0 },
430 : { "fs:id", fs_guid_handler, 1, 0 },
431 : { "fs:none", fs_none_handler, 0, 0 },
432 : { "fs:once", fs_once_handler, 0, 0 },
433 : { "fs:daily", fs_daily_handler, 0, 0 },
434 : { "fs:weekly", fs_weekly_handler, 0, 0 },
435 : { "fs:monthly", fs_monthly_handler, 0, 0 },
436 : { "fs:month_relative", fs_month_relative_handler, 0, 0 },
437 : { "fs:composite", fs_composite_handler, 0, 0 },
438 : { NULL, NULL, 0, 0 }
439 : };
440 :
441 : static
442 : gboolean
443 0 : gnc_fs_handler (xmlNodePtr node, gpointer d)
444 : {
445 0 : return dom_tree_generic_parse (node, fs_dom_handlers, d);
446 : }
447 :
448 : static
449 : gboolean
450 0 : gnc_freqSpec_end_handler (gpointer data_for_children,
451 : GSList* data_from_children, GSList* sibling_data,
452 : gpointer parent_data, gpointer global_data,
453 : gpointer* result, const gchar* tag)
454 : {
455 : fsParseData fspd;
456 0 : gboolean successful = FALSE;
457 0 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
458 0 : sixtp_gdv2* globaldata = (sixtp_gdv2*)global_data;
459 :
460 0 : fspd_init (&fspd);
461 0 : fspd.book = globaldata->book;
462 :
463 : /* this won't actually get invoked [FreqSpecs aren't top-level
464 : elements]; see dom_tree_to_freqSpec(), below. */
465 0 : if (parent_data)
466 0 : return TRUE;
467 :
468 0 : if (!tag)
469 0 : return TRUE;
470 :
471 0 : g_return_val_if_fail (tree, FALSE);
472 :
473 0 : successful = dom_tree_generic_parse (tree, fs_dom_handlers, &fspd);
474 0 : if (!successful)
475 : {
476 0 : xmlElemDump (stdout, NULL, tree);
477 : }
478 :
479 0 : xmlFreeNode (tree);
480 :
481 0 : return successful;
482 : }
483 :
484 : sixtp*
485 0 : gnc_freqSpec_sixtp_parser_create (void)
486 : {
487 0 : return sixtp_dom_parser_new (gnc_freqSpec_end_handler, NULL, NULL);
488 : }
489 :
490 : static void
491 0 : common_parse (fsParseData* fspd, xmlNodePtr node, QofBook* book)
492 : {
493 : gboolean successful;
494 :
495 0 : fspd->book = book;
496 0 : successful = dom_tree_generic_parse (node, fs_dom_handlers, fspd);
497 0 : if (!successful)
498 : {
499 0 : xmlElemDump (stdout, NULL, node);
500 : }
501 0 : }
502 :
503 : GList*
504 0 : dom_tree_freqSpec_to_recurrences (xmlNodePtr node, QofBook* book)
505 : {
506 : fsParseData fspd;
507 0 : fspd_init (&fspd);
508 0 : common_parse (&fspd, node, book);
509 0 : if (fspd.recurrence_list == NULL)
510 : {
511 0 : fspd.recurrence_list = g_list_append (fspd.recurrence_list, fspd.recurrence);
512 : }
513 0 : return fspd.recurrence_list;
514 : }
|