GnuCash c935c2f+
Loading...
Searching...
No Matches
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
41static QofLogModule log_module = GNC_MOD_IO;
42
43const 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
54xmlNodePtr
55gnc_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
89static gboolean
90budget_id_handler (xmlNodePtr node, gpointer bgt)
91{
92 auto guid = dom_tree_to_guid (node);
93 g_return_val_if_fail (guid, FALSE);
94 qof_instance_set_guid (QOF_INSTANCE (bgt), &*guid);
95 return TRUE;
96}
97
98static gboolean
99budget_name_handler (xmlNodePtr node, gpointer bgt)
100{
101 return apply_xmlnode_text (gnc_budget_set_name, GNC_BUDGET (bgt), node);
102}
103
104static gboolean
105budget_description_handler (xmlNodePtr node, gpointer bgt)
106{
107 return apply_xmlnode_text (gnc_budget_set_description, GNC_BUDGET (bgt), node);
108}
109
110static gboolean
111budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
112{
113 guint num_periods;
114
115 if (dom_tree_to_guint (node, &num_periods))
116 {
117 gnc_budget_set_num_periods (GNC_BUDGET (bgt), num_periods);
118 return TRUE;
119 }
120 else
121 return FALSE;
122}
123
124static gboolean
125budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
126{
127 Recurrence* r;
128
129 if ((r = dom_tree_to_recurrence (node)) == NULL)
130 return FALSE;
131
132 gnc_budget_set_recurrence (GNC_BUDGET (bgt), r);
133 g_free (r);
134 return TRUE;
135}
136
137static gboolean
138budget_slots_handler (xmlNodePtr node, gpointer bgt)
139{
140 return dom_tree_create_instance_slots (node, QOF_INSTANCE (bgt));
141}
142
143static struct dom_tree_handler budget_handlers[] =
144{
145 { bgt_id_string, budget_id_handler, 1, 0 },
146 { bgt_name_string, budget_name_handler, 0, 0 },
147 { bgt_description_string, budget_description_handler, 0, 0 },
148 { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
149 { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
150 { bgt_slots_string, budget_slots_handler, 0, 0},
151 { NULL, 0, 0, 0 }
152};
153
154static gboolean
155gnc_budget_end_handler (gpointer data_for_children,
156 GSList* data_from_children, GSList* sibling_data,
157 gpointer parent_data, gpointer global_data,
158 gpointer* result, const gchar* tag)
159{
160 GncBudget* bgt;
161 xmlNodePtr tree = (xmlNodePtr)data_for_children;
162 gxpf_data* gdata = (gxpf_data*)global_data;
163 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
164
165 if (parent_data)
166 {
167 return TRUE;
168 }
169
170 /* OK. For some messed up reason this is getting called again with a
171 NULL tag. So we ignore those cases */
172 if (!tag)
173 {
174 return TRUE;
175 }
176
177 g_return_val_if_fail (tree, FALSE);
178
179 bgt = dom_tree_to_budget (tree, book);
180 xmlFreeNode (tree);
181 if (bgt != NULL)
182 {
183 /* ends up calling book_callback */
184 gdata->cb (tag, gdata->parsedata, bgt);
185 }
186
187 return bgt != NULL;
188}
189
190
191GncBudget*
192dom_tree_to_budget (xmlNodePtr node, QofBook* book)
193{
194 GncBudget* bgt;
195
196 bgt = gnc_budget_new (book);
197 if (!dom_tree_generic_parse (node, budget_handlers, bgt))
198 {
199 PERR ("failed to parse budget tree");
200 gnc_budget_destroy (bgt);
201 bgt = NULL;
202 }
203 return bgt;
204}
205
206sixtp*
207gnc_budget_sixtp_parser_create (void)
208{
209 return sixtp_dom_parser_new (gnc_budget_end_handler, NULL, NULL);
210}
211/* ====================== END OF FILE ===================*/
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition qoflog.h:282
#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
void gnc_budget_set_num_periods(GncBudget *budget, guint num_periods)
Set/Get the number of periods in the Budget.
GncBudget * gnc_budget_new(QofBook *book)
Creates and initializes a Budget.
void gnc_budget_destroy(GncBudget *budget)
Deletes the given budget object.
void gnc_budget_set_description(GncBudget *budget, const gchar *description)
Set/Get the description of the Budget.
void gnc_budget_set_name(GncBudget *budget, const gchar *name)
Set/Get the name of the Budget.
api for GnuCash version 2 XML-based file format
QofBook reference.
Definition qofbook-p.hpp:47
Definition sixtp.h:130