From 55d5bd21685f2e5e64a62e9fbb23624fdb9e8ea3 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Mon, 8 Sep 2014 11:47:19 +1200 Subject: Add --show-config option that shows combined configuration --- common/configuration.c | 58 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) (limited to 'common/configuration.c') diff --git a/common/configuration.c b/common/configuration.c index aeba6e30..bf08c7d9 100644 --- a/common/configuration.c +++ b/common/configuration.c @@ -15,7 +15,10 @@ struct ConfigurationPrivate { + gchar *dir; GKeyFile *key_file; + GList *sources; + GHashTable *key_sources; }; G_DEFINE_TYPE (Configuration, config, G_TYPE_OBJECT); @@ -34,7 +37,7 @@ gboolean config_load_from_file (Configuration *config, const gchar *path, GError **error) { GKeyFile *key_file; - gchar **groups; + gchar *source_path, **groups; int i; key_file = g_key_file_new (); @@ -44,6 +47,9 @@ config_load_from_file (Configuration *config, const gchar *path, GError **error) return FALSE; } + source_path = g_strdup (path); + config->priv->sources = g_list_append (config->priv->sources, source_path); + groups = g_key_file_get_groups (key_file, NULL); for (i = 0; groups[i]; i++) { @@ -56,11 +62,14 @@ config_load_from_file (Configuration *config, const gchar *path, GError **error) for (j = 0; keys[j]; j++) { - gchar *value; + gchar *value, *k; value = g_key_file_get_value (key_file, groups[i], keys[j], NULL); g_key_file_set_value (config->priv->key_file, groups[i], keys[j], value); g_free (value); + + k = g_strdup_printf ("%s]%s", groups[i], keys[j]); + g_hash_table_insert (config->priv->key_sources, k, source_path); } g_strfreev (keys); @@ -159,25 +168,26 @@ load_config_directories (const gchar * const *dirs, GList **messages) gboolean config_load_from_standard_locations (Configuration *config, const gchar *config_path, GList **messages) { - gchar *config_dir, *config_d_dir = NULL, *path; + gchar *config_d_dir = NULL, *path; gboolean success = TRUE; GError *error = NULL; + g_return_val_if_fail (config->priv->dir == NULL, FALSE); + load_config_directories (g_get_system_data_dirs (), messages); load_config_directories (g_get_system_config_dirs (), messages); if (config_path) { - config_dir = path_make_absolute (g_path_get_basename (config_path)); path = g_strdup (config_path); + config->priv->dir = path_make_absolute (g_path_get_basename (config_path)); } else { - config_dir = g_strdup (CONFIG_DIR); - config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL); - path = g_build_filename (config_dir, "lightdm.conf", NULL); + config->priv->dir = g_strdup (CONFIG_DIR); + config_d_dir = g_build_filename (config->priv->dir, "lightdm.conf.d", NULL); + path = g_build_filename (config->priv->dir, "lightdm.conf", NULL); } - config_set_string (config, "LightDM", "config-directory", config_dir); if (config_d_dir) load_config_directory (config_d_dir, messages); @@ -199,13 +209,18 @@ config_load_from_standard_locations (Configuration *config, const gchar *config_ } g_clear_error (&error); - g_free (config_dir); g_free (config_d_dir); g_free (path); return success; } +const gchar * +config_get_directory (Configuration *config) +{ + return config->priv->dir; +} + gchar ** config_get_groups (Configuration *config) { @@ -224,6 +239,25 @@ config_has_key (Configuration *config, const gchar *section, const gchar *key) return g_key_file_has_key (config->priv->key_file, section, key, NULL); } +GList * +config_get_sources (Configuration *config) +{ + return config->priv->sources; +} + +const gchar * +config_get_source (Configuration *config, const gchar *section, const gchar *key) +{ + gchar *k; + const gchar *source; + + k = g_strdup_printf ("%s]%s", section, key); + source = g_hash_table_lookup (config->priv->key_sources, k); + g_free (k); + + return source; +} + void config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value) { @@ -276,7 +310,8 @@ static void config_init (Configuration *config) { config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate); - config->priv->key_file = g_key_file_new (); + config->priv->key_file = g_key_file_new (); + config->priv->key_sources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); } static void @@ -286,7 +321,10 @@ config_finalize (GObject *object) self = CONFIGURATION (object); + g_free (self->priv->dir); g_key_file_free (self->priv->key_file); + g_list_free_full (self->priv->sources, g_free); + g_hash_table_destroy (self->priv->key_sources); G_OBJECT_CLASS (config_parent_class)->finalize (object); } -- cgit v1.2.1