1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41 da_pay_free(Payee
*item
)
43 DB( g_print("da_pay_free\n") );
46 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
57 DB( g_print("da_pay_malloc\n") );
58 return g_malloc0(sizeof(Payee
));
65 DB( g_print("da_pay_destroy\n") );
66 g_hash_table_destroy(GLOBALS
->h_pay
);
75 DB( g_print("da_pay_new\n") );
76 GLOBALS
->h_pay
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_pay_free
);
78 // insert our 'no payee'
79 item
= da_pay_malloc();
80 item
->name
= g_strdup("");
85 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
86 static void da_pay_max_key_ghfunc(gpointer key
, Payee
*item
, guint32
*max_key
)
88 *max_key
= MAX(*max_key
, item
->key
);
91 static gboolean
da_pay_name_grfunc(gpointer key
, Payee
*item
, gchar
*name
)
93 if( name
&& item
->name
)
95 if(!strcasecmp(name
, item
->name
))
104 * Return value: the number of elements
109 return g_hash_table_size(GLOBALS
->h_pay
);
114 da_pay_create_none(void)
119 DB( g_print("da_pay_insert none\n") );
121 pay = da_pay_malloc();
122 new_key = g_new0(guint32, 1);
125 pay->name = g_strdup("");
127 DB( g_print(" -> insert id: %d\n", *new_key) );
129 g_hash_table_insert(GLOBALS->h_pay, new_key, pay);
140 * remove an payee from the GHashTable
142 * Return value: TRUE if the key was found and removed
146 da_pay_remove(guint32 key
)
148 DB( g_print("da_pay_remove %d\n", key
) );
150 return g_hash_table_remove(GLOBALS
->h_pay
, &key
);
156 * insert an payee into the GHashTable
158 * Return value: TRUE if inserted
162 da_pay_insert(Payee
*item
)
166 DB( g_print("da_pay_insert\n") );
168 new_key
= g_new0(guint32
, 1);
169 *new_key
= item
->key
;
170 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
179 * append a new payee into the GHashTable
181 * Return value: TRUE if inserted
185 da_pay_append(Payee
*item
)
190 DB( g_print("da_pay_append\n") );
192 /* ensure no duplicate */
193 //g_strstrip(item->name);
194 if( item
->name
!= NULL
)
196 existitem
= da_pay_get_by_name( item
->name
);
197 if( existitem
== NULL
)
199 new_key
= g_new0(guint32
, 1);
200 *new_key
= da_pay_get_max_key() + 1;
201 item
->key
= *new_key
;
203 DB( g_print(" -> append id: %d\n", *new_key
) );
205 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
210 DB( g_print(" -> %s already exist: %d\n", item
->name
, item
->key
) );
216 * da_pay_get_max_key:
218 * Get the biggest key from the GHashTable
220 * Return value: the biggest key value
224 da_pay_get_max_key(void)
228 g_hash_table_foreach(GLOBALS
->h_pay
, (GHFunc
)da_pay_max_key_ghfunc
, &max_key
);
236 * da_pay_get_by_name:
238 * Get an payee structure by its name
240 * Return value: Payee * or NULL if not found
244 da_pay_get_by_name(gchar
*name
)
246 DB( g_print("da_pay_get_by_name\n") );
248 return g_hash_table_find(GLOBALS
->h_pay
, (GHRFunc
)da_pay_name_grfunc
, name
);
256 * Get an payee structure by key
258 * Return value: Payee * or NULL if not found
262 da_pay_get(guint32 key
)
264 //DB( g_print("da_pay_get\n") );
266 return g_hash_table_lookup(GLOBALS
->h_pay
, &key
);
270 void da_pay_consistency(Payee
*item
)
272 g_strstrip(item
->name
);
276 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
281 da_pay_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
286 DB( g_print(" %d :: %s\n", *id
, item
->name
) );
291 da_pay_debug_list(void)
294 DB( g_print("\n** debug **\n") );
296 g_hash_table_foreach(GLOBALS
->h_pay
, da_pay_debug_list_ghfunc
, NULL
);
298 DB( g_print("\n** end debug **\n") );
307 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
313 payee_is_used(guint32 key
)
317 list
= g_list_first(GLOBALS
->ope_list
);
320 Transaction
*entry
= list
->data
;
321 if( key
== entry
->kpay
)
323 list
= g_list_next(list
);
326 list
= g_list_first(GLOBALS
->arc_list
);
329 Archive
*entry
= list
->data
;
330 if( key
== entry
->kpay
)
332 list
= g_list_next(list
);
335 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
338 Assign
*entry
= list
->data
;
340 if( key
== entry
->kpay
)
342 list
= g_list_next(list
);
351 payee_move(guint32 key1
, guint32 key2
)
355 list
= g_list_first(GLOBALS
->ope_list
);
358 Transaction
*entry
= list
->data
;
359 if(entry
->kpay
== key1
)
362 entry
->flags
|= OF_CHANGED
;
364 list
= g_list_next(list
);
367 list
= g_list_first(GLOBALS
->arc_list
);
370 Archive
*entry
= list
->data
;
371 if(entry
->kpay
== key1
)
375 list
= g_list_next(list
);
378 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
381 Assign
*entry
= list
->data
;
383 if(entry
->kpay
== key1
)
387 list
= g_list_next(list
);
394 payee_rename(Payee
*item
, const gchar
*newname
)
399 stripname
= g_strdup(newname
);
400 g_strstrip(stripname
);
402 existitem
= da_pay_get_by_name(stripname
);
404 if( existitem
!= NULL
)
406 if( existitem
->key
== item
->key
)
412 item
->name
= g_strdup(stripname
);
423 * payee_append_if_new:
425 * append a new payee into the GHashTable
427 * Return value: a new Payee or NULL
431 payee_append_if_new(gchar
*name
)
436 stripname
= g_strdup(name
);
437 g_strstrip(stripname
);
438 item
= da_pay_get_by_name(stripname
);
442 item
= da_pay_malloc();
443 item
->name
= g_strdup(stripname
);
455 payee_glist_name_compare_func(Payee
*a
, Payee
*b
)
457 return hb_string_utf8_compare(a
->name
, b
->name
);
462 payee_glist_key_compare_func(Payee
*a
, Payee
*b
)
464 return a
->key
- b
->key
;
468 GList
*payee_glist_sorted(gint column
)
470 GList
*list
= g_hash_table_get_values(GLOBALS
->h_pay
);
473 return g_list_sort(list
, (GCompareFunc
)payee_glist_key_compare_func
);
475 return g_list_sort(list
, (GCompareFunc
)payee_glist_name_compare_func
);
481 payee_load_csv(gchar
*filename
)
486 const gchar
*encoding
;
488 encoding
= homebank_file_getencoding(filename
);
490 io
= g_io_channel_new_file(filename
, "r", NULL
);
493 DB( g_print(" -> encoding should be %s\n", encoding
) );
494 if( encoding
!= NULL
)
496 g_io_channel_set_encoding(io
, encoding
, NULL
);
501 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, NULL
);
502 if( io_stat
== G_IO_STATUS_EOF
)
504 if( io_stat
== G_IO_STATUS_NORMAL
)
508 hb_string_strip_crlf(tmpstr
);
510 DB( g_print(" read %s\n", tmpstr
) );
512 if( payee_append_if_new( tmpstr
) )
514 GLOBALS
->changes_count
++;
522 g_io_channel_unref (io
);
529 payee_save_csv(gchar
*filename
)
535 io
= g_io_channel_new_file(filename
, "w", NULL
);
538 lpay
= list
= payee_glist_sorted(1);
542 Payee
*item
= list
->data
;
546 outstr
= g_strdup_printf("%s\n", item
->name
);
547 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
549 DB( g_print("%s", outstr
) );
553 list
= g_list_next(list
);
557 g_io_channel_unref (io
);