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