GnuCash  5.6-150-g038405b370+
gnc-budget-xml-v2.cpp
1 /*
2  * gnc-budget-xml-v2.c -- budget xml i/o implementation
3  *
4  * Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
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 #include <glib.h>
24 
25 #include <config.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "gnc-xml-helper.h"
30 #include "sixtp.h"
31 #include "sixtp-utils.h"
32 #include "sixtp-parsers.h"
33 #include "sixtp-utils.h"
34 #include "sixtp-dom-parsers.h"
35 #include "sixtp-dom-generators.h"
36 
37 #include "gnc-xml.h"
38 #include "io-gncxml-gen.h"
39 #include "io-gncxml-v2.h"
40 
41 static QofLogModule log_module = GNC_MOD_IO;
42 
43 const gchar* budget_version_string = "2.0.0";
44 
45 /* ids */
46 #define gnc_budget_string "gnc:budget"
47 #define bgt_id_string "bgt:id"
48 #define bgt_name_string "bgt:name"
49 #define bgt_description_string "bgt:description"
50 #define bgt_num_periods_string "bgt:num-periods"
51 #define bgt_recurrence_string "bgt:recurrence"
52 #define bgt_slots_string "bgt:slots"
53 
54 xmlNodePtr
55 gnc_budget_dom_tree_create (GncBudget* bgt)
56 {
57  xmlNodePtr ret;
58 
59  ENTER ("(budget=%p)", bgt);
60 
61  ret = xmlNewNode (NULL, BAD_CAST gnc_budget_string);
62  xmlSetProp (ret, BAD_CAST "version", BAD_CAST budget_version_string);
63 
64  /* field: GncGUID */
65  xmlAddChild (ret, guid_to_dom_tree (bgt_id_string,
66  gnc_budget_get_guid (bgt)));
67  /* field: char* name */
68  xmlAddChild (ret, text_to_dom_tree (bgt_name_string,
69  gnc_budget_get_name (bgt)));
70  /* field: char* description */
71  xmlAddChild (ret, text_to_dom_tree (bgt_description_string,
72  gnc_budget_get_description (bgt)));
73  /* field: guint num_periods */
74  xmlAddChild (ret, guint_to_dom_tree (bgt_num_periods_string,
75  gnc_budget_get_num_periods (bgt)));
76  /* field: Recurrence* */
77  xmlAddChild (ret, recurrence_to_dom_tree (bgt_recurrence_string,
78  gnc_budget_get_recurrence (bgt)));
79  /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
80  xmlAddChild (ret, qof_instance_slots_to_dom_tree (bgt_slots_string,
81  QOF_INSTANCE (bgt)));
82 
83  LEAVE (" ");
84  return ret;
85 }
86 
87 /***********************************************************************/
88 static inline gboolean
89 set_string (xmlNodePtr node, GncBudget* bgt,
90  void (*func) (GncBudget* bgt, const gchar* txt))
91 {
92  gchar* txt = dom_tree_to_text (node);
93  g_return_val_if_fail (txt, FALSE);
94 
95  func (bgt, txt);
96  g_free (txt);
97  return TRUE;
98 }
99 
100 static gboolean
101 budget_id_handler (xmlNodePtr node, gpointer bgt)
102 {
103  GncGUID* guid;
104 
105  guid = dom_tree_to_guid (node);
106  g_return_val_if_fail (guid, FALSE);
107  qof_instance_set_guid (QOF_INSTANCE (bgt), guid);
108  guid_free (guid);
109  return TRUE;
110 }
111 
112 static gboolean
113 budget_name_handler (xmlNodePtr node, gpointer bgt)
114 {
115  return set_string (node, GNC_BUDGET (bgt), gnc_budget_set_name);
116 }
117 
118 static gboolean
119 budget_description_handler (xmlNodePtr node, gpointer bgt)
120 {
121  return set_string (node, GNC_BUDGET (bgt), gnc_budget_set_description);
122 }
123 
124 static gboolean
125 budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
126 {
127  guint num_periods;
128 
129  if (dom_tree_to_guint (node, &num_periods))
130  {
131  gnc_budget_set_num_periods (GNC_BUDGET (bgt), num_periods);
132  return TRUE;
133  }
134  else
135  return FALSE;
136 }
137 
138 static gboolean
139 budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
140 {
141  Recurrence* r;
142 
143  if ((r = dom_tree_to_recurrence (node)) == NULL)
144  return FALSE;
145 
146  gnc_budget_set_recurrence (GNC_BUDGET (bgt), r);
147  g_free (r);
148  return TRUE;
149 }
150 
151 static gboolean
152 budget_slots_handler (xmlNodePtr node, gpointer bgt)
153 {
154  return dom_tree_create_instance_slots (node, QOF_INSTANCE (bgt));
155 }
156 
157 static struct dom_tree_handler budget_handlers[] =
158 {
159  { bgt_id_string, budget_id_handler, 1, 0 },
160  { bgt_name_string, budget_name_handler, 0, 0 },
161  { bgt_description_string, budget_description_handler, 0, 0 },
162  { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
163  { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
164  { bgt_slots_string, budget_slots_handler, 0, 0},
165  { NULL, 0, 0, 0 }
166 };
167 
168 static gboolean
169 gnc_budget_end_handler (gpointer data_for_children,
170  GSList* data_from_children, GSList* sibling_data,
171  gpointer parent_data, gpointer global_data,
172  gpointer* result, const gchar* tag)
173 {
174  GncBudget* bgt;
175  xmlNodePtr tree = (xmlNodePtr)data_for_children;
176  gxpf_data* gdata = (gxpf_data*)global_data;
177  QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
178 
179  if (parent_data)
180  {
181  return TRUE;
182  }
183 
184  /* OK. For some messed up reason this is getting called again with a
185  NULL tag. So we ignore those cases */
186  if (!tag)
187  {
188  return TRUE;
189  }
190 
191  g_return_val_if_fail (tree, FALSE);
192 
193  bgt = dom_tree_to_budget (tree, book);
194  xmlFreeNode (tree);
195  if (bgt != NULL)
196  {
197  /* ends up calling book_callback */
198  gdata->cb (tag, gdata->parsedata, bgt);
199  }
200 
201  return bgt != NULL;
202 }
203 
204 
205 GncBudget*
206 dom_tree_to_budget (xmlNodePtr node, QofBook* book)
207 {
208  GncBudget* bgt;
209 
210  bgt = gnc_budget_new (book);
211  if (!dom_tree_generic_parse (node, budget_handlers, bgt))
212  {
213  PERR ("failed to parse budget tree");
214  gnc_budget_destroy (bgt);
215  bgt = NULL;
216  }
217  return bgt;
218 }
219 
220 sixtp*
221 gnc_budget_sixtp_parser_create (void)
222 {
223  return sixtp_dom_parser_new (gnc_budget_end_handler, NULL, NULL);
224 }
225 /* ====================== END OF FILE ===================*/
void gnc_budget_set_num_periods(GncBudget *budget, guint num_periods)
Set/Get the number of periods in the Budget.
Definition: gnc-budget.cpp:467
void gnc_budget_destroy(GncBudget *budget)
Deletes the given budget object.
Definition: gnc-budget.cpp:321
Definition: sixtp.h:129
GncBudget * gnc_budget_new(QofBook *book)
Creates and initializes a Budget.
Definition: gnc-budget.cpp:305
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
api for GnuCash version 2 XML-based file format
void gnc_budget_set_name(GncBudget *budget, const gchar *name)
Set/Get the name of the Budget.
Definition: gnc-budget.cpp:386
void gnc_budget_set_description(GncBudget *budget, const gchar *description)
Set/Get the description of the Budget.
Definition: gnc-budget.cpp:411
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
The type used to store guids in C.
Definition: guid.h:75