mirror of
https://github.com/profanity-im/profanity.git
synced 2025-01-03 14:57:42 -05:00
Added setting of unique form list values
This commit is contained in:
parent
b6242fbd3d
commit
8cfe80e979
@ -1979,21 +1979,33 @@ cmd_room(gchar **args, struct cmd_help_t help)
|
||||
} else {
|
||||
form_field_type_t field_type = form_get_field_type(current->form, tag);
|
||||
gboolean valid = FALSE;
|
||||
gboolean added = FALSE;
|
||||
switch (field_type) {
|
||||
case FIELD_LIST_MULTI:
|
||||
valid = form_field_contains_option(current->form, tag, value);
|
||||
if (valid == TRUE) {
|
||||
form_add_value(current->form, tag, value);
|
||||
ui_current_print_line("Added %s to %s", value, tag);
|
||||
if (valid) {
|
||||
added = form_add_unique_value(current->form, tag, value);
|
||||
if (added) {
|
||||
ui_current_print_line("Added %s to %s", value, tag);
|
||||
} else {
|
||||
ui_current_print_line("Value %s already selected for %s", value, tag);
|
||||
}
|
||||
} else {
|
||||
ui_current_print_line("Value %s not a valid option for field: %s", value, tag);
|
||||
}
|
||||
break;
|
||||
case FIELD_TEXT_MULTI:
|
||||
case FIELD_JID_MULTI:
|
||||
form_add_value(current->form, tag, value);
|
||||
ui_current_print_line("Added %s to %s", value, tag);
|
||||
break;
|
||||
case FIELD_JID_MULTI:
|
||||
added = form_add_unique_value(current->form, tag, value);
|
||||
if (added) {
|
||||
ui_current_print_line("Added %s to %s", value, tag);
|
||||
} else {
|
||||
ui_current_print_line("JID %s already exists in %s", value, tag);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ui_current_print_line("Add command not valid for field: %s", tag);
|
||||
break;
|
||||
|
@ -430,6 +430,7 @@ _form_set_value(DataForm *form, const char * const tag, char *value)
|
||||
if (g_strcmp0(field->var, var) == 0) {
|
||||
if (g_slist_length(field->values) == 0) {
|
||||
field->values = g_slist_append(field->values, strdup(value));
|
||||
return;
|
||||
} else if (g_slist_length(field->values) == 1) {
|
||||
free(field->values->data);
|
||||
field->values->data = strdup(value);
|
||||
@ -450,19 +451,7 @@ _form_add_value(DataForm *form, const char * const tag, char *value)
|
||||
while (curr != NULL) {
|
||||
FormField *field = curr->data;
|
||||
if (g_strcmp0(field->var, var) == 0) {
|
||||
gboolean already_set = FALSE;
|
||||
GSList *curr_value = field->values;
|
||||
while (curr_value != NULL) {
|
||||
if (g_strcmp0(curr_value->data, value) == 0) {
|
||||
already_set = TRUE;
|
||||
break;
|
||||
}
|
||||
curr_value = g_slist_next(curr_value);
|
||||
}
|
||||
|
||||
if (!already_set) {
|
||||
field->values = g_slist_append(field->values, strdup(value));
|
||||
}
|
||||
field->values = g_slist_append(field->values, strdup(value));
|
||||
return;
|
||||
}
|
||||
curr = g_slist_next(curr);
|
||||
@ -470,6 +459,33 @@ _form_add_value(DataForm *form, const char * const tag, char *value)
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_form_add_unique_value(DataForm *form, const char * const tag, char *value)
|
||||
{
|
||||
char *var = g_hash_table_lookup(form->tag_to_var, tag);
|
||||
if (var != NULL) {
|
||||
GSList *curr = form->fields;
|
||||
while (curr != NULL) {
|
||||
FormField *field = curr->data;
|
||||
if (g_strcmp0(field->var, var) == 0) {
|
||||
GSList *curr_value = field->values;
|
||||
while (curr_value != NULL) {
|
||||
if (g_strcmp0(curr_value->data, value) == 0) {
|
||||
return FALSE;
|
||||
}
|
||||
curr_value = g_slist_next(curr_value);
|
||||
}
|
||||
|
||||
field->values = g_slist_append(field->values, strdup(value));
|
||||
return TRUE;
|
||||
}
|
||||
curr = g_slist_next(curr);
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
_form_remove_value(DataForm *form, const char * const tag, char *value)
|
||||
{
|
||||
@ -508,6 +524,7 @@ form_init_module(void)
|
||||
form_get_form_type_field = _form_get_form_type_field;
|
||||
form_get_field_type = _form_get_field_type;
|
||||
form_set_value = _form_set_value;
|
||||
form_add_unique_value = _form_add_unique_value;
|
||||
form_add_value = _form_add_value;
|
||||
form_remove_value = _form_remove_value;
|
||||
form_field_contains_option = _form_field_contains_option;
|
||||
|
@ -209,6 +209,7 @@ void (*roster_send_remove)(const char * const barejid);
|
||||
void (*form_destroy)(DataForm *form);
|
||||
char * (*form_get_form_type_field)(DataForm *form);
|
||||
void (*form_set_value)(DataForm *form, const char * const tag, char *value);
|
||||
gboolean (*form_add_unique_value)(DataForm *form, const char * const tag, char *value);
|
||||
void (*form_add_value)(DataForm *form, const char * const tag, char *value);
|
||||
void (*form_remove_value)(DataForm *form, const char * const tag, char *value);
|
||||
gboolean (*form_tag_exists)(DataForm *form, const char * const tag);
|
||||
|
Loading…
Reference in New Issue
Block a user