2 * @file gntrequest.c GNT Request API
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include <gntbutton.h>
31 #include <gntcheckbox.h>
32 #include <gntcombobox.h>
34 #include <gntfilesel.h>
40 #include "gntrequest.h"
44 /* XXX: Until gobjectification ... */
47 #define FINCH_GET_DATA(obj) purple_request_field_get_ui_data(obj)
48 #define FINCH_SET_DATA(obj, data) purple_request_field_set_ui_data(obj, data)
59 setup_request_window(const char *title
, const char *primary
,
60 const char *secondary
, PurpleRequestType type
)
64 window
= gnt_vbox_new(FALSE
);
65 gnt_box_set_toplevel(GNT_BOX(window
), TRUE
);
66 gnt_box_set_title(GNT_BOX(window
), title
);
67 gnt_box_set_alignment(GNT_BOX(window
), GNT_ALIGN_MID
);
70 gnt_box_add_widget(GNT_BOX(window
),
71 gnt_label_new_with_format(primary
, GNT_TEXT_FLAG_BOLD
));
73 gnt_box_add_widget(GNT_BOX(window
), gnt_label_new(secondary
));
75 g_signal_connect_swapped(G_OBJECT(window
), "destroy", G_CALLBACK(purple_request_close
),
76 GINT_TO_POINTER(type
));
82 * If the window is closed by the wm (ie, without triggering any of
83 * the buttons, then do some default callback.
86 setup_default_callback(GntWidget
*window
, gpointer default_cb
, gpointer data
)
88 if (default_cb
== NULL
)
90 g_object_set_data(G_OBJECT(window
), "default-callback", default_cb
);
91 g_signal_connect_swapped(G_OBJECT(window
), "destroy", G_CALLBACK(default_cb
), data
);
95 action_performed(GntWidget
*button
, gpointer data
)
97 g_signal_handlers_disconnect_matched(data
, G_SIGNAL_MATCH_FUNC
,
99 g_object_get_data(data
, "default-callback"),
104 * window: this is the window
105 * userdata: the userdata to pass to the primary callbacks
107 * data: data for the callback
108 * (text, primary-callback) pairs, ended by a NULL
110 * The cancellation callback should be the last callback sent.
113 setup_button_box(GntWidget
*win
, gpointer userdata
, gpointer cb
, gpointer data
, ...)
116 GntWidget
*button
= NULL
;
121 box
= gnt_hbox_new(FALSE
);
123 va_start(list
, data
);
125 while ((text
= va_arg(list
, const char *)))
127 callback
= va_arg(list
, gpointer
);
128 button
= gnt_button_new(text
);
129 gnt_box_add_widget(GNT_BOX(box
), button
);
130 g_object_set_data(G_OBJECT(button
), "activate-callback", callback
);
131 g_object_set_data(G_OBJECT(button
), "activate-userdata", userdata
);
132 g_signal_connect(G_OBJECT(button
), "activate", G_CALLBACK(action_performed
), win
);
133 g_signal_connect(G_OBJECT(button
), "activate", G_CALLBACK(cb
), data
);
137 g_object_set_data(G_OBJECT(button
), "cancellation-function", GINT_TO_POINTER(TRUE
));
144 notify_input_cb(GntWidget
*button
, GntWidget
*entry
)
146 PurpleRequestInputCb callback
= g_object_get_data(G_OBJECT(button
), "activate-callback");
147 gpointer data
= g_object_get_data(G_OBJECT(button
), "activate-userdata");
148 const char *text
= gnt_entry_get_text(GNT_ENTRY(entry
));
151 callback(data
, text
);
153 while (button
->parent
)
154 button
= button
->parent
;
156 purple_request_close(PURPLE_REQUEST_INPUT
, button
);
160 finch_request_input(const char *title
, const char *primary
,
161 const char *secondary
, const char *default_value
,
162 gboolean multiline
, gboolean masked
, gchar
*hint
,
163 const char *ok_text
, GCallback ok_cb
,
164 const char *cancel_text
, GCallback cancel_cb
,
165 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
168 GntWidget
*window
, *box
, *entry
;
170 window
= setup_request_window(title
, primary
, secondary
, PURPLE_REQUEST_INPUT
);
172 entry
= gnt_entry_new(default_value
);
174 gnt_entry_set_masked(GNT_ENTRY(entry
), TRUE
);
175 gnt_box_add_widget(GNT_BOX(window
), entry
);
177 box
= setup_button_box(window
, user_data
, notify_input_cb
, entry
,
178 ok_text
, ok_cb
, cancel_text
, cancel_cb
, NULL
);
179 gnt_box_add_widget(GNT_BOX(window
), box
);
181 setup_default_callback(window
, cancel_cb
, user_data
);
182 gnt_widget_show(window
);
188 finch_close_request(PurpleRequestType type
, gpointer ui_handle
)
190 GntWidget
*widget
= GNT_WIDGET(ui_handle
);
191 if (type
== PURPLE_REQUEST_FIELDS
) {
192 PurpleRequestFields
*fields
= g_object_get_data(G_OBJECT(widget
), "fields");
193 purple_request_fields_destroy(fields
);
196 while (widget
->parent
)
197 widget
= widget
->parent
;
198 gnt_widget_destroy(widget
);
202 request_choice_cb(GntWidget
*button
, GntComboBox
*combo
)
204 PurpleRequestChoiceCb callback
= g_object_get_data(G_OBJECT(button
), "activate-callback");
205 gpointer data
= g_object_get_data(G_OBJECT(button
), "activate-userdata");
206 int choice
= GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(combo
))) - 1;
209 callback(data
, choice
);
211 while (button
->parent
)
212 button
= button
->parent
;
214 purple_request_close(PURPLE_REQUEST_INPUT
, button
);
218 finch_request_choice(const char *title
, const char *primary
,
219 const char *secondary
, int default_value
,
220 const char *ok_text
, GCallback ok_cb
,
221 const char *cancel_text
, GCallback cancel_cb
,
222 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
223 void *user_data
, va_list choices
)
225 GntWidget
*window
, *combo
, *box
;
229 window
= setup_request_window(title
, primary
, secondary
, PURPLE_REQUEST_CHOICE
);
231 combo
= gnt_combo_box_new();
232 gnt_box_add_widget(GNT_BOX(window
), combo
);
233 while ((text
= va_arg(choices
, const char *)))
235 val
= va_arg(choices
, int);
236 gnt_combo_box_add_data(GNT_COMBO_BOX(combo
), GINT_TO_POINTER(val
+ 1), text
);
238 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo
), GINT_TO_POINTER(default_value
+ 1));
240 box
= setup_button_box(window
, user_data
, request_choice_cb
, combo
,
241 ok_text
, ok_cb
, cancel_text
, cancel_cb
, NULL
);
242 gnt_box_add_widget(GNT_BOX(window
), box
);
244 setup_default_callback(window
, cancel_cb
, user_data
);
245 gnt_widget_show(window
);
251 request_action_cb(GntWidget
*button
, GntWidget
*window
)
253 PurpleRequestActionCb callback
= g_object_get_data(G_OBJECT(button
), "activate-callback");
254 gpointer data
= g_object_get_data(G_OBJECT(button
), "activate-userdata");
255 int id
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button
), "activate-id"));
260 purple_request_close(PURPLE_REQUEST_ACTION
, window
);
264 finch_request_action(const char *title
, const char *primary
,
265 const char *secondary
, int default_value
,
266 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
267 void *user_data
, size_t actioncount
,
270 GntWidget
*window
, *box
, *button
, *focus
= NULL
;
273 window
= setup_request_window(title
, primary
, secondary
, PURPLE_REQUEST_ACTION
);
275 box
= gnt_hbox_new(FALSE
);
276 gnt_box_add_widget(GNT_BOX(window
), box
);
277 for (i
= 0; i
< actioncount
; i
++)
279 const char *text
= va_arg(actions
, const char *);
280 PurpleRequestActionCb callback
= va_arg(actions
, PurpleRequestActionCb
);
282 button
= gnt_button_new(text
);
283 gnt_box_add_widget(GNT_BOX(box
), button
);
285 g_object_set_data(G_OBJECT(button
), "activate-callback", callback
);
286 g_object_set_data(G_OBJECT(button
), "activate-userdata", user_data
);
287 g_object_set_data(G_OBJECT(button
), "activate-id", GINT_TO_POINTER(i
));
288 g_signal_connect(G_OBJECT(button
), "activate", G_CALLBACK(request_action_cb
), window
);
290 if (i
== default_value
)
294 gnt_widget_show(window
);
296 gnt_box_give_focus_to_child(GNT_BOX(window
), focus
);
302 request_fields_cb(GntWidget
*button
, PurpleRequestFields
*fields
)
304 PurpleRequestFieldsCb callback
= g_object_get_data(G_OBJECT(button
), "activate-callback");
305 gpointer data
= g_object_get_data(G_OBJECT(button
), "activate-userdata");
308 /* Update the data of the fields. Pidgin does this differently. Instead of
309 * updating the fields at the end like here, it updates the appropriate field
310 * instantly whenever a change is made. That allows it to make sure the
311 * 'required' fields are entered before the user can hit OK. It's not the case
312 * here, althought it can be done. */
313 for (list
= purple_request_fields_get_groups(fields
); list
; list
= list
->next
)
315 PurpleRequestFieldGroup
*group
= list
->data
;
316 GList
*fields
= purple_request_field_group_get_fields(group
);
318 for (; fields
; fields
= fields
->next
)
320 PurpleRequestField
*field
= fields
->data
;
321 PurpleRequestFieldType type
= purple_request_field_get_type(field
);
322 if (!purple_request_field_is_visible(field
))
324 if (type
== PURPLE_REQUEST_FIELD_BOOLEAN
)
326 GntWidget
*check
= FINCH_GET_DATA(field
);
327 gboolean value
= gnt_check_box_get_checked(GNT_CHECK_BOX(check
));
328 purple_request_field_bool_set_value(field
, value
);
330 else if (type
== PURPLE_REQUEST_FIELD_STRING
)
332 GntWidget
*entry
= FINCH_GET_DATA(field
);
333 const char *text
= gnt_entry_get_text(GNT_ENTRY(entry
));
334 purple_request_field_string_set_value(field
, (text
&& *text
) ? text
: NULL
);
336 else if (type
== PURPLE_REQUEST_FIELD_INTEGER
)
338 GntWidget
*entry
= FINCH_GET_DATA(field
);
339 const char *text
= gnt_entry_get_text(GNT_ENTRY(entry
));
340 int value
= (text
&& *text
) ? atoi(text
) : 0;
341 purple_request_field_int_set_value(field
, value
);
343 else if (type
== PURPLE_REQUEST_FIELD_CHOICE
)
345 GntWidget
*combo
= FINCH_GET_DATA(field
);
347 id
= GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(combo
)));
348 purple_request_field_choice_set_value(field
, id
);
350 else if (type
== PURPLE_REQUEST_FIELD_LIST
)
352 GList
*list
= NULL
, *iter
;
353 if (purple_request_field_list_get_multi_select(field
))
355 GntWidget
*tree
= FINCH_GET_DATA(field
);
357 iter
= purple_request_field_list_get_items(field
);
358 for (; iter
; iter
= iter
->next
)
360 const char *text
= iter
->data
;
361 gpointer key
= purple_request_field_list_get_data(field
, text
);
362 if (gnt_tree_get_choice(GNT_TREE(tree
), key
))
363 list
= g_list_prepend(list
, (gpointer
)text
);
368 GntWidget
*combo
= FINCH_GET_DATA(field
);
369 gpointer data
= gnt_combo_box_get_selected_data(GNT_COMBO_BOX(combo
));
371 iter
= purple_request_field_list_get_items(field
);
372 for (; iter
; iter
= iter
->next
) {
373 const char *text
= iter
->data
;
374 gpointer key
= purple_request_field_list_get_data(field
, text
);
376 list
= g_list_prepend(list
, (gpointer
)text
);
382 purple_request_field_list_set_selected(field
, list
);
385 else if (type
== PURPLE_REQUEST_FIELD_ACCOUNT
)
387 GntWidget
*combo
= FINCH_GET_DATA(field
);
388 PurpleAccount
*acc
= gnt_combo_box_get_selected_data(GNT_COMBO_BOX(combo
));
389 purple_request_field_account_set_value(field
, acc
);
394 purple_notify_close_with_handle(button
);
396 if (!g_object_get_data(G_OBJECT(button
), "cancellation-function") &&
397 !purple_request_fields_all_required_filled(fields
)) {
398 purple_notify_error(button
, _("Error"),
399 _("You must fill all the required fields."),
400 _("The required fields are underlined."));
405 callback(data
, fields
);
407 while (button
->parent
)
408 button
= button
->parent
;
410 purple_request_close(PURPLE_REQUEST_FIELDS
, button
);
414 update_selected_account(GntEntry
*username
, const char *start
, const char *end
,
415 GntComboBox
*accountlist
)
417 GList
*accounts
= gnt_tree_get_rows(GNT_TREE(accountlist
->dropdown
));
418 const char *name
= gnt_entry_get_text(username
);
420 if (purple_find_buddy(accounts
->data
, name
)) {
421 gnt_combo_box_set_selected(accountlist
, accounts
->data
);
422 gnt_widget_draw(GNT_WIDGET(accountlist
));
425 accounts
= accounts
->next
;
430 create_boolean_field(PurpleRequestField
*field
)
432 const char *label
= purple_request_field_get_label(field
);
433 GntWidget
*check
= gnt_check_box_new(label
);
434 gnt_check_box_set_checked(GNT_CHECK_BOX(check
),
435 purple_request_field_bool_get_default_value(field
));
440 create_string_field(PurpleRequestField
*field
, GntWidget
**username
)
442 const char *hint
= purple_request_field_get_type_hint(field
);
443 GntWidget
*entry
= gnt_entry_new(
444 purple_request_field_string_get_default_value(field
));
445 gnt_entry_set_masked(GNT_ENTRY(entry
),
446 purple_request_field_string_is_masked(field
));
447 if (hint
&& purple_str_has_prefix(hint
, "screenname")) {
448 PurpleBlistNode
*node
= purple_blist_get_root();
449 gboolean offline
= purple_str_has_suffix(hint
, "all");
450 for (; node
; node
= purple_blist_node_next(node
, offline
)) {
451 if (!PURPLE_BLIST_NODE_IS_BUDDY(node
))
453 gnt_entry_add_suggest(GNT_ENTRY(entry
), purple_buddy_get_name((PurpleBuddy
*)node
));
455 gnt_entry_set_always_suggest(GNT_ENTRY(entry
), TRUE
);
458 } else if (hint
&& !strcmp(hint
, "group")) {
459 PurpleBlistNode
*node
;
460 for (node
= purple_blist_get_root(); node
;
461 node
= purple_blist_node_get_sibling_next(node
)) {
462 if (PURPLE_BLIST_NODE_IS_GROUP(node
))
463 gnt_entry_add_suggest(GNT_ENTRY(entry
), purple_group_get_name((PurpleGroup
*)node
));
470 create_integer_field(PurpleRequestField
*field
)
473 int val
= purple_request_field_int_get_default_value(field
);
476 snprintf(str
, sizeof(str
), "%d", val
);
477 entry
= gnt_entry_new(str
);
478 gnt_entry_set_flag(GNT_ENTRY(entry
), GNT_ENTRY_FLAG_INT
);
483 create_choice_field(PurpleRequestField
*field
)
487 GntWidget
*combo
= gnt_combo_box_new();
489 list
= purple_request_field_choice_get_labels(field
);
490 for (id
= 1; list
; list
= list
->next
, id
++)
492 gnt_combo_box_add_data(GNT_COMBO_BOX(combo
),
493 GINT_TO_POINTER(id
), list
->data
);
495 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo
),
496 GINT_TO_POINTER(purple_request_field_choice_get_default_value(field
)));
501 create_list_field(PurpleRequestField
*field
)
503 GntWidget
*ret
= NULL
;
505 gboolean multi
= purple_request_field_list_get_multi_select(field
);
508 GntWidget
*tree
= gnt_tree_new();
510 list
= purple_request_field_list_get_items(field
);
511 for (; list
; list
= list
->next
)
513 const char *text
= list
->data
;
514 gpointer key
= purple_request_field_list_get_data(field
, text
);
515 gnt_tree_add_choice(GNT_TREE(tree
), key
,
516 gnt_tree_create_row(GNT_TREE(tree
), text
), NULL
, NULL
);
517 if (purple_request_field_list_is_selected(field
, text
))
518 gnt_tree_set_choice(GNT_TREE(tree
), key
, TRUE
);
524 GntWidget
*combo
= gnt_combo_box_new();
526 list
= purple_request_field_list_get_items(field
);
527 for (; list
; list
= list
->next
)
529 const char *text
= list
->data
;
530 gpointer key
= purple_request_field_list_get_data(field
, text
);
531 gnt_combo_box_add_data(GNT_COMBO_BOX(combo
), key
, text
);
532 if (purple_request_field_list_is_selected(field
, text
))
533 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo
), key
);
541 create_account_field(PurpleRequestField
*field
)
546 GntWidget
*combo
= gnt_combo_box_new();
548 all
= purple_request_field_account_get_show_all(field
);
549 def
= purple_request_field_account_get_value(field
);
551 def
= purple_request_field_account_get_default_value(field
);
554 list
= purple_accounts_get_all();
556 list
= purple_connections_get_all();
558 for (; list
; list
= list
->next
)
560 PurpleAccount
*account
;
564 account
= list
->data
;
566 account
= purple_connection_get_account(list
->data
);
568 text
= g_strdup_printf("%s (%s)",
569 purple_account_get_username(account
),
570 purple_account_get_protocol_name(account
));
571 gnt_combo_box_add_data(GNT_COMBO_BOX(combo
), account
, text
);
574 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo
), account
);
576 gnt_widget_set_size(combo
, 20, 3); /* ew */
581 finch_request_fields(const char *title
, const char *primary
,
582 const char *secondary
, PurpleRequestFields
*allfields
,
583 const char *ok
, GCallback ok_cb
,
584 const char *cancel
, GCallback cancel_cb
,
585 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
588 GntWidget
*window
, *box
;
590 GntWidget
*username
= NULL
, *accountlist
= NULL
;
592 window
= setup_request_window(title
, primary
, secondary
, PURPLE_REQUEST_FIELDS
);
594 /* This is how it's going to work: the request-groups are going to be
595 * stacked vertically one after the other. A GntLine will be separating
597 box
= gnt_vbox_new(FALSE
);
598 gnt_box_set_pad(GNT_BOX(box
), 0);
599 gnt_box_set_fill(GNT_BOX(box
), TRUE
);
600 for (grlist
= purple_request_fields_get_groups(allfields
); grlist
; grlist
= grlist
->next
)
602 PurpleRequestFieldGroup
*group
= grlist
->data
;
603 GList
*fields
= purple_request_field_group_get_fields(group
);
605 const char *title
= purple_request_field_group_get_title(group
);
608 gnt_box_add_widget(GNT_BOX(box
),
609 gnt_label_new_with_format(title
, GNT_TEXT_FLAG_BOLD
));
611 for (; fields
; fields
= fields
->next
)
613 PurpleRequestField
*field
= fields
->data
;
614 PurpleRequestFieldType type
= purple_request_field_get_type(field
);
615 const char *label
= purple_request_field_get_label(field
);
617 if (!purple_request_field_is_visible(field
))
620 hbox
= gnt_hbox_new(TRUE
); /* hrm */
621 gnt_box_add_widget(GNT_BOX(box
), hbox
);
623 if (type
!= PURPLE_REQUEST_FIELD_BOOLEAN
&& label
)
626 if (purple_request_field_is_required(field
))
627 l
= gnt_label_new_with_format(label
, GNT_TEXT_FLAG_UNDERLINE
);
629 l
= gnt_label_new(label
);
630 gnt_widget_set_size(l
, 0, 1);
631 gnt_box_add_widget(GNT_BOX(hbox
), l
);
634 if (type
== PURPLE_REQUEST_FIELD_BOOLEAN
)
636 FINCH_SET_DATA(field
, create_boolean_field(field
));
638 else if (type
== PURPLE_REQUEST_FIELD_STRING
)
640 FINCH_SET_DATA(field
, create_string_field(field
, &username
));
642 else if (type
== PURPLE_REQUEST_FIELD_INTEGER
)
644 FINCH_SET_DATA(field
, create_integer_field(field
));
646 else if (type
== PURPLE_REQUEST_FIELD_CHOICE
)
648 FINCH_SET_DATA(field
, create_choice_field(field
));
650 else if (type
== PURPLE_REQUEST_FIELD_LIST
)
652 FINCH_SET_DATA(field
, create_list_field(field
));
654 else if (type
== PURPLE_REQUEST_FIELD_ACCOUNT
)
656 accountlist
= create_account_field(field
);
657 FINCH_SET_DATA(field
, accountlist
);
661 FINCH_SET_DATA(field
, gnt_label_new_with_format(_("Not implemented yet."),
662 GNT_TEXT_FLAG_BOLD
));
664 gnt_box_set_alignment(GNT_BOX(hbox
), GNT_ALIGN_MID
);
665 gnt_box_add_widget(GNT_BOX(hbox
), GNT_WIDGET(FINCH_GET_DATA(field
)));
668 gnt_box_add_widget(GNT_BOX(box
), gnt_hline_new());
670 gnt_box_add_widget(GNT_BOX(window
), box
);
672 box
= setup_button_box(window
, userdata
, request_fields_cb
, allfields
,
673 ok
, ok_cb
, cancel
, cancel_cb
, NULL
);
674 gnt_box_add_widget(GNT_BOX(window
), box
);
676 setup_default_callback(window
, cancel_cb
, userdata
);
677 gnt_widget_show(window
);
679 if (username
&& accountlist
) {
680 g_signal_connect(username
, "completion", G_CALLBACK(update_selected_account
), accountlist
);
683 g_object_set_data(G_OBJECT(window
), "fields", allfields
);
689 file_cancel_cb(gpointer fq
, GntWidget
*wid
)
691 FinchFileRequest
*data
= fq
;
692 if (data
->cbs
[1] != NULL
)
693 ((PurpleRequestFileCb
)data
->cbs
[1])(data
->user_data
, NULL
);
695 purple_request_close(PURPLE_REQUEST_FILE
, data
->dialog
);
699 file_ok_cb(gpointer fq
, GntWidget
*widget
)
701 FinchFileRequest
*data
= fq
;
702 char *file
= gnt_file_sel_get_selected_file(GNT_FILE_SEL(data
->dialog
));
703 char *dir
= g_path_get_dirname(file
);
704 if (data
->cbs
[0] != NULL
)
705 ((PurpleRequestFileCb
)data
->cbs
[0])(data
->user_data
, file
);
707 purple_prefs_set_path(data
->save
? "/finch/filelocations/last_save_folder" :
708 "/finch/filelocations/last_open_folder", dir
);
711 purple_request_close(PURPLE_REQUEST_FILE
, data
->dialog
);
715 file_request_destroy(FinchFileRequest
*data
)
721 static FinchFileRequest
*
722 finch_file_request_window(const char *title
, const char *path
,
723 GCallback ok_cb
, GCallback cancel_cb
,
726 GntWidget
*window
= gnt_file_sel_new();
727 GntFileSel
*sel
= GNT_FILE_SEL(window
);
728 FinchFileRequest
*data
= g_new0(FinchFileRequest
, 1);
730 data
->user_data
= user_data
;
731 data
->cbs
= g_new0(GCallback
, 2);
732 data
->cbs
[0] = ok_cb
;
733 data
->cbs
[1] = cancel_cb
;
734 data
->dialog
= window
;
735 gnt_box_set_title(GNT_BOX(window
), title
);
737 gnt_file_sel_set_current_location(sel
, (path
&& *path
) ? path
: purple_home_dir());
739 g_signal_connect(G_OBJECT(sel
->cancel
), "activate",
740 G_CALLBACK(action_performed
), window
);
741 g_signal_connect(G_OBJECT(sel
->select
), "activate",
742 G_CALLBACK(action_performed
), window
);
743 g_signal_connect_swapped(G_OBJECT(sel
->cancel
), "activate",
744 G_CALLBACK(file_cancel_cb
), data
);
745 g_signal_connect_swapped(G_OBJECT(sel
->select
), "activate",
746 G_CALLBACK(file_ok_cb
), data
);
748 setup_default_callback(window
, file_cancel_cb
, data
);
749 g_object_set_data_full(G_OBJECT(window
), "filerequestdata", data
,
750 (GDestroyNotify
)file_request_destroy
);
756 finch_request_file(const char *title
, const char *filename
,
758 GCallback ok_cb
, GCallback cancel_cb
,
759 PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
762 FinchFileRequest
*data
;
765 path
= purple_prefs_get_path(savedialog
? "/finch/filelocations/last_save_folder" : "/finch/filelocations/last_open_folder");
766 data
= finch_file_request_window(title
? title
: (savedialog
? _("Save File...") : _("Open File...")), path
,
767 ok_cb
, cancel_cb
, user_data
);
768 data
->save
= savedialog
;
770 gnt_file_sel_set_suggested_filename(GNT_FILE_SEL(data
->dialog
), filename
);
772 gnt_widget_show(data
->dialog
);
777 finch_request_folder(const char *title
, const char *dirname
, GCallback ok_cb
,
778 GCallback cancel_cb
, PurpleAccount
*account
, const char *who
, PurpleConversation
*conv
,
781 FinchFileRequest
*data
;
783 data
= finch_file_request_window(title
? title
: _("Choose Location..."), dirname
,
784 ok_cb
, cancel_cb
, user_data
);
786 gnt_file_sel_set_dirs_only(GNT_FILE_SEL(data
->dialog
), TRUE
);
788 gnt_widget_show(data
->dialog
);
792 static PurpleRequestUiOps uiops
=
795 finch_request_choice
,
796 finch_request_action
,
797 finch_request_fields
,
800 finch_request_folder
,
807 PurpleRequestUiOps
*finch_request_get_ui_ops()
812 void finch_request_init()
816 void finch_request_uninit()
820 void finch_request_save_in_prefs(gpointer null
, PurpleRequestFields
*allfields
)
823 for (list
= purple_request_fields_get_groups(allfields
); list
; list
= list
->next
) {
824 PurpleRequestFieldGroup
*group
= list
->data
;
825 GList
*fields
= purple_request_field_group_get_fields(group
);
827 for (; fields
; fields
= fields
->next
) {
828 PurpleRequestField
*field
= fields
->data
;
829 PurpleRequestFieldType type
= purple_request_field_get_type(field
);
832 const char *id
= purple_request_field_get_id(field
);
835 case PURPLE_REQUEST_FIELD_LIST
:
836 val
= purple_request_field_list_get_selected(field
)->data
;
837 val
= purple_request_field_list_get_data(field
, val
);
839 case PURPLE_REQUEST_FIELD_BOOLEAN
:
840 val
= GINT_TO_POINTER(purple_request_field_bool_get_value(field
));
842 case PURPLE_REQUEST_FIELD_INTEGER
:
843 val
= GINT_TO_POINTER(purple_request_field_int_get_value(field
));
845 case PURPLE_REQUEST_FIELD_STRING
:
846 val
= (gpointer
)purple_request_field_string_get_value(field
);
852 pt
= purple_prefs_get_type(id
);
854 case PURPLE_PREF_INT
:
856 long int tmp
= GPOINTER_TO_INT(val
);
857 if (type
== PURPLE_REQUEST_FIELD_LIST
) /* Lists always return string */
858 sscanf(val
, "%ld", &tmp
);
859 purple_prefs_set_int(id
, (gint
)tmp
);
862 case PURPLE_PREF_BOOLEAN
:
863 purple_prefs_set_bool(id
, GPOINTER_TO_INT(val
));
865 case PURPLE_PREF_STRING
:
866 purple_prefs_set_string(id
, val
);
875 GntWidget
*finch_request_field_get_widget(PurpleRequestField
*field
)
877 GntWidget
*ret
= NULL
;
878 switch (purple_request_field_get_type(field
)) {
879 case PURPLE_REQUEST_FIELD_BOOLEAN
:
880 ret
= create_boolean_field(field
);
882 case PURPLE_REQUEST_FIELD_STRING
:
883 ret
= create_string_field(field
, NULL
);
885 case PURPLE_REQUEST_FIELD_INTEGER
:
886 ret
= create_integer_field(field
);
888 case PURPLE_REQUEST_FIELD_CHOICE
:
889 ret
= create_choice_field(field
);
891 case PURPLE_REQUEST_FIELD_LIST
:
892 ret
= create_list_field(field
);
894 case PURPLE_REQUEST_FIELD_ACCOUNT
:
895 ret
= create_account_field(field
);
898 purple_debug_error("GntRequest", "Unimplemented request-field %d\n", purple_request_field_get_type(field
));