]> Dogcows Code - chaz/homebank/blob - src/hb-category.c
add gitignore
[chaz/homebank] / src / hb-category.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 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 removed
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 * remove a category from the GHashTable
137 *
138 * Return value: TRUE if the key was found and removed
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 // ensure type equal for categories and its children
540 if(!(item->flags & GF_SUB) && item->key > 0)
541 {
542 isIncome = (item->flags & GF_INCOME) ? TRUE : FALSE;
543 category_change_type(item, isIncome);
544 }
545 g_strstrip(item->name);
546 }
547
548
549
550 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
551
552 #if MYDEBUG
553
554 static void
555 da_cat_debug_list_ghfunc(gpointer key, gpointer value, gpointer user_data)
556 {
557 guint32 *id = key;
558 Category *cat = value;
559
560 DB( g_print(" %d :: %s (parent=%d\n", *id, cat->name, cat->parent) );
561
562 }
563
564 static void
565 da_cat_debug_list(void)
566 {
567
568 DB( g_print("\n** debug **\n") );
569
570 g_hash_table_foreach(GLOBALS->h_cat, da_cat_debug_list_ghfunc, NULL);
571
572 DB( g_print("\n** end debug **\n") );
573
574 }
575
576 #endif
577
578
579
580 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
581
582 gboolean
583 category_is_used(guint32 key)
584 {
585 GList *lrul, *list;
586 guint i, nbsplit;
587
588 list = g_list_first(GLOBALS->ope_list);
589 while (list != NULL)
590 {
591 Transaction *entry = list->data;
592 if( key == entry->kcat )
593 return TRUE;
594
595 // check split category #1340142
596 nbsplit = da_transaction_splits_count(entry);
597 for(i=0;i<nbsplit;i++)
598 {
599 Split *split = entry->splits[i];
600
601 if( key == split->kcat )
602 return TRUE;
603 }
604
605 list = g_list_next(list);
606 }
607
608 list = g_list_first(GLOBALS->arc_list);
609 while (list != NULL)
610 {
611 Archive *entry = list->data;
612 if( key == entry->kcat )
613 return TRUE;
614 list = g_list_next(list);
615 }
616
617 //todo: add budget use here
618
619 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
620 while (list != NULL)
621 {
622 Assign *entry = list->data;
623
624 if( key == entry->kcat)
625 return TRUE;
626 list = g_list_next(list);
627 }
628 g_list_free(lrul);
629
630 return FALSE;
631 }
632
633 void
634 category_move(guint32 key1, guint32 key2)
635 {
636 GList *lrul, *list;
637 guint i, nbsplit;
638
639 list = g_list_first(GLOBALS->ope_list);
640 while (list != NULL)
641 {
642 Transaction *entry = list->data;
643 if(entry->kcat == key1)
644 {
645 entry->kcat = key2;
646 entry->flags |= OF_CHANGED;
647 }
648
649 // move split category #1340142
650 nbsplit = da_transaction_splits_count(entry);
651 for(i=0;i<nbsplit;i++)
652 {
653 Split *split = entry->splits[i];
654
655 if( split->kcat == key1 )
656 {
657 split->kcat = key2;
658 entry->flags |= OF_CHANGED;
659 }
660 }
661
662 list = g_list_next(list);
663 }
664
665 list = g_list_first(GLOBALS->arc_list);
666 while (list != NULL)
667 {
668 Archive *entry = list->data;
669 if(entry->kcat == key1)
670 {
671 entry->kcat = key2;
672 }
673 list = g_list_next(list);
674 }
675
676 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
677 while (list != NULL)
678 {
679 Assign *entry = list->data;
680
681 if(entry->kcat == key1)
682 {
683 entry->kcat = key2;
684 }
685 list = g_list_next(list);
686 }
687 g_list_free(lrul);
688
689 }
690
691
692 gboolean
693 category_rename(Category *item, const gchar *newname)
694 {
695 Category *parent, *existitem;
696 gchar *fullname = NULL;
697 gchar *stripname;
698 gboolean retval;
699
700 DB( g_print("(category) rename\n") );
701
702 stripname = g_strdup(newname);
703 g_strstrip(stripname);
704
705 if( item->parent == 0)
706 fullname = g_strdup(stripname);
707 else
708 {
709 parent = da_cat_get(item->parent);
710 if( parent )
711 {
712 fullname = g_strdup_printf("%s:%s", parent->name, stripname);
713 }
714 }
715
716 DB( g_print(" - search: %s\n", fullname) );
717
718 existitem = da_cat_get_by_fullname( fullname );
719
720 if( existitem != NULL && existitem->key != item->key)
721 {
722 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem->key, item->key) );
723 retval = FALSE;
724 }
725 else
726 {
727 DB( g_print(" -renaming\n") );
728
729 g_free(item->name);
730 item->name = g_strdup(stripname);
731 retval = TRUE;
732 }
733
734 g_free(fullname);
735 g_free(stripname);
736
737 return retval;
738 }
739
740
741 static gint category_glist_name_compare_func(Category *c1, Category *c2)
742 {
743 gchar *name1, *name2;
744 gint retval = 0;
745
746 if( c1 != NULL && c2 != NULL )
747 {
748 name1 = da_cat_get_fullname(c1);
749 name2 = da_cat_get_fullname(c2);
750
751 retval = hb_string_utf8_compare(name1, name2);
752
753 g_free(name2);
754 g_free(name1);
755 }
756 return retval;
757 }
758
759
760 static gint category_glist_key_compare_func(Category *a, Category *b)
761 {
762 gint ka, kb, retval = 0;
763
764 if(a->parent == 0 && b->parent == a->key)
765 retval = -1;
766 else
767 if(b->parent == 0 && a->parent == b->key)
768 retval = 1;
769 else
770 {
771 ka = a->parent != 0 ? a->parent : a->key;
772 kb = b->parent != 0 ? b->parent : b->key;
773 retval = ka - kb;
774 }
775
776
777 #if MYDEBUG == 1
778 gchar *str;
779
780 if(retval < 0)
781 str = "a < b";
782 else
783 if(retval ==0)
784 str = "a = b";
785 else
786 if(retval > 0)
787 str = "a > b";
788
789 DB( g_print("compare a=%2d:%2d to b=%2d:%2d :: %d [%s]\n", a->key, a->parent, b->key, b->parent, retval, str ) );
790 #endif
791
792 return retval;
793 }
794
795
796 GList *category_glist_sorted(gint column)
797 {
798 GList *list = g_hash_table_get_values(GLOBALS->h_cat);
799
800 if(column == 0)
801 return g_list_sort(list, (GCompareFunc)category_glist_key_compare_func);
802 else
803 return g_list_sort(list, (GCompareFunc)category_glist_name_compare_func);
804 }
805
806
807 gboolean
808 category_load_csv(gchar *filename, gchar **error)
809 {
810 gboolean retval;
811 GIOChannel *io;
812 gchar *tmpstr;
813 gint io_stat;
814 gchar **str_array;
815 gchar *lastcatname = NULL;
816 gchar *fullcatname;
817 GError *err = NULL;
818 Category *item;
819 gint type = 0;
820 const gchar *encoding;
821
822 encoding = homebank_file_getencoding(filename);
823
824 DB( g_print(" -> encoding should be %s\n", encoding) );
825
826
827 retval = TRUE;
828 *error = NULL;
829 io = g_io_channel_new_file(filename, "r", NULL);
830 if(io != NULL)
831 {
832
833 if( encoding != NULL )
834 {
835 g_io_channel_set_encoding(io, encoding, NULL);
836 }
837
838 for(;;)
839 {
840 if( *error != NULL )
841 break;
842 io_stat = g_io_channel_read_line(io, &tmpstr, NULL, NULL, &err);
843
844 DB( g_print(" + iostat %d\n", io_stat) );
845
846 if( io_stat == G_IO_STATUS_ERROR )
847 {
848 DB (g_print(" + ERROR %s\n",err->message));
849 break;
850 }
851 if( io_stat == G_IO_STATUS_EOF)
852 break;
853 if( io_stat == G_IO_STATUS_NORMAL)
854 {
855 if( tmpstr != NULL )
856 {
857 DB( g_print(" + strip %s\n", tmpstr) );
858
859 hb_string_strip_crlf(tmpstr);
860
861 DB( g_print(" + split\n") );
862
863 str_array = g_strsplit (tmpstr, ";", 3);
864 // type; sign; name
865
866 if( g_strv_length (str_array) != 3 )
867 {
868 *error = _("invalid csv format");
869 retval = FALSE;
870 DB( g_print(" + error %s\n", *error) );
871 }
872 else
873 {
874 DB( g_print(" + read %s : %s : %s\n", str_array[0], str_array[1], str_array[2]) );
875
876 fullcatname = NULL;
877 if( g_str_has_prefix(str_array[0], "1") )
878 {
879 fullcatname = g_strdup(str_array[2]);
880 g_free(lastcatname);
881 lastcatname = g_strdup(str_array[2]);
882
883 type = g_str_has_prefix(str_array[1], "+") ? GF_INCOME : 0;
884
885 DB( g_print(" + type = %d\n", type) );
886
887 }
888 else
889 if( g_str_has_prefix(str_array[0], "2") )
890 {
891 fullcatname = g_strdup_printf("%s:%s", lastcatname, str_array[2]);
892 }
893
894 DB( g_print(" + fullcatname %s\n", fullcatname) );
895
896 item = da_cat_append_ifnew_by_fullname(fullcatname, FALSE);
897
898 DB( g_print(" + item %p\n", item) );
899
900 if( item != NULL)
901 {
902 DB( g_print(" + assign flags: '%c'\n", type) );
903
904 item->flags |= type;
905
906 }
907
908 g_free(fullcatname);
909 g_strfreev (str_array);
910 }
911 }
912
913 }
914 g_free(tmpstr);
915
916 }
917 g_io_channel_unref (io);
918
919
920 }
921
922 g_free(lastcatname);
923
924 return retval;
925 }
926
927
928
929 gboolean
930 category_save_csv(gchar *filename, gchar **error)
931 {
932 gboolean retval = FALSE;
933 GIOChannel *io;
934 gchar *outstr;
935 GList *lcat, *list;
936
937
938 io = g_io_channel_new_file(filename, "w", NULL);
939 if(io != NULL)
940 {
941 lcat = list = category_glist_sorted(1);
942
943 while (list != NULL)
944 {
945 Category *item = list->data;
946
947 if(item->key != 0)
948 {
949 gchar lvel, type;
950
951 if( item->parent == 0)
952 {
953 lvel = '1';
954 type = (item->flags & GF_INCOME) ? '+' : '-';
955 }
956 else
957 {
958 lvel = '2';
959 type = ' ';
960 }
961
962 outstr = g_strdup_printf("%c;%c;%s\n", lvel, type, item->name);
963
964 DB( g_print(" + export %s\n", outstr) );
965
966 g_io_channel_write_chars(io, outstr, -1, NULL, NULL);
967
968 g_free(outstr);
969 }
970 list = g_list_next(list);
971 }
972
973 retval = TRUE;
974
975 g_list_free(lcat);
976
977 g_io_channel_unref (io);
978 }
979
980
981 return retval;
982 }
983
984
985 gint category_change_type(Category *item, gboolean isIncome)
986 {
987 gint changes = 1;
988 GList *lcat, *list;
989
990 item->flags &= ~(GF_INCOME); //remove flag
991 if(isIncome == TRUE)
992 item->flags |= GF_INCOME;
993
994 // change also childs
995 lcat = list = g_hash_table_get_values(GLOBALS->h_cat);
996 while (list != NULL)
997 {
998 Category *child = list->data;
999
1000 if(child->parent == item->key)
1001 {
1002 child->flags &= ~(GF_INCOME); //remove flag
1003 if(isIncome == TRUE)
1004 child->flags |= GF_INCOME;
1005 changes++;
1006 }
1007 list = g_list_next(list);
1008 }
1009
1010 g_list_free(lcat);
1011
1012 return changes;
1013 }
1014
1015
1016
1017
1018
1019 /**
1020 * category_find_preset:
1021 *
1022 * find a user language compatible file for category preset
1023 *
1024 * Return value: a pathname to the file or NULL
1025 *
1026 */
1027 gchar *category_find_preset(gchar **lang)
1028 {
1029 gchar **langs;
1030 gchar *filename;
1031 gboolean exists;
1032 guint i;
1033
1034 DB( g_print("** category_find_preset **\n") );
1035
1036 langs = (gchar **)g_get_language_names ();
1037
1038 DB( g_print(" -> %d languages detected\n", g_strv_length(langs)) );
1039
1040 for(i=0;i<g_strv_length(langs);i++)
1041 {
1042 DB( g_print(" -> %d '%s'\n", i, langs[i]) );
1043 filename = g_strdup_printf("hb-categories-%s.csv", langs[i]);
1044 gchar *pathfilename = g_build_filename(homebank_app_get_datas_dir(), filename, NULL);
1045 exists = g_file_test(pathfilename, G_FILE_TEST_EXISTS);
1046 DB( g_print(" -> '%s' exists=%d\n", pathfilename, exists) );
1047 if(exists)
1048 {
1049 g_free(filename);
1050 *lang = langs[i];
1051 return pathfilename;
1052 }
1053 g_free(filename);
1054 g_free(pathfilename);
1055 }
1056
1057 DB( g_print("return NULL\n") );
1058
1059 *lang = NULL;
1060 return NULL;
1061 }
1062
1063
This page took 0.081368 seconds and 4 git commands to generate.