]> Dogcows Code - chaz/homebank/blob - src/hb-preferences.c
add gitignore
[chaz/homebank] / src / hb-preferences.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-preferences.h"
22 #include "hb-filter.h"
23 #include "gtk-chart-colors.h"
24
25 #ifdef G_OS_WIN32
26 #include <windows.h>
27 #endif
28
29 /****************************************************************************/
30 /* Debug macros */
31 /****************************************************************************/
32 #define MYDEBUG 0
33
34 #if MYDEBUG
35 #define DB(x) (x);
36 #else
37 #define DB(x);
38 #endif
39
40 /* our global datas */
41 extern struct HomeBank *GLOBALS;
42 extern struct Preferences *PREFS;
43
44
45 static void homebank_pref_init_monetary(void)
46 {
47 DB( g_print("\n[preferences] monetary\n") );
48
49
50 #ifdef G_OS_UNIX
51
52 struct lconv *lc = localeconv();
53
54 DB( g_print("\n[preferences] monetary unix\n") );
55
56 DB( g_print("mon_decimal_point is utf8: %d\n", g_utf8_validate(lc->mon_decimal_point, -1, NULL)) );
57 DB( g_print("mon_decimal_point '%s'\n", lc->mon_decimal_point) );
58 DB( g_print("mon_decimal_point %x %x %x %x\n", lc->mon_decimal_point[0], lc->mon_decimal_point[1], lc->mon_decimal_point[2], lc->mon_decimal_point[3]) );
59
60 DB( g_print("mon_thousands_sep is utf8: %d\n", g_utf8_validate(lc->mon_thousands_sep, -1, NULL)) );
61 DB( g_print("mon_thousands_sep '%s'\n", lc->mon_thousands_sep) );
62 DB( g_print("mon_thousands_sep %x %x %x %x\n", lc->mon_thousands_sep[0], lc->mon_thousands_sep[1], lc->mon_thousands_sep[2], lc->mon_thousands_sep[3]) );
63
64
65 DB( g_print("frac_digits '%d'\n", (gint)lc->frac_digits) );
66
67 DB( g_print("currency_symbol '%s'\n", lc->currency_symbol) );
68
69 DB( g_print("p_cs_precedes '%d'\n", lc->p_cs_precedes) );
70
71 DB( g_print("n_cs_precedes '%d'\n", lc->n_cs_precedes) );
72
73 /* ok assign */
74
75
76 if( lc->p_cs_precedes || lc->n_cs_precedes )
77 {
78 PREFS->base_cur.symbol = g_strdup(lc->currency_symbol);
79 PREFS->base_cur.is_prefix = TRUE;
80 DB( g_print("locale mon cs is a prefix\n") );
81 }
82 else
83 {
84 PREFS->base_cur.symbol = g_strdup(lc->currency_symbol);
85 PREFS->base_cur.is_prefix = FALSE;
86 }
87
88 PREFS->base_cur.decimal_char = g_strdup(lc->mon_decimal_point);
89
90 PREFS->base_cur.grouping_char = g_strdup(lc->mon_thousands_sep);
91
92 //todo:fix
93 //PREFS->base_cur.grouping_char = g_locale_to_utf8(lc->mon_thousands_sep, -1, NULL, NULL, NULL);
94 //PREFS->base_cur.grouping_char = g_convert (lc->mon_thousands_sep, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
95
96 DB( g_print(" -> grouping_char: '%s'\n", PREFS->base_cur.grouping_char) );
97
98 PREFS->base_cur.frac_digits = lc->frac_digits;
99
100 //fix 378992/421228
101 if( PREFS->base_cur.frac_digits > MAX_FRAC_DIGIT )
102 {
103 PREFS->base_cur.frac_digits = 2;
104 g_free(PREFS->base_cur.decimal_char);
105 PREFS->base_cur.decimal_char = g_strdup(".");
106 }
107
108 #else
109 #ifdef G_OS_WIN32
110 //todo: to be really set by a win32 specialist from the registry...
111 #define BUFFER_SIZE 512
112 char buffer[BUFFER_SIZE];
113 //LPWSTR wcBuffer = buffer;
114 LPSTR wcBuffer = buffer;
115 int iResult;
116 gsize toto;
117
118 //see g_locale_to_utf8 here
119 iResult = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, wcBuffer, BUFFER_SIZE);
120 if(iResult > 0)
121 {
122 DB( g_print("LOCALE_SCURRENCY='%s'\n", buffer) );
123 PREFS->base_cur.symbol = g_locale_to_utf8(buffer, -1, NULL, &toto, NULL);
124 }
125
126 iResult = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, wcBuffer, BUFFER_SIZE);
127 if(iResult > 0)
128 {
129 DB( g_print("LOCALE_SDECIMAL='%s'\n", buffer) );
130 PREFS->base_cur.decimal_char = g_locale_to_utf8(buffer, -1, NULL, &toto, NULL);
131 }
132
133 iResult = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, wcBuffer, BUFFER_SIZE);
134 if(iResult > 0)
135 {
136 DB( g_print("LOCALE_STHOUSAND='%s'\n", buffer) );
137 PREFS->base_cur.grouping_char = g_locale_to_utf8(buffer, -1, NULL, &toto, NULL);
138 }
139
140 PREFS->base_cur.frac_digits = 2;
141
142 #else
143
144 PREFS->base_cur.prefix_symbol = NULL; //g_strdup("");
145 PREFS->base_cur.suffix_symbol = NULL; //g_strdup("");
146 PREFS->base_cur.decimal_char = g_strdup(".");
147 PREFS->base_cur.grouping_char = NULL; //g_strdup("");
148 PREFS->base_cur.frac_digits = 2;
149
150 #endif
151 #endif
152
153 }
154
155
156
157 static void homebank_pref_init_wingeometry(struct WinGeometry *wg, gint l, gint t, gint w, gint h)
158 {
159 wg->l = l;
160 wg->t = t;
161 wg->w = w;
162 wg->h = h;
163 wg->s = 0;
164 }
165
166
167 /*
168 ** create the format string for monetary strfmon (major/minor)
169 */
170 static void _homebank_pref_createformat(void)
171 {
172 struct CurrencyFmt *cur;
173
174 DB( g_print("\n[preferences] pref create format\n") );
175
176 /*
177 if(PREFS->base_cur.grouping_char != NULL)
178 g_snprintf(GLOBALS->fmt_maj_number, 15, "%%^.%dn", PREFS->base_cur.frac_digits);
179 else
180 g_snprintf(GLOBALS->fmt_maj_number, 15, "%%.%dn", PREFS->base_cur.frac_digits);
181
182 DB( g_print("+ major is: '%s'\n", GLOBALS->fmt_maj_number) );
183
184
185 if(PREFS->minor_cur.grouping_char != NULL)
186 g_snprintf(GLOBALS->fmt_min_number, 15, "%s %%!^.%dn %s",
187 PREFS->minor_cur.prefix_symbol,
188 PREFS->minor_cur.frac_digits,
189 PREFS->minor_cur.suffix_symbol
190 );
191 else
192 g_snprintf(GLOBALS->fmt_min_number, 15, "%s %%!.%dn %s",
193 PREFS->minor_cur.prefix_symbol,
194 PREFS->minor_cur.frac_digits,
195 PREFS->minor_cur.suffix_symbol
196 );
197
198 DB( g_print("+ minor is: '%s'\n", GLOBALS->fmt_min_number) );
199 */
200
201 /* base mon format */
202 cur = &PREFS->base_cur;
203 g_snprintf(cur->format , 8-1, "%%.%df", cur->frac_digits);
204 g_snprintf(cur->monfmt, 32-1, (cur->is_prefix) ? "%s %%s" : "%%s %s", cur->symbol);
205 DB( g_print(" - format: '%s'\n", cur->format) );
206 DB( g_print(" - monfmt: '%s'\n", cur->monfmt) );
207
208 /* minor mon format */
209 cur = &PREFS->minor_cur;
210 g_snprintf(cur->format , 8-1, "%%.%df", cur->frac_digits);
211 g_snprintf(cur->monfmt, 32-1, (cur->is_prefix) ? "%s %%s" : "%%s %s", cur->symbol);
212 DB( g_print(" - format: '%s'\n", cur->format) );
213 DB( g_print(" - monfmt: '%s'\n", cur->monfmt) );
214
215 }
216
217
218 //vehicle_unit_100
219 //vehicle_unit_distbyvol
220 //=> used for column title
221
222 static void _homebank_pref_init_measurement_units(void)
223 {
224 // unit is kilometer
225 if(!PREFS->vehicle_unit_ismile)
226 {
227 PREFS->vehicle_unit_dist = "%d km";
228 PREFS->vehicle_unit_100 = "100 km";
229 }
230 // unit is miles
231 else
232 {
233 PREFS->vehicle_unit_dist = "%d m.";
234 PREFS->vehicle_unit_100 = "100 miles";
235 }
236
237 // unit is Liters
238 if(!PREFS->vehicle_unit_isgal)
239 {
240 PREFS->vehicle_unit_vol = "%.2f L";
241 if(!PREFS->vehicle_unit_ismile)
242 PREFS->vehicle_unit_distbyvol = "km/L";
243 else
244 PREFS->vehicle_unit_distbyvol = "miles/gal";
245 }
246 // unit is gallon
247 else
248 {
249 PREFS->vehicle_unit_vol = "%.2f gal";
250 if(!PREFS->vehicle_unit_ismile)
251 PREFS->vehicle_unit_distbyvol = "km/gal";
252 else
253 PREFS->vehicle_unit_distbyvol = "miles/gal";
254 }
255
256 }
257
258
259 void homebank_pref_free(void)
260 {
261 DB( g_print("\n[preferences] free\n") );
262
263
264 g_free(PREFS->date_format);
265
266 g_free(PREFS->color_exp);
267 g_free(PREFS->color_inc);
268 g_free(PREFS->color_warn);
269
270 g_free(PREFS->path_hbfile);
271 g_free(PREFS->path_import);
272 g_free(PREFS->path_export);
273 //g_free(PREFS->path_navigator);
274
275 g_free(PREFS->language);
276
277 g_free(PREFS->base_cur.symbol);
278 g_free(PREFS->base_cur.decimal_char);
279 g_free(PREFS->base_cur.grouping_char);
280
281 g_free(PREFS->minor_cur.symbol);
282 g_free(PREFS->minor_cur.decimal_char);
283 g_free(PREFS->minor_cur.grouping_char);
284
285 memset(PREFS, 0, sizeof(struct Preferences));
286 }
287
288
289 void homebank_pref_setdefault(void)
290 {
291 gint i;
292
293 DB( g_print("\n[preferences] pref init\n") );
294
295 homebank_pref_free();
296
297 PREFS->language = NULL;
298
299 PREFS->date_format = g_strdup(DEFAULT_FORMAT_DATE);
300
301 PREFS->path_hbfile = g_strdup_printf("%s", g_get_home_dir ());
302 PREFS->path_import = g_strdup_printf("%s", g_get_home_dir ());
303 PREFS->path_export = g_strdup_printf("%s", g_get_home_dir ());
304 //PREFS->path_navigator = g_strdup(DEFAULT_PATH_NAVIGATOR);
305
306 PREFS->showsplash = TRUE;
307 PREFS->loadlast = TRUE;
308 PREFS->appendscheduled = FALSE;
309
310 PREFS->heritdate = FALSE;
311 PREFS->hidereconciled = FALSE;
312
313 PREFS->toolbar_style = 4; //text beside icons
314 PREFS->custom_colors = TRUE;
315 PREFS->color_exp = g_strdup(DEFAULT_EXP_COLOR);
316 PREFS->color_inc = g_strdup(DEFAULT_INC_COLOR);
317 PREFS->color_warn = g_strdup(DEFAULT_WARN_COLOR);
318 PREFS->rules_hint = FALSE;
319
320 /* fiscal year */
321 PREFS->fisc_year_day = 1;
322 PREFS->fisc_year_month = 1;
323
324 /* windows position/size */
325 homebank_pref_init_wingeometry(&PREFS->wal_wg, 0, 0, 1024, 600);
326 homebank_pref_init_wingeometry(&PREFS->acc_wg, 0, 0, 1024, 600);
327 homebank_pref_init_wingeometry(&PREFS->sta_wg, 0, 0, 800, 494);
328 homebank_pref_init_wingeometry(&PREFS->tme_wg, 0, 0, 800, 494);
329 homebank_pref_init_wingeometry(&PREFS->ove_wg, 0, 0, 800, 494);
330 homebank_pref_init_wingeometry(&PREFS->bud_wg, 0, 0, 800, 494);
331 homebank_pref_init_wingeometry(&PREFS->cst_wg, 0, 0, 800, 494);
332
333 homebank_pref_init_monetary();
334
335 PREFS->wal_toolbar = TRUE;
336 PREFS->wal_spending = TRUE;
337 PREFS->wal_upcoming = TRUE;
338 PREFS->wal_vpaned = 600/2;
339 PREFS->wal_hpaned = 1024/2;
340
341
342
343 i = 0;
344 /* prior v4.5
345 PREFS->lst_ope_columns[i++] = LST_DSPOPE_STATUS;
346 PREFS->lst_ope_columns[i++] = LST_DSPOPE_DATE;
347 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INFO;
348 PREFS->lst_ope_columns[i++] = LST_DSPOPE_PAYEE;
349 PREFS->lst_ope_columns[i++] = LST_DSPOPE_WORDING;
350 PREFS->lst_ope_columns[i++] = -LST_DSPOPE_AMOUNT;
351 PREFS->lst_ope_columns[i++] = LST_DSPOPE_EXPENSE;
352 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INCOME;
353 PREFS->lst_ope_columns[i++] = LST_DSPOPE_CATEGORY;
354 PREFS->lst_ope_columns[i++] = LST_DSPOPE_TAGS;
355 */
356 PREFS->lst_ope_columns[i++] = LST_DSPOPE_STATUS; //always displayed
357 PREFS->lst_ope_columns[i++] = LST_DSPOPE_DATE; //always displayed
358 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INFO;
359 PREFS->lst_ope_columns[i++] = LST_DSPOPE_PAYEE;
360 PREFS->lst_ope_columns[i++] = LST_DSPOPE_CATEGORY;
361 PREFS->lst_ope_columns[i++] = LST_DSPOPE_TAGS;
362 PREFS->lst_ope_columns[i++] = -LST_DSPOPE_AMOUNT;
363 PREFS->lst_ope_columns[i++] = LST_DSPOPE_EXPENSE;
364 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INCOME;
365 PREFS->lst_ope_columns[i++] = LST_DSPOPE_BALANCE;
366 PREFS->lst_ope_columns[i++] = LST_DSPOPE_WORDING;
367
368 PREFS->lst_ope_sort_id = LST_DSPOPE_DATE;
369 PREFS->lst_ope_sort_order = GTK_SORT_ASCENDING;
370
371
372 //PREFS->base_cur.nbdecimal = 2;
373 //PREFS->base_cur.separator = TRUE;
374
375 PREFS->date_range_wal = FLT_RANGE_LASTMONTH;
376 PREFS->date_range_txn = FLT_RANGE_LAST12MONTHS;
377 PREFS->date_range_rep = FLT_RANGE_THISYEAR;
378
379
380 //todo: add intelligence here
381 PREFS->euro_active = FALSE;
382
383 PREFS->euro_country = 0;
384 PREFS->euro_value = 1.0;
385 //PREFS->euro_nbdec = 2;
386 //PREFS->euro_thsep = TRUE;
387 //PREFS->euro_symbol = g_strdup("??");
388
389 PREFS->stat_byamount = FALSE;
390 PREFS->stat_showdetail = FALSE;
391 PREFS->stat_showrate = FALSE;
392 PREFS->budg_showdetail = FALSE;
393 PREFS->report_color_scheme = CHART_COLMAP_HOMEBANK;
394
395 PREFS->chart_legend = FALSE;
396
397 PREFS->vehicle_unit_ismile = FALSE;
398 PREFS->vehicle_unit_isgal = FALSE;
399
400 _homebank_pref_createformat();
401 _homebank_pref_init_measurement_units();
402
403 }
404
405
406 /*
407 ** load preference from homedir/.homebank (HB_DATA_PATH)
408 */
409 static void homebank_pref_get_wingeometry(
410 GKeyFile *key_file,
411 const gchar *group_name,
412 const gchar *key,
413 struct WinGeometry *storage)
414 {
415 if( g_key_file_has_key(key_file, group_name, key, NULL) )
416 {
417 gint *wg;
418 gsize length;
419
420 wg = g_key_file_get_integer_list(key_file, group_name, key, &length, NULL);
421 memcpy(storage, wg, 5*sizeof(gint));
422 g_free(wg);
423 // #606613 ensure left/top to be > 0
424 if(storage->l < 0)
425 storage->l = 0;
426
427 if(storage->t < 0)
428 storage->t = 0;
429 }
430 }
431
432
433
434
435 static void homebank_pref_get_boolean(
436 GKeyFile *key_file,
437 const gchar *group_name,
438 const gchar *key,
439 gboolean *storage)
440 {
441
442 if( g_key_file_has_key(key_file, group_name, key, NULL) )
443 {
444 *storage = g_key_file_get_boolean(key_file, group_name, key, NULL);
445 }
446 }
447
448 static void homebank_pref_get_integer(
449 GKeyFile *key_file,
450 const gchar *group_name,
451 const gchar *key,
452 gint *storage)
453 {
454
455 DB( g_print(" search %s in %s\n", key, group_name) );
456
457
458 if( g_key_file_has_key(key_file, group_name, key, NULL) )
459 {
460 *storage = g_key_file_get_integer(key_file, group_name, key, NULL);
461
462 DB( g_print(" store integer %d for %s at %x\n", *storage, key, *storage) );
463 }
464 }
465
466 static void homebank_pref_get_guint32(
467 GKeyFile *key_file,
468 const gchar *group_name,
469 const gchar *key,
470 guint32 *storage)
471 {
472
473 if( g_key_file_has_key(key_file, group_name, key, NULL) )
474 {
475 *storage = g_key_file_get_integer(key_file, group_name, key, NULL);
476 }
477 }
478
479 static void homebank_pref_get_short(
480 GKeyFile *key_file,
481 const gchar *group_name,
482 const gchar *key,
483 gshort *storage)
484 {
485
486 if( g_key_file_has_key(key_file, group_name, key, NULL) )
487 {
488 *storage = (gshort)g_key_file_get_integer(key_file, group_name, key, NULL);
489 }
490 }
491
492 static void homebank_pref_get_string(
493 GKeyFile *key_file,
494 const gchar *group_name,
495 const gchar *key,
496 gchar **storage)
497 {
498 gchar *string;
499
500 if( g_key_file_has_key(key_file, group_name, key, NULL) )
501 {
502 /* free any previous string */
503 if( *storage != NULL )
504 {
505 //DB( g_print(" storage was not null, freeing\n") );
506
507 g_free(*storage);
508 }
509
510 *storage = NULL;
511
512 string = g_key_file_get_string(key_file, group_name, key, NULL);
513 if( string != NULL )
514 {
515 *storage = g_strdup(string);
516
517 //DB( g_print(" store '%s' for %s at %x\n", string, key, *storage) );
518 }
519 }
520
521 /*
522 if (error)
523 {
524 g_warning ("error: %s\n", error->message);
525 g_error_free(error);
526 error = NULL;
527 }
528 */
529
530
531 }
532
533
534 static void homebank_pref_currfmt_convert(struct CurrencyFmt *cur, gchar *prefix, gchar *suffix)
535 {
536
537 if( (prefix != NULL) && (strlen(prefix) > 0) )
538 {
539 cur->symbol = g_strdup(prefix);
540 cur->is_prefix = TRUE;
541 }
542 else if( (suffix != NULL) )
543 {
544 cur->symbol = g_strdup(suffix);
545 cur->is_prefix = FALSE;
546 }
547 }
548
549
550 gboolean homebank_pref_load(void)
551 {
552 GKeyFile *keyfile;
553 gboolean retval = FALSE;
554 gchar *group, *filename;
555 guint32 version;
556 GError *error = NULL;
557
558 DB( g_print("\n[preferences] pref load\n") );
559
560 keyfile = g_key_file_new();
561 if(keyfile)
562 {
563 filename = g_build_filename(homebank_app_get_config_dir(), "preferences", NULL );
564
565 DB( g_print(" - filename: %s\n", filename) );
566
567
568 if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
569 {
570
571 group = "General";
572
573 DB( g_print(" -> ** General\n") );
574
575 //since 4.51 version is integer
576 homebank_pref_get_guint32 (keyfile, group, "Version", &version);
577 if(version == 0) // old double number
578 {
579 gdouble v = g_key_file_get_double (keyfile, group, "Version", NULL);
580 version = (guint32)(v * 10);
581 }
582
583 DB( g_print(" - version: %d\n", version) );
584
585 homebank_pref_get_string(keyfile, group, "Language", &PREFS->language);
586
587 homebank_pref_get_short(keyfile, group, "BarStyle" , &PREFS->toolbar_style);
588
589 if(version <= 6 && PREFS->toolbar_style == 0) // force system to text beside
590 {
591 PREFS->toolbar_style = 4;
592 }
593
594 if(version <= 2) // retrieve old settings
595 {
596 guint32 color;
597
598 homebank_pref_get_guint32(keyfile, group, "ColorExp" , &color);
599 g_free(PREFS->color_exp);
600 PREFS->color_exp = g_strdup_printf("#%06x", color);
601
602 homebank_pref_get_guint32(keyfile, group, "ColorInc" , &color);
603 g_free(PREFS->color_inc);
604 PREFS->color_inc = g_strdup_printf("#%06x", color);
605
606 homebank_pref_get_guint32(keyfile, group, "ColorWarn", &color);
607 g_free(PREFS->color_warn);
608 PREFS->color_warn = g_strdup_printf("#%06x", color);
609 }
610 else
611 {
612 homebank_pref_get_boolean(keyfile, group, "CustomColors", &PREFS->custom_colors);
613
614 homebank_pref_get_string(keyfile, group, "ColorExp" , &PREFS->color_exp);
615 homebank_pref_get_string(keyfile, group, "ColorInc" , &PREFS->color_inc);
616 homebank_pref_get_string(keyfile, group, "ColorWarn", &PREFS->color_warn);
617
618 homebank_pref_get_boolean(keyfile, group, "RulesHint", &PREFS->rules_hint);
619 }
620
621 DB( g_print(" - color exp: %s\n", PREFS->color_exp) );
622 DB( g_print(" - color inc: %s\n", PREFS->color_inc) );
623 DB( g_print(" - color wrn: %s\n", PREFS->color_warn) );
624
625
626 homebank_pref_get_string(keyfile, group, "WalletPath", &PREFS->path_hbfile);
627 homebank_pref_get_string(keyfile, group, "ImportPath", &PREFS->path_import);
628 homebank_pref_get_string(keyfile, group, "ExportPath", &PREFS->path_export);
629
630 homebank_pref_get_boolean(keyfile, group, "ShowSplash", &PREFS->showsplash);
631 homebank_pref_get_boolean(keyfile, group, "LoadLast", &PREFS->loadlast);
632 homebank_pref_get_boolean(keyfile, group, "AppendScheduled", &PREFS->appendscheduled);
633
634 homebank_pref_get_boolean(keyfile, group, "HeritDate", &PREFS->heritdate);
635 homebank_pref_get_boolean(keyfile, group, "HideReconciled", &PREFS->hidereconciled);
636
637 if( g_key_file_has_key(keyfile, group, "ColumnsOpe", NULL) )
638 {
639 gboolean *bsrc;
640 gint *src, i;
641 gsize length;
642
643 if(version <= 2) //retrieve old 0.1 or 0.2 visibility boolean
644 {
645 bsrc = g_key_file_get_boolean_list(keyfile, group, "ColumnsOpe", &length, &error);
646 if( length == NUM_LST_DSPOPE-1 )
647 {
648 //and convert
649 for(i=0; i<NUM_LST_DSPOPE-1 ; i++)
650 {
651 PREFS->lst_ope_columns[i] = (bsrc[i] == TRUE) ? i+1 : -(i+1);
652 }
653 }
654 g_free(bsrc);
655 }
656 else
657 {
658 src = g_key_file_get_integer_list(keyfile, group, "ColumnsOpe", &length, &error);
659
660 DB( g_print(" - length %d (max=%d)\n", length, NUM_LST_DSPOPE) );
661
662 if( length == NUM_LST_DSPOPE-1 )
663 {
664 DB( g_print(" - copying column order from pref file\n") );
665 memcpy(PREFS->lst_ope_columns, src, length*sizeof(gint));
666 }
667 else
668 {
669 if(version <= 7)
670 {
671 if( length == NUM_LST_DSPOPE-2 ) //1 less column before v4.5.1
672 {
673 DB( g_print(" - upgrade from v7\n") );
674 DB( g_print(" - copying column order from pref file\n") );
675 memcpy(PREFS->lst_ope_columns, src, length*sizeof(gint));
676 PREFS->lst_ope_columns[10] = LST_DSPOPE_BALANCE;
677 }
678 }
679
680 }
681
682 g_free(src);
683 }
684
685 }
686
687 homebank_pref_get_integer(keyfile, group, "OpeSortId", &PREFS->lst_ope_sort_id);
688 homebank_pref_get_integer(keyfile, group, "OpeSortOrder", &PREFS->lst_ope_sort_order);
689
690 DB( g_print(" - set sort to %d %d\n", PREFS->lst_ope_sort_id, PREFS->lst_ope_sort_order) );
691
692 homebank_pref_get_short(keyfile, group, "FiscYearDay", &PREFS->fisc_year_day);
693 homebank_pref_get_short(keyfile, group, "FiscYearMonth", &PREFS->fisc_year_month);
694
695
696 group = "Windows";
697
698 DB( g_print(" -> ** Windows\n") );
699
700 homebank_pref_get_wingeometry(keyfile, group, "Wal", &PREFS->wal_wg);
701 homebank_pref_get_wingeometry(keyfile, group, "Acc", &PREFS->acc_wg);
702 homebank_pref_get_wingeometry(keyfile, group, "Sta", &PREFS->sta_wg);
703 homebank_pref_get_wingeometry(keyfile, group, "Tme", &PREFS->tme_wg);
704 homebank_pref_get_wingeometry(keyfile, group, "Ove", &PREFS->ove_wg);
705 homebank_pref_get_wingeometry(keyfile, group, "Bud", &PREFS->bud_wg);
706 homebank_pref_get_wingeometry(keyfile, group, "Car", &PREFS->cst_wg);
707 if(version <= 7) //set maximize to 0
708 {
709 PREFS->wal_wg.s = 0;
710 PREFS->acc_wg.s = 0;
711 PREFS->sta_wg.s = 0;
712 PREFS->tme_wg.s = 0;
713 PREFS->ove_wg.s = 0;
714 PREFS->bud_wg.s = 0;
715 PREFS->cst_wg.s = 0;
716 }
717 homebank_pref_get_integer(keyfile, group, "WalVPaned", &PREFS->wal_vpaned);
718 homebank_pref_get_integer(keyfile, group, "WalHPaned", &PREFS->wal_hpaned);
719 homebank_pref_get_boolean(keyfile, group, "WalToolbar", &PREFS->wal_toolbar);
720 homebank_pref_get_boolean(keyfile, group, "WalSpending", &PREFS->wal_spending);
721 homebank_pref_get_boolean(keyfile, group, "WalUpcoming", &PREFS->wal_upcoming);
722
723
724 group = "Format";
725
726 DB( g_print(" -> ** Format\n") );
727
728 homebank_pref_get_string(keyfile, group, "DateFmt", &PREFS->date_format);
729
730 if(version <= 1)
731 {
732 //retrieve old 0.1 preferences
733 homebank_pref_get_short(keyfile, group, "NumNbDec", &PREFS->base_cur.frac_digits);
734 //PREFS->base_cur.separator = g_key_file_get_boolean (keyfile, group, "NumSep", NULL);
735 }
736 else
737 {
738 if(version < 460)
739 {
740 gchar *prefix = NULL;
741 gchar *suffix = NULL;
742
743 homebank_pref_get_string(keyfile, group, "PreSymbol", &prefix);
744 homebank_pref_get_string(keyfile, group, "SufSymbol", &suffix);
745 homebank_pref_currfmt_convert(&PREFS->base_cur, prefix, suffix);
746 g_free(prefix);
747 g_free(suffix);
748 }
749 else
750 {
751 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->base_cur.symbol);
752 homebank_pref_get_boolean(keyfile, group, "IsPrefix", &PREFS->base_cur.is_prefix);
753 }
754 homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->base_cur.decimal_char);
755 homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->base_cur.grouping_char);
756 homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->base_cur.frac_digits);
757
758 //fix 378992/421228
759 if( PREFS->base_cur.frac_digits > MAX_FRAC_DIGIT )
760 PREFS->base_cur.frac_digits = MAX_FRAC_DIGIT;
761 }
762
763 if(version < 460)
764 {
765 gboolean useimperial;
766
767 homebank_pref_get_boolean(keyfile, group, "UKUnits", &useimperial);
768 if(useimperial)
769 {
770 PREFS->vehicle_unit_ismile = TRUE;
771 PREFS->vehicle_unit_isgal = TRUE;
772 }
773 }
774
775 homebank_pref_get_boolean(keyfile, group, "UnitIsMile", &PREFS->vehicle_unit_ismile);
776 homebank_pref_get_boolean(keyfile, group, "UnitIsGal", &PREFS->vehicle_unit_isgal);
777
778
779 group = "Filter";
780
781 DB( g_print(" -> ** Filter\n") );
782
783 homebank_pref_get_integer(keyfile, group, "DateRangeWal", &PREFS->date_range_wal);
784 homebank_pref_get_integer(keyfile, group, "DateRangeTxn", &PREFS->date_range_txn);
785 homebank_pref_get_integer(keyfile, group, "DateRangeRep", &PREFS->date_range_rep);
786
787 if(version <= 7)
788 {
789 // shift date range >= 5, since we inserted a new one at position 5
790 if(PREFS->date_range_wal >= FLT_RANGE_LASTYEAR)
791 PREFS->date_range_wal++;
792 if(PREFS->date_range_txn >= FLT_RANGE_LASTYEAR)
793 PREFS->date_range_txn++;
794 if(PREFS->date_range_rep >= FLT_RANGE_LASTYEAR)
795 PREFS->date_range_rep++;
796 }
797
798
799 group = "Euro";
800
801 DB( g_print(" -> ** Euro\n") );
802
803 //homebank_pref_get_string(keyfile, group, "DefCurrency" , &PREFS->curr_default);
804
805 homebank_pref_get_boolean(keyfile, group, "Active", &PREFS->euro_active);
806 homebank_pref_get_integer(keyfile, group, "Country", &PREFS->euro_country);
807
808 gchar *ratestr = g_key_file_get_string (keyfile, group, "ChangeRate", NULL);
809 if(ratestr != NULL) PREFS->euro_value = g_ascii_strtod(ratestr, NULL);
810
811 if(version <= 1)
812 {
813 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->minor_cur.symbol);
814 PREFS->minor_cur.frac_digits = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
815
816 //PREFS->euro_nbdec = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
817 //PREFS->euro_thsep = g_key_file_get_boolean (keyfile, group, "Sep", NULL);
818 //gchar *tmpstr = g_key_file_get_string (keyfile, group, "Symbol", &error);
819 }
820 else
821 {
822 if(version < 460)
823 {
824 gchar *prefix = NULL;
825 gchar *suffix = NULL;
826
827 homebank_pref_get_string(keyfile, group, "PreSymbol", &prefix);
828 homebank_pref_get_string(keyfile, group, "SufSymbol", &suffix);
829 homebank_pref_currfmt_convert(&PREFS->minor_cur, prefix, suffix);
830 g_free(prefix);
831 g_free(suffix);
832 }
833 else
834 {
835 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->minor_cur.symbol);
836 homebank_pref_get_boolean(keyfile, group, "IsPrefix", &PREFS->minor_cur.is_prefix);
837 }
838 homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->minor_cur.decimal_char);
839 homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->minor_cur.grouping_char);
840 homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->minor_cur.frac_digits);
841
842 //fix 378992/421228
843 if( PREFS->minor_cur.frac_digits > MAX_FRAC_DIGIT )
844 PREFS->minor_cur.frac_digits = MAX_FRAC_DIGIT;
845
846 }
847
848 //PREFS->euro_symbol = g_locale_to_utf8(tmpstr, -1, NULL, NULL, NULL);
849
850 group = "Report";
851
852 DB( g_print(" -> ** Report\n") );
853
854 homebank_pref_get_boolean(keyfile, group, "StatByAmount", &PREFS->stat_byamount);
855 homebank_pref_get_boolean(keyfile, group, "StatDetail", &PREFS->stat_showdetail);
856 homebank_pref_get_boolean(keyfile, group, "StatRate", &PREFS->stat_showrate);
857 homebank_pref_get_boolean(keyfile, group, "BudgDetail", &PREFS->budg_showdetail);
858 homebank_pref_get_integer(keyfile, group, "ColorScheme", &PREFS->report_color_scheme);
859
860 group = "Exchange";
861
862 DB( g_print(" -> ** Exchange\n") );
863
864 homebank_pref_get_integer(keyfile, group, "DateFmt", &PREFS->dtex_datefmt);
865 homebank_pref_get_integer(keyfile, group, "OfxMemo", &PREFS->dtex_ofxmemo);
866
867
868 //group = "Chart";
869 //PREFS->chart_legend = g_key_file_get_boolean (keyfile, group, "Legend", NULL);
870
871
872 /*
873 #if MYDEBUG == 1
874 gsize length;
875 gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
876 //g_print(" keyfile:\n%s\n len=%d\n", contents, length);
877 g_free(contents);
878 #endif
879 */
880
881 }
882 g_free(filename);
883 g_key_file_free (keyfile);
884
885 _homebank_pref_createformat();
886 _homebank_pref_init_measurement_units();
887 }
888
889 return retval;
890 }
891
892 static void homebank_pref_set_string(
893 GKeyFile *key_file,
894 const gchar *group_name,
895 const gchar *key,
896 gchar *string)
897 {
898
899 DB( g_print(" - homebank_pref_set_string :: group='%s' key='%s' value='%s'\n", group_name, key, string) );
900
901 if( string != NULL && *string != '\0')
902 g_key_file_set_string (key_file, group_name, key, string);
903 else
904 g_key_file_set_string (key_file, group_name, key, "");
905
906 }
907
908
909 /*
910 ** save preference to homedir/.homebank (HB_DATA_PATH)
911 */
912 gboolean homebank_pref_save(void)
913 {
914 GKeyFile *keyfile;
915 gboolean retval = FALSE;
916 gchar *group, *filename;
917 gsize length;
918
919 DB( g_print("\n[preferences] pref save\n") );
920
921 keyfile = g_key_file_new();
922 if(keyfile )
923 {
924
925 DB( g_print(" -> ** general\n") );
926
927
928 group = "General";
929 g_key_file_set_integer (keyfile, group, "Version", PREF_VERSION);
930
931 homebank_pref_set_string (keyfile, group, "Language", PREFS->language);
932
933 g_key_file_set_integer (keyfile, group, "BarStyle", PREFS->toolbar_style);
934 //g_key_file_set_integer (keyfile, group, "BarImageSize", PREFS->image_size);
935
936 g_key_file_set_boolean (keyfile, group, "CustomColors", PREFS->custom_colors);
937 g_key_file_set_string (keyfile, group, "ColorExp" , PREFS->color_exp);
938 g_key_file_set_string (keyfile, group, "ColorInc" , PREFS->color_inc);
939 g_key_file_set_string (keyfile, group, "ColorWarn", PREFS->color_warn);
940
941 g_key_file_set_boolean (keyfile, group, "RulesHint", PREFS->rules_hint);
942
943
944 homebank_pref_set_string (keyfile, group, "WalletPath" , PREFS->path_hbfile);
945 homebank_pref_set_string (keyfile, group, "ImportPath" , PREFS->path_import);
946 homebank_pref_set_string (keyfile, group, "ExportPath" , PREFS->path_export);
947 //g_key_file_set_string (keyfile, group, "NavigatorPath", PREFS->path_navigator);
948
949 g_key_file_set_boolean (keyfile, group, "ShowSplash", PREFS->showsplash);
950 g_key_file_set_boolean (keyfile, group, "LoadLast", PREFS->loadlast);
951 g_key_file_set_boolean (keyfile, group, "AppendScheduled", PREFS->appendscheduled);
952
953 g_key_file_set_boolean (keyfile, group, "HeritDate", PREFS->heritdate);
954 g_key_file_set_boolean (keyfile, group, "HideReconciled", PREFS->hidereconciled);
955
956 g_key_file_set_integer_list(keyfile, group, "ColumnsOpe", PREFS->lst_ope_columns, NUM_LST_DSPOPE-1);
957 g_key_file_set_integer (keyfile, group, "OpeSortId" , PREFS->lst_ope_sort_id);
958 g_key_file_set_integer (keyfile, group, "OpeSortOrder" , PREFS->lst_ope_sort_order);
959
960 g_key_file_set_integer (keyfile, group, "FiscYearDay" , PREFS->fisc_year_day);
961 g_key_file_set_integer (keyfile, group, "FiscYearMonth" , PREFS->fisc_year_month);
962
963 // added v3.4
964 DB( g_print(" -> ** windows\n") );
965
966 group = "Windows";
967 g_key_file_set_integer_list(keyfile, group, "Wal", (gint *)&PREFS->wal_wg, 5);
968 g_key_file_set_integer_list(keyfile, group, "Acc", (gint *)&PREFS->acc_wg, 5);
969 g_key_file_set_integer_list(keyfile, group, "Sta", (gint *)&PREFS->sta_wg, 5);
970 g_key_file_set_integer_list(keyfile, group, "Tme", (gint *)&PREFS->tme_wg, 5);
971 g_key_file_set_integer_list(keyfile, group, "Ove", (gint *)&PREFS->ove_wg, 5);
972 g_key_file_set_integer_list(keyfile, group, "Bud", (gint *)&PREFS->bud_wg, 5);
973 g_key_file_set_integer_list(keyfile, group, "Car", (gint *)&PREFS->cst_wg, 5);
974
975 g_key_file_set_integer (keyfile, group, "WalVPaned" , PREFS->wal_vpaned);
976 g_key_file_set_integer (keyfile, group, "WalHPaned" , PREFS->wal_hpaned);
977 g_key_file_set_boolean (keyfile, group, "WalToolbar", PREFS->wal_toolbar);
978 g_key_file_set_boolean (keyfile, group, "WalSpending", PREFS->wal_spending);
979 g_key_file_set_boolean (keyfile, group, "WalUpcoming", PREFS->wal_upcoming);
980
981
982 DB( g_print(" -> ** format\n") );
983
984 group = "Format";
985 homebank_pref_set_string (keyfile, group, "DateFmt" , PREFS->date_format);
986
987 homebank_pref_set_string (keyfile, group, "Symbol" , PREFS->base_cur.symbol);
988 g_key_file_set_boolean (keyfile, group, "IsPrefix" , PREFS->base_cur.is_prefix);
989 homebank_pref_set_string (keyfile, group, "DecChar" , PREFS->base_cur.decimal_char);
990 homebank_pref_set_string (keyfile, group, "GroupChar" , PREFS->base_cur.grouping_char);
991 g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->base_cur.frac_digits);
992
993 //g_key_file_set_boolean (keyfile, group, "UKUnits" , PREFS->imperial_unit);
994 g_key_file_set_boolean (keyfile, group, "UnitIsMile" , PREFS->vehicle_unit_ismile);
995 g_key_file_set_boolean (keyfile, group, "UnitIsGal" , PREFS->vehicle_unit_isgal);
996
997
998 DB( g_print(" -> ** filter\n") );
999
1000 group = "Filter";
1001 g_key_file_set_integer (keyfile, group, "DateRangeWal", PREFS->date_range_wal);
1002 g_key_file_set_integer (keyfile, group, "DateRangeTxn", PREFS->date_range_txn);
1003 g_key_file_set_integer (keyfile, group, "DateRangeRep", PREFS->date_range_rep);
1004
1005 DB( g_print(" -> ** euro\n") );
1006
1007 //euro options
1008 group = "Euro";
1009
1010 //homebank_pref_set_string(keyfile, group, "DefCurrency" , PREFS->curr_default);
1011
1012 g_key_file_set_boolean (keyfile, group, "Active" , PREFS->euro_active);
1013 if( PREFS->euro_active )
1014 {
1015 g_key_file_set_integer (keyfile, group, "Country", PREFS->euro_country);
1016 gchar ratestr[64];
1017 g_ascii_dtostr(ratestr, 63, PREFS->euro_value);
1018 homebank_pref_set_string (keyfile, group, "ChangeRate", ratestr);
1019 homebank_pref_set_string (keyfile, group, "Symbol" , PREFS->minor_cur.symbol);
1020 g_key_file_set_boolean (keyfile, group, "IsPrefix" , PREFS->minor_cur.is_prefix);
1021 homebank_pref_set_string (keyfile, group, "DecChar" , PREFS->minor_cur.decimal_char);
1022 homebank_pref_set_string (keyfile, group, "GroupChar" , PREFS->minor_cur.grouping_char);
1023 g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->minor_cur.frac_digits);
1024 }
1025
1026 //report options
1027 DB( g_print(" -> ** report\n") );
1028
1029 group = "Report";
1030 g_key_file_set_boolean (keyfile, group, "StatByAmount", PREFS->stat_byamount);
1031 g_key_file_set_boolean (keyfile, group, "StatDetail" , PREFS->stat_showdetail);
1032 g_key_file_set_boolean (keyfile, group, "StatRate" , PREFS->stat_showrate);
1033 g_key_file_set_boolean (keyfile, group, "BudgDetail" , PREFS->budg_showdetail);
1034 g_key_file_set_integer (keyfile, group, "ColorScheme" , PREFS->report_color_scheme);
1035
1036
1037 group = "Exchange";
1038 g_key_file_set_integer (keyfile, group, "DateFmt", PREFS->dtex_datefmt);
1039 g_key_file_set_integer (keyfile, group, "OfxMemo", PREFS->dtex_ofxmemo);
1040
1041 //group = "Chart";
1042 //g_key_file_set_boolean (keyfile, group, "Legend", PREFS->chart_legend);
1043
1044 //g_key_file_set_string (keyfile, group, "", PREFS->);
1045 //g_key_file_set_boolean (keyfile, group, "", PREFS->);
1046 //g_key_file_set_integer (keyfile, group, "", PREFS->);
1047
1048 DB( g_print(" -> ** g_key_file_to_data\n") );
1049
1050 gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
1051
1052 //DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
1053
1054 filename = g_build_filename(homebank_app_get_config_dir(), "preferences", NULL );
1055
1056 DB( g_print(" -> filename: %s\n", filename) );
1057
1058 g_file_set_contents(filename, contents, length, NULL);
1059
1060 DB( g_print(" -> contents: %s\n", contents) );
1061
1062 DB( g_print(" -> freeing filename\n") );
1063 g_free(filename);
1064
1065 DB( g_print(" -> freeing buffer\n") );
1066
1067 g_free(contents);
1068
1069 DB( g_print(" -> freeing keyfile\n") );
1070
1071 g_key_file_free (keyfile);
1072 }
1073
1074 _homebank_pref_createformat();
1075 _homebank_pref_init_measurement_units();
1076
1077 return retval;
1078 }
1079
This page took 0.077677 seconds and 4 git commands to generate.