Read rc files from data directories

This commit is contained in:
Yevgen Muntyan 2007-10-26 04:46:20 -05:00
parent 60ba420c8e
commit 2b72fbe06b
4 changed files with 115 additions and 39 deletions

View File

@ -60,6 +60,13 @@
#define SESSION_VERSION "1.0"
#ifndef __WIN32__
#define TMPL_RC_FILE "%src"
#else
#define TMPL_RC_FILE "%s.ini"
#endif
#define TMPL_STATE_FILE "%s.state"
static MooApp *moo_app_instance = NULL;
static MooAppInput *moo_app_input = NULL;
@ -1914,8 +1921,8 @@ move_rc_files (MooApp *app)
char *new_file;
char *old_file;
new_file = g_strdup_printf ("%s/%src", g_get_user_config_dir (), g_get_prgname ());
old_file = g_strdup_printf ("%s/.%src", g_get_home_dir (), g_get_prgname ());
new_file = g_strdup_printf ("%s/" TMPL_RC_FILE, g_get_user_config_dir (), g_get_prgname ());
old_file = g_strdup_printf ("%s/." TMPL_RC_FILE, g_get_home_dir (), g_get_prgname ());
if (!g_file_test (new_file, G_FILE_TEST_EXISTS) &&
g_file_test (old_file, G_FILE_TEST_EXISTS) &&
@ -1942,17 +1949,17 @@ move_rc_files (MooApp *app)
{
const char *new_file;
char *old_file = g_strdup_printf ("%s/.%s.state", g_get_home_dir (), g_get_prgname ());
char *old_file = g_strdup_printf ("%s/." TMPL_STATE_FILE, g_get_home_dir (), g_get_prgname ());
if (app->priv->instance_name)
app->priv->rc_files[MOO_PREFS_STATE] =
g_strdup_printf ("%s/%s.state.%s",
g_strdup_printf ("%s/" TMPL_STATE_FILE ".%s",
cache_dir,
g_get_prgname (),
app->priv->instance_name);
else
app->priv->rc_files[MOO_PREFS_STATE] =
g_strdup_printf ("%s/%s.state",
g_strdup_printf ("%s/" TMPL_STATE_FILE,
cache_dir,
g_get_prgname ());
@ -1974,25 +1981,57 @@ move_rc_files (MooApp *app)
#endif
static char **
get_rc_files (void)
{
char *prefix;
char *var;
const char *value;
char **files = NULL;
prefix = g_ascii_strup (g_get_prgname (), -1);
var = g_strdup_printf ("%s_RC_FILES", prefix);
value = g_getenv (var);
if (value && value[0])
{
files = g_strsplit (value, G_SEARCHPATH_SEPARATOR_S, 0);
}
else
{
char *tmpl = g_strdup_printf (TMPL_RC_FILE, g_get_prgname ());
files = moo_get_data_files (tmpl, MOO_DATA_SHARE, NULL);
g_free (tmpl);
}
g_free (var);
g_free (prefix);
return files;
}
static void
moo_app_load_prefs (MooApp *app)
{
GError *error = NULL;
char **sys_files;
#ifndef __WIN32__
move_rc_files (app);
#else
app->priv->rc_files[MOO_PREFS_RC] =
g_strdup_printf ("%s/%s.ini", g_get_user_config_dir (), g_get_prgname ());
g_strdup_printf ("%s/" TMPL_RC_FILE, g_get_user_config_dir (), g_get_prgname ());
app->priv->rc_files[MOO_PREFS_STATE] =
g_strdup_printf ("%s/%s.state", g_get_user_config_dir (), g_get_prgname ());
g_strdup_printf ("%s/" TMPL_STATE_FILE, g_get_user_config_dir (), g_get_prgname ());
#endif
if (!moo_prefs_load (app->priv->rc_files[MOO_PREFS_RC],
sys_files = get_rc_files ();
if (!moo_prefs_load (sys_files,
app->priv->rc_files[MOO_PREFS_RC],
app->priv->rc_files[MOO_PREFS_STATE],
&error))
{
g_warning ("%s: could not read config file", G_STRLOC);
g_warning ("%s: could not read config files", G_STRLOC);
if (error)
{
@ -2000,6 +2039,8 @@ moo_app_load_prefs (MooApp *app)
g_error_free (error);
}
}
g_strfreev (sys_files);
}
@ -2012,7 +2053,7 @@ moo_app_save_prefs (MooApp *app)
app->priv->rc_files[MOO_PREFS_STATE],
&error))
{
g_warning ("%s: could not save config file", G_STRLOC);
g_warning ("%s: could not save config files", G_STRLOC);
if (error)
{

View File

@ -1923,8 +1923,9 @@
(c-name "moo_prefs_load")
(return-type "gboolean")
(parameters
'("const-char*" "file_rc")
'("const-char*" "file_state")
'("strv" "sys_files" (null-ok) (default "NULL"))
'("const-char*" "file_rc" (null-ok) (default "NULL"))
'("const-char*" "file_state" (null-ok) (default "NULL"))
'("GError**" "error")
)
)

View File

@ -25,6 +25,8 @@
#define PREFS_ROOT "Prefs"
/* #define DEBUG_READWRITE 1 */
#define MOO_PREFS_SYS -1
#define MOO_TYPE_PREFS (_moo_prefs_get_type ())
#define MOO_PREFS(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_PREFS, MooPrefs))
#define MOO_PREFS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_PREFS, MooPrefsClass))
@ -59,6 +61,7 @@ typedef struct {
GValue value;
GValue default_value;
guint prefs_type : 1;
guint overridden : 1;
} PrefsItem;
@ -70,7 +73,7 @@ static void prefs_new_key (MooPrefs *prefs,
static void prefs_new_key_from_string (MooPrefs *prefs,
const char *key,
const char *value,
MooPrefsType prefs_type);
int prefs_type);
static void prefs_delete_key (MooPrefs *prefs,
const char *key);
static GType prefs_get_key_type (MooPrefs *prefs,
@ -109,7 +112,7 @@ static gboolean item_set_default (PrefsItem *item,
static void moo_prefs_finalize (GObject *object);
static void moo_prefs_new_key_from_string (const char *key,
const char *value,
MooPrefsType prefs_type);
int prefs_type);
static void moo_prefs_set_modified (gboolean modified);
@ -352,7 +355,7 @@ moo_prefs_delete_key (const char *key)
static void
moo_prefs_new_key_from_string (const char *key,
const char *value,
MooPrefsType prefs_type)
int prefs_type)
{
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
@ -441,10 +444,11 @@ prefs_new_key (MooPrefs *prefs,
{
changed = item_set_type (item, type);
if (item_set_default (item, default_value))
if (!item->overridden && item_set_default (item, default_value))
changed = TRUE;
if (item->prefs_type == MOO_PREFS_RC &&
if (!item->overridden &&
item->prefs_type == MOO_PREFS_RC &&
item->prefs_type != prefs_type)
moo_prefs_set_modified (TRUE);
@ -460,7 +464,7 @@ static void
prefs_new_key_from_string (MooPrefs *prefs,
const char *key,
const char *value,
MooPrefsType prefs_type)
int prefs_type)
{
PrefsItem *item;
GValue val, default_val;
@ -476,16 +480,30 @@ prefs_new_key_from_string (MooPrefs *prefs,
default_val.g_type = 0;
g_value_init (&default_val, G_TYPE_STRING);
if (prefs_type == MOO_PREFS_SYS)
g_value_set_static_string (&default_val, value);
if (!item)
{
prefs_new_key (prefs, key, G_TYPE_STRING, &default_val, prefs_type);
prefs_new_key (prefs, key, G_TYPE_STRING, &default_val,
prefs_type == MOO_PREFS_SYS ? MOO_PREFS_RC : prefs_type);
item = prefs_get_item (prefs, key);
item_set (item, &val);
if (prefs_type == MOO_PREFS_SYS)
item->overridden = TRUE;
}
else
{
_moo_value_convert (&val, &item->value);
item->prefs_type = prefs_type;
if (prefs_type == MOO_PREFS_SYS)
{
_moo_value_convert (&default_val, &item->default_value);
item->overridden = TRUE;
}
else
item->prefs_type = prefs_type;
}
g_value_unset (&val);
@ -998,7 +1016,7 @@ moo_prefs_notify_disconnect (guint id)
static void
process_element (MooMarkupElement *elm,
MooPrefsType prefs_type)
int prefs_type)
{
MooMarkupNode *child;
gboolean dir = FALSE;
@ -1020,7 +1038,8 @@ process_element (MooMarkupElement *elm,
path = moo_markup_element_get_path (elm);
if (strlen (path) < strlen (PREFS_ROOT) + 2)
if (strncmp (path, PREFS_ROOT "/", strlen (PREFS_ROOT "/")) != 0 ||
strlen (path) < strlen (PREFS_ROOT) + 2)
{
g_free (path);
g_return_if_reached ();
@ -1047,7 +1066,7 @@ process_element (MooMarkupElement *elm,
static gboolean
load_file (const char *file,
MooPrefsType prefs_type,
int prefs_type,
GError **error)
{
MooMarkupDoc *xml;
@ -1065,6 +1084,11 @@ load_file (const char *file,
case MOO_PREFS_STATE:
target = &prefs->xml_state;
break;
case MOO_PREFS_SYS:
target = NULL;
break;
default:
g_assert_not_reached ();
}
if (!g_file_test (file, G_FILE_TEST_EXISTS))
@ -1075,13 +1099,16 @@ load_file (const char *file,
if (!xml)
return FALSE;
if (*target)
if (target)
{
g_warning ("%s: implement me", G_STRLOC);
moo_markup_doc_unref (*target);
}
if (*target)
{
g_warning ("%s: implement me", G_STRLOC);
moo_markup_doc_unref (*target);
}
*target = xml;
*target = moo_markup_doc_ref (xml);
}
if (prefs_type != MOO_PREFS_STATE)
{
@ -1091,27 +1118,33 @@ load_file (const char *file,
root = moo_markup_get_root_element (xml, PREFS_ROOT);
if (!root)
return TRUE;
process_element (MOO_MARKUP_ELEMENT (root), prefs_type);
if (root)
process_element (MOO_MARKUP_ELEMENT (root), prefs_type);
moo_markup_doc_unref (xml);
return TRUE;
}
gboolean
moo_prefs_load (const char *file_rc,
moo_prefs_load (char **sys_files,
const char *file_rc,
const char *file_state,
GError **error)
{
g_return_val_if_fail (file_rc != NULL, FALSE);
g_return_val_if_fail (file_state != NULL, FALSE);
moo_prefs_set_modified (FALSE);
return load_file (file_rc, MOO_PREFS_RC, error) &&
load_file (file_state, MOO_PREFS_STATE, error);
for (; sys_files && *sys_files; ++sys_files)
if (!load_file (*sys_files, MOO_PREFS_SYS, error))
return FALSE;
if (file_rc && !load_file (file_rc, MOO_PREFS_RC, error))
return FALSE;
if (file_state && !load_file (file_state, MOO_PREFS_STATE, error))
return FALSE;
return TRUE;
}

View File

@ -47,7 +47,8 @@ typedef enum {
GType moo_prefs_type_get_type (void) G_GNUC_CONST;
GType moo_prefs_match_type_get_type (void) G_GNUC_CONST;
gboolean moo_prefs_load (const char *file_rc,
gboolean moo_prefs_load (char **sys_files,
const char *file_rc,
const char *file_state,
GError **error);
gboolean moo_prefs_save (const char *file_rc,