1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00
profanity/src/xmpp/form.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

706 lines
21 KiB
C
Raw Permalink Normal View History

2014-09-05 16:04:16 -04:00
/*
* form.c
2019-11-13 06:11:05 -05:00
* vim: expandtab:ts=4:sts=4:sw=4
2014-09-05 16:04:16 -04:00
*
2019-01-22 05:31:45 -05:00
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
2014-09-05 16:04:16 -04:00
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2016-07-23 20:14:49 -04:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
2014-09-05 16:04:16 -04:00
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
*/
2016-03-31 16:05:02 -04:00
#include "config.h"
2014-09-05 16:04:16 -04:00
#include <string.h>
#include <stdlib.h>
2014-09-05 16:04:16 -04:00
#include <strophe.h>
2014-09-06 17:40:57 -04:00
#include <glib.h>
2014-09-05 16:04:16 -04:00
2014-09-06 17:40:57 -04:00
#include "log.h"
2014-09-05 16:04:16 -04:00
#include "xmpp/xmpp.h"
#include "xmpp/stanza.h"
#include "xmpp/connection.h"
2014-09-05 16:04:16 -04:00
2014-09-06 17:40:57 -04:00
static gboolean
2020-07-07 08:18:57 -04:00
_is_valid_form_element(xmpp_stanza_t* stanza)
2014-09-06 17:40:57 -04:00
{
2020-07-07 08:18:57 -04:00
const char* name = xmpp_stanza_get_name(stanza);
2014-09-06 17:40:57 -04:00
if (g_strcmp0(name, STANZA_NAME_X) != 0) {
log_error("Error parsing form, root element not <x/>.");
return FALSE;
}
2014-09-05 16:04:16 -04:00
2020-07-07 08:18:57 -04:00
const char* ns = xmpp_stanza_get_ns(stanza);
2014-09-06 17:40:57 -04:00
if (g_strcmp0(ns, STANZA_NS_DATA) != 0) {
log_error("Error parsing form, namespace not %s.", STANZA_NS_DATA);
return FALSE;
}
2020-07-07 08:18:57 -04:00
const char* type = xmpp_stanza_get_type(stanza);
if ((g_strcmp0(type, "form") != 0) && (g_strcmp0(type, "submit") != 0) && (g_strcmp0(type, "cancel") != 0) && (g_strcmp0(type, "result") != 0)) {
2014-09-06 17:40:57 -04:00
log_error("Error parsing form, unknown type.");
return FALSE;
}
return TRUE;
}
2015-10-25 20:14:23 -04:00
static DataForm*
2014-09-06 17:40:57 -04:00
_form_new(void)
2014-09-05 16:04:16 -04:00
{
2020-07-07 08:18:57 -04:00
DataForm* form = calloc(1, sizeof(DataForm));
2014-09-06 17:40:57 -04:00
return form;
}
2015-10-25 20:14:23 -04:00
static FormField*
2014-09-06 17:40:57 -04:00
_field_new(void)
{
2020-07-07 08:18:57 -04:00
FormField* field = calloc(1, sizeof(FormField));
2014-09-06 17:40:57 -04:00
return field;
}
2015-10-25 20:14:23 -04:00
static char*
2020-07-07 08:18:57 -04:00
_get_property(xmpp_stanza_t* const stanza, const char* const property)
2014-09-06 17:40:57 -04:00
{
2020-07-07 08:18:57 -04:00
char* result = NULL;
xmpp_ctx_t* ctx = connection_get_ctx();
2014-09-05 16:04:16 -04:00
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* child = xmpp_stanza_get_child_by_name(stanza, property);
2014-09-21 16:43:42 -04:00
if (child) {
2020-07-07 08:18:57 -04:00
char* child_text = xmpp_stanza_get_text(child);
2014-09-21 16:43:42 -04:00
if (child_text) {
2014-09-06 17:40:57 -04:00
result = strdup(child_text);
xmpp_free(ctx, child_text);
}
}
return result;
}
2015-10-25 20:14:23 -04:00
static char*
2020-07-07 08:18:57 -04:00
_get_attr(xmpp_stanza_t* const stanza, const char* const attr)
2014-09-06 17:40:57 -04:00
{
2020-07-07 08:18:57 -04:00
const char* result = xmpp_stanza_get_attribute(stanza, attr);
2014-09-21 16:43:42 -04:00
if (result) {
2014-09-06 17:40:57 -04:00
return strdup(result);
} else {
return NULL;
}
}
2014-09-05 16:04:16 -04:00
2014-09-06 17:40:57 -04:00
static gboolean
2020-07-07 08:18:57 -04:00
_is_required(xmpp_stanza_t* const stanza)
2014-09-06 17:40:57 -04:00
{
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* child = xmpp_stanza_get_child_by_name(stanza, "required");
2014-09-21 16:43:42 -04:00
if (child) {
2014-09-06 17:40:57 -04:00
return TRUE;
} else {
return FALSE;
2014-09-05 16:04:16 -04:00
}
2014-09-06 17:40:57 -04:00
}
2014-09-05 16:04:16 -04:00
2014-09-10 08:18:36 -04:00
static form_field_type_t
2020-07-07 08:18:57 -04:00
_get_field_type(const char* const type)
2014-09-10 08:18:36 -04:00
{
if (g_strcmp0(type, "hidden") == 0) {
return FIELD_HIDDEN;
}
if (g_strcmp0(type, "text-single") == 0) {
return FIELD_TEXT_SINGLE;
}
if (g_strcmp0(type, "text-private") == 0) {
return FIELD_TEXT_PRIVATE;
}
if (g_strcmp0(type, "text-multi") == 0) {
return FIELD_TEXT_MULTI;
}
if (g_strcmp0(type, "boolean") == 0) {
return FIELD_BOOLEAN;
}
if (g_strcmp0(type, "list-single") == 0) {
return FIELD_LIST_SINGLE;
}
if (g_strcmp0(type, "list-multi") == 0) {
2014-09-13 19:18:06 -04:00
return FIELD_LIST_MULTI;
2014-09-10 08:18:36 -04:00
}
if (g_strcmp0(type, "jid-single") == 0) {
return FIELD_JID_SINGLE;
}
if (g_strcmp0(type, "jid-multi") == 0) {
return FIELD_JID_MULTI;
}
if (g_strcmp0(type, "fixed") == 0) {
return FIELD_FIXED;
}
return FIELD_UNKNOWN;
}
2015-10-25 20:14:23 -04:00
DataForm*
2020-07-07 08:18:57 -04:00
form_create(xmpp_stanza_t* const form_stanza)
2014-09-06 17:40:57 -04:00
{
2020-07-07 08:18:57 -04:00
xmpp_ctx_t* ctx = connection_get_ctx();
2014-09-05 16:04:16 -04:00
2014-09-06 17:40:57 -04:00
if (!_is_valid_form_element(form_stanza)) {
return NULL;
}
2014-09-05 16:04:16 -04:00
2020-07-07 08:18:57 -04:00
DataForm* form = _form_new();
2014-09-06 17:40:57 -04:00
form->type = _get_attr(form_stanza, "type");
form->title = _get_property(form_stanza, "title");
form->instructions = _get_property(form_stanza, "instructions");
2014-09-11 18:44:39 -04:00
form->var_to_tag = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
form->tag_to_var = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
form->tag_ac = autocomplete_new();
form->modified = FALSE;
2014-09-11 18:44:39 -04:00
int tag_num = 1;
2014-09-06 17:40:57 -04:00
// get fields
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* form_child = xmpp_stanza_get_children(form_stanza);
2014-09-21 16:43:42 -04:00
while (form_child) {
2020-07-07 08:18:57 -04:00
const char* child_name = xmpp_stanza_get_name(form_child);
2014-09-06 17:40:57 -04:00
if (g_strcmp0(child_name, "field") == 0) {
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* field_stanza = form_child;
2014-09-06 17:40:57 -04:00
2020-07-07 08:18:57 -04:00
FormField* field = _field_new();
2014-09-06 17:40:57 -04:00
field->label = _get_attr(field_stanza, "label");
field->type = _get_attr(field_stanza, "type");
2014-09-10 08:18:36 -04:00
field->type_t = _get_field_type(field->type);
field->value_ac = autocomplete_new();
2014-09-11 18:44:39 -04:00
2014-09-06 17:40:57 -04:00
field->var = _get_attr(field_stanza, "var");
2014-09-13 16:46:54 -04:00
2015-05-04 18:17:18 -04:00
if (field->type_t != FIELD_HIDDEN && field->var) {
2020-07-07 08:18:57 -04:00
GString* tag = g_string_new("");
2014-09-13 16:46:54 -04:00
g_string_printf(tag, "field%d", tag_num++);
g_hash_table_insert(form->var_to_tag, strdup(field->var), strdup(tag->str));
g_hash_table_insert(form->tag_to_var, strdup(tag->str), strdup(field->var));
autocomplete_add(form->tag_ac, tag->str);
g_string_free(tag, TRUE);
}
2014-09-11 18:44:39 -04:00
2014-09-06 17:40:57 -04:00
field->description = _get_property(field_stanza, "desc");
field->required = _is_required(field_stanza);
// handle repeated field children
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* field_child = xmpp_stanza_get_children(field_stanza);
int value_index = 1;
2014-09-21 16:43:42 -04:00
while (field_child) {
child_name = xmpp_stanza_get_name(field_child);
2014-09-06 17:40:57 -04:00
// handle values
if (g_strcmp0(child_name, "value") == 0) {
2020-07-07 08:18:57 -04:00
char* value = xmpp_stanza_get_text(field_child);
2014-09-21 16:43:42 -04:00
if (value) {
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_TEXT_MULTI) {
2020-07-07 08:18:57 -04:00
GString* ac_val = g_string_new("");
g_string_printf(ac_val, "val%d", value_index++);
autocomplete_add(field->value_ac, ac_val->str);
g_string_free(ac_val, TRUE);
}
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_add(field->value_ac, value);
}
xmpp_free(ctx, value);
2014-09-06 17:40:57 -04:00
}
2020-07-07 08:18:57 -04:00
// handle options
2014-09-06 17:40:57 -04:00
} else if (g_strcmp0(child_name, "option") == 0) {
2020-07-07 08:18:57 -04:00
FormOption* option = malloc(sizeof(FormOption));
2014-09-06 17:40:57 -04:00
option->label = _get_attr(field_child, "label");
option->value = _get_property(field_child, "value");
2014-09-05 16:04:16 -04:00
if ((field->type_t == FIELD_LIST_SINGLE) || (field->type_t == FIELD_LIST_MULTI)) {
autocomplete_add(field->value_ac, option->value);
}
2014-09-06 17:40:57 -04:00
field->options = g_slist_append(field->options, option);
2014-09-05 16:04:16 -04:00
}
2014-09-06 17:40:57 -04:00
field_child = xmpp_stanza_get_next(field_child);
2014-09-05 16:04:16 -04:00
}
2014-09-06 17:40:57 -04:00
form->fields = g_slist_append(form->fields, field);
2014-09-05 16:04:16 -04:00
}
2014-09-06 17:40:57 -04:00
form_child = xmpp_stanza_get_next(form_child);
2014-09-05 16:04:16 -04:00
}
2014-09-06 17:40:57 -04:00
return form;
2014-09-05 16:04:16 -04:00
}
2015-10-25 20:14:23 -04:00
xmpp_stanza_t*
2020-07-07 08:18:57 -04:00
form_create_submission(DataForm* form)
{
2020-07-07 08:18:57 -04:00
xmpp_ctx_t* ctx = connection_get_ctx();
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* x = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(x, STANZA_NAME_X);
xmpp_stanza_set_ns(x, STANZA_NS_DATA);
xmpp_stanza_set_type(x, "submit");
2020-07-07 08:18:57 -04:00
GSList* curr_field = form->fields;
2014-09-21 16:43:42 -04:00
while (curr_field) {
2020-07-07 08:18:57 -04:00
FormField* field = curr_field->data;
if (field->type_t != FIELD_FIXED) {
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* field_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(field_stanza, "field");
xmpp_stanza_set_attribute(field_stanza, "var", field->var);
2020-07-07 08:18:57 -04:00
xmpp_stanza_t* value_stanza = NULL;
GSList* curr_value = NULL;
switch (field->type_t) {
2020-07-07 08:18:57 -04:00
case FIELD_HIDDEN:
case FIELD_TEXT_SINGLE:
case FIELD_TEXT_PRIVATE:
case FIELD_BOOLEAN:
case FIELD_LIST_SINGLE:
case FIELD_JID_SINGLE:
value_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(value_stanza, "value");
if (field->values) {
if (field->values->data) {
xmpp_stanza_t* text_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text_stanza, field->values->data);
xmpp_stanza_add_child(value_stanza, text_stanza);
xmpp_stanza_release(text_stanza);
}
}
xmpp_stanza_add_child(field_stanza, value_stanza);
xmpp_stanza_release(value_stanza);
break;
case FIELD_TEXT_MULTI:
case FIELD_LIST_MULTI:
case FIELD_JID_MULTI:
curr_value = field->values;
while (curr_value) {
char* value = curr_value->data;
value_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(value_stanza, "value");
2020-07-07 08:18:57 -04:00
if (value) {
xmpp_stanza_t* text_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text_stanza, value);
xmpp_stanza_add_child(value_stanza, text_stanza);
xmpp_stanza_release(text_stanza);
}
2020-07-07 08:18:57 -04:00
xmpp_stanza_add_child(field_stanza, value_stanza);
xmpp_stanza_release(value_stanza);
2020-07-07 08:18:57 -04:00
curr_value = g_slist_next(curr_value);
}
break;
case FIELD_FIXED:
default:
break;
}
xmpp_stanza_add_child(x, field_stanza);
xmpp_stanza_release(field_stanza);
}
curr_field = g_slist_next(curr_field);
}
return x;
}
2014-09-06 17:40:57 -04:00
static void
2020-07-07 08:18:57 -04:00
_free_option(FormOption* option)
2014-09-06 17:40:57 -04:00
{
2014-09-21 16:43:42 -04:00
if (option) {
free(option->label);
free(option->value);
2014-09-06 17:40:57 -04:00
free(option);
}
}
static void
2020-07-07 08:18:57 -04:00
_free_field(FormField* field)
2014-09-06 17:40:57 -04:00
{
2014-09-21 16:43:42 -04:00
if (field) {
free(field->label);
free(field->type);
free(field->var);
free(field->description);
g_slist_free_full(field->values, free);
g_slist_free_full(field->options, (GDestroyNotify)_free_option);
autocomplete_free(field->value_ac);
2014-09-06 17:40:57 -04:00
free(field);
}
}
2014-12-22 17:13:42 -05:00
void
2020-07-07 08:18:57 -04:00
form_destroy(DataForm* form)
2014-09-05 16:04:16 -04:00
{
2014-09-21 16:43:42 -04:00
if (form) {
free(form->type);
free(form->title);
free(form->instructions);
g_slist_free_full(form->fields, (GDestroyNotify)_free_field);
2014-09-11 18:44:39 -04:00
g_hash_table_destroy(form->var_to_tag);
g_hash_table_destroy(form->tag_to_var);
autocomplete_free(form->tag_ac);
2014-09-05 16:04:16 -04:00
free(form);
}
}
2014-09-21 16:43:42 -04:00
static int
2020-07-07 08:18:57 -04:00
_field_compare_by_var(FormField* a, FormField* b)
2014-09-21 16:43:42 -04:00
{
return g_strcmp0(a->var, b->var);
}
2015-10-25 20:14:23 -04:00
GSList*
2020-07-07 08:18:57 -04:00
form_get_non_form_type_fields_sorted(DataForm* form)
2014-09-21 16:43:42 -04:00
{
2020-07-07 08:18:57 -04:00
GSList* sorted = NULL;
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
2014-09-21 16:43:42 -04:00
if (g_strcmp0(field->var, "FORM_TYPE") != 0) {
sorted = g_slist_insert_sorted(sorted, field, (GCompareFunc)_field_compare_by_var);
}
curr = g_slist_next(curr);
}
return sorted;
}
2015-10-25 20:14:23 -04:00
GSList*
2020-07-07 08:18:57 -04:00
form_get_field_values_sorted(FormField* field)
2014-09-21 16:43:42 -04:00
{
2020-07-07 08:18:57 -04:00
GSList* sorted = NULL;
GSList* curr = field->values;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
char* value = curr->data;
2014-09-21 16:43:42 -04:00
if (value) {
sorted = g_slist_insert_sorted(sorted, value, (GCompareFunc)g_strcmp0);
}
curr = g_slist_next(curr);
}
return sorted;
}
2015-10-25 20:14:23 -04:00
char*
2020-07-07 08:18:57 -04:00
form_get_form_type_field(DataForm* form)
2014-09-05 16:04:16 -04:00
{
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, "FORM_TYPE") == 0) {
2014-09-06 17:40:57 -04:00
return field->values->data;
}
curr = g_slist_next(curr);
}
return NULL;
2014-09-05 16:04:16 -04:00
}
2014-12-22 17:13:42 -05:00
gboolean
2020-07-07 08:18:57 -04:00
form_tag_exists(DataForm* form, const char* const tag)
{
2020-07-07 08:18:57 -04:00
GList* tags = g_hash_table_get_keys(form->tag_to_var);
GList* curr = tags;
2014-09-21 16:43:42 -04:00
while (curr) {
if (g_strcmp0(curr->data, tag) == 0) {
2021-03-17 11:18:56 -04:00
g_list_free(tags);
return TRUE;
}
curr = g_list_next(curr);
}
2014-10-31 21:48:36 -04:00
g_list_free(tags);
return FALSE;
}
2014-12-22 17:13:42 -05:00
form_field_type_t
2020-07-07 08:18:57 -04:00
form_get_field_type(DataForm* form, const char* const tag)
2014-09-13 17:39:06 -04:00
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
2014-09-13 17:39:06 -04:00
if (g_strcmp0(field->var, var) == 0) {
return field->type_t;
}
curr = g_slist_next(curr);
}
}
return FIELD_UNKNOWN;
}
2014-12-22 17:13:42 -05:00
void
2020-07-07 08:18:57 -04:00
form_set_value(DataForm* form, const char* const tag, char* value)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
if (g_slist_length(field->values) == 0) {
field->values = g_slist_append(field->values, strdup(value));
form->modified = TRUE;
return;
} else if (g_slist_length(field->values) == 1) {
free(field->values->data);
field->values->data = strdup(value);
form->modified = TRUE;
return;
}
}
curr = g_slist_next(curr);
}
}
}
2014-12-22 17:13:42 -05:00
void
2020-07-07 08:18:57 -04:00
form_add_value(DataForm* form, const char* const tag, char* value)
2014-09-13 19:18:06 -04:00
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
2014-09-13 19:18:06 -04:00
if (g_strcmp0(field->var, var) == 0) {
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_TEXT_MULTI) {
int total = g_slist_length(field->values);
2020-07-07 08:18:57 -04:00
GString* value_index = g_string_new("");
g_string_printf(value_index, "val%d", total);
autocomplete_add(field->value_ac, value_index->str);
g_string_free(value_index, TRUE);
}
form->modified = TRUE;
return;
}
curr = g_slist_next(curr);
}
}
}
2014-12-22 17:13:42 -05:00
gboolean
2020-07-07 08:18:57 -04:00
form_add_unique_value(DataForm* form, const char* const tag, char* value)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
2020-07-07 08:18:57 -04:00
GSList* curr_value = field->values;
2014-09-21 16:43:42 -04:00
while (curr_value) {
2014-09-13 19:18:06 -04:00
if (g_strcmp0(curr_value->data, value) == 0) {
return FALSE;
2014-09-13 19:18:06 -04:00
}
curr_value = g_slist_next(curr_value);
}
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_add(field->value_ac, value);
}
form->modified = TRUE;
return TRUE;
2014-09-13 19:18:06 -04:00
}
curr = g_slist_next(curr);
}
}
return FALSE;
2014-09-13 19:18:06 -04:00
}
2014-12-22 17:13:42 -05:00
gboolean
2020-07-07 08:18:57 -04:00
form_remove_value(DataForm* form, const char* const tag, char* value)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
2020-07-07 08:18:57 -04:00
GSList* found = g_slist_find_custom(field->values, value, (GCompareFunc)g_strcmp0);
2014-09-21 16:43:42 -04:00
if (found) {
free(found->data);
found->data = NULL;
field->values = g_slist_delete_link(field->values, found);
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_remove(field->value_ac, value);
}
form->modified = TRUE;
return TRUE;
} else {
return FALSE;
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
2014-12-22 17:13:42 -05:00
gboolean
2020-07-07 08:18:57 -04:00
form_remove_text_multi_value(DataForm* form, const char* const tag, int index)
{
index--;
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
2020-07-07 08:18:57 -04:00
GSList* item = g_slist_nth(field->values, index);
2014-09-21 16:43:42 -04:00
if (item) {
free(item->data);
item->data = NULL;
field->values = g_slist_delete_link(field->values, item);
2020-07-07 08:18:57 -04:00
GString* value_index = g_string_new("");
g_string_printf(value_index, "val%d", index + 1);
autocomplete_remove(field->value_ac, value_index->str);
g_string_free(value_index, TRUE);
form->modified = TRUE;
return TRUE;
} else {
return FALSE;
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
2014-12-22 17:13:42 -05:00
int
2020-07-07 08:18:57 -04:00
form_get_value_count(DataForm* form, const char* const tag)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
if ((g_slist_length(field->values) == 1) && (field->values->data == NULL)) {
return 0;
} else {
return g_slist_length(field->values);
}
}
curr = g_slist_next(curr);
}
}
return 0;
}
2014-12-22 17:13:42 -05:00
gboolean
2020-07-07 08:18:57 -04:00
form_field_contains_option(DataForm* form, const char* const tag, char* value)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
2020-07-07 08:18:57 -04:00
GSList* curr_option = field->options;
2014-09-21 16:43:42 -04:00
while (curr_option) {
2020-07-07 08:18:57 -04:00
FormOption* option = curr_option->data;
if (g_strcmp0(option->value, value) == 0) {
return TRUE;
}
curr_option = g_slist_next(curr_option);
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
2015-10-25 20:14:23 -04:00
FormField*
2020-07-07 08:18:57 -04:00
form_get_field_by_tag(DataForm* form, const char* const tag)
2014-09-15 18:28:12 -04:00
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
2014-09-15 18:28:12 -04:00
if (g_strcmp0(field->var, var) == 0) {
return field;
}
curr = g_slist_next(curr);
}
}
return NULL;
}
2014-12-22 17:13:42 -05:00
Autocomplete
2020-07-07 08:18:57 -04:00
form_get_value_ac(DataForm* form, const char* const tag)
{
2020-07-07 08:18:57 -04:00
char* var = g_hash_table_lookup(form->tag_to_var, tag);
2014-09-21 16:43:42 -04:00
if (var) {
2020-07-07 08:18:57 -04:00
GSList* curr = form->fields;
2014-09-21 16:43:42 -04:00
while (curr) {
2020-07-07 08:18:57 -04:00
FormField* field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
return field->value_ac;
}
curr = g_slist_next(curr);
}
}
return NULL;
}
2014-12-22 17:13:42 -05:00
void
2020-07-07 08:18:57 -04:00
form_reset_autocompleters(DataForm* form)
{
autocomplete_reset(form->tag_ac);
2020-07-07 08:18:57 -04:00
GSList* curr_field = form->fields;
while (curr_field) {
2020-07-07 08:18:57 -04:00
FormField* field = curr_field->data;
autocomplete_reset(field->value_ac);
curr_field = g_slist_next(curr_field);
}
}