mark PurpleImageClass as private
[pidgin-git.git] / pidgin / gtkrequest.c
blob6b1d58ef3b6d92e73aad3bb186e791a2f76bf6ee
1 /* pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include <talkatu.h>
24 #include "internal.h"
25 #include "pidgin.h"
27 #include "debug.h"
28 #include "prefs.h"
29 #include "util.h"
31 #include "gtkrequest.h"
32 #include "gtkutils.h"
33 #include "pidginstock.h"
34 #include "gtkblist.h"
35 #include "gtkinternal.h"
36 #include "pidginaccountchooser.h"
38 #include <gdk/gdkkeysyms.h>
40 #include "gtk3compat.h"
42 typedef struct
44 PurpleRequestType type;
46 void *user_data;
47 GtkWidget *dialog;
49 GtkWidget *ok_button;
51 size_t cb_count;
52 GCallback *cbs;
54 union
56 struct
58 GtkProgressBar *progress_bar;
59 } wait;
61 struct
63 GtkWidget *entry;
65 gboolean multiline;
66 gchar *hint;
68 } input;
70 struct
72 PurpleRequestFields *fields;
74 } multifield;
76 struct
78 gboolean savedialog;
79 gchar *name;
81 } file;
83 } u;
85 } PidginRequestData;
87 static GHashTable *datasheet_stock = NULL;
89 static GtkWidget * create_account_field(PurpleRequestField *field);
91 static void
92 pidgin_widget_decorate_account(GtkWidget *cont, PurpleAccount *account)
94 GtkWidget *image;
95 GdkPixbuf *pixbuf;
97 if (!account)
98 return;
100 pixbuf = pidgin_create_protocol_icon(account, PIDGIN_PROTOCOL_ICON_SMALL);
101 image = gtk_image_new_from_pixbuf(pixbuf);
102 g_object_unref(G_OBJECT(pixbuf));
104 gtk_widget_set_tooltip_text(image, purple_account_get_username(account));
106 if (GTK_IS_DIALOG(cont)) {
107 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(cont))),
108 image, FALSE, TRUE, 0);
109 gtk_box_reorder_child(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(cont))),
110 image, 0);
111 } else if (GTK_IS_BOX(cont)) {
112 gtk_widget_set_halign(image, GTK_ALIGN_START);
113 gtk_widget_set_valign(image, GTK_ALIGN_START);
114 gtk_box_pack_end(GTK_BOX(cont), image, FALSE, TRUE, 0);
116 gtk_widget_show(image);
119 static void
120 generic_response_start(PidginRequestData *data)
122 g_return_if_fail(data != NULL);
124 /* Tell the user we're doing something. */
125 pidgin_set_cursor(GTK_WIDGET(data->dialog), GDK_WATCH);
127 g_object_set_data(G_OBJECT(data->dialog),
128 "pidgin-window-is-closing", GINT_TO_POINTER(TRUE));
129 gtk_widget_set_visible(GTK_WIDGET(data->dialog), FALSE);
132 static void
133 input_response_cb(GtkDialog *dialog, gint id, PidginRequestData *data)
135 const char *value;
136 char *multiline_value = NULL;
138 generic_response_start(data);
140 if (data->u.input.multiline || purple_strequal(data->u.input.hint, "html")) {
141 GtkTextBuffer *buffer =
142 gtk_text_view_get_buffer(GTK_TEXT_VIEW(data->u.input.entry));
144 if (purple_strequal(data->u.input.hint, "html")) {
145 multiline_value = talkatu_markup_get_html(buffer, NULL);
146 } else {
147 GtkTextIter start_iter, end_iter;
149 gtk_text_buffer_get_start_iter(buffer, &start_iter);
150 gtk_text_buffer_get_end_iter(buffer, &end_iter);
152 multiline_value = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
153 FALSE);
156 value = multiline_value;
158 else {
159 value = gtk_entry_get_text(GTK_ENTRY(data->u.input.entry));
162 if (id >= 0 && (gsize)id < data->cb_count && data->cbs[id] != NULL)
163 ((PurpleRequestInputCb)data->cbs[id])(data->user_data, value);
164 else if (data->cbs[1] != NULL)
165 ((PurpleRequestInputCb)data->cbs[1])(data->user_data, value);
167 if (data->u.input.multiline) {
168 g_free(multiline_value);
171 purple_request_close(PURPLE_REQUEST_INPUT, data);
174 static void
175 action_response_cb(GtkDialog *dialog, gint id, PidginRequestData *data)
177 generic_response_start(data);
179 if (id >= 0 && (gsize)id < data->cb_count && data->cbs[id] != NULL)
180 ((PurpleRequestActionCb)data->cbs[id])(data->user_data, id);
182 purple_request_close(PURPLE_REQUEST_INPUT, data);
186 static void
187 choice_response_cb(GtkDialog *dialog, gint id, PidginRequestData *data)
189 GtkWidget *radio = g_object_get_data(G_OBJECT(dialog), "radio");
190 GSList *group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio));
192 generic_response_start(data);
194 if (id >= 0 && (gsize)id < data->cb_count && data->cbs[id] != NULL)
195 while (group) {
196 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(group->data))) {
197 ((PurpleRequestChoiceCb)data->cbs[id])(data->user_data, g_object_get_data(G_OBJECT(group->data), "choice_value"));
198 break;
200 group = group->next;
202 purple_request_close(PURPLE_REQUEST_INPUT, data);
205 static gboolean
206 field_string_focus_out_cb(GtkWidget *entry, GdkEventFocus *event,
207 PurpleRequestField *field)
209 const char *value;
211 if (purple_request_field_string_is_multiline(field))
213 GtkTextBuffer *buffer;
214 GtkTextIter start_iter, end_iter;
216 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
218 gtk_text_buffer_get_start_iter(buffer, &start_iter);
219 gtk_text_buffer_get_end_iter(buffer, &end_iter);
221 value = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
223 else
224 value = gtk_entry_get_text(GTK_ENTRY(entry));
226 purple_request_field_string_set_value(field,
227 (*value == '\0' ? NULL : value));
229 return FALSE;
232 static void
233 field_bool_cb(GtkToggleButton *button, PurpleRequestField *field)
235 purple_request_field_bool_set_value(field,
236 gtk_toggle_button_get_active(button));
239 static void
240 field_choice_menu_cb(GtkComboBox *menu, PurpleRequestField *field)
242 int active = gtk_combo_box_get_active(menu);
243 gpointer *values = g_object_get_data(G_OBJECT(menu), "values");
245 g_return_if_fail(values != NULL);
246 g_return_if_fail(active >= 0);
248 purple_request_field_choice_set_value(field, values[active]);
251 static void
252 field_choice_option_cb(GtkRadioButton *button, PurpleRequestField *field)
254 int active;
255 gpointer *values = g_object_get_data(G_OBJECT(g_object_get_data(
256 G_OBJECT(button), "box")), "values");
258 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
259 return;
261 active = (g_slist_length(gtk_radio_button_get_group(button)) -
262 g_slist_index(gtk_radio_button_get_group(button), button)) - 1;
264 g_return_if_fail(values != NULL);
265 g_return_if_fail(active >= 0);
267 purple_request_field_choice_set_value(field, values[active]);
270 static void
271 field_account_cb(GObject *w, PurpleRequestField *field)
273 purple_request_field_account_set_value(
274 field, pidgin_account_chooser_get_selected(GTK_WIDGET(w)));
277 static void
278 multifield_ok_cb(GtkWidget *button, PidginRequestData *data)
280 generic_response_start(data);
282 if (!gtk_widget_has_focus(button))
283 gtk_widget_grab_focus(button);
285 if (data->cbs[0] != NULL)
286 ((PurpleRequestFieldsCb)data->cbs[0])(data->user_data,
287 data->u.multifield.fields);
289 purple_request_close(PURPLE_REQUEST_FIELDS, data);
292 static void
293 multifield_cancel_cb(GtkWidget *button, PidginRequestData *data)
295 generic_response_start(data);
297 if (data->cbs[1] != NULL)
298 ((PurpleRequestFieldsCb)data->cbs[1])(data->user_data,
299 data->u.multifield.fields);
301 purple_request_close(PURPLE_REQUEST_FIELDS, data);
304 static void
305 multifield_extra_cb(GtkWidget *button, PidginRequestData *data)
307 PurpleRequestFieldsCb cb;
309 generic_response_start(data);
311 cb = g_object_get_data(G_OBJECT(button), "extra-cb");
313 if (cb != NULL)
314 cb(data->user_data, data->u.multifield.fields);
316 purple_request_close(PURPLE_REQUEST_FIELDS, data);
319 static gboolean
320 destroy_multifield_cb(GtkWidget *dialog, GdkEvent *event,
321 PidginRequestData *data)
323 multifield_cancel_cb(NULL, data);
324 return FALSE;
327 static gchar *
328 pidgin_request_escape(PurpleRequestCommonParameters *cpar, const gchar *text)
330 if (text == NULL)
331 return NULL;
333 if (purple_request_cpar_is_html(cpar)) {
334 gboolean valid;
336 valid = pango_parse_markup(text, -1, 0, NULL, NULL, NULL, NULL);
338 if (valid)
339 return g_strdup(text);
340 else {
341 purple_debug_error("pidgin", "Passed label text is not "
342 "a valid markup. Falling back to plain text.");
346 return g_markup_escape_text(text, -1);
349 static GtkWidget *
350 pidgin_request_dialog_icon(PurpleRequestType dialog_type,
351 PurpleRequestCommonParameters *cpar)
353 GtkWidget *img = NULL;
354 PurpleRequestIconType icon_type;
355 gconstpointer icon_data;
356 gsize icon_size;
357 const gchar *icon_name = "dialog-question";
359 /* Dialog icon. */
360 icon_data = purple_request_cpar_get_custom_icon(cpar, &icon_size);
361 if (icon_data) {
362 GdkPixbuf *pixbuf;
364 pixbuf = pidgin_pixbuf_from_data(icon_data, icon_size);
365 if (pixbuf) {
366 /* scale the image if it is too large */
367 int width = gdk_pixbuf_get_width(pixbuf);
368 int height = gdk_pixbuf_get_height(pixbuf);
369 if (width > 128 || height > 128) {
370 int scaled_width = width > height ?
371 128 : (128 * width) / height;
372 int scaled_height = height > width ?
373 128 : (128 * height) / width;
374 GdkPixbuf *scaled;
376 purple_debug_info("pidgin", "dialog icon was "
377 "too large, scaling it down");
379 scaled = gdk_pixbuf_scale_simple(pixbuf,
380 scaled_width, scaled_height,
381 GDK_INTERP_BILINEAR);
382 if (scaled) {
383 g_object_unref(pixbuf);
384 pixbuf = scaled;
387 img = gtk_image_new_from_pixbuf(pixbuf);
388 g_object_unref(pixbuf);
389 } else {
390 purple_debug_info("pidgin",
391 "failed to parse dialog icon");
395 if (img)
396 return img;
398 icon_type = purple_request_cpar_get_icon(cpar);
399 switch (icon_type)
401 case PURPLE_REQUEST_ICON_DEFAULT:
402 icon_name = NULL;
403 break;
404 case PURPLE_REQUEST_ICON_REQUEST:
405 icon_name = "dialog-question";
406 break;
407 case PURPLE_REQUEST_ICON_DIALOG:
408 case PURPLE_REQUEST_ICON_INFO:
409 case PURPLE_REQUEST_ICON_WAIT: /* TODO: we need another icon */
410 icon_name = "dialog-information";
411 break;
412 case PURPLE_REQUEST_ICON_WARNING:
413 icon_name = "dialog-warning";
414 break;
415 case PURPLE_REQUEST_ICON_ERROR:
416 icon_name = "dialog-error";
417 break;
418 /* intentionally no default value */
421 if (icon_name == NULL) {
422 switch (dialog_type) {
423 case PURPLE_REQUEST_INPUT:
424 case PURPLE_REQUEST_CHOICE:
425 case PURPLE_REQUEST_ACTION:
426 case PURPLE_REQUEST_FIELDS:
427 case PURPLE_REQUEST_FILE:
428 case PURPLE_REQUEST_FOLDER:
429 icon_name = "dialog-question";
430 break;
431 case PURPLE_REQUEST_WAIT:
432 icon_name = "dialog-information";
433 break;
434 /* intentionally no default value */
438 img = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_DIALOG);
440 if (img || icon_type == PURPLE_REQUEST_ICON_REQUEST)
441 return img;
443 return gtk_image_new_from_icon_name("dialog-question",
444 GTK_ICON_SIZE_DIALOG);
447 static void
448 pidgin_request_help_clicked(GtkButton *button, gpointer _unused)
450 PurpleRequestHelpCb cb;
451 gpointer data;
453 cb = g_object_get_data(G_OBJECT(button), "pidgin-help-cb");
454 data = g_object_get_data(G_OBJECT(button), "pidgin-help-data");
456 g_return_if_fail(cb != NULL);
457 cb(data);
460 static void
461 pidgin_request_add_help(GtkDialog *dialog, PurpleRequestCommonParameters *cpar)
463 GtkWidget *button;
464 PurpleRequestHelpCb help_cb;
465 gpointer help_data;
467 help_cb = purple_request_cpar_get_help_cb(cpar, &help_data);
468 if (help_cb == NULL)
469 return;
471 button = gtk_dialog_add_button(dialog, _("_Help"), GTK_RESPONSE_HELP);
473 g_object_set_data(G_OBJECT(button), "pidgin-help-cb", help_cb);
474 g_object_set_data(G_OBJECT(button), "pidgin-help-data", help_data);
476 g_signal_connect(G_OBJECT(button), "clicked",
477 G_CALLBACK(pidgin_request_help_clicked), NULL);
480 static void *
481 pidgin_request_input(const char *title, const char *primary,
482 const char *secondary, const char *default_value,
483 gboolean multiline, gboolean masked, gchar *hint,
484 const char *ok_text, GCallback ok_cb,
485 const char *cancel_text, GCallback cancel_cb,
486 PurpleRequestCommonParameters *cpar,
487 void *user_data)
489 PidginRequestData *data;
490 GtkWidget *dialog;
491 GtkWidget *vbox;
492 GtkWidget *hbox;
493 GtkLabel *label;
494 GtkWidget *img;
495 char *label_text;
496 char *primary_esc, *secondary_esc;
498 data = g_new0(PidginRequestData, 1);
499 data->type = PURPLE_REQUEST_INPUT;
500 data->user_data = user_data;
502 data->cb_count = 2;
503 data->cbs = g_new0(GCallback, 2);
505 data->cbs[0] = ok_cb;
506 data->cbs[1] = cancel_cb;
508 /* Create the dialog. */
509 dialog = gtk_dialog_new_with_buttons(title ? title : PIDGIN_ALERT_TITLE,
510 NULL, 0,
511 cancel_text, 1,
512 ok_text, 0,
513 NULL);
514 data->dialog = dialog;
516 g_signal_connect(G_OBJECT(dialog), "response",
517 G_CALLBACK(input_response_cb), data);
519 /* Setup the dialog */
520 gtk_container_set_border_width(GTK_CONTAINER(dialog), PIDGIN_HIG_BORDER/2);
521 gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
522 PIDGIN_HIG_BORDER / 2);
523 if (!multiline)
524 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
525 gtk_dialog_set_default_response(GTK_DIALOG(dialog), 0);
526 gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
527 PIDGIN_HIG_BORDER);
529 /* Setup the main horizontal box */
530 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
531 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
532 hbox);
534 /* Dialog icon. */
535 img = pidgin_request_dialog_icon(PURPLE_REQUEST_INPUT, cpar);
536 gtk_widget_set_halign(img, GTK_ALIGN_START);
537 gtk_widget_set_valign(img, GTK_ALIGN_START);
538 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
540 pidgin_request_add_help(GTK_DIALOG(dialog), cpar);
542 /* Vertical box */
543 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
545 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
547 pidgin_widget_decorate_account(hbox, purple_request_cpar_get_account(cpar));
549 /* Descriptive label */
550 primary_esc = pidgin_request_escape(cpar, primary);
551 secondary_esc = pidgin_request_escape(cpar, secondary);
552 label_text = g_strdup_printf((primary ? "<span weight=\"bold\" size=\"larger\">"
553 "%s</span>%s%s" : "%s%s%s"),
554 (primary ? primary_esc : ""),
555 ((primary && secondary) ? "\n\n" : ""),
556 (secondary ? secondary_esc : ""));
557 g_free(primary_esc);
558 g_free(secondary_esc);
560 label = GTK_LABEL(gtk_label_new(NULL));
562 gtk_label_set_markup(label, label_text);
563 gtk_label_set_line_wrap(label, TRUE);
564 gtk_label_set_xalign(label, 0);
565 gtk_label_set_yalign(label, 0);
566 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(label), FALSE, FALSE, 0);
568 g_free(label_text);
570 /* Entry field. */
571 data->u.input.multiline = multiline;
572 data->u.input.hint = g_strdup(hint);
574 gtk_widget_show_all(hbox);
576 if(multiline || purple_strequal(data->u.input.hint, "html")) {
577 GtkWidget *editor = talkatu_editor_new();
578 GtkWidget *view = talkatu_editor_get_view(TALKATU_EDITOR(editor));
579 GtkTextBuffer *buffer = NULL;
581 gtk_widget_set_size_request(view, 320, 130);
582 gtk_widget_set_name(view, "pidgin_request_view");
583 gtk_box_pack_start(GTK_BOX(vbox), editor, TRUE, TRUE, 0);
584 gtk_widget_show(editor);
586 if (purple_strequal(data->u.input.hint, "html")) {
587 buffer = talkatu_html_buffer_new();
589 if(default_value != NULL) {
590 talkatu_markup_set_html(TALKATU_BUFFER(buffer), default_value, -1);
592 } else {
593 buffer = gtk_text_buffer_new(NULL);
595 if(default_value != NULL) {
596 gtk_text_buffer_set_text(buffer, default_value, -1);
600 gtk_text_view_set_buffer(GTK_TEXT_VIEW(view), buffer);
602 data->u.input.entry = view;
603 } else {
604 GtkWidget *entry = gtk_entry_new();
606 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
607 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
609 if(default_value != NULL) {
610 gtk_entry_set_text(GTK_ENTRY(entry), default_value);
613 if(masked) {
614 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
617 data->u.input.entry = entry;
620 gtk_widget_show_all(vbox);
622 pidgin_set_accessible_label(data->u.input.entry, label);
624 pidgin_auto_parent_window(dialog);
626 /* Show everything. */
627 gtk_widget_show(dialog);
629 return data;
632 static void *
633 pidgin_request_choice(const char *title, const char *primary,
634 const char *secondary, gpointer default_value, const char *ok_text,
635 GCallback ok_cb, const char *cancel_text, GCallback cancel_cb,
636 PurpleRequestCommonParameters *cpar, void *user_data, va_list args)
638 PidginRequestData *data;
639 GtkWidget *dialog;
640 GtkWidget *vbox, *vbox2;
641 GtkWidget *hbox;
642 GtkWidget *label;
643 GtkWidget *img;
644 GtkWidget *radio = NULL;
645 char *label_text;
646 char *radio_text;
647 char *primary_esc, *secondary_esc;
649 data = g_new0(PidginRequestData, 1);
650 data->type = PURPLE_REQUEST_ACTION;
651 data->user_data = user_data;
653 data->cb_count = 2;
654 data->cbs = g_new0(GCallback, 2);
655 data->cbs[0] = cancel_cb;
656 data->cbs[1] = ok_cb;
658 /* Create the dialog. */
659 data->dialog = dialog = gtk_dialog_new();
661 if (title != NULL)
662 gtk_window_set_title(GTK_WINDOW(dialog), title);
663 #ifdef _WIN32
664 gtk_window_set_title(GTK_WINDOW(dialog), PIDGIN_ALERT_TITLE);
665 #endif
667 gtk_dialog_add_button(GTK_DIALOG(dialog), cancel_text, 0);
668 gtk_dialog_add_button(GTK_DIALOG(dialog), ok_text, 1);
670 g_signal_connect(G_OBJECT(dialog), "response",
671 G_CALLBACK(choice_response_cb), data);
673 /* Setup the dialog */
674 gtk_container_set_border_width(GTK_CONTAINER(dialog), PIDGIN_HIG_BORDER/2);
675 gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
676 PIDGIN_HIG_BORDER / 2);
677 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
678 gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
679 PIDGIN_HIG_BORDER);
681 /* Setup the main horizontal box */
682 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
683 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
684 hbox);
686 /* Dialog icon. */
687 img = pidgin_request_dialog_icon(PURPLE_REQUEST_CHOICE, cpar);
688 gtk_widget_set_halign(img, GTK_ALIGN_START);
689 gtk_widget_set_valign(img, GTK_ALIGN_START);
690 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
692 pidgin_widget_decorate_account(hbox, purple_request_cpar_get_account(cpar));
694 pidgin_request_add_help(GTK_DIALOG(dialog), cpar);
696 /* Vertical box */
697 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
698 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
700 /* Descriptive label */
701 primary_esc = pidgin_request_escape(cpar, primary);
702 secondary_esc = pidgin_request_escape(cpar, secondary);
703 label_text = g_strdup_printf((primary ? "<span weight=\"bold\" size=\"larger\">"
704 "%s</span>%s%s" : "%s%s%s"),
705 (primary ? primary_esc : ""),
706 ((primary && secondary) ? "\n\n" : ""),
707 (secondary ? secondary_esc : ""));
708 g_free(primary_esc);
709 g_free(secondary_esc);
711 label = gtk_label_new(NULL);
713 gtk_label_set_markup(GTK_LABEL(label), label_text);
714 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
715 gtk_label_set_xalign(GTK_LABEL(label), 0);
716 gtk_label_set_yalign(GTK_LABEL(label), 0);
717 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
719 g_free(label_text);
721 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BOX_SPACE);
722 gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, FALSE, 0);
723 while ((radio_text = va_arg(args, char*))) {
724 gpointer resp = va_arg(args, gpointer);
725 radio = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio), radio_text);
726 gtk_box_pack_start(GTK_BOX(vbox2), radio, FALSE, FALSE, 0);
727 g_object_set_data(G_OBJECT(radio), "choice_value", resp);
728 if (resp == default_value)
729 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUE);
732 g_object_set_data(G_OBJECT(dialog), "radio", radio);
734 /* Show everything. */
735 pidgin_auto_parent_window(dialog);
737 gtk_widget_show_all(dialog);
739 return data;
742 static void *
743 pidgin_request_action(const char *title, const char *primary,
744 const char *secondary, int default_action,
745 PurpleRequestCommonParameters *cpar, void *user_data,
746 size_t action_count, va_list actions)
748 PidginRequestData *data;
749 GtkWidget *dialog;
750 GtkWidget *vbox;
751 GtkWidget *hbox;
752 GtkWidget *label;
753 GtkWidget *img = NULL;
754 void **buttons;
755 char *label_text;
756 char *primary_esc, *secondary_esc;
757 gsize i;
759 data = g_new0(PidginRequestData, 1);
760 data->type = PURPLE_REQUEST_ACTION;
761 data->user_data = user_data;
763 data->cb_count = action_count;
764 data->cbs = g_new0(GCallback, action_count);
766 /* Reverse the buttons */
767 buttons = g_new0(void *, action_count * 2);
769 for (i = 0; i < action_count * 2; i += 2) {
770 buttons[(action_count * 2) - i - 2] = va_arg(actions, char *);
771 buttons[(action_count * 2) - i - 1] = va_arg(actions, GCallback);
774 /* Create the dialog. */
775 data->dialog = dialog = gtk_dialog_new();
777 gtk_window_set_deletable(GTK_WINDOW(data->dialog), FALSE);
779 if (title != NULL)
780 gtk_window_set_title(GTK_WINDOW(dialog), title);
781 #ifdef _WIN32
782 else
783 gtk_window_set_title(GTK_WINDOW(dialog), PIDGIN_ALERT_TITLE);
784 #endif
786 for (i = 0; i < action_count; i++) {
787 gtk_dialog_add_button(GTK_DIALOG(dialog), buttons[2 * i], i);
789 data->cbs[i] = buttons[2 * i + 1];
792 g_free(buttons);
794 g_signal_connect(G_OBJECT(dialog), "response",
795 G_CALLBACK(action_response_cb), data);
797 /* Setup the dialog */
798 gtk_container_set_border_width(GTK_CONTAINER(dialog), PIDGIN_HIG_BORDER/2);
799 gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
800 PIDGIN_HIG_BORDER / 2);
801 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
802 gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
803 PIDGIN_HIG_BORDER);
805 /* Setup the main horizontal box */
806 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
807 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
808 hbox);
810 img = pidgin_request_dialog_icon(PURPLE_REQUEST_ACTION, cpar);
811 gtk_widget_set_halign(img, GTK_ALIGN_START);
812 gtk_widget_set_valign(img, GTK_ALIGN_START);
813 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
815 /* Vertical box */
816 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
817 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
819 pidgin_widget_decorate_account(hbox,
820 purple_request_cpar_get_account(cpar));
822 pidgin_request_add_help(GTK_DIALOG(dialog), cpar);
824 /* Descriptive label */
825 primary_esc = pidgin_request_escape(cpar, primary);
826 secondary_esc = pidgin_request_escape(cpar, secondary);
827 label_text = g_strdup_printf((primary ? "<span weight=\"bold\" size=\"larger\">"
828 "%s</span>%s%s" : "%s%s%s"),
829 (primary ? primary_esc : ""),
830 ((primary && secondary) ? "\n\n" : ""),
831 (secondary ? secondary_esc : ""));
832 g_free(primary_esc);
833 g_free(secondary_esc);
835 label = gtk_label_new(NULL);
837 gtk_label_set_markup(GTK_LABEL(label), label_text);
838 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
839 gtk_label_set_xalign(GTK_LABEL(label), 0);
840 gtk_label_set_yalign(GTK_LABEL(label), 0);
841 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
842 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
844 g_free(label_text);
847 if (default_action == PURPLE_DEFAULT_ACTION_NONE) {
848 gtk_widget_set_can_default(img, TRUE);
849 gtk_widget_set_can_focus(img, TRUE);
850 gtk_widget_grab_focus(img);
851 gtk_widget_grab_default(img);
852 } else
854 * Need to invert the default_action number because the
855 * buttons are added to the dialog in reverse order.
857 gtk_dialog_set_default_response(GTK_DIALOG(dialog), action_count - 1 - default_action);
859 /* Show everything. */
860 pidgin_auto_parent_window(dialog);
862 gtk_widget_show_all(dialog);
864 return data;
867 static void
868 wait_cancel_cb(GtkWidget *button, PidginRequestData *data)
870 generic_response_start(data);
872 if (data->cbs[0] != NULL)
873 ((PurpleRequestCancelCb)data->cbs[0])(data->user_data);
875 purple_request_close(PURPLE_REQUEST_FIELDS, data);
878 static void *
879 pidgin_request_wait(const char *title, const char *primary,
880 const char *secondary, gboolean with_progress,
881 PurpleRequestCancelCb cancel_cb, PurpleRequestCommonParameters *cpar,
882 void *user_data)
884 PidginRequestData *data;
885 GtkWidget *dialog;
886 GtkWidget *hbox, *vbox, *img, *label, *button;
887 gchar *primary_esc, *secondary_esc, *label_text;
889 data = g_new0(PidginRequestData, 1);
890 data->type = PURPLE_REQUEST_WAIT;
891 data->user_data = user_data;
893 data->cb_count = 1;
894 data->cbs = g_new0(GCallback, 1);
895 data->cbs[0] = (GCallback)cancel_cb;
897 data->dialog = dialog = gtk_dialog_new();
899 gtk_window_set_deletable(GTK_WINDOW(data->dialog), cancel_cb != NULL);
901 if (title != NULL)
902 gtk_window_set_title(GTK_WINDOW(dialog), title);
903 else
904 gtk_window_set_title(GTK_WINDOW(dialog), _("Please wait"));
906 /* Setup the dialog */
907 gtk_container_set_border_width(GTK_CONTAINER(dialog),
908 PIDGIN_HIG_BORDER / 2);
909 gtk_container_set_border_width(GTK_CONTAINER(
910 gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
911 PIDGIN_HIG_BORDER / 2);
912 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
913 gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(
914 GTK_DIALOG(dialog))), PIDGIN_HIG_BORDER);
916 /* Setup the main horizontal box */
917 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
918 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(
919 GTK_DIALOG(dialog))), hbox);
921 img = pidgin_request_dialog_icon(PURPLE_REQUEST_WAIT, cpar);
922 gtk_widget_set_halign(img, GTK_ALIGN_START);
923 gtk_widget_set_valign(img, GTK_ALIGN_START);
924 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
926 /* Cancel button */
927 button = pidgin_dialog_add_button(GTK_DIALOG(dialog), _("Cancel"),
928 G_CALLBACK(wait_cancel_cb), data);
929 gtk_widget_set_can_default(button, FALSE);
931 /* Vertical box */
932 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
933 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
935 pidgin_widget_decorate_account(hbox,
936 purple_request_cpar_get_account(cpar));
938 pidgin_request_add_help(GTK_DIALOG(dialog), cpar);
940 /* Descriptive label */
941 primary_esc = pidgin_request_escape(cpar, primary);
942 secondary_esc = pidgin_request_escape(cpar, secondary);
943 label_text = g_strdup_printf((primary ? "<span weight=\"bold\" "
944 "size=\"larger\">%s</span>%s%s" : "%s%s%s"),
945 (primary ? primary_esc : ""),
946 ((primary && secondary) ? "\n\n" : ""),
947 (secondary ? secondary_esc : ""));
948 g_free(primary_esc);
949 g_free(secondary_esc);
951 label = gtk_label_new(NULL);
953 gtk_label_set_markup(GTK_LABEL(label), label_text);
954 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
955 gtk_label_set_xalign(GTK_LABEL(label), 0);
956 gtk_label_set_yalign(GTK_LABEL(label), 0);
957 gtk_label_set_selectable(GTK_LABEL(label), FALSE);
958 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
960 g_free(label_text);
962 if (with_progress) {
963 GtkProgressBar *bar;
965 bar = data->u.wait.progress_bar =
966 GTK_PROGRESS_BAR(gtk_progress_bar_new());
967 gtk_progress_bar_set_fraction(bar, 0);
968 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(bar),
969 FALSE, FALSE, 0);
972 /* Move focus out of cancel button. */
973 gtk_widget_set_can_default(img, TRUE);
974 gtk_widget_set_can_focus(img, TRUE);
975 gtk_widget_grab_focus(img);
976 gtk_widget_grab_default(img);
978 /* Show everything. */
979 pidgin_auto_parent_window(dialog);
981 gtk_widget_show_all(dialog);
983 return data;
986 static void
987 pidgin_request_wait_update(void *ui_handle, gboolean pulse, gfloat fraction)
989 GtkProgressBar *bar;
990 PidginRequestData *data = ui_handle;
992 g_return_if_fail(data->type == PURPLE_REQUEST_WAIT);
994 bar = data->u.wait.progress_bar;
995 if (pulse)
996 gtk_progress_bar_pulse(bar);
997 else
998 gtk_progress_bar_set_fraction(bar, fraction);
1001 static void
1002 req_entry_field_changed_cb(GtkWidget *entry, PurpleRequestField *field)
1004 if (purple_request_field_get_field_type(field) == PURPLE_REQUEST_FIELD_INTEGER) {
1005 int value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(entry));
1006 purple_request_field_int_set_value(field, value);
1007 return;
1010 if (purple_request_field_string_is_multiline(field))
1012 char *text;
1013 GtkTextIter start_iter, end_iter;
1015 gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(entry), &start_iter);
1016 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(entry), &end_iter);
1018 text = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(entry), &start_iter, &end_iter, FALSE);
1019 purple_request_field_string_set_value(field, (!text || !*text) ? NULL : text);
1020 g_free(text);
1022 else
1024 const char *text = NULL;
1025 text = gtk_entry_get_text(GTK_ENTRY(entry));
1026 purple_request_field_string_set_value(field, (*text == '\0') ? NULL : text);
1030 static void
1031 req_field_changed_cb(GtkWidget *widget, PurpleRequestField *field)
1033 PurpleRequestFieldGroup *group;
1034 PurpleRequestFields *fields;
1035 PidginRequestData *req_data;
1036 const GList *it;
1038 group = purple_request_field_get_group(field);
1039 fields = purple_request_field_group_get_fields_list(group);
1040 req_data = purple_request_fields_get_ui_data(fields);
1042 gtk_widget_set_sensitive(req_data->ok_button,
1043 purple_request_fields_all_required_filled(fields) &&
1044 purple_request_fields_all_valid(fields));
1046 it = purple_request_fields_get_autosensitive(fields);
1047 for (; it != NULL; it = g_list_next(it)) {
1048 PurpleRequestField *field = it->data;
1049 GtkWidget *widget = purple_request_field_get_ui_data(field);
1050 gboolean sensitive;
1052 sensitive = purple_request_field_is_sensitive(field);
1053 gtk_widget_set_sensitive(widget, sensitive);
1055 /* XXX: and what about multiline? */
1056 if (GTK_IS_EDITABLE(widget))
1057 gtk_editable_set_editable(GTK_EDITABLE(widget), sensitive);
1061 static void
1062 setup_entry_field(GtkWidget *entry, PurpleRequestField *field)
1064 const char *type_hint;
1066 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
1068 g_signal_connect(G_OBJECT(entry), "changed",
1069 G_CALLBACK(req_entry_field_changed_cb), field);
1070 g_signal_connect(G_OBJECT(entry), "changed",
1071 G_CALLBACK(req_field_changed_cb), field);
1073 if ((type_hint = purple_request_field_get_field_type_hint(field)) != NULL)
1075 if (purple_str_has_prefix(type_hint, "screenname"))
1077 GtkWidget *optmenu = NULL;
1078 PurpleRequestFieldGroup *group = purple_request_field_get_group(field);
1079 GList *fields = purple_request_field_group_get_fields(group);
1081 /* Ensure the account option menu is created (if the widget hasn't
1082 * been initialized already) for username auto-completion. */
1083 while (fields)
1085 PurpleRequestField *fld = fields->data;
1086 fields = fields->next;
1088 if (purple_request_field_get_field_type(fld) == PURPLE_REQUEST_FIELD_ACCOUNT &&
1089 purple_request_field_is_visible(fld))
1091 const char *type_hint = purple_request_field_get_field_type_hint(fld);
1092 if (purple_strequal(type_hint, "account"))
1094 optmenu = GTK_WIDGET(purple_request_field_get_ui_data(fld));
1095 if (optmenu == NULL) {
1096 optmenu = GTK_WIDGET(create_account_field(fld));
1097 purple_request_field_set_ui_data(fld, optmenu);
1099 break;
1103 pidgin_setup_screenname_autocomplete(entry, optmenu, pidgin_screenname_autocomplete_default_filter, GINT_TO_POINTER(purple_strequal(type_hint, "screenname-all")));
1108 static GtkWidget *
1109 create_string_field(PurpleRequestField *field)
1111 const char *value;
1112 GtkWidget *widget;
1113 gboolean is_editable;
1115 value = purple_request_field_string_get_default_value(field);
1116 is_editable = purple_request_field_is_sensitive(field);
1118 if (purple_request_field_string_is_multiline(field))
1120 GtkWidget *textview;
1122 textview = gtk_text_view_new();
1123 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview),
1124 TRUE);
1125 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview),
1126 GTK_WRAP_WORD_CHAR);
1128 gtk_widget_show(textview);
1130 if (value != NULL)
1132 GtkTextBuffer *buffer;
1134 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
1136 gtk_text_buffer_set_text(buffer, value, -1);
1139 gtk_widget_set_tooltip_text(textview, purple_request_field_get_tooltip(field));
1141 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), is_editable);
1143 g_signal_connect(G_OBJECT(textview), "focus-out-event",
1144 G_CALLBACK(field_string_focus_out_cb), field);
1146 if (purple_request_field_is_required(field))
1148 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
1149 g_signal_connect(G_OBJECT(buffer), "changed",
1150 G_CALLBACK(req_entry_field_changed_cb), field);
1153 widget = pidgin_make_scrollable(textview, GTK_POLICY_NEVER, GTK_POLICY_ALWAYS, GTK_SHADOW_IN, -1, 75);
1155 else
1157 widget = gtk_entry_new();
1159 setup_entry_field(widget, field);
1161 if (value != NULL)
1162 gtk_entry_set_text(GTK_ENTRY(widget), value);
1164 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1166 if (purple_request_field_string_is_masked(field))
1168 gtk_entry_set_visibility(GTK_ENTRY(widget), FALSE);
1171 gtk_editable_set_editable(GTK_EDITABLE(widget), is_editable);
1173 g_signal_connect(G_OBJECT(widget), "focus-out-event",
1174 G_CALLBACK(field_string_focus_out_cb), field);
1177 return widget;
1180 static GtkWidget *
1181 create_int_field(PurpleRequestField *field)
1183 int value;
1184 GtkWidget *widget;
1186 widget = gtk_spin_button_new_with_range(
1187 purple_request_field_int_get_lower_bound(field),
1188 purple_request_field_int_get_upper_bound(field), 1);
1190 setup_entry_field(widget, field);
1192 value = purple_request_field_int_get_default_value(field);
1193 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), value);
1195 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1197 return widget;
1200 static GtkWidget *
1201 create_bool_field(PurpleRequestField *field,
1202 PurpleRequestCommonParameters *cpar)
1204 GtkWidget *widget;
1205 gchar *label;
1207 label = pidgin_request_escape(cpar,
1208 purple_request_field_get_label(field));
1209 widget = gtk_check_button_new_with_label(label);
1210 g_free(label);
1212 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1214 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1215 purple_request_field_bool_get_default_value(field));
1217 g_signal_connect(G_OBJECT(widget), "toggled",
1218 G_CALLBACK(field_bool_cb), field);
1219 g_signal_connect(widget, "toggled",
1220 G_CALLBACK(req_field_changed_cb), field);
1222 return widget;
1225 static GtkWidget *
1226 create_choice_field(PurpleRequestField *field,
1227 PurpleRequestCommonParameters *cpar)
1229 GtkWidget *widget;
1230 GList *elements = purple_request_field_choice_get_elements(field);
1231 int num_labels = g_list_length(elements) / 2;
1232 GList *l;
1233 gpointer *values = g_new(gpointer, num_labels);
1234 gpointer default_value;
1235 gboolean default_found = FALSE;
1236 int i;
1238 default_value = purple_request_field_choice_get_value(field);
1239 if (num_labels > 5 || purple_request_cpar_is_compact(cpar))
1241 int default_index = 0;
1242 widget = gtk_combo_box_text_new();
1244 i = 0;
1245 l = elements;
1246 while (l != NULL)
1248 const char *text;
1249 gpointer *value;
1251 text = l->data;
1252 l = g_list_next(l);
1253 value = l->data;
1254 l = g_list_next(l);
1256 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), text);
1257 if (value == default_value) {
1258 default_index = i;
1259 default_found = TRUE;
1261 values[i++] = value;
1264 gtk_combo_box_set_active(GTK_COMBO_BOX(widget), default_index);
1266 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1268 g_signal_connect(G_OBJECT(widget), "changed",
1269 G_CALLBACK(field_choice_menu_cb), field);
1271 else
1273 GtkWidget *box;
1274 GtkWidget *first_radio = NULL;
1275 GtkWidget *radio;
1277 if (num_labels == 2)
1278 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BOX_SPACE);
1279 else
1280 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1282 widget = box;
1284 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1286 i = 0;
1287 l = elements;
1288 while (l != NULL)
1290 const char *text;
1291 gpointer *value;
1293 text = l->data;
1294 l = g_list_next(l);
1295 value = l->data;
1296 l = g_list_next(l);
1298 radio = gtk_radio_button_new_with_label_from_widget(
1299 GTK_RADIO_BUTTON(first_radio), text);
1300 g_object_set_data(G_OBJECT(radio), "box", box);
1302 if (first_radio == NULL)
1303 first_radio = radio;
1305 if (value == default_value) {
1306 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUE);
1307 default_found = TRUE;
1309 values[i++] = value;
1311 gtk_box_pack_start(GTK_BOX(box), radio, TRUE, TRUE, 0);
1312 gtk_widget_show(radio);
1314 g_signal_connect(G_OBJECT(radio), "toggled",
1315 G_CALLBACK(field_choice_option_cb), field);
1319 if (!default_found && i > 0)
1320 purple_request_field_choice_set_value(field, values[0]);
1322 g_object_set_data_full(G_OBJECT(widget), "values", values, g_free);
1324 return widget;
1327 static GtkWidget *
1328 create_image_field(PurpleRequestField *field)
1330 GtkWidget *widget;
1331 GdkPixbuf *buf, *scale;
1333 buf = pidgin_pixbuf_from_data(
1334 (const guchar *)purple_request_field_image_get_buffer(field),
1335 purple_request_field_image_get_size(field));
1337 scale = gdk_pixbuf_scale_simple(buf,
1338 purple_request_field_image_get_scale_x(field) * gdk_pixbuf_get_width(buf),
1339 purple_request_field_image_get_scale_y(field) * gdk_pixbuf_get_height(buf),
1340 GDK_INTERP_BILINEAR);
1341 widget = gtk_image_new_from_pixbuf(scale);
1342 g_object_unref(G_OBJECT(buf));
1343 g_object_unref(G_OBJECT(scale));
1345 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1347 return widget;
1350 static GtkWidget *
1351 create_account_field(PurpleRequestField *field)
1353 GtkWidget *widget;
1355 widget = pidgin_account_chooser_new(
1356 purple_request_field_account_get_default_value(field),
1357 purple_request_field_account_get_show_all(field));
1358 pidgin_account_chooser_set_filter_func(
1359 PIDGIN_ACCOUNT_CHOOSER(widget),
1360 purple_request_field_account_get_filter(field));
1361 g_signal_connect(widget, "changed", G_CALLBACK(field_account_cb),
1362 field);
1364 gtk_widget_set_tooltip_text(widget, purple_request_field_get_tooltip(field));
1365 g_signal_connect(widget, "changed",
1366 G_CALLBACK(req_field_changed_cb), field);
1368 return widget;
1371 static void
1372 select_field_list_item(GtkTreeModel *model, GtkTreePath *path,
1373 GtkTreeIter *iter, gpointer data)
1375 PurpleRequestField *field = (PurpleRequestField *)data;
1376 char *text;
1378 gtk_tree_model_get(model, iter, 1, &text, -1);
1380 purple_request_field_list_add_selected(field, text);
1381 g_free(text);
1384 static void
1385 list_field_select_changed_cb(GtkTreeSelection *sel, PurpleRequestField *field)
1387 purple_request_field_list_clear_selected(field);
1389 gtk_tree_selection_selected_foreach(sel, select_field_list_item, field);
1392 static GtkWidget *
1393 create_list_field(PurpleRequestField *field)
1395 GtkWidget *treeview;
1396 GtkListStore *store;
1397 GtkCellRenderer *renderer;
1398 GtkTreeSelection *sel;
1399 GtkTreeViewColumn *column;
1400 GtkTreeIter iter;
1401 GList *l;
1402 GList *icons = NULL;
1404 icons = purple_request_field_list_get_icons(field);
1407 /* Create the list store */
1408 if (icons)
1409 store = gtk_list_store_new(3, G_TYPE_POINTER, G_TYPE_STRING, GDK_TYPE_PIXBUF);
1410 else
1411 store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_STRING);
1413 /* Create the tree view */
1414 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1415 g_object_unref(G_OBJECT(store));
1416 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
1418 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
1420 if (purple_request_field_list_get_multi_select(field))
1421 gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
1423 column = gtk_tree_view_column_new();
1424 gtk_tree_view_insert_column(GTK_TREE_VIEW(treeview), column, -1);
1426 renderer = gtk_cell_renderer_text_new();
1427 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1428 gtk_tree_view_column_add_attribute(column, renderer, "text", 1);
1430 if (icons)
1432 renderer = gtk_cell_renderer_pixbuf_new();
1433 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1434 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", 2);
1436 gtk_widget_set_size_request(treeview, 200, 400);
1439 for (l = purple_request_field_list_get_items(field); l != NULL; l = l->next)
1441 const char *text = (const char *)l->data;
1443 gtk_list_store_append(store, &iter);
1445 if (icons)
1447 const char *icon_path = (const char *)icons->data;
1448 GdkPixbuf* pixbuf = NULL;
1450 if (icon_path)
1451 pixbuf = pidgin_pixbuf_new_from_file(icon_path);
1453 gtk_list_store_set(store, &iter,
1454 0, purple_request_field_list_get_data(field, text),
1455 1, text,
1456 2, pixbuf,
1457 -1);
1458 icons = icons->next;
1460 else
1461 gtk_list_store_set(store, &iter,
1462 0, purple_request_field_list_get_data(field, text),
1463 1, text,
1464 -1);
1466 if (purple_request_field_list_is_selected(field, text))
1467 gtk_tree_selection_select_iter(sel, &iter);
1471 * We only want to catch changes made by the user, so it's important
1472 * that we wait until after the list is created to connect this
1473 * handler. If we connect the handler before the loop above and
1474 * there are multiple items selected, then selecting the first iter
1475 * in the tree causes list_field_select_changed_cb to be triggered
1476 * which clears out the rest of the list of selected items.
1478 g_signal_connect(G_OBJECT(sel), "changed",
1479 G_CALLBACK(list_field_select_changed_cb), field);
1481 gtk_widget_show(treeview);
1483 return pidgin_make_scrollable(treeview, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_IN, -1, -1);
1486 static GdkPixbuf*
1487 _pidgin_datasheet_stock_icon_get(const gchar *stock_name)
1489 GdkPixbuf *image = NULL;
1490 gchar *domain, *id;
1492 if (stock_name == NULL)
1493 return NULL;
1495 /* core is quitting */
1496 if (datasheet_stock == NULL)
1497 return NULL;
1499 if (g_hash_table_lookup_extended(datasheet_stock, stock_name,
1500 NULL, (gpointer*)&image))
1502 return image;
1505 domain = g_strdup(stock_name);
1506 id = strchr(domain, '/');
1507 if (!id) {
1508 g_free(domain);
1509 return NULL;
1511 id[0] = '\0';
1512 id++;
1514 if (purple_strequal(domain, "protocol")) {
1515 PurpleAccount *account;
1516 gchar *protocol_id, *accountname;
1518 protocol_id = id;
1519 accountname = strchr(id, ':');
1521 if (!accountname) {
1522 g_free(domain);
1523 return NULL;
1526 accountname[0] = '\0';
1527 accountname++;
1529 account = purple_accounts_find(accountname, protocol_id);
1530 if (account) {
1531 image = pidgin_create_protocol_icon(account,
1532 PIDGIN_PROTOCOL_ICON_SMALL);
1534 } else if (purple_strequal(domain, "e2ee")) {
1535 image = pidgin_pixbuf_from_image(
1536 _pidgin_e2ee_stock_icon_get(id));
1537 } else {
1538 purple_debug_error("gtkrequest", "Unknown domain: %s", domain);
1539 g_free(domain);
1540 return NULL;
1543 g_hash_table_insert(datasheet_stock, g_strdup(stock_name), image);
1544 return image;
1547 static PurpleRequestDatasheetRecord*
1548 datasheet_get_selected_row(GtkWidget *sheet_widget)
1550 PurpleRequestDatasheet *sheet;
1551 GtkTreeView *view;
1552 GtkTreeSelection *selection;
1553 GtkTreeModel *model;
1554 GtkTreeIter iter;
1555 GList *sel_list;
1556 gpointer key;
1558 g_return_val_if_fail(sheet_widget != NULL, NULL);
1560 view = GTK_TREE_VIEW(g_object_get_data(G_OBJECT(sheet_widget), "view"));
1561 sheet = g_object_get_data(G_OBJECT(sheet_widget), "sheet");
1563 g_return_val_if_fail(view != NULL, NULL);
1564 g_return_val_if_fail(sheet != NULL, NULL);
1566 selection = gtk_tree_view_get_selection(view);
1567 if (gtk_tree_selection_count_selected_rows(selection) != 1)
1568 return NULL;
1570 sel_list = gtk_tree_selection_get_selected_rows(selection, &model);
1571 gtk_tree_model_get_iter(model, &iter, sel_list->data);
1572 g_list_foreach(sel_list, (GFunc)gtk_tree_path_free, NULL);
1573 g_list_free(sel_list);
1575 gtk_tree_model_get(model, &iter, 0, &key, -1);
1577 return purple_request_datasheet_record_find(sheet, key);
1580 static void
1581 datasheet_button_check_sens(GtkWidget *button, gpointer _sheet_widget)
1583 PurpleRequestDatasheetAction *act;
1584 GtkWidget *sheet_widget = GTK_WIDGET(_sheet_widget);
1586 g_return_if_fail(sheet_widget != NULL);
1588 act = g_object_get_data(G_OBJECT(button), "action");
1590 g_return_if_fail(act != NULL);
1592 gtk_widget_set_sensitive(button,
1593 purple_request_datasheet_action_is_sensitive(act,
1594 datasheet_get_selected_row(sheet_widget)));
1597 static void
1598 datasheet_selection_changed(GtkWidget *sheet_widget)
1600 gpointer buttons_box;
1602 g_return_if_fail(sheet_widget != NULL);
1604 buttons_box = g_object_get_data(G_OBJECT(sheet_widget), "buttons");
1605 gtk_container_foreach(GTK_CONTAINER(buttons_box),
1606 datasheet_button_check_sens, sheet_widget);
1609 static void
1610 datasheet_update_rec(PurpleRequestDatasheetRecord *rec, GtkListStore *model,
1611 GtkTreeIter *iter)
1613 guint i, col_count;
1614 PurpleRequestDatasheet *sheet;
1616 g_return_if_fail(rec != NULL);
1617 g_return_if_fail(model != NULL);
1618 g_return_if_fail(iter != NULL);
1620 sheet = purple_request_datasheet_record_get_datasheet(rec);
1622 g_return_if_fail(sheet != NULL);
1624 col_count = purple_request_datasheet_get_column_count(sheet);
1626 for (i = 0; i < col_count; i++) {
1627 PurpleRequestDatasheetColumnType type;
1629 type = purple_request_datasheet_get_column_type(
1630 sheet, i);
1631 if (type == PURPLE_REQUEST_DATASHEET_COLUMN_STRING) {
1632 GValue val;
1634 val.g_type = 0;
1635 g_value_init(&val, G_TYPE_STRING);
1636 g_value_set_string(&val,
1637 purple_request_datasheet_record_get_string_data(
1638 rec, i));
1639 gtk_list_store_set_value(model, iter,
1640 i + 1, &val);
1641 } else if (type ==
1642 PURPLE_REQUEST_DATASHEET_COLUMN_IMAGE)
1644 GdkPixbuf *pixbuf;
1646 pixbuf = _pidgin_datasheet_stock_icon_get(
1647 purple_request_datasheet_record_get_image_data(
1648 rec, i));
1649 gtk_list_store_set(model, iter, i + 1,
1650 pixbuf, -1);
1651 } else
1652 g_warn_if_reached();
1656 static void
1657 datasheet_fill(PurpleRequestDatasheet *sheet, GtkListStore *model)
1659 const GList *it;
1661 gtk_list_store_clear(model);
1663 it = purple_request_datasheet_get_records(sheet);
1664 for (; it != NULL; it = g_list_next(it)) {
1665 PurpleRequestDatasheetRecord *rec = it->data;
1666 GtkTreeIter iter;
1668 gtk_list_store_append(model, &iter);
1669 gtk_list_store_set(model, &iter, 0,
1670 purple_request_datasheet_record_get_key(rec), -1);
1672 datasheet_update_rec(rec, model, &iter);
1675 datasheet_selection_changed(GTK_WIDGET(g_object_get_data(
1676 G_OBJECT(model), "sheet-widget")));
1679 static void
1680 datasheet_update(PurpleRequestDatasheet *sheet, gpointer key,
1681 GtkListStore *model)
1683 PurpleRequestDatasheetRecord *rec;
1684 GtkTreeIter iter;
1685 GtkTreeModel *tmodel = GTK_TREE_MODEL(model);
1686 gboolean found = FALSE;
1688 g_return_if_fail(tmodel != NULL);
1690 if (key == NULL) {
1691 datasheet_fill(sheet, model);
1692 return;
1695 rec = purple_request_datasheet_record_find(sheet, key);
1697 if (gtk_tree_model_get_iter_first(tmodel, &iter)) {
1698 do {
1699 gpointer ikey;
1701 gtk_tree_model_get(tmodel, &iter, 0, &ikey, -1);
1703 if (key == ikey) {
1704 found = TRUE;
1705 break;
1707 } while (gtk_tree_model_iter_next(tmodel, &iter));
1710 if (rec == NULL && !found)
1711 return;
1713 if (rec == NULL) {
1714 gtk_list_store_remove(model, &iter);
1715 return;
1718 if (!found) {
1719 gtk_list_store_append(model, &iter);
1720 gtk_list_store_set(model, &iter, 0, key, -1);
1723 datasheet_update_rec(rec, model, &iter);
1725 datasheet_selection_changed(GTK_WIDGET(g_object_get_data(
1726 G_OBJECT(model), "sheet-widget")));
1730 static void
1731 datasheet_selection_changed_cb(GtkTreeSelection *sel, gpointer sheet_widget)
1733 datasheet_selection_changed(GTK_WIDGET(sheet_widget));
1736 static void
1737 datasheet_action_clicked(GtkButton *btn, PurpleRequestDatasheetAction *act)
1739 GtkWidget *sheet_widget;
1741 sheet_widget = g_object_get_data(G_OBJECT(btn), "sheet-widget");
1743 g_return_if_fail(sheet_widget != NULL);
1745 purple_request_datasheet_action_call(act, datasheet_get_selected_row(
1746 sheet_widget));
1749 static GtkWidget *
1750 create_datasheet_field(PurpleRequestField *field, GtkSizeGroup *buttons_sg)
1752 PurpleRequestDatasheet *sheet;
1753 guint i, col_count;
1754 GType *col_types;
1755 GtkListStore *model;
1756 GtkTreeView *view;
1757 GtkTreeSelection *sel;
1758 GtkWidget *scrollable;
1759 GtkCellRenderer *renderer_image = NULL, *renderer_text = NULL;
1760 GtkTreeViewColumn *id_column;
1761 GtkWidget *main_box;
1762 GtkWidget *buttons_box;
1763 const GList *it;
1765 sheet = purple_request_field_datasheet_get_sheet(field);
1766 main_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1768 col_count = purple_request_datasheet_get_column_count(sheet);
1770 col_types = g_new0(GType, col_count + 1);
1771 col_types[0] = G_TYPE_POINTER;
1772 for (i = 0; i < col_count; i++) {
1773 PurpleRequestDatasheetColumnType type;
1774 type = purple_request_datasheet_get_column_type(sheet, i);
1775 if (type == PURPLE_REQUEST_DATASHEET_COLUMN_STRING)
1776 col_types[i + 1] = G_TYPE_STRING;
1777 else if (type == PURPLE_REQUEST_DATASHEET_COLUMN_IMAGE)
1778 col_types[i + 1] = GDK_TYPE_PIXBUF;
1779 else
1780 g_warn_if_reached();
1782 model = gtk_list_store_newv(col_count + 1, col_types);
1783 g_free(col_types);
1785 view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(
1786 GTK_TREE_MODEL(model)));
1787 g_object_set_data(G_OBJECT(model), "sheet-widget", main_box);
1788 g_object_unref(G_OBJECT(model));
1790 id_column = gtk_tree_view_column_new();
1791 gtk_tree_view_column_set_visible(id_column, FALSE);
1792 gtk_tree_view_append_column(view, id_column);
1794 for (i = 0; i < col_count; i++) {
1795 PurpleRequestDatasheetColumnType type;
1796 const gchar *title;
1797 GtkCellRenderer *renderer = NULL;
1798 const gchar *type_str = "";
1800 type = purple_request_datasheet_get_column_type(sheet, i);
1801 title = purple_request_datasheet_get_column_title(sheet, i);
1803 if (type == PURPLE_REQUEST_DATASHEET_COLUMN_STRING) {
1804 type_str = "text";
1805 if (!renderer_text)
1806 renderer_text = gtk_cell_renderer_text_new();
1807 renderer = renderer_text;
1809 else if (type == PURPLE_REQUEST_DATASHEET_COLUMN_IMAGE) {
1810 type_str = "pixbuf";
1811 if (!renderer_image)
1812 renderer_image = gtk_cell_renderer_pixbuf_new();
1813 renderer = renderer_image;
1814 } else
1815 g_warn_if_reached();
1817 if (title == NULL)
1818 title = "";
1819 gtk_tree_view_insert_column_with_attributes(
1820 view, -1, title, renderer, type_str,
1821 i + 1, NULL);
1824 gtk_widget_set_size_request(GTK_WIDGET(view), 400, 250);
1826 scrollable = pidgin_make_scrollable(GTK_WIDGET(view),
1827 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS, GTK_SHADOW_IN, -1, -1);
1828 gtk_widget_show(GTK_WIDGET(view));
1830 buttons_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
1831 gtk_size_group_add_widget(buttons_sg, buttons_box);
1833 gtk_box_pack_start(GTK_BOX(main_box), scrollable, TRUE, TRUE, 0);
1834 gtk_box_pack_start(GTK_BOX(main_box), buttons_box,
1835 FALSE, FALSE, 0);
1836 gtk_widget_show(scrollable);
1837 gtk_widget_show(buttons_box);
1839 it = purple_request_datasheet_get_actions(sheet);
1840 for (; it != NULL; it = g_list_next(it)) {
1841 PurpleRequestDatasheetAction *act = it->data;
1842 GtkButton *btn;
1843 const gchar *label;
1845 label = purple_request_datasheet_action_get_label(act);
1847 btn = GTK_BUTTON(gtk_button_new_with_label(label ? label : ""));
1849 g_object_set_data(G_OBJECT(btn), "action", act);
1850 g_object_set_data(G_OBJECT(btn), "sheet-widget", main_box);
1851 g_signal_connect(G_OBJECT(btn), "clicked",
1852 G_CALLBACK(datasheet_action_clicked), act);
1854 gtk_box_pack_start(GTK_BOX(buttons_box), GTK_WIDGET(btn),
1855 FALSE, FALSE, 0);
1856 gtk_widget_show(GTK_WIDGET(btn));
1859 g_object_set_data(G_OBJECT(main_box), "view", view);
1860 g_object_set_data(G_OBJECT(main_box), "buttons", buttons_box);
1861 g_object_set_data(G_OBJECT(main_box), "sheet", sheet);
1863 datasheet_fill(sheet, model);
1864 purple_signal_connect(sheet, "record-changed",
1865 pidgin_request_get_handle(),
1866 PURPLE_CALLBACK(datasheet_update), model);
1868 sel = gtk_tree_view_get_selection(view);
1869 g_signal_connect(G_OBJECT(sel), "changed",
1870 G_CALLBACK(datasheet_selection_changed_cb), main_box);
1872 return main_box;
1875 static void *
1876 pidgin_request_fields(const char *title, const char *primary,
1877 const char *secondary, PurpleRequestFields *fields, const char *ok_text,
1878 GCallback ok_cb, const char *cancel_text, GCallback cancel_cb,
1879 PurpleRequestCommonParameters *cpar, void *user_data)
1881 PidginRequestData *data;
1882 GtkWidget *win;
1883 GtkNotebook *notebook;
1884 GtkWidget **pages;
1885 GtkWidget *hbox, *vbox;
1886 GtkWidget *frame;
1887 GtkWidget *label;
1888 GtkWidget *grid;
1889 GtkWidget *button;
1890 GtkWidget *img;
1891 GtkSizeGroup *sg, *datasheet_buttons_sg;
1892 GList *gl, *fl;
1893 PurpleRequestFieldGroup *group;
1894 PurpleRequestField *field;
1895 char *label_text;
1896 char *primary_esc, *secondary_esc;
1897 const gboolean compact = purple_request_cpar_is_compact(cpar);
1898 GSList *extra_actions, *it;
1899 size_t extra_actions_count, i;
1900 const gchar **tab_names;
1901 guint tab_count;
1902 gboolean ok_btn = (ok_text != NULL);
1904 data = g_new0(PidginRequestData, 1);
1905 data->type = PURPLE_REQUEST_FIELDS;
1906 data->user_data = user_data;
1907 data->u.multifield.fields = fields;
1909 purple_request_fields_set_ui_data(fields, data);
1911 extra_actions = purple_request_cpar_get_extra_actions(cpar);
1912 extra_actions_count = g_slist_length(extra_actions) / 2;
1914 data->cb_count = 2;
1915 data->cbs = g_new0(GCallback, 2);
1917 data->cbs[0] = ok_cb;
1918 data->cbs[1] = cancel_cb;
1920 #ifdef _WIN32
1921 data->dialog = win = pidgin_create_dialog(PIDGIN_ALERT_TITLE, PIDGIN_HIG_BORDER, "multifield", TRUE) ;
1922 #else /* !_WIN32 */
1923 data->dialog = win = pidgin_create_dialog(title, PIDGIN_HIG_BORDER, "multifield", TRUE) ;
1924 #endif /* _WIN32 */
1926 g_signal_connect(G_OBJECT(win), "delete_event",
1927 G_CALLBACK(destroy_multifield_cb), data);
1929 /* Setup the main horizontal box */
1930 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
1931 gtk_container_add(GTK_CONTAINER(pidgin_dialog_get_vbox(GTK_DIALOG(win))), hbox);
1932 gtk_widget_show(hbox);
1934 /* Dialog icon. */
1935 img = pidgin_request_dialog_icon(PURPLE_REQUEST_FIELDS, cpar);
1936 gtk_widget_set_halign(img, GTK_ALIGN_START);
1937 gtk_widget_set_valign(img, GTK_ALIGN_START);
1938 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
1939 gtk_widget_show(img);
1941 pidgin_request_add_help(GTK_DIALOG(win), cpar);
1943 it = extra_actions;
1944 for (i = 0; i < extra_actions_count; i++, it = it->next->next) {
1945 const gchar *label = it->data;
1946 PurpleRequestFieldsCb *cb = it->next->data;
1948 button = pidgin_dialog_add_button(GTK_DIALOG(win), label,
1949 G_CALLBACK(multifield_extra_cb), data);
1950 g_object_set_data(G_OBJECT(button), "extra-cb", cb);
1953 /* Cancel button */
1954 button = pidgin_dialog_add_button(GTK_DIALOG(win), cancel_text,
1955 G_CALLBACK(multifield_cancel_cb), data);
1956 gtk_widget_set_can_default(button, TRUE);
1958 /* OK button */
1959 if (!ok_btn) {
1960 gtk_window_set_default(GTK_WINDOW(win), button);
1961 } else {
1962 button = pidgin_dialog_add_button(GTK_DIALOG(win), ok_text,
1963 G_CALLBACK(multifield_ok_cb), data);
1964 data->ok_button = button;
1965 gtk_widget_set_can_default(button, TRUE);
1966 gtk_window_set_default(GTK_WINDOW(win), button);
1969 pidgin_widget_decorate_account(hbox,
1970 purple_request_cpar_get_account(cpar));
1972 /* Setup the vbox */
1973 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
1974 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1975 gtk_widget_show(vbox);
1977 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1978 datasheet_buttons_sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1980 if(primary) {
1981 primary_esc = pidgin_request_escape(cpar, primary);
1982 label_text = g_strdup_printf(
1983 "<span weight=\"bold\" size=\"larger\">%s</span>", primary_esc);
1984 g_free(primary_esc);
1985 label = gtk_label_new(NULL);
1987 gtk_label_set_markup(GTK_LABEL(label), label_text);
1988 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1989 gtk_label_set_xalign(GTK_LABEL(label), 0);
1990 gtk_label_set_yalign(GTK_LABEL(label), 0);
1991 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1992 gtk_widget_show(label);
1993 g_free(label_text);
1996 /* Setup tabs */
1997 tab_names = purple_request_fields_get_tab_names(fields);
1998 if (tab_names == NULL) {
1999 notebook = NULL;
2000 tab_count = 1;
2002 pages = g_new0(GtkWidget*, 1);
2003 pages[0] = vbox;
2004 } else {
2005 tab_count = g_strv_length((gchar **)tab_names);
2006 notebook = GTK_NOTEBOOK(gtk_notebook_new());
2008 pages = g_new0(GtkWidget*, tab_count);
2010 for (i = 0; i < tab_count; i++) {
2011 pages[i] = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
2012 gtk_container_set_border_width(GTK_CONTAINER(pages[i]), PIDGIN_HIG_BORDER);
2013 gtk_notebook_append_page(notebook, pages[i], NULL);
2014 gtk_notebook_set_tab_label_text(notebook, pages[i], tab_names[i]);
2015 gtk_widget_show(pages[i]);
2019 for (i = 0; i < tab_count; i++) {
2020 guint total_fields = 0;
2021 GList *it;
2023 it = purple_request_fields_get_groups(fields);
2024 for (; it != NULL; it = g_list_next(it)) {
2025 group = it->data;
2026 if (purple_request_field_group_get_tab(group) != i)
2027 continue;
2028 total_fields += g_list_length(
2029 purple_request_field_group_get_fields(group));
2032 if(total_fields > 9) {
2033 GtkWidget *hbox_for_spacing, *vbox_for_spacing;
2035 gtk_container_set_border_width(
2036 GTK_CONTAINER(pages[i]), 0);
2038 hbox_for_spacing =
2039 gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PIDGIN_HIG_BORDER);
2040 gtk_box_pack_start(GTK_BOX(pages[i]),
2041 pidgin_make_scrollable(hbox_for_spacing,
2042 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC,
2043 GTK_SHADOW_NONE, -1, 200),
2044 TRUE, TRUE, 0);
2045 gtk_widget_show(hbox_for_spacing);
2047 vbox_for_spacing =
2048 gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
2049 gtk_box_pack_start(GTK_BOX(hbox_for_spacing),
2050 vbox_for_spacing, TRUE, TRUE,
2051 PIDGIN_HIG_BOX_SPACE);
2052 gtk_widget_show(vbox_for_spacing);
2054 pages[i] = gtk_box_new(GTK_ORIENTATION_VERTICAL, PIDGIN_HIG_BORDER);
2055 gtk_box_pack_start(GTK_BOX(vbox_for_spacing),
2056 pages[i], TRUE, TRUE, PIDGIN_HIG_BOX_SPACE);
2057 gtk_widget_show(pages[i]);
2060 if (notebook == NULL)
2061 vbox = pages[0];
2064 if (secondary) {
2065 secondary_esc = pidgin_request_escape(cpar, secondary);
2066 label = gtk_label_new(NULL);
2068 gtk_label_set_markup(GTK_LABEL(label), secondary_esc);
2069 g_free(secondary_esc);
2070 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
2071 gtk_label_set_xalign(GTK_LABEL(label), 0);
2072 gtk_label_set_yalign(GTK_LABEL(label), 0);
2073 gtk_box_pack_start(GTK_BOX(vbox), label, (notebook == NULL),
2074 (notebook == NULL), 0);
2075 gtk_widget_show(label);
2078 if (notebook != NULL) {
2079 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
2080 gtk_widget_show(GTK_WIDGET(notebook));
2083 for (gl = purple_request_fields_get_groups(fields);
2084 gl != NULL;
2085 gl = gl->next)
2087 GList *field_list;
2088 size_t field_count = 0;
2089 size_t cols = 1;
2090 size_t rows;
2091 #if 0
2092 size_t col_num;
2093 #endif
2094 size_t row_num = 0;
2095 guint tab_no;
2096 gboolean contains_resizable = FALSE, frame_fill;
2098 group = gl->data;
2099 field_list = purple_request_field_group_get_fields(group);
2100 tab_no = purple_request_field_group_get_tab(group);
2101 if (tab_no >= tab_count) {
2102 purple_debug_warning("gtkrequest",
2103 "Invalid tab number: %d", tab_no);
2104 tab_no = 0;
2107 if (purple_request_field_group_get_title(group) != NULL)
2109 frame = pidgin_make_frame(pages[tab_no],
2110 purple_request_field_group_get_title(group));
2112 else
2113 frame = pages[tab_no];
2115 field_count = g_list_length(field_list);
2116 #if 0
2117 if (field_count > 9)
2119 rows = field_count / 2;
2120 cols++;
2122 else
2123 #endif
2124 rows = field_count;
2126 #if 0
2127 col_num = 0;
2128 #endif
2130 for (fl = field_list; fl != NULL; fl = fl->next)
2132 PurpleRequestFieldType type;
2134 field = (PurpleRequestField *)fl->data;
2136 type = purple_request_field_get_field_type(field);
2138 if (type == PURPLE_REQUEST_FIELD_DATASHEET)
2139 contains_resizable = TRUE;
2141 if (type == PURPLE_REQUEST_FIELD_LABEL)
2143 #if 0
2144 if (col_num > 0)
2145 rows++;
2146 #endif
2148 rows++;
2150 else if ((type == PURPLE_REQUEST_FIELD_LIST) ||
2151 (type == PURPLE_REQUEST_FIELD_STRING &&
2152 purple_request_field_string_is_multiline(field)))
2154 #if 0
2155 if (col_num > 0)
2156 rows++;
2157 #endif
2159 rows += 2;
2160 } else if (compact && type != PURPLE_REQUEST_FIELD_BOOLEAN)
2161 rows++;
2163 #if 0
2164 col_num++;
2166 if (col_num >= cols)
2167 col_num = 0;
2168 #endif
2171 grid = gtk_grid_new();
2172 gtk_grid_set_row_spacing(GTK_GRID(grid), PIDGIN_HIG_BOX_SPACE);
2173 gtk_grid_set_column_spacing(GTK_GRID(grid), PIDGIN_HIG_BOX_SPACE);
2175 frame_fill = (notebook == NULL || contains_resizable);
2176 gtk_box_pack_start(GTK_BOX(frame), grid, frame_fill, frame_fill, 0);
2177 gtk_widget_show(grid);
2179 for (row_num = 0, fl = field_list;
2180 row_num < rows && fl != NULL;
2181 row_num++)
2183 #if 0
2184 for (col_num = 0;
2185 col_num < cols && fl != NULL;
2186 col_num++, fl = fl->next)
2187 #else
2188 gboolean dummy_counter = TRUE;
2189 /* it's the same as loop above */
2190 for (; dummy_counter && fl != NULL; dummy_counter = FALSE, fl = fl->next)
2191 #endif
2193 #if 0
2194 size_t col_offset = col_num * 2;
2195 #else
2196 size_t col_offset = 0;
2197 #endif
2198 PurpleRequestFieldType type;
2199 GtkWidget *widget = NULL;
2200 gchar *field_label;
2202 label = NULL;
2203 field = fl->data;
2205 if (!purple_request_field_is_visible(field)) {
2206 #if 0
2207 col_num--;
2208 #endif
2209 continue;
2212 type = purple_request_field_get_field_type(field);
2213 field_label = pidgin_request_escape(cpar,
2214 purple_request_field_get_label(field));
2216 if (type != PURPLE_REQUEST_FIELD_BOOLEAN && field_label)
2218 char *text = NULL;
2220 if (field_label[strlen(field_label) - 1] != ':' &&
2221 field_label[strlen(field_label) - 1] != '?' &&
2222 type != PURPLE_REQUEST_FIELD_LABEL)
2224 text = g_strdup_printf("%s:", field_label);
2227 label = gtk_label_new(NULL);
2228 gtk_label_set_markup_with_mnemonic(GTK_LABEL(label), text ? text : field_label);
2229 g_free(text);
2231 gtk_widget_set_hexpand(label, TRUE);
2232 gtk_widget_set_vexpand(label, TRUE);
2233 gtk_label_set_xalign(GTK_LABEL(label), 0);
2235 gtk_size_group_add_widget(sg, label);
2237 if (type == PURPLE_REQUEST_FIELD_LABEL ||
2238 type == PURPLE_REQUEST_FIELD_LIST ||
2239 (type == PURPLE_REQUEST_FIELD_STRING &&
2240 purple_request_field_string_is_multiline(field)))
2242 #if 0
2243 if(col_num > 0)
2244 row_num++;
2245 #endif
2247 gtk_grid_attach(GTK_GRID(grid), label,
2248 0, row_num, 2 * cols, 1);
2250 row_num++;
2251 #if 0
2252 col_num=cols;
2253 #endif
2255 else
2257 gtk_grid_attach(GTK_GRID(grid), label,
2258 col_offset, row_num, 1, 1);
2261 gtk_widget_show(label);
2262 g_free(field_label);
2265 widget = GTK_WIDGET(purple_request_field_get_ui_data(field));
2266 if (widget == NULL)
2268 if (type == PURPLE_REQUEST_FIELD_STRING)
2269 widget = create_string_field(field);
2270 else if (type == PURPLE_REQUEST_FIELD_INTEGER)
2271 widget = create_int_field(field);
2272 else if (type == PURPLE_REQUEST_FIELD_BOOLEAN)
2273 widget = create_bool_field(field, cpar);
2274 else if (type == PURPLE_REQUEST_FIELD_CHOICE)
2275 widget = create_choice_field(field, cpar);
2276 else if (type == PURPLE_REQUEST_FIELD_LIST)
2277 widget = create_list_field(field);
2278 else if (type == PURPLE_REQUEST_FIELD_IMAGE)
2279 widget = create_image_field(field);
2280 else if (type == PURPLE_REQUEST_FIELD_ACCOUNT)
2281 widget = create_account_field(field);
2282 else if (type == PURPLE_REQUEST_FIELD_DATASHEET)
2283 widget = create_datasheet_field(field, datasheet_buttons_sg);
2284 else
2285 continue;
2288 gtk_widget_set_sensitive(widget,
2289 purple_request_field_is_sensitive(field));
2291 if (label)
2292 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
2294 gtk_widget_set_hexpand(widget, TRUE);
2295 gtk_widget_set_vexpand(widget, TRUE);
2296 gtk_widget_set_margin_start(widget, 5);
2297 gtk_widget_set_margin_end(widget, 5);
2299 if (type == PURPLE_REQUEST_FIELD_STRING &&
2300 purple_request_field_string_is_multiline(field))
2302 gtk_grid_attach(GTK_GRID(grid), widget,
2303 0, row_num, 2 * cols, 1);
2305 else if (type == PURPLE_REQUEST_FIELD_LIST)
2307 gtk_grid_attach(GTK_GRID(grid), widget,
2308 0, row_num, 2 * cols, 1);
2310 else if (type == PURPLE_REQUEST_FIELD_BOOLEAN)
2312 gtk_grid_attach(GTK_GRID(grid), widget,
2313 col_offset, row_num, 1, 1);
2315 else if (compact) {
2316 row_num++;
2317 gtk_grid_attach(GTK_GRID(grid), widget,
2318 0, row_num, 2 * cols, 1);
2319 } else {
2320 gtk_grid_attach(GTK_GRID(grid), widget,
2321 1, row_num, 2 * cols - 1, 1);
2324 gtk_widget_show(widget);
2326 purple_request_field_set_ui_data(field, widget);
2331 g_object_unref(sg);
2332 g_object_unref(datasheet_buttons_sg);
2334 if (!purple_request_fields_all_required_filled(fields))
2335 gtk_widget_set_sensitive(data->ok_button, FALSE);
2337 if (!purple_request_fields_all_valid(fields))
2338 gtk_widget_set_sensitive(data->ok_button, FALSE);
2340 g_free(pages);
2342 pidgin_auto_parent_window(win);
2344 gtk_widget_show(win);
2346 return data;
2349 static void
2350 file_yes_no_cb(PidginRequestData *data, gint id)
2352 /* Only call the callback if yes was selected, otherwise the request
2353 * (eg. file transfer) will be cancelled, then when a new filename is chosen
2354 * things go BOOM */
2355 if (id == 1) {
2356 if (data->cbs[1] != NULL)
2357 ((PurpleRequestFileCb)data->cbs[1])(data->user_data, data->u.file.name);
2358 purple_request_close(data->type, data);
2359 } else {
2360 pidgin_clear_cursor(GTK_WIDGET(data->dialog));
2364 static void
2365 file_ok_check_if_exists_cb(GtkWidget *widget, gint response, PidginRequestData *data)
2367 gchar *current_folder;
2369 generic_response_start(data);
2371 if (response != GTK_RESPONSE_ACCEPT) {
2372 if (data->cbs[0] != NULL)
2373 ((PurpleRequestFileCb)data->cbs[0])(data->user_data, NULL);
2374 purple_request_close(data->type, data);
2375 return;
2378 data->u.file.name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data->dialog));
2379 current_folder = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(data->dialog));
2380 if (current_folder != NULL) {
2381 if (data->u.file.savedialog) {
2382 purple_prefs_set_path(PIDGIN_PREFS_ROOT "/filelocations/last_save_folder", current_folder);
2383 } else {
2384 purple_prefs_set_path(PIDGIN_PREFS_ROOT "/filelocations/last_open_folder", current_folder);
2386 g_free(current_folder);
2388 if ((data->u.file.savedialog == TRUE) &&
2389 (g_file_test(data->u.file.name, G_FILE_TEST_EXISTS))) {
2390 purple_request_action(data, NULL, _("That file already exists"),
2391 _("Would you like to overwrite it?"), 0,
2392 NULL,
2393 data, 2,
2394 _("Overwrite"), G_CALLBACK(file_yes_no_cb),
2395 _("Choose New Name"), G_CALLBACK(file_yes_no_cb));
2396 } else
2397 file_yes_no_cb(data, 1);
2400 static void *
2401 pidgin_request_file(const char *title, const char *filename,
2402 gboolean savedialog, GCallback ok_cb, GCallback cancel_cb,
2403 PurpleRequestCommonParameters *cpar, void *user_data)
2405 PidginRequestData *data;
2406 GtkWidget *filesel;
2407 #ifdef _WIN32
2408 const gchar *current_folder;
2409 gboolean folder_set = FALSE;
2410 #endif
2412 data = g_new0(PidginRequestData, 1);
2413 data->type = PURPLE_REQUEST_FILE;
2414 data->user_data = user_data;
2415 data->cb_count = 2;
2416 data->cbs = g_new0(GCallback, 2);
2417 data->cbs[0] = cancel_cb;
2418 data->cbs[1] = ok_cb;
2419 data->u.file.savedialog = savedialog;
2421 filesel = gtk_file_chooser_dialog_new(
2422 title ? title : (savedialog ? _("Save File...")
2423 : _("Open File...")),
2424 NULL,
2425 savedialog ? GTK_FILE_CHOOSER_ACTION_SAVE
2426 : GTK_FILE_CHOOSER_ACTION_OPEN,
2427 _("_Cancel"), GTK_RESPONSE_CANCEL,
2428 savedialog ? _("_Save")
2429 : _("_Open"),
2430 GTK_RESPONSE_ACCEPT,
2431 NULL);
2432 gtk_dialog_set_default_response(GTK_DIALOG(filesel), GTK_RESPONSE_ACCEPT);
2434 pidgin_request_add_help(GTK_DIALOG(filesel), cpar);
2436 if ((filename != NULL) && (*filename != '\0')) {
2437 if (savedialog)
2438 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(filesel), filename);
2439 else if (g_file_test(filename, G_FILE_TEST_EXISTS))
2440 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filesel), filename);
2443 #ifdef _WIN32
2444 if (savedialog) {
2445 current_folder = purple_prefs_get_path(PIDGIN_PREFS_ROOT "/filelocations/last_save_folder");
2446 } else {
2447 current_folder = purple_prefs_get_path(PIDGIN_PREFS_ROOT "/filelocations/last_open_folder");
2450 if ((filename == NULL || *filename == '\0' || !g_file_test(filename, G_FILE_TEST_EXISTS)) &&
2451 (current_folder != NULL) && (*current_folder != '\0')) {
2452 folder_set = gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(filesel), current_folder);
2455 if (!folder_set && (filename == NULL || *filename == '\0' || !g_file_test(filename, G_FILE_TEST_EXISTS))) {
2456 char *my_documents = wpurple_get_special_folder(CSIDL_PERSONAL);
2458 if (my_documents != NULL) {
2459 gtk_file_chooser_set_current_folder(
2460 GTK_FILE_CHOOSER(filesel), my_documents);
2462 g_free(my_documents);
2465 #endif
2467 g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(filesel)), "response",
2468 G_CALLBACK(file_ok_check_if_exists_cb), data);
2470 pidgin_auto_parent_window(filesel);
2472 data->dialog = filesel;
2473 gtk_widget_show(filesel);
2475 return (void *)data;
2478 static void *
2479 pidgin_request_folder(const char *title, const char *dirname, GCallback ok_cb,
2480 GCallback cancel_cb, PurpleRequestCommonParameters *cpar,
2481 void *user_data)
2483 PidginRequestData *data;
2484 GtkWidget *dirsel;
2486 data = g_new0(PidginRequestData, 1);
2487 data->type = PURPLE_REQUEST_FOLDER;
2488 data->user_data = user_data;
2489 data->cb_count = 2;
2490 data->cbs = g_new0(GCallback, 2);
2491 data->cbs[0] = cancel_cb;
2492 data->cbs[1] = ok_cb;
2493 data->u.file.savedialog = FALSE;
2495 dirsel = gtk_file_chooser_dialog_new(
2496 title ? title : _("Select Folder..."),
2497 NULL,
2498 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
2499 _("_Cancel"), GTK_RESPONSE_CANCEL,
2500 _("_OK"), GTK_RESPONSE_ACCEPT,
2501 NULL);
2502 gtk_dialog_set_default_response(GTK_DIALOG(dirsel), GTK_RESPONSE_ACCEPT);
2504 pidgin_request_add_help(GTK_DIALOG(dirsel), cpar);
2506 if ((dirname != NULL) && (*dirname != '\0'))
2507 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dirsel), dirname);
2509 g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(dirsel)), "response",
2510 G_CALLBACK(file_ok_check_if_exists_cb), data);
2512 data->dialog = dirsel;
2513 pidgin_auto_parent_window(dirsel);
2515 gtk_widget_show(dirsel);
2517 return (void *)data;
2520 /* if request callback issues another request, it should be attached to the
2521 * primary request parent */
2522 static void
2523 pidgin_window_detach_children(GtkWindow* win)
2525 GList *it;
2526 GtkWindow *par;
2528 g_return_if_fail(win != NULL);
2530 par = gtk_window_get_transient_for(win);
2531 it = gtk_window_list_toplevels();
2532 for (it = g_list_first(it); it != NULL; it = g_list_next(it)) {
2533 GtkWindow *child = GTK_WINDOW(it->data);
2534 if (gtk_window_get_transient_for(child) != win)
2535 continue;
2536 if (gtk_window_get_destroy_with_parent(child)) {
2537 #ifdef _WIN32
2538 /* XXX test/verify it: Win32 gtk ignores
2539 * gtk_window_set_destroy_with_parent(..., FALSE). */
2540 gtk_window_set_transient_for(child, NULL);
2541 #endif
2542 continue;
2544 gtk_window_set_transient_for(child, par);
2548 static void
2549 pidgin_close_request(PurpleRequestType type, void *ui_handle)
2551 PidginRequestData *data = (PidginRequestData *)ui_handle;
2553 g_free(data->cbs);
2555 pidgin_window_detach_children(GTK_WINDOW(data->dialog));
2557 gtk_widget_destroy(data->dialog);
2559 if (type == PURPLE_REQUEST_FIELDS)
2560 purple_request_fields_destroy(data->u.multifield.fields);
2561 else if (type == PURPLE_REQUEST_FILE)
2562 g_free(data->u.file.name);
2564 g_free(data);
2567 GtkWindow *
2568 pidgin_request_get_dialog_window(void *ui_handle)
2570 PidginRequestData *data = ui_handle;
2572 g_return_val_if_fail(
2573 purple_request_is_valid_ui_handle(data, NULL), NULL);
2575 return GTK_WINDOW(data->dialog);
2578 static PurpleRequestUiOps ops =
2580 PURPLE_REQUEST_FEATURE_HTML,
2581 pidgin_request_input,
2582 pidgin_request_choice,
2583 pidgin_request_action,
2584 pidgin_request_wait,
2585 pidgin_request_wait_update,
2586 pidgin_request_fields,
2587 pidgin_request_file,
2588 pidgin_request_folder,
2589 pidgin_close_request,
2590 NULL,
2591 NULL,
2592 NULL,
2593 NULL
2596 PurpleRequestUiOps *
2597 pidgin_request_get_ui_ops(void)
2599 return &ops;
2602 void *
2603 pidgin_request_get_handle(void)
2605 static int handle;
2607 return &handle;
2610 static void
2611 pidgin_request_datasheet_stock_remove(gpointer obj)
2613 if (obj == NULL)
2614 return;
2615 g_object_unref(obj);
2618 void
2619 pidgin_request_init(void)
2621 datasheet_stock = g_hash_table_new_full(g_str_hash, g_str_equal,
2622 g_free, pidgin_request_datasheet_stock_remove);
2625 void
2626 pidgin_request_uninit(void)
2628 purple_signals_disconnect_by_handle(pidgin_request_get_handle());
2629 g_hash_table_destroy(datasheet_stock);
2630 datasheet_stock = NULL;