summaryrefslogtreecommitdiff
path: root/wocky/wocky-data-form.c
blob: c74c1aeb19e006d4e9fb5819e177da86b4c1bc11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/*
 * wocky-data-form.c - WockyDataForm
 * Copyright © 2009–2010 Collabora Ltd.
 * Copyright © 2010 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * SECTION: wocky-data-form
 * @title: WockyDataForm
 * @short_description: An object to represent an XMPP data form
 * @include: wocky/wocky-data-form.h
 *
 * An object that represents an XMPP data form as described in
 * XEP-0004.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "wocky-data-form.h"

#include <string.h>

#include "wocky-namespaces.h"
#include "wocky-utils.h"

#define WOCKY_DEBUG_FLAG WOCKY_DEBUG_DATA_FORM
#include "wocky-debug-internal.h"

G_DEFINE_TYPE (WockyDataForm, wocky_data_form, G_TYPE_OBJECT)

/* properties */
enum
{
  PROP_TITLE = 1,
  PROP_INSTRUCTIONS,
};

/* private structure */
struct _WockyDataFormPrivate
{
  gchar *title;
  gchar *instructions;

  /* (gchar *) => owned (WockyDataFormField *) */
  GHashTable *reported;

  gboolean dispose_has_run;
};

GQuark
wocky_data_form_error_quark (void)
{
  static GQuark quark = 0;

  if (quark == 0)
    quark = g_quark_from_static_string ("wocky-data-form-error");

  return quark;
}

static WockyDataFormFieldOption *
wocky_data_form_field_option_new (const gchar *label,
    const gchar *value)
{
  WockyDataFormFieldOption *option;

  g_assert (value != NULL);

  option = g_slice_new0 (WockyDataFormFieldOption);
  option->label = g_strdup (label);
  option->value = g_strdup (value);
  return option;
}

static void
wocky_data_form_field_option_free (WockyDataFormFieldOption *option)
{
  g_free (option->label);
  g_free (option->value);
  g_slice_free (WockyDataFormFieldOption, option);
}

/* pass ownership of the default_value, raw_value_contents, the value
 * and the options list */
static WockyDataFormField *
wocky_data_form_field_new (
  WockyDataFormFieldType type,
  const gchar *var,
  const gchar *label,
  const gchar *desc,
  gboolean required,
  GValue *default_value,
  gchar **raw_value_contents,
  GValue *value,
  GSList *options)
{
  WockyDataFormField *field;

  field = g_slice_new0 (WockyDataFormField);
  field->type = type;
  field->var = g_strdup (var);
  field->label = g_strdup (label);
  field->desc = g_strdup (desc);
  field->required = required;
  field->default_value = default_value;
  field->raw_value_contents = raw_value_contents;
  field->value = value;
  field->options = options;
  return field;
}

static void
wocky_data_form_field_free (WockyDataFormField *field)
{
  if (field == NULL)
    return;

  g_free (field->var);
  g_free (field->label);
  g_free (field->desc);
  g_strfreev (field->raw_value_contents);

  if (field->default_value != NULL)
    wocky_g_value_slice_free (field->default_value);
  if (field->value != NULL)
    wocky_g_value_slice_free (field->value);

  g_slist_foreach (field->options, (GFunc) wocky_data_form_field_option_free,
      NULL);
  g_slist_free (field->options);
  g_slice_free (WockyDataFormField, field);
}

static void
wocky_data_form_init (WockyDataForm *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, WOCKY_TYPE_DATA_FORM,
      WockyDataFormPrivate);

  self->fields = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
  self->fields_list = NULL;

  self->priv->reported = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
      (GDestroyNotify) wocky_data_form_field_free);
  self->results = NULL;
}

static void
wocky_data_form_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  WockyDataForm *self = WOCKY_DATA_FORM (object);
  WockyDataFormPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_TITLE:
        priv->title = g_value_dup_string (value);
        break;
      case PROP_INSTRUCTIONS:
        priv->instructions = g_value_dup_string (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
wocky_data_form_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  WockyDataForm *self = WOCKY_DATA_FORM (object);
  WockyDataFormPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_TITLE:
        g_value_set_string (value, priv->title);
        break;
      case PROP_INSTRUCTIONS:
        g_value_set_string (value, priv->instructions);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}


static void
wocky_data_form_dispose (GObject *object)
{
  WockyDataForm *self = WOCKY_DATA_FORM (object);
  WockyDataFormPrivate *priv = self->priv;

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  if (G_OBJECT_CLASS (wocky_data_form_parent_class)->dispose)
    G_OBJECT_CLASS (wocky_data_form_parent_class)->dispose (object);
}

static void
data_form_field_list_free (GSList *fields)
{
  g_slist_foreach (fields, (GFunc) wocky_data_form_field_free, NULL);
  g_slist_free (fields);
}

static void
wocky_data_form_finalize (GObject *object)
{
  WockyDataForm *self = WOCKY_DATA_FORM (object);
  WockyDataFormPrivate *priv = self->priv;

  g_free (priv->title);
  g_free (priv->instructions);
  g_hash_table_unref (self->fields);

  data_form_field_list_free (self->fields_list);

  g_slist_foreach (self->results, (GFunc) data_form_field_list_free, NULL);
  g_slist_free (self->results);

  g_hash_table_unref (priv->reported);

  G_OBJECT_CLASS (wocky_data_form_parent_class)->finalize (object);
}

static void
wocky_data_form_class_init (
    WockyDataFormClass *wocky_data_form_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (wocky_data_form_class);
  GParamSpec *param_spec;

  g_type_class_add_private (wocky_data_form_class,
      sizeof (WockyDataFormPrivate));

  object_class->set_property = wocky_data_form_set_property;
  object_class->get_property = wocky_data_form_get_property;
  object_class->dispose = wocky_data_form_dispose;
  object_class->finalize = wocky_data_form_finalize;

  param_spec = g_param_spec_string ("title", "title",
      "Title",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_TITLE, param_spec);

  param_spec = g_param_spec_string ("instructions", "instructions",
      "Instructions",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_INSTRUCTIONS, param_spec);
}

static const gchar *
type_to_str (WockyDataFormFieldType type)
{
  return wocky_enum_to_nick (WOCKY_TYPE_DATA_FORM_FIELD_TYPE, type);
}

/*
 * extract_options_list:
 * @node: a <field/> node
 *
 * Returns: a list of (WockyDataFormFieldOption *) containing all the
 *          <option/>s defined in the node
 */
static GSList *
extract_options_list (WockyNode *node)
{
  GSList *result = NULL;
  WockyNodeIter iter;
  WockyNode *option_node;

  wocky_node_iter_init (&iter, node, "option", NULL);

  while (wocky_node_iter_next (&iter, &option_node))
    {
      WockyDataFormFieldOption *option;
      const gchar *value, *label;

      value = wocky_node_get_content_from_child (option_node, "value");
      label = wocky_node_get_attribute (option_node, "label");

      if (value == NULL)
        continue;

      /* the label is optional */

      DEBUG ("Add option: %s", value);
      option = wocky_data_form_field_option_new (label, value);
      result = g_slist_append (result, option);
    }

  return result;
}

/*
 * extract_value_list:
 * @node: a <field/> element
 *
 * Returns: a newly allocated array of strings containing the content of all
 *          the 'value' children nodes of the node
 */
static GStrv
extract_value_list (WockyNode *node)
{
  GPtrArray *tmp = g_ptr_array_new ();
  WockyNodeIter iter;
  WockyNode *value;

  wocky_node_iter_init (&iter, node, "value", NULL);

  while (wocky_node_iter_next (&iter, &value))
    {
      if (value->content != NULL)
        g_ptr_array_add (tmp, g_strdup (value->content));
    }

  /* Add trailing NULL */
  g_ptr_array_add (tmp, NULL);

  return (GStrv) g_ptr_array_free (tmp, FALSE);
}

static GValue *
get_field_value (
    WockyDataFormFieldType type,
    WockyNode *field,
    gchar ***raw_value_contents)
{
  WockyNode *node;
  const gchar *value;
  GValue *ret = NULL;

  if (type == WOCKY_DATA_FORM_FIELD_TYPE_UNSPECIFIED)
    {
      /* While parsing a form, we shouldn't get this far without having treated
       * the absence of type='' to mean text-single.
       */
      g_warn_if_reached ();
      return NULL;
    }

  node = wocky_node_get_child (field, "value");
  if (node == NULL)
    /* no default value */
    return NULL;

  value = node->content;

  switch (type)
    {
      case WOCKY_DATA_FORM_FIELD_TYPE_BOOLEAN:
        {
          if (!wocky_strdiff (value, "true") || !wocky_strdiff (value, "1"))
            ret = wocky_g_value_slice_new_boolean (TRUE);
          else if (!wocky_strdiff (value, "false") || !wocky_strdiff (value, "0"))
            ret = wocky_g_value_slice_new_boolean (FALSE);
          else
            DEBUG ("Invalid boolean value: %s", value);

          if (ret != NULL)
            {
              const gchar const *value_str[] = { value, NULL };

              if (raw_value_contents != NULL)
                *raw_value_contents = g_strdupv ((GStrv) value_str);

              return ret;
            }
        }
        break;

      case WOCKY_DATA_FORM_FIELD_TYPE_FIXED:
      case WOCKY_DATA_FORM_FIELD_TYPE_HIDDEN:
      case WOCKY_DATA_FORM_FIELD_TYPE_JID_SINGLE:
      case WOCKY_DATA_FORM_FIELD_TYPE_TEXT_PRIVATE:
      case WOCKY_DATA_FORM_FIELD_TYPE_TEXT_SINGLE:
      case WOCKY_DATA_FORM_FIELD_TYPE_LIST_SINGLE:
        {
          const gchar const *value_str[] = { value, NULL };

          if (raw_value_contents != NULL)
            *raw_value_contents = g_strdupv ((GStrv) value_str);

          return wocky_g_value_slice_new_string (value);
        }

      case WOCKY_DATA_FORM_FIELD_TYPE_JID_MULTI:
      case WOCKY_DATA_FORM_FIELD_TYPE_TEXT_MULTI:
      case WOCKY_DATA_FORM_FIELD_TYPE_LIST_MULTI:
        {
          gchar **value_str = extract_value_list (field);

          if (raw_value_contents != NULL)
            *raw_value_contents = g_strdupv (value_str);

          return wocky_g_value_slice_new_take_boxed (G_TYPE_STRV,
              value_str);
        }

      default:
        g_assert_not_reached ();
    }

  return NULL;
}

static WockyDataFormField *
create_field (WockyNode *field_node,
    const gchar *var,
    WockyDataFormFieldType type,
    const gchar *label,
    const gchar *desc,
    gboolean required)
{
  GValue *default_value = NULL;
  GSList *options = NULL;
  WockyDataFormField *field;
  gchar **raw_value_contents = NULL;

  if (type == WOCKY_DATA_FORM_FIELD_TYPE_LIST_MULTI ||
      type == WOCKY_DATA_FORM_FIELD_TYPE_LIST_SINGLE)
    {
      /* get the list of options (accepted values) */
      options = extract_options_list (field_node);
      if (options == NULL)
        {
          DEBUG ("No options provided for '%s'", var);
          return NULL;
        }
    }

  /* get default value (if any) */
  default_value = get_field_value (type, field_node, &raw_value_contents);

  field = wocky_data_form_field_new (type, var, label, desc, required,
      default_value, raw_value_contents, NULL, options);

  return field;
}

static gboolean
extract_var_type_label (WockyNode *node,
    const gchar **_var,
    WockyDataFormFieldType *_type,
    const gchar **_label)
{
  const gchar *tmp, *var, *label;
  gint type = 0;

  if (wocky_strdiff (node->name, "field"))
    return FALSE;

  /* For data forms of type "form", each <field/> element SHOULD possess a
   * 'type' attribute that defines the data "type" of the field data (if no
   * 'type' is specified, the default is "text-single")
   */
  tmp = wocky_node_get_attribute (node, "type");
  if (tmp == NULL)
    {
      WockyNodeIter iter;

      type = WOCKY_DATA_FORM_FIELD_TYPE_TEXT_SINGLE;

      wocky_node_iter_init (&iter, node, "value", NULL);

      if (wocky_node_iter_next (&iter, NULL) &&
          wocky_node_iter_next (&iter, NULL))
        {
          type = WOCKY_DATA_FORM_FIELD_TYPE_TEXT_MULTI;
        }
    }
  else if (!wocky_enum_from_nick (WOCKY_TYPE_DATA_FORM_FIELD_TYPE,
                tmp, &type))
    {
      DEBUG ("Invalid field type: %s", tmp);
      return FALSE;
    }

  var = wocky_node_get_attribute (node, "var");
  if (var == NULL && type != WOCKY_DATA_FORM_FIELD_TYPE_FIXED)
    {
      DEBUG ("field node doesn't have a 'var' attribute; ignoring");
      return FALSE;
    }

  label = wocky_node_get_attribute (node, "label");

  if (_var != NULL)
    *_var = var;
  if (_type != NULL)
    *_type = type;
  if (_label != NULL)
    *_label = label;

  return TRUE;
}

static WockyDataFormField *
data_form_parse_form_field (WockyNode *field_node)
{
  WockyDataFormField *field;
  const gchar *var, *label, *desc;
  WockyDataFormFieldType type;
  gboolean required;

  if (!extract_var_type_label (field_node, &var, &type, &label))
    return NULL;

  desc = wocky_node_get_content_from_child (field_node, "desc");
  required = (wocky_node_get_child (field_node, "required") != NULL);
  field = create_field (field_node, var, type, label, desc, required);

  if (field == NULL)
    return NULL;

  if (field->var != NULL)
    DEBUG ("parsed field '%s' of type %s", field->var, type_to_str (type));
  else
    DEBUG ("parsed anonymous field of type %s", type_to_str (type));

  return field;
}

static void
data_form_add_field (WockyDataForm *self,
    WockyDataFormField *field,
    gboolean prepend)
{

  self->fields_list =
      (prepend ? g_slist_prepend : g_slist_append) (self->fields_list, field);

  /* Fixed fields can be used for instructions to the user. They have
   * no var='' attribute and hence shouldn't be included in the form
   * submission.*/
  if (field->var != NULL)
    g_hash_table_insert (self->fields, field->var, field);
}

WockyDataForm *
wocky_data_form_new_from_node (WockyNode *x,
    GError **error)
{
  WockyNode *node;
  WockyNodeIter iter;
  const gchar *type, *title, *instructions;
  WockyDataForm *form;

  if (!wocky_node_matches (x, "x", WOCKY_XMPP_NS_DATA))
    {
      DEBUG ("Invalid 'x' node");
      g_set_error (error, WOCKY_DATA_FORM_ERROR,
          WOCKY_DATA_FORM_ERROR_NOT_FORM, "Invalid 'x' node");
      return NULL;
    }

  type = wocky_node_get_attribute (x, "type");
  if (wocky_strdiff (type, "form") && wocky_strdiff (type, "result"))
    {
      DEBUG ("'type' attribute is not 'form' or 'result': %s", type);
      g_set_error (error, WOCKY_DATA_FORM_ERROR,
          WOCKY_DATA_FORM_ERROR_WRONG_TYPE,
          "'type' attribute is not 'form' or 'result': %s", type);
      return NULL;
    }

  title = wocky_node_get_content_from_child (x, "title");
  instructions = wocky_node_get_content_from_child (x, "instructions");

  form = g_object_new (WOCKY_TYPE_DATA_FORM,
      "title", title,
      "instructions", instructions,
      NULL);

  /* add fields */
  wocky_node_iter_init (&iter, x, "field", NULL);

  while (wocky_node_iter_next (&iter, &node))
    {
      WockyDataFormField *field = data_form_parse_form_field (node);

      if (field != NULL)
        data_form_add_field (form, field, TRUE);
    }

  form->fields_list = g_slist_reverse (form->fields_list);

  return form;
}

WockyDataForm *
wocky_data_form_new_from_form (WockyNode *root,
    GError **error)
{
  WockyNode *x;

  x = wocky_node_get_child_ns (root, "x", WOCKY_XMPP_NS_DATA);
  if (x == NULL)
    {
      DEBUG ("No 'x' node");
      g_set_error (error, WOCKY_DATA_FORM_ERROR,
          WOCKY_DATA_FORM_ERROR_NOT_FORM, "No 'x' node");
      return NULL;
    }

  return wocky_data_form_new_from_node (x, error);
}

/**
 * wocky_data_form_set_type:
 * @self: a #WockyDataForm
 * @form_type: the URI to use as the FORM_TYPE field; may not be %NULL
 *
 * Creates a hidden FORM_TYPE field in @self 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 *self,
    const gchar *form_type)
{
  WockyDataFormField *field;
  const gchar const *raw_value_contents[] =
      { form_type, NULL };

  g_return_val_if_fail (form_type != NULL, FALSE);

  field = g_hash_table_lookup (self->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,
      wocky_g_value_slice_new_string (form_type),
      g_strdupv ((GStrv) raw_value_contents),
      wocky_g_value_slice_new_string (form_type),
      NULL);
  data_form_add_field (self, field, FALSE);

  return TRUE;
}

/*
 * data_form_set_value:
 * @self: a data form
 * @field_name: the name of a field of @self
 * @value: a slice-allocated value to fill in for @field_name (stolen by this
 *         function)
 * @create_if_missing: if no field named @field_name exists, create it
 *
 * Returns: %TRUE if the field was successfully filled in; %FALSE if the field
 *          did not exist or has a different type to @value.
 */
static gboolean
data_form_set_value (WockyDataForm *self,
    const gchar *field_name,
    GValue *value,
    gboolean create_if_missing)
{
  WockyDataFormField *field;
  GType t;

  g_return_val_if_fail (field_name != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  field = g_hash_table_lookup (self->fields, field_name);

  if (field == NULL)
    {
      if (create_if_missing)
        {
          field = wocky_data_form_field_new (
              WOCKY_DATA_FORM_FIELD_TYPE_UNSPECIFIED, field_name, NULL, NULL,
              FALSE, NULL, NULL, NULL, NULL);
          data_form_add_field (self, field, FALSE);
        }
      else
        {
          DEBUG ("field %s not found", field_name);
          wocky_g_value_slice_free (value);
          return FALSE;
        }
    }

  if (field->value != NULL)
    wocky_g_value_slice_free (field->value);

  field->value = value;

  g_strfreev (field->raw_value_contents);

  t = G_VALUE_TYPE (field->value);
  if (t == G_TYPE_STRING)
    {
      const gchar const *value_str[] =
          { g_value_get_string (field->value), NULL };

      field->raw_value_contents = g_strdupv ((GStrv) value_str);
    }
  else if (t == G_TYPE_BOOLEAN)
    {
      const gchar const *value_str[] =
          { g_value_get_boolean (field->value) ? "1" : "0", NULL };

      field->raw_value_contents = g_strdupv ((GStrv) value_str);
    }
  else if (t == G_TYPE_STRV)
    {
      const GStrv value_str = g_value_get_boxed (field->value);

      field->raw_value_contents = g_strdupv (value_str);
    }
  else
    {
      g_assert_not_reached ();
    }

  return TRUE;
}

/**
 * wocky_data_form_set_boolean:
 * @self: a data form
 * @field_name: the name of a boolean field of @self
 * @field_value: the value to fill in for @field_name
 * @create_if_missing: if no field named @field_name exists, create it
 *
 * Returns: %TRUE if the field was successfully filled in; %FALSE if the field
 *          did not exist or does not accept a boolean
 */
gboolean
wocky_data_form_set_boolean (WockyDataForm *self,
    const gchar *field_name,
    gboolean field_value,
    gboolean create_if_missing)
{
  return data_form_set_value (self, field_name,
      wocky_g_value_slice_new_boolean (field_value), create_if_missing);
}

/**
 * wocky_data_form_set_string:
 * @self: a data form
 * @field_name: the name of a string field of @self
 * @field_value: the value to fill in for @field_name
 * @create_if_missing: if no field named @field_name exists, create it
 *
 * Returns: %TRUE if the field was successfully filled in; %FALSE if the field
 *          did not exist or does not accept a string
 */
gboolean
wocky_data_form_set_string (WockyDataForm *self,
    const gchar *field_name,
    const gchar *field_value,
    gboolean create_if_missing)
{
  return data_form_set_value (self, field_name,
      wocky_g_value_slice_new_string (field_value), create_if_missing);
}

gboolean
wocky_data_form_set_strv (WockyDataForm *self,
    const gchar *field_name,
    const gchar * const *field_values,
    gboolean create_if_missing)
{
  return data_form_set_value (self, field_name,
      wocky_g_value_slice_new_boxed (G_TYPE_STRV, field_values),
      create_if_missing);
}

static void
add_field_to_node (WockyDataFormField *field,
    WockyNode *node)
{
  const GValue *value = field->value;
  GType t;
  WockyNode *field_node;

  /* Skip anonymous fields, which are used for instructions to the user. */
  if (field->var == NULL)
    return;

  /* Hidden fields shouldn't have their values modified, but should be returned
   * along with the form.
   */
  if (value == NULL && field->type == WOCKY_DATA_FORM_FIELD_TYPE_HIDDEN)
    value = field->default_value;

  /* Skip fields which don't have a value. */
  if (value == NULL)
    return;

  field_node = wocky_node_add_child (node, "field");
  wocky_node_set_attribute (field_node, "var", field->var);

  if (field->type != WOCKY_DATA_FORM_FIELD_TYPE_UNSPECIFIED)
    wocky_node_set_attribute (field_node, "type",
        type_to_str (field->type));

  t = G_VALUE_TYPE (value);

  if (t == G_TYPE_BOOLEAN)
    {
      wocky_node_add_child_with_content (field_node, "value",
          g_value_get_boolean (value) ? "1" : "0");
    }
  else if (t == G_TYPE_STRING)
    {
      wocky_node_add_child_with_content (field_node, "value",
          g_value_get_string (value));
    }
  else if (t == G_TYPE_STRV)
    {
      GStrv tmp = g_value_get_boxed (value);
      GStrv s;

      for (s = tmp; *s != NULL; s++)
        wocky_node_add_child_with_content (field_node, "value", *s);
    }
  else
    {
      g_assert_not_reached ();
    }
}

/**
 * wocky_data_form_submit:
 * @self: a data form
 * @node: a node to which to add a form submission
 *
 * Adds a node tree which submits @self based on the current values set on
 * @self's fields.
 */
void
wocky_data_form_submit (WockyDataForm *self,
    WockyNode *node)
{
  WockyNode *x;

  x = wocky_node_add_child_ns (node, "x", WOCKY_XMPP_NS_DATA);
  wocky_node_set_attribute (x, "type", "submit");

  g_slist_foreach (self->fields_list, (GFunc) add_field_to_node, x);
}

static void
data_form_parse_reported (WockyDataForm *self,
    WockyNode *reported_node)
{
  WockyDataFormPrivate *priv = self->priv;
  GSList *l;

  for (l = reported_node->children; l != NULL; l = g_slist_next (l))
    {
      WockyNode *node = l->data;
      const gchar *var, *label;
      WockyDataFormField *field;
      WockyDataFormFieldType type;

      if (!extract_var_type_label (node, &var, &type, &label))
        continue;

      field = wocky_data_form_field_new (type, var, label, NULL, FALSE, NULL,
          NULL, NULL, NULL);

      DEBUG ("Add '%s'", field->var);
      g_hash_table_insert (priv->reported, field->var, field);
    }
}

static void
data_form_parse_item (WockyDataForm *self,
    WockyNode *item_node)
{
  WockyDataFormPrivate *priv = self->priv;
  WockyNodeIter iter;
  WockyNode *field_node;
  GSList *item = NULL;

  wocky_node_iter_init (&iter, item_node, "field", NULL);
  while (wocky_node_iter_next (&iter, &field_node))
    {
      const gchar *var;
      WockyDataFormField *field, *result;
      GValue *value;

      var = wocky_node_get_attribute (field_node, "var");
      if (var == NULL)
        continue;

      field = g_hash_table_lookup (priv->reported, var);
      if (field == NULL)
        {
          DEBUG ("Field '%s' wasn't in the reported fields; ignoring", var);
          continue;
        }

      value = get_field_value (field->type, field_node, NULL);
      if (value == NULL)
        continue;

      result = wocky_data_form_field_new (field->type, var, field->label,
          field->desc, field->required, field->default_value,
          field->raw_value_contents, value, NULL);

      item = g_slist_prepend (item, result);
    }

  item = g_slist_reverse (item);
  self->results = g_slist_prepend (self->results, item);
}

static void
parse_unique_result (WockyDataForm *self,
    WockyNode *x)
{
  GSList *l, *item = NULL;

  for (l = x->children; l != NULL; l = g_slist_next (l))
    {
      WockyNode *node = l->data;
      const gchar *var;
      WockyDataFormFieldType type;
      WockyDataFormField *result;
      GValue *value;

      if (!extract_var_type_label (node, &var, &type, NULL))
        continue;

      value = get_field_value (type, node, NULL);
      if (value == NULL)
        continue;

      result = wocky_data_form_field_new (type, var, NULL,
          NULL, FALSE, NULL, NULL, value, NULL);

      item = g_slist_prepend (item, result);
    }

  self->results = g_slist_prepend (self->results, item);
}

gboolean
wocky_data_form_parse_result (WockyDataForm *self,
    WockyNode *node,
    GError **error)
{
  WockyNode *x, *reported;
  const gchar *type;

  x = wocky_node_get_child_ns (node, "x", WOCKY_XMPP_NS_DATA);
  if (x == NULL)
    {
      DEBUG ("No 'x' node");
      g_set_error (error, WOCKY_DATA_FORM_ERROR,
          WOCKY_DATA_FORM_ERROR_NOT_FORM, "No 'x' node");
      return FALSE;
    }

  type = wocky_node_get_attribute (x, "type");
  if (wocky_strdiff (type, "result"))
    {
      DEBUG ("'type' attribute is not 'result': %s", type);
      g_set_error (error, WOCKY_DATA_FORM_ERROR,
          WOCKY_DATA_FORM_ERROR_WRONG_TYPE,
          "'type' attribute is not 'result': %s", type);
      return FALSE;
    }

  reported = wocky_node_get_child (x, "reported");

  if (reported != NULL)
    {
      WockyNodeIter iter;
      WockyNode *item;

      /* The field definitions are in a <reported/> header, and a series of
       * <item/> nodes contain sets of results.
       */
      data_form_parse_reported (self, reported);

      wocky_node_iter_init (&iter, x, "item", NULL);
      while (wocky_node_iter_next (&iter, &item))
        data_form_parse_item (self, item);
    }
  else
    {
      /* no <reported/> header; so there must be only one result. */
      parse_unique_result (self, x);
    }

  self->results = g_slist_reverse (self->results);
  return TRUE;
}

const gchar *
wocky_data_form_get_title (WockyDataForm *self)
{
  WockyDataFormPrivate *priv = self->priv;

  return priv->title;
}

const gchar *
wocky_data_form_get_instructions (WockyDataForm *self)
{
  WockyDataFormPrivate *priv = self->priv;

  return priv->instructions;
}

gint
wocky_data_form_field_cmp (const WockyDataFormField *left,
    const WockyDataFormField *right)
{
  return g_strcmp0 (left->var, right->var);
}

static void
add_field_to_node_using_default (WockyDataFormField *field,
    WockyNode *node)
{
  WockyNode *field_node;
  GStrv s;

  /* Skip anonymous fields, which are used for instructions to the user. */
  if (field->var == NULL)
    return;

  field_node = wocky_node_add_child (node, "field");
  wocky_node_set_attribute (field_node, "var", field->var);

  if (field->type != WOCKY_DATA_FORM_FIELD_TYPE_UNSPECIFIED)
    wocky_node_set_attribute (field_node, "type",
        type_to_str (field->type));

  g_assert (field->raw_value_contents != NULL);
  for (s = field->raw_value_contents; *s != NULL; s++)
    wocky_node_add_child_with_content (field_node, "value", *s);
}

/**
 * wocky_data_form_add_to_node:
 * @self: the #WockyDataForm object
 * @node: a node to which to add the form
 *
 * Adds a node tree with default values of @self based on the defaults
 * set on each field when first created.
 *
 * This function is for adding a data form to an existing node, like
 * the query node of a disco response. wocky_data_form_submit(), in
 * contrast, is for adding a node tree which submits the data form
 * based on the current values set on its fields.
 */
void
wocky_data_form_add_to_node (WockyDataForm *self,
    WockyNode *node)
{
  WockyNode *x;

  x = wocky_node_add_child_ns (node, "x", WOCKY_XMPP_NS_DATA);
  wocky_node_set_attribute (x, "type", "result");

  g_slist_foreach (self->fields_list,
      (GFunc) add_field_to_node_using_default, x);
}