[go: up one dir, main page]

Menu

[b1f2d5]: / src / settings.c  Maximize  Restore  History

Download this file

290 lines (257 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* viking -- GPS Data and Topo Analyzer, Explorer, and Manager
*
* Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* Sort of like the globals file, but values are automatically saved via program use.
* Some settings are *not* intended to have any GUI controls.
* Other settings be can used to set other GUI elements.
*
* ATM This is implemented using the simple (for me!) GKeyFile API - AKA an .ini file
* One might wish to consider the more modern alternative such as:
* http://developer.gnome.org/gio/2.26/GSettings.html
* Since these settings are 'internal' I have no problem with them *not* being supported
* between various Viking versions, should one switch to different API/storage methods.
* Indeed even the internal settings themselves can be liable to change.
*/
#include <glib.h>
#include "dir.h"
static GKeyFile *keyfile;
#define VIKING_INI_FILE "viking.ini"
static gboolean settings_load_from_file()
{
GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS;
GError *error = NULL;
gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
if ( !g_key_file_load_from_file ( keyfile, fn, flags, &error ) ) {
g_warning ( "%s: %s", error->message, fn );
g_free ( fn );
g_error_free ( error );
return FALSE;
}
g_free ( fn );
return TRUE;
}
void a_settings_init()
{
keyfile = g_key_file_new();
settings_load_from_file();
}
/**
* a_settings_uninit:
*
* ATM: The only time settings are saved is on program exit
* Could change this to occur on window exit or dialog exit or have memory hash of values...?
*/
void a_settings_uninit()
{
GError *error = NULL;
gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL );
gsize size;
gchar *keyfilestr = g_key_file_to_data ( keyfile, &size, &error );
if ( error ) {
g_warning ( "%s", error->message );
g_error_free ( error );
goto tidy;
}
g_file_set_contents ( fn, keyfilestr, size, &error );
if ( error ) {
g_warning ( "%s: %s", error->message, fn );
g_error_free ( error );
}
g_key_file_free ( keyfile );
tidy:
g_free ( keyfilestr );
g_free ( fn );
}
// ATM, can't see a point in having any more than one group for various settings
#define VIKING_SETTINGS_GROUP "viking"
static gboolean settings_get_boolean ( const gchar *group, const gchar *name, gboolean *val )
{
GError *error = NULL;
gboolean success = TRUE;
gboolean bb = g_key_file_get_boolean ( keyfile, group, name, &error );
if ( error ) {
// Only print on debug - as often may have requests for keys not in the file
g_debug ( "%s", error->message );
g_error_free ( error );
success = FALSE;
}
*val = bb;
return success;
}
gboolean a_settings_get_boolean ( const gchar *name, gboolean *val )
{
return settings_get_boolean ( VIKING_SETTINGS_GROUP, name, val );
}
void a_settings_set_boolean ( const gchar *name, gboolean val )
{
g_key_file_set_boolean ( keyfile, VIKING_SETTINGS_GROUP, name, val );
}
static gboolean settings_get_string ( const gchar *group, const gchar *name, gchar **val )
{
GError *error = NULL;
gboolean success = TRUE;
gchar *str = g_key_file_get_string ( keyfile, group, name, &error );
if ( error ) {
// Only print on debug - as often may have requests for keys not in the file
g_debug ( "%s", error->message );
g_error_free ( error );
success = FALSE;
}
*val = str;
return success;
}
gboolean a_settings_get_string ( const gchar *name, gchar **val )
{
return settings_get_string ( VIKING_SETTINGS_GROUP, name, val );
}
void a_settings_set_string ( const gchar *name, const gchar *val )
{
g_key_file_set_string ( keyfile, VIKING_SETTINGS_GROUP, name, val );
}
static gboolean settings_get_integer ( const gchar *group, const gchar *name, gint *val )
{
GError *error = NULL;
gboolean success = TRUE;
gint ii = g_key_file_get_integer ( keyfile, group, name, &error );
if ( error ) {
// Only print on debug - as often may have requests for keys not in the file
g_debug ( "%s", error->message );
g_error_free ( error );
success = FALSE;
}
*val = ii;
return success;
}
gboolean a_settings_get_integer ( const gchar *name, gint *val )
{
return settings_get_integer ( VIKING_SETTINGS_GROUP, name, val );
}
void a_settings_set_integer ( const gchar *name, gint val )
{
g_key_file_set_integer ( keyfile, VIKING_SETTINGS_GROUP, name, val );
}
static gboolean settings_get_double ( const gchar *group, const gchar *name, gdouble *val )
{
GError *error = NULL;
gboolean success = TRUE;
gdouble dd = g_key_file_get_double ( keyfile, group, name, &error );
if ( error ) {
// Only print on debug - as often may have requests for keys not in the file
g_debug ( "%s", error->message );
g_error_free ( error );
success = FALSE;
}
*val = dd;
return success;
}
gboolean a_settings_get_double ( const gchar *name, gdouble *val )
{
return settings_get_double ( VIKING_SETTINGS_GROUP, name, val );
}
void a_settings_set_double ( const gchar *name, gdouble val )
{
g_key_file_set_double ( keyfile, VIKING_SETTINGS_GROUP, name, val );
}
static gboolean settings_get_integer_list ( const gchar *group, const gchar *name, gint **vals, gsize *length )
{
GError *error = NULL;
gboolean success = TRUE;
gint *ints = g_key_file_get_integer_list ( keyfile, group, name, length, &error );
if ( error ) {
// Only print on debug - as often may have requests for keys not in the file
g_debug ( "%s", error->message );
g_error_free ( error );
success = FALSE;
}
*vals = ints;
return success;
}
/*
* The returned list of integers should be freed when no longer needed
*/
static gboolean a_settings_get_integer_list ( const gchar *name, gint **vals, gsize* length )
{
return settings_get_integer_list ( VIKING_SETTINGS_GROUP, name, vals, length );
}
static void a_settings_set_integer_list ( const gchar *name, gint vals[], gsize length )
{
g_key_file_set_integer_list ( keyfile, VIKING_SETTINGS_GROUP, name, vals, length );
}
gboolean a_settings_get_integer_list_contains ( const gchar *name, gint val )
{
gint* vals = NULL;
gsize length;
// Get current list and see if the value supplied is in the list
gboolean contains = FALSE;
// Get current list
if ( a_settings_get_integer_list ( name, &vals, &length ) ) {
// See if it's not already there
gint ii = 0;
if ( vals && length ) {
while ( ii < length ) {
if ( vals[ii] == val ) {
contains = TRUE;
break;
}
ii++;
}
// Free old array
g_free (vals);
}
}
return contains;
}
void a_settings_set_integer_list_containing ( const gchar *name, gint val )
{
gint* vals = NULL;
gsize length;
gboolean need_to_add = TRUE;
gint ii = 0;
// Get current list
if ( a_settings_get_integer_list ( name, &vals, &length ) ) {
// See if it's not already there
if ( vals && length ) {
while ( ii < length ) {
if ( vals[ii] == val ) {
need_to_add = FALSE;
break;
}
ii++;
}
}
}
// Add value into array if necessary
if ( need_to_add ) {
// NB not bothering to sort this 'list' ATM as there is not much to be gained
guint new_length = length + 1;
gint new_vals[new_length];
// Copy array
for ( ii = 0; ii < length; ii++ ) {
new_vals[ii] = vals[ii];
}
new_vals[length] = val; // Set the new value
// Apply
a_settings_set_integer_list ( name, new_vals, new_length );
// Free old array
g_free (vals);
}
}