]> git.street.me.uk Git - andy/viking.git/blob - src/settings.c
[DOC] Fix some incorrect docbook markup.
[andy/viking.git] / src / settings.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4  *
5  * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22  /*
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.
26   *
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.
33   */
34 #include <glib.h>
35 #include "dir.h"
36
37 static GKeyFile *keyfile;
38
39 #define VIKING_INI_FILE "viking.ini"
40
41 static gboolean settings_load_from_file()
42 {
43         GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS;
44
45         GError *error = NULL;
46
47         gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
48
49         if ( !g_key_file_load_from_file ( keyfile, fn, flags, &error ) ) {
50                 g_warning ( "%s: %s", error->message, fn );
51                 g_free ( fn );
52                 g_error_free ( error );
53                 return FALSE;
54         }
55
56         g_free ( fn );
57
58         return TRUE;
59 }
60
61 void a_settings_init()
62 {
63         keyfile = g_key_file_new();
64         settings_load_from_file();
65 }
66
67 /**
68  * a_settings_uninit:
69  *
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...?
72  */
73 void a_settings_uninit()
74 {
75         GError *error = NULL;
76         gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
77         gsize size;
78
79         gchar *keyfilestr = g_key_file_to_data ( keyfile, &size, &error );
80
81         if ( error ) {
82                 g_warning ( error->message );
83                 g_error_free ( error );
84                 return;
85         }
86
87         g_file_set_contents ( fn, keyfilestr, size, &error );
88         if ( error ) {
89                 g_warning ( "%s: %s", error->message, fn );
90                 g_error_free ( error );
91         }
92
93         g_key_file_free ( keyfile );
94 }
95
96 // ATM, can't see a point in having any more than one group for various settings
97 #define VIKING_SETTINGS_GROUP "viking"
98
99 static gboolean settings_get_boolean ( const gchar *group, const gchar *name, gboolean *val )
100 {
101         GError *error = NULL;
102         gboolean success = TRUE;
103         gboolean bb = g_key_file_get_boolean ( keyfile, group, name, &error );
104         if ( 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 );
108                 success = FALSE;
109         }
110         *val = bb;
111         return success;
112 }
113
114 gboolean a_settings_get_boolean ( const gchar *name, gboolean *val )
115 {
116         return settings_get_boolean ( VIKING_SETTINGS_GROUP, name, val );
117 }
118
119 void a_settings_set_boolean ( const gchar *name, gboolean val )
120 {
121         g_key_file_set_boolean ( keyfile, VIKING_SETTINGS_GROUP, name, val );
122 }
123
124 static gboolean settings_get_string ( const gchar *group, const gchar *name, gchar **val )
125 {
126         GError *error = NULL;
127         gboolean success = TRUE;
128         gchar *str = g_key_file_get_string ( keyfile, group, name, &error );
129         if ( 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 );
133                 success = FALSE;
134         }
135         *val = str;
136         return success;
137 }
138
139 gboolean a_settings_get_string ( const gchar *name, gchar **val )
140 {
141         return settings_get_string ( VIKING_SETTINGS_GROUP, name, val );
142 }
143
144 void a_settings_set_string ( const gchar *name, const gchar *val )
145 {
146         g_key_file_set_string ( keyfile, VIKING_SETTINGS_GROUP, name, val );
147 }
148
149 static gboolean settings_get_integer ( const gchar *group, const gchar *name, gint *val )
150 {
151         GError *error = NULL;
152         gboolean success = TRUE;
153         gint ii = g_key_file_get_integer ( keyfile, group, name, &error );
154         if ( 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 );
158                 success = FALSE;
159         }
160         *val = ii;
161         return success;
162 }
163
164 gboolean a_settings_get_integer ( const gchar *name, gint *val )
165 {
166         return settings_get_integer ( VIKING_SETTINGS_GROUP, name, val );
167 }
168
169 void a_settings_set_integer ( const gchar *name, gint val )
170 {
171         g_key_file_set_integer ( keyfile, VIKING_SETTINGS_GROUP, name, val );
172 }
173
174 static gboolean settings_get_double ( const gchar *group, const gchar *name, gdouble *val )
175 {
176         GError *error = NULL;
177         gboolean success = TRUE;
178         gdouble dd = g_key_file_get_double ( keyfile, group, name, &error );
179         if ( 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 );
183                 success = FALSE;
184         }
185         *val = dd;
186         return success;
187 }
188
189 gboolean a_settings_get_double ( const gchar *name, gdouble *val )
190 {
191         return settings_get_double ( VIKING_SETTINGS_GROUP, name, val );
192 }
193
194 void a_settings_set_double ( const gchar *name, gdouble val )
195 {
196         g_key_file_set_double ( keyfile, VIKING_SETTINGS_GROUP, name, val );
197 }