GnuCash  4.6-313-gb6cbc9786+
engine-helpers.c
1 /********************************************************************\
2  * engine-helpers.c -- gnucash engine helper functions *
3  * Copyright (C) 2000 Linas Vepstas <linas@linas.org> *
4  * Copyright (C) 2001 Linux Developers Group, Inc. *
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 *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 
25 #include <config.h>
26 
27 #include <string.h>
28 
29 #include "Account.h"
30 #include "engine-helpers.h"
31 #include "gnc-date.h"
32 #include "gnc-engine.h"
33 #include "gnc-session.h"
34 #include <qof.h>
35 #include <qofbookslots.h>
36 
41 #include "qofquery-p.h"
42 #include "qofquerycore-p.h"
43 
44 #define FUNC_NAME G_STRFUNC
45 
46 static QofLogModule log_module = GNC_MOD_ENGINE;
47 
55 const char *
56 gnc_get_num_action (const Transaction *trans, const Split *split)
57 {
58  if (trans && !split)
59  return xaccTransGetNum(trans);
60  if (split && !trans)
61  return xaccSplitGetAction(split);
62  if (trans && split)
63  {
64  QofBook* book = qof_session_get_book(gnc_get_current_session ());
65  if (!book)
66  {
67  PERR("Session has no book but has a transaction or split!");
68  return NULL;
69  }
71  return xaccSplitGetAction(split);
72  else
73  return xaccTransGetNum(trans);
74  }
75  else return NULL;
76 }
77 
84 const char *
85 gnc_get_action_num (const Transaction *trans, const Split *split)
86 {
87  gboolean num_action = qof_book_use_split_action_for_num_field
88  (qof_session_get_book(gnc_get_current_session ()));
89 
90  if (trans && !split)
91  return xaccTransGetNum(trans);
92  if (split && !trans)
93  return xaccSplitGetAction(split);
94  if (trans && split)
95  {
96  if (num_action)
97  return xaccTransGetNum(trans);
98  else
99  return xaccSplitGetAction(split);
100  }
101  else return NULL;
102 }
103 
115 void
116 gnc_set_num_action (Transaction *trans, Split *split,
117  const char *num, const char *action)
118 {
119  gboolean num_action = qof_book_use_split_action_for_num_field
120  (qof_session_get_book(gnc_get_current_session ()));
121 
122  if (trans && num && !split && !action)
123  {
124  xaccTransSetNum (trans, num);
125  return;
126  }
127 
128  if (!trans && !num && split && action)
129  {
130  xaccSplitSetAction (split, action);
131  return;
132  }
133 
134  if (trans)
135  {
136  if (!num_action && num)
137  xaccTransSetNum (trans, num);
138  if (num_action && action)
139  xaccTransSetNum (trans, action);
140  }
141 
142  if (split)
143  {
144  if (!num_action && action)
145  xaccSplitSetAction (split, action);
146  if (num_action && num)
147  xaccSplitSetAction (split, num);
148  }
149 }
150 
151 /************************************************************/
152 /* Notification of Book Option Changes */
153 /************************************************************/
154 
155 static GOnce bo_init_once = G_ONCE_INIT;
156 static GHashTable *bo_callback_hash = NULL;
157 static GHookList *bo_final_hook_list = NULL;
158 
159 static gpointer
160 bo_init (gpointer unused)
161 {
162  bo_callback_hash = g_hash_table_new(g_str_hash, g_str_equal);
163 
164  bo_final_hook_list = g_malloc(sizeof(GHookList));
165  g_hook_list_init(bo_final_hook_list, sizeof(GHook));
166  return NULL;
167 }
168 
169 static void
170 bo_call_hook (GHook *hook, gpointer data)
171 {
172  ((GFunc)hook->func)(data, hook->data);
173 }
174 
177 void
178 gnc_book_option_num_field_source_change (gboolean num_action)
179 {
180  GHookList *hook_list;
181  const gchar *key = OPTION_NAME_NUM_FIELD_SOURCE;
182 
183  g_once(&bo_init_once, bo_init, NULL);
184 
185  hook_list = g_hash_table_lookup(bo_callback_hash, key);
186  if (hook_list != NULL)
187  g_hook_list_marshal(hook_list, TRUE, bo_call_hook, &num_action);
188  g_hook_list_invoke(bo_final_hook_list, TRUE);
189 }
190 
193 void
194 gnc_book_option_book_currency_selected (gboolean use_book_currency)
195 {
196  GHookList *hook_list;
197  const gchar *key = OPTION_NAME_BOOK_CURRENCY;
198 
199  g_once(&bo_init_once, bo_init, NULL);
200 
201  hook_list = g_hash_table_lookup(bo_callback_hash, key);
202  if (hook_list != NULL)
203  g_hook_list_marshal(hook_list, TRUE, bo_call_hook, &use_book_currency);
204  g_hook_list_invoke(bo_final_hook_list, TRUE);
205 }
206 
207 void
208 gnc_book_option_register_cb (gchar *key, GncBOCb func, gpointer user_data)
209 {
210  GHookList *hook_list;
211  GHook *hook;
212 
213  g_once(&bo_init_once, bo_init, NULL);
214  hook_list = g_hash_table_lookup(bo_callback_hash, key);
215  if (hook_list == NULL)
216  {
217  hook_list = g_malloc(sizeof(GHookList));
218  g_hook_list_init(hook_list, sizeof(GHook));
219  g_hash_table_insert(bo_callback_hash, (gpointer)key, hook_list);
220  }
221 
222  hook = g_hook_find_func_data(hook_list, TRUE, func, user_data);
223  if (hook != NULL)
224  {
225  return;
226  }
227 
228  hook = g_hook_alloc(hook_list);
229  hook->func = func;
230  hook->data = user_data;
231  g_hook_append(hook_list, hook);
232 }
233 
234 void
235 gnc_book_option_remove_cb (gchar *key, GncBOCb func, gpointer user_data)
236 {
237  GHookList *hook_list;
238  GHook *hook;
239 
240  g_once(&bo_init_once, bo_init, NULL);
241  hook_list = g_hash_table_lookup(bo_callback_hash, key);
242  if (hook_list == NULL)
243  return;
244  hook = g_hook_find_func_data(hook_list, TRUE, func, user_data);
245  if (hook == NULL)
246  return;
247 
248  g_hook_destroy_link(hook_list, hook);
249  if (hook_list->hooks == NULL)
250  {
251  g_hash_table_remove(bo_callback_hash, key);
252  g_free(hook_list);
253  }
254 }
255 
256 
void xaccSplitSetAction(Split *split, const char *actn)
The Action is an arbitrary user-assigned string.
Definition: Split.c:1747
Date and Time handling routines.
gboolean qof_book_use_split_action_for_num_field(const QofBook *book)
Returns TRUE if this book uses split action field as the &#39;Num&#39; field, FALSE if it uses transaction nu...
void xaccTransSetNum(Transaction *trans, const char *xnum)
Sets the transaction Number (or ID) field; rather than use this function directly, see &#39;gnc_set_num_action&#39; in engine/engine-helpers.c & .h which takes a user-set book option for selecting the source for the num-cell (the transaction-number or the split-action field) in registers/reports into account automatically.
Definition: Transaction.c:2173
const char * xaccTransGetNum(const Transaction *trans)
Gets the transaction Number (or ID) field; rather than use this function directly, see &#39;gnc_get_num_action&#39; and &#39;gnc_get_action_num&#39; in engine/engine-helpers.c & .h which takes a user-set book option for selecting the source for the num-cell (the transaction-number or the split-action field) in registers/reports into account automatically.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
Definition: qofsession.cpp:578
Account handling public routines.
All type declarations for the whole Gnucash engine.
const char * xaccSplitGetAction(const Split *split)
Returns the action string.