GnuCash  5.6-150-g038405b370+
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 
191 void
192 gnc_book_option_register_cb (const gchar *key, GncBOCb func, gpointer user_data)
193 {
194  GHookList *hook_list;
195  GHook *hook;
196 
197  g_once(&bo_init_once, bo_init, NULL);
198  hook_list = g_hash_table_lookup(bo_callback_hash, key);
199  if (hook_list == NULL)
200  {
201  hook_list = g_malloc(sizeof(GHookList));
202  g_hook_list_init(hook_list, sizeof(GHook));
203  g_hash_table_insert(bo_callback_hash, (gpointer)key, hook_list);
204  }
205 
206  hook = g_hook_find_func_data(hook_list, TRUE, func, user_data);
207  if (hook != NULL)
208  {
209  return;
210  }
211 
212  hook = g_hook_alloc(hook_list);
213  hook->func = func;
214  hook->data = user_data;
215  g_hook_append(hook_list, hook);
216 }
217 
218 void
219 gnc_book_option_remove_cb (const gchar *key, GncBOCb func, gpointer user_data)
220 {
221  GHookList *hook_list;
222  GHook *hook;
223 
224  g_once(&bo_init_once, bo_init, NULL);
225  hook_list = g_hash_table_lookup(bo_callback_hash, key);
226  if (hook_list == NULL)
227  return;
228  hook = g_hook_find_func_data(hook_list, TRUE, func, user_data);
229  if (hook == NULL)
230  return;
231 
232  g_hook_destroy_link(hook_list, hook);
233  if (hook_list->hooks == NULL)
234  {
235  g_hash_table_remove(bo_callback_hash, key);
236  g_free(hook_list);
237  }
238 }
239 
240 
void xaccSplitSetAction(Split *split, const char *actn)
The Action is an arbitrary user-assigned string.
Definition: Split.cpp:1748
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.
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:574
Account handling public routines.
All type declarations for the whole Gnucash engine.
const char * xaccSplitGetAction(const Split *split)
Returns the action string.