1
/* HomeBank -- Free, easy, personal accounting for everyone.
2
* Copyright (C) 1995-2008 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 "preferences.h"
24
/****************************************************************************/
26
/****************************************************************************/
35
/* our global datas */
36
extern struct HomeBank *GLOBALS;
37
extern struct Preferences *PREFS;
40
static void homebank_pref_init_monetary(void)
42
DB( g_print("(preferences) monetary\n") );
47
struct lconv *lc = localeconv();
49
DB( g_print("(preferences) monetary unix\n") );
51
DB( g_print("mon_decimal_point '%s'\n", lc->mon_decimal_point) );
52
DB( g_print("mon_thousands_sep '%s'\n", lc->mon_thousands_sep) );
54
DB( g_print("frac_digits '%d'\n", (gint)lc->frac_digits) );
56
DB( g_print("currency_symbol '%s'\n", lc->currency_symbol) );
58
DB( g_print("p_cs_precedes '%d'\n", lc->p_cs_precedes) );
60
DB( g_print("n_cs_precedes '%d'\n", lc->n_cs_precedes) );
64
if( lc->p_cs_precedes || lc->n_cs_precedes )
66
PREFS->base_cur.prefix_symbol = g_strdup(lc->currency_symbol);
67
PREFS->base_cur.suffix_symbol = g_strdup("");
68
DB( g_print("locale mon cs is a prefix\n") );
72
PREFS->base_cur.prefix_symbol = g_strdup("");
73
PREFS->base_cur.suffix_symbol = g_strdup(lc->currency_symbol);
76
PREFS->base_cur.decimal_char = g_strdup(lc->mon_decimal_point);
77
PREFS->base_cur.grouping_char = g_strdup(lc->mon_thousands_sep);
78
PREFS->base_cur.frac_digits = lc->frac_digits;
83
//todo: to be really set by a win32 specialist from the registry...
85
PREFS->base_cur.prefix_symbol = g_strdup("");;
86
PREFS->base_cur.suffix_symbol = g_strdup("");;
87
PREFS->base_cur.decimal_char = g_strdup(".");
88
PREFS->base_cur.grouping_char = g_strdup("");;
89
PREFS->base_cur.frac_digits = 2;
91
PREFS->base_cur.prefix_symbol = g_strdup("");;
92
PREFS->base_cur.suffix_symbol = g_strdup("");;
93
PREFS->base_cur.decimal_char = g_strdup(".");
94
PREFS->base_cur.grouping_char = g_strdup("");;
95
PREFS->base_cur.frac_digits = 2;
102
static void homebank_pref_update_to_tango_colors(void)
104
if(PREFS->color_exp == OLD_EXP_COLOR)
105
PREFS->color_exp = DEFAULT_EXP_COLOR;
107
if(PREFS->color_inc == OLD_INC_COLOR)
108
PREFS->color_inc = DEFAULT_INC_COLOR;
110
if(PREFS->color_warn == OLD_WARN_COLOR)
111
PREFS->color_warn = DEFAULT_WARN_COLOR;
115
static void homebank_pref_init_wingeometry(struct WinGeometry *wg, gint l, gint t, gint w, gint h)
124
void homebank_pref_free(void)
126
DB( g_print("(preferences) free\n") );
129
g_free(PREFS->date_format);
131
g_free(PREFS->path_wallet);
132
g_free(PREFS->path_import);
133
//g_free(PREFS->path_navigator);
135
g_free(PREFS->base_cur.prefix_symbol);
136
g_free(PREFS->base_cur.suffix_symbol);
137
g_free(PREFS->base_cur.decimal_char);
138
g_free(PREFS->base_cur.grouping_char);
140
g_free(PREFS->minor_cur.prefix_symbol);
141
g_free(PREFS->minor_cur.suffix_symbol);
142
g_free(PREFS->minor_cur.decimal_char);
143
g_free(PREFS->minor_cur.grouping_char);
145
memset(PREFS, 0, sizeof(struct Preferences));
149
void homebank_pref_setdefault(void)
152
DB( g_print("(preferences) pref init\n") );
154
homebank_pref_free();
156
PREFS->date_format = g_strdup(DEFAULT_FORMAT_DATE);
158
PREFS->path_wallet = g_strdup_printf("%s", g_get_home_dir ());
159
PREFS->path_import = g_strdup_printf("%s", g_get_home_dir ());
160
//PREFS->path_navigator = g_strdup(DEFAULT_PATH_NAVIGATOR);
163
PREFS->color_exp = DEFAULT_EXP_COLOR;
164
PREFS->color_inc = DEFAULT_INC_COLOR;
165
PREFS->color_warn = DEFAULT_WARN_COLOR;
167
//PREFS->runwizard = TRUE;
169
DB( g_print(" + #%x\n", PREFS->color_exp) );
170
DB( g_print(" + #%x\n", PREFS->color_inc) );
171
DB( g_print(" + #%x\n", PREFS->color_warn) );
173
/* windows position/size */
174
homebank_pref_init_wingeometry(&PREFS->wal_wg, 0, 0, 640, 480);
175
homebank_pref_init_wingeometry(&PREFS->acc_wg, 0, 0, 640, 480);
176
homebank_pref_init_wingeometry(&PREFS->sta_wg, 0, 0, 640, 480);
177
homebank_pref_init_wingeometry(&PREFS->ove_wg, 0, 0, 640, 480);
178
homebank_pref_init_wingeometry(&PREFS->bud_wg, 0, 0, 640, 480);
179
homebank_pref_init_wingeometry(&PREFS->car_wg, 0, 0, 640, 480);
181
PREFS->acc_wg.l = 20;
182
PREFS->acc_wg.t = 20;
183
PREFS->acc_wg.w = 640;
184
PREFS->acc_wg.h = 480;
186
homebank_pref_init_monetary();
188
PREFS->lst_ope_columns[COL_OPE_STATUS] = TRUE;
189
PREFS->lst_ope_columns[COL_OPE_DATE] = TRUE;
190
PREFS->lst_ope_columns[COL_OPE_INFO] = TRUE;
191
PREFS->lst_ope_columns[COL_OPE_PAYEE] = TRUE;
192
PREFS->lst_ope_columns[COL_OPE_WORDING] = TRUE;
193
PREFS->lst_ope_columns[COL_OPE_AMOUNT] = FALSE;
194
PREFS->lst_ope_columns[COL_OPE_EXPENSE] = TRUE;
195
PREFS->lst_ope_columns[COL_OPE_INCOME] = TRUE;
196
PREFS->lst_ope_columns[COL_OPE_CATEGORY] = TRUE;
198
//PREFS->base_cur.nbdecimal = 2;
199
//PREFS->base_cur.separator = TRUE;
201
PREFS->filter_range = 7;
204
//todo: add intelligence here
205
PREFS->euro_active = FALSE;
207
//todo add intelligence here
208
PREFS->minor_cur.prefix_symbol = g_strdup("");
209
PREFS->minor_cur.suffix_symbol = g_strdup("??");
210
PREFS->minor_cur.decimal_char = g_strdup(".");
211
PREFS->minor_cur.grouping_char = g_strdup(" ");
212
PREFS->minor_cur.frac_digits = 2;
215
PREFS->euro_country = 0;
216
PREFS->euro_value = 1.0;
217
//PREFS->euro_nbdec = 2;
218
//PREFS->euro_thsep = TRUE;
219
//PREFS->euro_symbol = g_strdup("??");
221
PREFS->stat_byamount = FALSE;
222
PREFS->stat_showdetail = FALSE;
223
PREFS->stat_showrate = FALSE;
224
PREFS->budg_showdetail = FALSE;
226
PREFS->chart_legend = FALSE;
231
** create the format string for monetary strfmon (major/minor)
233
void homebank_pref_createformat(void)
236
DB( g_print("(preferences) pref create format\n") );
239
if(PREFS->base_cur.grouping_char != NULL)
240
g_snprintf(GLOBALS->fmt_maj_number, 15, "%%^.%dn", PREFS->base_cur.frac_digits);
242
g_snprintf(GLOBALS->fmt_maj_number, 15, "%%.%dn", PREFS->base_cur.frac_digits);
244
DB( g_print("+ major is: '%s'\n", GLOBALS->fmt_maj_number) );
247
if(PREFS->minor_cur.grouping_char != NULL)
248
g_snprintf(GLOBALS->fmt_min_number, 15, "%s %%!^.%dn %s",
249
PREFS->minor_cur.prefix_symbol,
250
PREFS->minor_cur.frac_digits,
251
PREFS->minor_cur.suffix_symbol
254
g_snprintf(GLOBALS->fmt_min_number, 15, "%s %%!.%dn %s",
255
PREFS->minor_cur.prefix_symbol,
256
PREFS->minor_cur.frac_digits,
257
PREFS->minor_cur.suffix_symbol
260
DB( g_print("+ minor is: '%s'\n", GLOBALS->fmt_min_number) );
263
g_snprintf(PREFS->base_cur.format , 8-1, "%%.%df", PREFS->base_cur.frac_digits);
264
g_snprintf(PREFS->minor_cur.format, 8-1, "%%.%df", PREFS->minor_cur.frac_digits);
269
** load preference from homedir/.homebank
271
static void homebank_pref_get_wingeometry(
273
const gchar *group_name,
275
struct WinGeometry *storage)
277
if( g_key_file_has_key(key_file, group_name, key, NULL) )
282
wg = g_key_file_get_integer_list(key_file, group_name, key, &length, NULL);
283
memcpy(storage, wg, 4*sizeof(gint));
291
static void homebank_pref_get_boolean(
293
const gchar *group_name,
298
if( g_key_file_has_key(key_file, group_name, key, NULL) )
300
*storage = g_key_file_get_boolean(key_file, group_name, key, NULL);
304
static void homebank_pref_get_integer(
306
const gchar *group_name,
311
if( g_key_file_has_key(key_file, group_name, key, NULL) )
313
*storage = g_key_file_get_integer(key_file, group_name, key, NULL);
317
static void homebank_pref_get_guint32(
319
const gchar *group_name,
324
if( g_key_file_has_key(key_file, group_name, key, NULL) )
326
*storage = g_key_file_get_integer(key_file, group_name, key, NULL);
330
static void homebank_pref_get_short(
332
const gchar *group_name,
337
if( g_key_file_has_key(key_file, group_name, key, NULL) )
339
*storage = (gshort)g_key_file_get_integer(key_file, group_name, key, NULL);
343
static void homebank_pref_get_string(
345
const gchar *group_name,
351
if( g_key_file_has_key(key_file, group_name, key, NULL) )
353
/* free any previous string */
354
if( *storage != NULL )
356
DB( g_print(" storage was not null, freeing\n") );
363
string = g_key_file_get_string(key_file, group_name, key, NULL);
366
*storage = g_strdup(string);
368
DB( g_print(" store '%s' for %s at %x\n", string, key, *storage) );
375
g_warning ("error: %s\n", error->message);
385
gboolean homebank_pref_load(void)
388
gboolean retval = FALSE;
389
gchar *group, *filename;
390
GError *error = NULL;
392
DB( g_print("(preferences) pref load\n") );
394
keyfile = g_key_file_new();
397
filename = g_strdup_printf("%s/.homebank/preferences", g_get_home_dir ());
398
if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
403
gchar *version = g_key_file_get_string (keyfile, group, "Version", NULL);
404
DB( g_print(" version: %s\n", version) );
406
//todo x = strtoul(s, 0, 16); converti une chaine hexa en int
408
homebank_pref_get_short(keyfile, group, "BarStyle" , &PREFS->toolbar_style);
409
homebank_pref_get_guint32(keyfile, group, "ColorExp" , &PREFS->color_exp);
410
homebank_pref_get_guint32(keyfile, group, "ColorInc" , &PREFS->color_inc);
411
homebank_pref_get_guint32(keyfile, group, "ColorWarn", &PREFS->color_warn);
414
homebank_pref_get_boolean(keyfile, group, "RunWizard", &PREFS->runwizard);
417
homebank_pref_get_string(keyfile, group, "WalletPath", &PREFS->path_wallet);
418
homebank_pref_get_string(keyfile, group, "ImportPath", &PREFS->path_import);
419
//homebank_pref_get_string(keyfile, group, "NavigatorPath", &PREFS->path_navigator);
422
if( g_key_file_has_key(keyfile, group, "ColumnsOpe", NULL) )
427
pos = g_key_file_get_boolean_list(keyfile, group, "ColumnsOpe",
430
if( length == NUM_COL_OPE)
431
memcpy(PREFS->lst_ope_columns, pos, length*sizeof(gboolean));
438
homebank_pref_get_wingeometry(keyfile, group, "Wal", &PREFS->wal_wg);
439
homebank_pref_get_wingeometry(keyfile, group, "Acc", &PREFS->acc_wg);
440
homebank_pref_get_wingeometry(keyfile, group, "Sta", &PREFS->sta_wg);
441
homebank_pref_get_wingeometry(keyfile, group, "Ove", &PREFS->ove_wg);
442
homebank_pref_get_wingeometry(keyfile, group, "Bud", &PREFS->bud_wg);
443
homebank_pref_get_wingeometry(keyfile, group, "Car", &PREFS->car_wg);
447
homebank_pref_get_string(keyfile, group, "DateFmt", &PREFS->date_format);
449
if(!strcmp("0.1", version))
451
//retrieve old 0.1 preferences
452
homebank_pref_get_short(keyfile, group, "NumNbDec", &PREFS->base_cur.frac_digits);
453
//PREFS->base_cur.separator = g_key_file_get_boolean (keyfile, group, "NumSep", NULL);
457
homebank_pref_get_string(keyfile, group, "PreSymbol", &PREFS->base_cur.prefix_symbol);
458
homebank_pref_get_string(keyfile, group, "SufSymbol", &PREFS->base_cur.suffix_symbol);
459
homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->base_cur.decimal_char);
460
homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->base_cur.grouping_char);
461
homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->base_cur.frac_digits);
464
homebank_pref_get_boolean(keyfile, group, "UKUnits", &PREFS->imperial_unit);
469
homebank_pref_get_integer(keyfile, group, "DefRange", &PREFS->filter_range);
474
homebank_pref_get_boolean(keyfile, group, "Active", &PREFS->euro_active);
475
homebank_pref_get_integer(keyfile, group, "Country", &PREFS->euro_country);
477
gchar *ratestr = g_key_file_get_string (keyfile, group, "ChangeRate", NULL);
478
if(ratestr != NULL) PREFS->euro_value = g_ascii_strtod(ratestr, NULL);
480
if(!strcmp("0.1", version))
482
homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->minor_cur.suffix_symbol);
483
PREFS->minor_cur.frac_digits = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
485
//PREFS->euro_nbdec = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
486
//PREFS->euro_thsep = g_key_file_get_boolean (keyfile, group, "Sep", NULL);
487
//gchar *tmpstr = g_key_file_get_string (keyfile, group, "Symbol", &error);
491
homebank_pref_get_string(keyfile, group, "PreSymbol", &PREFS->minor_cur.prefix_symbol);
492
homebank_pref_get_string(keyfile, group, "SufSymbol", &PREFS->minor_cur.suffix_symbol);
493
homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->minor_cur.decimal_char);
494
homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->minor_cur.grouping_char);
495
homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->minor_cur.frac_digits);
498
//PREFS->euro_symbol = g_locale_to_utf8(tmpstr, -1, NULL, NULL, NULL);
503
homebank_pref_get_boolean(keyfile, group, "StatByAmount", &PREFS->stat_byamount);
504
homebank_pref_get_boolean(keyfile, group, "StatDetail", &PREFS->stat_showdetail);
505
homebank_pref_get_boolean(keyfile, group, "StatRate", &PREFS->stat_showrate);
506
homebank_pref_get_boolean(keyfile, group, "BudgDetail", &PREFS->budg_showdetail);
510
//PREFS->chart_legend = g_key_file_get_boolean (keyfile, group, "Legend", NULL);
516
gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
517
g_print(" keyfile:\n%s\n len=%d\n", contents, length);
524
g_key_file_free (keyfile);
527
/* update to tango ;-) */
528
homebank_pref_update_to_tango_colors();
530
//homebank_pref_createformat();
537
** save preference to homedir/.homebank
539
gboolean homebank_pref_save(void)
542
gboolean retval = FALSE;
543
gchar *group, *filename;
546
DB( g_print("(preferences) pref save\n") );
548
keyfile = g_key_file_new();
552
g_key_file_set_string (keyfile, group, "Version", PREF_VERSION);
554
g_key_file_set_integer (keyfile, group, "BarStyle", PREFS->toolbar_style);
555
//g_key_file_set_integer (keyfile, group, "BarImageSize", PREFS->image_size);
557
g_key_file_set_integer (keyfile, group, "ColorExp" , PREFS->color_exp);
558
g_key_file_set_integer (keyfile, group, "ColorInc" , PREFS->color_inc);
559
g_key_file_set_integer (keyfile, group, "ColorWarn", PREFS->color_warn);
562
g_key_file_set_boolean (keyfile, group, "RunWizard", PREFS->runwizard);
565
g_key_file_set_string (keyfile, group, "WalletPath" , PREFS->path_wallet);
566
g_key_file_set_string (keyfile, group, "ImportPath" , PREFS->path_import);
567
//g_key_file_set_string (keyfile, group, "NavigatorPath", PREFS->path_navigator);
569
g_key_file_set_boolean_list(keyfile, group, "ColumnsOpe", PREFS->lst_ope_columns, NUM_COL_OPE);
573
g_key_file_set_integer_list(keyfile, group, "Wal", (gint *)&PREFS->wal_wg, 4);
574
g_key_file_set_integer_list(keyfile, group, "Acc", (gint *)&PREFS->acc_wg, 4);
575
g_key_file_set_integer_list(keyfile, group, "Sta", (gint *)&PREFS->sta_wg, 4);
576
g_key_file_set_integer_list(keyfile, group, "Ove", (gint *)&PREFS->ove_wg, 4);
577
g_key_file_set_integer_list(keyfile, group, "Bud", (gint *)&PREFS->bud_wg, 4);
578
g_key_file_set_integer_list(keyfile, group, "Car", (gint *)&PREFS->car_wg, 4);
581
g_key_file_set_string (keyfile, group, "DateFmt" , PREFS->date_format);
583
g_key_file_set_string (keyfile, group, "PreSymbol" , PREFS->base_cur.prefix_symbol);
584
g_key_file_set_string (keyfile, group, "SufSymbol" , PREFS->base_cur.suffix_symbol);
585
g_key_file_set_string (keyfile, group, "DecChar" , PREFS->base_cur.decimal_char);
586
g_key_file_set_string (keyfile, group, "GroupChar" , PREFS->base_cur.grouping_char);
587
g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->base_cur.frac_digits);
589
g_key_file_set_boolean (keyfile, group, "UKUnits" , PREFS->imperial_unit);
592
g_key_file_set_integer (keyfile, group, "DefRange", PREFS->filter_range);
596
g_key_file_set_boolean (keyfile, group, "Active" , PREFS->euro_active);
597
g_key_file_set_integer (keyfile, group, "Country", PREFS->euro_country);
599
g_ascii_dtostr(ratestr, 63, PREFS->euro_value);
600
g_key_file_set_string (keyfile, group, "ChangeRate", ratestr);
601
g_key_file_set_string (keyfile, group, "PreSymbol" , PREFS->minor_cur.prefix_symbol);
602
g_key_file_set_string (keyfile, group, "SufSymbol" , PREFS->minor_cur.suffix_symbol);
603
g_key_file_set_string (keyfile, group, "DecChar" , PREFS->minor_cur.decimal_char);
604
g_key_file_set_string (keyfile, group, "GroupChar" , PREFS->minor_cur.grouping_char);
605
g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->minor_cur.frac_digits);
609
g_key_file_set_boolean (keyfile, group, "StatByAmount", PREFS->stat_byamount);
610
g_key_file_set_boolean (keyfile, group, "StatDetail" , PREFS->stat_showdetail);
611
g_key_file_set_boolean (keyfile, group, "StatRate" , PREFS->stat_showrate);
612
g_key_file_set_boolean (keyfile, group, "BudgDetail" , PREFS->budg_showdetail);
615
//g_key_file_set_boolean (keyfile, group, "Legend", PREFS->chart_legend);
617
//g_key_file_set_string (keyfile, group, "", PREFS->);
618
//g_key_file_set_boolean (keyfile, group, "", PREFS->);
619
//g_key_file_set_integer (keyfile, group, "", PREFS->);
621
gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
623
//DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
625
filename = g_strdup_printf("%s/.homebank/preferences", g_get_home_dir ());
626
g_file_set_contents(filename, contents, length, NULL);
630
g_key_file_free (keyfile);