[134] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
| 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "config.h" |
---|
| 22 | |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <fcntl.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <string.h> |
---|
| 28 | #include <signal.h> |
---|
| 29 | #include <sys/stat.h> |
---|
| 30 | #include <sys/types.h> |
---|
| 31 | |
---|
| 32 | #include <glib.h> |
---|
| 33 | #include <glib/gi18n.h> |
---|
| 34 | #include <glib/gstdio.h> |
---|
| 35 | #include <glib-object.h> |
---|
| 36 | |
---|
| 37 | #include "gdm-settings.h" |
---|
| 38 | #include "gdm-settings-utils.h" |
---|
| 39 | #include "gdm-settings-direct.h" |
---|
| 40 | |
---|
| 41 | static GHashTable *schemas; |
---|
| 42 | static GdmSettings *settings_object; |
---|
| 43 | |
---|
| 44 | static GdmSettingsEntry * |
---|
| 45 | get_entry_for_key (const char *key) |
---|
| 46 | { |
---|
| 47 | GdmSettingsEntry *entry; |
---|
| 48 | |
---|
| 49 | entry = g_hash_table_lookup (schemas, key); |
---|
| 50 | |
---|
| 51 | return entry; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | static void |
---|
| 55 | assert_signature (GdmSettingsEntry *entry, |
---|
| 56 | const char *signature) |
---|
| 57 | { |
---|
| 58 | const char *sig; |
---|
| 59 | |
---|
| 60 | sig = gdm_settings_entry_get_signature (entry); |
---|
| 61 | |
---|
| 62 | g_assert (sig != NULL); |
---|
| 63 | g_assert (signature != NULL); |
---|
| 64 | g_assert (strcmp (signature, sig) == 0); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | static gboolean |
---|
| 68 | get_value (const char *key, |
---|
| 69 | char **value) |
---|
| 70 | { |
---|
| 71 | GError *error; |
---|
| 72 | char *str; |
---|
| 73 | gboolean res; |
---|
| 74 | |
---|
| 75 | error = NULL; |
---|
| 76 | res = gdm_settings_get_value (settings_object, key, &str, &error); |
---|
| 77 | if (! res) { |
---|
| 78 | if (error != NULL) { |
---|
| 79 | g_error_free (error); |
---|
| 80 | } else { |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return FALSE; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (value != NULL) { |
---|
| 87 | *value = g_strdup (str); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | g_free (str); |
---|
| 91 | |
---|
| 92 | return TRUE; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | gboolean |
---|
| 96 | gdm_settings_direct_get_int (const char *key, |
---|
| 97 | int *value) |
---|
| 98 | { |
---|
| 99 | GdmSettingsEntry *entry; |
---|
| 100 | gboolean ret; |
---|
| 101 | gboolean res; |
---|
| 102 | char *str; |
---|
| 103 | |
---|
| 104 | g_return_val_if_fail (key != NULL, FALSE); |
---|
| 105 | |
---|
| 106 | entry = get_entry_for_key (key); |
---|
| 107 | g_assert (entry != NULL); |
---|
| 108 | |
---|
| 109 | assert_signature (entry, "i"); |
---|
| 110 | |
---|
| 111 | ret = FALSE; |
---|
| 112 | |
---|
| 113 | res = get_value (key, &str); |
---|
| 114 | |
---|
| 115 | if (! res) { |
---|
| 116 | /* use the default */ |
---|
| 117 | str = g_strdup (gdm_settings_entry_get_default_value (entry)); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | ret = gdm_settings_parse_value_as_integer (str, value); |
---|
| 121 | |
---|
| 122 | g_free (str); |
---|
| 123 | |
---|
| 124 | return ret; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | gboolean |
---|
| 128 | gdm_settings_direct_get_uint (const char *key, |
---|
| 129 | uint *value) |
---|
| 130 | { |
---|
| 131 | gboolean ret; |
---|
| 132 | int intvalue; |
---|
| 133 | |
---|
| 134 | ret = FALSE; |
---|
| 135 | ret = gdm_settings_direct_get_int (key, &intvalue); |
---|
| 136 | |
---|
| 137 | if (intvalue >= 0) |
---|
| 138 | *value = intvalue; |
---|
| 139 | else |
---|
| 140 | ret = FALSE; |
---|
| 141 | |
---|
| 142 | return ret; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | gboolean |
---|
| 146 | gdm_settings_direct_get_boolean (const char *key, |
---|
| 147 | gboolean *value) |
---|
| 148 | { |
---|
| 149 | GdmSettingsEntry *entry; |
---|
| 150 | gboolean ret; |
---|
| 151 | gboolean res; |
---|
| 152 | char *str; |
---|
| 153 | |
---|
| 154 | g_return_val_if_fail (key != NULL, FALSE); |
---|
| 155 | |
---|
| 156 | entry = get_entry_for_key (key); |
---|
| 157 | g_assert (entry != NULL); |
---|
| 158 | |
---|
| 159 | assert_signature (entry, "b"); |
---|
| 160 | |
---|
| 161 | ret = FALSE; |
---|
| 162 | |
---|
| 163 | res = get_value (key, &str); |
---|
| 164 | |
---|
| 165 | if (! res) { |
---|
| 166 | /* use the default */ |
---|
| 167 | str = g_strdup (gdm_settings_entry_get_default_value (entry)); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | ret = gdm_settings_parse_value_as_boolean (str, value); |
---|
| 171 | |
---|
| 172 | g_free (str); |
---|
| 173 | |
---|
| 174 | return ret; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | gboolean |
---|
| 178 | gdm_settings_direct_get_string (const char *key, |
---|
| 179 | char **value) |
---|
| 180 | { |
---|
| 181 | GdmSettingsEntry *entry; |
---|
| 182 | gboolean ret; |
---|
| 183 | gboolean res; |
---|
| 184 | char *str; |
---|
| 185 | |
---|
| 186 | g_return_val_if_fail (key != NULL, FALSE); |
---|
| 187 | |
---|
| 188 | entry = get_entry_for_key (key); |
---|
| 189 | g_assert (entry != NULL); |
---|
| 190 | |
---|
| 191 | assert_signature (entry, "s"); |
---|
| 192 | |
---|
| 193 | ret = FALSE; |
---|
| 194 | |
---|
| 195 | res = get_value (key, &str); |
---|
| 196 | |
---|
| 197 | if (! res) { |
---|
| 198 | /* use the default */ |
---|
| 199 | str = g_strdup (gdm_settings_entry_get_default_value (entry)); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | if (value != NULL) { |
---|
| 203 | *value = g_strdup (str); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | g_free (str); |
---|
| 207 | |
---|
| 208 | return ret; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | gboolean |
---|
| 212 | gdm_settings_direct_set (const char *key, |
---|
| 213 | GValue *value) |
---|
| 214 | { |
---|
| 215 | return TRUE; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | static void |
---|
| 219 | hashify_list (GdmSettingsEntry *entry, |
---|
| 220 | gpointer data) |
---|
| 221 | { |
---|
| 222 | g_hash_table_insert (schemas, g_strdup (gdm_settings_entry_get_key (entry)), entry); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | gboolean |
---|
| 226 | gdm_settings_direct_init (GdmSettings *settings, |
---|
| 227 | const char *file, |
---|
| 228 | const char *root) |
---|
| 229 | { |
---|
| 230 | GSList *list; |
---|
| 231 | |
---|
| 232 | g_return_val_if_fail (file != NULL, FALSE); |
---|
| 233 | g_return_val_if_fail (root != NULL, FALSE); |
---|
| 234 | |
---|
| 235 | g_assert (schemas == NULL); |
---|
| 236 | |
---|
| 237 | if (! gdm_settings_parse_schemas (file, root, &list)) { |
---|
| 238 | g_warning ("Unable to parse schemas"); |
---|
| 239 | return FALSE; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | schemas = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)gdm_settings_entry_free); |
---|
| 243 | g_slist_foreach (list, (GFunc)hashify_list, NULL); |
---|
| 244 | |
---|
| 245 | settings_object = settings; |
---|
| 246 | |
---|
| 247 | return TRUE; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | void |
---|
| 251 | gdm_settings_direct_shutdown (void) |
---|
| 252 | { |
---|
| 253 | |
---|
| 254 | } |
---|