qofclass.c

00001 /********************************************************************\
00002  * qofclass.c -- provide QOF parameterized data objects             *
00003  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU>                *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
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 clear_table (gpointer key, gpointer value, gpointer user_data)
00038 {
00039   g_hash_table_destroy (value);
00040   return TRUE;
00041 }
00042 
00043 /* *******************************************************************/
00044 /* PRIVATE FUNCTIONS */
00045 
00046 static gboolean check_init (void)
00047 {
00048     if (initialized) return TRUE;
00049 
00050     PERR("You must call qof_class_init() before using qof_class.");
00051     return FALSE;
00052 }
00053 
00054 void
00055 qof_class_init(void)
00056 {
00057   if (initialized) return;
00058   initialized = TRUE;
00059 
00060   classTable = g_hash_table_new (g_str_hash, g_str_equal);
00061   sortTable = g_hash_table_new (g_str_hash, g_str_equal);
00062 }
00063 
00064 void
00065 qof_class_shutdown (void)
00066 {
00067   if (!initialized) return;
00068   initialized = FALSE;
00069 
00070   g_hash_table_foreach_remove (classTable, clear_table, NULL);
00071   g_hash_table_destroy (classTable);
00072   g_hash_table_destroy (sortTable);
00073 }
00074 
00075 QofSortFunc
00076 qof_class_get_default_sort (QofIdTypeConst obj_name)
00077 {
00078   if (!obj_name) return NULL;
00079   return g_hash_table_lookup (sortTable, obj_name);
00080 }
00081 
00082 /* *******************************************************************/
00083 /* PUBLISHED API FUNCTIONS */
00084 
00085 void
00086 qof_class_register (QofIdTypeConst obj_name,
00087                     QofSortFunc default_sort_function,
00088                     const QofParam *params)
00089 {
00090   GHashTable *ht;
00091   int i;
00092 
00093   if (!obj_name) return;
00094   if (!check_init()) return;
00095 
00096   if (default_sort_function)
00097   {
00098     g_hash_table_insert (sortTable, (char *)obj_name, default_sort_function);
00099   }
00100 
00101   ht = g_hash_table_lookup (classTable, obj_name);
00102 
00103   /* If it doesn't already exist, create a new table for this object */
00104   if (!ht)
00105   {
00106     ht = g_hash_table_new (g_str_hash, g_str_equal);
00107     g_hash_table_insert (classTable, (char *)obj_name, ht);
00108   }
00109 
00110   /* At least right now, we allow dummy, parameterless objects,
00111    * for testing purposes.  Although I suppose that should be
00112    * an error..  */
00113   /* Now insert all the parameters */
00114   if (params)
00115   {
00116     for (i = 0; params[i].param_name; i++)
00117       g_hash_table_insert (ht,
00118                (char *)params[i].param_name,
00119                (gpointer)&(params[i]));
00120   }
00121 }
00122 
00123 gboolean
00124 qof_class_is_registered (QofIdTypeConst obj_name)
00125 {
00126   if (!obj_name) return FALSE;
00127   if (!check_init()) return FALSE;
00128 
00129   if (g_hash_table_lookup (classTable, obj_name)) return TRUE;
00130 
00131   return FALSE;
00132 }
00133 
00134 const QofParam *
00135 qof_class_get_parameter (QofIdTypeConst obj_name,
00136                           const char *parameter)
00137 {
00138   GHashTable *ht;
00139 
00140   g_return_val_if_fail (obj_name, NULL);
00141   g_return_val_if_fail (parameter, NULL);
00142   if (!check_init()) return NULL;
00143 
00144   ht = g_hash_table_lookup (classTable, obj_name);
00145   if (!ht)
00146   {
00147     PWARN ("no object of type %s", obj_name);
00148     return NULL;
00149   }
00150 
00151   return (g_hash_table_lookup (ht, parameter));
00152 }
00153 
00154 QofAccessFunc
00155 qof_class_get_parameter_getter (QofIdTypeConst obj_name,
00156                                  const char *parameter)
00157 {
00158   const QofParam *prm;
00159 
00160   g_return_val_if_fail (obj_name, NULL);
00161   g_return_val_if_fail (parameter, NULL);
00162 
00163   prm = qof_class_get_parameter (obj_name, parameter);
00164   if (prm)
00165     return prm->param_getfcn;
00166 
00167   return NULL;
00168 }
00169 
00170 QofSetterFunc
00171 qof_class_get_parameter_setter (QofIdTypeConst obj_name,
00172                                  const char *parameter)
00173 {
00174   const QofParam *prm;
00175 
00176   g_return_val_if_fail (obj_name, NULL);
00177   g_return_val_if_fail (parameter, NULL);
00178 
00179   prm = qof_class_get_parameter (obj_name, parameter);
00180   if (prm)
00181     return prm->param_setfcn;
00182 
00183   return NULL;
00184 }
00185 
00186 QofType
00187 qof_class_get_parameter_type (QofIdTypeConst obj_name,
00188                                const char *param_name)
00189 {
00190   const QofParam *prm;
00191 
00192   if (!obj_name || !param_name) return NULL;
00193 
00194   prm = qof_class_get_parameter (obj_name, param_name);
00195   if (!prm) return NULL;
00196 
00197   return (prm->param_type);
00198 }
00199 
00200 /* ================================================================ */
00201 
00202 struct class_iterate {
00203   QofClassForeachCB   fcn;
00204   gpointer            data;
00205 };
00206 
00207 static void
00208 class_foreach_cb (gpointer key, gpointer item, gpointer arg)
00209 {
00210   struct class_iterate *iter = arg;
00211   QofIdTypeConst id = key;
00212 
00213   iter->fcn (id, iter->data);
00214 }
00215 
00216 void
00217 qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
00218 {
00219   struct class_iterate iter;
00220 
00221   if (!cb) return;
00222   if (!classTable) return;
00223 
00224   iter.fcn = cb;
00225   iter.data = user_data;
00226 
00227   g_hash_table_foreach (classTable, class_foreach_cb, &iter);
00228 }
00229 
00230 /* ================================================================ */
00231 
00232 struct parm_iterate {
00233   QofParamForeachCB   fcn;
00234   gpointer            data;
00235 };
00236 
00237 static void
00238 param_foreach_cb (gpointer key, gpointer item, gpointer arg)
00239 {
00240   struct parm_iterate *iter = arg;
00241   QofParam *parm = item;
00242 
00243   iter->fcn (parm, iter->data);
00244 }
00245 
00246 void
00247 qof_class_param_foreach (QofIdTypeConst obj_name,
00248                          QofParamForeachCB cb, gpointer user_data)
00249 {
00250   struct parm_iterate iter;
00251   GHashTable *param_ht;
00252 
00253   if (!obj_name || !cb) return;
00254   if (!classTable) return;
00255   param_ht = g_hash_table_lookup (classTable, obj_name);
00256   if (!param_ht) return;
00257 
00258   iter.fcn = cb;
00259   iter.data = user_data;
00260 
00261   g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
00262 }
00263 
00264 struct param_ref_list
00265 {
00266         GList *list;
00267 };
00268 
00269 static void
00270 find_reference_param_cb(QofParam *param, gpointer user_data)
00271 {
00272         struct param_ref_list *b;
00273 
00274         b = (struct param_ref_list*)user_data;
00275         if((param->param_getfcn == NULL)||(param->param_setfcn == NULL)) { return; }
00276         if(0 == safe_strcmp(param->param_type, QOF_TYPE_STRING)) { return; }
00277         if(0 == safe_strcmp(param->param_type, QOF_TYPE_NUMERIC)) { return; }
00278         if(0 == safe_strcmp(param->param_type, QOF_TYPE_DATE)) { return; }
00279         if(0 == safe_strcmp(param->param_type, QOF_TYPE_CHAR)) { return; }
00280         if(0 == safe_strcmp(param->param_type, QOF_TYPE_DEBCRED)) { return; }
00281         if(0 == safe_strcmp(param->param_type, QOF_TYPE_GUID)) { return; }
00282         if(0 == safe_strcmp(param->param_type, QOF_TYPE_INT32)) { return; }
00283         if(0 == safe_strcmp(param->param_type, QOF_TYPE_INT64)) { return; }
00284         if(0 == safe_strcmp(param->param_type, QOF_TYPE_DOUBLE)) { return; }
00285         if(0 == safe_strcmp(param->param_type, QOF_TYPE_KVP)) { return; }
00286         if(0 == safe_strcmp(param->param_type, QOF_TYPE_BOOLEAN)) { return; }
00287         if(0 == safe_strcmp(param->param_type, QOF_ID_BOOK)) { return; }
00288         b->list = g_list_append(b->list, param);
00289 }
00290 
00291 GList*
00292 qof_class_get_referenceList(QofIdTypeConst type)
00293 {
00294         GList *ref_list;
00295         struct param_ref_list b;
00296 
00297         ref_list = NULL;
00298         b.list = NULL;
00299         qof_class_param_foreach(type, find_reference_param_cb, &b);
00300         ref_list = g_list_copy(b.list);
00301         return ref_list;
00302 }
00303 
00304 
00305 /* ============================= END OF FILE ======================== */

Generated on Fri May 12 17:57:20 2006 for QOF by  doxygen 1.4.4