GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-recurrence-xml-v2.c
1 /*
2  * gnc-recurrence-xml-v2.c -- xml routines for Recurrence
3  *
4  * Copyright (C) 2005 Chris Shoemaker <[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 <string.h>
29 
30 #include "gnc-xml.h"
31 #include "gnc-xml-helper.h"
32 #include "qof.h"
33 
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 #include "io-gncxml-v2.h"
41 #include "Recurrence.h"
42 
43 static QofLogModule log_module = GNC_MOD_IO;
44 
45 const gchar *recurrence_version_string = "1.0.0";
46 #define recurrence_root "gnc:recurrence"
47 #define recurrence_mult "recurrence:mult"
48 #define recurrence_period_type "recurrence:period_type"
49 #define recurrence_start "recurrence:start"
50 #define recurrence_weekend_adj "recurrence:weekend_adj"
51 
52 //TODO: I think three of these functions rightly belong in Recurrence.c.
53 
54 static gboolean
55 recurrence_period_type_handler(xmlNodePtr node, gpointer d)
56 {
57  PeriodType pt;
58  char *nodeTxt;
59 
60  nodeTxt = dom_tree_to_text(node);
61  g_return_val_if_fail(nodeTxt, FALSE);
62  pt = recurrencePeriodTypeFromString(nodeTxt);
63  ((Recurrence *) d)->ptype = pt;
64  g_free(nodeTxt);
65  return (pt != -1);
66 }
67 
68 static gboolean
69 recurrence_start_date_handler(xmlNodePtr node, gpointer r)
70 {
71  GDate *d;
72 
73  d = dom_tree_to_gdate(node);
74  g_return_val_if_fail(d, FALSE);
75  g_return_val_if_fail(g_date_valid(d), FALSE);
76  ((Recurrence *) r)->start = *d;
77  g_date_free(d);
78  return TRUE;
79 }
80 
81 static gboolean
82 recurrence_mult_handler(xmlNodePtr node, gpointer r)
83 {
84  return dom_tree_to_guint16(node, &((Recurrence *)r)->mult);
85 }
86 
87 static gboolean
88 recurrence_weekend_adj_handler(xmlNodePtr node, gpointer d)
89 {
90  WeekendAdjust wadj;
91  char *nodeTxt;
92 
93  nodeTxt = dom_tree_to_text(node);
94  g_return_val_if_fail(nodeTxt, FALSE);
95  wadj = recurrenceWeekendAdjustFromString(nodeTxt);
96  ((Recurrence *) d)->wadj = wadj;
97  g_free(nodeTxt);
98  return (wadj != -1);
99 }
100 
101 static struct dom_tree_handler recurrence_dom_handlers[] =
102 {
103  { recurrence_mult, recurrence_mult_handler, 1, 0 },
104  { recurrence_period_type, recurrence_period_type_handler, 1, 0 },
105  { recurrence_start, recurrence_start_date_handler, 1, 0 },
106  { recurrence_weekend_adj, recurrence_weekend_adj_handler, 0, 0 },
107  { NULL, NULL, 0, 0 }
108 };
109 
110 Recurrence*
111 dom_tree_to_recurrence(xmlNodePtr node)
112 {
113  gboolean successful;
114  Recurrence *r;
115 
116  r = g_new(Recurrence, 1);
117  /* In case the file doesn't have a weekend adjustment element */
118  r->wadj = WEEKEND_ADJ_NONE;
119  successful = dom_tree_generic_parse (node, recurrence_dom_handlers, r);
120  if (!successful)
121  {
122  PERR ("failed to parse recurrence node");
123  xmlElemDump(stdout, NULL, node);
124  g_free(r);
125  r = NULL;
126  }
127  return r;
128 }
129 
130 xmlNodePtr
131 recurrence_to_dom_tree(const gchar *tag, const Recurrence *r)
132 {
133  xmlNodePtr n;
134  PeriodType pt;
135  GDate d;
136  WeekendAdjust wadj;
137 
138  n = xmlNewNode(NULL, BAD_CAST tag);
139  xmlSetProp(n, BAD_CAST "version", BAD_CAST recurrence_version_string);
140  xmlAddChild(n, guint_to_dom_tree(recurrence_mult,
141  recurrenceGetMultiplier(r)));
142  pt = recurrenceGetPeriodType(r);
143  xmlAddChild(n, text_to_dom_tree(recurrence_period_type,
144  recurrencePeriodTypeToString(pt)));
145  d = recurrenceGetDate(r);
146  xmlAddChild(n, gdate_to_dom_tree(recurrence_start, &d));
147  wadj = recurrenceGetWeekendAdjust(r);
148  if (wadj != WEEKEND_ADJ_NONE)
149  {
150  /* In r17725 and r17751, I introduced this extra XML child
151  element, but this means a gnucash-2.2.x cannot read the SX
152  recurrence of a >=2.3.x file anymore, which is bad. In order
153  to improve this broken backward compatibility for most of the
154  cases, we don't write out this XML element as long as it is
155  only "none". */
156  xmlAddChild(n, text_to_dom_tree(recurrence_weekend_adj,
157  recurrenceWeekendAdjustToString(wadj)));
158  }
159  return n;
160 }
#define PERR(format, args...)
Definition: qoflog.h:237
api for GnuCash version 2 XML-based file format
const gchar * QofLogModule
Definition: qofid.h:89