]>
Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "hb-hbfile.h"
22 #include "hb-archive.h"
23 #include "hb-transaction.h"
25 /****************************************************************************/
27 /****************************************************************************/
36 /* our global datas */
37 extern struct HomeBank
*GLOBALS
;
38 extern struct Preferences
*PREFS
;
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
44 gboolean
hbfile_file_hasbackup(gchar
*filepath
)
48 bakfilepath
= hb_util_filename_new_with_extension(GLOBALS
->xhb_filepath
, "xhb~");
49 GLOBALS
->xhb_hasbak
= g_file_test(bakfilepath
, G_FILE_TEST_EXISTS
);
51 //todo check here if need to return something
52 return GLOBALS
->xhb_hasbak
;
60 static gint hbfile_file_load_xhb(gchar *filepath)
71 static void hbfile_file_load_backup_xhb(void)
73 //todo: get from dialog.c, and split between dilaog.c/hbfile.c
80 void hbfile_replace_basecurrency(Currency4217
*curfmt
)
85 DB( g_print("\n[hbfile] replace base currency \n") );
87 oldkcur
= GLOBALS
->kcur
;
88 da_cur_remove(oldkcur
);
89 item
= currency_add_from_user(curfmt
);
90 GLOBALS
->kcur
= item
->key
;
92 DB( g_print(" %d ==> %d %s\n", oldkcur
, GLOBALS
->kcur
, item
->iso_code
) );
96 void hbfile_change_basecurrency(guint32 key
)
101 // set every rate to 0
102 list
= g_hash_table_get_values(GLOBALS
->h_cur
);
105 Currency
*entry
= list
->data
;
107 if(entry
->key
!= GLOBALS
->kcur
)
113 list
= g_list_next(list
);
117 oldkcur
= GLOBALS
->kcur
;
120 // update account with old base currency
121 list
= g_hash_table_get_values(GLOBALS
->h_acc
);
124 Account
*acc
= list
->data
;
126 if( acc
->kcur
== oldkcur
)
129 list
= g_list_next(list
);
134 GLOBALS
->changes_count
++;
138 GList
*hbfile_transaction_get_all(guint32 kacc
)
140 GList
*lst_acc
, *lnk_acc
;
146 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
147 lnk_acc
= g_list_first(lst_acc
);
148 while (lnk_acc
!= NULL
)
150 Account
*acc
= lnk_acc
->data
;
152 if( (acc
->flags
& (AF_CLOSED
|AF_NOREPORT
)) )
154 if( (kacc
> 0 ) && (acc
->key
!= kacc
) )
157 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
158 while (lnk_txn
!= NULL
)
160 list
= g_list_append(list
, lnk_txn
->data
);
161 lnk_txn
= g_list_next(lnk_txn
);
165 lnk_acc
= g_list_next(lnk_acc
);
167 g_list_free(lst_acc
);
169 return da_transaction_sort (list
);
173 GQueue
*hbfile_transaction_get_partial(guint32 minjulian
, guint32 maxjulian
)
175 GList
*lst_acc
, *lnk_acc
;
179 txn_queue
= g_queue_new ();
181 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
182 lnk_acc
= g_list_first(lst_acc
);
183 while (lnk_acc
!= NULL
)
185 Account
*acc
= lnk_acc
->data
;
187 if( (acc
->flags
& (AF_CLOSED
|AF_NOREPORT
)) )
190 lnk_txn
= g_queue_peek_tail_link(acc
->txn_queue
);
191 while (lnk_txn
!= NULL
)
193 Transaction
*txn
= lnk_txn
->data
;
195 if( txn
->date
< minjulian
) //no need to go below mindate
198 if( !(txn
->status
== TXN_STATUS_REMIND
)
199 && (txn
->date
>= minjulian
)
200 && (txn
->date
<= maxjulian
)
203 g_queue_push_head (txn_queue
, txn
);
206 lnk_txn
= g_list_previous(lnk_txn
);
210 lnk_acc
= g_list_next(lnk_acc
);
212 g_list_free(lst_acc
);
218 void hbfile_sanity_check(void)
220 GList
*lst_acc
, *lnk_acc
;
224 DB( g_print("\n[hbfile] !! sanity_check !! \n") );
226 DB( g_print(" - transaction\n") );
227 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
228 lnk_acc
= g_list_first(lst_acc
);
229 while (lnk_acc
!= NULL
)
231 Account
*acc
= lnk_acc
->data
;
233 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
234 while (lnk_txn
!= NULL
)
236 Transaction
*txn
= lnk_txn
->data
;
238 da_transaction_consistency(txn
);
239 lnk_txn
= g_list_next(lnk_txn
);
241 lnk_acc
= g_list_next(lnk_acc
);
243 g_list_free(lst_acc
);
246 DB( g_print(" - scheduled/template\n") );
247 list
= g_list_first(GLOBALS
->arc_list
);
250 Archive
*entry
= list
->data
;
252 da_archive_consistency(entry
);
253 list
= g_list_next(list
);
257 DB( g_print(" - account\n") );
258 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
261 Account
*item
= list
->data
;
263 da_acc_consistency(item
);
264 list
= g_list_next(list
);
269 DB( g_print(" - payee\n") );
270 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
273 Payee
*item
= list
->data
;
275 da_pay_consistency(item
);
276 list
= g_list_next(list
);
281 DB( g_print(" - category\n") );
282 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
285 Category
*item
= list
->data
;
287 da_cat_consistency(item
);
288 list
= g_list_next(list
);
295 void hbfile_anonymize(void)
297 GList
*lst_acc
, *lnk_acc
;
302 DB( g_print("\n[hbfile] anonymize\n") );
305 hbfile_change_owner(g_strdup("An0nym0us"));
306 GLOBALS
->changes_count
++;
307 GLOBALS
->hbfile_is_new
= TRUE
;
310 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "anonymized.xhb", NULL
));
313 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
316 Account
*item
= list
->data
;
318 item
->name
= g_strdup_printf("account %d", item
->key
);
319 g_free(item
->number
);
321 g_free(item
->bankname
);
322 item
->bankname
= NULL
;
324 GLOBALS
->changes_count
++;
325 list
= g_list_next(list
);
330 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
333 Payee
*item
= list
->data
;
338 item
->name
= g_strdup_printf("payee %d", item
->key
);
339 GLOBALS
->changes_count
++;
341 list
= g_list_next(list
);
346 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
349 Category
*item
= list
->data
;
354 item
->name
= g_strdup_printf("category %d", item
->key
);
355 GLOBALS
->changes_count
++;
357 list
= g_list_next(list
);
362 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_tag
);
365 Tag
*item
= list
->data
;
370 item
->name
= g_strdup_printf("tag %d", item
->key
);
371 GLOBALS
->changes_count
++;
373 list
= g_list_next(list
);
378 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
381 Assign
*item
= list
->data
;
386 item
->text
= g_strdup_printf("assign %d", item
->key
);
387 GLOBALS
->changes_count
++;
389 list
= g_list_next(list
);
395 list
= g_list_first(GLOBALS
->arc_list
);
398 Archive
*item
= list
->data
;
400 g_free(item
->wording
);
401 item
->wording
= g_strdup_printf("archive %d", cnt
++);
402 GLOBALS
->changes_count
++;
404 //later split anonymize also
406 list
= g_list_next(list
);
410 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
411 lnk_acc
= g_list_first(lst_acc
);
412 while (lnk_acc
!= NULL
)
414 Account
*acc
= lnk_acc
->data
;
416 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
417 while (lnk_txn
!= NULL
)
419 Transaction
*item
= lnk_txn
->data
;
424 g_free(item
->wording
);
425 item
->wording
= g_strdup_printf("memo %d", item
->date
);
426 GLOBALS
->changes_count
++;
428 if(item
->flags
& OF_SPLIT
)
430 for(i
=0;i
<TXN_MAX_SPLIT
;i
++)
432 split
= item
->splits
[i
];
433 if( split
== NULL
) break;
435 if(split
->memo
!= NULL
)
438 split
->memo
= g_strdup_printf("memo %d", i
);
439 GLOBALS
->changes_count
++;
442 lnk_txn
= g_list_next(lnk_txn
);
444 lnk_acc
= g_list_next(lnk_acc
);
446 g_list_free(lst_acc
);
451 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
454 void hbfile_change_owner(gchar
*owner
)
456 g_free(GLOBALS
->owner
);
457 GLOBALS
->owner
= (owner
!= NULL
) ? owner
: NULL
;
461 void hbfile_change_filepath(gchar
*filepath
)
463 g_free(GLOBALS
->xhb_filepath
);
464 GLOBALS
->xhb_filepath
= (filepath
!= NULL
) ? filepath
: NULL
;
468 void hbfile_cleanup(gboolean file_clear
)
472 DB( g_print("\n[hbfile] cleanup\n") );
473 DB( g_print("- file clear is %d\n", file_clear
) );
476 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
479 da_transaction_free (txn
);
480 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
483 da_transaction_destroy();
484 da_archive_destroy(GLOBALS
->arc_list
);
485 g_hash_table_destroy(GLOBALS
->h_memo
);
493 hbfile_change_owner(NULL
);
496 hbfile_change_filepath(NULL
);
501 void hbfile_setup(gboolean file_clear
)
504 DB( g_print("\n[hbfile] setup\n") );
505 DB( g_print("- file clear is %d\n", file_clear
) );
507 // Allocate data storage
515 GLOBALS
->h_memo
= g_hash_table_new_full(g_str_hash
, g_str_equal
, (GDestroyNotify
)g_free
, NULL
);
516 GLOBALS
->arc_list
= NULL
;
517 GLOBALS
->txn_stk
= NULL
;
519 if(file_clear
== TRUE
)
521 //todo: maybe translate this also
522 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "untitled.xhb", NULL
));
523 GLOBALS
->hbfile_is_new
= TRUE
;
525 DB( g_print("- path_hbfile is '%s'\n", PREFS
->path_hbfile
) );
526 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS
->xhb_filepath
) );
530 GLOBALS
->hbfile_is_new
= FALSE
;
533 hbfile_change_owner(g_strdup(_("Unknown")));
537 GLOBALS
->vehicle_category
= 0;
539 GLOBALS
->auto_smode
= 1;
540 GLOBALS
->auto_nbdays
= 0;
541 GLOBALS
->auto_weekday
= 1;
543 GLOBALS
->changes_count
= 0;
545 GLOBALS
->xhb_hasbak
= FALSE
;
This page took 0.065969 seconds and 4 git commands to generate.