View | Details | Raw Unified | Return to bug 31
Collapse All | Expand All

(-)src/prefs.c (-33 / +79 lines)
Lines 18-23 Link Here
18
#include <sys/stat.h>
18
#include <sys/stat.h>
19
#include <fcntl.h>
19
#include <fcntl.h>
20
#include <unistd.h>
20
#include <unistd.h>
21
#include <glib.h>
22
#include <glib/gstdio.h>
21
23
22
#include "prefs.h"
24
#include "prefs.h"
23
#include "main.h"
25
#include "main.h"
Lines 322-364 Link Here
322
    p->proprietary_formats_expanded = gtk_expander_get_expanded (GTK_EXPANDER(lookup_widget(win_prefs, "proprietary_formats_expander")));
324
    p->proprietary_formats_expanded = gtk_expander_get_expanded (GTK_EXPANDER(lookup_widget(win_prefs, "proprietary_formats_expander")));
323
}
325
}
324
326
325
// get a config file path, using XDG_CONFIG_HOME is available
327
// get a config file path to load from, using XDG_CONFIG_HOME if available
326
static char *get_prefs_config_path(void)
328
//
329
// Priority:
330
//    1: Use the file got from g_get_user_config_dir + "asunder" + CONFIG_FILENAME
331
//    2: XDG_CONFIG_HOME + /asunder/CONFIG_FILENAME
332
//    3: getenv(HOME) + dot_filename
333
// 
334
static gchar *get_prefs_load_config_path(void)
327
{
335
{
328
	char *confdir;
336
    gchar *result_filename = NULL;
329
	char *file = NULL;
337
    
330
	int len;
338
    const gchar *xdg_config_home;
339
    gchar *xdg_config_home_filename;
340
    gchar *getenv_home_filename;
341
    
342
    gchar *user_config_home_filename;
331
343
332
	confdir = getenv("XDG_CONFIG_HOME");
344
    xdg_config_home = getenv("XDG_CONFIG_HOME");
333
	if (confdir == NULL || *confdir == '\0') {
345
    xdg_config_home_filename = g_build_filename(xdg_config_home, "asunder", CONFIG_FILENAME, NULL);
334
		confdir = getenv("HOME");
346
335
		len = strlen(confdir);
347
    user_config_home_filename = g_build_filename(g_get_user_config_dir(), "asunder", CONFIG_FILENAME, NULL);
336
		file = malloc(sizeof(char) * (len + strlen("/.") + strlen(CONFIG_FILENAME) + 1));
348
    
337
		if (file != NULL) {
349
    gchar *dot_filename = g_strdup_printf(".%s", CONFIG_FILENAME);
338
			strncpy(file, confdir, len);
350
    getenv_home_filename = g_build_filename(getenv("HOME"), dot_filename, NULL);
339
			strncpy(&file[len], "/.", 2);
351
    g_free(dot_filename);
340
			strcpy(&file[len + 2], CONFIG_FILENAME);
352
  
341
		}
353
    if (g_file_test(user_config_home_filename, G_FILE_TEST_EXISTS)) {
342
	}
354
343
	else {
355
        // Use the user_config_home_filename
344
		len = strlen(confdir);
356
        result_filename = g_strdup(user_config_home_filename);
345
		file = malloc(sizeof(char) * (len + strlen("/") + strlen(CONFIG_FILENAME) + 1));
357
        
346
		if (file != NULL) {
358
    } else if (xdg_config_home == NULL || *xdg_config_home == '\0') {
347
			strncpy(file, confdir, len);
359
        // use the getenv(HOME) one
348
			strncpy(&file[len], "/", 1);
360
        if (g_file_test(getenv_home_filename, G_FILE_TEST_EXISTS)) {
349
			strcpy(&file[len + 1], CONFIG_FILENAME);
361
            result_filename = g_strdup(getenv_home_filename);
350
		}
362
        }
351
	}
363
    }
352
	if (file == NULL) {
364
    else {
353
		fatalError("malloc() failed. Out of memory.");
365
        // use the xdg one
354
	}
366
        result_filename = g_strdup(xdg_config_home_filename);
355
	return file;
367
    }
368
    
369
    g_free(xdg_config_home_filename);
370
    g_free(getenv_home_filename);
371
    g_free(user_config_home_filename);
372
    
373
    return result_filename;
356
}
374
}
357
375
376
// get a config file path for saving prefs - Should always save
377
// to the get_user_config_dir location.
378
static gchar *get_prefs_save_config_path(void)
379
{
380
    gchar *result_filename = NULL;
381
    
382
    // Check if path exists, otherwise, create it.
383
    const gchar *config_path = g_get_user_config_dir();
384
    
385
    // ~/.config/asunder/
386
    gchar *path = g_build_filename(config_path, "asunder", NULL);
387
    
388
    // and then filename CONFIG_FILENAME which is "asunder".
389
    result_filename = g_build_filename(path, CONFIG_FILENAME, NULL);
390
    
391
    if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
392
        
393
        // Folder doesn't exist, create it.
394
        if (g_mkdir(path, 0755) != 0) {
395
            fatalError("g_mkdir() failed. Couldn't create folder.");
396
        }
397
    }
398
    
399
    g_free(path);
400
    
401
    return result_filename;
402
}
403
358
// store the given prefs struct to the config file
404
// store the given prefs struct to the config file
359
void save_prefs(prefs * p)
405
void save_prefs(prefs * p)
360
{
406
{
361
    char * file = get_prefs_config_path();
407
    gchar * file = get_prefs_save_config_path();
362
    debugLog("Saving configuration\n");
408
    debugLog("Saving configuration\n");
363
    
409
    
364
    FILE * config = fopen(file, "w");
410
    FILE * config = fopen(file, "w");
Lines 410-422 Link Here
410
    } else {
456
    } else {
411
        fprintf(stderr, "Warning: could not save config file: %s\n", strerror(errno));
457
        fprintf(stderr, "Warning: could not save config file: %s\n", strerror(errno));
412
    }
458
    }
413
    free(file);
459
    g_free(file);
414
}
460
}
415
461
416
// load the prefereces from the config file into the given prefs struct
462
// load the prefereces from the config file into the given prefs struct
417
void load_prefs(prefs * p)
463
void load_prefs(prefs * p)
418
{
464
{
419
	char * file = get_prefs_config_path();
465
    gchar * file = get_prefs_load_config_path();
420
    debugLog("Loading configuration\n");
466
    debugLog("Loading configuration\n");
421
    
467
    
422
    int fd = open(file, O_RDONLY);
468
    int fd = open(file, O_RDONLY);
Lines 602-608 Link Here
602
    } else {
648
    } else {
603
        fprintf(stderr, "Warning: could not load config file: %s\n", strerror(errno));
649
        fprintf(stderr, "Warning: could not load config file: %s\n", strerror(errno));
604
    }
650
    }
605
    free(file);
651
    g_free(file);
606
}
652
}
607
653
608
// use this method when reading the "music_dir" field of a prefs struct
654
// use this method when reading the "music_dir" field of a prefs struct

Return to bug 31