1 /* ************************************************************************** */
2 /* */
3 /* Copyright (C) 2000-2008 Cédric Auger (cedric@grisbi.org) */
4 /* 2003-2008 Benjamin Drieu (bdrieu@april.org) */
5 /* https://www.grisbi.org */
6 /* */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or */
10 /* (at your option) any later version. */
11 /* */
12 /* This program is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
15 /* GNU General Public License for more details. */
16 /* */
17 /* You should have received a copy of the GNU General Public License */
18 /* along with this program; if not, write to the Free Software */
19 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20 /* */
21 /* ************************************************************************** */
22
23 /**
24 * \file gsb_data_bank.c
25 * work with the bank structure, no GUI here
26 */
27
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "include.h"
34
35 /*START_INCLUDE*/
36 #include "gsb_data_bank.h"
37 #include "gsb_data_account.h"
38 #include "utils_str.h"
39 /*END_INCLUDE*/
40
41
42 /* these two macros are here to reduce
43 * amount of code for setters and getters */
44 #define BANK_GET_OR_RETURN(bank, number, ret) \
45 bank = gsb_data_bank_get_structure ( number ); \
46 if ( !bank ) \
47 return ret;
48 #define BANK_SET_FIELD(bank, field, value) \
49 g_free (bank -> field); \
50 bank -> field = my_strdup (value);
51
52
53 /**
54 * \struct
55 * Describe a bank
56 */
57 typedef struct
58 {
59 /* WARNING : cannot set guint for bank_number because a bug before (and after ?)
60 * 0.6 sometimes set -1 to bank_number and big pb after that if we set guint here */
61 gint bank_number;
62 gchar *bank_name;
63 gchar *bank_code;
64 gchar *bank_BIC; /* BIC number */
65
66 gchar *bank_address;
67 gchar *bank_tel;
68 gchar *bank_mail;
69 gchar *bank_web;
70 gchar *bank_note;
71
72 gchar *correspondent_name;
73 gchar *correspondent_tel;
74 gchar *correspondent_mail;
75 gchar *correspondent_fax;
76 } struct_bank;
77
78 /*START_STATIC*/
79 static void _gsb_data_bank_free ( struct_bank* bank);
80 static gpointer gsb_data_bank_get_structure ( gint bank_number );
81 /*END_STATIC*/
82
83 /*START_EXTERN*/
84 /*END_EXTERN*/
85
86 /** contains the g_slist of struct_bank */
87 static GSList *bank_list = NULL;
88
89 /** a pointer to the last bank used (to increase the speed) */
90 static struct_bank *bank_buffer;
91
92
93 /**
94 * set the banks global variables to NULL, usually when we init all the global variables
95 *
96 * \param none
97 *
98 * \return FALSE
99 * */
gsb_data_bank_init_variables(void)100 gboolean gsb_data_bank_init_variables ( void )
101 {
102 if ( bank_list )
103 {
104 g_slist_free_full ( bank_list, (GDestroyNotify) _gsb_data_bank_free );
105 bank_list = NULL;
106 }
107
108 bank_buffer = NULL;
109
110 return FALSE;
111 }
112
113
114 /**
115 * find and return the structure of the bank asked
116 *
117 * \param bank_number number of bank
118 *
119 * \return the adr of the struct of the bank (NULL if doesn't exit)
120 * */
gsb_data_bank_get_structure(gint bank_number)121 gpointer gsb_data_bank_get_structure ( gint bank_number )
122 {
123 GSList *tmp;
124
125 if (!bank_number)
126 return NULL;
127
128 /* before checking all the banks, we check the buffer */
129 if ( bank_buffer
130 &&
131 bank_buffer -> bank_number == bank_number )
132 return bank_buffer;
133
134 tmp = bank_list;
135
136 while ( tmp )
137 {
138 struct_bank *bank;
139
140 bank = tmp -> data;
141
142 if ( bank -> bank_number == bank_number )
143 {
144 bank_buffer = bank;
145 return bank;
146 }
147 tmp = tmp -> next;
148 }
149 return NULL;
150 }
151
152 /**
153 * return the number of the bank given in param
154 *
155 * \param bank_ptr a pointer to the struct of the bank
156 *
157 * \return the number of the bank, 0 if problem
158 * */
gsb_data_bank_get_no_bank(gpointer bank_ptr)159 gint gsb_data_bank_get_no_bank ( gpointer bank_ptr )
160 {
161 struct_bank *bank;
162
163 if ( !bank_ptr )
164 return 0;
165
166 bank = bank_ptr;
167 bank_buffer = bank;
168 return bank -> bank_number;
169 }
170
171
172 /**
173 * give the g_slist of bank structure
174 *
175 * \param none
176 *
177 * \return the g_slist of banks structure
178 * */
gsb_data_bank_get_bank_list(void)179 GSList *gsb_data_bank_get_bank_list ( void )
180 {
181 return bank_list;
182 }
183
184
185
186 /**
187 * find and return the last number of bank
188 *
189 * \param none
190 *
191 * \return last number of bank
192 * */
gsb_data_bank_max_number(void)193 gint gsb_data_bank_max_number ( void )
194 {
195 GSList *tmp;
196 gint number_tmp = 0;
197
198 tmp = bank_list;
199
200 while ( tmp )
201 {
202 struct_bank *bank;
203
204 bank = tmp -> data;
205
206 if ( bank -> bank_number > number_tmp )
207 number_tmp = bank -> bank_number;
208
209 tmp = tmp -> next;
210 }
211 return number_tmp;
212 }
213
214
215 /**
216 * create a new bank, give it a number, append it to the list
217 * and return the number
218 *
219 * \param name the name of the bank (can be freed after, it's a copy) or NULL
220 *
221 * \return the number of the new bank
222 * */
gsb_data_bank_new(const gchar * name)223 gint gsb_data_bank_new ( const gchar *name )
224 {
225 struct_bank *bank;
226
227 bank = g_malloc0 ( sizeof ( struct_bank ));
228 bank -> bank_number = gsb_data_bank_max_number () + 1;
229
230 if (name)
231 bank -> bank_name = my_strdup (name);
232
233 bank_list = g_slist_append ( bank_list, bank );
234
235 return bank -> bank_number;
236 }
237
238 /**
239 * This internal function is called to free the memory used by a struct_bank structure
240 */
_gsb_data_bank_free(struct_bank * bank)241 static void _gsb_data_bank_free ( struct_bank* bank)
242 {
243 if ( !bank )
244 return;
245
246 /* free string fields */
247 g_free ( bank -> bank_name );
248 g_free ( bank -> bank_code );
249 g_free ( bank -> bank_BIC );
250 g_free ( bank -> bank_address );
251 g_free (bank->bank_tel);
252 g_free (bank->bank_mail);
253 g_free ( bank -> bank_web );
254 g_free ( bank -> bank_note );
255 g_free ( bank -> correspondent_name );
256 g_free ( bank -> correspondent_tel );
257 g_free ( bank -> correspondent_mail );
258 g_free ( bank -> correspondent_fax );
259
260 g_free ( bank );
261
262 if ( bank_buffer == bank )
263 bank_buffer = NULL;
264 }
265
266 /**
267 * remove a bank, remove also the number in the accounts linked to that bank
268 *
269 * \param bank_number the bank we want to remove
270 *
271 * \return TRUE ok
272 * */
gsb_data_bank_remove(gint bank_number)273 gboolean gsb_data_bank_remove ( gint bank_number )
274 {
275 struct_bank *bank;
276 GSList *list_tmp;
277
278 bank = gsb_data_bank_get_structure ( bank_number );
279
280 if (!bank)
281 return FALSE;
282
283 bank_list = g_slist_remove ( bank_list,
284 bank );
285 _gsb_data_bank_free ( bank );
286
287 /* remove that bank of the accounts */
288 list_tmp = gsb_data_account_get_list_accounts ();
289
290 while (list_tmp)
291 {
292 gint account_number = gsb_data_account_get_no_account (list_tmp -> data);
293
294 if ( gsb_data_account_get_bank (account_number) == bank_number )
295 gsb_data_account_set_bank ( account_number, 0 );
296 list_tmp = list_tmp -> next;
297 }
298
299 return TRUE;
300 }
301
302
303 /**
304 * set a new number for the bank
305 * normally used only while loading the file because
306 * the number are given automaticly
307 *
308 * \param bank_number the number of the bank
309 * \param new_no_bank the new number of the bank
310 *
311 * \return the new number or 0 if the bank doen't exist
312 * */
gsb_data_bank_set_new_number(gint bank_number,gint new_no_bank)313 gint gsb_data_bank_set_new_number ( gint bank_number,
314 gint new_no_bank )
315 {
316 struct_bank *bank;
317 BANK_GET_OR_RETURN(bank, bank_number, 0);
318 bank -> bank_number = new_no_bank;
319 return new_no_bank;
320 }
321
322
323 /**
324 * Setters and getters are defined just after.
325 *
326 * Each setter takes a string as parameter. So the field of the structure is
327 * first freed and the value is duplicated. It is freed when the bank is
328 * destroyed (by _gsb_data_bank_free).
329 * Setters return TRUE on success, FALSE otherwise
330 *
331 * Each getter returns a pointer on a string, which must not be freed.
332 * Setters return the pointer on success, NULL otherwise
333 */
334
335
336 /**
337 * Getter for the bank_name
338 */
gsb_data_bank_get_name(gint bank_number)339 const gchar *gsb_data_bank_get_name ( gint bank_number )
340 {
341 struct_bank *bank;
342 BANK_GET_OR_RETURN(bank, bank_number, NULL);
343 return bank -> bank_name;
344 }
345 /**
346 * Setter for the bank_name
347 */
gsb_data_bank_set_name(gint bank_number,const gchar * name)348 gboolean gsb_data_bank_set_name ( gint bank_number,
349 const gchar *name )
350 {
351 struct_bank *bank;
352 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
353 BANK_SET_FIELD(bank, bank_name, name);
354 return TRUE;
355 }
356
357
358 /**
359 * Getter for the bank_code
360 */
gsb_data_bank_get_code(gint bank_number)361 const gchar *gsb_data_bank_get_code ( gint bank_number )
362 {
363 struct_bank *bank;
364 BANK_GET_OR_RETURN(bank, bank_number, NULL);
365 return bank -> bank_code;
366 }
367 /**
368 * Setter for the bank_code
369 */
gsb_data_bank_set_code(gint bank_number,const gchar * bank_code)370 gboolean gsb_data_bank_set_code ( gint bank_number,
371 const gchar *bank_code )
372 {
373 struct_bank *bank;
374 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
375 BANK_SET_FIELD(bank, bank_code, bank_code);
376 return TRUE;
377 }
378
379
380 /**
381 * Getter for the bank_address
382 */
gsb_data_bank_get_bank_address(gint bank_number)383 const gchar *gsb_data_bank_get_bank_address ( gint bank_number )
384 {
385 struct_bank *bank;
386 BANK_GET_OR_RETURN(bank, bank_number, NULL);
387 return bank -> bank_address;
388 }
389 /**
390 * Setter for the bank_address
391 */
gsb_data_bank_set_bank_address(gint bank_number,const gchar * bank_address)392 gboolean gsb_data_bank_set_bank_address ( gint bank_number,
393 const gchar *bank_address )
394 {
395 struct_bank *bank;
396 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
397 BANK_SET_FIELD(bank, bank_address, bank_address);
398 return TRUE;
399 }
400
401
402 /**
403 * Getter for the bank_tel
404 */
gsb_data_bank_get_bank_tel(gint bank_number)405 const gchar *gsb_data_bank_get_bank_tel ( gint bank_number )
406 {
407 struct_bank *bank;
408 BANK_GET_OR_RETURN(bank, bank_number, NULL);
409 return bank -> bank_tel;
410 }
411 /**
412 * Setter for the bank_tel
413 */
gsb_data_bank_set_bank_tel(gint bank_number,const gchar * bank_tel)414 gboolean gsb_data_bank_set_bank_tel (gint bank_number,
415 const gchar *bank_tel)
416 {
417 struct_bank *bank;
418
419 bank = gsb_data_bank_get_structure (bank_number);
420 if (!bank)
421 return FALSE;
422
423 if (bank->bank_tel)
424 g_free (bank->bank_tel);
425 bank->bank_tel = my_strdup (bank_tel);
426
427 return TRUE;
428 }
429
430
431 /**
432 * Getter for the bank_mail
433 */
gsb_data_bank_get_bank_mail(gint bank_number)434 const gchar *gsb_data_bank_get_bank_mail ( gint bank_number )
435 {
436 struct_bank *bank;
437 BANK_GET_OR_RETURN(bank, bank_number, NULL);
438 return bank -> bank_mail;
439 }
440 /**
441 * Setter for the bank_mail
442 */
gsb_data_bank_set_bank_mail(gint bank_number,const gchar * bank_mail)443 gboolean gsb_data_bank_set_bank_mail ( gint bank_number,
444 const gchar *bank_mail )
445 {
446 struct_bank *bank;
447 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
448 BANK_SET_FIELD(bank, bank_mail, bank_mail);
449 return TRUE;
450 }
451
452
453 /**
454 * Getter for the bank_web
455 */
gsb_data_bank_get_bank_web(gint bank_number)456 const gchar *gsb_data_bank_get_bank_web ( gint bank_number )
457 {
458 struct_bank *bank;
459 BANK_GET_OR_RETURN(bank, bank_number, NULL);
460 return bank -> bank_web;
461 }
462 /**
463 * Setter for the bank_web
464 */
gsb_data_bank_set_bank_web(gint bank_number,const gchar * bank_web)465 gboolean gsb_data_bank_set_bank_web ( gint bank_number,
466 const gchar *bank_web )
467 {
468 struct_bank *bank;
469 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
470 BANK_SET_FIELD(bank, bank_web, bank_web);
471 return TRUE;
472 }
473
474
475 /**
476 * Getter for the bank_note
477 */
gsb_data_bank_get_bank_note(gint bank_number)478 const gchar *gsb_data_bank_get_bank_note ( gint bank_number )
479 {
480 struct_bank *bank;
481 BANK_GET_OR_RETURN(bank, bank_number, NULL);
482 return bank -> bank_note;
483 }
484 /**
485 * Setter for the bank_note
486 */
gsb_data_bank_set_bank_note(gint bank_number,const gchar * bank_note)487 gboolean gsb_data_bank_set_bank_note ( gint bank_number,
488 const gchar *bank_note )
489 {
490 struct_bank *bank;
491 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
492 BANK_SET_FIELD(bank, bank_note, bank_note);
493 return TRUE;
494 }
495
496
497 /**
498 * Getter for the correspondent_name
499 */
gsb_data_bank_get_correspondent_name(gint bank_number)500 const gchar *gsb_data_bank_get_correspondent_name ( gint bank_number )
501 {
502 struct_bank *bank;
503 BANK_GET_OR_RETURN(bank, bank_number, NULL);
504 return bank -> correspondent_name;
505 }
506 /**
507 * Setter for the correspondent_name
508 */
gsb_data_bank_set_correspondent_name(gint bank_number,const gchar * correspondent_name)509 gboolean gsb_data_bank_set_correspondent_name ( gint bank_number,
510 const gchar *correspondent_name )
511 {
512 struct_bank *bank;
513 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
514 BANK_SET_FIELD(bank, correspondent_name, correspondent_name);
515 return TRUE;
516 }
517
518
519 /**
520 * Getter for the correspondent_tel
521 */
gsb_data_bank_get_correspondent_tel(gint bank_number)522 const gchar *gsb_data_bank_get_correspondent_tel ( gint bank_number )
523 {
524 struct_bank *bank;
525 BANK_GET_OR_RETURN(bank, bank_number, NULL);
526 return bank -> correspondent_tel;
527 }
528 /**
529 * Setter for the correspondent_tel
530 */
gsb_data_bank_set_correspondent_tel(gint bank_number,const gchar * correspondent_tel)531 gboolean gsb_data_bank_set_correspondent_tel ( gint bank_number,
532 const gchar *correspondent_tel )
533 {
534 struct_bank *bank;
535 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
536 BANK_SET_FIELD(bank, correspondent_tel, correspondent_tel);
537 return TRUE;
538 }
539
540
541 /**
542 * Getter for the correspondent_mail
543 */
gsb_data_bank_get_correspondent_mail(gint bank_number)544 const gchar *gsb_data_bank_get_correspondent_mail ( gint bank_number )
545 {
546 struct_bank *bank;
547 BANK_GET_OR_RETURN(bank, bank_number, NULL);
548 return bank -> correspondent_mail;
549 }
550 /**
551 * Setter for the correspondent_mail
552 */
gsb_data_bank_set_correspondent_mail(gint bank_number,const gchar * correspondent_mail)553 gboolean gsb_data_bank_set_correspondent_mail ( gint bank_number,
554 const gchar *correspondent_mail )
555 {
556 struct_bank *bank;
557 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
558 BANK_SET_FIELD(bank, correspondent_mail, correspondent_mail);
559 return TRUE;
560 }
561
562
563 /**
564 * Getter for the correspondent_fax
565 */
gsb_data_bank_get_correspondent_fax(gint bank_number)566 const gchar *gsb_data_bank_get_correspondent_fax ( gint bank_number )
567 {
568 struct_bank *bank;
569 BANK_GET_OR_RETURN(bank, bank_number, NULL);
570 return bank -> correspondent_fax;
571 }
572 /**
573 * Setter for the correspondent_fax
574 */
gsb_data_bank_set_correspondent_fax(gint bank_number,const gchar * correspondent_fax)575 gboolean gsb_data_bank_set_correspondent_fax ( gint bank_number,
576 const gchar *correspondent_fax )
577 {
578 struct_bank *bank;
579 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
580 BANK_SET_FIELD(bank, correspondent_fax, correspondent_fax);
581 return TRUE;
582 }
583
584
585 /**
586 * Getter for BIC code
587 */
gsb_data_bank_get_bic(gint bank_number)588 const gchar *gsb_data_bank_get_bic ( gint bank_number )
589 {
590 struct_bank *bank;
591 BANK_GET_OR_RETURN(bank, bank_number, NULL);
592 return bank -> bank_BIC;
593 }
594 /**
595 * Setter for BIC code
596 */
gsb_data_bank_set_bic(gint bank_number,const gchar * bank_BIC)597 gboolean gsb_data_bank_set_bic ( gint bank_number, const gchar *bank_BIC )
598 {
599 struct_bank *bank;
600 BANK_GET_OR_RETURN(bank, bank_number, FALSE);
601 BANK_SET_FIELD(bank, bank_BIC, bank_BIC);
602 return TRUE;
603 }
604
605