GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-budget-xml-v2.c
1 /*
2  * gnc-budget-xml-v2.c -- budget xml i/o implementation
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 <stdlib.h>
29 #include <string.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 static QofLogModule log_module = GNC_MOD_IO;
44 
45 const gchar *budget_version_string = "2.0.0";
46 
47 /* ids */
48 #define gnc_budget_string "gnc:budget"
49 #define bgt_id_string "bgt:id"
50 #define bgt_name_string "bgt:name"
51 #define bgt_description_string "bgt:description"
52 #define bgt_num_periods_string "bgt:num-periods"
53 #define bgt_recurrence_string "bgt:recurrence"
54 #define bgt_slots_string "bgt:slots"
55 
56 xmlNodePtr
57 gnc_budget_dom_tree_create(GncBudget *bgt)
58 {
59  xmlNodePtr ret;
60  KvpFrame *kf;
61 
62  ENTER ("(budget=%p)", bgt);
63 
64  ret = xmlNewNode(NULL, BAD_CAST gnc_budget_string);
65  xmlSetProp(ret, BAD_CAST "version", BAD_CAST budget_version_string);
66 
67  /* field: GncGUID */
68  xmlAddChild(ret, guid_to_dom_tree(bgt_id_string,
69  gnc_budget_get_guid(bgt)));
70  /* field: char* name */
71  xmlAddChild(ret, text_to_dom_tree(bgt_name_string,
72  gnc_budget_get_name(bgt)));
73  /* field: char* description */
74  xmlAddChild(ret, text_to_dom_tree(bgt_description_string,
75  gnc_budget_get_description(bgt)));
76  /* field: guint num_periods */
77  xmlAddChild(ret, guint_to_dom_tree(bgt_num_periods_string,
78  gnc_budget_get_num_periods(bgt)));
79  /* field: Recurrence* */
80  xmlAddChild(ret, recurrence_to_dom_tree(bgt_recurrence_string,
81  gnc_budget_get_recurrence(bgt)));
82  /* slots */
83  kf = qof_instance_get_slots(QOF_INSTANCE(bgt));
84  if (kf)
85  {
86  xmlNodePtr kvpnode = kvp_frame_to_dom_tree(bgt_slots_string, kf);
87  if (kvpnode)
88  xmlAddChild(ret, kvpnode);
89  }
90 
91  LEAVE (" ");
92  return ret;
93 }
94 
95 /***********************************************************************/
96 static inline gboolean
97 set_string(xmlNodePtr node, GncBudget* bgt,
98  void (*func)(GncBudget *bgt, const gchar *txt))
99 {
100  gchar* txt = dom_tree_to_text(node);
101  g_return_val_if_fail(txt, FALSE);
102 
103  func(bgt, txt);
104  g_free(txt);
105  return TRUE;
106 }
107 
108 static gboolean
109 budget_id_handler (xmlNodePtr node, gpointer bgt)
110 {
111  GncGUID *guid;
112 
113  guid = dom_tree_to_guid(node);
114  g_return_val_if_fail(guid, FALSE);
115  qof_instance_set_guid(QOF_INSTANCE(bgt), guid);
116  g_free(guid);
117  return TRUE;
118 }
119 
120 static gboolean
121 budget_name_handler (xmlNodePtr node, gpointer bgt)
122 {
123  return set_string(node, GNC_BUDGET(bgt), gnc_budget_set_name);
124 }
125 
126 static gboolean
127 budget_description_handler (xmlNodePtr node, gpointer bgt)
128 {
129  return set_string(node, GNC_BUDGET(bgt), gnc_budget_set_description);
130 }
131 
132 static gboolean
133 budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
134 {
135  guint num_periods;
136 
137  if (dom_tree_to_guint(node, &num_periods))
138  {
139  gnc_budget_set_num_periods(GNC_BUDGET(bgt), num_periods);
140  return TRUE;
141  }
142  else
143  return FALSE;
144 }
145 
146 static gboolean
147 budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
148 {
149  Recurrence *r;
150 
151  if ((r = dom_tree_to_recurrence(node)) == NULL)
152  return FALSE;
153 
154  gnc_budget_set_recurrence(GNC_BUDGET(bgt), r);
155  g_free(r);
156  return TRUE;
157 }
158 
159 static gboolean
160 budget_slots_handler (xmlNodePtr node, gpointer bgt)
161 {
162  return dom_tree_to_kvp_frame_given(
163  node, qof_instance_get_slots(QOF_INSTANCE(bgt)));
164 }
165 
166 static struct dom_tree_handler budget_handlers[] =
167 {
168  { bgt_id_string, budget_id_handler, 1, 0 },
169  { bgt_name_string, budget_name_handler, 0, 0 },
170  { bgt_description_string, budget_description_handler, 0, 0 },
171  { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
172  { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
173  { bgt_slots_string, budget_slots_handler, 0, 0},
174  { NULL, 0, 0, 0 }
175 };
176 
177 static gboolean
178 gnc_budget_end_handler(gpointer data_for_children,
179  GSList* data_from_children, GSList* sibling_data,
180  gpointer parent_data, gpointer global_data,
181  gpointer *result, const gchar *tag)
182 {
183  GncBudget *bgt;
184  xmlNodePtr tree = (xmlNodePtr)data_for_children;
185  gxpf_data *gdata = (gxpf_data*)global_data;
186  QofBook *book = gdata->bookdata;
187 
188  if (parent_data)
189  {
190  return TRUE;
191  }
192 
193  /* OK. For some messed up reason this is getting called again with a
194  NULL tag. So we ignore those cases */
195  if (!tag)
196  {
197  return TRUE;
198  }
199 
200  g_return_val_if_fail(tree, FALSE);
201 
202  bgt = dom_tree_to_budget(tree, book);
203  xmlFreeNode(tree);
204  if (bgt != NULL)
205  {
206  /* ends up calling book_callback */
207  gdata->cb(tag, gdata->parsedata, bgt);
208  }
209 
210  return bgt != NULL;
211 }
212 
213 
214 GncBudget*
215 dom_tree_to_budget (xmlNodePtr node, QofBook *book)
216 {
217  GncBudget *bgt;
218 
219  bgt = gnc_budget_new(book);
220  if (!dom_tree_generic_parse (node, budget_handlers, bgt))
221  {
222  PERR ("failed to parse budget tree");
223  gnc_budget_destroy(bgt);
224  bgt = NULL;
225  }
226  return bgt;
227 }
228 
229 sixtp*
230 gnc_budget_sixtp_parser_create(void)
231 {
232  return sixtp_dom_parser_new(gnc_budget_end_handler, NULL, NULL);
233 }
234 /* ====================== END OF FILE ===================*/
void gnc_budget_set_num_periods(GncBudget *budget, guint num_periods)
Definition: gnc-budget.c:452
void gnc_budget_destroy(GncBudget *budget)
Definition: gnc-budget.c:306
Definition: sixtp.h:93
GncBudget * gnc_budget_new(QofBook *book)
Definition: gnc-budget.c:289
#define PERR(format, args...)
Definition: qoflog.h:237
#define ENTER(format, args...)
Definition: qoflog.h:261
Definition: guid.h:65
api for GnuCash version 2 XML-based file format
void gnc_budget_set_name(GncBudget *budget, const gchar *name)
Definition: gnc-budget.c:371
void gnc_budget_set_description(GncBudget *budget, const gchar *description)
Definition: gnc-budget.c:396
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
#define LEAVE(format, args...)
Definition: qoflog.h:271
const gchar * QofLogModule
Definition: qofid.h:89