GnuCash  4.8a-132-gcdaeb421d+
qofchoice.cpp
1 /***************************************************************************
2  * qofchoice.c
3  *
4  * Thu Jul 7 12:24:30 2005
5  * Copyright 2005 Neil Williams
6  * linux@codehelp.co.uk
7  ****************************************************************************/
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #include <config.h>
25 #include <glib.h>
26 
27 #include "qof.h"
28 #include "qofchoice.h"
29 
30 static QofLogModule log_module = QOF_MOD_CHOICE;
31 static GHashTable *qof_choice_table = NULL;
32 
33 /* To initialise, call qof_choice_add_class in
34 qof_object_register for the choice object. */
35 static gboolean qof_choice_is_initialized(void)
36 {
37  if (!qof_choice_table)
38  {
39  qof_choice_table = g_hash_table_new(g_str_hash, g_str_equal);
40  }
41  if (!qof_choice_table)
42  {
43  return FALSE;
44  }
45  return TRUE;
46 }
47 
49 {
50  gpointer value = NULL;
51 
52  if (!qof_choice_is_initialized())
53  {
54  return FALSE;
55  }
56  g_return_val_if_fail(type != NULL, FALSE);
57  value = g_hash_table_lookup(qof_choice_table, type);
58  if ((GHashTable*)value)
59  {
60  return TRUE;
61  }
62  DEBUG (" QOF_TYPE_CHOICE setup failed for %s\n", type);
63  return FALSE;
64 }
65 
66 gboolean
67 qof_choice_create(char* type)
68 {
69  GHashTable *param_table;
70 
71  g_return_val_if_fail(type != NULL, FALSE);
72  g_return_val_if_fail(qof_choice_is_initialized() == TRUE, FALSE);
73  param_table = g_hash_table_new(g_str_hash, g_str_equal);
74  g_hash_table_insert(qof_choice_table, type, param_table);
75  return TRUE;
76 }
77 
78 gboolean qof_choice_add_class(const char* select,
79  char* option,
80  char* param_name)
81 {
82  GHashTable *param_table;
83  GList *option_list;
84 
85  option_list = NULL;
86  param_table = NULL;
87  g_return_val_if_fail(select != NULL, FALSE);
88  g_return_val_if_fail(qof_object_is_choice(select), FALSE);
89  param_table = (GHashTable*)g_hash_table_lookup(qof_choice_table, select);
90  g_return_val_if_fail(param_table, FALSE);
91  option_list = (GList*)g_hash_table_lookup(param_table, param_name);
92  option_list = g_list_append(option_list, option);
93  g_hash_table_insert(param_table, param_name, option_list);
94  return TRUE;
95 }
96 
97 GList* qof_object_get_choices(QofIdType type, QofParam *param)
98 {
99  GList *choices;
100  GHashTable *param_table;
101 
102  g_return_val_if_fail(type != NULL, NULL);
103  g_return_val_if_fail(qof_choice_is_initialized() == TRUE, FALSE);
104  choices = NULL;
105  param_table = static_cast<GHashTable*>(g_hash_table_lookup(qof_choice_table, type));
106  choices = static_cast<GList*>(g_hash_table_lookup(param_table, param->param_name));
107  return choices;
108 }
109 
110 gboolean qof_choice_check(const char* choice_obj,
111  const char *param_name,
112  const char* choice )
113 {
114  GList *choices, *result;
115  GHashTable *param_table;
116 
117  choices = result = NULL;
118  g_return_val_if_fail(qof_object_is_choice(choice_obj), FALSE);
119  param_table = static_cast<GHashTable*>(g_hash_table_lookup(qof_choice_table, choice_obj));
120  choices = static_cast<GList*>(g_hash_table_lookup(param_table, param_name));
121  result = g_list_find(choices, choice);
122  if (!result)
123  {
124  return FALSE;
125  }
126  return TRUE;
127 }
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition: qofid.h:87
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
const gchar * QofIdType
QofIdType declaration.
Definition: qofid.h:85
gboolean qof_choice_check(const char *choice_obj, const char *param_name, const char *choice)
Is the choice valid for this param_name?
Definition: qofchoice.cpp:110
gboolean qof_object_is_choice(QofIdTypeConst type)
Does this object contain a choice parameter?
Definition: qofchoice.cpp:48
gboolean qof_choice_create(char *type)
Set an object as using QOF_TYPE_CHOICE.
Definition: qofchoice.cpp:67
GList * qof_object_get_choices(QofIdType type, QofParam *param)
Return the list of all object types usable with this parameter.
Definition: qofchoice.cpp:97
gboolean qof_choice_add_class(const char *select, char *option, char *param_name)
Add the choices for this parameter to the object.
Definition: qofchoice.cpp:78
Linking one entity to other entities of many possible types.