]> Dogcows Code - chaz/homebank/blob - src/hb-hbfile.c
import homebank-4.6.3
[chaz/homebank] / src / hb-hbfile.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 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 gboolean hbfile_file_hasbackup(gchar *filepath)
43 {
44 gchar *bakfilepath;
45
46 bakfilepath = hb_filename_new_with_extention(GLOBALS->xhb_filepath, "xhb~");
47 GLOBALS->xhb_hasbak = g_file_test(bakfilepath, G_FILE_TEST_EXISTS);
48 g_free(bakfilepath);
49 //todo check here if need to return something
50 return GLOBALS->xhb_hasbak;
51 }
52
53
54
55
56
57 /*
58 static gint hbfile_file_load_xhb(gchar *filepath)
59 {
60
61
62
63
64
65
66 }
67
68
69 static void hbfile_file_load_backup_xhb(void)
70 {
71 //todo: get from dialog.c, and split between dilaog.c/hbfile.c
72
73
74
75
76
77
78 }
79 */
80
81
82
83 /*
84 void hbfile_change_basecurrency(guint32 key)
85 {
86 GList *list;
87
88 list = g_hash_table_get_values(GLOBALS->h_cur);
89 while (list != NULL)
90 {
91 Currency *entry = list->data;
92
93 if(entry->key != GLOBALS->kcur)
94 entry->rate = 0.0;
95
96 list = g_list_next(list);
97 }
98 g_list_free(list);
99
100
101 GLOBALS->changes_count++;
102 GLOBALS->kcur = key;
103
104 }
105 */
106
107
108 void hbfile_change_owner(gchar *owner)
109 {
110 g_free(GLOBALS->owner);
111 GLOBALS->owner = (owner != NULL) ? owner : NULL;
112 }
113
114
115 void hbfile_change_filepath(gchar *filepath)
116 {
117 g_free(GLOBALS->xhb_filepath);
118 GLOBALS->xhb_filepath = (filepath != NULL) ? filepath : NULL;
119 }
120
121
122 void hbfile_sanity_check(void)
123 {
124 GList *lxxx, *list;
125
126 DB( g_print("\n[hbfile] sanity_check\n") );
127
128
129 list = g_list_first(GLOBALS->ope_list);
130 while (list != NULL)
131 {
132 Transaction *entry = list->data;
133
134 da_transaction_consistency(entry);
135 list = g_list_next(list);
136 }
137
138
139 list = g_list_first(GLOBALS->arc_list);
140 while (list != NULL)
141 {
142 Archive *entry = list->data;
143
144 da_archive_consistency(entry);
145 list = g_list_next(list);
146 }
147
148
149 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
150 while (list != NULL)
151 {
152 Account *item = list->data;
153
154 da_acc_consistency(item);
155 list = g_list_next(list);
156 }
157 g_list_free(lxxx);
158
159
160 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
161 while (list != NULL)
162 {
163 Payee *item = list->data;
164
165 da_pay_consistency(item);
166 list = g_list_next(list);
167 }
168 g_list_free(lxxx);
169
170
171 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
172 while (list != NULL)
173 {
174 Category *item = list->data;
175
176 da_cat_consistency(item);
177 list = g_list_next(list);
178 }
179 g_list_free(lxxx);
180
181 }
182
183
184 void hbfile_anonymize(void)
185 {
186 GList *lxxx, *list;
187 guint cnt, i;
188
189 DB( g_print("\n[hbfile] anonymize\n") );
190
191 // owner
192 hbfile_change_owner(g_strdup("An0nym0us"));
193 GLOBALS->changes_count++;
194 GLOBALS->hbfile_is_new = TRUE;
195
196 // filename
197 hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "anonymized.xhb", NULL));
198
199 // accounts
200 lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
201 while (list != NULL)
202 {
203 Account *item = list->data;
204 g_free(item->name);
205 item->name = g_strdup_printf("account %d", item->key);
206 g_free(item->number);
207 item->number = NULL;
208 g_free(item->bankname);
209 item->bankname = NULL;
210
211 GLOBALS->changes_count++;
212 list = g_list_next(list);
213 }
214 g_list_free(lxxx);
215
216 //payees
217 lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
218 while (list != NULL)
219 {
220 Payee *item = list->data;
221
222 if(item->key != 0)
223 {
224 g_free(item->name);
225 item->name = g_strdup_printf("payee %d", item->key);
226 GLOBALS->changes_count++;
227 }
228 list = g_list_next(list);
229 }
230 g_list_free(lxxx);
231
232 //categories
233 lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
234 while (list != NULL)
235 {
236 Category *item = list->data;
237
238 if(item->key != 0)
239 {
240 g_free(item->name);
241 item->name = g_strdup_printf("category %d", item->key);
242 GLOBALS->changes_count++;
243 }
244 list = g_list_next(list);
245 }
246 g_list_free(lxxx);
247
248 //tags
249 lxxx = list = g_hash_table_get_values(GLOBALS->h_tag);
250 while (list != NULL)
251 {
252 Tag *item = list->data;
253
254 if(item->key != 0)
255 {
256 g_free(item->name);
257 item->name = g_strdup_printf("tag %d", item->key);
258 GLOBALS->changes_count++;
259 }
260 list = g_list_next(list);
261 }
262 g_list_free(lxxx);
263
264 //assigns
265 lxxx = list = g_hash_table_get_values(GLOBALS->h_rul);
266 while (list != NULL)
267 {
268 Assign *item = list->data;
269
270 if(item->key != 0)
271 {
272 g_free(item->name);
273 item->name = g_strdup_printf("assign %d", item->key);
274 GLOBALS->changes_count++;
275 }
276 list = g_list_next(list);
277 }
278 g_list_free(lxxx);
279
280 //archives
281 cnt = 0;
282 list = g_list_first(GLOBALS->arc_list);
283 while (list != NULL)
284 {
285 Archive *item = list->data;
286
287 g_free(item->wording);
288 item->wording = g_strdup_printf("archive %d", cnt++);
289 GLOBALS->changes_count++;
290
291 //later split anonymize also
292
293 list = g_list_next(list);
294 }
295
296 //transaction
297 list = g_list_first(GLOBALS->ope_list);
298 while (list != NULL)
299 {
300 Transaction *item = list->data;
301 Split *split;
302
303 g_free(item->info);
304 item->info = NULL;
305 g_free(item->wording);
306 item->wording = g_strdup_printf("memo %d", item->date);
307 GLOBALS->changes_count++;
308
309 if(item->flags & OF_SPLIT)
310 {
311 for(i=0;i<TXN_MAX_SPLIT;i++)
312 {
313 split = item->splits[i];
314 if( split == NULL ) break;
315
316 if(split->memo != NULL)
317 g_free(split->memo);
318
319 split->memo = g_strdup_printf("memo %d", i);
320 GLOBALS->changes_count++;
321 }
322
323
324 }
325
326 list = g_list_next(list);
327 }
328
329 }
330
331
332 void hbfile_cleanup(gboolean file_clear)
333 {
334 DB( g_print("\n[hbfile] cleanup\n") );
335 DB( g_print("- file clear is %d\n", file_clear) );
336
337 // Free data storage
338 //da_cur_destroy();
339 da_acc_destroy();
340 da_pay_destroy();
341 da_cat_destroy();
342 da_tag_destroy();
343 da_asg_destroy();
344 g_hash_table_destroy(GLOBALS->h_memo);
345 da_archive_destroy(GLOBALS->arc_list);
346 da_transaction_destroy(GLOBALS->ope_list);
347
348 hbfile_change_owner(NULL);
349
350 if(file_clear)
351 hbfile_change_filepath(NULL);
352
353 }
354
355
356 void hbfile_setup(gboolean file_clear)
357 {
358
359 DB( g_print("\n[hbfile] setup\n") );
360 DB( g_print("- file clear is %d\n", file_clear) );
361
362 // Allocate data storage
363 //da_cur_new();
364 da_acc_new();
365 da_pay_new();
366 da_cat_new();
367 da_tag_new();
368 da_asg_new();
369
370 GLOBALS->h_memo = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, NULL);
371 GLOBALS->arc_list = NULL;
372 GLOBALS->ope_list = NULL;
373
374
375 if(file_clear == TRUE)
376 {
377 //todo: maybe translate this also
378 hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "untitled.xhb", NULL));
379 GLOBALS->hbfile_is_new = TRUE;
380
381 DB( g_print("- path_hbfile is '%s'\n", PREFS->path_hbfile) );
382 DB( g_print("- xhb_filepath is '%s'\n", GLOBALS->xhb_filepath) );
383 }
384 else
385 {
386 GLOBALS->hbfile_is_new = FALSE;
387 }
388
389 hbfile_change_owner(g_strdup(_("Unknown")));
390
391 //GLOBALS->kcur = 0;
392
393 GLOBALS->vehicle_category = 0;
394
395 GLOBALS->auto_smode = 1;
396 GLOBALS->auto_nbdays = 0;
397 GLOBALS->auto_weekday = 1;
398
399 GLOBALS->changes_count = 0;
400
401 GLOBALS->xhb_hasbak = FALSE;
402
403 }
404
This page took 0.048755 seconds and 4 git commands to generate.