]> Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
b376d370d6f7e4c91c94b79adaba2192184d9c93
[chaz/homebank] / src / hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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-hbfile.h"
22 #include "hb-archive.h"
23 #include "hb-transaction.h"
24
25 /****************************************************************************/
26 /* Debug macros */
27 /****************************************************************************/
28 #define MYDEBUG 0
29
30 #if MYDEBUG
31 #define DB(x) (x);
32 #else
33 #define DB(x);
34 #endif
35
36 /* our global datas */
37 extern struct HomeBank *GLOBALS;
38 extern struct Preferences *PREFS;
39
40
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
42
43
44 gboolean hbfile_file_hasbackup(gchar *filepath)
45 {
46 gchar *bakfilepath;
47
48 bakfilepath = hb_util_filename_new_with_extension(GLOBALS->xhb_filepath, "xhb~");
49 GLOBALS->xhb_hasbak = g_file_test(bakfilepath, G_FILE_TEST_EXISTS);
50 g_free(bakfilepath);
51 //todo check here if need to return something
52 return GLOBALS->xhb_hasbak;
53 }
54
55
56
57
58
59 /*
60 static gint hbfile_file_load_xhb(gchar *filepath)
61 {
62
63
64
65
66
67
68 }
69
70
71 static void hbfile_file_load_backup_xhb(void)
72 {
73 //todo: get from dialog.c, and split between dilaog.c/hbfile.c
74
75
76
77 }
78 */
79
80 void hbfile_replace_basecurrency(Currency4217 *curfmt)
81 {
82 Currency *item;
83 guint32 oldkcur;
84
85 DB( g_print("\n[hbfile] replace base currency \n") );
86
87 oldkcur = GLOBALS->kcur;
88 da_cur_remove(oldkcur);
89 item = currency_add_from_user(curfmt);
90 GLOBALS->kcur = item->key;
91
92 DB( g_print(" %d ==> %d %s\n", oldkcur, GLOBALS->kcur, item->iso_code) );
93 }
94
95
96 void hbfile_change_basecurrency(guint32 key)
97 {
98 GList *list;
99 guint32 oldkcur;
100
101 // set every rate to 0
102 list = g_hash_table_get_values(GLOBALS->h_cur);
103 while (list != NULL)
104 {
105 Currency *entry = list->data;
106
107 if(entry->key != GLOBALS->kcur)
108 {
109 entry->rate = 0.0;
110 entry->mdate = 0;
111 }
112
113 list = g_list_next(list);
114 }
115 g_list_free(list);
116
117 oldkcur = GLOBALS->kcur;
118 GLOBALS->kcur = key;
119
120 // update account with old base currency
121 list = g_hash_table_get_values(GLOBALS->h_acc);
122 while (list != NULL)
123 {
124 Account *acc = list->data;
125
126 if( acc->kcur == oldkcur )
127 acc->kcur = key;
128
129 list = g_list_next(list);
130 }
131 g_list_free(list);
132
133
134 GLOBALS->changes_count++;
135 }
136
137
138 GList *hbfile_transaction_get_all(void)
139 {
140 GList *lst_acc, *lnk_acc;
141 GList *lnk_txn;
142 GList *list;
143
144 list = NULL;
145
146 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
147 lnk_acc = g_list_first(lst_acc);
148 while (lnk_acc != NULL)
149 {
150 Account *acc = lnk_acc->data;
151
152 if( (acc->flags & (AF_CLOSED|AF_NOREPORT)) )
153 goto next_acc;
154
155 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
156 while (lnk_txn != NULL)
157 {
158 list = g_list_append(list, lnk_txn->data);
159 lnk_txn = g_list_next(lnk_txn);
160 }
161
162 next_acc:
163 lnk_acc = g_list_next(lnk_acc);
164 }
165 g_list_free(lst_acc);
166
167 return da_transaction_sort (list);
168 }
169
170
171 static GQueue *hbfile_transaction_get_partial_internal(guint32 minjulian, guint32 maxjulian, gushort exclusionflags)
172 {
173 GList *lst_acc, *lnk_acc;
174 GList *lnk_txn;
175 GQueue *txn_queue;
176
177 txn_queue = g_queue_new ();
178
179 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
180 lnk_acc = g_list_first(lst_acc);
181 while (lnk_acc != NULL)
182 {
183 Account *acc = lnk_acc->data;
184
185 if( (acc->flags & exclusionflags) )
186 goto next_acc;
187
188 lnk_txn = g_queue_peek_tail_link(acc->txn_queue);
189 while (lnk_txn != NULL)
190 {
191 Transaction *txn = lnk_txn->data;
192
193 if( txn->date < minjulian ) //no need to go below mindate
194 break;
195
196 if( !(txn->status == TXN_STATUS_REMIND)
197 && (txn->date >= minjulian)
198 && (txn->date <= maxjulian)
199 )
200 {
201 g_queue_push_head (txn_queue, txn);
202 }
203
204 lnk_txn = g_list_previous(lnk_txn);
205 }
206
207 next_acc:
208 lnk_acc = g_list_next(lnk_acc);
209 }
210 g_list_free(lst_acc);
211
212 return txn_queue;
213 }
214
215
216 GQueue *hbfile_transaction_get_partial(guint32 minjulian, guint32 maxjulian)
217 {
218 return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT));
219 }
220
221
222 GQueue *hbfile_transaction_get_partial_budget(guint32 minjulian, guint32 maxjulian)
223 {
224 return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT|AF_NOBUDGET));
225 }
226
227
228 void hbfile_sanity_check(void)
229 {
230 GList *lst_acc, *lnk_acc;
231 GList *lnk_txn;
232 GList *lxxx, *list;
233
234 DB( g_print("\n[hbfile] !! sanity_check !! \n") );
235
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)
240 {
241 Account *acc = lnk_acc->data;
242
243 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
244 while (lnk_txn != NULL)
245 {
246 Transaction *txn = lnk_txn->data;
247
248 da_transaction_consistency(txn);
249 lnk_txn = g_list_next(lnk_txn);
250 }
251 lnk_acc = g_list_next(lnk_acc);
252 }
253 g_list_free(lst_acc);
254
255
256 DB( g_print(" - scheduled/template\n") );
257 list = g_list_first(GLOBALS->arc_list);
258 while (list != NULL)
259 {
260 Archive *entry = list->data;
261
262 da_archive_consistency(entry);
263 list = g_list_next(list);
264 }
265
266
267 DB( g_print(" - account\n") );
268 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
269 while (list != NULL)
270 {
271 Account *item = list->data;
272
273 da_acc_consistency(item);
274 list = g_list_next(list);
275 }
276 g_list_free(lxxx);
277
278
279 DB( g_print(" - payee\n") );
280 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
281 while (list != NULL)
282 {
283 Payee *item = list->data;
284
285 da_pay_consistency(item);
286 list = g_list_next(list);
287 }
288 g_list_free(lxxx);
289
290
291 DB( g_print(" - category\n") );
292 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
293 while (list != NULL)
294 {
295 Category *item = list->data;
296
297 da_cat_consistency(item);
298 list = g_list_next(list);
299 }
300 g_list_free(lxxx);
301
302 }
303
304
305 void hbfile_anonymize(void)
306 {
307 GList *lst_acc, *lnk_acc;
308 GList *lnk_txn;
309 GList *lxxx, *list;
310 guint cnt, i;
311
312 DB( g_print("\n[hbfile] anonymize\n") );
313
314 // owner
315 hbfile_change_owner(g_strdup("An0nym0us"));
316 GLOBALS->changes_count++;
317 GLOBALS->hbfile_is_new = TRUE;
318
319 // filename
320 hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "anonymized.xhb", NULL));
321
322 // accounts
323 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
324 while (list != NULL)
325 {
326 Account *item = list->data;
327 g_free(item->name);
328 item->name = g_strdup_printf("account %d", item->key);
329 g_free(item->number);
330 item->number = NULL;
331 g_free(item->bankname);
332 item->bankname = NULL;
333
334 GLOBALS->changes_count++;
335 list = g_list_next(list);
336 }
337 g_list_free(lxxx);
338
339 //payees
340 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
341 while (list != NULL)
342 {
343 Payee *item = list->data;
344
345 if(item->key != 0)
346 {
347 g_free(item->name);
348 item->name = g_strdup_printf("payee %d", item->key);
349 GLOBALS->changes_count++;
350 }
351 list = g_list_next(list);
352 }
353 g_list_free(lxxx);
354
355 //categories
356 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
357 while (list != NULL)
358 {
359 Category *item = list->data;
360
361 if(item->key != 0)
362 {
363 g_free(item->name);
364 item->name = g_strdup_printf("category %d", item->key);
365 GLOBALS->changes_count++;
366 }
367 list = g_list_next(list);
368 }
369 g_list_free(lxxx);
370
371 //tags
372 lxxx = list = g_hash_table_get_values(GLOBALS->h_tag);
373 while (list != NULL)
374 {
375 Tag *item = list->data;
376
377 if(item->key != 0)
378 {
379 g_free(item->name);
380 item->name = g_strdup_printf("tag %d", item->key);
381 GLOBALS->changes_count++;
382 }
383 list = g_list_next(list);
384 }
385 g_list_free(lxxx);
386
387 //assigns
388 lxxx = list = g_hash_table_get_values(GLOBALS->h_rul);
389 while (list != NULL)
390 {
391 Assign *item = list->data;
392
393 if(item->key != 0)
394 {
395 g_free(item->text);
396 item->text = g_strdup_printf("assign %d", item->key);
397 GLOBALS->changes_count++;
398 }
399 list = g_list_next(list);
400 }
401 g_list_free(lxxx);
402
403 //archives
404 cnt = 0;
405 list = g_list_first(GLOBALS->arc_list);
406 while (list != NULL)
407 {
408 Archive *item = list->data;
409
410 g_free(item->wording);
411 item->wording = g_strdup_printf("archive %d", cnt++);
412 GLOBALS->changes_count++;
413
414 //later split anonymize also
415
416 list = g_list_next(list);
417 }
418
419 //transaction
420 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
421 lnk_acc = g_list_first(lst_acc);
422 while (lnk_acc != NULL)
423 {
424 Account *acc = lnk_acc->data;
425
426 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
427 while (lnk_txn != NULL)
428 {
429 Transaction *item = lnk_txn->data;
430 Split *split;
431
432 g_free(item->info);
433 item->info = NULL;
434 g_free(item->wording);
435 item->wording = g_strdup_printf("memo %d", item->date);
436 GLOBALS->changes_count++;
437
438 if(item->flags & OF_SPLIT)
439 {
440 for(i=0;i<TXN_MAX_SPLIT;i++)
441 {
442 split = item->splits[i];
443 if( split == NULL ) break;
444
445 if(split->memo != NULL)
446 g_free(split->memo);
447
448 split->memo = g_strdup_printf("memo %d", i);
449 GLOBALS->changes_count++;
450 }
451 }
452 lnk_txn = g_list_next(lnk_txn);
453 }
454 lnk_acc = g_list_next(lnk_acc);
455 }
456 g_list_free(lst_acc);
457
458 }
459
460
461 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
462
463
464 void hbfile_change_owner(gchar *owner)
465 {
466 g_free(GLOBALS->owner);
467 GLOBALS->owner = (owner != NULL) ? owner : NULL;
468 }
469
470
471 void hbfile_change_filepath(gchar *filepath)
472 {
473 g_free(GLOBALS->xhb_filepath);
474 GLOBALS->xhb_filepath = (filepath != NULL) ? filepath : NULL;
475 }
476
477
478 void hbfile_cleanup(gboolean file_clear)
479 {
480 Transaction *txn;
481
482 DB( g_print("\n[hbfile] cleanup\n") );
483 DB( g_print("- file clear is %d\n", file_clear) );
484
485 // Free data storage
486 txn = g_trash_stack_pop(&GLOBALS->txn_stk);
487 while( txn != NULL )
488 {
489 da_transaction_free (txn);
490 txn = g_trash_stack_pop(&GLOBALS->txn_stk);
491 }
492
493 da_transaction_destroy();
494 da_archive_destroy(GLOBALS->arc_list);
495 g_hash_table_destroy(GLOBALS->h_memo);
496 da_asg_destroy();
497 da_tag_destroy();
498 da_cat_destroy();
499 da_pay_destroy();
500 da_acc_destroy();
501 da_cur_destroy();
502
503 hbfile_change_owner(NULL);
504
505 if(file_clear)
506 hbfile_change_filepath(NULL);
507
508 }
509
510
511 void hbfile_setup(gboolean file_clear)
512 {
513
514 DB( g_print("\n[hbfile] setup\n") );
515 DB( g_print("- file clear is %d\n", file_clear) );
516
517 // Allocate data storage
518 da_cur_new();
519 da_acc_new();
520 da_pay_new();
521 da_cat_new();
522 da_tag_new();
523 da_asg_new();
524
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;
528
529 if(file_clear == TRUE)
530 {
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;
534
535 DB( g_print("- path_hbfile is '%s'\n", PREFS->path_hbfile) );
536 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS->xhb_filepath) );
537 }
538 else
539 {
540 GLOBALS->hbfile_is_new = FALSE;
541 }
542
543 hbfile_change_owner(g_strdup(_("Unknown")));
544
545 GLOBALS->kcur = 1;
546
547 GLOBALS->vehicle_category = 0;
548
549 GLOBALS->auto_smode = 1;
550 GLOBALS->auto_nbdays = 0;
551 GLOBALS->auto_weekday = 1;
552
553 GLOBALS->changes_count = 0;
554
555 GLOBALS->xhb_hasbak = FALSE;
556
557 }
558
This page took 0.054478 seconds and 4 git commands to generate.