[go: up one dir, main page]

Menu

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

Download this file

406 lines (364 with data), 11.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* -*- 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 "settings.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;
}
/**
* Free the returned string in @val after use
*/
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
*/
gboolean a_settings_get_integer_list ( const gchar *name, gint **vals, gsize* length )
{
return settings_get_integer_list ( VIKING_SETTINGS_GROUP, name, vals, length );
}
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 = 0;
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 ) {
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 any previous values
if ( vals ) {
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
if ( vals )
g_free (vals);
}
/*
* The returned list of strings should be freed when no longer needed
* use g_strfreev().
*/
static gchar** settings_get_string_list ( const gchar *group, const gchar *name, gsize *length )
{
GError *error = NULL;
gchar **msgs = g_key_file_get_string_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 );
}
return msgs;
}
/**
* a_settings_get_string_list:
*
* Returns: A #GList of strings
*
* Free the returned list after use (including the contents)
*/
GList* a_settings_get_string_list ( const gchar *name )
{
//gchar **vals = NULL;
GList *gl = NULL;
gsize length;
gchar **vals = settings_get_string_list ( VIKING_SETTINGS_GROUP, name, &length );
if ( vals ) {
for ( guint ii = 0; ii < length; ii++ )
gl = g_list_prepend ( gl, g_strdup(vals[ii]) );
gl = g_list_reverse ( gl );
g_strfreev ( vals );
}
return gl;
}
void a_settings_clear_string_list ( const gchar *name )
{
g_key_file_set_string_list ( keyfile, VIKING_SETTINGS_GROUP, name, NULL, 0 );
}
gboolean a_settings_get_string_list_contains ( const gchar *name, const gchar *val )
{
// gchar **vals = NULL;
gsize length;
// Get current list and see if the value supplied is in the list
gboolean contains = FALSE;
// Get current list
gchar **vals = settings_get_string_list ( VIKING_SETTINGS_GROUP, name, &length );
if ( vals ) {
//if ( settings_get_string_list ( VIKING_SETTINGS_GROUP, name, vals, &length ) ) {
// See if it's not already there
// if ( vals ) {
contains = g_strv_contains ( (const gchar * const*)vals, val );
g_strfreev ( vals );
//}
}
return contains;
}
void a_settings_set_string_list_containing ( const gchar *name, const gchar *val )
{
// gchar** vals = NULL;
gsize length = 0;
gboolean need_to_add = TRUE;
//gint ii = 0;
// Get current list
gchar **vals = settings_get_string_list ( VIKING_SETTINGS_GROUP, name, &length );
if ( vals ) {
// if ( settings_get_string_list ( VIKING_SETTINGS_GROUP, name, vals, &length ) ) {
// See if it's not already there
//if ( vals )
need_to_add = ! g_strv_contains ( (const gchar * const*)vals, val );
}
// Add value into array if necessary
if ( need_to_add ) {
//gchar **new_vals = g_strdupv ( vals ); // Not sure how to append to this
// NB not bothering to sort this 'list' ATM as there is not much to be gained
const gchar *new_vals[length+2]; // +2 for new value + null
// Copy any previous values
for ( guint ii=0; ii<length; ii++ )
new_vals[ii] = g_strdup ( vals[ii] );
// Add the new value and the final terminator
new_vals[length] = g_strdup ( val ); // Set the new value
new_vals[length+1] = NULL;
/*
GPtrArray *string_list = g_ptr_array_new ();
// Copy any previous values
while ( ii < length ) {
g_ptr_array_add ( string_list, g_strdup ( vals[ii] ) );
ii++;
}
// Add the new value
g_ptr_array_add ( string_list, g_strdup ( val ) );
gchar **new_vals = (gchar**)g_ptr_array_free ( string_list, FALSE );
*/
// Apply
g_key_file_set_string_list ( keyfile, VIKING_SETTINGS_GROUP, name, new_vals, length+1 );
}
// Free old array
if ( vals )
g_strfreev ( vals );
}