GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-owner.c
1 /*
2  * Copyright (C) 2002 Derek Atkins
3  *
4  * Authors: Derek Atkins <[email protected]>
5  *
6  * Copyright (c) 2006 David Hampton <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 
31 #include "qof.h"
32 #include "gnc-ui-util.h"
33 #include "gnc-gui-query.h"
34 #include "gncOwner.h"
35 
36 #include "business-gnome-utils.h"
37 #include "search-owner.h"
38 #include "search-core-utils.h"
39 
40 #define d(x)
41 
42 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
43 static gboolean gncs_validate (GNCSearchCoreType *fe);
44 static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
45 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
46 
47 static void gnc_search_owner_class_init (GNCSearchOwnerClass *klass);
48 static void gnc_search_owner_init (GNCSearchOwner *gspaper);
49 static void gnc_search_owner_finalize (GObject *obj);
50 
51 #define _PRIVATE(x) (((GNCSearchOwner *)(x))->priv)
52 
53 typedef struct _GNCSearchOwnerPrivate
54 {
55  GncOwner owner;
56  GtkWidget * owner_box;
57  GtkWidget * owner_choice;
59 
60 #define GNC_SEARCH_OWNER_GET_PRIVATE(o) \
61  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_OWNER, GNCSearchOwnerPrivate))
62 
63 static GNCSearchCoreTypeClass *parent_class;
64 
65 enum
66 {
67  LAST_SIGNAL
68 };
69 
70 #if LAST_SIGNAL > 0
71 static guint signals[LAST_SIGNAL] = { 0 };
72 #endif
73 
74 GType
75 gnc_search_owner_get_type (void)
76 {
77  static GType type = 0;
78 
79  if (!type)
80  {
81  GTypeInfo type_info =
82  {
83  sizeof(GNCSearchOwnerClass), /* class_size */
84  NULL, /* base_init */
85  NULL, /* base_finalize */
86  (GClassInitFunc)gnc_search_owner_class_init,
87  NULL, /* class_finalize */
88  NULL, /* class_data */
89  sizeof(GNCSearchOwner), /* */
90  0, /* n_preallocs */
91  (GInstanceInitFunc)gnc_search_owner_init,
92  };
93 
94  type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
95  "GNCSearchOwner",
96  &type_info, 0);
97  }
98 
99  return type;
100 }
101 
102 static void
103 gnc_search_owner_class_init (GNCSearchOwnerClass *klass)
104 {
105  GObjectClass *object_class;
106  GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
107 
108  object_class = G_OBJECT_CLASS (klass);
109  parent_class = g_type_class_peek_parent (klass);
110 
111  object_class->finalize = gnc_search_owner_finalize;
112 
113  /* override methods */
114  gnc_search_core_type->validate = gncs_validate;
115  gnc_search_core_type->get_widget = gncs_get_widget;
116  gnc_search_core_type->get_predicate = gncs_get_predicate;
117  gnc_search_core_type->clone = gncs_clone;
118 
119  g_type_class_add_private(klass, sizeof(GNCSearchOwnerPrivate));
120 }
121 
122 static void
123 gnc_search_owner_init (GNCSearchOwner *o)
124 {
125 }
126 
127 static void
128 gnc_search_owner_finalize (GObject *obj)
129 {
130  g_assert (IS_GNCSEARCH_OWNER (obj));
131 
132  G_OBJECT_CLASS (parent_class)->finalize(obj);
133 }
134 
143 gnc_search_owner_new (void)
144 {
145  GNCSearchOwner *o = g_object_new(gnc_search_owner_get_type (), NULL);
146  return o;
147 }
148 
149 static gboolean
150 gncs_validate (GNCSearchCoreType *fe)
151 {
152  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
153  GNCSearchOwnerPrivate *priv;
154  gboolean valid = TRUE;
155 
156  g_return_val_if_fail (fi, FALSE);
157  g_return_val_if_fail (IS_GNCSEARCH_OWNER (fi), FALSE);
158 
159  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fi);
160  if (priv->owner.owner.undefined == NULL)
161  {
162  valid = FALSE;
163  gnc_error_dialog (NULL, "%s", _("You have not selected an owner"));
164  }
165 
166  /* XXX */
167 
168  return valid;
169 }
170 
171 static int
172 owner_changed_cb (GtkWidget *widget, gpointer data)
173 {
174  GNCSearchOwner *fe = data;
175  GNCSearchOwnerPrivate *priv;
176 
177  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fe);
178  gnc_owner_get_owner (priv->owner_choice, &(priv->owner));
179  return FALSE;
180 }
181 
182 static void
183 set_owner_widget (GNCSearchOwner *fe)
184 {
185  GNCSearchOwnerPrivate *priv;
186 
187  /* Remove the old choice widget */
188  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fe);
189  if (priv->owner_choice)
190  gtk_container_remove (GTK_CONTAINER (priv->owner_box), priv->owner_choice);
191 
192  /* Create a new choice widget */
193  priv->owner_choice =
194  gnc_owner_select_create (NULL, priv->owner_box,
195  gnc_get_current_book(), &(priv->owner));
196 
197  /* Setup the "changed" callback */
198  g_signal_connect (G_OBJECT (priv->owner_choice), "changed",
199  G_CALLBACK (owner_changed_cb), fe);
200 
201  gtk_widget_show_all (priv->owner_choice);
202 }
203 
204 static void
205 type_combo_changed (GtkWidget *widget, GNCSearchOwner *fe)
206 {
207  GNCSearchOwnerPrivate *priv;
208  GncOwnerType type;
209 
210  g_return_if_fail(GTK_IS_COMBO_BOX(widget));
211 
212  type = gnc_combo_box_search_get_active(GTK_COMBO_BOX(widget));
213 
214  /* If the type changed or if we don't have a type create the owner_choice */
215  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fe);
216  if (type != gncOwnerGetType (&(priv->owner)))
217  {
218  priv->owner.type = type;
219  priv->owner.owner.undefined = NULL;
220  set_owner_widget (fe);
221  }
222  else if (priv->owner_choice == NULL)
223  set_owner_widget (fe);
224 }
225 
226 static GtkWidget *
227 make_type_menu (GNCSearchCoreType *fe)
228 {
229  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
230  GNCSearchOwnerPrivate *priv;
231  GtkComboBox *combo;
232  GncOwnerType type;
233 
234  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fi);
235  type = gncOwnerGetType (&(priv->owner));
236 
237  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
238  gnc_combo_box_search_add(combo, _("Customer"), GNC_OWNER_CUSTOMER);
239  gnc_combo_box_search_add(combo, _("Vendor"), GNC_OWNER_VENDOR);
240  gnc_combo_box_search_add(combo, _("Employee"), GNC_OWNER_EMPLOYEE);
241  gnc_combo_box_search_add(combo, _("Job"), GNC_OWNER_JOB);
242 
243  g_signal_connect (combo, "changed", G_CALLBACK (type_combo_changed), fe);
244  gnc_combo_box_search_set_active(combo, type);
245 
246  return GTK_WIDGET(combo);
247 
248 
249 }
250 
251 static GtkWidget *
252 make_how_menu (GNCSearchCoreType *fe)
253 {
254  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
255  GtkComboBox *combo;
256 
257  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
258  gnc_combo_box_search_add(combo, _("is"), QOF_GUID_MATCH_ANY);
259  gnc_combo_box_search_add(combo, _("is not"), QOF_GUID_MATCH_NONE);
260  gnc_combo_box_search_changed(combo, &fi->how);
261  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_GUID_MATCH_ANY);
262 
263  return GTK_WIDGET(combo);
264 }
265 
266 static GtkWidget *
267 gncs_get_widget (GNCSearchCoreType *fe)
268 {
269  GtkWidget *how_menu, *type_menu, *box;
270  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
271  GNCSearchOwnerPrivate *priv;
272 
273  g_return_val_if_fail (fi, NULL);
274  g_return_val_if_fail (IS_GNCSEARCH_OWNER (fi), NULL);
275 
276  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fi);
277  box = gtk_hbox_new (FALSE, 3);
278 
279  /* Build and connect the "how" option menu. */
280  how_menu = make_how_menu (fe);
281  gtk_box_pack_start (GTK_BOX (box), how_menu, FALSE, FALSE, 3);
282 
283  /* Create the owner box */
284  priv->owner_box = gtk_hbox_new (FALSE, 0);
285 
286  /* Build and connect the "type" option menu.
287  * Note that this will build the owner_choice and
288  * put it in the owner_box we just created.
289  */
290  type_menu = make_type_menu (fe);
291  gtk_box_pack_start (GTK_BOX (box), type_menu, FALSE, FALSE, 3);
292 
293  /* connect the owner box */
294  gtk_box_pack_start (GTK_BOX (box), priv->owner_box, FALSE, FALSE, 3);
295 
296  /* And return the box */
297  return box;
298 }
299 
300 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
301 {
302  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
303  GNCSearchOwnerPrivate *priv;
304  const GncGUID *guid;
305  GList *l = NULL;
306 
307  g_return_val_if_fail (fi, NULL);
308  g_return_val_if_fail (IS_GNCSEARCH_OWNER (fi), NULL);
309 
310  priv = GNC_SEARCH_OWNER_GET_PRIVATE(fi);
311  guid = gncOwnerGetGUID (&(priv->owner));
312  l = g_list_prepend (l, (gpointer)guid);
313 
314  return qof_query_guid_predicate (fi->how, l);
315 }
316 
317 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
318 {
319  GNCSearchOwner *se, *fse = (GNCSearchOwner *)fe;
320  GNCSearchOwnerPrivate *se_priv, *fse_priv;
321 
322  g_return_val_if_fail (fse, NULL);
323  g_return_val_if_fail (IS_GNCSEARCH_OWNER (fse), NULL);
324 
325  se = gnc_search_owner_new ();
326  se->how = fse->how;
327  se_priv = GNC_SEARCH_OWNER_GET_PRIVATE(se);
328  fse_priv = GNC_SEARCH_OWNER_GET_PRIVATE(fse);
329  gncOwnerCopy (&(fse_priv->owner), &(se_priv->owner));
330 
331  return (GNCSearchCoreType *)se;
332 }
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Definition: gncOwner.c:496
Business Interface: Object OWNERs.
utility functions for the GnuCash UI
Definition: guid.h:65
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Definition: gncOwner.c:201