Moved default boolean values into the MooConfig

master
Yevgen Muntyan 2006-04-22 21:58:43 -05:00
parent cce718f26f
commit 9e4ae7a3e8
9 changed files with 138 additions and 84 deletions

View File

@ -236,7 +236,7 @@ config_item_get_langs (MooConfigItem *item)
char **pieces, **p;
GSList *list = NULL;
string = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_LANG);
string = moo_config_item_get (item, MOO_USER_TOOL_KEY_LANG);
if (!string)
return NULL;
@ -265,7 +265,7 @@ config_item_get_command (MooConfigItem *item)
code = moo_config_item_get_content (item);
g_return_val_if_fail (code != NULL, NULL);
type = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_COMMAND);
type = moo_config_item_get (item, MOO_USER_TOOL_KEY_COMMAND);
if (type)
cmd_type = moo_command_type_parse (type);
@ -285,7 +285,7 @@ config_item_get_options (MooConfigItem *item)
char **pieces, **p;
ActionOptions opts = 0;
string = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_OPTIONS);
string = moo_config_item_get (item, MOO_USER_TOOL_KEY_OPTIONS);
if (!string)
return 0;
@ -337,7 +337,7 @@ load_config_item (FileType type,
g_return_if_fail (item != NULL);
os = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_OS);
os = moo_config_item_get (item, MOO_USER_TOOL_KEY_OS);
if (os)
{
@ -354,10 +354,10 @@ load_config_item (FileType type,
g_free (norm);
}
name = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_ACTION);
label = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_LABEL);
accel = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_ACCEL);
pos = moo_config_item_get_value (item, MOO_USER_TOOL_KEY_POSITION);
name = moo_config_item_get (item, MOO_USER_TOOL_KEY_ACTION);
label = moo_config_item_get (item, MOO_USER_TOOL_KEY_LABEL);
accel = moo_config_item_get (item, MOO_USER_TOOL_KEY_ACCEL);
pos = moo_config_item_get (item, MOO_USER_TOOL_KEY_POSITION);
g_return_if_fail (name != NULL);
cmd = config_item_get_command (item);

View File

@ -105,13 +105,13 @@ _as_plugin_prefs_page (MooPlugin *plugin)
new_item_func, NULL);
moo_config_add_widget (treeview, moo_glade_xml_get_widget (xml, "pattern"),
AS_KEY_PATTERN, TRUE, FALSE);
AS_KEY_PATTERN, TRUE);
moo_config_add_widget (treeview, moo_glade_xml_get_widget (xml, "lang"),
AS_KEY_LANG, FALSE, FALSE);
AS_KEY_LANG, FALSE);
moo_config_add_widget (treeview, moo_glade_xml_get_widget (xml, "enabled"),
AS_KEY_ENABLED, TRUE, TRUE);
AS_KEY_ENABLED, TRUE);
moo_config_add_widget (treeview, moo_glade_xml_get_widget (xml, "script"),
NULL, FALSE, FALSE);
NULL, FALSE);
g_object_unref (xml);
return page;
@ -131,8 +131,8 @@ pattern_data_func (G_GNUC_UNUSED GtkTreeViewColumn *column,
gtk_tree_model_get (model, iter, 0, &item, -1);
g_return_if_fail (item != NULL);
pattern = moo_config_item_get_value (item, AS_KEY_PATTERN);
enabled = moo_config_item_get_bool (item, AS_KEY_ENABLED, TRUE);
pattern = moo_config_item_get (item, AS_KEY_PATTERN);
enabled = moo_config_get_bool (MOO_CONFIG (model), item, AS_KEY_ENABLED);
g_object_set (cell, "text", pattern,
"foreground", enabled ? NULL : "grey",
@ -180,6 +180,8 @@ prefs_page_init (MooGladeXML *xml)
if (!config)
config = moo_config_new ();
moo_config_set_default_bool (config, AS_KEY_ENABLED, TRUE);
treeview = moo_glade_xml_get_widget (xml, "treeview");
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (config));
moo_tree_view_select_first (GTK_TREE_VIEW (treeview));
@ -193,5 +195,5 @@ new_item_func (MooConfig *config,
MooConfigItem *item,
G_GNUC_UNUSED gpointer data)
{
moo_config_set_value (config, item, AS_KEY_PATTERN, "?", TRUE);
moo_config_set (config, item, AS_KEY_PATTERN, "?", TRUE);
}

View File

@ -727,9 +727,9 @@ as_plugin_parse_config (MooConfig *config)
ASInfo *info;
MooConfigItem *item = moo_config_nth_item (config, i);
pattern = moo_config_item_get_value (item, AS_KEY_PATTERN);
lang = moo_config_item_get_value (item, AS_KEY_LANG);
enabled = moo_config_item_get_bool (item, AS_KEY_ENABLED, TRUE);
pattern = moo_config_item_get (item, AS_KEY_PATTERN);
lang = moo_config_item_get (item, AS_KEY_LANG);
enabled = moo_config_get_bool (config, item, AS_KEY_ENABLED);
script = moo_config_item_get_content (item);
if (!pattern)
@ -1097,6 +1097,10 @@ _as_plugin_load_config (void)
g_critical ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
else
{
moo_config_set_default_bool (config, AS_KEY_ENABLED, TRUE);
}
g_free (file);
return config;

View File

@ -210,13 +210,13 @@ cmpl_data_read_config_file (CmplData *data)
item = moo_config_nth_item (config, i);
pattern = moo_config_item_get_value (item, "pattern");
prefix = moo_config_item_get_value (item, "prefix");
suffix = moo_config_item_get_value (item, "insert-suffix");
script = moo_config_item_get_value (item, "insert-script");
pattern = moo_config_item_get (item, "pattern");
prefix = moo_config_item_get (item, "prefix");
suffix = moo_config_item_get (item, "insert-suffix");
script = moo_config_item_get (item, "insert-script");
groups = moo_config_item_get_value (item, "group");
groups = groups ? groups : moo_config_item_get_value (item, "groups");
groups = moo_config_item_get (item, "group");
groups = groups ? groups : moo_config_item_get (item, "groups");
groups = groups ? groups : "0";
if (!pattern)

View File

@ -27,6 +27,7 @@ struct _MooConfigPrivate {
GPtrArray *items;
int model_stamp;
gboolean modified;
GHashTable *default_bool;
};
struct _MooConfigItem {
@ -96,6 +97,8 @@ moo_config_init (MooConfig *config)
config->priv = g_new0 (MooConfigPrivate, 1);
config->priv->items = g_ptr_array_new ();
config->priv->model_stamp = 1;
config->priv->default_bool =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
@ -108,7 +111,7 @@ moo_config_finalize (GObject *object)
(GFunc) moo_config_item_free,
NULL);
g_ptr_array_free (config->priv->items, TRUE);
g_hash_table_destroy (config->priv->default_bool);
g_free (config->priv);
moo_config_parent_class->finalize (object);
@ -370,8 +373,8 @@ normalize_key (const char *string)
const char *
moo_config_item_get_value (MooConfigItem *item,
const char *key)
moo_config_item_get (MooConfigItem *item,
const char *key)
{
char *norm;
const char *value;
@ -426,11 +429,11 @@ is_empty_string (const char *string)
void
moo_config_set_value (MooConfig *config,
MooConfigItem *item,
const char *key,
const char *value,
gboolean modify)
moo_config_set (MooConfig *config,
MooConfigItem *item,
const char *key,
const char *value,
gboolean modify)
{
char *norm, *old_value, *new_value = NULL;
gboolean modified = FALSE;
@ -1124,13 +1127,33 @@ moo_config_parse_file (MooConfig *config,
}
gboolean
moo_config_item_get_bool (MooConfigItem *item,
const char *key,
gboolean default_val)
static gboolean
get_default_bool (MooConfig *config,
const char *key)
{
return moo_convert_string_to_bool (moo_config_item_get_value (item, key),
default_val);
char *norm_key = normalize_key (key);
gpointer val = g_hash_table_lookup (config->priv->default_bool, key);
g_free (norm_key);
return GPOINTER_TO_INT (val);
}
gboolean
moo_config_get_bool (MooConfig *config,
MooConfigItem *item,
const char *key)
{
const char *string;
gboolean default_val;
g_return_val_if_fail (MOO_IS_CONFIG (config), FALSE);
g_return_val_if_fail (item != NULL, FALSE);
g_return_val_if_fail (key != NULL, FALSE);
string = moo_config_item_get (item, key);
default_val = get_default_bool (config, key);
return moo_convert_string_to_bool (string, default_val);
}
@ -1141,9 +1164,39 @@ moo_config_set_bool (MooConfig *config,
gboolean value,
gboolean modify)
{
moo_config_set_value (config, item, key,
moo_convert_bool_to_string (value),
modify);
gboolean default_val;
g_return_if_fail (MOO_IS_CONFIG (config));
g_return_if_fail (item != NULL);
g_return_if_fail (key != NULL);
default_val = get_default_bool (config, key);
value = value != 0;
if (default_val == value)
moo_config_set (config, item, key, NULL, modify);
else
moo_config_set (config, item, key,
value ? "true" : "false",
modify);
}
void
moo_config_set_default_bool (MooConfig *config,
const char *key,
gboolean value)
{
char *norm_key;
g_return_if_fail (MOO_IS_CONFIG (config));
g_return_if_fail (key != NULL);
norm_key = normalize_key (key);
value = value != 0;
g_hash_table_insert (config->priv->default_bool, norm_key,
GINT_TO_POINTER (value));
}

View File

@ -98,16 +98,20 @@ void moo_config_move_item (MooConfig *config,
guint new_index,
gboolean modify);
const char *moo_config_item_get_value (MooConfigItem *item,
void moo_config_set_default_bool (MooConfig *config,
const char *key,
gboolean value);
const char *moo_config_item_get (MooConfigItem *item,
const char *key);
void moo_config_set_value (MooConfig *config,
void moo_config_set (MooConfig *config,
MooConfigItem *item,
const char *key,
const char *value,
gboolean modify);
gboolean moo_config_item_get_bool (MooConfigItem *item,
const char *key,
gboolean default_val);
gboolean moo_config_get_bool (MooConfig *config,
MooConfigItem *item,
const char *key);
void moo_config_set_bool (MooConfig *config,
MooConfigItem *item,
const char *key,

View File

@ -133,10 +133,10 @@ moo_parse_user_actions (const char *filename,
MooCommandType cmd_type = 0;
MooConfigItem *item = moo_config_nth_item (config, i);
name = moo_config_item_get_value (item, "action");
label = moo_config_item_get_value (item, "label");
accel = moo_config_item_get_value (item, "accel");
type = moo_config_item_get_value (item, "command");
name = moo_config_item_get (item, "action");
label = moo_config_item_get (item, "label");
accel = moo_config_item_get (item, "accel");
type = moo_config_item_get (item, "command");
code = moo_config_item_get_content (item);
if (!name)

View File

@ -33,7 +33,6 @@ moo_tree_view_select_first (GtkTreeView *tree_view)
typedef struct {
char *key;
gboolean default_val;
gboolean update_live;
gulong handler;
MooConfigWidgetToItem widget_to_item;
@ -53,8 +52,7 @@ typedef struct {
} Widgets;
static WidgetInfo *widget_info_new (const char *key,
gboolean update_live,
gboolean default_bool);
gboolean update_live);
static void widget_info_free (WidgetInfo *info);
static Widgets *widgets_new (GtkWidget *tree_view,
GtkWidget *new_btn,
@ -142,11 +140,7 @@ toggle_button_to_item (GtkToggleButton *button,
g_return_if_fail (info != NULL);
value = gtk_toggle_button_get_active (button);
if (value == info->default_val)
moo_config_set_value (config, item, info->key, NULL, TRUE);
else
moo_config_set_bool (config, item, info->key, value, TRUE);
moo_config_set_bool (config, item, info->key, value, TRUE);
}
static void
@ -162,7 +156,7 @@ entry_to_item (GtkEntry *entry,
text = gtk_entry_get_text (entry);
if (info->key)
moo_config_set_value (config, item, info->key, text, TRUE);
moo_config_set (config, item, info->key, text, TRUE);
else
moo_config_set_item_content (config, item, text, TRUE);
}
@ -190,7 +184,7 @@ text_view_to_item (GtkTextView *view,
}
if (info->key)
moo_config_set_value (config, item, info->key, text, TRUE);
moo_config_set (config, item, info->key, text, TRUE);
else
moo_config_set_item_content (config, item, text, TRUE);
@ -212,7 +206,8 @@ get_widget_to_item_func (GtkWidget *widget)
static void
item_to_toggle_button (MooConfigItem *item,
item_to_toggle_button (MooConfig *config,
MooConfigItem *item,
GtkToggleButton *button)
{
gboolean value;
@ -220,12 +215,13 @@ item_to_toggle_button (MooConfigItem *item,
g_return_if_fail (info != NULL);
value = moo_config_item_get_bool (item, info->key, info->default_val);
value = moo_config_get_bool (config, item, info->key);
gtk_toggle_button_set_active (button, value);
}
static void
item_to_entry (MooConfigItem *item,
item_to_entry (G_GNUC_UNUSED MooConfig *config,
MooConfigItem *item,
GtkEntry *entry)
{
const char *text;
@ -234,7 +230,7 @@ item_to_entry (MooConfigItem *item,
g_return_if_fail (info != NULL);
if (info->key)
text = moo_config_item_get_value (item, info->key);
text = moo_config_item_get (item, info->key);
else
text = moo_config_item_get_content (item);
@ -242,7 +238,8 @@ item_to_entry (MooConfigItem *item,
}
static void
item_to_text_view (MooConfigItem *item,
item_to_text_view (G_GNUC_UNUSED MooConfig *config,
MooConfigItem *item,
GtkTextView *view)
{
const char *text;
@ -252,7 +249,7 @@ item_to_text_view (MooConfigItem *item,
g_return_if_fail (info != NULL);
if (info->key)
text = moo_config_item_get_value (item, info->key);
text = moo_config_item_get (item, info->key);
else
text = moo_config_item_get_content (item);
@ -281,8 +278,7 @@ moo_config_add_widget_full (GtkWidget *tree_view,
MooConfigWidgetToItem widget_to_item_func,
MooConfigItemToWidget item_to_widget_func,
gpointer data,
gboolean update_live,
gboolean default_bool)
gboolean update_live)
{
Widgets *widgets;
WidgetInfo *info;
@ -306,7 +302,7 @@ moo_config_add_widget_full (GtkWidget *tree_view,
g_return_if_fail (item_to_widget_func != NULL);
}
info = widget_info_new (key, update_live, default_bool);
info = widget_info_new (key, update_live);
info->widget_to_item = widget_to_item_func;
info->item_to_widget = item_to_widget_func;
info->data = data;
@ -335,12 +331,10 @@ void
moo_config_add_widget (GtkWidget *tree_view,
GtkWidget *widget,
const char *key,
gboolean update_live,
gboolean default_bool)
gboolean update_live)
{
return moo_config_add_widget_full (tree_view, widget, key,
NULL, NULL, NULL,
update_live, default_bool);
NULL, NULL, NULL, update_live);
}
@ -389,14 +383,12 @@ moo_config_disconnect_widget (GtkWidget *treeview)
static WidgetInfo *
widget_info_new (const char *key,
gboolean update_live,
gboolean default_bool)
gboolean update_live)
{
WidgetInfo *info = g_new0 (WidgetInfo, 1);
info->key = g_strdup (key);
info->update_live = update_live;
info->default_val = default_bool;
return info;
}
@ -534,7 +526,7 @@ set_from_model (Widgets *widgets,
if (item)
{
info->item_to_widget (item, widget, info->data);
info->item_to_widget (MOO_CONFIG (model), item, widget, info->data);
}
else
{
@ -754,8 +746,8 @@ entry_changed (Widgets *widgets,
g_return_if_fail (item != NULL);
if (info->key)
moo_config_set_value (MOO_CONFIG (model), item,
info->key, text, TRUE);
moo_config_set (MOO_CONFIG (model), item,
info->key, text, TRUE);
else
moo_config_set_item_content (MOO_CONFIG (model), item,
text, TRUE);

View File

@ -27,7 +27,8 @@ typedef void (*MooConfigWidgetToItem) (GtkWidget *widget,
MooConfig *config,
MooConfigItem *item,
gpointer data);
typedef void (*MooConfigItemToWidget) (MooConfigItem *item,
typedef void (*MooConfigItemToWidget) (MooConfig *config,
MooConfigItem *item,
GtkWidget *widget,
gpointer data);
@ -41,16 +42,14 @@ void moo_config_connect_widget (GtkWidget *tree_view,
void moo_config_add_widget (GtkWidget *tree_view,
GtkWidget *widget,
const char *key,
gboolean update_live,
gboolean default_bool);
gboolean update_live);
void moo_config_add_widget_full (GtkWidget *tree_view,
GtkWidget *widget,
const char *key,
MooConfigWidgetToItem widget_to_item_func,
MooConfigItemToWidget item_to_widget_func,
gpointer data,
gboolean update_live,
gboolean default_bool);
gboolean update_live);
void moo_config_disconnect_widget(GtkWidget *tree_view);
void moo_config_update_tree_view (GtkWidget *tree_view,
GtkTreeModel *model,