GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-param.c
1 /*
2  * search-param.c -- a container for a Search Parameter
3  * Copyright (C) 2002 Derek Atkins <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, contact:
17  *
18  * Free Software Foundation Voice: +1-617-542-5942
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
20  * Boston, MA 02110-1301, USA [email protected]
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <gtk/gtk.h>
28 #include <string.h>
29 #include <stdarg.h>
30 
31 #include "gnc-engine.h"
32 #include "qof.h"
33 
34 #include "search-param.h"
35 
36 static void gnc_search_param_class_init (GNCSearchParamClass *klass);
37 static void gnc_search_param_init (GNCSearchParam *gspaper);
38 static void gnc_search_param_finalize (GObject *obj);
39 
41 
43 {
44  GSList * converters;
45  GSList * param_path;
46  QofIdTypeConst type;
47 
48  GNCSearchParamFcn lookup_fcn;
49  gpointer lookup_arg;
50 };
51 
52 #define GNC_SEARCH_PARAM_GET_PRIVATE(o) \
53  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_PARAM, GNCSearchParamPrivate))
54 
55 static GObjectClass *parent_class;
56 
57 enum
58 {
59  LAST_SIGNAL
60 };
61 
62 #if LAST_SIGNAL > 0
63 static guint signals[LAST_SIGNAL] = { 0 };
64 #endif
65 
66 GType
67 gnc_search_param_get_type (void)
68 {
69  static GType type = 0;
70 
71  if (type == 0)
72  {
73  static GTypeInfo type_info =
74  {
75  sizeof(GNCSearchParamClass),
76  NULL,
77  NULL,
78  (GClassInitFunc)gnc_search_param_class_init,
79  NULL,
80  NULL,
81  sizeof(GNCSearchParam),
82  0,
83  (GInstanceInitFunc)gnc_search_param_init
84  };
85 
86  type = g_type_register_static (G_TYPE_OBJECT, "GNCSearchParam",
87  &type_info, 0);
88  }
89 
90  return type;
91 }
92 
93 static void
94 gnc_search_param_class_init (GNCSearchParamClass *klass)
95 {
96  GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 
98  parent_class = g_type_class_peek_parent (klass);
99 
100  object_class->finalize = gnc_search_param_finalize;
101 
102  g_type_class_add_private(klass, sizeof(GNCSearchParamPrivate));
103 }
104 
105 static void
106 gnc_search_param_init (GNCSearchParam *o)
107 {
108 }
109 
110 static void
111 gnc_search_param_finalize (GObject *obj)
112 {
113  GNCSearchParam *o;
114  GNCSearchParamPrivate *priv;
115 
116  g_return_if_fail (obj != NULL);
117  g_return_if_fail (GNC_IS_SEARCH_PARAM (obj));
118 
119  o = GNC_SEARCH_PARAM (obj);
120  priv = GNC_SEARCH_PARAM_GET_PRIVATE(o);
121 
122  g_slist_free (priv->param_path);
123  priv->param_path = NULL;
124  g_slist_free (priv->converters);
125  priv->converters = NULL;
126 
127  G_OBJECT_CLASS (parent_class)->finalize(obj);
128 }
129 
138 gnc_search_param_new (void)
139 {
140  GNCSearchParam *o = (GNCSearchParam *)g_object_new(gnc_search_param_get_type (), NULL);
141  return o;
142 }
143 
144 void
145 gnc_search_param_set_param_path (GNCSearchParam *param,
146  QofIdTypeConst search_type,
147  GSList *param_path)
148 {
149  GNCSearchParamPrivate *priv;
150  QofIdTypeConst type = NULL;
151  GSList *converters = NULL;
152 
153  g_return_if_fail (GNC_IS_SEARCH_PARAM (param));
154 
155  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
156  if (priv->param_path)
157  {
158  g_slist_free (priv->param_path);
159  }
160  priv->param_path = g_slist_copy (param_path);
161 
162  /* Compute the parameter type */
163  for (; param_path; param_path = param_path->next)
164  {
165  QofIdType param_name = param_path->data;
166  const QofParam *objDef =
167  qof_class_get_parameter (search_type, param_name);
168 
169  /* If it doesn't exist, then we've reached the end */
170  if (objDef == NULL)
171  break;
172 
173  /* Save the converter */
174  converters = g_slist_prepend (converters, (gpointer) objDef);
175 
176  /* And reset for the next parameter */
177  type = search_type = objDef->param_type;
178  }
179 
180  /* Save the type */
181  priv->type = type;
182 
183  /* Save the converters */
184  if (priv->converters)
185  {
186  g_slist_free (priv->converters);
187  }
188  priv->converters = g_slist_reverse (converters);
189 }
190 
191 void
192 gnc_search_param_override_param_type (GNCSearchParam *param,
193  QofIdTypeConst param_type)
194 {
195  GNCSearchParamPrivate *priv;
196 
197  g_return_if_fail (GNC_IS_SEARCH_PARAM (param));
198  g_return_if_fail (param_type != NULL && *param_type != '\0');
199 
200  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
201  priv->type = param_type;
202  /* XXX: What about the converters? */
203 }
204 
205 GSList *
206 gnc_search_param_get_param_path (GNCSearchParam *param)
207 {
208  GNCSearchParamPrivate *priv;
209 
210  g_return_val_if_fail (GNC_IS_SEARCH_PARAM (param), NULL);
211 
212  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
213  return g_slist_copy (priv->param_path);
214 }
215 
216 GSList *
217 gnc_search_param_get_converters (GNCSearchParam *param)
218 {
219  GNCSearchParamPrivate *priv;
220 
221  g_return_val_if_fail (GNC_IS_SEARCH_PARAM (param), NULL);
222 
223  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
224  return priv->converters;
225 }
226 
228 gnc_search_param_get_param_type (GNCSearchParam *param)
229 {
230  GNCSearchParamPrivate *priv;
231 
232  g_return_val_if_fail (GNC_IS_SEARCH_PARAM (param), NULL);
233 
234  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
235  return priv->type;
236 }
237 
238 void
239 gnc_search_param_set_title (GNCSearchParam *param, const char *title)
240 {
241  g_return_if_fail (GNC_IS_SEARCH_PARAM (param));
242 
243  param->title = title;
244 }
245 
246 void
247 gnc_search_param_set_justify (GNCSearchParam *param, GtkJustification justify)
248 {
249  g_return_if_fail (GNC_IS_SEARCH_PARAM (param));
250 
251  param->justify = justify;
252 }
253 
254 void
255 gnc_search_param_set_passive (GNCSearchParam *param, gboolean value)
256 {
257  g_assert (GNC_IS_SEARCH_PARAM (param));
258 
259  param->passive = value;
260 }
261 
262 void
263 gnc_search_param_set_non_resizeable (GNCSearchParam *param, gboolean value)
264 {
265  g_assert (GNC_IS_SEARCH_PARAM (param));
266 
267  param->non_resizeable = value;
268 }
269 
270 gboolean
271 gnc_search_param_type_match (GNCSearchParam *a, GNCSearchParam *b)
272 {
273  GNCSearchParamPrivate *a_priv, *b_priv;
274 
275  g_return_val_if_fail (GNC_IS_SEARCH_PARAM (a), FALSE);
276  g_return_val_if_fail (GNC_IS_SEARCH_PARAM (b), FALSE);
277 
278  a_priv = GNC_SEARCH_PARAM_GET_PRIVATE(a);
279  b_priv = GNC_SEARCH_PARAM_GET_PRIVATE(b);
280  if (a_priv->type == b_priv->type ||
281  !g_strcmp0 (a_priv->type, b_priv->type))
282  return TRUE;
283 
284  return FALSE;
285 }
286 
287 static GList *
288 gnc_search_param_prepend_internal (GList *list, char const *title,
289  GtkJustification justify,
290  QofIdTypeConst type_override,
291  QofIdTypeConst search_type,
292  const char *param, va_list args)
293 {
294  GNCSearchParam *p;
295  GSList *path = NULL;
296  const char *this_param;
297 
298  p = gnc_search_param_new ();
299  gnc_search_param_set_title (p, title);
300  gnc_search_param_set_justify (p, justify);
301 
302  for (this_param = param; this_param;
303  this_param = va_arg (args, const char *))
304  {
305  path = g_slist_prepend (path, (gpointer)this_param);
306  }
307 
308  /* put the path into the right order, and set it */
309  path = g_slist_reverse (path);
310  gnc_search_param_set_param_path (p, search_type, path);
311 
312  /* Maybe over-ride the type */
313  if (type_override)
314  gnc_search_param_override_param_type (p, type_override);
315 
316  /* And return it */
317  return g_list_prepend (list, p);
318 }
319 
320 
321 GList *
322 gnc_search_param_prepend_with_justify (GList *list, char const *title,
323  GtkJustification justify,
324  QofIdTypeConst type_override,
325  QofIdTypeConst search_type,
326  const char *param, ...)
327 {
328  GList *result;
329  va_list ap;
330 
331  g_return_val_if_fail (title, list);
332  g_return_val_if_fail (search_type, list);
333  g_return_val_if_fail (param, list);
334 
335  /* Build the parameter path */
336  va_start (ap, param);
337  result = gnc_search_param_prepend_internal (list, title, justify,
338  type_override, search_type,
339  param, ap);
340  va_end (ap);
341  return result;
342 }
343 
344 GList *
345 gnc_search_param_prepend (GList *list, char const *title,
346  QofIdTypeConst type_override,
347  QofIdTypeConst search_type,
348  const char *param, ...)
349 {
350  GList *result;
351  va_list ap;
352 
353  g_return_val_if_fail (title, list);
354  g_return_val_if_fail (search_type, list);
355  g_return_val_if_fail (param, list);
356 
357  /* Build the parameter path */
358  va_start (ap, param);
359  result = gnc_search_param_prepend_internal (list, title, GTK_JUSTIFY_LEFT,
360  type_override, search_type,
361  param, ap);
362  va_end (ap);
363  return result;
364 }
365 
366 void
367 gnc_search_param_set_param_fcn (GNCSearchParam *param,
368  QofIdTypeConst param_type,
369  GNCSearchParamFcn fcn,
370  gpointer arg)
371 {
372  GNCSearchParamPrivate *priv;
373 
374  g_return_if_fail (param);
375  g_return_if_fail (param_type && *param_type);
376  g_return_if_fail (fcn);
377  g_return_if_fail (GNC_IS_SEARCH_PARAM(param));
378 
379  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
380  priv->lookup_fcn = fcn;
381  priv->lookup_arg = arg;
382  gnc_search_param_override_param_type (param, param_type);
383 }
384 
385 /* Compute the value of this parameter for this object */
386 gpointer
387 gnc_search_param_compute_value (GNCSearchParam *param, gpointer object)
388 {
389  GNCSearchParamPrivate *priv;
390 
391  g_return_val_if_fail(param, NULL);
392  g_return_val_if_fail(GNC_IS_SEARCH_PARAM(param), NULL);
393 
394  priv = GNC_SEARCH_PARAM_GET_PRIVATE(param);
395  if (priv->lookup_fcn)
396  {
397  return ((priv->lookup_fcn)(object, priv->lookup_arg));
398  }
399  else
400  {
401  GSList *converters = gnc_search_param_get_converters (param);
402  gpointer res = object;
403 
404  /* Do all the object conversions */
405  for (; converters; converters = converters->next)
406  {
407  QofParam *qp = converters->data;
408  res = (qp->param_getfcn) (res, qp);
409  }
410 
411  return res;
412  }
413 }
const gchar * QofIdTypeConst
Definition: qofid.h:87
const gchar * QofIdType
Definition: qofid.h:85
const QofParam * qof_class_get_parameter(QofIdTypeConst obj_name, const char *parameter)
All type declarations for the whole Gnucash engine.