|
GnuCash 2.4.99
|
00001 /*************************************************************************** 00002 * qofchoice.c 00003 * 00004 * Thu Jul 7 12:24:30 2005 00005 * Copyright 2005 Neil Williams 00006 * linux@codehelp.co.uk 00007 ****************************************************************************/ 00008 /* 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU Library General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include "config.h" 00025 #include <glib.h> 00026 #include "qof.h" 00027 #include "qofchoice.h" 00028 00029 static QofLogModule log_module = QOF_MOD_CHOICE; 00030 static GHashTable *qof_choice_table = NULL; 00031 00032 /* To initialise, call qof_choice_add_class in 00033 qof_object_register for the choice object. */ 00034 static gboolean qof_choice_is_initialized(void) 00035 { 00036 if (!qof_choice_table) 00037 { 00038 qof_choice_table = g_hash_table_new(g_str_hash, g_str_equal); 00039 } 00040 if (!qof_choice_table) 00041 { 00042 return FALSE; 00043 } 00044 return TRUE; 00045 } 00046 00047 gboolean qof_object_is_choice(QofIdTypeConst type) 00048 { 00049 gpointer value, check; 00050 00051 value = NULL; 00052 check = NULL; 00053 if (!qof_choice_is_initialized()) 00054 { 00055 return FALSE; 00056 } 00057 g_return_val_if_fail(type != NULL, FALSE); 00058 value = g_hash_table_lookup(qof_choice_table, type); 00059 if ((GHashTable*)value) 00060 { 00061 return TRUE; 00062 } 00063 DEBUG (" QOF_TYPE_CHOICE setup failed for %s\n", type); 00064 return FALSE; 00065 } 00066 00067 gboolean 00068 qof_choice_create(char* type) 00069 { 00070 GHashTable *param_table; 00071 00072 g_return_val_if_fail(type != NULL, FALSE); 00073 g_return_val_if_fail(qof_choice_is_initialized() == TRUE, FALSE); 00074 param_table = g_hash_table_new(g_str_hash, g_str_equal); 00075 g_hash_table_insert(qof_choice_table, type, param_table); 00076 return TRUE; 00077 } 00078 00079 gboolean qof_choice_add_class(const char* select, 00080 char* option, 00081 char* param_name) 00082 { 00083 GHashTable *param_table; 00084 GList *option_list; 00085 00086 option_list = NULL; 00087 param_table = NULL; 00088 g_return_val_if_fail(select != NULL, FALSE); 00089 g_return_val_if_fail(qof_object_is_choice(select), FALSE); 00090 param_table = (GHashTable*)g_hash_table_lookup(qof_choice_table, select); 00091 g_return_val_if_fail(param_table, FALSE); 00092 option_list = (GList*)g_hash_table_lookup(param_table, param_name); 00093 option_list = g_list_append(option_list, option); 00094 g_hash_table_insert(param_table, param_name, option_list); 00095 return TRUE; 00096 } 00097 00098 GList* qof_object_get_choices(QofIdType type, QofParam *param) 00099 { 00100 GList *choices; 00101 GHashTable *param_table; 00102 00103 g_return_val_if_fail(type != NULL, NULL); 00104 g_return_val_if_fail(qof_choice_is_initialized() == TRUE, FALSE); 00105 choices = NULL; 00106 param_table = g_hash_table_lookup(qof_choice_table, type); 00107 choices = g_hash_table_lookup(param_table, param->param_name); 00108 return choices; 00109 } 00110 00111 gboolean qof_choice_check(const char* choice_obj, 00112 const char *param_name, 00113 const char* choice ) 00114 { 00115 GList *choices, *result; 00116 GHashTable *param_table; 00117 00118 choices = result = NULL; 00119 g_return_val_if_fail(qof_object_is_choice(choice_obj), FALSE); 00120 param_table = g_hash_table_lookup(qof_choice_table, choice_obj); 00121 choices = g_hash_table_lookup(param_table, param_name); 00122 result = g_list_find(choices, choice); 00123 if (!result) 00124 { 00125 return FALSE; 00126 } 00127 return TRUE; 00128 }
1.7.4