Wed, 31 Dec 2003 08:22:55 +0000
[gaim-migrate @ 8642]
oops
| 7923 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | #include "request.h" | |
| 23 | #include "server.h" | |
| 24 | ||
| 25 | #include "xdata.h" | |
| 26 | ||
| 27 | typedef enum { | |
| 28 | JABBER_X_DATA_IGNORE = 0, | |
| 29 | JABBER_X_DATA_TEXT_SINGLE, | |
| 30 | JABBER_X_DATA_TEXT_MULTI, | |
| 31 | JABBER_X_DATA_LIST_SINGLE, | |
| 32 | JABBER_X_DATA_LIST_MULTI, | |
| 33 | JABBER_X_DATA_BOOLEAN | |
| 34 | } jabber_x_data_field_type; | |
| 35 | ||
| 36 | struct jabber_x_data_data { | |
| 37 | GHashTable *fields; | |
| 38 | GSList *values; | |
| 39 | jabber_x_data_cb cb; | |
| 40 | gpointer user_data; | |
| 41 | JabberStream *js; | |
| 42 | }; | |
| 43 | ||
| 44 | static void jabber_x_data_ok_cb(struct jabber_x_data_data *data, GaimRequestFields *fields) { | |
| 45 | xmlnode *result = xmlnode_new("x"); | |
| 46 | jabber_x_data_cb cb = data->cb; | |
| 47 | gpointer user_data = data->user_data; | |
| 48 | JabberStream *js = data->js; | |
| 49 | GList *groups, *flds; | |
| 50 | ||
| 51 | xmlnode_set_attrib(result, "xmlns", "jabber:x:data"); | |
| 52 | xmlnode_set_attrib(result, "type", "submit"); | |
| 53 | ||
| 54 | for(groups = gaim_request_fields_get_groups(fields); groups; groups = groups->next) { | |
| 55 | for(flds = gaim_request_field_group_get_fields(groups->data); flds; flds = flds->next) { | |
| 56 | xmlnode *fieldnode, *valuenode; | |
| 57 | GaimRequestField *field = flds->data; | |
| 58 | const char *id = gaim_request_field_get_id(field); | |
| 59 | jabber_x_data_field_type type = GPOINTER_TO_INT(g_hash_table_lookup(data->fields, id)); | |
| 60 | ||
| 61 | switch(type) { | |
| 62 | case JABBER_X_DATA_TEXT_SINGLE: | |
| 63 | { | |
| 64 | const char *value = gaim_request_field_string_get_value(field); | |
| 65 | fieldnode = xmlnode_new_child(result, "field"); | |
| 66 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 67 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 68 | if(value) | |
| 69 | xmlnode_insert_data(valuenode, value, -1); | |
| 70 | break; | |
| 71 | } | |
| 72 | case JABBER_X_DATA_TEXT_MULTI: | |
| 73 | { | |
| 74 | char **pieces, **p; | |
| 75 | const char *value = gaim_request_field_string_get_value(field); | |
| 76 | fieldnode = xmlnode_new_child(result, "field"); | |
| 77 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 78 | ||
| 79 | pieces = g_strsplit(value, "\n", -1); | |
| 80 | for(p = pieces; *p != NULL; p++) { | |
| 81 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 82 | xmlnode_insert_data(valuenode, *p, -1); | |
| 83 | } | |
| 84 | g_strfreev(pieces); | |
| 85 | } | |
| 86 | break; | |
| 87 | case JABBER_X_DATA_LIST_SINGLE: | |
| 88 | case JABBER_X_DATA_LIST_MULTI: | |
| 89 | { | |
| 90 | const GList *selected = gaim_request_field_list_get_selected(field); | |
| 91 | char *value; | |
| 92 | fieldnode = xmlnode_new_child(result, "field"); | |
| 93 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 94 | ||
| 95 | while(selected) { | |
| 96 | value = gaim_request_field_list_get_data(field, selected->data); | |
| 97 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 98 | if(value) | |
| 99 | xmlnode_insert_data(valuenode, value, -1); | |
| 100 | selected = selected->next; | |
| 101 | } | |
| 102 | } | |
| 103 | break; | |
| 104 | case JABBER_X_DATA_BOOLEAN: | |
| 105 | fieldnode = xmlnode_new_child(result, "field"); | |
| 106 | xmlnode_set_attrib(fieldnode, "var", id); | |
| 107 | valuenode = xmlnode_new_child(fieldnode, "value"); | |
| 108 | if(gaim_request_field_bool_get_value(field)) | |
| 109 | xmlnode_insert_data(valuenode, "1", -1); | |
| 110 | else | |
| 111 | xmlnode_insert_data(valuenode, "0", -1); | |
| 112 | break; | |
| 113 | case JABBER_X_DATA_IGNORE: | |
| 114 | break; | |
| 115 | } | |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | g_hash_table_destroy(data->fields); | |
| 120 | while(data->values) { | |
| 121 | g_free(data->values->data); | |
| 122 | data->values = g_slist_delete_link(data->values, data->values); | |
| 123 | } | |
| 124 | g_free(data); | |
| 125 | ||
| 126 | cb(js, result, user_data); | |
| 127 | } | |
| 128 | ||
| 129 | static void jabber_x_data_cancel_cb(struct jabber_x_data_data *data, GaimRequestFields *fields) { | |
| 130 | xmlnode *result = xmlnode_new("x"); | |
| 131 | jabber_x_data_cb cb = data->cb; | |
| 132 | gpointer user_data = data->user_data; | |
| 133 | JabberStream *js = data->js; | |
| 134 | g_hash_table_destroy(data->fields); | |
| 135 | while(data->values) { | |
| 136 | g_free(data->values->data); | |
| 137 | data->values = g_slist_delete_link(data->values, data->values); | |
| 138 | } | |
| 139 | g_free(data); | |
| 140 | ||
| 141 | xmlnode_set_attrib(result, "xmlns", "jabber:x:data"); | |
| 142 | xmlnode_set_attrib(result, "type", "cancel"); | |
| 143 | ||
| 144 | cb(js, result, user_data); | |
| 145 | } | |
| 146 | ||
| 147 | void jabber_x_data_request(JabberStream *js, xmlnode *packet, jabber_x_data_cb cb, gpointer user_data) | |
| 148 | { | |
| 149 | xmlnode *fn, *x; | |
| 150 | GaimRequestFields *fields; | |
| 151 | GaimRequestFieldGroup *group; | |
| 152 | GaimRequestField *field; | |
| 153 | ||
| 154 | char *title = NULL; | |
| 155 | char *instructions = NULL; | |
| 156 | ||
| 157 | struct jabber_x_data_data *data = g_new0(struct jabber_x_data_data, 1); | |
| 158 | ||
| 159 | data->fields = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
| 160 | data->user_data = user_data; | |
| 161 | data->cb = cb; | |
| 162 | data->js = js; | |
| 163 | ||
| 164 | fields = gaim_request_fields_new(); | |
| 165 | group = gaim_request_field_group_new(NULL); | |
| 166 | gaim_request_fields_add_group(fields, group); | |
| 167 | ||
| 168 | for(fn = packet->child; fn; fn = fn->next) { | |
| 169 | if(fn->name && !strcmp(fn->name, "field")) { | |
| 170 | xmlnode *valuenode; | |
| 171 | const char *type = xmlnode_get_attrib(fn, "type"); | |
| 172 | const char *label = xmlnode_get_attrib(fn, "label"); | |
| 173 | const char *var = xmlnode_get_attrib(fn, "var"); | |
| 174 | char *value = NULL; | |
| 175 | ||
| 176 | if(!type) | |
| 177 | continue; | |
| 178 | ||
| 179 | if(!var && strcmp(type, "fixed")) | |
| 180 | continue; | |
| 181 | if(!label) | |
| 182 | label = var; | |
| 183 | ||
| 184 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 185 | value = xmlnode_get_data(valuenode); | |
| 186 | ||
| 187 | ||
| 188 | /* XXX: handle <required/> */ | |
| 189 | ||
| 190 | if(!strcmp(type, "text-private")) { | |
| 191 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 192 | value = xmlnode_get_data(valuenode); | |
| 193 | ||
| 194 | field = gaim_request_field_string_new(var, label, | |
| 195 | value ? value : "", FALSE); | |
| 196 | gaim_request_field_string_set_masked(field, TRUE); | |
| 197 | gaim_request_field_group_add_field(group, field); | |
| 198 | ||
| 199 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); | |
| 200 | ||
| 201 | if(value) | |
| 202 | g_free(value); | |
| 203 | } else if(!strcmp(type, "text-multi") || !strcmp(type, "jid-multi")) { | |
| 204 | GString *str = g_string_new(""); | |
| 205 | ||
| 206 | for(valuenode = fn->child; valuenode; valuenode = valuenode->next) { | |
| 207 | if(strcmp(valuenode->name, "value")) | |
| 208 | continue; | |
| 209 | ||
| 210 | if(!(value = xmlnode_get_data(valuenode))) | |
| 211 | continue; | |
| 212 | ||
| 213 | g_string_append_printf(str, "%s\n", value); | |
| 214 | g_free(value); | |
| 215 | } | |
| 216 | ||
| 217 | field = gaim_request_field_string_new(var, label, | |
| 218 | str->str, TRUE); | |
| 219 | gaim_request_field_group_add_field(group, field); | |
| 220 | ||
| 221 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_MULTI)); | |
| 222 | ||
| 223 | g_string_free(str, TRUE); | |
| 224 | } else if(!strcmp(type, "list-single") || !strcmp(type, "list-multi")) { | |
| 225 | xmlnode *optnode; | |
| 226 | GList *selected = NULL; | |
| 227 | ||
| 228 | field = gaim_request_field_list_new(var, label); | |
| 229 | ||
| 230 | if(!strcmp(type, "list-multi")) { | |
| 231 | gaim_request_field_list_set_multi_select(field, TRUE); | |
| 232 | g_hash_table_replace(data->fields, g_strdup(var), | |
| 233 | GINT_TO_POINTER(JABBER_X_DATA_LIST_MULTI)); | |
| 234 | } else { | |
| 235 | g_hash_table_replace(data->fields, g_strdup(var), | |
| 236 | GINT_TO_POINTER(JABBER_X_DATA_LIST_SINGLE)); | |
| 237 | } | |
| 238 | ||
| 239 | for(valuenode = fn->child; valuenode; valuenode = valuenode->next) { | |
| 240 | if(strcmp(valuenode->name, "value")) | |
| 241 | continue; | |
| 242 | selected = g_list_prepend(selected, xmlnode_get_data(valuenode)); | |
| 243 | } | |
| 244 | ||
| 245 | for(optnode = fn->child; optnode; optnode = optnode->next) { | |
| 246 | const char *lbl; | |
| 247 | ||
| 248 | if(strcmp(optnode->name, "option")) | |
| 249 | continue; | |
| 250 | ||
| 251 | if(!(valuenode = xmlnode_get_child(optnode, "value"))) | |
| 252 | continue; | |
| 253 | ||
| 254 | if(!(value = xmlnode_get_data(valuenode))) | |
| 255 | continue; | |
| 256 | ||
| 257 | if(!(lbl = xmlnode_get_attrib(optnode, "label"))) | |
| 258 | label = value; | |
| 259 | ||
| 260 | data->values = g_slist_prepend(data->values, value); | |
| 261 | ||
| 262 | gaim_request_field_list_add(field, lbl, value); | |
| 7965 | 263 | if(g_list_find_custom(selected, value, (GCompareFunc)strcmp)) |
| 7923 | 264 | gaim_request_field_list_add_selected(field, lbl); |
| 265 | } | |
| 266 | gaim_request_field_group_add_field(group, field); | |
| 267 | ||
| 268 | while(selected) { | |
| 269 | g_free(selected->data); | |
| 270 | selected = g_list_delete_link(selected, selected); | |
| 271 | } | |
| 272 | ||
| 273 | } else if(!strcmp(type, "boolean")) { | |
| 274 | gboolean def = FALSE; | |
| 275 | ||
| 276 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 277 | value = xmlnode_get_data(valuenode); | |
| 278 | ||
| 279 | if(value && (!strcasecmp(value, "yes") || | |
| 280 | !strcasecmp(value, "true") || !strcasecmp(value, "1"))) | |
| 281 | def = TRUE; | |
| 282 | ||
| 283 | field = gaim_request_field_bool_new(var, label, def); | |
| 284 | gaim_request_field_group_add_field(group, field); | |
| 285 | ||
| 286 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_BOOLEAN)); | |
| 287 | ||
| 288 | if(value) | |
| 289 | g_free(value); | |
| 290 | } else if(!strcmp(type, "fixed") && value) { | |
| 291 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 292 | value = xmlnode_get_data(valuenode); | |
| 293 | ||
| 294 | field = gaim_request_field_label_new("", value); | |
| 295 | gaim_request_field_group_add_field(group, field); | |
| 296 | ||
| 297 | if(value) | |
| 298 | g_free(value); | |
| 299 | } else if(!strcmp(type, "hidden")) { | |
| 300 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 301 | value = xmlnode_get_data(valuenode); | |
| 302 | ||
| 303 | field = gaim_request_field_string_new(var, "", value ? value : "", | |
| 304 | FALSE); | |
| 305 | gaim_request_field_set_visible(field, FALSE); | |
| 306 | gaim_request_field_group_add_field(group, field); | |
| 307 | ||
| 308 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); | |
| 309 | ||
| 310 | if(value) | |
| 311 | g_free(value); | |
| 312 | } else { /* text-single, jid-single, and the default */ | |
| 313 | if((valuenode = xmlnode_get_child(fn, "value"))) | |
| 314 | value = xmlnode_get_data(valuenode); | |
| 315 | ||
| 316 | field = gaim_request_field_string_new(var, label, | |
| 317 | value ? value : "", FALSE); | |
| 318 | gaim_request_field_group_add_field(group, field); | |
| 319 | ||
| 320 | g_hash_table_replace(data->fields, g_strdup(var), GINT_TO_POINTER(JABBER_X_DATA_TEXT_SINGLE)); | |
| 321 | ||
| 322 | if(value) | |
| 323 | g_free(value); | |
| 324 | } | |
| 325 | } | |
| 326 | } | |
| 327 | ||
| 328 | if((x = xmlnode_get_child(packet, "title"))) | |
| 329 | title = xmlnode_get_data(x); | |
| 330 | ||
| 331 | if((x = xmlnode_get_child(packet, "instructions"))) | |
| 332 | instructions = xmlnode_get_data(x); | |
| 333 | ||
| 334 | gaim_request_fields(js->gc, title, title, instructions, fields, _("OK"), G_CALLBACK(jabber_x_data_ok_cb), | |
| 335 | _("Cancel"), G_CALLBACK(jabber_x_data_cancel_cb), data); | |
| 336 | ||
| 337 | if(title) | |
| 338 | g_free(title); | |
| 339 | if(instructions) | |
| 340 | g_free(instructions); | |
| 341 | ||
| 342 | } | |
| 343 | ||
| 344 |