]> Dogcows Code - chaz/homebank/blob - src/hb-preferences.c
add plugin engine (supports C and Perl plugins)
[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 g_strfreev(PREFS->ext_path);
286 g_list_free_full(PREFS->ext_whitelist, g_free);
287
288 memset(PREFS, 0, sizeof(struct Preferences));
289 }
290
291
292 void homebank_pref_setdefault(void)
293 {
294 gint i;
295
296 DB( g_print("\n[preferences] pref init\n") );
297
298 homebank_pref_free();
299
300 PREFS->language = NULL;
301
302 PREFS->date_format = g_strdup(DEFAULT_FORMAT_DATE);
303
304 PREFS->path_hbfile = g_strdup_printf("%s", g_get_home_dir ());
305 PREFS->path_import = g_strdup_printf("%s", g_get_home_dir ());
306 PREFS->path_export = g_strdup_printf("%s", g_get_home_dir ());
307 //PREFS->path_navigator = g_strdup(DEFAULT_PATH_NAVIGATOR);
308
309 PREFS->showsplash = TRUE;
310 PREFS->loadlast = TRUE;
311 PREFS->appendscheduled = FALSE;
312
313 PREFS->heritdate = FALSE;
314 PREFS->hidereconciled = FALSE;
315
316 PREFS->toolbar_style = 4; //text beside icons
317 PREFS->custom_colors = TRUE;
318 PREFS->color_exp = g_strdup(DEFAULT_EXP_COLOR);
319 PREFS->color_inc = g_strdup(DEFAULT_INC_COLOR);
320 PREFS->color_warn = g_strdup(DEFAULT_WARN_COLOR);
321 PREFS->rules_hint = FALSE;
322
323 /* fiscal year */
324 PREFS->fisc_year_day = 1;
325 PREFS->fisc_year_month = 1;
326
327 /* windows position/size */
328 homebank_pref_init_wingeometry(&PREFS->wal_wg, 0, 0, 1024, 600);
329 homebank_pref_init_wingeometry(&PREFS->acc_wg, 0, 0, 1024, 600);
330 homebank_pref_init_wingeometry(&PREFS->sta_wg, 0, 0, 800, 494);
331 homebank_pref_init_wingeometry(&PREFS->tme_wg, 0, 0, 800, 494);
332 homebank_pref_init_wingeometry(&PREFS->ove_wg, 0, 0, 800, 494);
333 homebank_pref_init_wingeometry(&PREFS->bud_wg, 0, 0, 800, 494);
334 homebank_pref_init_wingeometry(&PREFS->cst_wg, 0, 0, 800, 494);
335
336 homebank_pref_init_monetary();
337
338 PREFS->wal_toolbar = TRUE;
339 PREFS->wal_spending = TRUE;
340 PREFS->wal_upcoming = TRUE;
341 PREFS->wal_vpaned = 600/2;
342 PREFS->wal_hpaned = 1024/2;
343
344
345
346 i = 0;
347 /* prior v4.5
348 PREFS->lst_ope_columns[i++] = LST_DSPOPE_STATUS;
349 PREFS->lst_ope_columns[i++] = LST_DSPOPE_DATE;
350 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INFO;
351 PREFS->lst_ope_columns[i++] = LST_DSPOPE_PAYEE;
352 PREFS->lst_ope_columns[i++] = LST_DSPOPE_WORDING;
353 PREFS->lst_ope_columns[i++] = -LST_DSPOPE_AMOUNT;
354 PREFS->lst_ope_columns[i++] = LST_DSPOPE_EXPENSE;
355 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INCOME;
356 PREFS->lst_ope_columns[i++] = LST_DSPOPE_CATEGORY;
357 PREFS->lst_ope_columns[i++] = LST_DSPOPE_TAGS;
358 */
359 PREFS->lst_ope_columns[i++] = LST_DSPOPE_STATUS; //always displayed
360 PREFS->lst_ope_columns[i++] = LST_DSPOPE_DATE; //always displayed
361 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INFO;
362 PREFS->lst_ope_columns[i++] = LST_DSPOPE_PAYEE;
363 PREFS->lst_ope_columns[i++] = LST_DSPOPE_CATEGORY;
364 PREFS->lst_ope_columns[i++] = LST_DSPOPE_TAGS;
365 PREFS->lst_ope_columns[i++] = -LST_DSPOPE_AMOUNT;
366 PREFS->lst_ope_columns[i++] = LST_DSPOPE_EXPENSE;
367 PREFS->lst_ope_columns[i++] = LST_DSPOPE_INCOME;
368 PREFS->lst_ope_columns[i++] = LST_DSPOPE_BALANCE;
369 PREFS->lst_ope_columns[i++] = LST_DSPOPE_WORDING;
370
371 PREFS->lst_ope_sort_id = LST_DSPOPE_DATE;
372 PREFS->lst_ope_sort_order = GTK_SORT_ASCENDING;
373
374
375 //PREFS->base_cur.nbdecimal = 2;
376 //PREFS->base_cur.separator = TRUE;
377
378 PREFS->date_range_wal = FLT_RANGE_LASTMONTH;
379 PREFS->date_range_txn = FLT_RANGE_LAST12MONTHS;
380 PREFS->date_range_rep = FLT_RANGE_THISYEAR;
381
382
383 //todo: add intelligence here
384 PREFS->euro_active = FALSE;
385
386 PREFS->euro_country = 0;
387 PREFS->euro_value = 1.0;
388 //PREFS->euro_nbdec = 2;
389 //PREFS->euro_thsep = TRUE;
390 //PREFS->euro_symbol = g_strdup("??");
391
392 PREFS->stat_byamount = FALSE;
393 PREFS->stat_showdetail = FALSE;
394 PREFS->stat_showrate = FALSE;
395 PREFS->budg_showdetail = FALSE;
396 PREFS->report_color_scheme = CHART_COLMAP_HOMEBANK;
397
398 PREFS->chart_legend = FALSE;
399
400 PREFS->vehicle_unit_ismile = FALSE;
401 PREFS->vehicle_unit_isgal = FALSE;
402
403 gchar** plugin_path = g_new0(gchar**, 4);
404 i = 0;
405 const gchar* env = g_getenv("HOMEBANK_PLUGINS");
406 if (env) {
407 if (g_path_is_absolute(env)) {
408 plugin_path[i++] = g_strdup(env);
409 } else {
410 gchar* cur = g_get_current_dir();
411 plugin_path[i++] = g_build_filename(cur, env, NULL);
412 g_free(cur);
413 }
414 }
415 plugin_path[i++] = g_build_filename(homebank_app_get_config_dir(), "plugins", NULL);
416 plugin_path[i++] = g_build_filename(homebank_app_get_pkglib_dir(), "plugins", NULL);
417 PREFS->ext_path = plugin_path;
418 PREFS->ext_whitelist = NULL;
419
420 _homebank_pref_createformat();
421 _homebank_pref_init_measurement_units();
422
423 }
424
425
426 /*
427 ** load preference from homedir/.homebank (HB_DATA_PATH)
428 */
429 static void homebank_pref_get_wingeometry(
430 GKeyFile *key_file,
431 const gchar *group_name,
432 const gchar *key,
433 struct WinGeometry *storage)
434 {
435 if( g_key_file_has_key(key_file, group_name, key, NULL) )
436 {
437 gint *wg;
438 gsize length;
439
440 wg = g_key_file_get_integer_list(key_file, group_name, key, &length, NULL);
441 memcpy(storage, wg, 5*sizeof(gint));
442 g_free(wg);
443 // #606613 ensure left/top to be > 0
444 if(storage->l < 0)
445 storage->l = 0;
446
447 if(storage->t < 0)
448 storage->t = 0;
449 }
450 }
451
452 static void homebank_pref_get_boolean(
453 GKeyFile *key_file,
454 const gchar *group_name,
455 const gchar *key,
456 gboolean *storage)
457 {
458
459 if( g_key_file_has_key(key_file, group_name, key, NULL) )
460 {
461 *storage = g_key_file_get_boolean(key_file, group_name, key, NULL);
462 }
463 }
464
465 static void homebank_pref_get_integer(
466 GKeyFile *key_file,
467 const gchar *group_name,
468 const gchar *key,
469 gint *storage)
470 {
471
472 DB( g_print(" search %s in %s\n", key, group_name) );
473
474
475 if( g_key_file_has_key(key_file, group_name, key, NULL) )
476 {
477 *storage = g_key_file_get_integer(key_file, group_name, key, NULL);
478
479 DB( g_print(" store integer %d for %s at %x\n", *storage, key, *storage) );
480 }
481 }
482
483 static void homebank_pref_get_guint32(
484 GKeyFile *key_file,
485 const gchar *group_name,
486 const gchar *key,
487 guint32 *storage)
488 {
489
490 if( g_key_file_has_key(key_file, group_name, key, NULL) )
491 {
492 *storage = g_key_file_get_integer(key_file, group_name, key, NULL);
493 }
494 }
495
496 static void homebank_pref_get_short(
497 GKeyFile *key_file,
498 const gchar *group_name,
499 const gchar *key,
500 gshort *storage)
501 {
502
503 if( g_key_file_has_key(key_file, group_name, key, NULL) )
504 {
505 *storage = (gshort)g_key_file_get_integer(key_file, group_name, key, NULL);
506 }
507 }
508
509 static void homebank_pref_get_string(
510 GKeyFile *key_file,
511 const gchar *group_name,
512 const gchar *key,
513 gchar **storage)
514 {
515 gchar *string;
516
517 if( g_key_file_has_key(key_file, group_name, key, NULL) )
518 {
519 /* free any previous string */
520 if( *storage != NULL )
521 {
522 //DB( g_print(" storage was not null, freeing\n") );
523
524 g_free(*storage);
525 }
526
527 *storage = NULL;
528
529 string = g_key_file_get_string(key_file, group_name, key, NULL);
530 if( string != NULL )
531 {
532 *storage = g_strdup(string);
533
534 //DB( g_print(" store '%s' for %s at %x\n", string, key, *storage) );
535 }
536 }
537
538 /*
539 if (error)
540 {
541 g_warning ("error: %s\n", error->message);
542 g_error_free(error);
543 error = NULL;
544 }
545 */
546
547
548 }
549
550
551 static void homebank_pref_currfmt_convert(struct CurrencyFmt *cur, gchar *prefix, gchar *suffix)
552 {
553
554 if( (prefix != NULL) && (strlen(prefix) > 0) )
555 {
556 cur->symbol = g_strdup(prefix);
557 cur->is_prefix = TRUE;
558 }
559 else if( (suffix != NULL) )
560 {
561 cur->symbol = g_strdup(suffix);
562 cur->is_prefix = FALSE;
563 }
564 }
565
566
567 gboolean homebank_pref_load(void)
568 {
569 GKeyFile *keyfile;
570 gboolean retval = FALSE;
571 gchar *group, *filename;
572 guint32 version;
573 GError *error = NULL;
574
575 DB( g_print("\n[preferences] pref load\n") );
576
577 keyfile = g_key_file_new();
578 if(keyfile)
579 {
580 filename = g_build_filename(homebank_app_get_config_dir(), "preferences", NULL );
581
582 DB( g_print(" - filename: %s\n", filename) );
583
584
585 if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
586 {
587
588 group = "General";
589
590 DB( g_print(" -> ** General\n") );
591
592 //since 4.51 version is integer
593 homebank_pref_get_guint32 (keyfile, group, "Version", &version);
594 if(version == 0) // old double number
595 {
596 gdouble v = g_key_file_get_double (keyfile, group, "Version", NULL);
597 version = (guint32)(v * 10);
598 }
599
600 DB( g_print(" - version: %d\n", version) );
601
602 homebank_pref_get_string(keyfile, group, "Language", &PREFS->language);
603
604 homebank_pref_get_short(keyfile, group, "BarStyle" , &PREFS->toolbar_style);
605
606 if(version <= 6 && PREFS->toolbar_style == 0) // force system to text beside
607 {
608 PREFS->toolbar_style = 4;
609 }
610
611 if(version <= 2) // retrieve old settings
612 {
613 guint32 color;
614
615 homebank_pref_get_guint32(keyfile, group, "ColorExp" , &color);
616 g_free(PREFS->color_exp);
617 PREFS->color_exp = g_strdup_printf("#%06x", color);
618
619 homebank_pref_get_guint32(keyfile, group, "ColorInc" , &color);
620 g_free(PREFS->color_inc);
621 PREFS->color_inc = g_strdup_printf("#%06x", color);
622
623 homebank_pref_get_guint32(keyfile, group, "ColorWarn", &color);
624 g_free(PREFS->color_warn);
625 PREFS->color_warn = g_strdup_printf("#%06x", color);
626 }
627 else
628 {
629 homebank_pref_get_boolean(keyfile, group, "CustomColors", &PREFS->custom_colors);
630
631 homebank_pref_get_string(keyfile, group, "ColorExp" , &PREFS->color_exp);
632 homebank_pref_get_string(keyfile, group, "ColorInc" , &PREFS->color_inc);
633 homebank_pref_get_string(keyfile, group, "ColorWarn", &PREFS->color_warn);
634
635 homebank_pref_get_boolean(keyfile, group, "RulesHint", &PREFS->rules_hint);
636 }
637
638 DB( g_print(" - color exp: %s\n", PREFS->color_exp) );
639 DB( g_print(" - color inc: %s\n", PREFS->color_inc) );
640 DB( g_print(" - color wrn: %s\n", PREFS->color_warn) );
641
642
643 homebank_pref_get_string(keyfile, group, "WalletPath", &PREFS->path_hbfile);
644 homebank_pref_get_string(keyfile, group, "ImportPath", &PREFS->path_import);
645 homebank_pref_get_string(keyfile, group, "ExportPath", &PREFS->path_export);
646
647 homebank_pref_get_boolean(keyfile, group, "ShowSplash", &PREFS->showsplash);
648 homebank_pref_get_boolean(keyfile, group, "LoadLast", &PREFS->loadlast);
649 homebank_pref_get_boolean(keyfile, group, "AppendScheduled", &PREFS->appendscheduled);
650
651 homebank_pref_get_boolean(keyfile, group, "HeritDate", &PREFS->heritdate);
652 homebank_pref_get_boolean(keyfile, group, "HideReconciled", &PREFS->hidereconciled);
653
654 if( g_key_file_has_key(keyfile, group, "ColumnsOpe", NULL) )
655 {
656 gboolean *bsrc;
657 gint *src, i;
658 gsize length;
659
660 if(version <= 2) //retrieve old 0.1 or 0.2 visibility boolean
661 {
662 bsrc = g_key_file_get_boolean_list(keyfile, group, "ColumnsOpe", &length, &error);
663 if( length == NUM_LST_DSPOPE-1 )
664 {
665 //and convert
666 for(i=0; i<NUM_LST_DSPOPE-1 ; i++)
667 {
668 PREFS->lst_ope_columns[i] = (bsrc[i] == TRUE) ? i+1 : -(i+1);
669 }
670 }
671 g_free(bsrc);
672 }
673 else
674 {
675 src = g_key_file_get_integer_list(keyfile, group, "ColumnsOpe", &length, &error);
676
677 DB( g_print(" - length %d (max=%d)\n", length, NUM_LST_DSPOPE) );
678
679 if( length == NUM_LST_DSPOPE-1 )
680 {
681 DB( g_print(" - copying column order from pref file\n") );
682 memcpy(PREFS->lst_ope_columns, src, length*sizeof(gint));
683 }
684 else
685 {
686 if(version <= 7)
687 {
688 if( length == NUM_LST_DSPOPE-2 ) //1 less column before v4.5.1
689 {
690 DB( g_print(" - upgrade from v7\n") );
691 DB( g_print(" - copying column order from pref file\n") );
692 memcpy(PREFS->lst_ope_columns, src, length*sizeof(gint));
693 PREFS->lst_ope_columns[10] = LST_DSPOPE_BALANCE;
694 }
695 }
696
697 }
698
699 g_free(src);
700 }
701
702 }
703
704 homebank_pref_get_integer(keyfile, group, "OpeSortId", &PREFS->lst_ope_sort_id);
705 homebank_pref_get_integer(keyfile, group, "OpeSortOrder", &PREFS->lst_ope_sort_order);
706
707 DB( g_print(" - set sort to %d %d\n", PREFS->lst_ope_sort_id, PREFS->lst_ope_sort_order) );
708
709 homebank_pref_get_short(keyfile, group, "FiscYearDay", &PREFS->fisc_year_day);
710 homebank_pref_get_short(keyfile, group, "FiscYearMonth", &PREFS->fisc_year_month);
711
712
713 group = "Windows";
714
715 DB( g_print(" -> ** Windows\n") );
716
717 homebank_pref_get_wingeometry(keyfile, group, "Wal", &PREFS->wal_wg);
718 homebank_pref_get_wingeometry(keyfile, group, "Acc", &PREFS->acc_wg);
719 homebank_pref_get_wingeometry(keyfile, group, "Sta", &PREFS->sta_wg);
720 homebank_pref_get_wingeometry(keyfile, group, "Tme", &PREFS->tme_wg);
721 homebank_pref_get_wingeometry(keyfile, group, "Ove", &PREFS->ove_wg);
722 homebank_pref_get_wingeometry(keyfile, group, "Bud", &PREFS->bud_wg);
723 homebank_pref_get_wingeometry(keyfile, group, "Car", &PREFS->cst_wg);
724 if(version <= 7) //set maximize to 0
725 {
726 PREFS->wal_wg.s = 0;
727 PREFS->acc_wg.s = 0;
728 PREFS->sta_wg.s = 0;
729 PREFS->tme_wg.s = 0;
730 PREFS->ove_wg.s = 0;
731 PREFS->bud_wg.s = 0;
732 PREFS->cst_wg.s = 0;
733 }
734 homebank_pref_get_integer(keyfile, group, "WalVPaned", &PREFS->wal_vpaned);
735 homebank_pref_get_integer(keyfile, group, "WalHPaned", &PREFS->wal_hpaned);
736 homebank_pref_get_boolean(keyfile, group, "WalToolbar", &PREFS->wal_toolbar);
737 homebank_pref_get_boolean(keyfile, group, "WalSpending", &PREFS->wal_spending);
738 homebank_pref_get_boolean(keyfile, group, "WalUpcoming", &PREFS->wal_upcoming);
739
740
741 group = "Format";
742
743 DB( g_print(" -> ** Format\n") );
744
745 homebank_pref_get_string(keyfile, group, "DateFmt", &PREFS->date_format);
746
747 if(version <= 1)
748 {
749 //retrieve old 0.1 preferences
750 homebank_pref_get_short(keyfile, group, "NumNbDec", &PREFS->base_cur.frac_digits);
751 //PREFS->base_cur.separator = g_key_file_get_boolean (keyfile, group, "NumSep", NULL);
752 }
753 else
754 {
755 if(version < 460)
756 {
757 gchar *prefix = NULL;
758 gchar *suffix = NULL;
759
760 homebank_pref_get_string(keyfile, group, "PreSymbol", &prefix);
761 homebank_pref_get_string(keyfile, group, "SufSymbol", &suffix);
762 homebank_pref_currfmt_convert(&PREFS->base_cur, prefix, suffix);
763 g_free(prefix);
764 g_free(suffix);
765 }
766 else
767 {
768 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->base_cur.symbol);
769 homebank_pref_get_boolean(keyfile, group, "IsPrefix", &PREFS->base_cur.is_prefix);
770 }
771 homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->base_cur.decimal_char);
772 homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->base_cur.grouping_char);
773 homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->base_cur.frac_digits);
774
775 //fix 378992/421228
776 if( PREFS->base_cur.frac_digits > MAX_FRAC_DIGIT )
777 PREFS->base_cur.frac_digits = MAX_FRAC_DIGIT;
778 }
779
780 if(version < 460)
781 {
782 gboolean useimperial;
783
784 homebank_pref_get_boolean(keyfile, group, "UKUnits", &useimperial);
785 if(useimperial)
786 {
787 PREFS->vehicle_unit_ismile = TRUE;
788 PREFS->vehicle_unit_isgal = TRUE;
789 }
790 }
791
792 homebank_pref_get_boolean(keyfile, group, "UnitIsMile", &PREFS->vehicle_unit_ismile);
793 homebank_pref_get_boolean(keyfile, group, "UnitIsGal", &PREFS->vehicle_unit_isgal);
794
795
796 group = "Filter";
797
798 DB( g_print(" -> ** Filter\n") );
799
800 homebank_pref_get_integer(keyfile, group, "DateRangeWal", &PREFS->date_range_wal);
801 homebank_pref_get_integer(keyfile, group, "DateRangeTxn", &PREFS->date_range_txn);
802 homebank_pref_get_integer(keyfile, group, "DateRangeRep", &PREFS->date_range_rep);
803
804 if(version <= 7)
805 {
806 // shift date range >= 5, since we inserted a new one at position 5
807 if(PREFS->date_range_wal >= FLT_RANGE_LASTYEAR)
808 PREFS->date_range_wal++;
809 if(PREFS->date_range_txn >= FLT_RANGE_LASTYEAR)
810 PREFS->date_range_txn++;
811 if(PREFS->date_range_rep >= FLT_RANGE_LASTYEAR)
812 PREFS->date_range_rep++;
813 }
814
815
816 group = "Euro";
817
818 DB( g_print(" -> ** Euro\n") );
819
820 //homebank_pref_get_string(keyfile, group, "DefCurrency" , &PREFS->curr_default);
821
822 homebank_pref_get_boolean(keyfile, group, "Active", &PREFS->euro_active);
823 homebank_pref_get_integer(keyfile, group, "Country", &PREFS->euro_country);
824
825 gchar *ratestr = g_key_file_get_string (keyfile, group, "ChangeRate", NULL);
826 if(ratestr != NULL) PREFS->euro_value = g_ascii_strtod(ratestr, NULL);
827
828 if(version <= 1)
829 {
830 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->minor_cur.symbol);
831 PREFS->minor_cur.frac_digits = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
832
833 //PREFS->euro_nbdec = g_key_file_get_integer (keyfile, group, "NBDec", NULL);
834 //PREFS->euro_thsep = g_key_file_get_boolean (keyfile, group, "Sep", NULL);
835 //gchar *tmpstr = g_key_file_get_string (keyfile, group, "Symbol", &error);
836 }
837 else
838 {
839 if(version < 460)
840 {
841 gchar *prefix = NULL;
842 gchar *suffix = NULL;
843
844 homebank_pref_get_string(keyfile, group, "PreSymbol", &prefix);
845 homebank_pref_get_string(keyfile, group, "SufSymbol", &suffix);
846 homebank_pref_currfmt_convert(&PREFS->minor_cur, prefix, suffix);
847 g_free(prefix);
848 g_free(suffix);
849 }
850 else
851 {
852 homebank_pref_get_string(keyfile, group, "Symbol", &PREFS->minor_cur.symbol);
853 homebank_pref_get_boolean(keyfile, group, "IsPrefix", &PREFS->minor_cur.is_prefix);
854 }
855 homebank_pref_get_string(keyfile, group, "DecChar" , &PREFS->minor_cur.decimal_char);
856 homebank_pref_get_string(keyfile, group, "GroupChar", &PREFS->minor_cur.grouping_char);
857 homebank_pref_get_short(keyfile, group, "FracDigits", &PREFS->minor_cur.frac_digits);
858
859 //fix 378992/421228
860 if( PREFS->minor_cur.frac_digits > MAX_FRAC_DIGIT )
861 PREFS->minor_cur.frac_digits = MAX_FRAC_DIGIT;
862
863 }
864
865 //PREFS->euro_symbol = g_locale_to_utf8(tmpstr, -1, NULL, NULL, NULL);
866
867 group = "Report";
868
869 DB( g_print(" -> ** Report\n") );
870
871 homebank_pref_get_boolean(keyfile, group, "StatByAmount", &PREFS->stat_byamount);
872 homebank_pref_get_boolean(keyfile, group, "StatDetail", &PREFS->stat_showdetail);
873 homebank_pref_get_boolean(keyfile, group, "StatRate", &PREFS->stat_showrate);
874 homebank_pref_get_boolean(keyfile, group, "BudgDetail", &PREFS->budg_showdetail);
875 homebank_pref_get_integer(keyfile, group, "ColorScheme", &PREFS->report_color_scheme);
876
877 group = "Exchange";
878
879 DB( g_print(" -> ** Exchange\n") );
880
881 homebank_pref_get_integer(keyfile, group, "DateFmt", &PREFS->dtex_datefmt);
882 homebank_pref_get_integer(keyfile, group, "OfxMemo", &PREFS->dtex_ofxmemo);
883
884
885 //group = "Chart";
886 //PREFS->chart_legend = g_key_file_get_boolean (keyfile, group, "Legend", NULL);
887
888
889 group = "Plugins";
890 {
891 DB( g_print(" -> ** Plugins\n") );
892
893 gchar** strv = g_key_file_get_string_list(keyfile, group, "Path", NULL, NULL);
894 if (strv) {
895 g_strfreev(PREFS->ext_path);
896 PREFS->ext_path = strv;
897 }
898
899 strv = g_key_file_get_string_list(keyfile, group, "Whitelist", NULL, NULL);
900 if (strv) {
901 gchar** it;
902 for (it = strv; it && *it; ++it) {
903 PREFS->ext_whitelist = g_list_append(PREFS->ext_whitelist, g_strdup(*it));
904 }
905 g_strfreev(strv);
906 }
907 }
908
909
910 /*
911 #if MYDEBUG == 1
912 gsize length;
913 gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
914 //g_print(" keyfile:\n%s\n len=%d\n", contents, length);
915 g_free(contents);
916 #endif
917 */
918
919 }
920 g_free(filename);
921 g_key_file_free (keyfile);
922
923 _homebank_pref_createformat();
924 _homebank_pref_init_measurement_units();
925 }
926
927 return retval;
928 }
929
930 static void homebank_pref_set_string(
931 GKeyFile *key_file,
932 const gchar *group_name,
933 const gchar *key,
934 gchar *string)
935 {
936
937 DB( g_print(" - homebank_pref_set_string :: group='%s' key='%s' value='%s'\n", group_name, key, string) );
938
939 if( string != NULL && *string != '\0')
940 g_key_file_set_string (key_file, group_name, key, string);
941 else
942 g_key_file_set_string (key_file, group_name, key, "");
943
944 }
945
946
947 /*
948 ** save preference to homedir/.homebank (HB_DATA_PATH)
949 */
950 gboolean homebank_pref_save(void)
951 {
952 GKeyFile *keyfile;
953 gboolean retval = FALSE;
954 gchar *group, *filename;
955 gsize length;
956
957 DB( g_print("\n[preferences] pref save\n") );
958
959 keyfile = g_key_file_new();
960 if(keyfile )
961 {
962
963 DB( g_print(" -> ** general\n") );
964
965
966 group = "General";
967 g_key_file_set_integer (keyfile, group, "Version", PREF_VERSION);
968
969 homebank_pref_set_string (keyfile, group, "Language", PREFS->language);
970
971 g_key_file_set_integer (keyfile, group, "BarStyle", PREFS->toolbar_style);
972 //g_key_file_set_integer (keyfile, group, "BarImageSize", PREFS->image_size);
973
974 g_key_file_set_boolean (keyfile, group, "CustomColors", PREFS->custom_colors);
975 g_key_file_set_string (keyfile, group, "ColorExp" , PREFS->color_exp);
976 g_key_file_set_string (keyfile, group, "ColorInc" , PREFS->color_inc);
977 g_key_file_set_string (keyfile, group, "ColorWarn", PREFS->color_warn);
978
979 g_key_file_set_boolean (keyfile, group, "RulesHint", PREFS->rules_hint);
980
981
982 homebank_pref_set_string (keyfile, group, "WalletPath" , PREFS->path_hbfile);
983 homebank_pref_set_string (keyfile, group, "ImportPath" , PREFS->path_import);
984 homebank_pref_set_string (keyfile, group, "ExportPath" , PREFS->path_export);
985 //g_key_file_set_string (keyfile, group, "NavigatorPath", PREFS->path_navigator);
986
987 g_key_file_set_boolean (keyfile, group, "ShowSplash", PREFS->showsplash);
988 g_key_file_set_boolean (keyfile, group, "LoadLast", PREFS->loadlast);
989 g_key_file_set_boolean (keyfile, group, "AppendScheduled", PREFS->appendscheduled);
990
991 g_key_file_set_boolean (keyfile, group, "HeritDate", PREFS->heritdate);
992 g_key_file_set_boolean (keyfile, group, "HideReconciled", PREFS->hidereconciled);
993
994 g_key_file_set_integer_list(keyfile, group, "ColumnsOpe", PREFS->lst_ope_columns, NUM_LST_DSPOPE-1);
995 g_key_file_set_integer (keyfile, group, "OpeSortId" , PREFS->lst_ope_sort_id);
996 g_key_file_set_integer (keyfile, group, "OpeSortOrder" , PREFS->lst_ope_sort_order);
997
998 g_key_file_set_integer (keyfile, group, "FiscYearDay" , PREFS->fisc_year_day);
999 g_key_file_set_integer (keyfile, group, "FiscYearMonth" , PREFS->fisc_year_month);
1000
1001 // added v3.4
1002 DB( g_print(" -> ** windows\n") );
1003
1004 group = "Windows";
1005 g_key_file_set_integer_list(keyfile, group, "Wal", (gint *)&PREFS->wal_wg, 5);
1006 g_key_file_set_integer_list(keyfile, group, "Acc", (gint *)&PREFS->acc_wg, 5);
1007 g_key_file_set_integer_list(keyfile, group, "Sta", (gint *)&PREFS->sta_wg, 5);
1008 g_key_file_set_integer_list(keyfile, group, "Tme", (gint *)&PREFS->tme_wg, 5);
1009 g_key_file_set_integer_list(keyfile, group, "Ove", (gint *)&PREFS->ove_wg, 5);
1010 g_key_file_set_integer_list(keyfile, group, "Bud", (gint *)&PREFS->bud_wg, 5);
1011 g_key_file_set_integer_list(keyfile, group, "Car", (gint *)&PREFS->cst_wg, 5);
1012
1013 g_key_file_set_integer (keyfile, group, "WalVPaned" , PREFS->wal_vpaned);
1014 g_key_file_set_integer (keyfile, group, "WalHPaned" , PREFS->wal_hpaned);
1015 g_key_file_set_boolean (keyfile, group, "WalToolbar", PREFS->wal_toolbar);
1016 g_key_file_set_boolean (keyfile, group, "WalSpending", PREFS->wal_spending);
1017 g_key_file_set_boolean (keyfile, group, "WalUpcoming", PREFS->wal_upcoming);
1018
1019
1020 DB( g_print(" -> ** format\n") );
1021
1022 group = "Format";
1023 homebank_pref_set_string (keyfile, group, "DateFmt" , PREFS->date_format);
1024
1025 homebank_pref_set_string (keyfile, group, "Symbol" , PREFS->base_cur.symbol);
1026 g_key_file_set_boolean (keyfile, group, "IsPrefix" , PREFS->base_cur.is_prefix);
1027 homebank_pref_set_string (keyfile, group, "DecChar" , PREFS->base_cur.decimal_char);
1028 homebank_pref_set_string (keyfile, group, "GroupChar" , PREFS->base_cur.grouping_char);
1029 g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->base_cur.frac_digits);
1030
1031 //g_key_file_set_boolean (keyfile, group, "UKUnits" , PREFS->imperial_unit);
1032 g_key_file_set_boolean (keyfile, group, "UnitIsMile" , PREFS->vehicle_unit_ismile);
1033 g_key_file_set_boolean (keyfile, group, "UnitIsGal" , PREFS->vehicle_unit_isgal);
1034
1035
1036 DB( g_print(" -> ** filter\n") );
1037
1038 group = "Filter";
1039 g_key_file_set_integer (keyfile, group, "DateRangeWal", PREFS->date_range_wal);
1040 g_key_file_set_integer (keyfile, group, "DateRangeTxn", PREFS->date_range_txn);
1041 g_key_file_set_integer (keyfile, group, "DateRangeRep", PREFS->date_range_rep);
1042
1043 DB( g_print(" -> ** euro\n") );
1044
1045 //euro options
1046 group = "Euro";
1047
1048 //homebank_pref_set_string(keyfile, group, "DefCurrency" , PREFS->curr_default);
1049
1050 g_key_file_set_boolean (keyfile, group, "Active" , PREFS->euro_active);
1051 if( PREFS->euro_active )
1052 {
1053 g_key_file_set_integer (keyfile, group, "Country", PREFS->euro_country);
1054 gchar ratestr[64];
1055 g_ascii_dtostr(ratestr, 63, PREFS->euro_value);
1056 homebank_pref_set_string (keyfile, group, "ChangeRate", ratestr);
1057 homebank_pref_set_string (keyfile, group, "Symbol" , PREFS->minor_cur.symbol);
1058 g_key_file_set_boolean (keyfile, group, "IsPrefix" , PREFS->minor_cur.is_prefix);
1059 homebank_pref_set_string (keyfile, group, "DecChar" , PREFS->minor_cur.decimal_char);
1060 homebank_pref_set_string (keyfile, group, "GroupChar" , PREFS->minor_cur.grouping_char);
1061 g_key_file_set_integer (keyfile, group, "FracDigits", PREFS->minor_cur.frac_digits);
1062 }
1063
1064 //report options
1065 DB( g_print(" -> ** report\n") );
1066
1067 group = "Report";
1068 g_key_file_set_boolean (keyfile, group, "StatByAmount", PREFS->stat_byamount);
1069 g_key_file_set_boolean (keyfile, group, "StatDetail" , PREFS->stat_showdetail);
1070 g_key_file_set_boolean (keyfile, group, "StatRate" , PREFS->stat_showrate);
1071 g_key_file_set_boolean (keyfile, group, "BudgDetail" , PREFS->budg_showdetail);
1072 g_key_file_set_integer (keyfile, group, "ColorScheme" , PREFS->report_color_scheme);
1073
1074
1075 group = "Exchange";
1076 g_key_file_set_integer (keyfile, group, "DateFmt", PREFS->dtex_datefmt);
1077 g_key_file_set_integer (keyfile, group, "OfxMemo", PREFS->dtex_ofxmemo);
1078
1079 //group = "Chart";
1080 //g_key_file_set_boolean (keyfile, group, "Legend", PREFS->chart_legend);
1081
1082 group = "Plugins";
1083 {
1084 g_key_file_set_string_list(keyfile, group, "Path", (const gchar* const*)PREFS->ext_path, g_strv_length(PREFS->ext_path));
1085
1086 gsize len = g_list_length(PREFS->ext_whitelist);
1087 gchar** strv = g_new0(gchar*, len + 1);
1088 guint i;
1089
1090 for (i = 0; i < len; ++i) {
1091 strv[i] = g_list_nth_data(PREFS->ext_whitelist, i);
1092 }
1093 g_key_file_set_string_list(keyfile, group, "Whitelist", (const gchar* const*)strv, len);
1094 g_free(strv);
1095 }
1096
1097 //g_key_file_set_string (keyfile, group, "", PREFS->);
1098 //g_key_file_set_boolean (keyfile, group, "", PREFS->);
1099 //g_key_file_set_integer (keyfile, group, "", PREFS->);
1100
1101 DB( g_print(" -> ** g_key_file_to_data\n") );
1102
1103 gchar *contents = g_key_file_to_data (keyfile, &length, NULL);
1104
1105 //DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
1106
1107 filename = g_build_filename(homebank_app_get_config_dir(), "preferences", NULL );
1108
1109 DB( g_print(" -> filename: %s\n", filename) );
1110
1111 g_file_set_contents(filename, contents, length, NULL);
1112
1113 DB( g_print(" -> contents: %s\n", contents) );
1114
1115 DB( g_print(" -> freeing filename\n") );
1116 g_free(filename);
1117
1118 DB( g_print(" -> freeing buffer\n") );
1119
1120 g_free(contents);
1121
1122 DB( g_print(" -> freeing keyfile\n") );
1123
1124 g_key_file_free (keyfile);
1125 }
1126
1127 _homebank_pref_createformat();
1128 _homebank_pref_init_measurement_units();
1129
1130 return retval;
1131 }
1132
This page took 0.091622 seconds and 4 git commands to generate.