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

Generated on Tue Sep 19 17:05:28 2006 for QOF by  doxygen 1.4.7