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/>.
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 removed
127 da_cat_remove_grfunc(gpointer key
, Category
*cat
, guint32
*remkey
)
129 if(cat
->key
== *remkey
|| cat
->parent
== *remkey
)
139 * remove a category from the GHashTable
141 * Return value: TRUE if the key was found and removed
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 // ensure type equal for categories and its children
543 if(!(item
->flags
& GF_SUB
) && item
->key
> 0)
545 isIncome
= (item
->flags
& GF_INCOME
) ? TRUE
: FALSE
;
546 category_change_type(item
, isIncome
);
548 g_strstrip(item
->name
);
553 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
558 da_cat_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
561 Category
*cat
= value
;
563 DB( g_print(" %d :: %s (parent=%d\n", *id
, cat
->name
, cat
->parent
) );
568 da_cat_debug_list(void)
571 DB( g_print("\n** debug **\n") );
573 g_hash_table_foreach(GLOBALS
->h_cat
, da_cat_debug_list_ghfunc
, NULL
);
575 DB( g_print("\n** end debug **\n") );
583 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
586 category_is_used(guint32 key
)
591 list
= g_list_first(GLOBALS
->ope_list
);
594 Transaction
*entry
= list
->data
;
595 if( key
== entry
->kcat
)
598 // check split category #1340142
599 nbsplit
= da_transaction_splits_count(entry
);
600 for(i
=0;i
<nbsplit
;i
++)
602 Split
*split
= entry
->splits
[i
];
604 if( key
== split
->kcat
)
608 list
= g_list_next(list
);
611 list
= g_list_first(GLOBALS
->arc_list
);
614 Archive
*entry
= list
->data
;
615 if( key
== entry
->kcat
)
617 list
= g_list_next(list
);
620 //todo: add budget use here
622 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
625 Assign
*entry
= list
->data
;
627 if( key
== entry
->kcat
)
629 list
= g_list_next(list
);
637 category_move(guint32 key1
, guint32 key2
)
642 list
= g_list_first(GLOBALS
->ope_list
);
645 Transaction
*entry
= list
->data
;
646 if(entry
->kcat
== key1
)
649 entry
->flags
|= OF_CHANGED
;
652 // move split category #1340142
653 nbsplit
= da_transaction_splits_count(entry
);
654 for(i
=0;i
<nbsplit
;i
++)
656 Split
*split
= entry
->splits
[i
];
658 if( split
->kcat
== key1
)
661 entry
->flags
|= OF_CHANGED
;
665 list
= g_list_next(list
);
668 list
= g_list_first(GLOBALS
->arc_list
);
671 Archive
*entry
= list
->data
;
672 if(entry
->kcat
== key1
)
676 list
= g_list_next(list
);
679 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
682 Assign
*entry
= list
->data
;
684 if(entry
->kcat
== key1
)
688 list
= g_list_next(list
);
696 category_rename(Category
*item
, const gchar
*newname
)
698 Category
*parent
, *existitem
;
699 gchar
*fullname
= NULL
;
703 DB( g_print("(category) rename\n") );
705 stripname
= g_strdup(newname
);
706 g_strstrip(stripname
);
708 if( item
->parent
== 0)
709 fullname
= g_strdup(stripname
);
712 parent
= da_cat_get(item
->parent
);
715 fullname
= g_strdup_printf("%s:%s", parent
->name
, stripname
);
719 DB( g_print(" - search: %s\n", fullname
) );
721 existitem
= da_cat_get_by_fullname( fullname
);
723 if( existitem
!= NULL
&& existitem
->key
!= item
->key
)
725 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem
->key
, item
->key
) );
730 DB( g_print(" -renaming\n") );
733 item
->name
= g_strdup(stripname
);
744 static gint
category_glist_name_compare_func(Category
*c1
, Category
*c2
)
746 gchar
*name1
, *name2
;
749 if( c1
!= NULL
&& c2
!= NULL
)
751 name1
= da_cat_get_fullname(c1
);
752 name2
= da_cat_get_fullname(c2
);
754 retval
= hb_string_utf8_compare(name1
, name2
);
763 static gint
category_glist_key_compare_func(Category
*a
, Category
*b
)
765 gint ka
, kb
, retval
= 0;
767 if(a
->parent
== 0 && b
->parent
== a
->key
)
770 if(b
->parent
== 0 && a
->parent
== b
->key
)
774 ka
= a
->parent
!= 0 ? a
->parent
: a
->key
;
775 kb
= b
->parent
!= 0 ? b
->parent
: b
->key
;
792 DB( g_print("compare a=%2d:%2d to b=%2d:%2d :: %d [%s]\n", a
->key
, a
->parent
, b
->key
, b
->parent
, retval
, str
) );
799 GList
*category_glist_sorted(gint column
)
801 GList
*list
= g_hash_table_get_values(GLOBALS
->h_cat
);
804 return g_list_sort(list
, (GCompareFunc
)category_glist_key_compare_func
);
806 return g_list_sort(list
, (GCompareFunc
)category_glist_name_compare_func
);
811 category_load_csv(gchar
*filename
, gchar
**error
)
818 gchar
*lastcatname
= NULL
;
823 const gchar
*encoding
;
825 encoding
= homebank_file_getencoding(filename
);
827 DB( g_print(" -> encoding should be %s\n", encoding
) );
832 io
= g_io_channel_new_file(filename
, "r", NULL
);
836 if( encoding
!= NULL
)
838 g_io_channel_set_encoding(io
, encoding
, NULL
);
845 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, &err
);
847 DB( g_print(" + iostat %d\n", io_stat
) );
849 if( io_stat
== G_IO_STATUS_ERROR
)
851 DB (g_print(" + ERROR %s\n",err
->message
));
854 if( io_stat
== G_IO_STATUS_EOF
)
856 if( io_stat
== G_IO_STATUS_NORMAL
)
860 DB( g_print(" + strip %s\n", tmpstr
) );
862 hb_string_strip_crlf(tmpstr
);
864 DB( g_print(" + split\n") );
866 str_array
= g_strsplit (tmpstr
, ";", 3);
869 if( g_strv_length (str_array
) != 3 )
871 *error
= _("invalid csv format");
873 DB( g_print(" + error %s\n", *error
) );
877 DB( g_print(" + read %s : %s : %s\n", str_array
[0], str_array
[1], str_array
[2]) );
880 if( g_str_has_prefix(str_array
[0], "1") )
882 fullcatname
= g_strdup(str_array
[2]);
884 lastcatname
= g_strdup(str_array
[2]);
886 type
= g_str_has_prefix(str_array
[1], "+") ? GF_INCOME
: 0;
888 DB( g_print(" + type = %d\n", type
) );
892 if( g_str_has_prefix(str_array
[0], "2") )
894 fullcatname
= g_strdup_printf("%s:%s", lastcatname
, str_array
[2]);
897 DB( g_print(" + fullcatname %s\n", fullcatname
) );
899 item
= da_cat_append_ifnew_by_fullname(fullcatname
, FALSE
);
901 DB( g_print(" + item %p\n", item
) );
905 DB( g_print(" + assign flags: '%c'\n", type
) );
912 g_strfreev (str_array
);
920 g_io_channel_unref (io
);
933 category_save_csv(gchar
*filename
, gchar
**error
)
935 gboolean retval
= FALSE
;
941 io
= g_io_channel_new_file(filename
, "w", NULL
);
944 lcat
= list
= category_glist_sorted(1);
948 Category
*item
= list
->data
;
954 if( item
->parent
== 0)
957 type
= (item
->flags
& GF_INCOME
) ? '+' : '-';
965 outstr
= g_strdup_printf("%c;%c;%s\n", lvel
, type
, item
->name
);
967 DB( g_print(" + export %s\n", outstr
) );
969 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
973 list
= g_list_next(list
);
980 g_io_channel_unref (io
);
988 gint
category_change_type(Category
*item
, gboolean isIncome
)
993 item
->flags
&= ~(GF_INCOME
); //remove flag
995 item
->flags
|= GF_INCOME
;
997 // change also childs
998 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
1001 Category
*child
= list
->data
;
1003 if(child
->parent
== item
->key
)
1005 child
->flags
&= ~(GF_INCOME
); //remove flag
1006 if(isIncome
== TRUE
)
1007 child
->flags
|= GF_INCOME
;
1010 list
= g_list_next(list
);
1023 * category_find_preset:
1025 * find a user language compatible file for category preset
1027 * Return value: a pathname to the file or NULL
1030 gchar
*category_find_preset(gchar
**lang
)
1037 DB( g_print("** category_find_preset **\n") );
1039 langs
= (gchar
**)g_get_language_names ();
1041 DB( g_print(" -> %d languages detected\n", g_strv_length(langs
)) );
1043 for(i
=0;i
<g_strv_length(langs
);i
++)
1045 DB( g_print(" -> %d '%s'\n", i
, langs
[i
]) );
1046 filename
= g_strdup_printf("hb-categories-%s.csv", langs
[i
]);
1047 gchar
*pathfilename
= g_build_filename(homebank_app_get_datas_dir(), filename
, NULL
);
1048 exists
= g_file_test(pathfilename
, G_FILE_TEST_EXISTS
);
1049 DB( g_print(" -> '%s' exists=%d\n", pathfilename
, exists
) );
1054 return pathfilename
;
1057 g_free(pathfilename
);
1060 DB( g_print("return NULL\n") );