diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2011-04-15 15:52:09 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2011-04-15 15:52:09 +0200 |
commit | 3ebfe02dd9c81b10f47ca258bb2a0ef080dbbbeb (patch) | |
tree | d936b4e68e219e8783e6ac70821da4a9e5a35cd7 /src/lib | |
parent | aa77bb9ee015eace791d5331b5ce5fe9e5e14634 (diff) | |
download | abrt-3ebfe02dd9c81b10f47ca258bb2a0ef080dbbbeb.tar.gz abrt-3ebfe02dd9c81b10f47ca258bb2a0ef080dbbbeb.tar.xz abrt-3ebfe02dd9c81b10f47ca258bb2a0ef080dbbbeb.zip |
Implement the possibility to add text labels to even config GUI. Closes #199.
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/event_config.c | 59 | ||||
-rw-r--r-- | src/lib/event_xml_parser.c | 83 |
2 files changed, 78 insertions, 64 deletions
diff --git a/src/lib/event_config.c b/src/lib/event_config.c index 2b57696d..66f9beba 100644 --- a/src/lib/event_config.c +++ b/src/lib/event_config.c @@ -35,11 +35,12 @@ void free_event_option(event_option_t *p) { if (!p) return; - free(p->name); - free(p->value); - free(p->label); - //free(p->description); - //free(p->allowed_value); + free(p->eo_name); + free(p->eo_value); + free(p->eo_label); + free(p->eo_note_html); + //free(p->eo_description); + //free(p->eo_allowed_value); free(p); } @@ -63,7 +64,8 @@ void free_event_config(event_config_t *p) static int cmp_event_option_name_with_string(gconstpointer a, gconstpointer b) { - return strcmp(((event_option_t *)a)->name, (char *)b); + const event_option_t *evopt = a; + return !evopt->eo_name || strcmp(evopt->eo_name, (char *)b) != 0; } event_option_t *get_event_option_from_list(const char *name, GList *options) @@ -113,20 +115,20 @@ static void load_config_files(const char *dir_path) { event_option_t *opt; GList *elem = g_list_find_custom(event_config->options, name, - &cmp_event_option_name_with_string); + cmp_event_option_name_with_string); if (elem) { opt = elem->data; // log("conf: replacing '%s' value:'%s'->'%s'", name, opt->value, value); - free(opt->value); + free(opt->eo_value); } else { // log("conf: new value %s='%s'", name, value); opt = new_event_option(); - opt->name = xstrdup(name); + opt->eo_name = xstrdup(name); } - opt->value = xstrdup(value); + opt->eo_value = xstrdup(value); if (!elem) event_config->options = g_list_append(event_config->options, opt); } @@ -261,9 +263,9 @@ GList *export_event_config(const char *event_name) for (lopt = config->options; lopt; lopt = lopt->next) { event_option_t *opt = lopt->data; - if (!opt->value) + if (!opt->eo_value) continue; - char *var_val = xasprintf("%s=%s", opt->name, opt->value); + char *var_val = xasprintf("%s=%s", opt->eo_name, opt->eo_value); VERB3 log("Exporting '%s'", var_val); env_list = g_list_prepend(env_list, var_val); putenv(var_val); @@ -288,18 +290,18 @@ void unexport_event_config(GList *env_list) /* return NULL if successful otherwise appropriate error message */ static char *validate_event_option(event_option_t *opt) { - if (!opt->allow_empty && (!opt->value || !opt->value[0])) + if (!opt->eo_allow_empty && (!opt->eo_value || !opt->eo_value[0])) return xstrdup(_("Missing mandatory value")); /* if value is NULL and allow-empty yes than it doesn't make sence to check it */ - if (!opt->value) + if (!opt->eo_value) return NULL; const gchar *s = NULL; - if (!g_utf8_validate(opt->value, -1, &s)) + if (!g_utf8_validate(opt->eo_value, -1, &s)) return xasprintf(_("Invalid utf8 character '%c'"), *s); - switch (opt->type) { + switch (opt->eo_type) { case OPTION_TYPE_TEXT: case OPTION_TYPE_PASSWORD: break; @@ -307,24 +309,26 @@ static char *validate_event_option(event_option_t *opt) { char *endptr; errno = 0; - long r = strtol(opt->value, &endptr, 10); + long r = strtol(opt->eo_value, &endptr, 10); (void) r; - if (errno != 0 || endptr == opt->value || *endptr != '\0') - return xasprintf(_("Invalid number '%s'"), opt->value); + if (errno != 0 || endptr == opt->eo_value || *endptr != '\0') + return xasprintf(_("Invalid number '%s'"), opt->eo_value); break; } case OPTION_TYPE_BOOL: - if (strcmp(opt->value, "yes") != 0 - && strcmp(opt->value, "no") != 0 - && strcmp(opt->value, "on") != 0 - && strcmp(opt->value, "off") != 0 - && strcmp(opt->value, "1") != 0 - && strcmp(opt->value, "0") != 0) + if (strcmp(opt->eo_value, "yes") != 0 + && strcmp(opt->eo_value, "no") != 0 + && strcmp(opt->eo_value, "on") != 0 + && strcmp(opt->eo_value, "off") != 0 + && strcmp(opt->eo_value, "1") != 0 + && strcmp(opt->eo_value, "0") != 0) { - return xasprintf(_("Invalid boolean value '%s'"), opt->value); + return xasprintf(_("Invalid boolean value '%s'"), opt->eo_value); } break; + case OPTION_TYPE_HINT_HTML: + return NULL; default: return xstrdup(_("Unsupported option type")); }; @@ -338,7 +342,6 @@ GHashTable *validate_event(const char *event_name) if (!config) return NULL; - GHashTable *errors = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); GList *li; @@ -347,7 +350,7 @@ GHashTable *validate_event(const char *event_name) event_option_t *opt = (event_option_t *)li->data; char *err = validate_event_option(opt); if (err) - g_hash_table_insert(errors, xstrdup(opt->name), err); + g_hash_table_insert(errors, xstrdup(opt->eo_name), err); } if (g_hash_table_size(errors)) diff --git a/src/lib/event_xml_parser.c b/src/lib/event_xml_parser.c index 8c899f14..859e8951 100644 --- a/src/lib/event_xml_parser.c +++ b/src/lib/event_xml_parser.c @@ -22,8 +22,9 @@ #define EVENT_ELEMENT "event" #define LABEL_ELEMENT "label" #define DESCRIPTION_ELEMENT "description" -#define LONG_DESCR_ELEMENT "long_description" +#define LONG_DESCR_ELEMENT "long-description" #define ALLOW_EMPTY_ELEMENT "allow-empty" +#define NOTE_HTML_ELEMENT "note-html" #define OPTION_ELEMENT "option" //#define ACTION_ELEMENT "action" #define NAME_ELEMENT "name" @@ -39,11 +40,12 @@ struct my_parse_data static const char *const option_types[] = { - "text", - "bool", - "password", - "number", - NULL + [OPTION_TYPE_TEXT ] = "text", + [OPTION_TYPE_BOOL ] = "bool", + [OPTION_TYPE_PASSWORD ] = "password", + [OPTION_TYPE_NUMBER ] = "number", + [OPTION_TYPE_HINT_HTML] = "hint-html", + [OPTION_TYPE_INVALID ] = NULL }; // Return xml:lang value for <foo xml:lang="value"> if value matches current locale, @@ -86,7 +88,9 @@ static char *get_element_lang(struct my_parse_data *parse_data, const gchar **at static int cmp_event_option_name_with_string(gconstpointer a, gconstpointer b) { - return strcmp(((event_option_t *)a)->name, (char *)b); + const event_option_t *evopt = a; + /* "When it is not a match?" */ + return !evopt->eo_name || strcmp(evopt->eo_name, (char *)b) != 0; } static void consume_cur_option(struct my_parse_data *parse_data) @@ -94,39 +98,38 @@ static void consume_cur_option(struct my_parse_data *parse_data) event_option_t *opt = parse_data->cur_option; if (!opt) return; - parse_data->cur_option = NULL; - if (!opt->name) - { -//TODO: "option w/o name" error msg? - free_event_option(opt); - return; - } - event_config_t *event_config = parse_data->event_config; - GList *elem = g_list_find_custom(event_config->options, opt->name, - &cmp_event_option_name_with_string); + + /* Example of "nameless" option: <option type="hint-html"> + * The remaining code does not like "nameless" options + * (strcmp would segfault, etc), so provide invented name: + */ + if (!opt->eo_name) + opt->eo_name = xasprintf("%u", (unsigned)g_list_length(event_config->options)); + + GList *elem = g_list_find_custom(event_config->options, opt->eo_name, cmp_event_option_name_with_string); if (elem) { /* we already have option with such name */ event_option_t *old_opt = elem->data; - if (old_opt->value) + if (old_opt->eo_value) { /* ...and it already has a value, which * overrides xml-defined default one: */ - free(opt->value); - opt->value = old_opt->value; - old_opt->value = NULL; + free(opt->eo_value); + opt->eo_value = old_opt->eo_value; + old_opt->eo_value = NULL; } - //log("xml: replacing '%s' value:'%s'->'%s'", opt->name, old_opt->value, opt->value); + //log("xml: replacing '%s' value:'%s'->'%s'", opt->eo_name, old_opt->eo_value, opt->eo_value); free_event_option(old_opt); elem->data = opt; } else { - //log("xml: new value %s='%s'", opt->name, opt->value); + //log("xml: new value %s='%s'", opt->eo_name, opt->eo_value); event_config->options = g_list_append(event_config->options, opt); } } @@ -159,8 +162,8 @@ static void start_element(GMarkupParseContext *context, VERB2 log("attr: %s:%s", attribute_names[i], attribute_values[i]); if (strcmp(attribute_names[i], "name") == 0) { - free(opt->name); - opt->name = xstrdup(attribute_values[i]); + free(opt->eo_name); + opt->eo_name = xstrdup(attribute_values[i]); } else if (strcmp(attribute_names[i], "type") == 0) { @@ -168,7 +171,7 @@ static void start_element(GMarkupParseContext *context, for (type = OPTION_TYPE_TEXT; type < OPTION_TYPE_INVALID; ++type) { if (strcmp(option_types[type], attribute_values[i]) == 0) - opt->type = type; + opt->eo_type = type; } } } @@ -229,10 +232,10 @@ static void text(GMarkupParseContext *context, * OR the label is still not set and we found the default value */ if (parse_data->attribute_lang[0] != '\0' - || !opt->label /* && parse_data->attribute_lang is "" - always true */ + || !opt->eo_label /* && parse_data->attribute_lang is "" - always true */ ) { - free(opt->label); - opt->label = text_copy; + free(opt->eo_label); + opt->eo_label = text_copy; } } return; @@ -246,23 +249,31 @@ static void text(GMarkupParseContext *context, if (strcmp(inner_element, DEFAULT_VALUE_ELEMENT) == 0) { VERB2 log("default value:'%s'", text_copy); - free(opt->value); - opt->value = text_copy; + free(opt->eo_value); + opt->eo_value = text_copy; + return; + } + + if (strcmp(inner_element, NOTE_HTML_ELEMENT) == 0) + { + VERB2 log("html note:'%s'", text_copy); + free(opt->eo_note_html); + opt->eo_note_html = text_copy; return; } if (strcmp(inner_element, ALLOW_EMPTY_ELEMENT) == 0) { VERB2 log("allow-empty:'%s'", text_copy); - opt->allow_empty = string_to_bool(text_copy); + opt->eo_allow_empty = string_to_bool(text_copy); return; } /* if (strcmp(inner_element, DESCRIPTION_ELEMENT) == 0) { VERB2 log("tooltip:'%s'", text_copy); - free(opt->description); - opt->description = text_copy; + free(opt->eo_description); + opt->eo_description = text_copy; return; } */ @@ -274,8 +285,8 @@ static void text(GMarkupParseContext *context, if (strcmp(inner_element, ACTION_ELEMENT) == 0) { VERB2 log("action description:'%s'", text_copy); - free(ui->action); - ui->action = text_copy; + free(ui->eo_action); + ui->eo_action = text_copy; return; } */ |