From 7a5069ea382a955fa6d882f11f357372d9af7b36 Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Fri, 21 Apr 2006 03:30:22 -0500 Subject: [PATCH] Added 'modify' argument to MooConfig methods --- moo/mooedit/moousertools.c | 4 +- .../plugins/activestrings/as-plugin-prefs.c | 8 +- moo/mooedit/plugins/activestrings/as-plugin.c | 2 +- moo/mooedit/plugins/completion/completion.c | 4 +- moo/mooutils/mooconfig.c | 95 ++++++++++++++----- moo/mooutils/mooconfig.h | 21 ++-- moo/mooutils/moouseractions.c | 2 +- 7 files changed, 92 insertions(+), 44 deletions(-) diff --git a/moo/mooedit/moousertools.c b/moo/mooedit/moousertools.c index 5005a6db..0cd98738 100644 --- a/moo/mooedit/moousertools.c +++ b/moo/mooedit/moousertools.c @@ -189,10 +189,10 @@ moo_edit_load_tools (FileType type, config = moo_config_new (MOO_USER_TOOL_KEY_ACTION); 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)) - moo_config_parse_file (config, user_file); + moo_config_parse_file (config, user_file, FALSE); n_items = moo_config_n_items (config); diff --git a/moo/mooedit/plugins/activestrings/as-plugin-prefs.c b/moo/mooedit/plugins/activestrings/as-plugin-prefs.c index b4665397..a9a5243a 100644 --- a/moo/mooedit/plugins/activestrings/as-plugin-prefs.c +++ b/moo/mooedit/plugins/activestrings/as-plugin-prefs.c @@ -250,15 +250,15 @@ make_config (GtkTreeModel *model) if (pattern) { - item = moo_config_new_item (config, pattern); + item = moo_config_new_item (config, pattern, FALSE); if (script) - moo_config_set_item_content (config, item, script); + moo_config_set_item_content (config, item, script, FALSE); 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) - moo_config_set_value (config, item, AS_KEY_ENABLED, "no"); + moo_config_set_value (config, item, AS_KEY_ENABLED, "no", FALSE); } g_free (pattern); diff --git a/moo/mooedit/plugins/activestrings/as-plugin.c b/moo/mooedit/plugins/activestrings/as-plugin.c index bd45ac54..0db3d05c 100644 --- a/moo/mooedit/plugins/activestrings/as-plugin.c +++ b/moo/mooedit/plugins/activestrings/as-plugin.c @@ -1044,7 +1044,7 @@ _as_plugin_load (G_GNUC_UNUSED MooPlugin *plugin, 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); return; diff --git a/moo/mooedit/plugins/completion/completion.c b/moo/mooedit/plugins/completion/completion.c index 779d7f15..3126c2ee 100644 --- a/moo/mooedit/plugins/completion/completion.c +++ b/moo/mooedit/plugins/completion/completion.c @@ -193,7 +193,7 @@ cmpl_data_read_config_file (CmplData *data) 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); return; @@ -218,9 +218,7 @@ cmpl_data_read_config_file (CmplData *data) pattern = moo_config_item_get_id (item); prefix = moo_config_item_get_value (item, "prefix"); 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 = script ? script : moo_config_item_get_value (item, "insert_script"); groups = moo_config_item_get_value (item, "group"); groups = groups ? groups : moo_config_item_get_value (item, "groups"); diff --git a/moo/mooutils/mooconfig.c b/moo/mooutils/mooconfig.c index 9fa3e29f..2ee5668d 100644 --- a/moo/mooutils/mooconfig.c +++ b/moo/mooutils/mooconfig.c @@ -28,6 +28,7 @@ struct _MooConfigPrivate { char *id_key; int model_stamp; GHashTable *ids; + gboolean modified; }; typedef struct { @@ -387,26 +388,41 @@ void moo_config_set_value (MooConfig *config, MooConfigItem *item, 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 (key != NULL); norm = normalize_key (key); + old_value = g_hash_table_lookup (item->dict, norm); if (value) { - g_hash_table_insert (item->dict, norm, g_strdup (value)); + if (!old_value || strcmp (value, old_value)) + { + g_hash_table_insert (item->dict, norm, g_strdup (value)); + modified = TRUE; + } } else { - g_hash_table_remove (item->dict, norm); - g_free (norm); + if (old_value) + { + g_hash_table_remove (item->dict, norm); + g_free (norm); + modified = TRUE; + } } - emit_row_changed (config, item); + if (modified && modify) + config->priv->modified = TRUE; + + if (modified) + emit_row_changed (config, item); } @@ -421,18 +437,29 @@ moo_config_item_get_content (MooConfigItem *item) void moo_config_set_item_content (MooConfig *config, MooConfigItem *item, - const char *content) + const char *content, + gboolean modify) { char *tmp; g_return_if_fail (item != NULL); g_return_if_fail (MOO_IS_CONFIG (config)); - tmp = item->content; - item->content = g_strdup (content); - g_free (tmp); + if (item->content == content) + return; - emit_row_changed (config, item); + if ((!item->content && content) || (item->content && !content) || + strcmp (content, item->content)) + { + tmp = item->content; + item->content = g_strdup (content); + g_free (tmp); + + if (modify) + config->priv->modified = TRUE; + + emit_row_changed (config, item); + } } @@ -452,7 +479,8 @@ get_item_index (MooConfig *config, void moo_config_delete_item (MooConfig *config, - const char *id) + const char *id, + gboolean modify) { MooConfigItem *item; GtkTreePath *path; @@ -473,6 +501,9 @@ moo_config_delete_item (MooConfig *config, g_hash_table_remove (config->priv->ids, id); moo_config_item_free (item); + if (modify) + config->priv->modified = TRUE; + config->priv->model_stamp++; path = gtk_tree_path_new_from_indices (index, -1); g_signal_emit_by_name (config, "row-deleted", path); @@ -483,7 +514,8 @@ moo_config_delete_item (MooConfig *config, static MooConfigItem * moo_config_new_item_real (MooConfig *config, const char *id, - GHashTable *dict) + GHashTable *dict, + gboolean modify) { MooConfigItem *item; GtkTreePath *path; @@ -492,7 +524,7 @@ moo_config_new_item_real (MooConfig *config, item = g_hash_table_lookup (config->priv->ids, id); if (item) - moo_config_delete_item (config, id); + moo_config_delete_item (config, id, modify); if (dict) 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_hash_table_insert (config->priv->ids, g_strdup (id), item); + if (modify) + config->priv->modified = TRUE; + config->priv->model_stamp++; ITER_SET (config, &iter, config->priv->items->len - 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 * 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 (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 moo_config_add_items (MooConfig *config, - GSList *list) + GSList *list, + gboolean modify) { while (list) { @@ -766,7 +803,7 @@ moo_config_add_items (MooConfig *config, } 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->start = data->start; item->end = data->end; @@ -783,7 +820,8 @@ skip: static gboolean moo_config_parse_lines (MooConfig *config, char **lines, - guint n_lines) + guint n_lines, + gboolean modify) { ItemData *data; GPtrArray *content; @@ -899,7 +937,7 @@ moo_config_parse_lines (MooConfig *config, 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_free (items); @@ -919,7 +957,8 @@ error: gboolean moo_config_parse_buffer (MooConfig *config, const char *string, - int len) + int len, + gboolean modify) { char **lines; guint n_lines; @@ -932,7 +971,7 @@ moo_config_parse_buffer (MooConfig *config, len = strlen (string); 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); return result; @@ -941,7 +980,8 @@ moo_config_parse_buffer (MooConfig *config, gboolean moo_config_parse_file (MooConfig *config, - const char *filename) + const char *filename, + gboolean modify) { GMappedFile *file; 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), - g_mapped_file_get_length (file)); + g_mapped_file_get_length (file), modify); g_mapped_file_free (file); return result; @@ -981,9 +1021,12 @@ void moo_config_set_bool (MooConfig *config, MooConfigItem *item, 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); } diff --git a/moo/mooutils/mooconfig.h b/moo/mooutils/mooconfig.h index 58ce7833..a03dc863 100644 --- a/moo/mooutils/mooconfig.h +++ b/moo/mooutils/mooconfig.h @@ -59,9 +59,11 @@ void moo_config_free (MooConfig *config); gboolean moo_config_parse_buffer (MooConfig *config, const char *string, - int len); + int len, + gboolean modify); gboolean moo_config_parse_file (MooConfig *config, - const char *filename); + const char *filename, + gboolean modify); char *moo_config_format (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, const char *id); MooConfigItem *moo_config_new_item (MooConfig *config, - const char *id); + const char *id, + gboolean modify); 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_value (MooConfigItem *item, @@ -84,19 +88,22 @@ const char *moo_config_item_get_value (MooConfigItem *item, void moo_config_set_value (MooConfig *config, MooConfigItem *item, const char *key, - const char *value); + const char *value, + gboolean modify); gboolean moo_config_item_get_bool (MooConfigItem *item, const char *key, gboolean default_val); void moo_config_set_bool (MooConfig *config, MooConfigItem *item, const char *key, - gboolean value); + gboolean value, + gboolean modify); const char *moo_config_item_get_content (MooConfigItem *item); void moo_config_set_item_content (MooConfig *config, MooConfigItem *item, - const char *content); + const char *content, + gboolean modify); G_END_DECLS diff --git a/moo/mooutils/moouseractions.c b/moo/mooutils/moouseractions.c index dda12b0f..f43431b2 100644 --- a/moo/mooutils/moouseractions.c +++ b/moo/mooutils/moouseractions.c @@ -122,7 +122,7 @@ moo_parse_user_actions (const char *filename, config = moo_config_new ("action"); - if (!moo_config_parse_file (config, filename)) + if (!moo_config_parse_file (config, filename, FALSE)) { g_object_unref (config); return;