QOF  0.8.7
qofchoice.c
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 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 #include "qof.h"
27 #include "qofchoice.h"
28 
29 static QofLogModule log_module = QOF_MOD_CHOICE;
30 static GHashTable *qof_choice_table = NULL;
31 
32 /* To initialise, call qof_choice_add_class in
33 qof_object_register for the choice object. */
34 static gboolean
35 qof_choice_is_initialized (void)
36 {
37  if (!qof_choice_table)
38  qof_choice_table = g_hash_table_new (g_str_hash, g_str_equal);
39  if (!qof_choice_table)
40  return FALSE;
41  return TRUE;
42 }
43 
44 gboolean
46 {
47  gpointer value;
48 
49  value = NULL;
50  if (!qof_choice_is_initialized ())
51  return FALSE;
52  g_return_val_if_fail (type != NULL, FALSE);
53  value = g_hash_table_lookup (qof_choice_table, type);
54  if ((GHashTable *) value)
55  return TRUE;
56  return FALSE;
57 }
58 
59 gboolean
60 qof_choice_create (gchar *type)
61 {
62  GHashTable *param_table;
63 
64  g_return_val_if_fail (type != NULL, FALSE);
65  g_return_val_if_fail (qof_choice_is_initialized () == TRUE, FALSE);
66  ENTER (" ");
67  param_table = g_hash_table_new (g_str_hash, g_str_equal);
68  g_hash_table_insert (qof_choice_table, type, param_table);
69  LEAVE (" ");
70  return TRUE;
71 }
72 
73 gboolean
74 qof_choice_add_class (gchar *select, gchar *option, gchar *param_name)
75 {
76  GHashTable *param_table;
77  GList *option_list;
78 
79  option_list = NULL;
80  param_table = NULL;
81  g_return_val_if_fail (select != NULL, FALSE);
82  g_return_val_if_fail (qof_object_is_choice (select), FALSE);
83  param_table =
84  (GHashTable *) g_hash_table_lookup (qof_choice_table, select);
85  g_return_val_if_fail (param_table, FALSE);
86  option_list = (GList *) g_hash_table_lookup (param_table, param_name);
87  option_list = g_list_append (option_list, option);
88  g_hash_table_insert (param_table, param_name, option_list);
89  return TRUE;
90 }
91 
92 GList *
94 {
95  GList *choices;
96  GHashTable *param_table;
97 
98  g_return_val_if_fail (type != NULL, NULL);
99  g_return_val_if_fail (qof_choice_is_initialized () == TRUE, FALSE);
100  choices = NULL;
101  param_table = g_hash_table_lookup (qof_choice_table, type);
102  choices = g_hash_table_lookup (param_table, param->param_name);
103  return choices;
104 }
105 
106 gboolean
107 qof_choice_check (gchar *choice_obj, gchar *param_name, gchar *choice)
108 {
109  GList *choices, *result;
110  GHashTable *param_table;
111 
112  choices = result = NULL;
113  g_return_val_if_fail (qof_object_is_choice (choice_obj), FALSE);
114  param_table = g_hash_table_lookup (qof_choice_table, choice_obj);
115  choices = g_hash_table_lookup (param_table, param_name);
116  result = g_list_find (choices, choice);
117  if (!result)
118  return FALSE;
119  return TRUE;
120 }
const gchar * QofIdType
Definition: qofid.h:81
gboolean qof_choice_create(gchar *type)
Set an object as using QOF_TYPE_CHOICE.
Definition: qofchoice.c:60
#define LEAVE(format, args...)
Definition: qoflog.h:227
GList * qof_object_get_choices(QofIdType type, QofParam *param)
Return the list of all object types usable with this parameter.
Definition: qofchoice.c:93
Linking one entity to other entities of many possible types.
gboolean qof_object_is_choice(QofIdType type)
Does this object contain a choice parameter?
Definition: qofchoice.c:45
gboolean qof_choice_add_class(gchar *select, gchar *option, gchar *param_name)
Add the choices for this parameter to the object.
Definition: qofchoice.c:74
gboolean qof_choice_check(gchar *choice_obj, gchar *param_name, gchar *choice)
Is the choice valid for this param_name?
Definition: qofchoice.c:107
#define ENTER(format, args...)
Definition: qoflog.h:217
const gchar * QofLogModule
Definition: qofid.h:85