1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 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/>.
21 #include "hb-category.h"
27 /****************************************************************************/
29 /****************************************************************************/
38 /* our global datas */
39 extern struct HomeBank
*GLOBALS
;
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
44 da_cat_clone(Category
*src_item
)
46 Category
*new_item
= rc_dup(src_item
, sizeof(Category
));
48 DB( g_print("da_cat_clone\n") );
51 //duplicate the string
52 new_item
->name
= g_strdup(src_item
->name
);
59 da_cat_free(Category
*item
)
61 DB( g_print("da_cat_free\n") );
64 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
75 DB( g_print("da_cat_malloc\n") );
76 return rc_alloc(sizeof(Category
));
83 DB( g_print("da_cat_destroy\n") );
84 g_hash_table_destroy(GLOBALS
->h_cat
);
93 DB( g_print("da_cat_new\n") );
94 GLOBALS
->h_cat
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_cat_free
);
96 // insert our 'no category'
97 item
= da_cat_malloc();
98 item
->name
= g_strdup("");
103 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
108 * Return value: the number of elements
113 return g_hash_table_size(GLOBALS
->h_cat
);
119 * da_cat_remove_grfunc:
121 * GRFunc to get the max id
123 * Return value: TRUE if the key/value must be deleted
127 da_cat_remove_grfunc(gpointer key
, Category
*cat
, guint32
*remkey
)
129 if(cat
->key
== *remkey
|| cat
->parent
== *remkey
)
139 * delete a category from the GHashTable
141 * Return value: TRUE if the key was found and deleted
145 da_cat_remove(guint32 key
)
147 DB( g_print("da_cat_remove %d\n", key
) );
149 return g_hash_table_foreach_remove(GLOBALS
->h_cat
, (GHRFunc
)da_cat_remove_grfunc
, &key
);
155 * insert a category into the GHashTable
157 * Return value: TRUE if inserted
161 da_cat_insert(Category
*item
)
165 DB( g_print("da_cat_insert\n") );
167 new_key
= g_new0(guint32
, 1);
168 *new_key
= item
->key
;
169 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, item
);
178 * append a category into the GHashTable
180 * Return value: TRUE if inserted
184 da_cat_append(Category
*cat
)
190 DB( g_print("da_cat_append\n") );
192 if( cat
->name
!= NULL
)
195 fullname
= da_cat_get_fullname(cat
);
196 existitem
= da_cat_get_by_fullname( fullname
);
199 if( existitem
== NULL
)
201 new_key
= g_new0(guint32
, 1);
202 *new_key
= da_cat_get_max_key() + 1;
205 DB( g_print(" -> insert id: %d\n", *new_key
) );
207 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, cat
);
213 DB( g_print(" -> %s already exist\n", cat
->name
) );
220 * da_cat_max_key_ghfunc:
222 * GHFunc for biggest key
226 da_cat_max_key_ghfunc(gpointer key
, Category
*cat
, guint32
*max_key
)
229 *max_key
= MAX(*max_key
, cat
->key
);
233 * da_cat_get_max_key:
235 * Get the biggest key from the GHashTable
237 * Return value: the biggest key value
241 da_cat_get_max_key(void)
245 g_hash_table_foreach(GLOBALS
->h_cat
, (GHFunc
)da_cat_max_key_ghfunc
, &max_key
);
250 * da_cat_get_fullname:
252 * Get category the fullname 'xxxx:yyyyy'
254 * Return value: the category fullname (free it with g_free)
258 da_cat_get_fullname(Category
*cat
)
262 if( cat
->parent
== 0 )
263 return g_strdup(cat
->name
);
266 parent
= da_cat_get(cat
->parent
);
269 return g_strdup_printf("%s:%s", parent
->name
, cat
->name
);
278 * da_cat_name_grfunc:
280 * GRFunc to get the max id
282 * Return value: TRUE if the key/value pair match our name
286 da_cat_name_grfunc(gpointer key
, Category
*cat
, gchar
*name
)
289 // DB( g_print("%s == %s\n", name, cat->name) );
290 if( name
&& cat
->name
)
292 if(!strcasecmp(name
, cat
->name
))
299 * da_cat_get_key_by_name:
301 * Get a category key by its name
303 * Return value: the category key or -1 if not found
307 da_cat_get_key_by_name(gchar
*name
)
311 DB( g_print("da_cat_get_key_by_name\n") );
313 cat
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
321 * da_cat_get_by_name:
323 * Get a category structure by its name
325 * Return value: Category * or NULL if not found
329 da_cat_get_by_name(gchar
*name
)
331 DB( g_print("da_cat_get_by_name\n") );
333 return g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
337 /* fullname i.e. car:refuel */
338 struct fullcatcontext
346 da_cat_fullname_grfunc(gpointer key
, Category
*item
, struct fullcatcontext
*ctx
)
349 //DB( g_print("'%s' == '%s'\n", ctx->name, item->name) );
350 if( item
->parent
== ctx
->parent
)
352 if(!strcasecmp(ctx
->name
, item
->name
))
359 da_cat_get_by_fullname(gchar
*fullname
)
361 struct fullcatcontext ctx
;
363 Category
*item
= NULL
;
365 DB( g_print("da_cat_get_by_fullname\n") );
367 typestr
= g_strsplit(fullname
, ":", 2);
368 if( g_strv_length(typestr
) == 2 )
371 ctx
.name
= typestr
[0];
372 DB( g_print(" [x:x] try to find the parent : '%s'\n", typestr
[0]) );
374 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
377 ctx
.parent
= parent
->key
;
378 ctx
.name
= typestr
[1];
380 DB( g_print(" [x:x] and searching sub %d '%s'\n", ctx
.parent
, ctx
.name
) );
382 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
390 DB( g_print(" [x] try to '%s'\n", fullname
) );
392 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
397 DB( g_print(" return value %p\n", item
) );
404 * da_cat_append_ifnew_by_fullname:
406 * append a category if it is new by fullname
412 da_cat_append_ifnew_by_fullname(gchar
*fullname
, gboolean imported
)
414 struct fullcatcontext ctx
;
416 Category
*newcat
, *item
, *retval
= NULL
;
419 DB( g_print("da_cat_append_ifnew_by_fullname\n") );
421 DB( g_print(" -> fullname: '%s' %d\n", fullname
, strlen(fullname
)) );
423 if( strlen(fullname
) > 0 )
425 typestr
= g_strsplit(fullname
, ":", 2);
427 /* if we have a subcategory : aaaa:bbb */
428 if( g_strv_length(typestr
) == 2 )
431 ctx
.name
= typestr
[0];
432 DB( g_print(" try to find the parent:'%s'\n", typestr
[0]) );
434 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
437 DB( g_print(" -> not found\n") );
439 // append a new category
440 new_key
= g_new0(guint32
, 1);
441 *new_key
= da_cat_get_max_key() + 1;
443 newcat
= da_cat_malloc();
444 newcat
->key
= *new_key
;
445 newcat
->name
= g_strdup(typestr
[0]);
446 newcat
->imported
= imported
;
450 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
452 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
455 ctx
.parent
= parent
->key
;
456 ctx
.name
= typestr
[1];
457 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
459 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
462 // append a new subcategory
463 new_key
= g_new0(guint32
, 1);
464 *new_key
= da_cat_get_max_key() + 1;
466 newcat
= da_cat_malloc();
467 newcat
->key
= *new_key
;
468 newcat
->parent
= parent
->key
;
469 newcat
->name
= g_strdup(typestr
[1]);
470 newcat
->imported
= imported
;
472 newcat
->flags
|= GF_SUB
;
474 DB( g_print(" -> insert subcat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
476 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
483 /* this a single category : aaaa */
487 ctx
.name
= typestr
[0];
488 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
490 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
493 // append a new category
494 new_key
= g_new0(guint32
, 1);
495 *new_key
= da_cat_get_max_key() + 1;
497 newcat
= da_cat_malloc();
498 newcat
->key
= *new_key
;
499 newcat
->name
= g_strdup(typestr
[0]);
500 newcat
->imported
= imported
;
502 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
504 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
524 * Get a category structure by key
526 * Return value: Category * or NULL if not found
530 da_cat_get(guint32 key
)
532 //DB( g_print("da_cat_get\n") );
534 return g_hash_table_lookup(GLOBALS
->h_cat
, &key
);
538 void da_cat_consistency(Category
*item
)
542 if((item
->flags
& GF_SUB
) && item
->key
> 0)
544 //check for existing parent
545 if( da_cat_get(item
->parent
) == NULL
)
547 Category
*parent
= da_cat_append_ifnew_by_fullname ("orphaned", FALSE
);
549 item
->parent
= parent
->key
;
551 g_warning("category consistency: fixed missing parent %d", item
->parent
);
555 // ensure type equal for categories and its children
556 if(!(item
->flags
& GF_SUB
) && item
->key
> 0)
558 isIncome
= (item
->flags
& GF_INCOME
) ? TRUE
: FALSE
;
559 if( category_change_type(item
, isIncome
) > 0 )
561 g_warning("category consistency: fixed type for child");
562 GLOBALS
->changes_count
++;
566 g_strstrip(item
->name
);
571 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
576 da_cat_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
579 Category
*cat
= value
;
581 DB( g_print(" %d :: %s (parent=%d\n", *id
, cat
->name
, cat
->parent
) );
586 da_cat_debug_list(void)
589 DB( g_print("\n** debug **\n") );
591 g_hash_table_foreach(GLOBALS
->h_cat
, da_cat_debug_list_ghfunc
, NULL
);
593 DB( g_print("\n** end debug **\n") );
601 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
603 guint32
category_report_id(guint32 key
, gboolean subcat
)
605 Category
*catentry
= da_cat_get(key
);
612 retval
= (catentry
->flags
& GF_SUB
) ? catentry
->parent
: catentry
->key
;
616 retval
= catentry
->key
;
624 category_delete_unused(void)
628 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
631 Category
*entry
= list
->data
;
633 if(entry
->usage_count
<= 0 && entry
->key
> 0)
634 da_cat_remove (entry
->key
);
636 list
= g_list_next(list
);
643 category_fill_usage_count(guint32 kcat
)
645 Category
*cat
= da_cat_get (kcat
);
651 if( cat
->parent
> 0 )
653 parent
= da_cat_get(cat
->parent
);
656 parent
->usage_count
++;
664 category_fill_usage(void)
667 GList
*lst_acc
, *lnk_acc
;
669 GList
*lpay
, *lrul
, *list
;
671 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
674 Category
*entry
= list
->data
;
675 entry
->usage_count
= 0;
676 list
= g_list_next(list
);
681 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
682 lnk_acc
= g_list_first(lst_acc
);
683 while (lnk_acc
!= NULL
)
685 Account
*acc
= lnk_acc
->data
;
687 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
688 while (lnk_txn
!= NULL
)
690 Transaction
*txn
= lnk_txn
->data
;
692 category_fill_usage_count(txn
->kcat
);
693 lnk_txn
= g_list_next(lnk_txn
);
695 lnk_acc
= g_list_next(lnk_acc
);
697 g_list_free(lst_acc
);
699 lpay
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
702 Payee
*entry
= list
->data
;
704 category_fill_usage_count(entry
->kcat
);
705 list
= g_list_next(list
);
710 list
= g_list_first(GLOBALS
->arc_list
);
713 Archive
*entry
= list
->data
;
715 category_fill_usage_count(entry
->kcat
);
716 list
= g_list_next(list
);
720 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
723 Assign
*entry
= list
->data
;
725 category_fill_usage_count(entry
->kcat
);
726 list
= g_list_next(list
);
734 category_move(guint32 key1
, guint32 key2
)
736 GList
*lst_acc
, *lnk_acc
;
741 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
742 lnk_acc
= g_list_first(lst_acc
);
743 while (lnk_acc
!= NULL
)
745 Account
*acc
= lnk_acc
->data
;
747 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
748 while (lnk_txn
!= NULL
)
750 Transaction
*txn
= lnk_txn
->data
;
752 if(txn
->kcat
== key1
)
755 txn
->flags
|= OF_CHANGED
;
758 // move split category #1340142
759 nbsplit
= da_splits_count(txn
->splits
);
760 for(i
=0;i
<nbsplit
;i
++)
762 Split
*split
= txn
->splits
[i
];
764 if( split
->kcat
== key1
)
767 txn
->flags
|= OF_CHANGED
;
771 lnk_txn
= g_list_next(lnk_txn
);
774 lnk_acc
= g_list_next(lnk_acc
);
776 g_list_free(lst_acc
);
779 list
= g_list_first(GLOBALS
->arc_list
);
782 Archive
*entry
= list
->data
;
783 if(entry
->kcat
== key1
)
787 list
= g_list_next(list
);
790 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
793 Assign
*entry
= list
->data
;
795 if(entry
->kcat
== key1
)
799 list
= g_list_next(list
);
807 category_rename(Category
*item
, const gchar
*newname
)
809 Category
*parent
, *existitem
;
810 gchar
*fullname
= NULL
;
814 DB( g_print("(category) rename\n") );
816 stripname
= g_strdup(newname
);
817 g_strstrip(stripname
);
819 if( item
->parent
== 0)
820 fullname
= g_strdup(stripname
);
823 parent
= da_cat_get(item
->parent
);
826 fullname
= g_strdup_printf("%s:%s", parent
->name
, stripname
);
830 DB( g_print(" - search: %s\n", fullname
) );
832 existitem
= da_cat_get_by_fullname( fullname
);
834 if( existitem
!= NULL
&& existitem
->key
!= item
->key
)
836 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem
->key
, item
->key
) );
841 DB( g_print(" -renaming\n") );
844 item
->name
= g_strdup(stripname
);
855 static gint
category_glist_name_compare_func(Category
*c1
, Category
*c2
)
857 gchar
*name1
, *name2
;
860 if( c1
!= NULL
&& c2
!= NULL
)
862 name1
= da_cat_get_fullname(c1
);
863 name2
= da_cat_get_fullname(c2
);
865 retval
= hb_string_utf8_compare(name1
, name2
);
874 static gint
category_glist_key_compare_func(Category
*a
, Category
*b
)
876 gint ka
, kb
, retval
= 0;
878 if(a
->parent
== 0 && b
->parent
== a
->key
)
881 if(b
->parent
== 0 && a
->parent
== b
->key
)
885 ka
= a
->parent
!= 0 ? a
->parent
: a
->key
;
886 kb
= b
->parent
!= 0 ? b
->parent
: b
->key
;
903 DB( g_print("compare a=%2d:%2d to b=%2d:%2d :: %d [%s]\n", a
->key
, a
->parent
, b
->key
, b
->parent
, retval
, str
) );
910 GList
*category_glist_sorted(gint column
)
912 GList
*list
= g_hash_table_get_values(GLOBALS
->h_cat
);
915 return g_list_sort(list
, (GCompareFunc
)category_glist_key_compare_func
);
917 return g_list_sort(list
, (GCompareFunc
)category_glist_name_compare_func
);
922 category_load_csv(gchar
*filename
, gchar
**error
)
929 gchar
*lastcatname
= NULL
;
934 const gchar
*encoding
;
936 encoding
= homebank_file_getencoding(filename
);
938 DB( g_print(" -> encoding should be %s\n", encoding
) );
943 io
= g_io_channel_new_file(filename
, "r", NULL
);
947 if( encoding
!= NULL
)
949 g_io_channel_set_encoding(io
, encoding
, NULL
);
956 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, &err
);
958 DB( g_print(" + iostat %d\n", io_stat
) );
960 if( io_stat
== G_IO_STATUS_ERROR
)
962 DB (g_print(" + ERROR %s\n",err
->message
));
965 if( io_stat
== G_IO_STATUS_EOF
)
967 if( io_stat
== G_IO_STATUS_NORMAL
)
971 DB( g_print(" + strip %s\n", tmpstr
) );
973 hb_string_strip_crlf(tmpstr
);
975 DB( g_print(" + split\n") );
977 str_array
= g_strsplit (tmpstr
, ";", 3);
980 if( g_strv_length (str_array
) != 3 )
982 *error
= _("invalid CSV format");
984 DB( g_print(" + error %s\n", *error
) );
988 DB( g_print(" + read %s : %s : %s\n", str_array
[0], str_array
[1], str_array
[2]) );
991 if( g_str_has_prefix(str_array
[0], "1") )
993 fullcatname
= g_strdup(str_array
[2]);
995 lastcatname
= g_strdup(str_array
[2]);
997 type
= g_str_has_prefix(str_array
[1], "+") ? GF_INCOME
: 0;
999 DB( g_print(" + type = %d\n", type
) );
1003 if( g_str_has_prefix(str_array
[0], "2") )
1005 fullcatname
= g_strdup_printf("%s:%s", lastcatname
, str_array
[2]);
1008 DB( g_print(" + fullcatname %s\n", fullcatname
) );
1010 item
= da_cat_append_ifnew_by_fullname(fullcatname
, FALSE
);
1012 DB( g_print(" + item %p\n", item
) );
1016 DB( g_print(" + assign flags: '%c'\n", type
) );
1018 item
->flags
|= type
;
1022 g_free(fullcatname
);
1023 g_strfreev (str_array
);
1031 g_io_channel_unref (io
);
1036 g_free(lastcatname
);
1044 category_save_csv(gchar
*filename
, gchar
**error
)
1046 gboolean retval
= FALSE
;
1052 io
= g_io_channel_new_file(filename
, "w", NULL
);
1055 lcat
= list
= category_glist_sorted(1);
1057 while (list
!= NULL
)
1059 Category
*item
= list
->data
;
1065 if( item
->parent
== 0)
1068 type
= (item
->flags
& GF_INCOME
) ? '+' : '-';
1076 outstr
= g_strdup_printf("%c;%c;%s\n", lvel
, type
, item
->name
);
1078 DB( g_print(" + export %s\n", outstr
) );
1080 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
1084 list
= g_list_next(list
);
1091 g_io_channel_unref (io
);
1098 gint
category_type_get(Category
*item
)
1100 if( (item
->flags
& (GF_INCOME
)) )
1107 static gint
category_change_type_eval(Category
*item
, gboolean isIncome
)
1109 if( (item
->flags
& (GF_INCOME
)) && !isIncome
)
1115 gint
category_change_type(Category
*item
, gboolean isIncome
)
1120 changes
+= category_change_type_eval(item
, isIncome
);
1122 item
->flags
&= ~(GF_INCOME
); //delete flag
1123 if(isIncome
== TRUE
)
1124 item
->flags
|= GF_INCOME
;
1126 // change also childs
1127 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
1128 while (list
!= NULL
)
1130 Category
*child
= list
->data
;
1132 if(child
->parent
== item
->key
)
1134 changes
+= category_change_type_eval(child
, isIncome
);
1135 child
->flags
&= ~(GF_INCOME
); //delete flag
1136 if(isIncome
== TRUE
)
1137 child
->flags
|= GF_INCOME
;
1139 list
= g_list_next(list
);
1152 * category_find_preset:
1154 * find a user language compatible file for category preset
1156 * Return value: a pathname to the file or NULL
1159 gchar
*category_find_preset(gchar
**lang
)
1166 DB( g_print("** category_find_preset **\n") );
1168 langs
= (gchar
**)g_get_language_names ();
1170 DB( g_print(" -> %d languages detected\n", g_strv_length(langs
)) );
1172 for(i
=0;i
<g_strv_length(langs
);i
++)
1174 DB( g_print(" -> %d '%s'\n", i
, langs
[i
]) );
1175 filename
= g_strdup_printf("hb-categories-%s.csv", langs
[i
]);
1176 gchar
*pathfilename
= g_build_filename(homebank_app_get_datas_dir(), filename
, NULL
);
1177 exists
= g_file_test(pathfilename
, G_FILE_TEST_EXISTS
);
1178 DB( g_print(" -> '%s' exists=%d\n", pathfilename
, exists
) );
1183 return pathfilename
;
1186 g_free(pathfilename
);
1189 DB( g_print("return NULL\n") );