summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Thompson <will.thompson@collabora.co.uk>2010-03-25 15:01:51 (GMT)
committerWill Thompson <will.thompson@collabora.co.uk>2010-03-25 15:20:06 (GMT)
commit921370d18d5633282c6d2e872879de691c215f58 (patch)
treea5e50e3145d2a7326ed6c08b70eee7d7df65cf82
parent74072d08ab03f6f70513542728d5b0fdbfe33229 (diff)
downloadwocky-921370d18d5633282c6d2e872879de691c215f58.tar.gz
wocky-921370d18d5633282c6d2e872879de691c215f58.tar.xz
Add API for setting FORM_TYPE when blindly submitting
-rw-r--r--tests/wocky-data-form-test.c25
-rw-r--r--wocky/wocky-data-form.c37
-rw-r--r--wocky/wocky-data-form.h3
3 files changed, 61 insertions, 4 deletions
diff --git a/tests/wocky-data-form-test.c b/tests/wocky-data-form-test.c
index 1ced924..c40025b 100644
--- a/tests/wocky-data-form-test.c
+++ b/tests/wocky-data-form-test.c
@@ -537,8 +537,9 @@ test_submit_blindly (void)
gboolean succeeded;
WockyXmppStanza *stanza, *expected;
- /* The form's empty, so the setters should all fail if we don't tell them to
- * create the fields if missing.
+ /* We didn't actually parse a form, so it doesn't have any pre-defined
+ * fields. Thus, the setters should all fail if we don't tell them to create
+ * the fields if missing.
*/
succeeded = wocky_data_form_set_string (form, "band-name", "The XX", FALSE);
g_assert (!succeeded);
@@ -552,8 +553,17 @@ test_submit_blindly (void)
g_assert (form->fields_list == NULL);
g_assert_cmpuint (0, ==, g_hash_table_size (form->fields));
- /* But if we forcibly create those fields, they should show up when we submit
- * the form!
+ /* Since the form doesn't have a FORM_TYPE yet, we should be able to set it.
+ */
+ succeeded = wocky_data_form_set_type (form, "http://example.com/band-info");
+ g_assert (succeeded);
+
+ /* But now that it does have one, we shouldn't be able to change it. */
+ succeeded = wocky_data_form_set_type (form, "stoats");
+ g_assert (!succeeded);
+
+ /* If we forcibly create the fields we care about, setting them should
+ * succeed, and they should show up when we submit the form!
*/
succeeded = wocky_data_form_set_string (form, "band-name", "The XX", TRUE);
g_assert (succeeded);
@@ -575,6 +585,13 @@ test_submit_blindly (void)
WOCKY_NODE_XMLNS, WOCKY_XMPP_NS_DATA,
WOCKY_NODE_ATTRIBUTE, "type", "submit",
WOCKY_NODE, "field",
+ WOCKY_NODE_ATTRIBUTE, "type", "hidden",
+ WOCKY_NODE_ATTRIBUTE, "var", "FORM_TYPE",
+ WOCKY_NODE, "value",
+ WOCKY_NODE_TEXT, "http://example.com/band-info",
+ WOCKY_NODE_END,
+ WOCKY_NODE_END,
+ WOCKY_NODE, "field",
WOCKY_NODE_ATTRIBUTE, "var", "band-name",
WOCKY_NODE, "value",
WOCKY_NODE_TEXT, "The XX",
diff --git a/wocky/wocky-data-form.c b/wocky/wocky-data-form.c
index f8d5dee..b31990b 100644
--- a/wocky/wocky-data-form.c
+++ b/wocky/wocky-data-form.c
@@ -566,6 +566,43 @@ wocky_data_form_new_from_form (WockyXmppNode *root,
return form;
}
+/**
+ * wocky_data_form_set_type:
+ * @form: a #WockyDataForm
+ * @form_type: the URI to use as the FORM_TYPE field; may not be %NULL
+ *
+ * Creates a hidden FORM_TYPE field in @form and sets its value to @form_type.
+ * This is intended only to be used on empty forms created for blind
+ * submission.
+ *
+ * Returns: %TRUE if the form's type was set; %FALSE if the form already had a
+ * type.
+ */
+gboolean
+wocky_data_form_set_type (WockyDataForm *form,
+ const gchar *form_type)
+{
+ WockyDataFormField *field;
+
+ g_return_val_if_fail (form_type != NULL, FALSE);
+
+ field = g_hash_table_lookup (form->fields, "FORM_TYPE");
+
+ if (field != NULL)
+ {
+ DEBUG ("form already has a FORM_TYPE");
+ return FALSE;
+ }
+
+ field = wocky_data_form_field_new (WOCKY_DATA_FORM_FIELD_TYPE_HIDDEN,
+ "FORM_TYPE", NULL, NULL, FALSE, NULL,
+ wocky_g_value_slice_new_string (form_type),
+ NULL);
+ data_form_add_field (form, field, FALSE);
+
+ return TRUE;
+}
+
/*
* data_form_set_value:
* @form: a data form
diff --git a/wocky/wocky-data-form.h b/wocky/wocky-data-form.h
index 9eb65bf..add36d3 100644
--- a/wocky/wocky-data-form.h
+++ b/wocky/wocky-data-form.h
@@ -115,6 +115,9 @@ GType wocky_data_form_get_type (void);
WockyDataForm * wocky_data_form_new_from_form (WockyXmppNode *node,
GError **error);
+gboolean wocky_data_form_set_type (WockyDataForm *form,
+ const gchar *form_type);
+
gboolean wocky_data_form_set_boolean (WockyDataForm *form,
const gchar *field_name,
gboolean field_value,