]> Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
import homebank-5.1.2
[chaz/homebank] / src / hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 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(guint32 kacc)
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 if( (kacc > 0 ) && (acc->key != kacc) )
155 goto next_acc;
156
157 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
158 while (lnk_txn != NULL)
159 {
160 list = g_list_append(list, lnk_txn->data);
161 lnk_txn = g_list_next(lnk_txn);
162 }
163
164 next_acc:
165 lnk_acc = g_list_next(lnk_acc);
166 }
167 g_list_free(lst_acc);
168
169 return da_transaction_sort (list);
170 }
171
172
173 GQueue *hbfile_transaction_get_partial(guint32 minjulian, guint32 maxjulian)
174 {
175 GList *lst_acc, *lnk_acc;
176 GList *lnk_txn;
177 GQueue *txn_queue;
178
179 txn_queue = g_queue_new ();
180
181 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
182 lnk_acc = g_list_first(lst_acc);
183 while (lnk_acc != NULL)
184 {
185 Account *acc = lnk_acc->data;
186
187 if( (acc->flags & (AF_CLOSED|AF_NOREPORT)) )
188 goto next_acc;
189
190 lnk_txn = g_queue_peek_tail_link(acc->txn_queue);
191 while (lnk_txn != NULL)
192 {
193 Transaction *txn = lnk_txn->data;
194
195 if( txn->date < minjulian ) //no need to go below mindate
196 break;
197
198 if( !(txn->status == TXN_STATUS_REMIND)
199 && (txn->date >= minjulian)
200 && (txn->date <= maxjulian)
201 )
202 {
203 g_queue_push_head (txn_queue, txn);
204 }
205
206 lnk_txn = g_list_previous(lnk_txn);
207 }
208
209 next_acc:
210 lnk_acc = g_list_next(lnk_acc);
211 }
212 g_list_free(lst_acc);
213
214 return txn_queue;
215 }
216
217
218 void hbfile_sanity_check(void)
219 {
220 GList *lst_acc, *lnk_acc;
221 GList *lnk_txn;
222 GList *lxxx, *list;
223
224 DB( g_print("\n[hbfile] !! sanity_check !! \n") );
225
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)
230 {
231 Account *acc = lnk_acc->data;
232
233 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
234 while (lnk_txn != NULL)
235 {
236 Transaction *txn = lnk_txn->data;
237
238 da_transaction_consistency(txn);
239 lnk_txn = g_list_next(lnk_txn);
240 }
241 lnk_acc = g_list_next(lnk_acc);
242 }
243 g_list_free(lst_acc);
244
245
246 DB( g_print(" - scheduled/template\n") );
247 list = g_list_first(GLOBALS->arc_list);
248 while (list != NULL)
249 {
250 Archive *entry = list->data;
251
252 da_archive_consistency(entry);
253 list = g_list_next(list);
254 }
255
256
257 DB( g_print(" - account\n") );
258 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
259 while (list != NULL)
260 {
261 Account *item = list->data;
262
263 da_acc_consistency(item);
264 list = g_list_next(list);
265 }
266 g_list_free(lxxx);
267
268
269 DB( g_print(" - payee\n") );
270 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
271 while (list != NULL)
272 {
273 Payee *item = list->data;
274
275 da_pay_consistency(item);
276 list = g_list_next(list);
277 }
278 g_list_free(lxxx);
279
280
281 DB( g_print(" - category\n") );
282 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
283 while (list != NULL)
284 {
285 Category *item = list->data;
286
287 da_cat_consistency(item);
288 list = g_list_next(list);
289 }
290 g_list_free(lxxx);
291
292 }
293
294
295 void hbfile_anonymize(void)
296 {
297 GList *lst_acc, *lnk_acc;
298 GList *lnk_txn;
299 GList *lxxx, *list;
300 guint cnt, i;
301
302 DB( g_print("\n[hbfile] anonymize\n") );
303
304 // owner
305 hbfile_change_owner(g_strdup("An0nym0us"));
306 GLOBALS->changes_count++;
307 GLOBALS->hbfile_is_new = TRUE;
308
309 // filename
310 hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "anonymized.xhb", NULL));
311
312 // accounts
313 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
314 while (list != NULL)
315 {
316 Account *item = list->data;
317 g_free(item->name);
318 item->name = g_strdup_printf("account %d", item->key);
319 g_free(item->number);
320 item->number = NULL;
321 g_free(item->bankname);
322 item->bankname = NULL;
323
324 GLOBALS->changes_count++;
325 list = g_list_next(list);
326 }
327 g_list_free(lxxx);
328
329 //payees
330 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
331 while (list != NULL)
332 {
333 Payee *item = list->data;
334
335 if(item->key != 0)
336 {
337 g_free(item->name);
338 item->name = g_strdup_printf("payee %d", item->key);
339 GLOBALS->changes_count++;
340 }
341 list = g_list_next(list);
342 }
343 g_list_free(lxxx);
344
345 //categories
346 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
347 while (list != NULL)
348 {
349 Category *item = list->data;
350
351 if(item->key != 0)
352 {
353 g_free(item->name);
354 item->name = g_strdup_printf("category %d", item->key);
355 GLOBALS->changes_count++;
356 }
357 list = g_list_next(list);
358 }
359 g_list_free(lxxx);
360
361 //tags
362 lxxx = list = g_hash_table_get_values(GLOBALS->h_tag);
363 while (list != NULL)
364 {
365 Tag *item = list->data;
366
367 if(item->key != 0)
368 {
369 g_free(item->name);
370 item->name = g_strdup_printf("tag %d", item->key);
371 GLOBALS->changes_count++;
372 }
373 list = g_list_next(list);
374 }
375 g_list_free(lxxx);
376
377 //assigns
378 lxxx = list = g_hash_table_get_values(GLOBALS->h_rul);
379 while (list != NULL)
380 {
381 Assign *item = list->data;
382
383 if(item->key != 0)
384 {
385 g_free(item->text);
386 item->text = g_strdup_printf("assign %d", item->key);
387 GLOBALS->changes_count++;
388 }
389 list = g_list_next(list);
390 }
391 g_list_free(lxxx);
392
393 //archives
394 cnt = 0;
395 list = g_list_first(GLOBALS->arc_list);
396 while (list != NULL)
397 {
398 Archive *item = list->data;
399
400 g_free(item->wording);
401 item->wording = g_strdup_printf("archive %d", cnt++);
402 GLOBALS->changes_count++;
403
404 //later split anonymize also
405
406 list = g_list_next(list);
407 }
408
409 //transaction
410 lst_acc = g_hash_table_get_values(GLOBALS->h_acc);
411 lnk_acc = g_list_first(lst_acc);
412 while (lnk_acc != NULL)
413 {
414 Account *acc = lnk_acc->data;
415
416 lnk_txn = g_queue_peek_head_link(acc->txn_queue);
417 while (lnk_txn != NULL)
418 {
419 Transaction *item = lnk_txn->data;
420 Split *split;
421
422 g_free(item->info);
423 item->info = NULL;
424 g_free(item->wording);
425 item->wording = g_strdup_printf("memo %d", item->date);
426 GLOBALS->changes_count++;
427
428 if(item->flags & OF_SPLIT)
429 {
430 for(i=0;i<TXN_MAX_SPLIT;i++)
431 {
432 split = item->splits[i];
433 if( split == NULL ) break;
434
435 if(split->memo != NULL)
436 g_free(split->memo);
437
438 split->memo = g_strdup_printf("memo %d", i);
439 GLOBALS->changes_count++;
440 }
441 }
442 lnk_txn = g_list_next(lnk_txn);
443 }
444 lnk_acc = g_list_next(lnk_acc);
445 }
446 g_list_free(lst_acc);
447
448 }
449
450
451 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
452
453
454 void hbfile_change_owner(gchar *owner)
455 {
456 g_free(GLOBALS->owner);
457 GLOBALS->owner = (owner != NULL) ? owner : NULL;
458 }
459
460
461 void hbfile_change_filepath(gchar *filepath)
462 {
463 g_free(GLOBALS->xhb_filepath);
464 GLOBALS->xhb_filepath = (filepath != NULL) ? filepath : NULL;
465 }
466
467
468 void hbfile_cleanup(gboolean file_clear)
469 {
470 Transaction *txn;
471
472 DB( g_print("\n[hbfile] cleanup\n") );
473 DB( g_print("- file clear is %d\n", file_clear) );
474
475 // Free data storage
476 txn = g_trash_stack_pop(&GLOBALS->txn_stk);
477 while( txn != NULL )
478 {
479 da_transaction_free (txn);
480 txn = g_trash_stack_pop(&GLOBALS->txn_stk);
481 }
482
483 da_transaction_destroy();
484 da_archive_destroy(GLOBALS->arc_list);
485 g_hash_table_destroy(GLOBALS->h_memo);
486 da_asg_destroy();
487 da_tag_destroy();
488 da_cat_destroy();
489 da_pay_destroy();
490 da_acc_destroy();
491 da_cur_destroy();
492
493 hbfile_change_owner(NULL);
494
495 if(file_clear)
496 hbfile_change_filepath(NULL);
497
498 }
499
500
501 void hbfile_setup(gboolean file_clear)
502 {
503
504 DB( g_print("\n[hbfile] setup\n") );
505 DB( g_print("- file clear is %d\n", file_clear) );
506
507 // Allocate data storage
508 da_cur_new();
509 da_acc_new();
510 da_pay_new();
511 da_cat_new();
512 da_tag_new();
513 da_asg_new();
514
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;
518
519 if(file_clear == TRUE)
520 {
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;
524
525 DB( g_print("- path_hbfile is '%s'\n", PREFS->path_hbfile) );
526 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS->xhb_filepath) );
527 }
528 else
529 {
530 GLOBALS->hbfile_is_new = FALSE;
531 }
532
533 hbfile_change_owner(g_strdup(_("Unknown")));
534
535 GLOBALS->kcur = 1;
536
537 GLOBALS->vehicle_category = 0;
538
539 GLOBALS->auto_smode = 1;
540 GLOBALS->auto_nbdays = 0;
541 GLOBALS->auto_weekday = 1;
542
543 GLOBALS->changes_count = 0;
544
545 GLOBALS->xhb_hasbak = FALSE;
546
547 }
548
This page took 0.053415 seconds and 4 git commands to generate.