|
GnuCash 2.4.99
|
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 { 00204 QofClassForeachCB fcn; 00205 gpointer data; 00206 }; 00207 00208 static void 00209 class_foreach_cb (gpointer key, gpointer item, gpointer arg) 00210 { 00211 struct class_iterate *iter = arg; 00212 QofIdTypeConst id = key; 00213 00214 iter->fcn (id, iter->data); 00215 } 00216 00217 void 00218 qof_class_foreach (QofClassForeachCB cb, gpointer user_data) 00219 { 00220 struct class_iterate iter; 00221 00222 if (!cb) return; 00223 if (!classTable) return; 00224 00225 iter.fcn = cb; 00226 iter.data = user_data; 00227 00228 g_hash_table_foreach (classTable, class_foreach_cb, &iter); 00229 } 00230 00231 /* ================================================================ */ 00232 00233 struct parm_iterate 00234 { 00235 QofParamForeachCB fcn; 00236 gpointer data; 00237 }; 00238 00239 static void 00240 param_foreach_cb (gpointer key, gpointer item, gpointer arg) 00241 { 00242 struct parm_iterate *iter = arg; 00243 QofParam *parm = item; 00244 00245 iter->fcn (parm, iter->data); 00246 } 00247 00248 void 00249 qof_class_param_foreach (QofIdTypeConst obj_name, 00250 QofParamForeachCB cb, gpointer user_data) 00251 { 00252 struct parm_iterate iter; 00253 GHashTable *param_ht; 00254 00255 if (!obj_name || !cb) return; 00256 if (!classTable) return; 00257 param_ht = g_hash_table_lookup (classTable, obj_name); 00258 if (!param_ht) return; 00259 00260 iter.fcn = cb; 00261 iter.data = user_data; 00262 00263 g_hash_table_foreach (param_ht, param_foreach_cb, &iter); 00264 } 00265 00266 struct param_ref_list 00267 { 00268 GList *list; 00269 }; 00270 00271 static void 00272 find_reference_param_cb(QofParam *param, gpointer user_data) 00273 { 00274 struct param_ref_list *b; 00275 00276 b = (struct param_ref_list*)user_data; 00277 if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL)) 00278 { 00279 return; 00280 } 00281 if (0 == safe_strcmp(param->param_type, QOF_TYPE_STRING)) 00282 { 00283 return; 00284 } 00285 if (0 == safe_strcmp(param->param_type, QOF_TYPE_NUMERIC)) 00286 { 00287 return; 00288 } 00289 if (0 == safe_strcmp(param->param_type, QOF_TYPE_DATE)) 00290 { 00291 return; 00292 } 00293 if (0 == safe_strcmp(param->param_type, QOF_TYPE_CHAR)) 00294 { 00295 return; 00296 } 00297 if (0 == safe_strcmp(param->param_type, QOF_TYPE_DEBCRED)) 00298 { 00299 return; 00300 } 00301 if (0 == safe_strcmp(param->param_type, QOF_TYPE_GUID)) 00302 { 00303 return; 00304 } 00305 if (0 == safe_strcmp(param->param_type, QOF_TYPE_INT32)) 00306 { 00307 return; 00308 } 00309 if (0 == safe_strcmp(param->param_type, QOF_TYPE_INT64)) 00310 { 00311 return; 00312 } 00313 if (0 == safe_strcmp(param->param_type, QOF_TYPE_DOUBLE)) 00314 { 00315 return; 00316 } 00317 if (0 == safe_strcmp(param->param_type, QOF_TYPE_KVP)) 00318 { 00319 return; 00320 } 00321 if (0 == safe_strcmp(param->param_type, QOF_TYPE_BOOLEAN)) 00322 { 00323 return; 00324 } 00325 if (0 == safe_strcmp(param->param_type, QOF_ID_BOOK)) 00326 { 00327 return; 00328 } 00329 b->list = g_list_append(b->list, param); 00330 } 00331 00332 GList* 00333 qof_class_get_referenceList(QofIdTypeConst type) 00334 { 00335 GList *ref_list; 00336 struct param_ref_list b; 00337 00338 ref_list = NULL; 00339 b.list = NULL; 00340 qof_class_param_foreach(type, find_reference_param_cb, &b); 00341 ref_list = g_list_copy(b.list); 00342 return ref_list; 00343 } 00344 00345 00346 /* ============================= END OF FILE ======================== */
1.7.4