]> Dogcows Code - chaz/homebank/blob - src/hb-category.c
Merge branch 'master' into ext-perl
[chaz/homebank] / src / hb-category.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include "homebank.h"
21 #include "hb-category.h"
22
23 #include "ext.h"
24 #include "refcount.h"
25
26
27 /****************************************************************************/
28 /* Debug macros */
29 /****************************************************************************/
30 #define MYDEBUG 0
31
32 #if MYDEBUG
33 #define DB(x) (x);
34 #else
35 #define DB(x);
36 #endif
37
38 /* our global datas */
39 extern struct HomeBank *GLOBALS;
40
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
42
43 Category *
44 da_cat_clone(Category *src_item)
45 {
46 Category *new_item = rc_dup(src_item, sizeof(Category));
47
48 DB( g_print("da_cat_clone\n") );
49 if(new_item)
50 {
51 //duplicate the string
52 new_item->name = g_strdup(src_item->name);
53 }
54 return new_item;
55 }
56
57
58 void
59 da_cat_free(Category *item)
60 {
61 DB( g_print("da_cat_free\n") );
62 if(rc_unref(item))
63 {
64 DB( g_print(" => %d, %s\n", item->key, item->name) );
65
66 g_free(item->name);
67 rc_free(item);
68 }
69 }
70
71
72 Category *
73 da_cat_malloc(void)
74 {
75 DB( g_print("da_cat_malloc\n") );
76 return rc_alloc(sizeof(Category));
77 }
78
79
80 void
81 da_cat_destroy(void)
82 {
83 DB( g_print("da_cat_destroy\n") );
84 g_hash_table_destroy(GLOBALS->h_cat);
85 }
86
87
88 void
89 da_cat_new(void)
90 {
91 Category *item;
92
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);
95
96 // insert our 'no category'
97 item = da_cat_malloc();
98 item->name = g_strdup("");
99 da_cat_insert(item);
100 }
101
102
103 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
104
105 /**
106 * da_cat_length:
107 *
108 * Return value: the number of elements
109 */
110 guint
111 da_cat_length(void)
112 {
113 return g_hash_table_size(GLOBALS->h_cat);
114 }
115
116
117
118 /**
119 * da_cat_remove_grfunc:
120 *
121 * GRFunc to get the max id
122 *
123 * Return value: TRUE if the key/value must be deleted
124 *
125 */
126 static gboolean
127 da_cat_remove_grfunc(gpointer key, Category *cat, guint32 *remkey)
128 {
129 if(cat->key == *remkey || cat->parent == *remkey)
130 return TRUE;
131
132 return FALSE;
133 }
134
135
136 /**
137 * da_cat_remove:
138 *
139 * delete a category from the GHashTable
140 *
141 * Return value: TRUE if the key was found and deleted
142 *
143 */
144 guint
145 da_cat_remove(guint32 key)
146 {
147 DB( g_print("da_cat_remove %d\n", key) );
148
149 return g_hash_table_foreach_remove(GLOBALS->h_cat, (GHRFunc)da_cat_remove_grfunc, &key);
150 }
151
152 /**
153 * da_cat_insert:
154 *
155 * insert a category into the GHashTable
156 *
157 * Return value: TRUE if inserted
158 *
159 */
160 gboolean
161 da_cat_insert(Category *item)
162 {
163 guint32 *new_key;
164
165 DB( g_print("da_cat_insert\n") );
166
167 new_key = g_new0(guint32, 1);
168 *new_key = item->key;
169 g_hash_table_insert(GLOBALS->h_cat, new_key, item);
170
171 return TRUE;
172 }
173
174
175 /**
176 * da_cat_append:
177 *
178 * append a category into the GHashTable
179 *
180 * Return value: TRUE if inserted
181 *
182 */
183 gboolean
184 da_cat_append(Category *cat)
185 {
186 Category *existitem;
187 guint32 *new_key;
188 gchar *fullname;
189
190 DB( g_print("da_cat_append\n") );
191
192 if( cat->name != NULL)
193 {
194
195 fullname = da_cat_get_fullname(cat);
196 existitem = da_cat_get_by_fullname( fullname );
197 g_free(fullname);
198
199 if( existitem == NULL )
200 {
201 new_key = g_new0(guint32, 1);
202 *new_key = da_cat_get_max_key() + 1;
203 cat->key = *new_key;
204
205 DB( g_print(" -> insert id: %d\n", *new_key) );
206
207 g_hash_table_insert(GLOBALS->h_cat, new_key, cat);
208 return TRUE;
209 }
210
211 }
212
213 DB( g_print(" -> %s already exist\n", cat->name) );
214
215 return FALSE;
216 }
217
218
219 /**
220 * da_cat_max_key_ghfunc:
221 *
222 * GHFunc for biggest key
223 *
224 */
225 static void
226 da_cat_max_key_ghfunc(gpointer key, Category *cat, guint32 *max_key)
227 {
228
229 *max_key = MAX(*max_key, cat->key);
230 }
231
232 /**
233 * da_cat_get_max_key:
234 *
235 * Get the biggest key from the GHashTable
236 *
237 * Return value: the biggest key value
238 *
239 */
240 guint32
241 da_cat_get_max_key(void)
242 {
243 guint32 max_key = 0;
244
245 g_hash_table_foreach(GLOBALS->h_cat, (GHFunc)da_cat_max_key_ghfunc, &max_key);
246 return max_key;
247 }
248
249 /**
250 * da_cat_get_fullname:
251 *
252 * Get category the fullname 'xxxx:yyyyy'
253 *
254 * Return value: the category fullname (free it with g_free)
255 *
256 */
257 gchar *
258 da_cat_get_fullname(Category *cat)
259 {
260 Category *parent;
261
262 if( cat->parent == 0 )
263 return g_strdup(cat->name);
264 else
265 {
266 parent = da_cat_get(cat->parent);
267 if( parent )
268 {
269 return g_strdup_printf("%s:%s", parent->name, cat->name);
270 }
271 }
272
273 return NULL;
274 }
275
276
277 /**
278 * da_cat_name_grfunc:
279 *
280 * GRFunc to get the max id
281 *
282 * Return value: TRUE if the key/value pair match our name
283 *
284 */
285 static gboolean
286 da_cat_name_grfunc(gpointer key, Category *cat, gchar *name)
287 {
288
289 // DB( g_print("%s == %s\n", name, cat->name) );
290 if( name && cat->name)
291 {
292 if(!strcasecmp(name, cat->name))
293 return TRUE;
294 }
295 return FALSE;
296 }
297
298 /**
299 * da_cat_get_key_by_name:
300 *
301 * Get a category key by its name
302 *
303 * Return value: the category key or -1 if not found
304 *
305 */
306 guint32
307 da_cat_get_key_by_name(gchar *name)
308 {
309 Category *cat;
310
311 DB( g_print("da_cat_get_key_by_name\n") );
312
313 cat = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_name_grfunc, name);
314 if( cat == NULL)
315 return -1;
316
317 return cat->key;
318 }
319
320 /**
321 * da_cat_get_by_name:
322 *
323 * Get a category structure by its name
324 *
325 * Return value: Category * or NULL if not found
326 *
327 */
328 Category *
329 da_cat_get_by_name(gchar *name)
330 {
331 DB( g_print("da_cat_get_by_name\n") );
332
333 return g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_name_grfunc, name);
334 }
335
336
337 /* fullname i.e. car:refuel */
338 struct fullcatcontext
339 {
340 guint parent;
341 gchar *name;
342 };
343
344
345 static gboolean
346 da_cat_fullname_grfunc(gpointer key, Category *item, struct fullcatcontext *ctx)
347 {
348
349 //DB( g_print("'%s' == '%s'\n", ctx->name, item->name) );
350 if( item->parent == ctx->parent )
351 {
352 if(!strcasecmp(ctx->name, item->name))
353 return TRUE;
354 }
355 return FALSE;
356 }
357
358 Category *
359 da_cat_get_by_fullname(gchar *fullname)
360 {
361 struct fullcatcontext ctx;
362 gchar **typestr;
363 Category *item = NULL;
364
365 DB( g_print("da_cat_get_by_fullname\n") );
366
367 typestr = g_strsplit(fullname, ":", 2);
368 if( g_strv_length(typestr) == 2 )
369 {
370 ctx.parent = 0;
371 ctx.name = typestr[0];
372 DB( g_print(" [x:x] try to find the parent : '%s'\n", typestr[0]) );
373
374 Category *parent = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
375 if( parent != NULL )
376 {
377 ctx.parent = parent->key;
378 ctx.name = typestr[1];
379
380 DB( g_print(" [x:x] and searching sub %d '%s'\n", ctx.parent, ctx.name) );
381
382 item = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
383 }
384 }
385 else
386 {
387 ctx.parent = 0;
388 ctx.name = fullname;
389
390 DB( g_print(" [x] try to '%s'\n", fullname) );
391
392 item = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
393 }
394
395 g_strfreev(typestr);
396
397 DB( g_print(" return value %p\n", item) );
398
399 return item;
400 }
401
402
403 /**
404 * da_cat_append_ifnew_by_fullname:
405 *
406 * append a category if it is new by fullname
407 *
408 * Return value:
409 *
410 */
411 Category *
412 da_cat_append_ifnew_by_fullname(gchar *fullname, gboolean imported)
413 {
414 struct fullcatcontext ctx;
415 gchar **typestr;
416 Category *newcat, *item, *retval = NULL;
417 guint32 *new_key;
418
419 DB( g_print("da_cat_append_ifnew_by_fullname\n") );
420
421 DB( g_print(" -> fullname: '%s' %d\n", fullname, strlen(fullname)) );
422
423 if( strlen(fullname) > 0 )
424 {
425 typestr = g_strsplit(fullname, ":", 2);
426
427 /* if we have a subcategory : aaaa:bbb */
428 if( g_strv_length(typestr) == 2 )
429 {
430 ctx.parent = 0;
431 ctx.name = typestr[0];
432 DB( g_print(" try to find the parent:'%s'\n", typestr[0]) );
433
434 Category *parent = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
435 if( parent == NULL )
436 {
437 DB( g_print(" -> not found\n") );
438
439 // append a new category
440 new_key = g_new0(guint32, 1);
441 *new_key = da_cat_get_max_key() + 1;
442
443 newcat = da_cat_malloc();
444 newcat->key = *new_key;
445 newcat->name = g_strdup(typestr[0]);
446 newcat->imported = imported;
447
448 parent = newcat;
449
450 DB( g_print(" -> insert cat '%s' id: %d\n", newcat->name, newcat->key) );
451
452 g_hash_table_insert(GLOBALS->h_cat, new_key, newcat);
453 }
454
455 ctx.parent = parent->key;
456 ctx.name = typestr[1];
457 DB( g_print(" searching %d '%s'\n", ctx.parent, ctx.name) );
458
459 item = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
460 if( item == NULL )
461 {
462 // append a new subcategory
463 new_key = g_new0(guint32, 1);
464 *new_key = da_cat_get_max_key() + 1;
465
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;
471
472 newcat->flags |= GF_SUB;
473
474 DB( g_print(" -> insert subcat '%s' id: %d\n", newcat->name, newcat->key) );
475
476 g_hash_table_insert(GLOBALS->h_cat, new_key, newcat);
477
478 retval = newcat;
479 }
480 else
481 retval = item;
482 }
483 /* this a single category : aaaa */
484 else
485 {
486 ctx.parent = 0;
487 ctx.name = typestr[0];
488 DB( g_print(" searching %d '%s'\n", ctx.parent, ctx.name) );
489
490 item = g_hash_table_find(GLOBALS->h_cat, (GHRFunc)da_cat_fullname_grfunc, &ctx);
491 if( item == NULL )
492 {
493 // append a new category
494 new_key = g_new0(guint32, 1);
495 *new_key = da_cat_get_max_key() + 1;
496
497 newcat = da_cat_malloc();
498 newcat->key = *new_key;
499 newcat->name = g_strdup(typestr[0]);
500 newcat->imported = imported;
501
502 DB( g_print(" -> insert cat '%s' id: %d\n", newcat->name, newcat->key) );
503
504 g_hash_table_insert(GLOBALS->h_cat, new_key, newcat);
505
506 retval = newcat;
507 }
508 else
509 retval = item;
510
511 }
512
513 g_strfreev(typestr);
514 }
515
516 return retval;
517 }
518
519
520
521 /**
522 * da_cat_get:
523 *
524 * Get a category structure by key
525 *
526 * Return value: Category * or NULL if not found
527 *
528 */
529 Category *
530 da_cat_get(guint32 key)
531 {
532 //DB( g_print("da_cat_get\n") );
533
534 return g_hash_table_lookup(GLOBALS->h_cat, &key);
535 }
536
537
538 void da_cat_consistency(Category *item)
539 {
540 gboolean isIncome;
541
542 if((item->flags & GF_SUB) && item->key > 0)
543 {
544 //check for existing parent
545 if( da_cat_get(item->parent) == NULL )
546 {
547 Category *parent = da_cat_append_ifnew_by_fullname ("orphaned", FALSE);
548
549 item->parent = parent->key;
550
551 g_warning("category consistency: fixed missing parent %d", item->parent);
552 }
553 }
554
555 // ensure type equal for categories and its children
556 if(!(item->flags & GF_SUB) && item->key > 0)
557 {
558 isIncome = (item->flags & GF_INCOME) ? TRUE : FALSE;
559 if( category_change_type(item, isIncome) > 0 )
560 {
561 g_warning("category consistency: fixed type for child");
562 GLOBALS->changes_count++;
563 }
564 }
565
566 g_strstrip(item->name);
567 }
568
569
570
571 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
572
573 #if MYDEBUG
574
575 static void
576 da_cat_debug_list_ghfunc(gpointer key, gpointer value, gpointer user_data)
577 {
578 guint32 *id = key;
579 Category *cat = value;
580
581 DB( g_print(" %d :: %s (parent=%d\n", *id, cat->name, cat->parent) );
582
583 }
584
585 static void
586 da_cat_debug_list(void)
587 {
588
589 DB( g_print("\n** debug **\n") );
590
591 g_hash_table_foreach(GLOBALS->h_cat, da_cat_debug_list_ghfunc, NULL);
592
593 DB( g_print("\n** end debug **\n") );
594
595 }
596
597 #endif
598
599
600
601 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
602
603 guint32 category_report_id(guint32 key, gboolean subcat)
604 {
605 Category *catentry = da_cat_get(key);
606 guint32 retval = 0;
607
608 if(catentry)
609 {
610 if(subcat == FALSE)
611 {
612 retval = (catentry->flags & GF_SUB) ? catentry->parent : catentry->key;
613 }
614 else
615 {
616 retval = catentry->key;
617 }
618 }
619 return retval;
620 }
621
622
623 void
624 category_delete_unused(void)
625 {
626 GList *lcat, *list;
627
628 lcat = list = g_hash_table_get_values(GLOBALS->h_cat);
629 while (list != NULL)
630 {
631 Category *entry = list->data;
632
633 if(entry->usage_count <= 0 && entry->key > 0)
634 da_cat_remove (entry->key);
635
636 list = g_list_next(list);
637 }
638 g_list_free(lcat);
639 }
640
641
642 static void
643 category_fill_usage_count(guint32 kcat)
644 {
645 Category *cat = da_cat_get (kcat);
646 Category *parent;
647
648 if(cat)
649 {
650 cat->usage_count++;
651 if( cat->parent > 0 )
652 {
653 parent = da_cat_get(cat->parent);
654 if( parent )
655 {
656 parent->usage_count++;
657 }
658 }
659 }
660 }
661
662
663 void
664 category_fill_usage(void)
665 {
666 GList *lcat;
667 GList *lst_acc, *lnk_acc;
668 GList *lnk_txn;
669 GList *lpay, *lrul, *list;
670
671 lcat = list = g_hash_table_get_values(GLOBALS->h_cat);
672 while (list != NULL)
673 {
674 Category *entry = list->data;
675 entry->usage_count = 0;
676 list = g_list_next(list);
677 }
678 g_list_free(lcat);
679
680
681 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
682 lnk_acc = g_list_first(lst_acc);
683 while (lnk_acc != NULL)
684 {
685 Account *acc = lnk_acc->data;
686
687 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
688 while (lnk_txn != NULL)
689 {
690 Transaction *txn = lnk_txn->data;
691
692 category_fill_usage_count(txn->kcat);
693 lnk_txn = g_list_next(lnk_txn);
694 }
695 lnk_acc = g_list_next(lnk_acc);
696 }
697 g_list_free(lst_acc);
698
699 lpay = list = g_hash_table_get_values(GLOBALS->h_pay);
700 while (list != NULL)
701 {
702 Payee *entry = list->data;
703
704 category_fill_usage_count(entry->kcat);
705 list = g_list_next(list);
706 }
707 g_list_free(lpay);
708
709
710 list = g_list_first(GLOBALS->arc_list);
711 while (list != NULL)
712 {
713 Archive *entry = list->data;
714
715 category_fill_usage_count(entry->kcat);
716 list = g_list_next(list);
717 }
718
719
720 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
721 while (list != NULL)
722 {
723 Assign *entry = list->data;
724
725 category_fill_usage_count(entry->kcat);
726 list = g_list_next(list);
727 }
728 g_list_free(lrul);
729
730 }
731
732
733 void
734 category_move(guint32 key1, guint32 key2)
735 {
736 GList *lst_acc, *lnk_acc;
737 GList *lnk_txn;
738 GList *lrul, *list;
739 guint i, nbsplit;
740
741 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
742 lnk_acc = g_list_first(lst_acc);
743 while (lnk_acc != NULL)
744 {
745 Account *acc = lnk_acc->data;
746
747 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
748 while (lnk_txn != NULL)
749 {
750 Transaction *txn = lnk_txn->data;
751
752 if(txn->kcat == key1)
753 {
754 txn->kcat = key2;
755 txn->flags |= OF_CHANGED;
756 }
757
758 // move split category #1340142
759 nbsplit = da_splits_count(txn->splits);
760 for(i=0;i<nbsplit;i++)
761 {
762 Split *split = txn->splits[i];
763
764 if( split->kcat == key1 )
765 {
766 split->kcat = key2;
767 txn->flags |= OF_CHANGED;
768 }
769 }
770
771 lnk_txn = g_list_next(lnk_txn);
772 }
773
774 lnk_acc = g_list_next(lnk_acc);
775 }
776 g_list_free(lst_acc);
777
778
779 list = g_list_first(GLOBALS->arc_list);
780 while (list != NULL)
781 {
782 Archive *entry = list->data;
783 if(entry->kcat == key1)
784 {
785 entry->kcat = key2;
786 }
787 list = g_list_next(list);
788 }
789
790 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
791 while (list != NULL)
792 {
793 Assign *entry = list->data;
794
795 if(entry->kcat == key1)
796 {
797 entry->kcat = key2;
798 }
799 list = g_list_next(list);
800 }
801 g_list_free(lrul);
802
803 }
804
805
806 gboolean
807 category_rename(Category *item, const gchar *newname)
808 {
809 Category *parent, *existitem;
810 gchar *fullname = NULL;
811 gchar *stripname;
812 gboolean retval;
813
814 DB( g_print("(category) rename\n") );
815
816 stripname = g_strdup(newname);
817 g_strstrip(stripname);
818
819 if( item->parent == 0)
820 fullname = g_strdup(stripname);
821 else
822 {
823 parent = da_cat_get(item->parent);
824 if( parent )
825 {
826 fullname = g_strdup_printf("%s:%s", parent->name, stripname);
827 }
828 }
829
830 DB( g_print(" - search: %s\n", fullname) );
831
832 existitem = da_cat_get_by_fullname( fullname );
833
834 if( existitem != NULL && existitem->key != item->key)
835 {
836 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem->key, item->key) );
837 retval = FALSE;
838 }
839 else
840 {
841 DB( g_print(" -renaming\n") );
842
843 g_free(item->name);
844 item->name = g_strdup(stripname);
845 retval = TRUE;
846 }
847
848 g_free(fullname);
849 g_free(stripname);
850
851 return retval;
852 }
853
854
855 static gint category_glist_name_compare_func(Category *c1, Category *c2)
856 {
857 gchar *name1, *name2;
858 gint retval = 0;
859
860 if( c1 != NULL && c2 != NULL )
861 {
862 name1 = da_cat_get_fullname(c1);
863 name2 = da_cat_get_fullname(c2);
864
865 retval = hb_string_utf8_compare(name1, name2);
866
867 g_free(name2);
868 g_free(name1);
869 }
870 return retval;
871 }
872
873
874 static gint category_glist_key_compare_func(Category *a, Category *b)
875 {
876 gint ka, kb, retval = 0;
877
878 if(a->parent == 0 && b->parent == a->key)
879 retval = -1;
880 else
881 if(b->parent == 0 && a->parent == b->key)
882 retval = 1;
883 else
884 {
885 ka = a->parent != 0 ? a->parent : a->key;
886 kb = b->parent != 0 ? b->parent : b->key;
887 retval = ka - kb;
888 }
889
890
891 #if MYDEBUG == 1
892 gchar *str;
893
894 if(retval < 0)
895 str = "a < b";
896 else
897 if(retval ==0)
898 str = "a = b";
899 else
900 if(retval > 0)
901 str = "a > b";
902
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 ) );
904 #endif
905
906 return retval;
907 }
908
909
910 GList *category_glist_sorted(gint column)
911 {
912 GList *list = g_hash_table_get_values(GLOBALS->h_cat);
913
914 if(column == 0)
915 return g_list_sort(list, (GCompareFunc)category_glist_key_compare_func);
916 else
917 return g_list_sort(list, (GCompareFunc)category_glist_name_compare_func);
918 }
919
920
921 gboolean
922 category_load_csv(gchar *filename, gchar **error)
923 {
924 gboolean retval;
925 GIOChannel *io;
926 gchar *tmpstr;
927 gint io_stat;
928 gchar **str_array;
929 gchar *lastcatname = NULL;
930 gchar *fullcatname;
931 GError *err = NULL;
932 Category *item;
933 gint type = 0;
934 const gchar *encoding;
935
936 encoding = homebank_file_getencoding(filename);
937
938 DB( g_print(" -> encoding should be %s\n", encoding) );
939
940
941 retval = TRUE;
942 *error = NULL;
943 io = g_io_channel_new_file(filename, "r", NULL);
944 if(io != NULL)
945 {
946
947 if( encoding != NULL )
948 {
949 g_io_channel_set_encoding(io, encoding, NULL);
950 }
951
952 for(;;)
953 {
954 if( *error != NULL )
955 break;
956 io_stat = g_io_channel_read_line(io, &tmpstr, NULL, NULL, &err);
957
958 DB( g_print(" + iostat %d\n", io_stat) );
959
960 if( io_stat == G_IO_STATUS_ERROR )
961 {
962 DB (g_print(" + ERROR %s\n",err->message));
963 break;
964 }
965 if( io_stat == G_IO_STATUS_EOF)
966 break;
967 if( io_stat == G_IO_STATUS_NORMAL)
968 {
969 if( tmpstr != NULL )
970 {
971 DB( g_print(" + strip %s\n", tmpstr) );
972
973 hb_string_strip_crlf(tmpstr);
974
975 DB( g_print(" + split\n") );
976
977 str_array = g_strsplit (tmpstr, ";", 3);
978 // type; sign; name
979
980 if( g_strv_length (str_array) != 3 )
981 {
982 *error = _("invalid CSV format");
983 retval = FALSE;
984 DB( g_print(" + error %s\n", *error) );
985 }
986 else
987 {
988 DB( g_print(" + read %s : %s : %s\n", str_array[0], str_array[1], str_array[2]) );
989
990 fullcatname = NULL;
991 if( g_str_has_prefix(str_array[0], "1") )
992 {
993 fullcatname = g_strdup(str_array[2]);
994 g_free(lastcatname);
995 lastcatname = g_strdup(str_array[2]);
996
997 type = g_str_has_prefix(str_array[1], "+") ? GF_INCOME : 0;
998
999 DB( g_print(" + type = %d\n", type) );
1000
1001 }
1002 else
1003 if( g_str_has_prefix(str_array[0], "2") )
1004 {
1005 fullcatname = g_strdup_printf("%s:%s", lastcatname, str_array[2]);
1006 }
1007
1008 DB( g_print(" + fullcatname %s\n", fullcatname) );
1009
1010 item = da_cat_append_ifnew_by_fullname(fullcatname, FALSE);
1011
1012 DB( g_print(" + item %p\n", item) );
1013
1014 if( item != NULL)
1015 {
1016 DB( g_print(" + assign flags: '%c'\n", type) );
1017
1018 item->flags |= type;
1019
1020 }
1021
1022 g_free(fullcatname);
1023 g_strfreev (str_array);
1024 }
1025 }
1026
1027 }
1028 g_free(tmpstr);
1029
1030 }
1031 g_io_channel_unref (io);
1032
1033
1034 }
1035
1036 g_free(lastcatname);
1037
1038 return retval;
1039 }
1040
1041
1042
1043 gboolean
1044 category_save_csv(gchar *filename, gchar **error)
1045 {
1046 gboolean retval = FALSE;
1047 GIOChannel *io;
1048 gchar *outstr;
1049 GList *lcat, *list;
1050
1051
1052 io = g_io_channel_new_file(filename, "w", NULL);
1053 if(io != NULL)
1054 {
1055 lcat = list = category_glist_sorted(1);
1056
1057 while (list != NULL)
1058 {
1059 Category *item = list->data;
1060
1061 if(item->key != 0)
1062 {
1063 gchar lvel, type;
1064
1065 if( item->parent == 0)
1066 {
1067 lvel = '1';
1068 type = (item->flags & GF_INCOME) ? '+' : '-';
1069 }
1070 else
1071 {
1072 lvel = '2';
1073 type = ' ';
1074 }
1075
1076 outstr = g_strdup_printf("%c;%c;%s\n", lvel, type, item->name);
1077
1078 DB( g_print(" + export %s\n", outstr) );
1079
1080 g_io_channel_write_chars(io, outstr, -1, NULL, NULL);
1081
1082 g_free(outstr);
1083 }
1084 list = g_list_next(list);
1085 }
1086
1087 retval = TRUE;
1088
1089 g_list_free(lcat);
1090
1091 g_io_channel_unref (io);
1092 }
1093
1094
1095 return retval;
1096 }
1097
1098 gint category_type_get(Category *item)
1099 {
1100 if( (item->flags & (GF_INCOME)) )
1101 return 1;
1102 return -1;
1103 }
1104
1105
1106
1107 static gint category_change_type_eval(Category *item, gboolean isIncome)
1108 {
1109 if( (item->flags & (GF_INCOME)) && !isIncome )
1110 return 1;
1111 return 0;
1112 }
1113
1114
1115 gint category_change_type(Category *item, gboolean isIncome)
1116 {
1117 gint changes = 0;
1118 GList *lcat, *list;
1119
1120 changes += category_change_type_eval(item, isIncome);
1121
1122 item->flags &= ~(GF_INCOME); //delete flag
1123 if(isIncome == TRUE)
1124 item->flags |= GF_INCOME;
1125
1126 // change also childs
1127 lcat = list = g_hash_table_get_values(GLOBALS->h_cat);
1128 while (list != NULL)
1129 {
1130 Category *child = list->data;
1131
1132 if(child->parent == item->key)
1133 {
1134 changes += category_change_type_eval(child, isIncome);
1135 child->flags &= ~(GF_INCOME); //delete flag
1136 if(isIncome == TRUE)
1137 child->flags |= GF_INCOME;
1138 }
1139 list = g_list_next(list);
1140 }
1141
1142 g_list_free(lcat);
1143
1144 return changes;
1145 }
1146
1147
1148
1149
1150
1151 /**
1152 * category_find_preset:
1153 *
1154 * find a user language compatible file for category preset
1155 *
1156 * Return value: a pathname to the file or NULL
1157 *
1158 */
1159 gchar *category_find_preset(gchar **lang)
1160 {
1161 gchar **langs;
1162 gchar *filename;
1163 gboolean exists;
1164 guint i;
1165
1166 DB( g_print("** category_find_preset **\n") );
1167
1168 langs = (gchar **)g_get_language_names ();
1169
1170 DB( g_print(" -> %d languages detected\n", g_strv_length(langs)) );
1171
1172 for(i=0;i<g_strv_length(langs);i++)
1173 {
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) );
1179 if(exists)
1180 {
1181 g_free(filename);
1182 *lang = langs[i];
1183 return pathfilename;
1184 }
1185 g_free(filename);
1186 g_free(pathfilename);
1187 }
1188
1189 DB( g_print("return NULL\n") );
1190
1191 *lang = NULL;
1192 return NULL;
1193 }
1194
1195
This page took 0.089239 seconds and 4 git commands to generate.