1 /*
2  *  Copyright (C) 2002 Derek Atkins
3  *
4  *  Authors: Derek Atkins <warlord@MIT.EDU>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <string.h>
27 #include <gtk/gtk.h>
28 
29 #include "qof.h"
30 #include "Account.h"		/* for ACCOUNT_MATCH_ALL_TYPE */
31 #include "Transaction.h"	/* for RECONCILED_MATCH_TYPE */
32 
33 #include "search-core-type.h"
34 #include "search-string.h"
35 #include "search-reconciled.h"
36 #include "search-date.h"
37 #include "search-double.h"
38 #include "search-int64.h"
39 #include "search-numeric.h"
40 #include "search-boolean.h"
41 #include "search-account.h"
42 
43 static void grab_focus (GNCSearchCoreType *fe);
44 static void editable_enters (GNCSearchCoreType *fe);
45 static gboolean validate (GNCSearchCoreType *fe);
46 
47 static void gnc_search_core_type_class_init	(GNCSearchCoreTypeClass *klass);
48 static void gnc_search_core_type_init	(GNCSearchCoreType *gspaper);
49 static void gnc_search_core_type_finalize	(GObject *obj);
50 
51 typedef struct _GNCSearchCoreTypePrivate GNCSearchCoreTypePrivate;
52 
53 struct _GNCSearchCoreTypePrivate
54 {
55     gpointer dummy;
56 };
57 
G_DEFINE_TYPE_WITH_PRIVATE(GNCSearchCoreType,gnc_search_core_type,G_TYPE_OBJECT)58 G_DEFINE_TYPE_WITH_PRIVATE(GNCSearchCoreType, gnc_search_core_type, G_TYPE_OBJECT)
59 
60 #define _PRIVATE(o) \
61    ((GNCSearchCoreTypePrivate*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_SEARCH_CORE_TYPE))
62 
63 static GObjectClass *parent_class;
64 
65 static GHashTable *typeTable = NULL;
66 
67 static void
68 gnc_search_core_type_class_init (GNCSearchCoreTypeClass *klass)
69 {
70     GObjectClass *object_class;
71 
72     object_class = G_OBJECT_CLASS (klass);
73     parent_class = g_type_class_peek_parent (klass);
74 
75     object_class->finalize = gnc_search_core_type_finalize;
76 
77     /* override methods */
78     klass->validate = validate;
79     klass->grab_focus = grab_focus;
80     klass->editable_enters = editable_enters;
81 }
82 
83 static void
gnc_search_core_type_init(GNCSearchCoreType * o)84 gnc_search_core_type_init (GNCSearchCoreType *o)
85 {
86 }
87 
88 static void
gnc_search_core_type_finalize(GObject * obj)89 gnc_search_core_type_finalize (GObject *obj)
90 {
91     GNCSearchCoreType *o = (GNCSearchCoreType *)obj;
92     g_assert (GNC_IS_SEARCH_CORE_TYPE (o));
93 
94     G_OBJECT_CLASS (parent_class)->finalize(obj);
95 }
96 
97 /**
98  * gnc_search_core_type_new:
99  *
100  * Create a new GNCSearchCoreType object.
101  *
102  * Return value: A new #GNCSearchCoreType object.
103  **/
104 GNCSearchCoreType *
gnc_search_core_type_new(void)105 gnc_search_core_type_new (void)
106 {
107     GNCSearchCoreType *o;
108 
109     o = g_object_new (GNC_TYPE_SEARCH_CORE_TYPE, NULL);
110 
111     return o;
112 }
113 
114 void
gnc_search_core_type_editable_enters(GNCSearchCoreType * fe)115 gnc_search_core_type_editable_enters (GNCSearchCoreType *fe)
116 {
117     GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->editable_enters (fe);
118 }
119 
120 void
gnc_search_core_type_grab_focus(GNCSearchCoreType * fe)121 gnc_search_core_type_grab_focus (GNCSearchCoreType *fe)
122 {
123     GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->grab_focus (fe);
124 }
125 
126 void
gnc_search_core_type_pass_parent(GNCSearchCoreType * fe,gpointer parent)127 gnc_search_core_type_pass_parent (GNCSearchCoreType *fe, gpointer parent)
128 {
129     GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->pass_parent (fe, parent);
130 }
131 
132 gboolean
gnc_search_core_type_validate(GNCSearchCoreType * fe)133 gnc_search_core_type_validate (GNCSearchCoreType *fe)
134 {
135     return GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->validate (fe);
136 }
137 
138 /**
139  * gnc_search_core_type_clone:
140  * @fe: search core_type
141  *
142  * Clones the GNCSearchCoreType @fe.
143  *
144  * Return value:
145  **/
146 GNCSearchCoreType *
gnc_search_core_type_clone(GNCSearchCoreType * fe)147 gnc_search_core_type_clone (GNCSearchCoreType *fe)
148 {
149     return GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->clone(fe);
150 }
151 
152 /**
153  * gnc_search_core_type_get_widget:
154  * @fe: search core_type
155  * @node: xml node
156  *
157  * Create a widget to represent this core_type.
158  *
159  * Return value:
160  **/
161 GtkWidget *
gnc_search_core_type_get_widget(GNCSearchCoreType * fe)162 gnc_search_core_type_get_widget (GNCSearchCoreType *fe)
163 {
164     return GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->get_widget(fe);
165 }
166 
167 /**
168  * gnc_search_core_type_get_predicate:
169  * @fe: search core_type
170  *
171  * Create a Predicate Data that matches this core_type
172  *
173  * Return value:
174  **/
175 QofQueryPredData*
gnc_search_core_type_get_predicate(GNCSearchCoreType * fe)176 gnc_search_core_type_get_predicate (GNCSearchCoreType *fe)
177 {
178     return GNC_SEARCH_CORE_TYPE_GET_CLASS (fe)->get_predicate(fe);
179 }
180 
181 /**
182  * gnc_search_core_type_new_type_name:
183  * @type: search core_type type
184  *
185  * Create a new search core_type based on its type name.
186  *
187  * Return value:
188  **/
189 GNCSearchCoreType *
gnc_search_core_type_new_type_name(const char * type)190 gnc_search_core_type_new_type_name (const char *type)
191 {
192     GNCSearchCoreNew fcn;
193 
194     g_return_val_if_fail (typeTable != NULL, NULL);
195 
196     if (type == NULL)
197         return NULL;
198 
199     fcn = g_hash_table_lookup (typeTable, type);
200     if (fcn)
201     {
202         return ((fcn)());
203     }
204     else
205     {
206         g_warning("Unknown search type '%s'", type);
207         return NULL;
208     }
209 }
210 
211 /* default implementations */
212 static gboolean
validate(GNCSearchCoreType * fe)213 validate (GNCSearchCoreType *fe)
214 {
215     return TRUE;
216 }
217 
218 static void
grab_focus(GNCSearchCoreType * fe)219 grab_focus (GNCSearchCoreType *fe)
220 {
221     return;
222 }
223 
224 static void
editable_enters(GNCSearchCoreType * fe)225 editable_enters (GNCSearchCoreType *fe)
226 {
227     return;
228 }
229 
230 void
gnc_search_core_register_type(const char * type_name,GNCSearchCoreNew fcn)231 gnc_search_core_register_type (const char *type_name, GNCSearchCoreNew fcn)
232 {
233     g_return_if_fail (type_name || *type_name || fcn);
234     g_return_if_fail (typeTable);
235 
236     g_hash_table_insert (typeTable, (char *) type_name, (gpointer) fcn);
237 }
238 
239 static void
init_table(void)240 init_table (void)
241 {
242     gnc_search_core_register_type (QOF_TYPE_STRING,
243                                    (GNCSearchCoreNew) gnc_search_string_new);
244     gnc_search_core_register_type (QOF_TYPE_DATE,
245                                    (GNCSearchCoreNew) gnc_search_date_new);
246     gnc_search_core_register_type (QOF_TYPE_INT64,
247                                    (GNCSearchCoreNew) gnc_search_int64_new);
248     gnc_search_core_register_type (QOF_TYPE_DOUBLE,
249                                    (GNCSearchCoreNew) gnc_search_double_new);
250     gnc_search_core_register_type (QOF_TYPE_NUMERIC,
251                                    (GNCSearchCoreNew) gnc_search_numeric_new);
252     gnc_search_core_register_type (QOF_TYPE_DEBCRED,
253                                    (GNCSearchCoreNew)
254                                    gnc_search_numeric_debcred_new);
255     gnc_search_core_register_type (QOF_TYPE_BOOLEAN,
256                                    (GNCSearchCoreNew) gnc_search_boolean_new);
257     gnc_search_core_register_type (GNC_ID_ACCOUNT,
258                                    (GNCSearchCoreNew) gnc_search_account_new);
259     gnc_search_core_register_type (ACCOUNT_MATCH_ALL_TYPE,
260                                    (GNCSearchCoreNew)
261                                    gnc_search_account_matchall_new);
262     gnc_search_core_register_type (RECONCILED_MATCH_TYPE,
263                                    (GNCSearchCoreNew) gnc_search_reconciled_new);
264 }
265 
266 void
gnc_search_core_initialize(void)267 gnc_search_core_initialize (void)
268 {
269     g_return_if_fail (typeTable == NULL);
270 
271     typeTable = g_hash_table_new (g_str_hash, g_str_equal);
272     init_table ();
273 }
274 
275 void
gnc_search_core_finalize(void)276 gnc_search_core_finalize (void)
277 {
278     g_return_if_fail (typeTable != NULL);
279 
280     g_hash_table_destroy (typeTable);
281     typeTable = NULL;
282 }
283