]>
Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2018 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"
26 /****************************************************************************/
28 /****************************************************************************/
37 /* our global datas */
38 extern struct HomeBank
*GLOBALS
;
39 extern struct Preferences
*PREFS
;
42 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
45 gboolean
hbfile_file_isbackup(gchar
*filepath
)
47 return g_str_has_suffix(filepath
, "xhb~");
54 gboolean
hbfile_file_hasbackup(gchar
*filepath
)
58 bakfilepath
= hb_filename_new_with_extension(GLOBALS
->xhb_filepath
, "xhb~");
59 GLOBALS
->xhb_hasbak
= g_file_test(bakfilepath
, G_FILE_TEST_EXISTS
);
61 //todo check here if need to return something
62 return GLOBALS
->xhb_hasbak
;
66 void hbfile_file_default(void)
68 //todo: maybe translate this also
69 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "untitled.xhb", NULL
));
70 GLOBALS
->hbfile_is_new
= TRUE
;
71 GLOBALS
->hbfile_is_bak
= FALSE
;
73 DB( g_print("- path_hbfile is '%s'\n", PREFS
->path_hbfile
) );
74 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS
->xhb_filepath
) );
80 static gint hbfile_file_load_xhb(gchar *filepath)
91 static void hbfile_file_load_backup_xhb(void)
93 //todo: get from dialog.c, and split between dilaog.c/hbfile.c
100 void hbfile_replace_basecurrency(Currency4217
*curfmt
)
105 DB( g_print("\n[hbfile] replace base currency \n") );
107 oldkcur
= GLOBALS
->kcur
;
108 da_cur_remove(oldkcur
);
109 item
= currency_add_from_user(curfmt
);
110 GLOBALS
->kcur
= item
->key
;
112 DB( g_print(" %d ==> %d %s\n", oldkcur
, GLOBALS
->kcur
, item
->iso_code
) );
116 void hbfile_change_basecurrency(guint32 key
)
121 // set every rate to 0
122 list
= g_hash_table_get_values(GLOBALS
->h_cur
);
125 Currency
*entry
= list
->data
;
127 if(entry
->key
!= GLOBALS
->kcur
)
133 list
= g_list_next(list
);
137 oldkcur
= GLOBALS
->kcur
;
140 // update account with old base currency
141 list
= g_hash_table_get_values(GLOBALS
->h_acc
);
144 Account
*acc
= list
->data
;
146 if( acc
->kcur
== oldkcur
)
149 list
= g_list_next(list
);
154 GLOBALS
->changes_count
++;
158 GList
*hbfile_transaction_get_all(void)
160 GList
*lst_acc
, *lnk_acc
;
166 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
167 lnk_acc
= g_list_first(lst_acc
);
168 while (lnk_acc
!= NULL
)
170 Account
*acc
= lnk_acc
->data
;
172 //#1674045 ony rely on nosummary
173 //if( (acc->flags & (AF_CLOSED|AF_NOREPORT)) )
174 if( (acc
->flags
& (AF_NOREPORT
)) )
177 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
178 while (lnk_txn
!= NULL
)
180 list
= g_list_append(list
, lnk_txn
->data
);
181 lnk_txn
= g_list_next(lnk_txn
);
185 lnk_acc
= g_list_next(lnk_acc
);
187 g_list_free(lst_acc
);
189 return da_transaction_sort (list
);
193 static GQueue
*hbfile_transaction_get_partial_internal(guint32 minjulian
, guint32 maxjulian
, gushort exclusionflags
)
195 GList
*lst_acc
, *lnk_acc
;
199 txn_queue
= g_queue_new ();
201 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
202 lnk_acc
= g_list_first(lst_acc
);
203 while (lnk_acc
!= NULL
)
205 Account
*acc
= lnk_acc
->data
;
207 if( (acc
->flags
& exclusionflags
) )
210 lnk_txn
= g_queue_peek_tail_link(acc
->txn_queue
);
211 while (lnk_txn
!= NULL
)
213 Transaction
*txn
= lnk_txn
->data
;
215 if( txn
->date
< minjulian
) //no need to go below mindate
218 if( !(txn
->status
== TXN_STATUS_REMIND
)
219 && (txn
->date
>= minjulian
)
220 && (txn
->date
<= maxjulian
)
223 g_queue_push_head (txn_queue
, txn
);
226 lnk_txn
= g_list_previous(lnk_txn
);
230 lnk_acc
= g_list_next(lnk_acc
);
232 g_list_free(lst_acc
);
238 GQueue
*hbfile_transaction_get_partial(guint32 minjulian
, guint32 maxjulian
)
240 //#1674045 ony rely on nosummary
241 //return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT));
242 return hbfile_transaction_get_partial_internal(minjulian
, maxjulian
, (AF_NOREPORT
));
246 GQueue
*hbfile_transaction_get_partial_budget(guint32 minjulian
, guint32 maxjulian
)
248 //#1674045 ony rely on nosummary
249 //return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT|AF_NOBUDGET));
250 return hbfile_transaction_get_partial_internal(minjulian
, maxjulian
, (AF_NOREPORT
|AF_NOBUDGET
));
254 void hbfile_sanity_check(void)
256 GList
*lst_acc
, *lnk_acc
;
260 DB( g_print("\n[hbfile] !! sanity_check !! \n") );
262 DB( g_print(" - transaction\n") );
263 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
264 lnk_acc
= g_list_first(lst_acc
);
265 while (lnk_acc
!= NULL
)
267 Account
*acc
= lnk_acc
->data
;
269 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
270 while (lnk_txn
!= NULL
)
272 Transaction
*txn
= lnk_txn
->data
;
274 da_transaction_consistency(txn
);
275 lnk_txn
= g_list_next(lnk_txn
);
277 lnk_acc
= g_list_next(lnk_acc
);
279 g_list_free(lst_acc
);
282 DB( g_print(" - scheduled/template\n") );
283 list
= g_list_first(GLOBALS
->arc_list
);
286 Archive
*entry
= list
->data
;
288 da_archive_consistency(entry
);
289 list
= g_list_next(list
);
293 DB( g_print(" - account\n") );
294 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
297 Account
*item
= list
->data
;
299 da_acc_consistency(item
);
300 list
= g_list_next(list
);
305 DB( g_print(" - payee\n") );
306 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
309 Payee
*item
= list
->data
;
311 da_pay_consistency(item
);
312 list
= g_list_next(list
);
317 DB( g_print(" - category\n") );
318 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
321 Category
*item
= list
->data
;
323 da_cat_consistency(item
);
324 list
= g_list_next(list
);
331 void hbfile_anonymize(void)
333 GList
*lst_acc
, *lnk_acc
;
338 DB( g_print("\n[hbfile] anonymize\n") );
341 hbfile_change_owner(g_strdup("An0nym0us"));
342 GLOBALS
->changes_count
++;
343 GLOBALS
->hbfile_is_new
= TRUE
;
346 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "anonymized.xhb", NULL
));
349 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
352 Account
*item
= list
->data
;
354 item
->name
= g_strdup_printf("account %d", item
->key
);
355 g_free(item
->number
);
357 g_free(item
->bankname
);
358 item
->bankname
= NULL
;
360 GLOBALS
->changes_count
++;
361 list
= g_list_next(list
);
366 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
369 Payee
*item
= list
->data
;
374 item
->name
= g_strdup_printf("payee %d", item
->key
);
375 GLOBALS
->changes_count
++;
377 list
= g_list_next(list
);
382 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
385 Category
*item
= list
->data
;
390 item
->name
= g_strdup_printf("category %d", item
->key
);
391 GLOBALS
->changes_count
++;
393 list
= g_list_next(list
);
398 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_tag
);
401 Tag
*item
= list
->data
;
406 item
->name
= g_strdup_printf("tag %d", item
->key
);
407 GLOBALS
->changes_count
++;
409 list
= g_list_next(list
);
414 lxxx
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
417 Assign
*item
= list
->data
;
422 item
->text
= g_strdup_printf("assign %d", item
->key
);
423 GLOBALS
->changes_count
++;
425 list
= g_list_next(list
);
431 list
= g_list_first(GLOBALS
->arc_list
);
434 Archive
*item
= list
->data
;
437 item
->memo
= g_strdup_printf("archive %d", cnt
++);
438 GLOBALS
->changes_count
++;
440 //later split anonymize also
442 list
= g_list_next(list
);
446 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
447 lnk_acc
= g_list_first(lst_acc
);
448 while (lnk_acc
!= NULL
)
450 Account
*acc
= lnk_acc
->data
;
452 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
453 while (lnk_txn
!= NULL
)
455 Transaction
*item
= lnk_txn
->data
;
461 item
->memo
= g_strdup_printf("memo %d", item
->date
);
462 GLOBALS
->changes_count
++;
464 if(item
->flags
& OF_SPLIT
)
466 for(i
=0;i
<TXN_MAX_SPLIT
;i
++)
468 split
= item
->splits
[i
];
469 if( split
== NULL
) break;
471 if(split
->memo
!= NULL
)
474 split
->memo
= g_strdup_printf("memo %d", i
);
475 GLOBALS
->changes_count
++;
478 lnk_txn
= g_list_next(lnk_txn
);
480 lnk_acc
= g_list_next(lnk_acc
);
482 g_list_free(lst_acc
);
487 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
490 void hbfile_change_owner(gchar
*owner
)
492 g_free(GLOBALS
->owner
);
493 GLOBALS
->owner
= (owner
!= NULL
) ? owner
: NULL
;
497 void hbfile_change_filepath(gchar
*filepath
)
499 g_free(GLOBALS
->xhb_filepath
);
500 GLOBALS
->xhb_filepath
= (filepath
!= NULL
) ? filepath
: NULL
;
504 void hbfile_cleanup(gboolean file_clear
)
508 DB( g_print("\n[hbfile] cleanup\n") );
509 DB( g_print("- file clear is %d\n", file_clear
) );
512 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
515 da_transaction_free (txn
);
516 txn
= g_trash_stack_pop(&GLOBALS
->txn_stk
);
519 da_transaction_destroy();
520 da_archive_destroy(GLOBALS
->arc_list
);
521 g_hash_table_destroy(GLOBALS
->h_memo
);
529 hbfile_change_owner(NULL
);
532 hbfile_change_filepath(NULL
);
537 void hbfile_setup(gboolean file_clear
)
540 DB( g_print("\n[hbfile] setup\n") );
541 DB( g_print("- file clear is %d\n", file_clear
) );
543 // Allocate data storage
551 GLOBALS
->h_memo
= g_hash_table_new_full(g_str_hash
, g_str_equal
, (GDestroyNotify
)g_free
, NULL
);
552 GLOBALS
->arc_list
= NULL
;
553 GLOBALS
->txn_stk
= NULL
;
555 if(file_clear
== TRUE
)
557 //todo: maybe translate this also
558 hbfile_change_filepath(g_build_filename(PREFS
->path_hbfile
, "untitled.xhb", NULL
));
559 GLOBALS
->hbfile_is_new
= TRUE
;
561 DB( g_print("- path_hbfile is '%s'\n", PREFS
->path_hbfile
) );
562 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS
->xhb_filepath
) );
566 GLOBALS
->hbfile_is_new
= FALSE
;
569 hbfile_change_owner(g_strdup(_("Unknown")));
573 GLOBALS
->vehicle_category
= 0;
575 GLOBALS
->auto_smode
= 1;
576 GLOBALS
->auto_nbdays
= 0;
577 GLOBALS
->auto_weekday
= 1;
579 GLOBALS
->changes_count
= 0;
581 GLOBALS
->xhb_hasbak
= FALSE
;
This page took 0.066953 seconds and 4 git commands to generate.