1c38000a |
1 | #include <unistd.h> |
2 | #include <fcntl.h> |
3 | #include <gio/gio.h> |
4 | |
5 | static GKeyFile *config; |
6 | |
7 | static const gchar *CONFIG_KEYS[] = { |
8 | "PrettyHostname", |
9 | "IconName", |
10 | "ChassisType" |
11 | }; |
12 | |
13 | /* NULL if key doesn't exist */ |
14 | gchar *get_option(gchar *key, gchar *group) { |
15 | |
16 | if(!group) |
17 | group = "default"; |
18 | |
19 | return g_key_file_get_string(config, group, key, NULL); |
20 | } |
21 | |
22 | /* false if key isn't already defined or value is invalid */ |
23 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
24 | |
25 | if(!group) |
26 | group = "default"; |
27 | |
28 | if(!g_key_file_get_string(config, group, key, NULL)) |
29 | return FALSE; |
30 | |
31 | //TODO safteycheck value |
32 | g_key_file_set_string(config, group, key, value); |
33 | return TRUE; |
34 | } |
35 | |
36 | /* initial load/check */ |
37 | |
38 | gboolean config_init() { |
39 | |
40 | static gchar *config_path; |
41 | int tryopen = 0; |
42 | /* config is already good to go */ |
43 | if(config) |
44 | return TRUE; |
45 | |
46 | /* does config file exist? if not, write one */ |
47 | else if(!g_key_file_load_from_data(config, "systemd_compat.conf", &config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) { |
48 | |
49 | tryopen = g_open("/etc/systemd_compat.conf", O_CREAT, 644); |
50 | |
51 | //TODO clean this up, use g_data_dirs and g_exit |
52 | /* can we open it rw? */ |
53 | if(!g_access("/etc/systemd_compat.conf", W_OK) && !tryopen) { |
54 | g_printf("%s\n", "ERROR: cannot open systemd_compat.conf as read/write!"); |
55 | return FALSE; |
56 | } |
57 | |
58 | if(tryopen) { |
59 | config_path = "/etc/systemd_compat.conf"; |
60 | g_close(tryopen, NULL); |
61 | } |
62 | |
63 | //TODO set these properly |
64 | config = g_key_file_new(); |
65 | |
66 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
67 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
68 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); |
69 | |
70 | if(!g_key_file_save_to_file(config, config_path, NULL)) |
71 | return FALSE; |
72 | |
73 | return TRUE; |
74 | |
75 | /* it does it exist and was written to config var */ |
76 | } else |
77 | return TRUE; |
78 | } |