| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.DataForms { |
| 4 |
|
| 5 |
public const string NS_URI = "jabber:x:data"; |
| 6 |
|
| 7 |
public class DataForm { |
| 8 |
|
| 9 |
public StanzaNode stanza_node { get; set; } |
| 10 |
public Gee.List<Field> fields = new ArrayList<Field>(); |
| 11 |
public string? form_type = null; |
| 12 |
public string? instructions = null; |
| 13 |
public string? title = null; |
| 14 |
|
| 15 |
public StanzaNode get_submit_node() { |
| 16 |
stanza_node.set_attribute("type", "submit"); |
| 17 |
return stanza_node; |
| 18 |
} |
| 19 |
|
| 20 |
public enum Type { |
| 21 |
BOOLEAN, |
| 22 |
FIXED, |
| 23 |
HIDDEN, |
| 24 |
JID_MULTI, |
| 25 |
LIST_SINGLE, |
| 26 |
LIST_MULTI, |
| 27 |
TEXT_PRIVATE, |
| 28 |
TEXT_SINGLE, |
| 29 |
} |
| 30 |
|
| 31 |
public class Option { |
| 32 |
public string label { get; set; } |
| 33 |
public string value { get; set; } |
| 34 |
|
| 35 |
public Option(string label, string value) { |
| 36 |
this.label = label; |
| 37 |
this.value = value; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public class Field : Object { |
| 42 |
public StanzaNode node { get; set; } |
| 43 |
public string? label { |
| 44 |
get { return node.get_attribute("label", NS_URI); } |
| 45 |
set { node.set_attribute("label", value); } |
| 46 |
} |
| 47 |
public virtual Type? type_ { get; internal set; default=null; } |
| 48 |
public string? var { |
| 49 |
get { return node.get_attribute("var", NS_URI); } |
| 50 |
set { node.set_attribute("var", value); } |
| 51 |
} |
| 52 |
|
| 53 |
public Field() { |
| 54 |
this.node = new StanzaNode.build("field", NS_URI); |
| 55 |
} |
| 56 |
|
| 57 |
public Field.from_node(StanzaNode node) { |
| 58 |
this.node = node; |
| 59 |
} |
| 60 |
|
| 61 |
internal Gee.List<string> get_values() { |
| 62 |
Gee.List<string> ret = new ArrayList<string>(); |
| 63 |
Gee.List<StanzaNode> value_nodes = node.get_subnodes("value", NS_URI); |
| 64 |
foreach (StanzaNode node in value_nodes) { |
| 65 |
ret.add(node.get_string_content()); |
| 66 |
} |
| 67 |
return ret; |
| 68 |
} |
| 69 |
|
| 70 |
public string get_value_string() { |
| 71 |
Gee.List<string> values = get_values(); |
| 72 |
return values.size > 0 ? values[0] : ""; |
| 73 |
} |
| 74 |
|
| 75 |
public void set_value_string(string val) { |
| 76 |
StanzaNode? value_node = node.get_subnode("value", NS_URI); |
| 77 |
if (value_node == null) { |
| 78 |
value_node = new StanzaNode.build("value", NS_URI); |
| 79 |
node.put_node(value_node); |
| 80 |
} |
| 81 |
value_node.sub_nodes.clear(); |
| 82 |
value_node.put_node(new StanzaNode.text(val)); |
| 83 |
} |
| 84 |
|
| 85 |
internal void add_value_string(string val) { |
| 86 |
StanzaNode node = new StanzaNode.build("value"); |
| 87 |
node.put_node(new StanzaNode.text(val)); |
| 88 |
} |
| 89 |
|
| 90 |
internal Gee.List<Option>? get_options() { |
| 91 |
Gee.List<Option> ret = new ArrayList<Option>(); |
| 92 |
Gee.List<StanzaNode> option_nodes = node.get_subnodes("option", NS_URI); |
| 93 |
foreach (StanzaNode node in option_nodes) { |
| 94 |
Option option = new Option(node.get_attribute("label", NS_URI), node.get_subnode("value").get_string_content()); |
| 95 |
ret.add(option); |
| 96 |
} |
| 97 |
return ret; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public class BooleanField : Field { |
| 102 |
public bool value { |
| 103 |
get { return get_value_string() == "1"; } |
| 104 |
set { set_value_string(value ? "1" : "0"); } |
| 105 |
} |
| 106 |
public BooleanField(StanzaNode node) { |
| 107 |
base.from_node(node); |
| 108 |
type_ = Type.BOOLEAN; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public class FixedField : Field { |
| 113 |
public string value { |
| 114 |
owned get { return get_value_string(); } |
| 115 |
set { set_value_string(value); } |
| 116 |
} |
| 117 |
public FixedField(StanzaNode node) { |
| 118 |
base.from_node(node); |
| 119 |
type_ = Type.FIXED; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public class HiddenField : Field { |
| 124 |
public HiddenField() { |
| 125 |
base(); |
| 126 |
type_ = Type.HIDDEN; |
| 127 |
node.put_attribute("type", "hidden"); |
| 128 |
} |
| 129 |
public HiddenField.from_node(StanzaNode node) { |
| 130 |
base.from_node(node); |
| 131 |
type_ = Type.HIDDEN; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public class JidMultiField : Field { |
| 136 |
public Gee.List<Option> options { owned get { return get_options(); } } |
| 137 |
public Gee.List<string> value { get; set; } |
| 138 |
public JidMultiField(StanzaNode node) { |
| 139 |
base.from_node(node); |
| 140 |
type_ = Type.JID_MULTI; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public class ListSingleField : Field { |
| 145 |
public Gee.List<Option> options { owned get { return get_options(); } } |
| 146 |
public string value { |
| 147 |
owned get { return get_value_string(); } |
| 148 |
set { set_value_string(value); } |
| 149 |
} |
| 150 |
public ListSingleField(StanzaNode node) { |
| 151 |
base.from_node(node); |
| 152 |
type_ = Type.LIST_SINGLE; |
| 153 |
node.set_attribute("type", "list-single"); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public class ListMultiField : Field { |
| 158 |
public Gee.List<Option> options { owned get { return get_options(); } } |
| 159 |
public Gee.List<string> value { get; set; } |
| 160 |
public ListMultiField(StanzaNode node) { |
| 161 |
base.from_node(node); |
| 162 |
type_ = Type.LIST_MULTI; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public class TextPrivateField : Field { |
| 167 |
public string value { |
| 168 |
owned get { return get_value_string(); } |
| 169 |
set { set_value_string(value); } |
| 170 |
} |
| 171 |
public TextPrivateField(StanzaNode node) { |
| 172 |
base.from_node(node); |
| 173 |
type_ = Type.TEXT_PRIVATE; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public class TextSingleField : Field { |
| 178 |
public string value { |
| 179 |
owned get { return get_value_string(); } |
| 180 |
set { set_value_string(value); } |
| 181 |
} |
| 182 |
public TextSingleField(StanzaNode node) { |
| 183 |
base.from_node(node); |
| 184 |
type_ = Type.TEXT_SINGLE; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// TODO text-multi |
| 189 |
|
| 190 |
internal DataForm.from_node(StanzaNode node) { |
| 191 |
this.stanza_node = node; |
| 192 |
|
| 193 |
Gee.List<StanzaNode> field_nodes = node.get_subnodes("field", NS_URI); |
| 194 |
foreach (StanzaNode field_node in field_nodes) { |
| 195 |
string? type = field_node.get_attribute("type", NS_URI); |
| 196 |
switch (type) { |
| 197 |
case "boolean": |
| 198 |
fields.add(new BooleanField(field_node)); break; |
| 199 |
case "fixed": |
| 200 |
fields.add(new FixedField(field_node)); break; |
| 201 |
case "hidden": |
| 202 |
HiddenField field = new HiddenField.from_node(field_node); |
| 203 |
if (field.var == "FORM_TYPE") { |
| 204 |
this.form_type = field.get_value_string(); |
| 205 |
break; |
| 206 |
} |
| 207 |
fields.add(field); break; |
| 208 |
case "jid-multi": |
| 209 |
fields.add(new JidMultiField(field_node)); break; |
| 210 |
case "list-single": |
| 211 |
fields.add(new ListSingleField(field_node)); break; |
| 212 |
case "list-multi": |
| 213 |
fields.add(new ListMultiField(field_node)); break; |
| 214 |
case "text-private": |
| 215 |
fields.add(new TextPrivateField(field_node)); break; |
| 216 |
case "text-single": |
| 217 |
fields.add(new TextSingleField(field_node)); break; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
StanzaNode? instructions_node = node.get_subnode("instructions", NS_URI); |
| 222 |
if (instructions_node != null) { |
| 223 |
instructions = instructions_node.get_string_content(); |
| 224 |
} |
| 225 |
|
| 226 |
StanzaNode? title_node = node.get_subnode("title", NS_URI); |
| 227 |
if (title_node != null) { |
| 228 |
title = title_node.get_string_content(); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
internal DataForm() { |
| 233 |
this.stanza_node = new StanzaNode.build("x", NS_URI).add_self_xmlns(); |
| 234 |
} |
| 235 |
|
| 236 |
public static DataForm? create_from_node(StanzaNode node) { |
| 237 |
return new DataForm.from_node(node); |
| 238 |
} |
| 239 |
|
| 240 |
public void add_field(Field field) { |
| 241 |
fields.add(field); |
| 242 |
stanza_node.put_node(field.node); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
} |