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/>.
27 /****************************************************************************/
29 /****************************************************************************/
38 /* our global datas */
39 extern struct HomeBank
*GLOBALS
;
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
44 da_pay_free(Payee
*item
)
46 DB( g_print("da_pay_free\n") );
49 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
60 DB( g_print("da_pay_malloc\n") );
61 return rc_alloc(sizeof(Payee
));
68 DB( g_print("da_pay_destroy\n") );
69 g_hash_table_destroy(GLOBALS
->h_pay
);
78 DB( g_print("da_pay_new\n") );
79 GLOBALS
->h_pay
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_pay_free
);
81 // insert our 'no payee'
82 item
= da_pay_malloc();
83 item
->name
= g_strdup("");
88 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
89 static void da_pay_max_key_ghfunc(gpointer key
, Payee
*item
, guint32
*max_key
)
91 *max_key
= MAX(*max_key
, item
->key
);
94 static gboolean
da_pay_name_grfunc(gpointer key
, Payee
*item
, gchar
*name
)
96 if( name
&& item
->name
)
98 if(!strcasecmp(name
, item
->name
))
107 * Return value: the number of elements
112 return g_hash_table_size(GLOBALS
->h_pay
);
117 da_pay_create_none(void)
122 DB( g_print("da_pay_insert none\n") );
124 pay = da_pay_malloc();
125 new_key = g_new0(guint32, 1);
128 pay->name = g_strdup("");
130 DB( g_print(" -> insert id: %d\n", *new_key) );
132 g_hash_table_insert(GLOBALS->h_pay, new_key, pay);
143 * remove an payee from the GHashTable
145 * Return value: TRUE if the key was found and removed
149 da_pay_remove(guint32 key
)
151 DB( g_print("da_pay_remove %d\n", key
) );
153 return g_hash_table_remove(GLOBALS
->h_pay
, &key
);
159 * insert an payee into the GHashTable
161 * Return value: TRUE if inserted
165 da_pay_insert(Payee
*item
)
169 DB( g_print("da_pay_insert\n") );
171 new_key
= g_new0(guint32
, 1);
172 *new_key
= item
->key
;
173 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
182 * append a new payee into the GHashTable
184 * Return value: TRUE if inserted
188 da_pay_append(Payee
*item
)
193 DB( g_print("da_pay_append\n") );
195 /* ensure no duplicate */
196 //g_strstrip(item->name);
197 if( item
->name
!= NULL
)
199 existitem
= da_pay_get_by_name( item
->name
);
200 if( existitem
== NULL
)
202 new_key
= g_new0(guint32
, 1);
203 *new_key
= da_pay_get_max_key() + 1;
204 item
->key
= *new_key
;
206 DB( g_print(" -> append id: %d\n", *new_key
) );
208 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
213 DB( g_print(" -> %s already exist: %d\n", item
->name
, item
->key
) );
219 * da_pay_get_max_key:
221 * Get the biggest key from the GHashTable
223 * Return value: the biggest key value
227 da_pay_get_max_key(void)
231 g_hash_table_foreach(GLOBALS
->h_pay
, (GHFunc
)da_pay_max_key_ghfunc
, &max_key
);
239 * da_pay_get_by_name:
241 * Get an payee structure by its name
243 * Return value: Payee * or NULL if not found
247 da_pay_get_by_name(gchar
*name
)
249 DB( g_print("da_pay_get_by_name\n") );
251 return g_hash_table_find(GLOBALS
->h_pay
, (GHRFunc
)da_pay_name_grfunc
, name
);
259 * Get an payee structure by key
261 * Return value: Payee * or NULL if not found
265 da_pay_get(guint32 key
)
267 //DB( g_print("da_pay_get\n") );
269 return g_hash_table_lookup(GLOBALS
->h_pay
, &key
);
273 void da_pay_consistency(Payee
*item
)
275 g_strstrip(item
->name
);
279 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
284 da_pay_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
289 DB( g_print(" %d :: %s\n", *id
, item
->name
) );
294 da_pay_debug_list(void)
297 DB( g_print("\n** debug **\n") );
299 g_hash_table_foreach(GLOBALS
->h_pay
, da_pay_debug_list_ghfunc
, NULL
);
301 DB( g_print("\n** end debug **\n") );
310 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
316 payee_is_used(guint32 key
)
320 list
= g_list_first(GLOBALS
->ope_list
);
323 Transaction
*entry
= list
->data
;
324 if( key
== entry
->kpay
)
326 list
= g_list_next(list
);
329 list
= g_list_first(GLOBALS
->arc_list
);
332 Archive
*entry
= list
->data
;
333 if( key
== entry
->kpay
)
335 list
= g_list_next(list
);
338 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
341 Assign
*entry
= list
->data
;
343 if( key
== entry
->kpay
)
345 list
= g_list_next(list
);
354 payee_move(guint32 key1
, guint32 key2
)
358 list
= g_list_first(GLOBALS
->ope_list
);
361 Transaction
*entry
= list
->data
;
362 if(entry
->kpay
== key1
)
365 entry
->flags
|= OF_CHANGED
;
367 list
= g_list_next(list
);
370 list
= g_list_first(GLOBALS
->arc_list
);
373 Archive
*entry
= list
->data
;
374 if(entry
->kpay
== key1
)
378 list
= g_list_next(list
);
381 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
384 Assign
*entry
= list
->data
;
386 if(entry
->kpay
== key1
)
390 list
= g_list_next(list
);
397 payee_rename(Payee
*item
, const gchar
*newname
)
402 stripname
= g_strdup(newname
);
403 g_strstrip(stripname
);
405 existitem
= da_pay_get_by_name(stripname
);
407 if( existitem
!= NULL
)
409 if( existitem
->key
== item
->key
)
415 item
->name
= g_strdup(stripname
);
426 * payee_append_if_new:
428 * append a new payee into the GHashTable
430 * Return value: a new Payee or NULL
434 payee_append_if_new(gchar
*name
)
439 stripname
= g_strdup(name
);
440 g_strstrip(stripname
);
441 item
= da_pay_get_by_name(stripname
);
445 item
= da_pay_malloc();
446 item
->name
= g_strdup(stripname
);
458 payee_glist_name_compare_func(Payee
*a
, Payee
*b
)
460 return hb_string_utf8_compare(a
->name
, b
->name
);
465 payee_glist_key_compare_func(Payee
*a
, Payee
*b
)
467 return a
->key
- b
->key
;
471 GList
*payee_glist_sorted(gint column
)
473 GList
*list
= g_hash_table_get_values(GLOBALS
->h_pay
);
476 return g_list_sort(list
, (GCompareFunc
)payee_glist_key_compare_func
);
478 return g_list_sort(list
, (GCompareFunc
)payee_glist_name_compare_func
);
484 payee_load_csv(gchar
*filename
)
489 const gchar
*encoding
;
491 encoding
= homebank_file_getencoding(filename
);
493 io
= g_io_channel_new_file(filename
, "r", NULL
);
496 DB( g_print(" -> encoding should be %s\n", encoding
) );
497 if( encoding
!= NULL
)
499 g_io_channel_set_encoding(io
, encoding
, NULL
);
504 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, NULL
);
505 if( io_stat
== G_IO_STATUS_EOF
)
507 if( io_stat
== G_IO_STATUS_NORMAL
)
511 hb_string_strip_crlf(tmpstr
);
513 DB( g_print(" read %s\n", tmpstr
) );
515 if( payee_append_if_new( tmpstr
) )
517 GLOBALS
->changes_count
++;
525 g_io_channel_unref (io
);
532 payee_save_csv(gchar
*filename
)
538 io
= g_io_channel_new_file(filename
, "w", NULL
);
541 lpay
= list
= payee_glist_sorted(1);
545 Payee
*item
= list
->data
;
549 outstr
= g_strdup_printf("%s\n", item
->name
);
550 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
552 DB( g_print("%s", outstr
) );
556 list
= g_list_next(list
);
560 g_io_channel_unref (io
);