aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.c
blob: 81a5bbb3d9233d02df2831a4d441c7ee5010e76c (plain) (blame)
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
#include "configuration.h"

#include "iniparser/src/iniparser.h"
#include "iniparser/src/dictionary.h"
#include "extString.h"
#include <string.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>

dictionary *
load_config(void)
{
    char *xdg_config = getenv ("XDG_CONFIG_HOME");
    char *home = getenv ("HOME");
    if (home == NULL)
        return NULL;
    if (xdg_config == NULL) {
        xdg_config = malloc (strlen(home)*sizeof(char)+strlen("/.config")*sizeof(char)+1);
        sprintf (xdg_config, "%s/.config", home);
    }
    char *config_path = malloc(strlen(xdg_config)*sizeof(char)+strlen("/autodarkmode/config.ini")*sizeof(char)+1);
    sprintf (config_path, "%s/autodarkmode/config.ini", xdg_config);

    free (xdg_config);

    if (access(config_path, F_OK) != 0) {
        g_printerr ("Config file not found. Using default values.\n");
        free (config_path);
        return NULL;
    }

    g_print ("Loading config file %s\n", config_path);

    dictionary *dict = iniparser_load (config_path);

    free (config_path);
    return dict;
}

enum LocationType
config_get_location_type(dictionary *d)
{
    if (d == NULL)
        return GCLUE;

    char *loctype = iniparser_getstring (d, "main:locationtype", "gclue");

    if (strcmp(strlwr(loctype), "gclue") == 0) {
        return GCLUE;
    } else if (strcmp(strlwr(loctype), "manual") == 0) {
        return MANUAL;
    } else {
        g_printerr("Invalid Location type %s. Defaulting to GeoClue\n", loctype);
        return GCLUE;
    }
    return GCLUE;
}

float
config_get_latitude (dictionary *d)
{
    if (d == NULL)
        return 0;

    return iniparser_getdouble (d, "manual:latitude", 0);
}

float
config_get_longitude (dictionary *d)
{
    if (d == NULL)
        return 0;

    return iniparser_getdouble (d, "manual:longitude", 0);
}