]>
Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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(void)
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
)) )
155 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
156 while (lnk_txn
!= NULL
)
158 list
= g_list_append(list
, lnk_txn
->data
);
159 lnk_txn
= g_list_next(lnk_txn
);
163 lnk_acc
= g_list_next(lnk_acc
);
165 g_list_free(lst_acc
);
167 return da_transaction_sort (list
);
171 static GQueue
*hbfile_transaction_get_partial_internal(guint32 minjulian
, guint32 maxjulian
, gushort exclusionflags
)
173 GList
*lst_acc
, *lnk_acc
;
177 txn_queue
= g_queue_new ();
179 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
180 lnk_acc
= g_list_first(lst_acc
);
181 while (lnk_acc
!= NULL
)
183 Account
*acc
= lnk_acc
->data
;
185 if( (acc
->flags
& exclusionflags
) )
188 lnk_txn
= g_queue_peek_tail_link(acc
->txn_queue
);
189 while (lnk_txn
!= NULL
)
191 Transaction
*txn
= lnk_txn
->data
;
193 if( txn
->date
< minjulian
) //no need to go below mindate
196 if( !(txn
->status
== TXN_STATUS_REMIND
)
197 && (txn
->date
>= minjulian
)
198 && (txn
->date
<= maxjulian
)
201 g_queue_push_head (txn_queue
, txn
);
204 lnk_txn
= g_list_previous(lnk_txn
);
208 lnk_acc
= g_list_next(lnk_acc
);
210 g_list_free(lst_acc
);
216 GQueue
*hbfile_transaction_get_partial(guint32 minjulian
, guint32 maxjulian
)
218 return hbfile_transaction_get_partial_internal(minjulian
, maxjulian
, (AF_CLOSED
|AF_NOREPORT
));
222 GQueue
*hbfile_transaction_get_partial_budget(guint32 minjulian
, guint32 maxjulian
)
224 return hbfile_transaction_get_partial_internal(minjulian
, maxjulian
, (AF_CLOSED
|AF_NOREPORT
|AF_NOBUDGET
));
228 void hbfile_sanity_check(void)
230 GList
*lst_acc
, *lnk_acc
;
234 DB( g_print("\n[hbfile] !! sanity_check !! \n") );
236 DB( g_print(" - transaction\n") );
237 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
238 lnk_acc
= g_list_first(lst_acc
);
239 while (lnk_acc
!= NULL
)
241 Account
*acc
= lnk_acc
->data
;
243 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
244 while (lnk_txn
!= NULL
)
246 Transaction
*txn
= lnk_txn
->data
;
248 da_transaction_consistency(txn
);
249 lnk_txn
= g_list_next(lnk_txn
);
251 lnk_acc
= g_list_next(lnk_acc
);
253 g_list_free(lst_acc
);
256 DB( g_print(" - scheduled/template\n") );
257 list
= g_list_first(GLOBALS
->arc_list
);
260 Archive
*entry
= list
->data
;
262 da_archive_consistency(entry
);
263 list
= g_list_next(list
);
267 DB( g_print(" - account\n") );
268 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
271 Account
*item
= list
->data
;
273 da_acc_consistency(item
);
274 list
= g_list_next(list
);
279 DB( g_print(" - payee\n") );
280 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
283 Payee
*item
= list
->data
;
285 da_pay_consistency(item
);
286 list
= g_list_next(list
);
291 DB( g_print(" - category\n") );
292 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
295 Category
*item
= list
->data
;
297 da_cat_consistency(item
);
298 list
= g_list_next(list
);
305 void hbfile_anonymize(void)
307 GList
*lst_acc
, *lnk_acc
;
312 DB( g_print("\n[hbfile] anonymize\n") );
315 hbfile_change_owner(g_strdup("An0nym0us"));
316 GLOBALS
->changes_count
++;
317 GLOBALS
->hbfile_is_new
= TRUE
;
320 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "anonymized.xhb", NULL
));
323 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
326 Account
*item
= list
->data
;
328 item
->name
= g_strdup_printf("account %d", item
->key
);
329 g_free(item
->number
);
331 g_free(item
->bankname
);
332 item
->bankname
= NULL
;
334 GLOBALS
->changes_count
++;
335 list
= g_list_next(list
);
340 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
343 Payee
*item
= list
->data
;
348 item
->name
= g_strdup_printf("payee %d", item
->key
);
349 GLOBALS
->changes_count
++;
351 list
= g_list_next(list
);
356 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
359 Category
*item
= list
->data
;
364 item
->name
= g_strdup_printf("category %d", item
->key
);
365 GLOBALS
->changes_count
++;
367 list
= g_list_next(list
);
372 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_tag
);
375 Tag
*item
= list
->data
;
380 item
->name
= g_strdup_printf("tag %d", item
->key
);
381 GLOBALS
->changes_count
++;
383 list
= g_list_next(list
);
388 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
391 Assign
*item
= list
->data
;
396 item
->text
= g_strdup_printf("assign %d", item
->key
);
397 GLOBALS
->changes_count
++;
399 list
= g_list_next(list
);
405 list
= g_list_first(GLOBALS
->arc_list
);
408 Archive
*item
= list
->data
;
410 g_free(item
->wording
);
411 item
->wording
= g_strdup_printf("archive %d", cnt
++);
412 GLOBALS
->changes_count
++;
414 //later split anonymize also
416 list
= g_list_next(list
);
420 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
421 lnk_acc
= g_list_first(lst_acc
);
422 while (lnk_acc
!= NULL
)
424 Account
*acc
= lnk_acc
->data
;
426 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
427 while (lnk_txn
!= NULL
)
429 Transaction
*item
= lnk_txn
->data
;
434 g_free(item
->wording
);
435 item
->wording
= g_strdup_printf("memo %d", item
->date
);
436 GLOBALS
->changes_count
++;
438 if(item
->flags
& OF_SPLIT
)
440 for(i
=0;i
<TXN_MAX_SPLIT
;i
++)
442 split
= item
->splits
[i
];
443 if( split
== NULL
) break;
445 if(split
->memo
!= NULL
)
448 split
->memo
= g_strdup_printf("memo %d", i
);
449 GLOBALS
->changes_count
++;
452 lnk_txn
= g_list_next(lnk_txn
);
454 lnk_acc
= g_list_next(lnk_acc
);
456 g_list_free(lst_acc
);
461 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
464 void hbfile_change_owner(gchar
*owner
)
466 g_free(GLOBALS
->owner
);
467 GLOBALS
->owner
= (owner
!= NULL
) ? owner
: NULL
;
471 void hbfile_change_filepath(gchar
*filepath
)
473 g_free(GLOBALS
->xhb_filepath
);
474 GLOBALS
->xhb_filepath
= (filepath
!= NULL
) ? filepath
: NULL
;
478 void hbfile_cleanup(gboolean file_clear
)
482 DB( g_print("\n[hbfile] cleanup\n") );
483 DB( g_print("- file clear is %d\n", file_clear
) );
486 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
489 da_transaction_free (txn
);
490 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
493 da_transaction_destroy();
494 da_archive_destroy(GLOBALS
->arc_list
);
495 g_hash_table_destroy(GLOBALS
->h_memo
);
503 hbfile_change_owner(NULL
);
506 hbfile_change_filepath(NULL
);
511 void hbfile_setup(gboolean file_clear
)
514 DB( g_print("\n[hbfile] setup\n") );
515 DB( g_print("- file clear is %d\n", file_clear
) );
517 // Allocate data storage
525 GLOBALS
->h_memo
= g_hash_table_new_full(g_str_hash
, g_str_equal
, (GDestroyNotify
)g_free
, NULL
);
526 GLOBALS
->arc_list
= NULL
;
527 GLOBALS
->txn_stk
= NULL
;
529 if(file_clear
== TRUE
)
531 //todo: maybe translate this also
532 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "untitled.xhb", NULL
));
533 GLOBALS
->hbfile_is_new
= TRUE
;
535 DB( g_print("- path_hbfile is '%s'\n", PREFS
->path_hbfile
) );
536 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS
->xhb_filepath
) );
540 GLOBALS
->hbfile_is_new
= FALSE
;
543 hbfile_change_owner(g_strdup(_("Unknown")));
547 GLOBALS
->vehicle_category
= 0;
549 GLOBALS
->auto_smode
= 1;
550 GLOBALS
->auto_nbdays
= 0;
551 GLOBALS
->auto_weekday
= 1;
553 GLOBALS
->changes_count
= 0;
555 GLOBALS
->xhb_hasbak
= FALSE
;
This page took 0.055279 seconds and 5 git commands to generate.