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