Mercurial > pidgin.yaz
comparison src/request.c @ 5807:11001789cb22
[gaim-migrate @ 6237]
Added a multi-field request API. Haven't tested it yet, or written a UI
implementation, so parts may change.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sun, 08 Jun 2003 04:06:21 +0000 |
parents | b7c0be69c749 |
children | 133e86584f4b |
comparison
equal
deleted
inserted
replaced
5806:a997762fbaeb | 5807:11001789cb22 |
---|---|
32 void *ui_handle; | 32 void *ui_handle; |
33 | 33 |
34 } GaimRequestInfo; | 34 } GaimRequestInfo; |
35 | 35 |
36 | 36 |
37 GaimRequestFields * | |
38 gaim_request_fields_new(void) | |
39 { | |
40 GaimRequestFields *fields; | |
41 | |
42 fields = g_new0(GaimRequestFields, 1); | |
43 | |
44 fields->fields = g_hash_table_new_full(g_str_hash, g_str_equal, | |
45 g_free, NULL); | |
46 | |
47 return fields; | |
48 } | |
49 | |
50 void | |
51 gaim_request_fields_destroy(GaimRequestFields *fields) | |
52 { | |
53 GList *l; | |
54 GaimRequestFieldGroup *group; | |
55 | |
56 g_return_if_fail(fields != NULL); | |
57 | |
58 for (l = fields->groups; l != NULL; l = l->next) { | |
59 group = l->data; | |
60 | |
61 gaim_request_field_group_destroy(group); | |
62 } | |
63 | |
64 g_list_free(fields->groups); | |
65 | |
66 g_hash_table_destroy(fields->fields); | |
67 | |
68 g_free(fields); | |
69 } | |
70 | |
71 void | |
72 gaim_request_fields_add_group(GaimRequestFields *fields, | |
73 GaimRequestFieldGroup *group) | |
74 { | |
75 GList *l; | |
76 GaimRequestField *field; | |
77 | |
78 g_return_if_fail(fields != NULL); | |
79 g_return_if_fail(group != NULL); | |
80 | |
81 fields->groups = g_list_append(fields->groups, group); | |
82 | |
83 group->fields_list = fields; | |
84 | |
85 for (l = gaim_request_field_group_get_fields(group); | |
86 l != NULL; | |
87 l = l->next) { | |
88 | |
89 field = l->data; | |
90 | |
91 g_hash_table_insert(fields->fields, | |
92 g_strdup(gaim_request_field_get_id(field)), field); | |
93 } | |
94 } | |
95 | |
96 GList * | |
97 gaim_request_fields_get_groups(const GaimRequestFields *fields) | |
98 { | |
99 g_return_val_if_fail(fields != NULL, NULL); | |
100 | |
101 return fields->groups; | |
102 } | |
103 | |
104 GaimRequestField * | |
105 gaim_request_fields_get_field(const GaimRequestFields *fields, const char *id) | |
106 { | |
107 GaimRequestField *field; | |
108 | |
109 g_return_val_if_fail(fields != NULL, NULL); | |
110 g_return_val_if_fail(id != NULL, NULL); | |
111 | |
112 field = g_hash_table_lookup(fields->fields, id); | |
113 | |
114 g_return_val_if_fail(field != NULL, NULL); | |
115 | |
116 return field; | |
117 } | |
118 | |
119 const char * | |
120 gaim_request_fields_get_string(const GaimRequestFields *fields, const char *id) | |
121 { | |
122 GaimRequestField *field; | |
123 | |
124 g_return_val_if_fail(fields != NULL, NULL); | |
125 g_return_val_if_fail(id != NULL, NULL); | |
126 | |
127 if ((field = gaim_request_fields_get_field(fields, id)) == NULL) | |
128 return NULL; | |
129 | |
130 return gaim_request_field_string_get_value(field); | |
131 } | |
132 | |
133 int | |
134 gaim_request_fields_get_integer(const GaimRequestFields *fields, | |
135 const char *id) | |
136 { | |
137 GaimRequestField *field; | |
138 | |
139 g_return_val_if_fail(fields != NULL, 0); | |
140 g_return_val_if_fail(id != NULL, 0); | |
141 | |
142 if ((field = gaim_request_fields_get_field(fields, id)) == NULL) | |
143 return 0; | |
144 | |
145 return gaim_request_field_int_get_value(field); | |
146 } | |
147 | |
148 gboolean | |
149 gaim_request_fields_get_bool(const GaimRequestFields *fields, const char *id) | |
150 { | |
151 GaimRequestField *field; | |
152 | |
153 g_return_val_if_fail(fields != NULL, FALSE); | |
154 g_return_val_if_fail(id != NULL, FALSE); | |
155 | |
156 if ((field = gaim_request_fields_get_field(fields, id)) == NULL) | |
157 return FALSE; | |
158 | |
159 return gaim_request_field_bool_get_value(field); | |
160 } | |
161 | |
162 int | |
163 gaim_request_fields_get_choice(const GaimRequestFields *fields, const char *id) | |
164 { | |
165 GaimRequestField *field; | |
166 | |
167 g_return_val_if_fail(fields != NULL, -1); | |
168 g_return_val_if_fail(id != NULL, -1); | |
169 | |
170 if ((field = gaim_request_fields_get_field(fields, id)) == NULL) | |
171 return -1; | |
172 | |
173 return gaim_request_field_choice_get_value(field); | |
174 } | |
175 | |
176 GaimRequestFieldGroup * | |
177 gaim_request_field_group_new(const char *title) | |
178 { | |
179 GaimRequestFieldGroup *group; | |
180 | |
181 group = g_new0(GaimRequestFieldGroup, 1); | |
182 | |
183 if (title != NULL) | |
184 group->title = g_strdup(title); | |
185 | |
186 return group; | |
187 } | |
188 | |
189 void | |
190 gaim_request_field_group_destroy(GaimRequestFieldGroup *group) | |
191 { | |
192 GaimRequestField *field; | |
193 GList *l; | |
194 | |
195 g_return_if_fail(group != NULL); | |
196 | |
197 if (group->title != NULL) | |
198 g_free(group->title); | |
199 | |
200 for (l = group->fields; l != NULL; l = l->next) { | |
201 field = l->data; | |
202 | |
203 gaim_request_field_destroy(field); | |
204 } | |
205 | |
206 g_list_free(group->fields); | |
207 | |
208 g_free(group); | |
209 } | |
210 | |
211 void | |
212 gaim_request_field_group_add_field(GaimRequestFieldGroup *group, | |
213 GaimRequestField *field) | |
214 { | |
215 g_return_if_fail(group != NULL); | |
216 g_return_if_fail(field != NULL); | |
217 | |
218 group->fields = g_list_append(group->fields, field); | |
219 | |
220 if (group->fields_list != NULL) { | |
221 g_hash_table_insert(group->fields_list->fields, | |
222 g_strdup(gaim_request_field_get_id(field)), field); | |
223 } | |
224 } | |
225 | |
226 const char * | |
227 gaim_request_field_group_get_title(const GaimRequestFieldGroup *group) | |
228 { | |
229 g_return_val_if_fail(group != NULL, NULL); | |
230 | |
231 return group->title; | |
232 } | |
233 | |
234 GList * | |
235 gaim_request_field_group_get_fields(const GaimRequestFieldGroup *group) | |
236 { | |
237 g_return_val_if_fail(group != NULL, NULL); | |
238 | |
239 return group->fields; | |
240 } | |
241 | |
242 GaimRequestField * | |
243 gaim_request_field_new(const char *id, const char *text, | |
244 GaimRequestFieldType type) | |
245 { | |
246 GaimRequestField *field; | |
247 | |
248 g_return_val_if_fail(id != NULL, NULL); | |
249 g_return_val_if_fail(type != GAIM_REQUEST_FIELD_NONE, NULL); | |
250 | |
251 field = g_new0(GaimRequestField, 1); | |
252 | |
253 field->id = g_strdup(id); | |
254 field->type = type; | |
255 | |
256 gaim_request_field_set_label(field, text); | |
257 | |
258 return field; | |
259 } | |
260 | |
261 void | |
262 gaim_request_field_destroy(GaimRequestField *field) | |
263 { | |
264 g_return_if_fail(field != NULL); | |
265 | |
266 if (field->id != NULL) | |
267 g_free(field->id); | |
268 | |
269 if (field->label != NULL) | |
270 g_free(field->label); | |
271 | |
272 if (field->type == GAIM_REQUEST_FIELD_STRING) { | |
273 if (field->u.string.default_value != NULL) | |
274 g_free(field->u.string.default_value); | |
275 | |
276 if (field->u.string.value != NULL) | |
277 g_free(field->u.string.value); | |
278 } | |
279 else if (field->type == GAIM_REQUEST_FIELD_CHOICE) { | |
280 GList *l; | |
281 | |
282 for (l = field->u.choice.labels; l != NULL; l = l->next) { | |
283 g_free(l->data); | |
284 } | |
285 | |
286 if (field->u.choice.labels != NULL) | |
287 g_list_free(field->u.choice.labels); | |
288 } | |
289 | |
290 g_free(field); | |
291 } | |
292 | |
293 void | |
294 gaim_request_field_set_label(GaimRequestField *field, const char *label) | |
295 { | |
296 g_return_if_fail(field != NULL); | |
297 | |
298 if (field->label != NULL) | |
299 g_free(field->label); | |
300 | |
301 field->label = (label == NULL ? NULL : g_strdup(label)); | |
302 } | |
303 | |
304 GaimRequestFieldType | |
305 gaim_request_field_get_type(const GaimRequestField *field) | |
306 { | |
307 g_return_val_if_fail(field != NULL, GAIM_REQUEST_FIELD_NONE); | |
308 | |
309 return field->type; | |
310 } | |
311 | |
312 const char * | |
313 gaim_request_field_get_id(const GaimRequestField *field) | |
314 { | |
315 g_return_val_if_fail(field != NULL, NULL); | |
316 | |
317 return field->id; | |
318 } | |
319 | |
320 const char * | |
321 gaim_request_field_get_label(const GaimRequestField *field) | |
322 { | |
323 g_return_val_if_fail(field != NULL, NULL); | |
324 | |
325 return field->label; | |
326 } | |
327 | |
328 GaimRequestField * | |
329 gaim_request_field_string_new(const char *id, const char *text, | |
330 const char *default_value, gboolean multiline) | |
331 { | |
332 GaimRequestField *field; | |
333 | |
334 g_return_val_if_fail(id != NULL, NULL); | |
335 g_return_val_if_fail(text != NULL, NULL); | |
336 | |
337 field = gaim_request_field_new(id, text, GAIM_REQUEST_FIELD_STRING); | |
338 | |
339 field->u.string.multiline = multiline; | |
340 | |
341 gaim_request_field_string_set_default_value(field, default_value); | |
342 | |
343 return field; | |
344 } | |
345 | |
346 void | |
347 gaim_request_field_string_set_default_value(GaimRequestField *field, | |
348 const char *default_value) | |
349 { | |
350 g_return_if_fail(field != NULL); | |
351 g_return_if_fail(field->type == GAIM_REQUEST_FIELD_STRING); | |
352 | |
353 if (field->u.string.default_value != NULL) | |
354 g_free(field->u.string.default_value); | |
355 | |
356 field->u.string.default_value = (default_value == NULL | |
357 ? NULL : g_strdup(default_value)); | |
358 } | |
359 | |
360 const char * | |
361 gaim_request_field_string_get_default_value(const GaimRequestField *field) | |
362 { | |
363 g_return_val_if_fail(field != NULL, NULL); | |
364 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_STRING, NULL); | |
365 | |
366 return field->u.string.default_value; | |
367 } | |
368 | |
369 const char * | |
370 gaim_request_field_string_get_value(const GaimRequestField *field) | |
371 { | |
372 g_return_val_if_fail(field != NULL, NULL); | |
373 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_STRING, NULL); | |
374 | |
375 return field->u.string.value; | |
376 } | |
377 | |
378 gboolean | |
379 gaim_request_field_string_is_multiline(const GaimRequestField *field) | |
380 { | |
381 g_return_val_if_fail(field != NULL, FALSE); | |
382 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_STRING, FALSE); | |
383 | |
384 return field->u.string.multiline; | |
385 } | |
386 | |
387 GaimRequestField * | |
388 gaim_request_field_int_new(const char *id, const char *text, | |
389 int default_value) | |
390 { | |
391 GaimRequestField *field; | |
392 | |
393 g_return_val_if_fail(id != NULL, NULL); | |
394 g_return_val_if_fail(text != NULL, NULL); | |
395 | |
396 field = gaim_request_field_new(id, text, GAIM_REQUEST_FIELD_INTEGER); | |
397 | |
398 gaim_request_field_int_set_default_value(field, default_value); | |
399 | |
400 return field; | |
401 } | |
402 | |
403 void | |
404 gaim_request_field_int_set_default_value(GaimRequestField *field, | |
405 int default_value) | |
406 { | |
407 g_return_if_fail(field != NULL); | |
408 g_return_if_fail(field->type == GAIM_REQUEST_FIELD_INTEGER); | |
409 | |
410 field->u.integer.default_value = default_value; | |
411 } | |
412 | |
413 int | |
414 gaim_request_field_int_get_default_value(const GaimRequestField *field) | |
415 { | |
416 g_return_val_if_fail(field != NULL, 0); | |
417 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_INTEGER, 0); | |
418 | |
419 return field->u.integer.default_value; | |
420 } | |
421 | |
422 int | |
423 gaim_request_field_int_get_value(const GaimRequestField *field) | |
424 { | |
425 g_return_val_if_fail(field != NULL, 0); | |
426 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_INTEGER, 0); | |
427 | |
428 return field->u.integer.value; | |
429 } | |
430 | |
431 GaimRequestField * | |
432 gaim_request_field_bool_new(const char *id, const char *text, | |
433 gboolean default_value) | |
434 { | |
435 GaimRequestField *field; | |
436 | |
437 g_return_val_if_fail(id != NULL, NULL); | |
438 g_return_val_if_fail(text != NULL, NULL); | |
439 | |
440 field = gaim_request_field_new(id, text, GAIM_REQUEST_FIELD_BOOLEAN); | |
441 | |
442 gaim_request_field_bool_set_default_value(field, default_value); | |
443 | |
444 return field; | |
445 } | |
446 | |
447 void | |
448 gaim_request_field_bool_set_default_value(GaimRequestField *field, | |
449 gboolean default_value) | |
450 { | |
451 g_return_if_fail(field != NULL); | |
452 g_return_if_fail(field->type == GAIM_REQUEST_FIELD_BOOLEAN); | |
453 | |
454 field->u.boolean.default_value = default_value; | |
455 } | |
456 | |
457 gboolean | |
458 gaim_request_field_bool_get_default_value(const GaimRequestField *field) | |
459 { | |
460 g_return_val_if_fail(field != NULL, FALSE); | |
461 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_BOOLEAN, FALSE); | |
462 | |
463 return field->u.boolean.default_value; | |
464 } | |
465 | |
466 gboolean | |
467 gaim_request_field_bool_get_value(const GaimRequestField *field) | |
468 { | |
469 g_return_val_if_fail(field != NULL, FALSE); | |
470 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_BOOLEAN, FALSE); | |
471 | |
472 return field->u.boolean.value; | |
473 } | |
474 | |
475 GaimRequestField * | |
476 gaim_request_field_choice_new(const char *id, const char *text, | |
477 int default_value) | |
478 { | |
479 GaimRequestField *field; | |
480 | |
481 g_return_val_if_fail(id != NULL, NULL); | |
482 g_return_val_if_fail(text != NULL, NULL); | |
483 | |
484 field = gaim_request_field_new(id, text, GAIM_REQUEST_FIELD_CHOICE); | |
485 | |
486 gaim_request_field_choice_set_default_value(field, default_value); | |
487 | |
488 return field; | |
489 } | |
490 | |
491 void | |
492 gaim_request_field_choice_add(GaimRequestField *field, const char *label) | |
493 { | |
494 g_return_if_fail(field != NULL); | |
495 g_return_if_fail(label != NULL); | |
496 g_return_if_fail(field->type == GAIM_REQUEST_FIELD_CHOICE); | |
497 | |
498 field->u.choice.labels = g_list_append(field->u.choice.labels, | |
499 g_strdup(label)); | |
500 } | |
501 | |
502 void | |
503 gaim_request_field_choice_set_default_value(GaimRequestField *field, | |
504 int default_value) | |
505 { | |
506 g_return_if_fail(field != NULL); | |
507 g_return_if_fail(field->type == GAIM_REQUEST_FIELD_CHOICE); | |
508 | |
509 field->u.choice.default_value = default_value; | |
510 } | |
511 | |
512 int | |
513 gaim_request_field_choice_get_default_value(const GaimRequestField *field) | |
514 { | |
515 g_return_val_if_fail(field != NULL, -1); | |
516 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_CHOICE, -1); | |
517 | |
518 return field->u.choice.default_value; | |
519 } | |
520 | |
521 int | |
522 gaim_request_field_choice_get_value(const GaimRequestField *field) | |
523 { | |
524 g_return_val_if_fail(field != NULL, -1); | |
525 g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_CHOICE, -1); | |
526 | |
527 return field->u.choice.value; | |
528 } | |
529 | |
530 /* -- */ | |
531 | |
37 void * | 532 void * |
38 gaim_request_input(void *handle, const char *title, const char *primary, | 533 gaim_request_input(void *handle, const char *title, const char *primary, |
39 const char *secondary, const char *default_value, | 534 const char *secondary, const char *default_value, |
40 gboolean multiline, | 535 gboolean multiline, |
41 const char *ok_text, GCallback ok_cb, | 536 const char *ok_text, GCallback ok_cb, |
183 } | 678 } |
184 | 679 |
185 return NULL; | 680 return NULL; |
186 } | 681 } |
187 | 682 |
683 void * | |
684 gaim_request_fields(void *handle, const char *title, const char *primary, | |
685 const char *secondary, GaimRequestFields *fields, | |
686 const char *ok_text, GCallback ok_cb, | |
687 const char *cancel_text, GCallback cancel_cb, | |
688 void *user_data) | |
689 { | |
690 GaimRequestUiOps *ops; | |
691 | |
692 g_return_val_if_fail(fields != NULL, NULL); | |
693 g_return_val_if_fail(ok_text != NULL, NULL); | |
694 g_return_val_if_fail(ok_cb != NULL, NULL); | |
695 | |
696 ops = gaim_get_request_ui_ops(); | |
697 | |
698 if (ops != NULL && ops->request_fields != NULL) { | |
699 GaimRequestInfo *info; | |
700 | |
701 info = g_new0(GaimRequestInfo, 1); | |
702 info->type = GAIM_REQUEST_FIELDS; | |
703 info->handle = handle; | |
704 info->ui_handle = ops->request_fields(title, primary, secondary, | |
705 fields, ok_text, ok_cb, | |
706 cancel_text, cancel_cb, | |
707 user_data); | |
708 | |
709 handles = g_list_append(handles, info); | |
710 | |
711 return info->ui_handle; | |
712 } | |
713 | |
714 return NULL; | |
715 } | |
716 | |
188 void | 717 void |
189 gaim_request_close(GaimRequestType type, void *ui_handle) | 718 gaim_request_close(GaimRequestType type, void *ui_handle) |
190 { | 719 { |
191 GList *l; | 720 GList *l; |
192 GaimRequestUiOps *ops; | 721 GaimRequestUiOps *ops; |