QOF  0.8.7
qofclass.c
1 /********************************************************************\
2  * qofclass.c -- provide QOF parameterized data objects *
3  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
4  * Copyright 2008 Neil Williams <linux@codehelp.co.uk> *
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 \********************************************************************/
24 
25 #include "config.h"
26 
27 #include <glib.h>
28 
29 #include "qof.h"
30 #include "qofclass-p.h"
31 
32 static QofLogModule log_module = QOF_MOD_CLASS;
33 
34 static GHashTable *classTable = NULL;
35 static GHashTable *sortTable = NULL;
36 static gboolean initialized = FALSE;
37 
38 static gboolean
39 clear_table (gpointer key __attribute__ ((unused)), gpointer value,
40  gpointer user_data __attribute__ ((unused)))
41 {
42  g_hash_table_destroy (value);
43  return TRUE;
44 }
45 
46 /* *******************************************************************/
47 /* PRIVATE FUNCTIONS */
48 
49 static gboolean
50 check_init (void)
51 {
52  if (initialized)
53  return TRUE;
54 
55  PERR ("You must call qof_class_init() before using qof_class.");
56  return FALSE;
57 }
58 
59 void
60 qof_class_init (void)
61 {
62  if (initialized)
63  return;
64  initialized = TRUE;
65 
66  classTable = g_hash_table_new (g_str_hash, g_str_equal);
67  sortTable = g_hash_table_new (g_str_hash, g_str_equal);
68 }
69 
70 void
71 qof_class_shutdown (void)
72 {
73  if (!initialized)
74  return;
75  initialized = FALSE;
76 
77  g_hash_table_foreach_remove (classTable, clear_table, NULL);
78  g_hash_table_destroy (classTable);
79  g_hash_table_destroy (sortTable);
80 }
81 
83 qof_class_get_default_sort (QofIdTypeConst obj_name)
84 {
85  if (!obj_name)
86  return NULL;
87  return g_hash_table_lookup (sortTable, obj_name);
88 }
89 
90 /* *******************************************************************/
91 /* PUBLISHED API FUNCTIONS */
92 
93 void
95  QofSortFunc default_sort_function, const QofParam * params)
96 {
97  GHashTable *ht;
98  int i;
99 
100  if (!obj_name)
101  return;
102  if (!check_init ())
103  return;
104 
105  if (default_sort_function)
106  {
107  g_hash_table_insert (sortTable, (gchar *) obj_name,
108  default_sort_function);
109  }
110 
111  ht = g_hash_table_lookup (classTable, obj_name);
112 
113  /* If it doesn't already exist, create a new table for this object */
114  if (!ht)
115  {
116  ht = g_hash_table_new (g_str_hash, g_str_equal);
117  g_hash_table_insert (classTable, (gchar *) obj_name, ht);
118  }
119 
120  /* At least right now, we allow dummy, parameterless objects,
121  * for testing purposes. Although I suppose that should be
122  * an error.. */
123  /* Now insert all the parameters */
124  if (params)
125  {
126  for (i = 0; params[i].param_name; i++)
127  g_hash_table_insert (ht,
128  (char *) params[i].param_name, (gpointer) & (params[i]));
129  }
130 }
131 
132 gboolean
134 {
135  if (!obj_name)
136  return FALSE;
137  if (!check_init ())
138  return FALSE;
139 
140  if (g_hash_table_lookup (classTable, obj_name))
141  return TRUE;
142 
143  return FALSE;
144 }
145 
146 const QofParam *
147 qof_class_get_parameter (QofIdTypeConst obj_name, const gchar *parameter)
148 {
149  GHashTable *ht;
150 
151  g_return_val_if_fail (obj_name, NULL);
152  g_return_val_if_fail (parameter, NULL);
153  if (!check_init ())
154  return NULL;
155 
156  ht = g_hash_table_lookup (classTable, obj_name);
157  if (!ht)
158  {
159  PWARN ("no object of type %s", obj_name);
160  return NULL;
161  }
162 
163  return (g_hash_table_lookup (ht, parameter));
164 }
165 
168  const gchar *parameter)
169 {
170  const QofParam *prm;
171 
172  g_return_val_if_fail (obj_name, NULL);
173  g_return_val_if_fail (parameter, NULL);
174 
175  prm = qof_class_get_parameter (obj_name, parameter);
176  if (prm)
177  return prm->param_getfcn;
178 
179  return NULL;
180 }
181 
184  const gchar *parameter)
185 {
186  const QofParam *prm;
187 
188  g_return_val_if_fail (obj_name, NULL);
189  g_return_val_if_fail (parameter, NULL);
190 
191  prm = qof_class_get_parameter (obj_name, parameter);
192  if (prm)
193  return prm->param_setfcn;
194 
195  return NULL;
196 }
197 
198 QofType
200  const gchar *param_name)
201 {
202  const QofParam *prm;
203 
204  if (!obj_name || !param_name)
205  return NULL;
206 
207  prm = qof_class_get_parameter (obj_name, param_name);
208  if (!prm)
209  return NULL;
210 
211  return (prm->param_type);
212 }
213 
214 /* ================================================================ */
215 
216 struct class_iterate
217 {
218  QofClassForeachCB fcn;
219  gpointer data;
220 };
221 
222 static void
223 class_foreach_cb (gpointer key __attribute__ ((unused)),
224  gpointer item __attribute__ ((unused)),
225  gpointer arg)
226 {
227  struct class_iterate *qiter = arg;
228  QofIdTypeConst id = key;
229 
230  qiter->fcn (id, qiter->data);
231 }
232 
233 void
234 qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
235 {
236  struct class_iterate qiter;
237 
238  if (!cb)
239  return;
240  if (!classTable)
241  return;
242 
243  qiter.fcn = cb;
244  qiter.data = user_data;
245 
246  g_hash_table_foreach (classTable, class_foreach_cb, &qiter);
247 }
248 
249 /* ================================================================ */
250 
251 struct parm_iterate
252 {
253  QofParamForeachCB fcn;
254  gpointer data;
255 };
256 
257 static void
258 param_foreach_cb (gpointer key __attribute__ ((unused)),
259  gpointer item, gpointer arg)
260 {
261  struct parm_iterate *qiter = arg;
262  QofParam *parm = item;
263 
264  qiter->fcn (parm, qiter->data);
265 }
266 
267 void
269  QofParamForeachCB cb, gpointer user_data)
270 {
271  struct parm_iterate qiter;
272  GHashTable *param_ht;
273 
274  if (!obj_name || !cb)
275  return;
276  if (!classTable)
277  return;
278  param_ht = g_hash_table_lookup (classTable, obj_name);
279  if (!param_ht)
280  return;
281 
282  qiter.fcn = cb;
283  qiter.data = user_data;
284 
285  g_hash_table_foreach (param_ht, param_foreach_cb, &qiter);
286 }
287 
288 struct param_ref_list
289 {
290  GList *list;
291 };
292 
293 static void
294 find_reference_param_cb (QofParam * param, gpointer user_data)
295 {
296  struct param_ref_list *b;
297 
298  b = (struct param_ref_list *) user_data;
299  if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
300  return;
301  if (0 == safe_strcmp (param->param_type, QOF_TYPE_STRING))
302  return;
303  if (0 == safe_strcmp (param->param_type, QOF_TYPE_NUMERIC))
304  return;
305  if (0 == safe_strcmp (param->param_type, QOF_TYPE_TIME))
306  return;
307  if (0 == safe_strcmp (param->param_type, QOF_TYPE_CHAR))
308  return;
309  if (0 == safe_strcmp (param->param_type, QOF_TYPE_DEBCRED))
310  return;
311  if (0 == safe_strcmp (param->param_type, QOF_TYPE_GUID))
312  return;
313  if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT32))
314  return;
315  if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT64))
316  return;
317  if (0 == safe_strcmp (param->param_type, QOF_TYPE_DOUBLE))
318  return;
319  if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
320  return;
321  if (0 == safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
322  return;
323  if (0 == safe_strcmp (param->param_type, QOF_ID_BOOK))
324  return;
325  b->list = g_list_append (b->list, param);
326 }
327 
328 GList *
330 {
331  GList *ref_list;
332  struct param_ref_list b;
333 
334  ref_list = NULL;
335  b.list = NULL;
336  qof_class_param_foreach (type, find_reference_param_cb, &b);
337  ref_list = g_list_copy (b.list);
338  return ref_list;
339 }
340 
341 
342 /* ============================= END OF FILE ======================== */
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qof_class_is_registered(QofIdTypeConst obj_name)
Definition: qofclass.c:133
void qof_class_param_foreach(QofIdTypeConst obj_name, QofParamForeachCB cb, gpointer user_data)
Definition: qofclass.c:268
QofSetterFunc qof_class_get_parameter_setter(QofIdTypeConst obj_name, const gchar *parameter)
Definition: qofclass.c:183
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_function, const QofParam *params)
registers a new object class with the Qof subsystem.
Definition: qofclass.c:94
gint(* QofSortFunc)(gconstpointer, gconstpointer)
Definition: qofclass.h:181
void(* QofClassForeachCB)(QofIdTypeConst, gpointer)
Definition: qofclass.h:251
void(* QofSetterFunc)(gpointer, gpointer)
Definition: qofclass.h:151
const QofParam * qof_class_get_parameter(QofIdTypeConst obj_name, const gchar *parameter)
Definition: qofclass.c:147
void qof_class_foreach(QofClassForeachCB cb, gpointer user_data)
Definition: qofclass.c:234
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:144
const gchar * QofType
Definition: qofclass.h:125
GList * qof_class_get_referenceList(QofIdTypeConst type)
List of the parameters that could be references.
Definition: qofclass.c:329
const gchar * QofIdTypeConst
Definition: qofid.h:83
#define PWARN(format, args...)
Definition: qoflog.h:191
QofType qof_class_get_parameter_type(QofIdTypeConst obj_name, const gchar *param_name)
Definition: qofclass.c:199
gint safe_strcmp(const gchar *da, const gchar *db)
Definition: qofutil.c:75
QofAccessFunc qof_class_get_parameter_getter(QofIdTypeConst obj_name, const gchar *parameter)
Definition: qofclass.c:167
const gchar * QofLogModule
Definition: qofid.h:85
void(* QofParamForeachCB)(QofParam *, gpointer user_data)
Definition: qofclass.h:260