1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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"
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41 da_cat_clone(Category
*src_item
)
43 Category
*new_item
= g_memdup(src_item
, sizeof(Category
));
45 DB( g_print("da_cat_clone\n") );
48 //duplicate the string
49 new_item
->name
= g_strdup(src_item
->name
);
56 da_cat_free(Category
*item
)
58 DB( g_print("da_cat_free\n") );
61 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
72 DB( g_print("da_cat_malloc\n") );
73 return g_malloc0(sizeof(Category
));
80 DB( g_print("da_cat_destroy\n") );
81 g_hash_table_destroy(GLOBALS
->h_cat
);
90 DB( g_print("da_cat_new\n") );
91 GLOBALS
->h_cat
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_cat_free
);
93 // insert our 'no category'
94 item
= da_cat_malloc();
95 item
->name
= g_strdup("");
100 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
105 * Return value: the number of elements
110 return g_hash_table_size(GLOBALS
->h_cat
);
116 * da_cat_remove_grfunc:
118 * GRFunc to get the max id
120 * Return value: TRUE if the key/value must be deleted
124 da_cat_remove_grfunc(gpointer key
, Category
*cat
, guint32
*remkey
)
126 if(cat
->key
== *remkey
|| cat
->parent
== *remkey
)
136 * delete a category from the GHashTable
138 * Return value: TRUE if the key was found and deleted
142 da_cat_remove(guint32 key
)
144 DB( g_print("da_cat_remove %d\n", key
) );
146 return g_hash_table_foreach_remove(GLOBALS
->h_cat
, (GHRFunc
)da_cat_remove_grfunc
, &key
);
152 * insert a category into the GHashTable
154 * Return value: TRUE if inserted
158 da_cat_insert(Category
*item
)
162 DB( g_print("da_cat_insert\n") );
164 new_key
= g_new0(guint32
, 1);
165 *new_key
= item
->key
;
166 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, item
);
175 * append a category into the GHashTable
177 * Return value: TRUE if inserted
181 da_cat_append(Category
*cat
)
187 DB( g_print("da_cat_append\n") );
189 if( cat
->name
!= NULL
)
192 fullname
= da_cat_get_fullname(cat
);
193 existitem
= da_cat_get_by_fullname( fullname
);
196 if( existitem
== NULL
)
198 new_key
= g_new0(guint32
, 1);
199 *new_key
= da_cat_get_max_key() + 1;
202 DB( g_print(" -> insert id: %d\n", *new_key
) );
204 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, cat
);
210 DB( g_print(" -> %s already exist\n", cat
->name
) );
217 * da_cat_max_key_ghfunc:
219 * GHFunc for biggest key
223 da_cat_max_key_ghfunc(gpointer key
, Category
*cat
, guint32
*max_key
)
226 *max_key
= MAX(*max_key
, cat
->key
);
230 * da_cat_get_max_key:
232 * Get the biggest key from the GHashTable
234 * Return value: the biggest key value
238 da_cat_get_max_key(void)
242 g_hash_table_foreach(GLOBALS
->h_cat
, (GHFunc
)da_cat_max_key_ghfunc
, &max_key
);
247 * da_cat_get_fullname:
249 * Get category the fullname 'xxxx:yyyyy'
251 * Return value: the category fullname (free it with g_free)
255 da_cat_get_fullname(Category
*cat
)
259 if( cat
->parent
== 0 )
260 return g_strdup(cat
->name
);
263 parent
= da_cat_get(cat
->parent
);
266 return g_strdup_printf("%s:%s", parent
->name
, cat
->name
);
275 * da_cat_name_grfunc:
277 * GRFunc to get the max id
279 * Return value: TRUE if the key/value pair match our name
283 da_cat_name_grfunc(gpointer key
, Category
*cat
, gchar
*name
)
286 // DB( g_print("%s == %s\n", name, cat->name) );
287 if( name
&& cat
->name
)
289 if(!strcasecmp(name
, cat
->name
))
296 * da_cat_get_key_by_name:
298 * Get a category key by its name
300 * Return value: the category key or -1 if not found
304 da_cat_get_key_by_name(gchar
*name
)
308 DB( g_print("da_cat_get_key_by_name\n") );
310 cat
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
318 * da_cat_get_by_name:
320 * Get a category structure by its name
322 * Return value: Category * or NULL if not found
326 da_cat_get_by_name(gchar
*name
)
328 DB( g_print("da_cat_get_by_name\n") );
330 return g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
334 /* fullname i.e. car:refuel */
335 struct fullcatcontext
343 da_cat_fullname_grfunc(gpointer key
, Category
*item
, struct fullcatcontext
*ctx
)
346 //DB( g_print("'%s' == '%s'\n", ctx->name, item->name) );
347 if( item
->parent
== ctx
->parent
)
349 if(!strcasecmp(ctx
->name
, item
->name
))
356 da_cat_get_by_fullname(gchar
*fullname
)
358 struct fullcatcontext ctx
;
360 Category
*item
= NULL
;
362 DB( g_print("da_cat_get_by_fullname\n") );
364 typestr
= g_strsplit(fullname
, ":", 2);
365 if( g_strv_length(typestr
) == 2 )
368 ctx
.name
= typestr
[0];
369 DB( g_print(" [x:x] try to find the parent : '%s'\n", typestr
[0]) );
371 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
374 ctx
.parent
= parent
->key
;
375 ctx
.name
= typestr
[1];
377 DB( g_print(" [x:x] and searching sub %d '%s'\n", ctx
.parent
, ctx
.name
) );
379 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
387 DB( g_print(" [x] try to '%s'\n", fullname
) );
389 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
394 DB( g_print(" return value %p\n", item
) );
401 * da_cat_append_ifnew_by_fullname:
403 * append a category if it is new by fullname
409 da_cat_append_ifnew_by_fullname(gchar
*fullname
, gboolean imported
)
411 struct fullcatcontext ctx
;
413 Category
*newcat
, *item
, *retval
= NULL
;
416 DB( g_print("da_cat_append_ifnew_by_fullname\n") );
418 DB( g_print(" -> fullname: '%s' %d\n", fullname
, strlen(fullname
)) );
420 if( strlen(fullname
) > 0 )
422 typestr
= g_strsplit(fullname
, ":", 2);
424 /* if we have a subcategory : aaaa:bbb */
425 if( g_strv_length(typestr
) == 2 )
428 ctx
.name
= typestr
[0];
429 DB( g_print(" try to find the parent:'%s'\n", typestr
[0]) );
431 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
434 DB( g_print(" -> not found\n") );
436 // append a new category
437 new_key
= g_new0(guint32
, 1);
438 *new_key
= da_cat_get_max_key() + 1;
440 newcat
= da_cat_malloc();
441 newcat
->key
= *new_key
;
442 newcat
->name
= g_strdup(typestr
[0]);
443 newcat
->imported
= imported
;
447 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
449 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
452 ctx
.parent
= parent
->key
;
453 ctx
.name
= typestr
[1];
454 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
456 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
459 // append a new subcategory
460 new_key
= g_new0(guint32
, 1);
461 *new_key
= da_cat_get_max_key() + 1;
463 newcat
= da_cat_malloc();
464 newcat
->key
= *new_key
;
465 newcat
->parent
= parent
->key
;
466 newcat
->name
= g_strdup(typestr
[1]);
467 newcat
->imported
= imported
;
469 newcat
->flags
|= GF_SUB
;
471 DB( g_print(" -> insert subcat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
473 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
480 /* this a single category : aaaa */
484 ctx
.name
= typestr
[0];
485 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
487 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
490 // append a new category
491 new_key
= g_new0(guint32
, 1);
492 *new_key
= da_cat_get_max_key() + 1;
494 newcat
= da_cat_malloc();
495 newcat
->key
= *new_key
;
496 newcat
->name
= g_strdup(typestr
[0]);
497 newcat
->imported
= imported
;
499 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
501 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
521 * Get a category structure by key
523 * Return value: Category * or NULL if not found
527 da_cat_get(guint32 key
)
529 //DB( g_print("da_cat_get\n") );
531 return g_hash_table_lookup(GLOBALS
->h_cat
, &key
);
535 void da_cat_consistency(Category
*item
)
539 if((item
->flags
& GF_SUB
) && item
->key
> 0)
541 //check for existing parent
542 if( da_cat_get(item
->parent
) == NULL
)
544 Category
*parent
= da_cat_append_ifnew_by_fullname ("orphaned", FALSE
);
546 item
->parent
= parent
->key
;
548 g_warning("category consistency: fixed missing parent %d", item
->parent
);
552 // ensure type equal for categories and its children
553 if(!(item
->flags
& GF_SUB
) && item
->key
> 0)
555 isIncome
= (item
->flags
& GF_INCOME
) ? TRUE
: FALSE
;
556 if( category_change_type(item
, isIncome
) > 0 )
558 g_warning("category consistency: fixed type for child");
559 GLOBALS
->changes_count
++;
563 g_strstrip(item
->name
);
568 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
573 da_cat_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
576 Category
*cat
= value
;
578 DB( g_print(" %d :: %s (parent=%d\n", *id
, cat
->name
, cat
->parent
) );
583 da_cat_debug_list(void)
586 DB( g_print("\n** debug **\n") );
588 g_hash_table_foreach(GLOBALS
->h_cat
, da_cat_debug_list_ghfunc
, NULL
);
590 DB( g_print("\n** end debug **\n") );
598 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
600 guint32
category_report_id(guint32 key
, gboolean subcat
)
602 Category
*catentry
= da_cat_get(key
);
609 retval
= (catentry
->flags
& GF_SUB
) ? catentry
->parent
: catentry
->key
;
613 retval
= catentry
->key
;
621 category_delete_unused(void)
625 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
628 Category
*entry
= list
->data
;
630 if(entry
->usage_count
<= 0 && entry
->key
> 0)
631 da_cat_remove (entry
->key
);
633 list
= g_list_next(list
);
640 category_fill_usage_count(guint32 kcat
)
642 Category
*cat
= da_cat_get (kcat
);
648 if( cat
->parent
> 0 )
650 parent
= da_cat_get(cat
->parent
);
653 parent
->usage_count
++;
661 category_fill_usage(void)
664 GList
*lst_acc
, *lnk_acc
;
666 GList
*lpay
, *lrul
, *list
;
668 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
671 Category
*entry
= list
->data
;
672 entry
->usage_count
= 0;
673 list
= g_list_next(list
);
678 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
679 lnk_acc
= g_list_first(lst_acc
);
680 while (lnk_acc
!= NULL
)
682 Account
*acc
= lnk_acc
->data
;
684 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
685 while (lnk_txn
!= NULL
)
687 Transaction
*txn
= lnk_txn
->data
;
689 category_fill_usage_count(txn
->kcat
);
690 lnk_txn
= g_list_next(lnk_txn
);
692 lnk_acc
= g_list_next(lnk_acc
);
694 g_list_free(lst_acc
);
696 lpay
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
699 Payee
*entry
= list
->data
;
701 category_fill_usage_count(entry
->kcat
);
702 list
= g_list_next(list
);
707 list
= g_list_first(GLOBALS
->arc_list
);
710 Archive
*entry
= list
->data
;
712 category_fill_usage_count(entry
->kcat
);
713 list
= g_list_next(list
);
717 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
720 Assign
*entry
= list
->data
;
722 category_fill_usage_count(entry
->kcat
);
723 list
= g_list_next(list
);
731 category_move(guint32 key1
, guint32 key2
)
733 GList
*lst_acc
, *lnk_acc
;
738 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
739 lnk_acc
= g_list_first(lst_acc
);
740 while (lnk_acc
!= NULL
)
742 Account
*acc
= lnk_acc
->data
;
744 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
745 while (lnk_txn
!= NULL
)
747 Transaction
*txn
= lnk_txn
->data
;
749 if(txn
->kcat
== key1
)
752 txn
->flags
|= OF_CHANGED
;
755 // move split category #1340142
756 nbsplit
= da_splits_count(txn
->splits
);
757 for(i
=0;i
<nbsplit
;i
++)
759 Split
*split
= txn
->splits
[i
];
761 if( split
->kcat
== key1
)
764 txn
->flags
|= OF_CHANGED
;
768 lnk_txn
= g_list_next(lnk_txn
);
771 lnk_acc
= g_list_next(lnk_acc
);
773 g_list_free(lst_acc
);
776 list
= g_list_first(GLOBALS
->arc_list
);
779 Archive
*entry
= list
->data
;
780 if(entry
->kcat
== key1
)
784 list
= g_list_next(list
);
787 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
790 Assign
*entry
= list
->data
;
792 if(entry
->kcat
== key1
)
796 list
= g_list_next(list
);
804 category_rename(Category
*item
, const gchar
*newname
)
806 Category
*parent
, *existitem
;
807 gchar
*fullname
= NULL
;
811 DB( g_print("(category) rename\n") );
813 stripname
= g_strdup(newname
);
814 g_strstrip(stripname
);
816 if( item
->parent
== 0)
817 fullname
= g_strdup(stripname
);
820 parent
= da_cat_get(item
->parent
);
823 fullname
= g_strdup_printf("%s:%s", parent
->name
, stripname
);
827 DB( g_print(" - search: %s\n", fullname
) );
829 existitem
= da_cat_get_by_fullname( fullname
);
831 if( existitem
!= NULL
&& existitem
->key
!= item
->key
)
833 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem
->key
, item
->key
) );
838 DB( g_print(" -renaming\n") );
841 item
->name
= g_strdup(stripname
);
852 static gint
category_glist_name_compare_func(Category
*c1
, Category
*c2
)
854 gchar
*name1
, *name2
;
857 if( c1
!= NULL
&& c2
!= NULL
)
859 name1
= da_cat_get_fullname(c1
);
860 name2
= da_cat_get_fullname(c2
);
862 retval
= hb_string_utf8_compare(name1
, name2
);
871 static gint
category_glist_key_compare_func(Category
*a
, Category
*b
)
873 gint ka
, kb
, retval
= 0;
875 if(a
->parent
== 0 && b
->parent
== a
->key
)
878 if(b
->parent
== 0 && a
->parent
== b
->key
)
882 ka
= a
->parent
!= 0 ? a
->parent
: a
->key
;
883 kb
= b
->parent
!= 0 ? b
->parent
: b
->key
;
900 DB( g_print("compare a=%2d:%2d to b=%2d:%2d :: %d [%s]\n", a
->key
, a
->parent
, b
->key
, b
->parent
, retval
, str
) );
907 GList
*category_glist_sorted(gint column
)
909 GList
*list
= g_hash_table_get_values(GLOBALS
->h_cat
);
912 return g_list_sort(list
, (GCompareFunc
)category_glist_key_compare_func
);
914 return g_list_sort(list
, (GCompareFunc
)category_glist_name_compare_func
);
919 category_load_csv(gchar
*filename
, gchar
**error
)
926 gchar
*lastcatname
= NULL
;
931 const gchar
*encoding
;
933 encoding
= homebank_file_getencoding(filename
);
935 DB( g_print(" -> encoding should be %s\n", encoding
) );
940 io
= g_io_channel_new_file(filename
, "r", NULL
);
944 if( encoding
!= NULL
)
946 g_io_channel_set_encoding(io
, encoding
, NULL
);
953 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, &err
);
955 DB( g_print(" + iostat %d\n", io_stat
) );
957 if( io_stat
== G_IO_STATUS_ERROR
)
959 DB (g_print(" + ERROR %s\n",err
->message
));
962 if( io_stat
== G_IO_STATUS_EOF
)
964 if( io_stat
== G_IO_STATUS_NORMAL
)
968 DB( g_print(" + strip %s\n", tmpstr
) );
969 hb_string_strip_crlf(tmpstr
);
971 DB( g_print(" + split\n") );
972 str_array
= g_strsplit (tmpstr
, ";", 3);
975 if( g_strv_length (str_array
) != 3 )
977 *error
= _("invalid CSV format");
979 DB( g_print(" + error %s\n", *error
) );
983 DB( g_print(" + read %s : %s : %s\n", str_array
[0], str_array
[1], str_array
[2]) );
986 if( g_str_has_prefix(str_array
[0], "1") )
988 fullcatname
= g_strdup(str_array
[2]);
990 lastcatname
= g_strdup(str_array
[2]);
992 type
= g_str_has_prefix(str_array
[1], "+") ? GF_INCOME
: 0;
994 DB( g_print(" + type = %d\n", type
) );
998 if( g_str_has_prefix(str_array
[0], "2") )
1000 fullcatname
= g_strdup_printf("%s:%s", lastcatname
, str_array
[2]);
1003 DB( g_print(" + fullcatname %s\n", fullcatname
) );
1005 item
= da_cat_append_ifnew_by_fullname(fullcatname
, FALSE
);
1007 DB( g_print(" + item %p\n", item
) );
1011 DB( g_print(" + assign flags: '%c'\n", type
) );
1013 item
->flags
|= type
;
1017 g_free(fullcatname
);
1018 g_strfreev (str_array
);
1026 g_io_channel_unref (io
);
1031 g_free(lastcatname
);
1039 category_save_csv(gchar
*filename
, gchar
**error
)
1041 gboolean retval
= FALSE
;
1047 io
= g_io_channel_new_file(filename
, "w", NULL
);
1050 lcat
= list
= category_glist_sorted(1);
1052 while (list
!= NULL
)
1054 Category
*item
= list
->data
;
1060 if( item
->parent
== 0)
1063 type
= (item
->flags
& GF_INCOME
) ? '+' : '-';
1071 outstr
= g_strdup_printf("%c;%c;%s\n", lvel
, type
, item
->name
);
1073 DB( g_print(" + export %s\n", outstr
) );
1075 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
1079 list
= g_list_next(list
);
1086 g_io_channel_unref (io
);
1093 gint
category_type_get(Category
*item
)
1095 if( (item
->flags
& (GF_INCOME
)) )
1102 static gint
category_change_type_eval(Category
*item
, gboolean isIncome
)
1104 if( (item
->flags
& (GF_INCOME
)) && !isIncome
)
1110 gint
category_change_type(Category
*item
, gboolean isIncome
)
1115 changes
+= category_change_type_eval(item
, isIncome
);
1117 item
->flags
&= ~(GF_INCOME
); //delete flag
1118 if(isIncome
== TRUE
)
1119 item
->flags
|= GF_INCOME
;
1121 // change also childs
1122 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
1123 while (list
!= NULL
)
1125 Category
*child
= list
->data
;
1127 if(child
->parent
== item
->key
)
1129 changes
+= category_change_type_eval(child
, isIncome
);
1130 child
->flags
&= ~(GF_INCOME
); //delete flag
1131 if(isIncome
== TRUE
)
1132 child
->flags
|= GF_INCOME
;
1134 list
= g_list_next(list
);
1147 * category_find_preset:
1149 * find a user language compatible file for category preset
1151 * Return value: a pathname to the file or NULL
1154 gchar
*category_find_preset(gchar
**lang
)
1161 DB( g_print("** category_find_preset **\n") );
1163 langs
= (gchar
**)g_get_language_names ();
1165 DB( g_print(" -> %d languages detected\n", g_strv_length(langs
)) );
1167 for(i
=0;i
<g_strv_length(langs
);i
++)
1169 DB( g_print(" -> %d '%s'\n", i
, langs
[i
]) );
1170 filename
= g_strdup_printf("hb-categories-%s.csv", langs
[i
]);
1171 gchar
*pathfilename
= g_build_filename(homebank_app_get_datas_dir(), filename
, NULL
);
1172 exists
= g_file_test(pathfilename
, G_FILE_TEST_EXISTS
);
1173 DB( g_print(" -> '%s' exists=%d\n", pathfilename
, exists
) );
1178 return pathfilename
;
1181 g_free(pathfilename
);
1184 DB( g_print("return NULL\n") );