00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025
00026 #include <glib.h>
00027
00028 #include "qof.h"
00029 #include "qofclass-p.h"
00030
00031 static QofLogModule log_module = QOF_MOD_CLASS;
00032
00033 static GHashTable *classTable = NULL;
00034 static GHashTable *sortTable = NULL;
00035 static gboolean initialized = FALSE;
00036
00037 static gboolean
00038 clear_table (gpointer key __attribute__ ((unused)), gpointer value,
00039 gpointer user_data __attribute__ ((unused)))
00040 {
00041 g_hash_table_destroy (value);
00042 return TRUE;
00043 }
00044
00045
00046
00047
00048 static gboolean
00049 check_init (void)
00050 {
00051 if (initialized)
00052 return TRUE;
00053
00054 PERR ("You must call qof_class_init() before using qof_class.");
00055 return FALSE;
00056 }
00057
00058 void
00059 qof_class_init (void)
00060 {
00061 if (initialized)
00062 return;
00063 initialized = TRUE;
00064
00065 classTable = g_hash_table_new (g_str_hash, g_str_equal);
00066 sortTable = g_hash_table_new (g_str_hash, g_str_equal);
00067 }
00068
00069 void
00070 qof_class_shutdown (void)
00071 {
00072 if (!initialized)
00073 return;
00074 initialized = FALSE;
00075
00076 g_hash_table_foreach_remove (classTable, clear_table, NULL);
00077 g_hash_table_destroy (classTable);
00078 g_hash_table_destroy (sortTable);
00079 }
00080
00081 QofSortFunc
00082 qof_class_get_default_sort (QofIdTypeConst obj_name)
00083 {
00084 if (!obj_name)
00085 return NULL;
00086 return g_hash_table_lookup (sortTable, obj_name);
00087 }
00088
00089
00090
00091
00092 void
00093 qof_class_register (QofIdTypeConst obj_name,
00094 QofSortFunc default_sort_function, const QofParam * params)
00095 {
00096 GHashTable *ht;
00097 int i;
00098
00099 if (!obj_name)
00100 return;
00101 if (!check_init ())
00102 return;
00103
00104 if (default_sort_function)
00105 {
00106 g_hash_table_insert (sortTable, (gchar *) obj_name,
00107 default_sort_function);
00108 }
00109
00110 ht = g_hash_table_lookup (classTable, obj_name);
00111
00112
00113 if (!ht)
00114 {
00115 ht = g_hash_table_new (g_str_hash, g_str_equal);
00116 g_hash_table_insert (classTable, (gchar *) obj_name, ht);
00117 }
00118
00119
00120
00121
00122
00123 if (params)
00124 {
00125 for (i = 0; params[i].param_name; i++)
00126 g_hash_table_insert (ht,
00127 (char *) params[i].param_name, (gpointer) & (params[i]));
00128 }
00129 }
00130
00131 gboolean
00132 qof_class_is_registered (QofIdTypeConst obj_name)
00133 {
00134 if (!obj_name)
00135 return FALSE;
00136 if (!check_init ())
00137 return FALSE;
00138
00139 if (g_hash_table_lookup (classTable, obj_name))
00140 return TRUE;
00141
00142 return FALSE;
00143 }
00144
00145 const QofParam *
00146 qof_class_get_parameter (QofIdTypeConst obj_name, const gchar *parameter)
00147 {
00148 GHashTable *ht;
00149
00150 g_return_val_if_fail (obj_name, NULL);
00151 g_return_val_if_fail (parameter, NULL);
00152 if (!check_init ())
00153 return NULL;
00154
00155 ht = g_hash_table_lookup (classTable, obj_name);
00156 if (!ht)
00157 {
00158 PWARN ("no object of type %s", obj_name);
00159 return NULL;
00160 }
00161
00162 return (g_hash_table_lookup (ht, parameter));
00163 }
00164
00165 QofAccessFunc
00166 qof_class_get_parameter_getter (QofIdTypeConst obj_name,
00167 const gchar *parameter)
00168 {
00169 const QofParam *prm;
00170
00171 g_return_val_if_fail (obj_name, NULL);
00172 g_return_val_if_fail (parameter, NULL);
00173
00174 prm = qof_class_get_parameter (obj_name, parameter);
00175 if (prm)
00176 return prm->param_getfcn;
00177
00178 return NULL;
00179 }
00180
00181 QofSetterFunc
00182 qof_class_get_parameter_setter (QofIdTypeConst obj_name,
00183 const gchar *parameter)
00184 {
00185 const QofParam *prm;
00186
00187 g_return_val_if_fail (obj_name, NULL);
00188 g_return_val_if_fail (parameter, NULL);
00189
00190 prm = qof_class_get_parameter (obj_name, parameter);
00191 if (prm)
00192 return prm->param_setfcn;
00193
00194 return NULL;
00195 }
00196
00197 QofType
00198 qof_class_get_parameter_type (QofIdTypeConst obj_name,
00199 const gchar *param_name)
00200 {
00201 const QofParam *prm;
00202
00203 if (!obj_name || !param_name)
00204 return NULL;
00205
00206 prm = qof_class_get_parameter (obj_name, param_name);
00207 if (!prm)
00208 return NULL;
00209
00210 return (prm->param_type);
00211 }
00212
00213
00214
00215 struct class_iterate
00216 {
00217 QofClassForeachCB fcn;
00218 gpointer data;
00219 };
00220
00221 static void
00222 class_foreach_cb (gpointer key __attribute__ ((unused)),
00223 gpointer item __attribute__ ((unused)),
00224 gpointer arg)
00225 {
00226 struct class_iterate *qiter = arg;
00227 QofIdTypeConst id = key;
00228
00229 qiter->fcn (id, qiter->data);
00230 }
00231
00232 void
00233 qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
00234 {
00235 struct class_iterate qiter;
00236
00237 if (!cb)
00238 return;
00239 if (!classTable)
00240 return;
00241
00242 qiter.fcn = cb;
00243 qiter.data = user_data;
00244
00245 g_hash_table_foreach (classTable, class_foreach_cb, &qiter);
00246 }
00247
00248
00249
00250 struct parm_iterate
00251 {
00252 QofParamForeachCB fcn;
00253 gpointer data;
00254 };
00255
00256 static void
00257 param_foreach_cb (gpointer key __attribute__ ((unused)),
00258 gpointer item, gpointer arg)
00259 {
00260 struct parm_iterate *qiter = arg;
00261 QofParam *parm = item;
00262
00263 qiter->fcn (parm, qiter->data);
00264 }
00265
00266 void
00267 qof_class_param_foreach (QofIdTypeConst obj_name,
00268 QofParamForeachCB cb, gpointer user_data)
00269 {
00270 struct parm_iterate qiter;
00271 GHashTable *param_ht;
00272
00273 if (!obj_name || !cb)
00274 return;
00275 if (!classTable)
00276 return;
00277 param_ht = g_hash_table_lookup (classTable, obj_name);
00278 if (!param_ht)
00279 return;
00280
00281 qiter.fcn = cb;
00282 qiter.data = user_data;
00283
00284 g_hash_table_foreach (param_ht, param_foreach_cb, &qiter);
00285 }
00286
00287 struct param_ref_list
00288 {
00289 GList *list;
00290 };
00291
00292 static void
00293 find_reference_param_cb (QofParam * param, gpointer user_data)
00294 {
00295 struct param_ref_list *b;
00296
00297 b = (struct param_ref_list *) user_data;
00298 if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
00299 return;
00300 if (0 == safe_strcmp (param->param_type, QOF_TYPE_STRING))
00301 return;
00302 if (0 == safe_strcmp (param->param_type, QOF_TYPE_NUMERIC))
00303 return;
00304 if (0 == safe_strcmp (param->param_type, QOF_TYPE_TIME))
00305 return;
00306 #ifndef QOF_DISABLE_DEPRECATED
00307 if (0 == safe_strcmp (param->param_type, QOF_TYPE_DATE))
00308 return;
00309 #endif
00310 if (0 == safe_strcmp (param->param_type, QOF_TYPE_CHAR))
00311 return;
00312 if (0 == safe_strcmp (param->param_type, QOF_TYPE_DEBCRED))
00313 return;
00314 if (0 == safe_strcmp (param->param_type, QOF_TYPE_GUID))
00315 return;
00316 if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT32))
00317 return;
00318 if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT64))
00319 return;
00320 if (0 == safe_strcmp (param->param_type, QOF_TYPE_DOUBLE))
00321 return;
00322 if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
00323 return;
00324 if (0 == safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
00325 return;
00326 if (0 == safe_strcmp (param->param_type, QOF_ID_BOOK))
00327 return;
00328 b->list = g_list_append (b->list, param);
00329 }
00330
00331 GList *
00332 qof_class_get_referenceList (QofIdTypeConst type)
00333 {
00334 GList *ref_list;
00335 struct param_ref_list b;
00336
00337 ref_list = NULL;
00338 b.list = NULL;
00339 qof_class_param_foreach (type, find_reference_param_cb, &b);
00340 ref_list = g_list_copy (b.list);
00341 return ref_list;
00342 }
00343
00344
00345