Added 'modify' argument to MooConfig methods

master
Yevgen Muntyan 2006-04-21 03:30:22 -05:00
parent 7164887245
commit 7a5069ea38
7 changed files with 92 additions and 44 deletions

View File

@ -189,10 +189,10 @@ moo_edit_load_tools (FileType type,
config = moo_config_new (MOO_USER_TOOL_KEY_ACTION); config = moo_config_new (MOO_USER_TOOL_KEY_ACTION);
for (i = 0; i < n_files; ++i) for (i = 0; i < n_files; ++i)
moo_config_parse_file (config, default_files[i]); moo_config_parse_file (config, default_files[i], FALSE);
if (user_file && g_file_test (user_file, G_FILE_TEST_EXISTS)) if (user_file && g_file_test (user_file, G_FILE_TEST_EXISTS))
moo_config_parse_file (config, user_file); moo_config_parse_file (config, user_file, FALSE);
n_items = moo_config_n_items (config); n_items = moo_config_n_items (config);

View File

@ -250,15 +250,15 @@ make_config (GtkTreeModel *model)
if (pattern) if (pattern)
{ {
item = moo_config_new_item (config, pattern); item = moo_config_new_item (config, pattern, FALSE);
if (script) if (script)
moo_config_set_item_content (config, item, script); moo_config_set_item_content (config, item, script, FALSE);
if (lang) if (lang)
moo_config_set_value (config, item, AS_KEY_LANG, lang); moo_config_set_value (config, item, AS_KEY_LANG, lang, FALSE);
if (!enabled) if (!enabled)
moo_config_set_value (config, item, AS_KEY_ENABLED, "no"); moo_config_set_value (config, item, AS_KEY_ENABLED, "no", FALSE);
} }
g_free (pattern); g_free (pattern);

View File

@ -1044,7 +1044,7 @@ _as_plugin_load (G_GNUC_UNUSED MooPlugin *plugin,
config = moo_config_new (AS_KEY_PATTERN); config = moo_config_new (AS_KEY_PATTERN);
if (!moo_config_parse_file (config, file)) if (!moo_config_parse_file (config, file, FALSE))
{ {
g_object_unref (config); g_object_unref (config);
return; return;

View File

@ -193,7 +193,7 @@ cmpl_data_read_config_file (CmplData *data)
config = moo_config_new ("pattern"); config = moo_config_new ("pattern");
if (!moo_config_parse_file (config, data->path)) if (!moo_config_parse_file (config, data->path, FALSE))
{ {
g_object_unref (config); g_object_unref (config);
return; return;
@ -218,9 +218,7 @@ cmpl_data_read_config_file (CmplData *data)
pattern = moo_config_item_get_id (item); pattern = moo_config_item_get_id (item);
prefix = moo_config_item_get_value (item, "prefix"); prefix = moo_config_item_get_value (item, "prefix");
suffix = moo_config_item_get_value (item, "insert-suffix"); suffix = moo_config_item_get_value (item, "insert-suffix");
suffix = suffix ? suffix : moo_config_item_get_value (item, "insert_suffix");
script = moo_config_item_get_value (item, "insert-script"); script = moo_config_item_get_value (item, "insert-script");
script = script ? script : moo_config_item_get_value (item, "insert_script");
groups = moo_config_item_get_value (item, "group"); groups = moo_config_item_get_value (item, "group");
groups = groups ? groups : moo_config_item_get_value (item, "groups"); groups = groups ? groups : moo_config_item_get_value (item, "groups");

View File

@ -28,6 +28,7 @@ struct _MooConfigPrivate {
char *id_key; char *id_key;
int model_stamp; int model_stamp;
GHashTable *ids; GHashTable *ids;
gboolean modified;
}; };
typedef struct { typedef struct {
@ -387,25 +388,40 @@ void
moo_config_set_value (MooConfig *config, moo_config_set_value (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *key, const char *key,
const char *value) const char *value,
gboolean modify)
{ {
char *norm; char *norm, *old_value;
gboolean modified = FALSE;
g_return_if_fail (item != NULL); g_return_if_fail (item != NULL);
g_return_if_fail (key != NULL); g_return_if_fail (key != NULL);
norm = normalize_key (key); norm = normalize_key (key);
old_value = g_hash_table_lookup (item->dict, norm);
if (value) if (value)
{
if (!old_value || strcmp (value, old_value))
{ {
g_hash_table_insert (item->dict, norm, g_strdup (value)); g_hash_table_insert (item->dict, norm, g_strdup (value));
modified = TRUE;
}
} }
else else
{
if (old_value)
{ {
g_hash_table_remove (item->dict, norm); g_hash_table_remove (item->dict, norm);
g_free (norm); g_free (norm);
modified = TRUE;
}
} }
if (modified && modify)
config->priv->modified = TRUE;
if (modified)
emit_row_changed (config, item); emit_row_changed (config, item);
} }
@ -421,18 +437,29 @@ moo_config_item_get_content (MooConfigItem *item)
void void
moo_config_set_item_content (MooConfig *config, moo_config_set_item_content (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *content) const char *content,
gboolean modify)
{ {
char *tmp; char *tmp;
g_return_if_fail (item != NULL); g_return_if_fail (item != NULL);
g_return_if_fail (MOO_IS_CONFIG (config)); g_return_if_fail (MOO_IS_CONFIG (config));
if (item->content == content)
return;
if ((!item->content && content) || (item->content && !content) ||
strcmp (content, item->content))
{
tmp = item->content; tmp = item->content;
item->content = g_strdup (content); item->content = g_strdup (content);
g_free (tmp); g_free (tmp);
if (modify)
config->priv->modified = TRUE;
emit_row_changed (config, item); emit_row_changed (config, item);
}
} }
@ -452,7 +479,8 @@ get_item_index (MooConfig *config,
void void
moo_config_delete_item (MooConfig *config, moo_config_delete_item (MooConfig *config,
const char *id) const char *id,
gboolean modify)
{ {
MooConfigItem *item; MooConfigItem *item;
GtkTreePath *path; GtkTreePath *path;
@ -473,6 +501,9 @@ moo_config_delete_item (MooConfig *config,
g_hash_table_remove (config->priv->ids, id); g_hash_table_remove (config->priv->ids, id);
moo_config_item_free (item); moo_config_item_free (item);
if (modify)
config->priv->modified = TRUE;
config->priv->model_stamp++; config->priv->model_stamp++;
path = gtk_tree_path_new_from_indices (index, -1); path = gtk_tree_path_new_from_indices (index, -1);
g_signal_emit_by_name (config, "row-deleted", path); g_signal_emit_by_name (config, "row-deleted", path);
@ -483,7 +514,8 @@ moo_config_delete_item (MooConfig *config,
static MooConfigItem * static MooConfigItem *
moo_config_new_item_real (MooConfig *config, moo_config_new_item_real (MooConfig *config,
const char *id, const char *id,
GHashTable *dict) GHashTable *dict,
gboolean modify)
{ {
MooConfigItem *item; MooConfigItem *item;
GtkTreePath *path; GtkTreePath *path;
@ -492,7 +524,7 @@ moo_config_new_item_real (MooConfig *config,
item = g_hash_table_lookup (config->priv->ids, id); item = g_hash_table_lookup (config->priv->ids, id);
if (item) if (item)
moo_config_delete_item (config, id); moo_config_delete_item (config, id, modify);
if (dict) if (dict)
g_hash_table_remove (dict, config->priv->id_key); g_hash_table_remove (dict, config->priv->id_key);
@ -501,6 +533,9 @@ moo_config_new_item_real (MooConfig *config,
g_ptr_array_add (config->priv->items, item); g_ptr_array_add (config->priv->items, item);
g_hash_table_insert (config->priv->ids, g_strdup (id), item); g_hash_table_insert (config->priv->ids, g_strdup (id), item);
if (modify)
config->priv->modified = TRUE;
config->priv->model_stamp++; config->priv->model_stamp++;
ITER_SET (config, &iter, config->priv->items->len - 1); ITER_SET (config, &iter, config->priv->items->len - 1);
path = gtk_tree_path_new_from_indices (config->priv->items->len - 1, -1); path = gtk_tree_path_new_from_indices (config->priv->items->len - 1, -1);
@ -513,11 +548,12 @@ moo_config_new_item_real (MooConfig *config,
MooConfigItem * MooConfigItem *
moo_config_new_item (MooConfig *config, moo_config_new_item (MooConfig *config,
const char *id) const char *id,
gboolean modify)
{ {
g_return_val_if_fail (MOO_IS_CONFIG (config), NULL); g_return_val_if_fail (MOO_IS_CONFIG (config), NULL);
g_return_val_if_fail (id && id[0], NULL); g_return_val_if_fail (id && id[0], NULL);
return moo_config_new_item_real (config, id, NULL); return moo_config_new_item_real (config, id, NULL, modify);
} }
@ -747,7 +783,8 @@ error:
static void static void
moo_config_add_items (MooConfig *config, moo_config_add_items (MooConfig *config,
GSList *list) GSList *list,
gboolean modify)
{ {
while (list) while (list)
{ {
@ -766,7 +803,7 @@ moo_config_add_items (MooConfig *config,
} }
id = g_strdup (id); id = g_strdup (id);
item = moo_config_new_item_real (config, id, data->dict); item = moo_config_new_item_real (config, id, data->dict, modify);
item->content = data->content; item->content = data->content;
item->start = data->start; item->start = data->start;
item->end = data->end; item->end = data->end;
@ -783,7 +820,8 @@ skip:
static gboolean static gboolean
moo_config_parse_lines (MooConfig *config, moo_config_parse_lines (MooConfig *config,
char **lines, char **lines,
guint n_lines) guint n_lines,
gboolean modify)
{ {
ItemData *data; ItemData *data;
GPtrArray *content; GPtrArray *content;
@ -899,7 +937,7 @@ moo_config_parse_lines (MooConfig *config,
items = g_slist_reverse (items); items = g_slist_reverse (items);
moo_config_add_items (config, items); moo_config_add_items (config, items, modify);
g_slist_foreach (items, (GFunc) item_data_free, NULL); g_slist_foreach (items, (GFunc) item_data_free, NULL);
g_slist_free (items); g_slist_free (items);
@ -919,7 +957,8 @@ error:
gboolean gboolean
moo_config_parse_buffer (MooConfig *config, moo_config_parse_buffer (MooConfig *config,
const char *string, const char *string,
int len) int len,
gboolean modify)
{ {
char **lines; char **lines;
guint n_lines; guint n_lines;
@ -932,7 +971,7 @@ moo_config_parse_buffer (MooConfig *config,
len = strlen (string); len = strlen (string);
lines = splitlines (string, len, &n_lines); lines = splitlines (string, len, &n_lines);
result = moo_config_parse_lines (config, lines, n_lines); result = moo_config_parse_lines (config, lines, n_lines, modify);
g_strfreev (lines); g_strfreev (lines);
return result; return result;
@ -941,7 +980,8 @@ moo_config_parse_buffer (MooConfig *config,
gboolean gboolean
moo_config_parse_file (MooConfig *config, moo_config_parse_file (MooConfig *config,
const char *filename) const char *filename,
gboolean modify)
{ {
GMappedFile *file; GMappedFile *file;
GError *error = NULL; GError *error = NULL;
@ -960,7 +1000,7 @@ moo_config_parse_file (MooConfig *config,
} }
result = moo_config_parse_buffer (config, g_mapped_file_get_contents (file), result = moo_config_parse_buffer (config, g_mapped_file_get_contents (file),
g_mapped_file_get_length (file)); g_mapped_file_get_length (file), modify);
g_mapped_file_free (file); g_mapped_file_free (file);
return result; return result;
@ -981,9 +1021,12 @@ void
moo_config_set_bool (MooConfig *config, moo_config_set_bool (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *key, const char *key,
gboolean value) gboolean value,
gboolean modify)
{ {
moo_config_set_value (config, item, key, moo_convert_bool_to_string (value)); moo_config_set_value (config, item, key,
moo_convert_bool_to_string (value),
modify);
} }

View File

@ -59,9 +59,11 @@ void moo_config_free (MooConfig *config);
gboolean moo_config_parse_buffer (MooConfig *config, gboolean moo_config_parse_buffer (MooConfig *config,
const char *string, const char *string,
int len); int len,
gboolean modify);
gboolean moo_config_parse_file (MooConfig *config, gboolean moo_config_parse_file (MooConfig *config,
const char *filename); const char *filename,
gboolean modify);
char *moo_config_format (MooConfig *config); char *moo_config_format (MooConfig *config);
gboolean moo_config_save (MooConfig *config, gboolean moo_config_save (MooConfig *config,
@ -74,9 +76,11 @@ MooConfigItem *moo_config_nth_item (MooConfig *config,
MooConfigItem *moo_config_get_item (MooConfig *config, MooConfigItem *moo_config_get_item (MooConfig *config,
const char *id); const char *id);
MooConfigItem *moo_config_new_item (MooConfig *config, MooConfigItem *moo_config_new_item (MooConfig *config,
const char *id); const char *id,
gboolean modify);
void moo_config_delete_item (MooConfig *config, void moo_config_delete_item (MooConfig *config,
const char *id); const char *id,
gboolean modify);
const char *moo_config_item_get_id (MooConfigItem *item); const char *moo_config_item_get_id (MooConfigItem *item);
const char *moo_config_item_get_value (MooConfigItem *item, const char *moo_config_item_get_value (MooConfigItem *item,
@ -84,19 +88,22 @@ const char *moo_config_item_get_value (MooConfigItem *item,
void moo_config_set_value (MooConfig *config, void moo_config_set_value (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *key, const char *key,
const char *value); const char *value,
gboolean modify);
gboolean moo_config_item_get_bool (MooConfigItem *item, gboolean moo_config_item_get_bool (MooConfigItem *item,
const char *key, const char *key,
gboolean default_val); gboolean default_val);
void moo_config_set_bool (MooConfig *config, void moo_config_set_bool (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *key, const char *key,
gboolean value); gboolean value,
gboolean modify);
const char *moo_config_item_get_content (MooConfigItem *item); const char *moo_config_item_get_content (MooConfigItem *item);
void moo_config_set_item_content (MooConfig *config, void moo_config_set_item_content (MooConfig *config,
MooConfigItem *item, MooConfigItem *item,
const char *content); const char *content,
gboolean modify);
G_END_DECLS G_END_DECLS

View File

@ -122,7 +122,7 @@ moo_parse_user_actions (const char *filename,
config = moo_config_new ("action"); config = moo_config_new ("action");
if (!moo_config_parse_file (config, filename)) if (!moo_config_parse_file (config, filename, FALSE))
{ {
g_object_unref (config); g_object_unref (config);
return; return;