1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Sort of like the globals file, but values are automatically saved via program use.
24 * Some settings are *not* intended to have any GUI controls.
25 * Other settings be can used to set other GUI elements.
27 * ATM This is implemented using the simple (for me!) GKeyFile API - AKA an .ini file
28 * One might wish to consider the more modern alternative such as:
29 * http://developer.gnome.org/gio/2.26/GSettings.html
30 * Since these settings are 'internal' I have no problem with them *not* being supported
31 * between various Viking versions, should one switch to different API/storage methods.
32 * Indeed even the internal settings themselves can be liable to change.
37 static GKeyFile *keyfile;
39 #define VIKING_INI_FILE "viking.ini"
41 static gboolean settings_load_from_file()
43 GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS;
47 gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
49 if ( !g_key_file_load_from_file ( keyfile, fn, flags, &error ) ) {
50 g_warning ( "%s: %s", error->message, fn );
52 g_error_free ( error );
61 void a_settings_init()
63 keyfile = g_key_file_new();
64 settings_load_from_file();
70 * ATM: The only time settings are saved is on program exit
71 * Could change this to occur on window exit or dialog exit or have memory hash of values...?
73 void a_settings_uninit()
76 gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
79 gchar *keyfilestr = g_key_file_to_data ( keyfile, &size, &error );
82 g_warning ( error->message );
83 g_error_free ( error );
87 g_file_set_contents ( fn, keyfilestr, size, &error );
89 g_warning ( "%s: %s", error->message, fn );
90 g_error_free ( error );
93 g_key_file_free ( keyfile );
96 // ATM, can't see a point in having any more than one group for various settings
97 #define VIKING_SETTINGS_GROUP "viking"
99 static gboolean settings_get_boolean ( const gchar *group, const gchar *name, gboolean *val )
101 GError *error = NULL;
102 gboolean success = TRUE;
103 gboolean bb = g_key_file_get_boolean ( keyfile, group, name, &error );
105 // Only print on debug - as often may have requests for keys not in the file
106 g_debug ( error->message );
107 g_error_free ( error );
114 gboolean a_settings_get_boolean ( const gchar *name, gboolean *val )
116 return settings_get_boolean ( VIKING_SETTINGS_GROUP, name, val );
119 void a_settings_set_boolean ( const gchar *name, gboolean val )
121 g_key_file_set_boolean ( keyfile, VIKING_SETTINGS_GROUP, name, val );
124 static gboolean settings_get_string ( const gchar *group, const gchar *name, gchar **val )
126 GError *error = NULL;
127 gboolean success = TRUE;
128 gchar *str = g_key_file_get_string ( keyfile, group, name, &error );
130 // Only print on debug - as often may have requests for keys not in the file
131 g_debug ( error->message );
132 g_error_free ( error );
139 gboolean a_settings_get_string ( const gchar *name, gchar **val )
141 return settings_get_string ( VIKING_SETTINGS_GROUP, name, val );
144 void a_settings_set_string ( const gchar *name, const gchar *val )
146 g_key_file_set_string ( keyfile, VIKING_SETTINGS_GROUP, name, val );
149 static gboolean settings_get_integer ( const gchar *group, const gchar *name, gint *val )
151 GError *error = NULL;
152 gboolean success = TRUE;
153 gint ii = g_key_file_get_integer ( keyfile, group, name, &error );
155 // Only print on debug - as often may have requests for keys not in the file
156 g_debug ( error->message );
157 g_error_free ( error );
164 gboolean a_settings_get_integer ( const gchar *name, gint *val )
166 return settings_get_integer ( VIKING_SETTINGS_GROUP, name, val );
169 void a_settings_set_integer ( const gchar *name, gint val )
171 g_key_file_set_integer ( keyfile, VIKING_SETTINGS_GROUP, name, val );
174 static gboolean settings_get_double ( const gchar *group, const gchar *name, gdouble *val )
176 GError *error = NULL;
177 gboolean success = TRUE;
178 gdouble dd = g_key_file_get_double ( keyfile, group, name, &error );
180 // Only print on debug - as often may have requests for keys not in the file
181 g_debug ( error->message );
182 g_error_free ( error );
189 gboolean a_settings_get_double ( const gchar *name, gdouble *val )
191 return settings_get_double ( VIKING_SETTINGS_GROUP, name, val );
194 void a_settings_set_double ( const gchar *name, gdouble val )
196 g_key_file_set_double ( keyfile, VIKING_SETTINGS_GROUP, name, val );