comparison pidgin/gtkutils.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children d54794a47c56
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /**
2 * @file gtkutils.c GTK+ utility functions
3 * @ingroup gtkui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26 #include "gtkgaim.h"
27
28 #ifndef _WIN32
29 # include <X11/Xlib.h>
30 #else
31 # ifdef small
32 # undef small
33 # endif
34 #endif /*_WIN32*/
35
36 #ifdef USE_GTKSPELL
37 # include <gtkspell/gtkspell.h>
38 # ifdef _WIN32
39 # include "wspell.h"
40 # endif
41 #endif
42
43 #include <gdk/gdkkeysyms.h>
44
45 #include "conversation.h"
46 #include "debug.h"
47 #include "desktopitem.h"
48 #include "imgstore.h"
49 #include "notify.h"
50 #include "prefs.h"
51 #include "prpl.h"
52 #include "request.h"
53 #include "signals.h"
54 #include "util.h"
55
56 #include "gtkconv.h"
57 #include "gtkdialogs.h"
58 #include "gtkimhtml.h"
59 #include "gtkimhtmltoolbar.h"
60 #include "gaimstock.h"
61 #include "gtkthemes.h"
62 #include "gtkutils.h"
63
64 static guint accels_save_timer = 0;
65
66 static gboolean
67 url_clicked_idle_cb(gpointer data)
68 {
69 gaim_notify_uri(NULL, data);
70 g_free(data);
71 return FALSE;
72 }
73
74 static void
75 url_clicked_cb(GtkWidget *w, const char *uri)
76 {
77 g_idle_add(url_clicked_idle_cb, g_strdup(uri));
78 }
79
80 static GtkIMHtmlFuncs gtkimhtml_cbs = {
81 (GtkIMHtmlGetImageFunc)gaim_imgstore_get,
82 (GtkIMHtmlGetImageDataFunc)gaim_imgstore_get_data,
83 (GtkIMHtmlGetImageSizeFunc)gaim_imgstore_get_size,
84 (GtkIMHtmlGetImageFilenameFunc)gaim_imgstore_get_filename,
85 gaim_imgstore_ref,
86 gaim_imgstore_unref,
87 };
88
89 void
90 gaim_setup_imhtml(GtkWidget *imhtml)
91 {
92 g_return_if_fail(imhtml != NULL);
93 g_return_if_fail(GTK_IS_IMHTML(imhtml));
94
95 g_signal_connect(G_OBJECT(imhtml), "url_clicked",
96 G_CALLBACK(url_clicked_cb), NULL);
97
98 gaim_gtkthemes_smiley_themeize(imhtml);
99
100 gtk_imhtml_set_funcs(GTK_IMHTML(imhtml), &gtkimhtml_cbs);
101 }
102
103 GtkWidget *
104 gaim_gtk_create_imhtml(gboolean editable, GtkWidget **imhtml_ret, GtkWidget **toolbar_ret, GtkWidget **sw_ret)
105 {
106 GtkWidget *frame;
107 GtkWidget *imhtml;
108 GtkWidget *sep;
109 GtkWidget *sw;
110 GtkWidget *toolbar = NULL;
111 GtkWidget *vbox;
112
113 frame = gtk_frame_new(NULL);
114 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
115
116 vbox = gtk_vbox_new(FALSE, 0);
117 gtk_container_add(GTK_CONTAINER(frame), vbox);
118 gtk_widget_show(vbox);
119
120 if (editable) {
121 toolbar = gtk_imhtmltoolbar_new();
122 gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
123 gtk_widget_show(toolbar);
124
125 sep = gtk_hseparator_new();
126 gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE, FALSE, 0);
127 g_signal_connect_swapped(G_OBJECT(toolbar), "show", G_CALLBACK(gtk_widget_show), sep);
128 g_signal_connect_swapped(G_OBJECT(toolbar), "hide", G_CALLBACK(gtk_widget_hide), sep);
129 gtk_widget_show(sep);
130 }
131
132 sw = gtk_scrolled_window_new(NULL, NULL);
133 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
134 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
135 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
136 gtk_widget_show(sw);
137
138 imhtml = gtk_imhtml_new(NULL, NULL);
139 gtk_imhtml_set_editable(GTK_IMHTML(imhtml), editable);
140 gtk_imhtml_set_format_functions(GTK_IMHTML(imhtml), GTK_IMHTML_ALL ^ GTK_IMHTML_IMAGE);
141 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(imhtml), GTK_WRAP_WORD_CHAR);
142 #ifdef USE_GTKSPELL
143 if (editable && gaim_prefs_get_bool("/gaim/gtk/conversations/spellcheck"))
144 gaim_gtk_setup_gtkspell(GTK_TEXT_VIEW(imhtml));
145 #endif
146 gtk_widget_show(imhtml);
147
148 if (editable) {
149 gtk_imhtmltoolbar_attach(GTK_IMHTMLTOOLBAR(toolbar), imhtml);
150 gtk_imhtmltoolbar_associate_smileys(GTK_IMHTMLTOOLBAR(toolbar), "default");
151 }
152 gaim_setup_imhtml(imhtml);
153
154 gtk_container_add(GTK_CONTAINER(sw), imhtml);
155
156 if (imhtml_ret != NULL)
157 *imhtml_ret = imhtml;
158
159 if (editable && (toolbar_ret != NULL))
160 *toolbar_ret = toolbar;
161
162 if (sw_ret != NULL)
163 *sw_ret = sw;
164
165 return frame;
166 }
167
168 void
169 gaim_gtk_set_sensitive_if_input(GtkWidget *entry, GtkWidget *dialog)
170 {
171 const char *text = gtk_entry_get_text(GTK_ENTRY(entry));
172 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), GTK_RESPONSE_OK,
173 (*text != '\0'));
174 }
175
176 void
177 gaim_gtk_toggle_sensitive(GtkWidget *widget, GtkWidget *to_toggle)
178 {
179 gboolean sensitivity;
180
181 if (to_toggle == NULL)
182 return;
183
184 sensitivity = GTK_WIDGET_IS_SENSITIVE(to_toggle);
185
186 gtk_widget_set_sensitive(to_toggle, !sensitivity);
187 }
188
189 void
190 gaim_gtk_toggle_sensitive_array(GtkWidget *w, GPtrArray *data)
191 {
192 gboolean sensitivity;
193 gpointer element;
194 int i;
195
196 for (i=0; i < data->len; i++) {
197 element = g_ptr_array_index(data,i);
198 if (element == NULL)
199 continue;
200
201 sensitivity = GTK_WIDGET_IS_SENSITIVE(element);
202
203 gtk_widget_set_sensitive(element, !sensitivity);
204 }
205 }
206
207 void
208 gaim_gtk_toggle_showhide(GtkWidget *widget, GtkWidget *to_toggle)
209 {
210 if (to_toggle == NULL)
211 return;
212
213 if (GTK_WIDGET_VISIBLE(to_toggle))
214 gtk_widget_hide(to_toggle);
215 else
216 gtk_widget_show(to_toggle);
217 }
218
219 void gaim_separator(GtkWidget *menu)
220 {
221 GtkWidget *menuitem;
222
223 menuitem = gtk_separator_menu_item_new();
224 gtk_widget_show(menuitem);
225 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
226 }
227
228 GtkWidget *gaim_new_item(GtkWidget *menu, const char *str)
229 {
230 GtkWidget *menuitem;
231 GtkWidget *label;
232
233 menuitem = gtk_menu_item_new();
234 if (menu)
235 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
236 gtk_widget_show(menuitem);
237
238 label = gtk_label_new(str);
239 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
240 gtk_label_set_pattern(GTK_LABEL(label), "_");
241 gtk_container_add(GTK_CONTAINER(menuitem), label);
242 gtk_widget_show(label);
243 /* FIXME: Go back and fix this
244 gtk_widget_add_accelerator(menuitem, "activate", accel, str[0],
245 GDK_MOD1_MASK, GTK_ACCEL_LOCKED);
246 */
247 gaim_set_accessible_label (menuitem, label);
248 return menuitem;
249 }
250
251 GtkWidget *gaim_new_check_item(GtkWidget *menu, const char *str,
252 GtkSignalFunc sf, gpointer data, gboolean checked)
253 {
254 GtkWidget *menuitem;
255 menuitem = gtk_check_menu_item_new_with_mnemonic(str);
256
257 if (menu)
258 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
259
260 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), checked);
261
262 if (sf)
263 g_signal_connect(G_OBJECT(menuitem), "activate", sf, data);
264
265 gtk_widget_show_all(menuitem);
266
267 return menuitem;
268 }
269
270 GtkWidget *
271 gaim_pixbuf_toolbar_button_from_stock(const char *icon)
272 {
273 GtkWidget *button, *image, *bbox;
274
275 button = gtk_toggle_button_new();
276 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
277
278 bbox = gtk_vbox_new(FALSE, 0);
279
280 gtk_container_add (GTK_CONTAINER(button), bbox);
281
282 image = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU);
283 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 0);
284
285 gtk_widget_show_all(bbox);
286
287 return button;
288 }
289
290 GtkWidget *
291 gaim_pixbuf_button_from_stock(const char *text, const char *icon,
292 GaimButtonOrientation style)
293 {
294 GtkWidget *button, *image, *label, *bbox, *ibox, *lbox = NULL;
295
296 button = gtk_button_new();
297
298 if (style == GAIM_BUTTON_HORIZONTAL) {
299 bbox = gtk_hbox_new(FALSE, 0);
300 ibox = gtk_hbox_new(FALSE, 0);
301 if (text)
302 lbox = gtk_hbox_new(FALSE, 0);
303 } else {
304 bbox = gtk_vbox_new(FALSE, 0);
305 ibox = gtk_vbox_new(FALSE, 0);
306 if (text)
307 lbox = gtk_vbox_new(FALSE, 0);
308 }
309
310 gtk_container_add(GTK_CONTAINER(button), bbox);
311
312 if (icon) {
313 gtk_box_pack_start_defaults(GTK_BOX(bbox), ibox);
314 image = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON);
315 gtk_box_pack_end(GTK_BOX(ibox), image, FALSE, TRUE, 0);
316 }
317
318 if (text) {
319 gtk_box_pack_start_defaults(GTK_BOX(bbox), lbox);
320 label = gtk_label_new(NULL);
321 gtk_label_set_text_with_mnemonic(GTK_LABEL(label), text);
322 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
323 gtk_box_pack_start(GTK_BOX(lbox), label, FALSE, TRUE, 0);
324 gaim_set_accessible_label (button, label);
325 }
326
327 gtk_widget_show_all(bbox);
328
329 return button;
330 }
331
332
333 GtkWidget *gaim_new_item_from_stock(GtkWidget *menu, const char *str, const char *icon, GtkSignalFunc sf, gpointer data, guint accel_key, guint accel_mods, char *mod)
334 {
335 GtkWidget *menuitem;
336 /*
337 GtkWidget *hbox;
338 GtkWidget *label;
339 */
340 GtkWidget *image;
341
342 if (icon == NULL)
343 menuitem = gtk_menu_item_new_with_mnemonic(str);
344 else
345 menuitem = gtk_image_menu_item_new_with_mnemonic(str);
346
347 if (menu)
348 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
349
350 if (sf)
351 g_signal_connect(G_OBJECT(menuitem), "activate", sf, data);
352
353 if (icon != NULL) {
354 image = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU);
355 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
356 }
357 /* FIXME: this isn't right
358 if (mod) {
359 label = gtk_label_new(mod);
360 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 2);
361 gtk_widget_show(label);
362 }
363 */
364 /*
365 if (accel_key) {
366 gtk_widget_add_accelerator(menuitem, "activate", accel, accel_key,
367 accel_mods, GTK_ACCEL_LOCKED);
368 }
369 */
370
371 gtk_widget_show_all(menuitem);
372
373 return menuitem;
374 }
375
376 GtkWidget *
377 gaim_gtk_make_frame(GtkWidget *parent, const char *title)
378 {
379 GtkWidget *vbox, *label, *hbox;
380 char *labeltitle;
381
382 vbox = gtk_vbox_new(FALSE, GAIM_HIG_BOX_SPACE);
383 gtk_box_pack_start(GTK_BOX(parent), vbox, FALSE, FALSE, 0);
384 gtk_widget_show(vbox);
385
386 label = gtk_label_new(NULL);
387
388 labeltitle = g_strdup_printf("<span weight=\"bold\">%s</span>", title);
389 gtk_label_set_markup(GTK_LABEL(label), labeltitle);
390 g_free(labeltitle);
391
392 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
393 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
394 gtk_widget_show(label);
395 gaim_set_accessible_label (vbox, label);
396
397 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
398 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
399 gtk_widget_show(hbox);
400
401 label = gtk_label_new(" ");
402 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
403 gtk_widget_show(label);
404
405 vbox = gtk_vbox_new(FALSE, GAIM_HIG_BOX_SPACE);
406 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
407 gtk_widget_show(vbox);
408
409 return vbox;
410 }
411
412 static void
413 protocol_menu_cb(GtkWidget *optmenu, GCallback cb)
414 {
415 GtkWidget *menu;
416 GtkWidget *item;
417 const char *protocol;
418 gpointer user_data;
419
420 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
421 item = gtk_menu_get_active(GTK_MENU(menu));
422
423 protocol = g_object_get_data(G_OBJECT(item), "protocol");
424 user_data = (g_object_get_data(G_OBJECT(optmenu), "user_data"));
425
426 if (cb != NULL)
427 ((void (*)(GtkWidget *, const char *, gpointer))cb)(item, protocol,
428 user_data);
429 }
430
431 GtkWidget *
432 gaim_gtk_protocol_option_menu_new(const char *id, GCallback cb,
433 gpointer user_data)
434 {
435 GaimPluginProtocolInfo *prpl_info;
436 GaimPlugin *plugin;
437 GtkWidget *hbox;
438 GtkWidget *label;
439 GtkWidget *optmenu;
440 GtkWidget *menu;
441 GtkWidget *item;
442 GtkWidget *image;
443 GdkPixbuf *pixbuf;
444 GdkPixbuf *scale;
445 GList *p;
446 GtkSizeGroup *sg;
447 char *filename;
448 const char *proto_name;
449 char buf[256];
450 int i, selected_index = -1;
451
452 optmenu = gtk_option_menu_new();
453 gtk_widget_show(optmenu);
454
455 g_object_set_data(G_OBJECT(optmenu), "user_data", user_data);
456
457 menu = gtk_menu_new();
458 gtk_widget_show(menu);
459
460 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
461
462 for (p = gaim_plugins_get_protocols(), i = 0;
463 p != NULL;
464 p = p->next, i++) {
465
466 plugin = (GaimPlugin *)p->data;
467 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
468
469 /* Create the item. */
470 item = gtk_menu_item_new();
471
472 /* Create the hbox. */
473 hbox = gtk_hbox_new(FALSE, 4);
474 gtk_container_add(GTK_CONTAINER(item), hbox);
475 gtk_widget_show(hbox);
476
477 /* Load the image. */
478 proto_name = prpl_info->list_icon(NULL, NULL);
479 g_snprintf(buf, sizeof(buf), "%s.png", proto_name);
480
481 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status",
482 "default", buf, NULL);
483 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
484 g_free(filename);
485
486 if (pixbuf != NULL) {
487 /* Scale and insert the image */
488 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16,
489 GDK_INTERP_BILINEAR);
490 image = gtk_image_new_from_pixbuf(scale);
491
492 g_object_unref(G_OBJECT(pixbuf));
493 g_object_unref(G_OBJECT(scale));
494 }
495 else
496 image = gtk_image_new();
497
498 gtk_size_group_add_widget(sg, image);
499
500 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
501 gtk_widget_show(image);
502
503 /* Create the label. */
504 label = gtk_label_new(plugin->info->name);
505 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
506 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
507 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
508 gtk_widget_show(label);
509
510 g_object_set_data(G_OBJECT(item), "protocol", plugin->info->id);
511
512 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
513 gtk_widget_show(item);
514 gaim_set_accessible_label (item, label);
515
516 if (id != NULL && !strcmp(plugin->info->id, id))
517 selected_index = i;
518 }
519
520 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
521
522 if (selected_index != -1)
523 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), selected_index);
524
525 g_signal_connect(G_OBJECT(optmenu), "changed",
526 G_CALLBACK(protocol_menu_cb), cb);
527
528 g_object_unref(sg);
529
530 return optmenu;
531 }
532
533 GaimAccount *
534 gaim_gtk_account_option_menu_get_selected(GtkWidget *optmenu)
535 {
536 GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
537 GtkWidget *item = gtk_menu_get_active(GTK_MENU(menu));
538 return g_object_get_data(G_OBJECT(item), "account");
539 }
540
541 static void
542 account_menu_cb(GtkWidget *optmenu, GCallback cb)
543 {
544 GtkWidget *menu;
545 GtkWidget *item;
546 GaimAccount *account;
547 gpointer user_data;
548
549 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
550 item = gtk_menu_get_active(GTK_MENU(menu));
551
552 account = g_object_get_data(G_OBJECT(item), "account");
553 user_data = g_object_get_data(G_OBJECT(optmenu), "user_data");
554
555 if (cb != NULL)
556 ((void (*)(GtkWidget *, GaimAccount *, gpointer))cb)(item, account,
557 user_data);
558 }
559
560 static void
561 create_account_menu(GtkWidget *optmenu, GaimAccount *default_account,
562 GaimFilterAccountFunc filter_func, gboolean show_all)
563 {
564 GaimAccount *account;
565 GtkWidget *menu;
566 GtkWidget *item;
567 GtkWidget *image;
568 GtkWidget *hbox;
569 GtkWidget *label;
570 GdkPixbuf *pixbuf;
571 GdkPixbuf *scale;
572 GList *list;
573 GList *p;
574 GtkSizeGroup *sg;
575 char *filename;
576 const char *proto_name;
577 char buf[256];
578 int i, selected_index = -1;
579
580 if (show_all)
581 list = gaim_accounts_get_all();
582 else
583 list = gaim_connections_get_all();
584
585 menu = gtk_menu_new();
586 gtk_widget_show(menu);
587
588 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
589
590 for (p = list, i = 0; p != NULL; p = p->next, i++) {
591 GaimPluginProtocolInfo *prpl_info = NULL;
592 GaimPlugin *plugin;
593
594 if (show_all)
595 account = (GaimAccount *)p->data;
596 else {
597 GaimConnection *gc = (GaimConnection *)p->data;
598
599 account = gaim_connection_get_account(gc);
600 }
601
602 if (filter_func && !filter_func(account)) {
603 i--;
604 continue;
605 }
606
607 plugin = gaim_find_prpl(gaim_account_get_protocol_id(account));
608
609 if (plugin != NULL)
610 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
611
612 /* Create the item. */
613 item = gtk_menu_item_new();
614
615 /* Create the hbox. */
616 hbox = gtk_hbox_new(FALSE, 4);
617 gtk_container_add(GTK_CONTAINER(item), hbox);
618 gtk_widget_show(hbox);
619
620 /* Load the image. */
621 if (prpl_info != NULL) {
622 proto_name = prpl_info->list_icon(account, NULL);
623 g_snprintf(buf, sizeof(buf), "%s.png", proto_name);
624
625 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status",
626 "default", buf, NULL);
627 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
628 g_free(filename);
629
630 if (pixbuf != NULL) {
631 /* Scale and insert the image */
632 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16,
633 GDK_INTERP_BILINEAR);
634
635 if (gaim_account_is_disconnected(account) && show_all &&
636 gaim_connections_get_all())
637 gdk_pixbuf_saturate_and_pixelate(scale, scale, 0.0, FALSE);
638
639 image = gtk_image_new_from_pixbuf(scale);
640
641 g_object_unref(G_OBJECT(pixbuf));
642 g_object_unref(G_OBJECT(scale));
643 }
644 else
645 image = gtk_image_new();
646 }
647 else
648 image = gtk_image_new();
649
650 gtk_size_group_add_widget(sg, image);
651
652 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
653 gtk_widget_show(image);
654
655 if (gaim_account_get_alias(account)) {
656 g_snprintf(buf, sizeof(buf), "%s (%s) (%s)",
657 gaim_account_get_username(account),
658 gaim_account_get_alias(account),
659 gaim_account_get_protocol_name(account));
660 } else {
661 g_snprintf(buf, sizeof(buf), "%s (%s)",
662 gaim_account_get_username(account),
663 gaim_account_get_protocol_name(account));
664 }
665
666 /* Create the label. */
667 label = gtk_label_new(buf);
668 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
669 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
670 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
671 gtk_widget_show(label);
672
673 g_object_set_data(G_OBJECT(item), "account", account);
674
675 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
676 gtk_widget_show(item);
677 gaim_set_accessible_label (item, label);
678
679 if (default_account != NULL && account == default_account)
680 selected_index = i;
681 }
682
683 g_object_unref(sg);
684
685 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
686
687 /* Set the place we should be at. */
688 if (selected_index != -1)
689 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), selected_index);
690 }
691
692 static void
693 regenerate_account_menu(GtkWidget *optmenu)
694 {
695 GtkWidget *menu;
696 GtkWidget *item;
697 gboolean show_all;
698 GaimAccount *account;
699 GaimFilterAccountFunc filter_func;
700
701 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
702 item = gtk_menu_get_active(GTK_MENU(menu));
703 account = g_object_get_data(G_OBJECT(item), "account");
704
705 show_all = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(optmenu),
706 "show_all"));
707
708 filter_func = g_object_get_data(G_OBJECT(optmenu),
709 "filter_func");
710
711 gtk_option_menu_remove_menu(GTK_OPTION_MENU(optmenu));
712
713 create_account_menu(optmenu, account, filter_func, show_all);
714 }
715
716 static void
717 account_menu_sign_on_off_cb(GaimConnection *gc, GtkWidget *optmenu)
718 {
719 regenerate_account_menu(optmenu);
720 }
721
722 static void
723 account_menu_added_removed_cb(GaimAccount *account, GtkWidget *optmenu)
724 {
725 regenerate_account_menu(optmenu);
726 }
727
728 static gboolean
729 account_menu_destroyed_cb(GtkWidget *optmenu, GdkEvent *event,
730 void *user_data)
731 {
732 gaim_signals_disconnect_by_handle(optmenu);
733
734 return FALSE;
735 }
736
737 void
738 gaim_gtk_account_option_menu_set_selected(GtkWidget *optmenu, GaimAccount *account)
739 {
740 GtkWidget *menu;
741 GtkWidget *item;
742 gboolean show_all;
743 GaimAccount *curaccount;
744 GaimFilterAccountFunc filter_func;
745
746 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
747 item = gtk_menu_get_active(GTK_MENU(menu));
748 curaccount = g_object_get_data(G_OBJECT(item), "account");
749
750 if (account == curaccount)
751 return;
752
753 show_all = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(optmenu),
754 "show_all"));
755
756 filter_func = g_object_get_data(G_OBJECT(optmenu),
757 "filter_func");
758
759 gtk_option_menu_remove_menu(GTK_OPTION_MENU(optmenu));
760
761 create_account_menu(optmenu, account, filter_func, show_all);
762 }
763
764 GtkWidget *
765 gaim_gtk_account_option_menu_new(GaimAccount *default_account,
766 gboolean show_all, GCallback cb,
767 GaimFilterAccountFunc filter_func,
768 gpointer user_data)
769 {
770 GtkWidget *optmenu;
771
772 /* Create the option menu */
773 optmenu = gtk_option_menu_new();
774 gtk_widget_show(optmenu);
775
776 g_signal_connect(G_OBJECT(optmenu), "destroy",
777 G_CALLBACK(account_menu_destroyed_cb), NULL);
778
779 /* Register the gaim sign on/off event callbacks. */
780 gaim_signal_connect(gaim_connections_get_handle(), "signed-on",
781 optmenu, GAIM_CALLBACK(account_menu_sign_on_off_cb),
782 optmenu);
783 gaim_signal_connect(gaim_connections_get_handle(), "signed-off",
784 optmenu, GAIM_CALLBACK(account_menu_sign_on_off_cb),
785 optmenu);
786 gaim_signal_connect(gaim_accounts_get_handle(), "account-added",
787 optmenu, GAIM_CALLBACK(account_menu_added_removed_cb),
788 optmenu);
789 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed",
790 optmenu, GAIM_CALLBACK(account_menu_added_removed_cb),
791 optmenu);
792
793 /* Set some data. */
794 g_object_set_data(G_OBJECT(optmenu), "user_data", user_data);
795 g_object_set_data(G_OBJECT(optmenu), "show_all", GINT_TO_POINTER(show_all));
796 g_object_set_data(G_OBJECT(optmenu), "filter_func",
797 filter_func);
798
799 /* Create and set the actual menu. */
800 create_account_menu(optmenu, default_account, filter_func, show_all);
801
802 /* And now the last callback. */
803 g_signal_connect(G_OBJECT(optmenu), "changed",
804 G_CALLBACK(account_menu_cb), cb);
805
806 return optmenu;
807 }
808
809 gboolean
810 gaim_gtk_check_if_dir(const char *path, GtkFileSelection *filesel)
811 {
812 char *dirname;
813
814 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
815 /* append a / if needed */
816 if (path[strlen(path) - 1] != G_DIR_SEPARATOR) {
817 dirname = g_strconcat(path, G_DIR_SEPARATOR_S, NULL);
818 } else {
819 dirname = g_strdup(path);
820 }
821 gtk_file_selection_set_filename(filesel, dirname);
822 g_free(dirname);
823 return TRUE;
824 }
825
826 return FALSE;
827 }
828
829 void
830 gaim_gtk_setup_gtkspell(GtkTextView *textview)
831 {
832 #ifdef USE_GTKSPELL
833 GError *error = NULL;
834 char *locale = NULL;
835
836 g_return_if_fail(textview != NULL);
837 g_return_if_fail(GTK_IS_TEXT_VIEW(textview));
838
839 if (gtkspell_new_attach(textview, locale, &error) == NULL && error)
840 {
841 gaim_debug_warning("gtkspell", "Failed to setup GtkSpell: %s\n",
842 error->message);
843 g_error_free(error);
844 }
845 #endif /* USE_GTKSPELL */
846 }
847
848 void
849 gaim_gtk_save_accels_cb(GtkAccelGroup *accel_group, guint arg1,
850 GdkModifierType arg2, GClosure *arg3,
851 gpointer data)
852 {
853 gaim_debug(GAIM_DEBUG_MISC, "accels",
854 "accel changed, scheduling save.\n");
855
856 if (!accels_save_timer)
857 accels_save_timer = g_timeout_add(5000, gaim_gtk_save_accels,
858 NULL);
859 }
860
861 gboolean
862 gaim_gtk_save_accels(gpointer data)
863 {
864 char *filename = NULL;
865
866 filename = g_build_filename(gaim_user_dir(), G_DIR_SEPARATOR_S,
867 "accels", NULL);
868 gaim_debug(GAIM_DEBUG_MISC, "accels", "saving accels to %s\n", filename);
869 gtk_accel_map_save(filename);
870 g_free(filename);
871
872 accels_save_timer = 0;
873 return FALSE;
874 }
875
876 void
877 gaim_gtk_load_accels()
878 {
879 char *filename = NULL;
880
881 filename = g_build_filename(gaim_user_dir(), G_DIR_SEPARATOR_S,
882 "accels", NULL);
883 gtk_accel_map_load(filename);
884 g_free(filename);
885 }
886
887 gboolean
888 gaim_gtk_parse_x_im_contact(const char *msg, gboolean all_accounts,
889 GaimAccount **ret_account, char **ret_protocol,
890 char **ret_username, char **ret_alias)
891 {
892 char *protocol = NULL;
893 char *username = NULL;
894 char *alias = NULL;
895 char *str;
896 char *c, *s;
897 gboolean valid;
898
899 g_return_val_if_fail(msg != NULL, FALSE);
900 g_return_val_if_fail(ret_protocol != NULL, FALSE);
901 g_return_val_if_fail(ret_username != NULL, FALSE);
902
903 s = str = g_strdup(msg);
904
905 while (*s != '\r' && *s != '\n' && *s != '\0')
906 {
907 char *key, *value;
908
909 key = s;
910
911 /* Grab the key */
912 while (*s != '\r' && *s != '\n' && *s != '\0' && *s != ' ')
913 s++;
914
915 if (*s == '\r') s++;
916
917 if (*s == '\n')
918 {
919 s++;
920 continue;
921 }
922
923 if (*s != '\0') *s++ = '\0';
924
925 /* Clear past any whitespace */
926 while (*s != '\0' && *s == ' ')
927 s++;
928
929 /* Now let's grab until the end of the line. */
930 value = s;
931
932 while (*s != '\r' && *s != '\n' && *s != '\0')
933 s++;
934
935 if (*s == '\r') *s++ = '\0';
936 if (*s == '\n') *s++ = '\0';
937
938 if ((c = strchr(key, ':')) != NULL)
939 {
940 if (!g_ascii_strcasecmp(key, "X-IM-Username:"))
941 username = g_strdup(value);
942 else if (!g_ascii_strcasecmp(key, "X-IM-Protocol:"))
943 protocol = g_strdup(value);
944 else if (!g_ascii_strcasecmp(key, "X-IM-Alias:"))
945 alias = g_strdup(value);
946 }
947 }
948
949 if (username != NULL && protocol != NULL)
950 {
951 valid = TRUE;
952
953 *ret_username = username;
954 *ret_protocol = protocol;
955
956 if (ret_alias != NULL)
957 *ret_alias = alias;
958
959 /* Check for a compatible account. */
960 if (ret_account != NULL)
961 {
962 GList *list;
963 GaimAccount *account = NULL;
964 GList *l;
965 const char *protoname;
966
967 if (all_accounts)
968 list = gaim_accounts_get_all();
969 else
970 list = gaim_connections_get_all();
971
972 for (l = list; l != NULL; l = l->next)
973 {
974 GaimConnection *gc;
975 GaimPluginProtocolInfo *prpl_info = NULL;
976 GaimPlugin *plugin;
977
978 if (all_accounts)
979 {
980 account = (GaimAccount *)l->data;
981
982 plugin = gaim_plugins_find_with_id(
983 gaim_account_get_protocol_id(account));
984
985 if (plugin == NULL)
986 {
987 account = NULL;
988
989 continue;
990 }
991
992 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
993 }
994 else
995 {
996 gc = (GaimConnection *)l->data;
997 account = gaim_connection_get_account(gc);
998
999 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
1000 }
1001
1002 protoname = prpl_info->list_icon(account, NULL);
1003
1004 if (!strcmp(protoname, protocol))
1005 break;
1006
1007 account = NULL;
1008 }
1009
1010 /* Special case for AIM and ICQ */
1011 if (account == NULL && (!strcmp(protocol, "aim") ||
1012 !strcmp(protocol, "icq")))
1013 {
1014 for (l = list; l != NULL; l = l->next)
1015 {
1016 GaimConnection *gc;
1017 GaimPluginProtocolInfo *prpl_info = NULL;
1018 GaimPlugin *plugin;
1019
1020 if (all_accounts)
1021 {
1022 account = (GaimAccount *)l->data;
1023
1024 plugin = gaim_plugins_find_with_id(
1025 gaim_account_get_protocol_id(account));
1026
1027 if (plugin == NULL)
1028 {
1029 account = NULL;
1030
1031 continue;
1032 }
1033
1034 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
1035 }
1036 else
1037 {
1038 gc = (GaimConnection *)l->data;
1039 account = gaim_connection_get_account(gc);
1040
1041 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
1042 }
1043
1044 protoname = prpl_info->list_icon(account, NULL);
1045
1046 if (!strcmp(protoname, "aim") || !strcmp(protoname, "icq"))
1047 break;
1048
1049 account = NULL;
1050 }
1051 }
1052
1053 *ret_account = account;
1054 }
1055 }
1056 else
1057 {
1058 valid = FALSE;
1059
1060 g_free(username);
1061 g_free(protocol);
1062 g_free(alias);
1063 }
1064
1065 g_free(str);
1066
1067 return valid;
1068 }
1069
1070 void
1071 gaim_set_accessible_label (GtkWidget *w, GtkWidget *l)
1072 {
1073 AtkObject *acc, *label;
1074 AtkObject *rel_obj[1];
1075 AtkRelationSet *set;
1076 AtkRelation *relation;
1077 const gchar *label_text;
1078 const gchar *existing_name;
1079
1080 acc = gtk_widget_get_accessible (w);
1081 label = gtk_widget_get_accessible (l);
1082
1083 /* If this object has no name, set it's name with the label text */
1084 existing_name = atk_object_get_name (acc);
1085 if (!existing_name) {
1086 label_text = gtk_label_get_text (GTK_LABEL(l));
1087 if (label_text)
1088 atk_object_set_name (acc, label_text);
1089 }
1090
1091 /* Create the labeled-by relation */
1092 set = atk_object_ref_relation_set (acc);
1093 rel_obj[0] = label;
1094 relation = atk_relation_new (rel_obj, 1, ATK_RELATION_LABELLED_BY);
1095 atk_relation_set_add (set, relation);
1096 g_object_unref (relation);
1097
1098 /* Create the label-for relation */
1099 set = atk_object_ref_relation_set (label);
1100 rel_obj[0] = acc;
1101 relation = atk_relation_new (rel_obj, 1, ATK_RELATION_LABEL_FOR);
1102 atk_relation_set_add (set, relation);
1103 g_object_unref (relation);
1104 }
1105
1106 #if GTK_CHECK_VERSION(2,2,0)
1107 static void
1108 gaim_gtk_menu_position_func(GtkMenu *menu,
1109 gint *x,
1110 gint *y,
1111 gboolean *push_in,
1112 gpointer data)
1113 {
1114 GtkWidget *widget;
1115 GtkRequisition requisition;
1116 GdkScreen *screen;
1117 GdkRectangle monitor;
1118 gint monitor_num;
1119 gint space_left, space_right, space_above, space_below;
1120 gint needed_width;
1121 gint needed_height;
1122 gint xthickness;
1123 gint ythickness;
1124 gboolean rtl;
1125
1126 g_return_if_fail(GTK_IS_MENU(menu));
1127
1128 widget = GTK_WIDGET(menu);
1129 screen = gtk_widget_get_screen(widget);
1130 xthickness = widget->style->xthickness;
1131 ythickness = widget->style->ythickness;
1132 rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
1133
1134 /*
1135 * We need the requisition to figure out the right place to
1136 * popup the menu. In fact, we always need to ask here, since
1137 * if a size_request was queued while we weren't popped up,
1138 * the requisition won't have been recomputed yet.
1139 */
1140 gtk_widget_size_request (widget, &requisition);
1141
1142 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
1143
1144 push_in = FALSE;
1145
1146 /*
1147 * The placement of popup menus horizontally works like this (with
1148 * RTL in parentheses)
1149 *
1150 * - If there is enough room to the right (left) of the mouse cursor,
1151 * position the menu there.
1152 *
1153 * - Otherwise, if if there is enough room to the left (right) of the
1154 * mouse cursor, position the menu there.
1155 *
1156 * - Otherwise if the menu is smaller than the monitor, position it
1157 * on the side of the mouse cursor that has the most space available
1158 *
1159 * - Otherwise (if there is simply not enough room for the menu on the
1160 * monitor), position it as far left (right) as possible.
1161 *
1162 * Positioning in the vertical direction is similar: first try below
1163 * mouse cursor, then above.
1164 */
1165 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1166
1167 space_left = *x - monitor.x;
1168 space_right = monitor.x + monitor.width - *x - 1;
1169 space_above = *y - monitor.y;
1170 space_below = monitor.y + monitor.height - *y - 1;
1171
1172 /* position horizontally */
1173
1174 /* the amount of space we need to position the menu. Note the
1175 * menu is offset "xthickness" pixels
1176 */
1177 needed_width = requisition.width - xthickness;
1178
1179 if (needed_width <= space_left ||
1180 needed_width <= space_right)
1181 {
1182 if ((rtl && needed_width <= space_left) ||
1183 (!rtl && needed_width > space_right))
1184 {
1185 /* position left */
1186 *x = *x + xthickness - requisition.width + 1;
1187 }
1188 else
1189 {
1190 /* position right */
1191 *x = *x - xthickness;
1192 }
1193
1194 /* x is clamped on-screen further down */
1195 }
1196 else if (requisition.width <= monitor.width)
1197 {
1198 /* the menu is too big to fit on either side of the mouse
1199 * cursor, but smaller than the monitor. Position it on
1200 * the side that has the most space
1201 */
1202 if (space_left > space_right)
1203 {
1204 /* left justify */
1205 *x = monitor.x;
1206 }
1207 else
1208 {
1209 /* right justify */
1210 *x = monitor.x + monitor.width - requisition.width;
1211 }
1212 }
1213 else /* menu is simply too big for the monitor */
1214 {
1215 if (rtl)
1216 {
1217 /* right justify */
1218 *x = monitor.x + monitor.width - requisition.width;
1219 }
1220 else
1221 {
1222 /* left justify */
1223 *x = monitor.x;
1224 }
1225 }
1226
1227 /* Position vertically. The algorithm is the same as above, but
1228 * simpler because we don't have to take RTL into account.
1229 */
1230 needed_height = requisition.height - ythickness;
1231
1232 if (needed_height <= space_above ||
1233 needed_height <= space_below)
1234 {
1235 if (needed_height <= space_below)
1236 *y = *y - ythickness;
1237 else
1238 *y = *y + ythickness - requisition.height + 1;
1239
1240 *y = CLAMP (*y, monitor.y,
1241 monitor.y + monitor.height - requisition.height);
1242 }
1243 else if (needed_height > space_below && needed_height > space_above)
1244 {
1245 if (space_below >= space_above)
1246 *y = monitor.y + monitor.height - requisition.height;
1247 else
1248 *y = monitor.y;
1249 }
1250 else
1251 {
1252 *y = monitor.y;
1253 }
1254 }
1255
1256 #endif
1257
1258 void
1259 gaim_gtk_treeview_popup_menu_position_func(GtkMenu *menu,
1260 gint *x,
1261 gint *y,
1262 gboolean *push_in,
1263 gpointer data)
1264 {
1265 GtkWidget *widget = GTK_WIDGET(data);
1266 GtkTreeView *tv = GTK_TREE_VIEW(data);
1267 GtkTreePath *path;
1268 GtkTreeViewColumn *col;
1269 GdkRectangle rect;
1270 gint ythickness = GTK_WIDGET(menu)->style->ythickness;
1271
1272 gdk_window_get_origin (widget->window, x, y);
1273 gtk_tree_view_get_cursor (tv, &path, &col);
1274 gtk_tree_view_get_cell_area (tv, path, col, &rect);
1275
1276 *x += rect.x+rect.width;
1277 *y += rect.y+rect.height+ythickness;
1278 #if GTK_CHECK_VERSION(2,2,0)
1279 gaim_gtk_menu_position_func (menu, x, y, push_in, data);
1280 #endif
1281 }
1282
1283 enum {
1284 DND_FILE_TRANSFER,
1285 DND_IM_IMAGE,
1286 DND_BUDDY_ICON
1287 };
1288
1289 typedef struct {
1290 char *filename;
1291 GaimAccount *account;
1292 char *who;
1293 } _DndData;
1294
1295 static void dnd_image_ok_callback(_DndData *data, int choice)
1296 {
1297 char *filedata;
1298 size_t size;
1299 struct stat st;
1300 GError *err = NULL;
1301 GaimConversation *conv;
1302 GaimGtkConversation *gtkconv;
1303 GtkTextIter iter;
1304 int id;
1305 switch (choice) {
1306 case DND_BUDDY_ICON:
1307 if (g_stat(data->filename, &st)) {
1308 char *str;
1309
1310 str = g_strdup_printf(_("The following error has occurred loading %s: %s"),
1311 data->filename, strerror(errno));
1312 gaim_notify_error(NULL, NULL,
1313 _("Failed to load image"),
1314 str);
1315 g_free(str);
1316
1317 return;
1318 }
1319
1320 gaim_gtk_set_custom_buddy_icon(data->account, data->who, data->filename);
1321 break;
1322 case DND_FILE_TRANSFER:
1323 serv_send_file(gaim_account_get_connection(data->account), data->who, data->filename);
1324 break;
1325 case DND_IM_IMAGE:
1326 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, data->account, data->who);
1327 gtkconv = GAIM_GTK_CONVERSATION(conv);
1328
1329 if (!g_file_get_contents(data->filename, &filedata, &size,
1330 &err)) {
1331 char *str;
1332
1333 str = g_strdup_printf(_("The following error has occurred loading %s: %s"), data->filename, err->message);
1334 gaim_notify_error(NULL, NULL,
1335 _("Failed to load image"),
1336 str);
1337
1338 g_error_free(err);
1339 g_free(str);
1340
1341 return;
1342 }
1343 id = gaim_imgstore_add(filedata, size, data->filename);
1344 g_free(filedata);
1345
1346 gtk_text_buffer_get_iter_at_mark(GTK_IMHTML(gtkconv->entry)->text_buffer, &iter,
1347 gtk_text_buffer_get_insert(GTK_IMHTML(gtkconv->entry)->text_buffer));
1348 gtk_imhtml_insert_image_at_iter(GTK_IMHTML(gtkconv->entry), id, &iter);
1349 gaim_imgstore_unref(id);
1350
1351 break;
1352 }
1353 free(data->filename);
1354 free(data->who);
1355 free(data);
1356 }
1357
1358 static void dnd_image_cancel_callback(_DndData *data, int choice)
1359 {
1360 free(data->filename);
1361 free(data->who);
1362 free(data);
1363 }
1364
1365 static void dnd_set_icon_ok_cb(_DndData *data)
1366 {
1367 dnd_image_ok_callback(data, DND_BUDDY_ICON);
1368 }
1369
1370 static void dnd_set_icon_cancel_cb(_DndData *data)
1371 {
1372 free(data->filename);
1373 free(data->who);
1374 free(data);
1375 }
1376
1377 void
1378 gaim_dnd_file_manage(GtkSelectionData *sd, GaimAccount *account, const char *who)
1379 {
1380 GList *tmp;
1381 GdkPixbuf *pb;
1382 GList *files = gaim_uri_list_extract_filenames((const gchar *)sd->data);
1383 GaimConnection *gc = gaim_account_get_connection(account);
1384 GaimPluginProtocolInfo *prpl_info = NULL;
1385 gboolean file_send_ok = FALSE;
1386 #ifndef _WIN32
1387 GaimDesktopItem *item;
1388 #endif
1389
1390 g_return_if_fail(account != NULL);
1391 g_return_if_fail(who != NULL);
1392
1393 for(tmp = files; tmp != NULL ; tmp = g_list_next(tmp)) {
1394 gchar *filename = tmp->data;
1395 gchar *basename = g_path_get_basename(filename);
1396
1397 /* Set the default action: don't send anything */
1398 file_send_ok = FALSE;
1399
1400 /* XXX - Make ft API support creating a transfer with more than one file */
1401 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
1402 continue;
1403 }
1404
1405 /* XXX - make ft api suupport sending a directory */
1406 /* Are we dealing with a directory? */
1407 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
1408 char *str;
1409
1410 str = g_strdup_printf(_("Cannot send folder %s."), basename);
1411 gaim_notify_error(NULL, NULL,
1412 str,_("Gaim cannot transfer a folder. You will need to send the files within individually"));
1413
1414 g_free(str);
1415
1416 continue;
1417 }
1418
1419 /* Are we dealing with an image? */
1420 pb = gdk_pixbuf_new_from_file(filename, NULL);
1421 if (pb) {
1422 _DndData *data = g_malloc(sizeof(_DndData));
1423 gboolean ft = FALSE, im = FALSE;
1424
1425 data->who = g_strdup(who);
1426 data->filename = g_strdup(filename);
1427 data->account = account;
1428
1429 if (gc)
1430 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
1431
1432 if (prpl_info && prpl_info->options & OPT_PROTO_IM_IMAGE)
1433 im = TRUE;
1434
1435 if (prpl_info && prpl_info->can_receive_file)
1436 ft = prpl_info->can_receive_file(gc, who);
1437
1438 if (im && ft)
1439 gaim_request_choice(NULL, NULL,
1440 _("You have dragged an image"),
1441 _("You can send this image as a file transfer, "
1442 "embed it into this message, or use it as the buddy icon for this user."),
1443 DND_FILE_TRANSFER, "OK", (GCallback)dnd_image_ok_callback,
1444 "Cancel", (GCallback)dnd_image_cancel_callback, data,
1445 _("Set as buddy icon"), DND_BUDDY_ICON,
1446 _("Send image file"), DND_FILE_TRANSFER,
1447 _("Insert in message"), DND_IM_IMAGE, NULL);
1448 else if (!(im || ft))
1449 gaim_request_yes_no(NULL, NULL, _("You have dragged an image"),
1450 _("Would you like to set it as the buddy icon for this user?"),
1451 0, data, (GCallback)dnd_set_icon_ok_cb, (GCallback)dnd_set_icon_cancel_cb);
1452 else
1453 gaim_request_choice(NULL, NULL,
1454 _("You have dragged an image"),
1455 ft ? _("You can send this image as a file transfer or "
1456 "embed it into this message, or use it as the buddy icon for this user.") :
1457 _("You can insert this image into this message, or use it as the buddy icon for this user"),
1458 ft ? DND_FILE_TRANSFER : DND_IM_IMAGE, "OK", (GCallback)dnd_image_ok_callback,
1459 "Cancel", (GCallback)dnd_image_cancel_callback, data,
1460 _("Set as buddy icon"), DND_BUDDY_ICON,
1461 ft ? _("Send image file") : _("Insert in message"), ft ? DND_FILE_TRANSFER : DND_IM_IMAGE, NULL);
1462 return;
1463 }
1464
1465 #ifndef _WIN32
1466 /* Are we trying to send a .desktop file? */
1467 else if (gaim_str_has_suffix(basename, ".desktop") && (item = gaim_desktop_item_new_from_file(filename))) {
1468 GaimDesktopItemType dtype;
1469 char key[64];
1470 const char *itemname = NULL;
1471
1472 #if GTK_CHECK_VERSION(2,6,0)
1473 const char * const *langs;
1474 int i;
1475 langs = g_get_language_names();
1476 for (i = 0; langs[i]; i++) {
1477 g_snprintf(key, sizeof(key), "Name[%s]", langs[i]);
1478 itemname = gaim_desktop_item_get_string(item, key);
1479 break;
1480 }
1481 #else
1482 const char *lang = g_getenv("LANG");
1483 char *dot;
1484 dot = strchr(lang, '.');
1485 if (dot)
1486 *dot = '\0';
1487 g_snprintf(key, sizeof(key), "Name[%s]", lang);
1488 itemname = gaim_desktop_item_get_string(item, key);
1489 #endif
1490 if (!itemname)
1491 itemname = gaim_desktop_item_get_string(item, "Name");
1492
1493 dtype = gaim_desktop_item_get_entry_type(item);
1494 switch (dtype) {
1495 GaimConversation *conv;
1496 GaimGtkConversation *gtkconv;
1497
1498 case GAIM_DESKTOP_ITEM_TYPE_LINK:
1499 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, who);
1500 gtkconv = GAIM_GTK_CONVERSATION(conv);
1501 gtk_imhtml_insert_link(GTK_IMHTML(gtkconv->entry),
1502 gtk_text_buffer_get_insert(GTK_IMHTML(gtkconv->entry)->text_buffer),
1503 gaim_desktop_item_get_string(item, "URL"), itemname);
1504 break;
1505 default:
1506 /* I don't know if we really want to do anything here. Most of the desktop item types are crap like
1507 * "MIME Type" (I have no clue how that would be a desktop item) and "Comment"... nothing we can really
1508 * send. The only logical one is "Application," but do we really want to send a binary and nothing else?
1509 * Probably not. I'll just give an error and return. */
1510 /* The original patch sent the icon used by the launcher. That's probably wrong */
1511 gaim_notify_error(NULL, NULL, _("Cannot send launcher"), _("You dragged a desktop launcher. "
1512 "Most likely you wanted to send whatever this launcher points to instead of this launcher"
1513 " itself."));
1514 break;
1515 }
1516 gaim_desktop_item_unref(item);
1517 return;
1518 }
1519 #endif /* _WIN32 */
1520
1521 /* Everything is fine, let's send */
1522 serv_send_file(gc, who, filename);
1523 g_free(filename);
1524 }
1525 g_list_free(files);
1526 }
1527
1528 void gaim_gtk_buddy_icon_get_scale_size(GdkPixbuf *buf, GaimBuddyIconSpec *spec, GaimIconScaleRules rules, int *width, int *height)
1529 {
1530 *width = gdk_pixbuf_get_width(buf);
1531 *height = gdk_pixbuf_get_height(buf);
1532
1533 if ((spec == NULL) || !(spec->scale_rules & rules))
1534 return;
1535
1536 gaim_buddy_icon_get_scale_size(spec, width, height);
1537
1538 /* and now for some arbitrary sanity checks */
1539 if(*width > 100)
1540 *width = 100;
1541 if(*height > 100)
1542 *height = 100;
1543 }
1544
1545 GdkPixbuf *
1546 gaim_gtk_create_prpl_icon(GaimAccount *account, double scale_factor)
1547 {
1548 GaimPlugin *prpl;
1549 GaimPluginProtocolInfo *prpl_info;
1550 const char *protoname = NULL;
1551 char buf[256]; /* TODO: We should use a define for max file length */
1552 char *filename = NULL;
1553 GdkPixbuf *pixbuf, *scaled;
1554
1555 g_return_val_if_fail(account != NULL, NULL);
1556
1557 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
1558 if (prpl == NULL)
1559 return NULL;
1560
1561 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl);
1562 if (prpl_info->list_icon == NULL)
1563 return NULL;
1564
1565 protoname = prpl_info->list_icon(account, NULL);
1566 if (protoname == NULL)
1567 return NULL;
1568
1569 /*
1570 * Status icons will be themeable too, and then it will look up
1571 * protoname from the theme
1572 */
1573 g_snprintf(buf, sizeof(buf), "%s.png", protoname);
1574
1575 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status",
1576 "default", buf, NULL);
1577 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
1578 g_free(filename);
1579
1580 scaled = gdk_pixbuf_scale_simple(pixbuf, 32*scale_factor,
1581 32*scale_factor, GDK_INTERP_BILINEAR);
1582 g_object_unref(pixbuf);
1583
1584 return scaled;
1585 }
1586
1587 static GdkPixbuf *
1588 overlay_status_onto_icon(GdkPixbuf *pixbuf, GaimStatusPrimitive primitive)
1589 {
1590 const char *type_name;
1591 char basename[256];
1592 char *filename;
1593 GdkPixbuf *emblem;
1594
1595 type_name = gaim_primitive_get_id_from_type(primitive);
1596
1597 g_snprintf(basename, sizeof(basename), "%s.png", type_name);
1598 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status",
1599 "default", basename, NULL);
1600 emblem = gdk_pixbuf_new_from_file(filename, NULL);
1601 g_free(filename);
1602
1603 if (emblem != NULL) {
1604 int width, height, emblem_width, emblem_height;
1605 int new_emblem_width, new_emblem_height;
1606
1607 width = gdk_pixbuf_get_width(pixbuf);
1608 height = gdk_pixbuf_get_height(pixbuf);
1609 emblem_width = gdk_pixbuf_get_width(emblem);
1610 emblem_height = gdk_pixbuf_get_height(emblem);
1611
1612 /*
1613 * Figure out how big to make the emblem. Normally the emblem
1614 * will have half the width of the pixbuf. But we don't make
1615 * an emblem any smaller than 10 pixels because it becomes
1616 * unrecognizable, unless the width of the pixbuf is less than
1617 * 10 pixels, in which case we make the emblem width the same
1618 * as the pixbuf width.
1619 */
1620 new_emblem_width = MAX(width / 2, MIN(width, 10));
1621 new_emblem_height = MAX(height / 2, MIN(height, 10));
1622
1623 /* Overlay emblem onto the bottom right corner of pixbuf */
1624 gdk_pixbuf_composite(emblem, pixbuf,
1625 width - new_emblem_width, height - new_emblem_height,
1626 new_emblem_width, new_emblem_height,
1627 width - new_emblem_width, height - new_emblem_height,
1628 (double)new_emblem_width / (double)emblem_width,
1629 (double)new_emblem_height / (double)emblem_height,
1630 GDK_INTERP_BILINEAR,
1631 255);
1632 g_object_unref(emblem);
1633 }
1634
1635 return pixbuf;
1636 }
1637
1638 GdkPixbuf *
1639 gaim_gtk_create_prpl_icon_with_status(GaimAccount *account, GaimStatusType *status_type, double scale_factor)
1640 {
1641 GdkPixbuf *pixbuf;
1642
1643 pixbuf = gaim_gtk_create_prpl_icon(account, scale_factor);
1644 if (pixbuf == NULL)
1645 return NULL;
1646
1647 /*
1648 * TODO: Let the prpl pick the emblem on a per status basis,
1649 * and only use the primitive as a fallback?
1650 */
1651
1652 return overlay_status_onto_icon(pixbuf,
1653 gaim_status_type_get_primitive(status_type));
1654 }
1655
1656 GdkPixbuf *
1657 gaim_gtk_create_gaim_icon_with_status(GaimStatusPrimitive primitive, double scale_factor)
1658 {
1659 gchar *filename;
1660 GdkPixbuf *orig, *pixbuf;
1661
1662 filename = g_build_filename(DATADIR, "pixmaps", "gaim.png", NULL);
1663 orig = gdk_pixbuf_new_from_file(filename, NULL);
1664 g_free(filename);
1665 if (orig == NULL)
1666 return NULL;
1667
1668 pixbuf = gdk_pixbuf_scale_simple(orig, 32*scale_factor,
1669 32*scale_factor, GDK_INTERP_BILINEAR);
1670 g_object_unref(G_OBJECT(orig));
1671
1672 return overlay_status_onto_icon(pixbuf, primitive);
1673 }
1674
1675 static void
1676 menu_action_cb(GtkMenuItem *item, gpointer object)
1677 {
1678 gpointer data;
1679 void (*callback)(gpointer, gpointer);
1680
1681 callback = g_object_get_data(G_OBJECT(item), "gaimcallback");
1682 data = g_object_get_data(G_OBJECT(item), "gaimcallbackdata");
1683
1684 if (callback)
1685 callback(object, data);
1686 }
1687
1688 void
1689 gaim_gtk_append_menu_action(GtkWidget *menu, GaimMenuAction *act,
1690 gpointer object)
1691 {
1692 if (act == NULL) {
1693 gaim_separator(menu);
1694 } else {
1695 GtkWidget *menuitem;
1696
1697 if (act->children == NULL) {
1698 menuitem = gtk_menu_item_new_with_mnemonic(act->label);
1699
1700 if (act->callback != NULL) {
1701 g_object_set_data(G_OBJECT(menuitem),
1702 "gaimcallback",
1703 act->callback);
1704 g_object_set_data(G_OBJECT(menuitem),
1705 "gaimcallbackdata",
1706 act->data);
1707 g_signal_connect(G_OBJECT(menuitem), "activate",
1708 G_CALLBACK(menu_action_cb),
1709 object);
1710 } else {
1711 gtk_widget_set_sensitive(menuitem, FALSE);
1712 }
1713
1714 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1715 } else {
1716 GList *l = NULL;
1717 GtkWidget *submenu = NULL;
1718 GtkAccelGroup *group;
1719
1720 menuitem = gtk_menu_item_new_with_mnemonic(act->label);
1721 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1722
1723 submenu = gtk_menu_new();
1724 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1725
1726 group = gtk_menu_get_accel_group(GTK_MENU(menu));
1727 if (group) {
1728 char *path = g_strdup_printf("%s/%s", GTK_MENU_ITEM(menuitem)->accel_path, act->label);
1729 gtk_menu_set_accel_path(GTK_MENU(submenu), path);
1730 g_free(path);
1731 gtk_menu_set_accel_group(GTK_MENU(submenu), group);
1732 }
1733
1734 for (l = act->children; l; l = l->next) {
1735 GaimMenuAction *act = (GaimMenuAction *)l->data;
1736
1737 gaim_gtk_append_menu_action(submenu, act, object);
1738 }
1739 g_list_free(act->children);
1740 act->children = NULL;
1741 }
1742 gaim_menu_action_free(act);
1743 }
1744 }
1745
1746 #if GTK_CHECK_VERSION(2,3,0)
1747 # define NEW_STYLE_COMPLETION
1748 #endif
1749
1750 #ifndef NEW_STYLE_COMPLETION
1751 typedef struct
1752 {
1753 GCompletion *completion;
1754
1755 gboolean completion_started;
1756 gboolean all;
1757
1758 } GaimGtkCompletionData;
1759 #endif
1760
1761 #ifndef NEW_STYLE_COMPLETION
1762 static gboolean
1763 completion_entry_event(GtkEditable *entry, GdkEventKey *event,
1764 GaimGtkCompletionData *data)
1765 {
1766 int pos, end_pos;
1767
1768 if (event->type == GDK_KEY_PRESS && event->keyval == GDK_Tab)
1769 {
1770 gtk_editable_get_selection_bounds(entry, &pos, &end_pos);
1771
1772 if (data->completion_started &&
1773 pos != end_pos && pos > 1 &&
1774 end_pos == strlen(gtk_entry_get_text(GTK_ENTRY(entry))))
1775 {
1776 gtk_editable_select_region(entry, 0, 0);
1777 gtk_editable_set_position(entry, -1);
1778
1779 return TRUE;
1780 }
1781 }
1782 else if (event->type == GDK_KEY_PRESS && event->length > 0)
1783 {
1784 char *prefix, *nprefix;
1785
1786 gtk_editable_get_selection_bounds(entry, &pos, &end_pos);
1787
1788 if (data->completion_started &&
1789 pos != end_pos && pos > 1 &&
1790 end_pos == strlen(gtk_entry_get_text(GTK_ENTRY(entry))))
1791 {
1792 char *temp;
1793
1794 temp = gtk_editable_get_chars(entry, 0, pos);
1795 prefix = g_strconcat(temp, event->string, NULL);
1796 g_free(temp);
1797 }
1798 else if (pos == end_pos && pos > 1 &&
1799 end_pos == strlen(gtk_entry_get_text(GTK_ENTRY(entry))))
1800 {
1801 prefix = g_strconcat(gtk_entry_get_text(GTK_ENTRY(entry)),
1802 event->string, NULL);
1803 }
1804 else
1805 return FALSE;
1806
1807 pos = strlen(prefix);
1808 nprefix = NULL;
1809
1810 g_completion_complete(data->completion, prefix, &nprefix);
1811
1812 if (nprefix != NULL)
1813 {
1814 gtk_entry_set_text(GTK_ENTRY(entry), nprefix);
1815 gtk_editable_set_position(entry, pos);
1816 gtk_editable_select_region(entry, pos, -1);
1817
1818 data->completion_started = TRUE;
1819
1820 g_free(nprefix);
1821 g_free(prefix);
1822
1823 return TRUE;
1824 }
1825
1826 g_free(prefix);
1827 }
1828
1829 return FALSE;
1830 }
1831
1832 static void
1833 destroy_completion_data(GtkWidget *w, GaimGtkCompletionData *data)
1834 {
1835 g_list_foreach(data->completion->items, (GFunc)g_free, NULL);
1836 g_completion_free(data->completion);
1837
1838 g_free(data);
1839 }
1840 #endif /* !NEW_STYLE_COMPLETION */
1841
1842 #ifdef NEW_STYLE_COMPLETION
1843 static gboolean screenname_completion_match_func(GtkEntryCompletion *completion,
1844 const gchar *key, GtkTreeIter *iter, gpointer user_data)
1845 {
1846 GtkTreeModel *model;
1847 GValue val1;
1848 GValue val2;
1849 const char *tmp;
1850
1851 model = gtk_entry_completion_get_model (completion);
1852
1853 val1.g_type = 0;
1854 gtk_tree_model_get_value(model, iter, 2, &val1);
1855 tmp = g_value_get_string(&val1);
1856 if (tmp != NULL && gaim_str_has_prefix(tmp, key))
1857 {
1858 g_value_unset(&val1);
1859 return TRUE;
1860 }
1861 g_value_unset(&val1);
1862
1863 val2.g_type = 0;
1864 gtk_tree_model_get_value(model, iter, 3, &val2);
1865 tmp = g_value_get_string(&val2);
1866 if (tmp != NULL && gaim_str_has_prefix(tmp, key))
1867 {
1868 g_value_unset(&val2);
1869 return TRUE;
1870 }
1871 g_value_unset(&val2);
1872
1873 return FALSE;
1874 }
1875
1876 static gboolean screenname_completion_match_selected_cb(GtkEntryCompletion *completion,
1877 GtkTreeModel *model, GtkTreeIter *iter, gpointer *user_data)
1878 {
1879 GValue val;
1880 GtkWidget *optmenu = user_data[1];
1881 GaimAccount *account;
1882
1883 val.g_type = 0;
1884 gtk_tree_model_get_value(model, iter, 1, &val);
1885 gtk_entry_set_text(GTK_ENTRY(user_data[0]), g_value_get_string(&val));
1886 g_value_unset(&val);
1887
1888 gtk_tree_model_get_value(model, iter, 4, &val);
1889 account = g_value_get_pointer(&val);
1890 g_value_unset(&val);
1891
1892 if (account == NULL)
1893 return TRUE;
1894
1895 if (optmenu != NULL) {
1896 GList *items;
1897 guint index = 0;
1898 gaim_gtk_account_option_menu_set_selected(optmenu, account);
1899 items = GTK_MENU_SHELL(gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu)))->children;
1900
1901 do {
1902 if (account == g_object_get_data(G_OBJECT(items->data), "account")) {
1903 /* Set the account in the GUI. */
1904 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), index);
1905 return TRUE;
1906 }
1907 index++;
1908 } while ((items = items->next) != NULL);
1909 }
1910
1911 return TRUE;
1912 }
1913
1914 static void
1915 add_screenname_autocomplete_entry(GtkListStore *store, const char *buddy_alias, const char *contact_alias,
1916 const GaimAccount *account, const char *screenname)
1917 {
1918 GtkTreeIter iter;
1919 gboolean completion_added = FALSE;
1920 gchar *normalized_screenname;
1921 gchar *tmp;
1922
1923 tmp = g_utf8_normalize(screenname, -1, G_NORMALIZE_DEFAULT);
1924 normalized_screenname = g_utf8_casefold(tmp, -1);
1925 g_free(tmp);
1926
1927 /* There's no sense listing things like: 'xxx "xxx"'
1928 when the screenname and buddy alias match. */
1929 if (buddy_alias && strcmp(buddy_alias, screenname)) {
1930 char *completion_entry = g_strdup_printf("%s \"%s\"", screenname, buddy_alias);
1931 char *tmp2 = g_utf8_normalize(buddy_alias, -1, G_NORMALIZE_DEFAULT);
1932
1933 tmp = g_utf8_casefold(tmp2, -1);
1934 g_free(tmp2);
1935
1936 gtk_list_store_append(store, &iter);
1937 gtk_list_store_set(store, &iter,
1938 0, completion_entry,
1939 1, screenname,
1940 2, normalized_screenname,
1941 3, tmp,
1942 4, account,
1943 -1);
1944 g_free(completion_entry);
1945 g_free(tmp);
1946 completion_added = TRUE;
1947 }
1948
1949 /* There's no sense listing things like: 'xxx "xxx"'
1950 when the screenname and contact alias match. */
1951 if (contact_alias && strcmp(contact_alias, screenname)) {
1952 /* We don't want duplicates when the contact and buddy alias match. */
1953 if (!buddy_alias || strcmp(contact_alias, buddy_alias)) {
1954 char *completion_entry = g_strdup_printf("%s \"%s\"",
1955 screenname, contact_alias);
1956 char *tmp2 = g_utf8_normalize(contact_alias, -1, G_NORMALIZE_DEFAULT);
1957
1958 tmp = g_utf8_casefold(tmp2, -1);
1959 g_free(tmp2);
1960
1961 gtk_list_store_append(store, &iter);
1962 gtk_list_store_set(store, &iter,
1963 0, completion_entry,
1964 1, screenname,
1965 2, normalized_screenname,
1966 3, tmp,
1967 4, account,
1968 -1);
1969 g_free(completion_entry);
1970 g_free(tmp);
1971 completion_added = TRUE;
1972 }
1973 }
1974
1975 if (completion_added == FALSE) {
1976 /* Add the buddy's screenname. */
1977 gtk_list_store_append(store, &iter);
1978 gtk_list_store_set(store, &iter,
1979 0, screenname,
1980 1, screenname,
1981 2, normalized_screenname,
1982 3, NULL,
1983 4, account,
1984 -1);
1985 }
1986
1987 g_free(normalized_screenname);
1988 }
1989 #endif /* NEW_STYLE_COMPLETION */
1990
1991 static void get_log_set_name(GaimLogSet *set, gpointer value, gpointer **set_hash_data)
1992 {
1993 /* 1. Don't show buddies because we will have gotten them already.
1994 * 2. Only show those with non-NULL accounts that are currently connected.
1995 * 3. The boxes that use this autocomplete code handle only IMs. */
1996 if (!set->buddy &&
1997 (GPOINTER_TO_INT(set_hash_data[1]) ||
1998 (set->account != NULL && gaim_account_is_connected(set->account))) &&
1999 set->type == GAIM_LOG_IM) {
2000 #ifdef NEW_STYLE_COMPLETION
2001 add_screenname_autocomplete_entry((GtkListStore *)set_hash_data[0],
2002 NULL, NULL, set->account, set->name);
2003 #else
2004 GList **items = ((GList **)set_hash_data[0]);
2005 /* Steal the name for the GCompletion. */
2006 *items = g_list_append(*items, set->name);
2007 set->name = set->normalized_name = NULL;
2008 #endif /* NEW_STYLE_COMPLETION */
2009 }
2010 }
2011
2012 #ifdef NEW_STYLE_COMPLETION
2013 static void
2014 add_completion_list(GtkListStore *store)
2015 {
2016 GaimBlistNode *gnode, *cnode, *bnode;
2017 gboolean all = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(store), "screenname-all"));
2018 GHashTable *sets;
2019 gpointer set_hash_data[] = {store, GINT_TO_POINTER(all)};
2020
2021 gtk_list_store_clear(store);
2022
2023 for (gnode = gaim_get_blist()->root; gnode != NULL; gnode = gnode->next)
2024 {
2025 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
2026 continue;
2027
2028 for (cnode = gnode->child; cnode != NULL; cnode = cnode->next)
2029 {
2030 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
2031 continue;
2032
2033 for (bnode = cnode->child; bnode != NULL; bnode = bnode->next)
2034 {
2035 GaimBuddy *buddy = (GaimBuddy *)bnode;
2036
2037 if (!all && !gaim_account_is_connected(buddy->account))
2038 continue;
2039
2040 add_screenname_autocomplete_entry(store,
2041 ((GaimContact *)cnode)->alias,
2042 gaim_buddy_get_contact_alias(buddy),
2043 buddy->account,
2044 buddy->name
2045 );
2046 }
2047 }
2048 }
2049
2050 sets = gaim_log_get_log_sets();
2051 g_hash_table_foreach(sets, (GHFunc)get_log_set_name, &set_hash_data);
2052 g_hash_table_destroy(sets);
2053 }
2054 #else
2055 static void
2056 add_completion_list(GaimGtkCompletionData *data)
2057 {
2058 GaimBlistNode *gnode, *cnode, *bnode;
2059 GCompletion *completion;
2060 GList *item = g_list_append(NULL, NULL);
2061 GHashTable *sets;
2062 gpointer set_hash_data[2];
2063
2064 completion = data->completion;
2065
2066 g_list_foreach(completion->items, (GFunc)g_free, NULL);
2067 g_completion_clear_items(completion);
2068
2069 for (gnode = gaim_get_blist()->root; gnode != NULL; gnode = gnode->next)
2070 {
2071 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
2072 continue;
2073
2074 for (cnode = gnode->child; cnode != NULL; cnode = cnode->next)
2075 {
2076 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
2077 continue;
2078
2079 for (bnode = cnode->child; bnode != NULL; bnode = bnode->next)
2080 {
2081 GaimBuddy *buddy = (GaimBuddy *)bnode;
2082
2083 if (!data->all && !gaim_account_is_connected(buddy->account))
2084 continue;
2085
2086 item->data = g_strdup(buddy->name);
2087 g_completion_add_items(data->completion, item);
2088 }
2089 }
2090 }
2091 g_list_free(item);
2092
2093 sets = gaim_log_get_log_sets();
2094 item = NULL;
2095 set_hash_data[0] = &item;
2096 set_hash_data[1] = GINT_TO_POINTER(data->all);
2097 g_hash_table_foreach(sets, (GHFunc)get_log_set_name, &set_hash_data);
2098 g_hash_table_destroy(sets);
2099 g_completion_add_items(data->completion, item);
2100 g_list_free(item);
2101 }
2102 #endif
2103
2104 static void
2105 screenname_autocomplete_destroyed_cb(GtkWidget *widget, gpointer data)
2106 {
2107 gaim_signals_disconnect_by_handle(widget);
2108 }
2109
2110 static void
2111 repopulate_autocomplete(gpointer something, gpointer data)
2112 {
2113 add_completion_list(data);
2114 }
2115
2116 void
2117 gaim_gtk_setup_screenname_autocomplete(GtkWidget *entry, GtkWidget *accountopt, gboolean all)
2118 {
2119 gpointer cb_data = NULL;
2120
2121 #ifdef NEW_STYLE_COMPLETION
2122 /* Store the displayed completion value, the screenname, the UTF-8 normalized & casefolded screenname,
2123 * the UTF-8 normalized & casefolded value for comparison, and the account. */
2124 GtkListStore *store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
2125
2126 GtkEntryCompletion *completion;
2127 gpointer *data;
2128
2129 g_object_set_data(G_OBJECT(store), "screenname-all", GINT_TO_POINTER(all));
2130 add_completion_list(store);
2131
2132 cb_data = store;
2133
2134 /* Sort the completion list by screenname. */
2135 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
2136 1, GTK_SORT_ASCENDING);
2137
2138 completion = gtk_entry_completion_new();
2139 gtk_entry_completion_set_match_func(completion, screenname_completion_match_func, NULL, NULL);
2140
2141 data = g_new0(gpointer, 2);
2142 data[0] = entry;
2143 data[1] = accountopt;
2144 g_signal_connect(G_OBJECT(completion), "match-selected",
2145 G_CALLBACK(screenname_completion_match_selected_cb), data);
2146
2147 gtk_entry_set_completion(GTK_ENTRY(entry), completion);
2148 g_object_unref(completion);
2149
2150 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(store));
2151 g_object_unref(store);
2152
2153 gtk_entry_completion_set_text_column(completion, 0);
2154
2155 #else /* !NEW_STYLE_COMPLETION */
2156 GaimGtkCompletionData *data;
2157
2158 data = g_new0(GaimGtkCompletionData, 1);
2159
2160 data->completion = g_completion_new(NULL);
2161 data->all = all;
2162
2163 g_completion_set_compare(data->completion, g_ascii_strncasecmp);
2164
2165 add_completion_list(data);
2166 cb_data = data;
2167
2168 g_signal_connect(G_OBJECT(entry), "event",
2169 G_CALLBACK(completion_entry_event), data);
2170 g_signal_connect(G_OBJECT(entry), "destroy",
2171 G_CALLBACK(destroy_completion_data), data);
2172
2173 #endif /* !NEW_STYLE_COMPLETION */
2174
2175 if (!all)
2176 {
2177 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", entry,
2178 GAIM_CALLBACK(repopulate_autocomplete), cb_data);
2179 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", entry,
2180 GAIM_CALLBACK(repopulate_autocomplete), cb_data);
2181 }
2182
2183 gaim_signal_connect(gaim_accounts_get_handle(), "account-added", entry,
2184 GAIM_CALLBACK(repopulate_autocomplete), cb_data);
2185 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed", entry,
2186 GAIM_CALLBACK(repopulate_autocomplete), cb_data);
2187
2188 g_signal_connect(G_OBJECT(entry), "destroy", G_CALLBACK(screenname_autocomplete_destroyed_cb), NULL);
2189 }
2190
2191 void gaim_gtk_set_cursor(GtkWidget *widget, GdkCursorType cursor_type)
2192 {
2193 GdkCursor *cursor;
2194
2195 g_return_if_fail(widget != NULL);
2196 if (widget->window == NULL)
2197 return;
2198
2199 cursor = gdk_cursor_new(GDK_WATCH);
2200 gdk_window_set_cursor(widget->window, cursor);
2201 gdk_cursor_unref(cursor);
2202
2203 #if GTK_CHECK_VERSION(2,4,0)
2204 gdk_display_flush(gdk_drawable_get_display(GDK_DRAWABLE(widget->window)));
2205 #else
2206 gdk_flush();
2207 #endif
2208 }
2209
2210 void gaim_gtk_clear_cursor(GtkWidget *widget)
2211 {
2212 g_return_if_fail(widget != NULL);
2213 if (widget->window == NULL)
2214 return;
2215
2216 gdk_window_set_cursor(widget->window, NULL);
2217 }
2218
2219 struct _icon_chooser {
2220 GtkWidget *icon_filesel;
2221 GtkWidget *icon_preview;
2222 GtkWidget *icon_text;
2223
2224 void (*callback)(const char*,gpointer);
2225 gpointer data;
2226 };
2227
2228 #if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2229 static void
2230 icon_filesel_delete_cb(GtkWidget *w, struct _icon_chooser *dialog)
2231 {
2232 if (dialog->icon_filesel != NULL)
2233 gtk_widget_destroy(dialog->icon_filesel);
2234
2235 if (dialog->callback)
2236 dialog->callback(NULL, dialog->data);
2237
2238 g_free(dialog);
2239 }
2240 #endif /* FILECHOOSER */
2241
2242
2243
2244 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2245 static void
2246 icon_filesel_choose_cb(GtkWidget *widget, gint response, struct _icon_chooser *dialog)
2247 {
2248 char *filename, *current_folder;
2249
2250 if (response != GTK_RESPONSE_ACCEPT) {
2251 if (response == GTK_RESPONSE_CANCEL) {
2252 gtk_widget_destroy(dialog->icon_filesel);
2253 }
2254 dialog->icon_filesel = NULL;
2255 if (dialog->callback)
2256 dialog->callback(NULL, dialog->data);
2257 g_free(dialog);
2258 return;
2259 }
2260
2261 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog->icon_filesel));
2262 current_folder = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog->icon_filesel));
2263 if (current_folder != NULL) {
2264 gaim_prefs_set_path("/gaim/gtk/filelocations/last_icon_folder", current_folder);
2265 g_free(current_folder);
2266 }
2267
2268 #else /* FILECHOOSER */
2269 static void
2270 icon_filesel_choose_cb(GtkWidget *w, struct _icon_chooser *dialog)
2271 {
2272 char *filename, *current_folder;
2273
2274 filename = g_strdup(gtk_file_selection_get_filename(
2275 GTK_FILE_SELECTION(dialog->icon_filesel)));
2276
2277 /* If they typed in a directory, change there */
2278 if (gaim_gtk_check_if_dir(filename,
2279 GTK_FILE_SELECTION(dialog->icon_filesel)))
2280 {
2281 g_free(filename);
2282 return;
2283 }
2284
2285 current_folder = g_path_get_dirname(filename);
2286 if (current_folder != NULL) {
2287 gaim_prefs_set_path("/gaim/gtk/filelocations/last_icon_folder", current_folder);
2288 g_free(current_folder);
2289 }
2290
2291 #endif /* FILECHOOSER */
2292 if (dialog->callback)
2293 dialog->callback(filename, dialog->data);
2294 gtk_widget_destroy(dialog->icon_filesel);
2295 g_free(filename);
2296 g_free(dialog);
2297 }
2298
2299
2300 static void
2301 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2302 icon_preview_change_cb(GtkFileChooser *widget, struct _icon_chooser *dialog)
2303 #else /* FILECHOOSER */
2304 icon_preview_change_cb(GtkTreeSelection *sel, struct _icon_chooser *dialog)
2305 #endif /* FILECHOOSER */
2306 {
2307 GdkPixbuf *pixbuf, *scale;
2308 int height, width;
2309 char *basename, *markup, *size;
2310 struct stat st;
2311 char *filename;
2312
2313 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2314 filename = gtk_file_chooser_get_preview_filename(
2315 GTK_FILE_CHOOSER(dialog->icon_filesel));
2316 #else /* FILECHOOSER */
2317 filename = g_strdup(gtk_file_selection_get_filename(
2318 GTK_FILE_SELECTION(dialog->icon_filesel)));
2319 #endif /* FILECHOOSER */
2320
2321 if (!filename || g_stat(filename, &st))
2322 {
2323 g_free(filename);
2324 return;
2325 }
2326
2327 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
2328 if (!pixbuf) {
2329 gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->icon_preview), NULL);
2330 gtk_label_set_markup(GTK_LABEL(dialog->icon_text), "");
2331 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2332 gtk_file_chooser_set_preview_widget_active(
2333 GTK_FILE_CHOOSER(dialog->icon_filesel), FALSE);
2334 #endif /* FILECHOOSER */
2335 g_free(filename);
2336 return;
2337 }
2338
2339 width = gdk_pixbuf_get_width(pixbuf);
2340 height = gdk_pixbuf_get_height(pixbuf);
2341 basename = g_path_get_basename(filename);
2342 size = gaim_str_size_to_units(st.st_size);
2343 markup = g_strdup_printf(_("<b>File:</b> %s\n"
2344 "<b>File size:</b> %s\n"
2345 "<b>Image size:</b> %dx%d"),
2346 basename, size, width, height);
2347
2348 scale = gdk_pixbuf_scale_simple(pixbuf, width * 50 / height,
2349 50, GDK_INTERP_BILINEAR);
2350 gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->icon_preview), scale);
2351 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2352 gtk_file_chooser_set_preview_widget_active(
2353 GTK_FILE_CHOOSER(dialog->icon_filesel), TRUE);
2354 #endif /* FILECHOOSER */
2355 gtk_label_set_markup(GTK_LABEL(dialog->icon_text), markup);
2356
2357 g_object_unref(G_OBJECT(pixbuf));
2358 g_object_unref(G_OBJECT(scale));
2359 g_free(filename);
2360 g_free(basename);
2361 g_free(size);
2362 g_free(markup);
2363 }
2364
2365
2366 GtkWidget *gaim_gtk_buddy_icon_chooser_new(GtkWindow *parent, void(*callback)(const char *, gpointer), gpointer data) {
2367 struct _icon_chooser *dialog = g_new0(struct _icon_chooser, 1);
2368
2369 #if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2370 GtkWidget *hbox;
2371 GtkWidget *tv;
2372 GtkTreeSelection *sel;
2373 #endif /* FILECHOOSER */
2374 const char *current_folder;
2375
2376 dialog->callback = callback;
2377 dialog->data = data;
2378
2379 if (dialog->icon_filesel != NULL) {
2380 gtk_window_present(GTK_WINDOW(dialog->icon_filesel));
2381 return NULL;
2382 }
2383
2384 current_folder = gaim_prefs_get_path("/gaim/gtk/filelocations/last_icon_folder");
2385 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2386
2387 dialog->icon_filesel = gtk_file_chooser_dialog_new(_("Buddy Icon"),
2388 parent,
2389 GTK_FILE_CHOOSER_ACTION_OPEN,
2390 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2391 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
2392 NULL);
2393 gtk_dialog_set_default_response(GTK_DIALOG(dialog->icon_filesel), GTK_RESPONSE_ACCEPT);
2394 if ((current_folder != NULL) && (*current_folder != '\0'))
2395 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog->icon_filesel),
2396 current_folder);
2397
2398 dialog->icon_preview = gtk_image_new();
2399 dialog->icon_text = gtk_label_new(NULL);
2400 gtk_widget_set_size_request(GTK_WIDGET(dialog->icon_preview), -1, 50);
2401 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog->icon_filesel),
2402 GTK_WIDGET(dialog->icon_preview));
2403 g_signal_connect(G_OBJECT(dialog->icon_filesel), "update-preview",
2404 G_CALLBACK(icon_preview_change_cb), dialog);
2405 g_signal_connect(G_OBJECT(dialog->icon_filesel), "response",
2406 G_CALLBACK(icon_filesel_choose_cb), dialog);
2407 icon_preview_change_cb(NULL, dialog);
2408 #else /* FILECHOOSER */
2409 dialog->icon_filesel = gtk_file_selection_new(_("Buddy Icon"));
2410 dialog->icon_preview = gtk_image_new();
2411 dialog->icon_text = gtk_label_new(NULL);
2412 if ((current_folder != NULL) && (*current_folder != '\0'))
2413 gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog->icon_filesel),
2414 current_folder);
2415
2416 gtk_widget_set_size_request(GTK_WIDGET(dialog->icon_preview), -1, 50);
2417 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
2418 gtk_box_pack_start(
2419 GTK_BOX(GTK_FILE_SELECTION(dialog->icon_filesel)->main_vbox),
2420 hbox, FALSE, FALSE, 0);
2421 gtk_box_pack_end(GTK_BOX(hbox), dialog->icon_preview,
2422 FALSE, FALSE, 0);
2423 gtk_box_pack_end(GTK_BOX(hbox), dialog->icon_text, FALSE, FALSE, 0);
2424
2425 tv = GTK_FILE_SELECTION(dialog->icon_filesel)->file_list;
2426 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
2427
2428 g_signal_connect(G_OBJECT(sel), "changed",
2429 G_CALLBACK(icon_preview_change_cb), dialog);
2430 g_signal_connect(
2431 G_OBJECT(GTK_FILE_SELECTION(dialog->icon_filesel)->ok_button),
2432 "clicked",
2433 G_CALLBACK(icon_filesel_choose_cb), dialog);
2434 g_signal_connect(
2435 G_OBJECT(GTK_FILE_SELECTION(dialog->icon_filesel)->cancel_button),
2436 "clicked",
2437 G_CALLBACK(icon_filesel_delete_cb), dialog);
2438 g_signal_connect(G_OBJECT(dialog->icon_filesel), "destroy",
2439 G_CALLBACK(icon_filesel_delete_cb), dialog);
2440 #endif /* FILECHOOSER */
2441 return dialog->icon_filesel;
2442 }
2443
2444
2445 #if GTK_CHECK_VERSION(2,2,0)
2446 static gboolean
2447 str_array_match(char **a, char **b)
2448 {
2449 int i, j;
2450
2451 if (!a || !b)
2452 return FALSE;
2453 for (i = 0; a[i] != NULL; i++)
2454 for (j = 0; b[j] != NULL; j++)
2455 if (!g_ascii_strcasecmp(a[i], b[j]))
2456 return TRUE;
2457 return FALSE;
2458 }
2459 #endif
2460
2461 char *
2462 gaim_gtk_convert_buddy_icon(GaimPlugin *plugin, const char *path)
2463 {
2464 GaimPluginProtocolInfo *prpl_info;
2465 #if GTK_CHECK_VERSION(2,2,0)
2466 char **prpl_formats;
2467 int width, height;
2468 char **pixbuf_formats = NULL;
2469 struct stat st;
2470 GdkPixbufFormat *format;
2471 GdkPixbuf *pixbuf;
2472 #if !GTK_CHECK_VERSION(2,4,0)
2473 GdkPixbufLoader *loader;
2474 #endif
2475 #endif
2476 gchar *contents;
2477 gsize length;
2478 const char *dirname;
2479 char *random;
2480 char *filename;
2481
2482 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
2483
2484 g_return_val_if_fail(prpl_info->icon_spec.format != NULL, NULL);
2485
2486 dirname = gaim_buddy_icons_get_cache_dir();
2487 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
2488 gaim_debug_info("buddyicon", "Creating icon cache directory.\n");
2489
2490 if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
2491 gaim_debug_error("buddyicon",
2492 "Unable to create directory %s: %s\n",
2493 dirname, strerror(errno));
2494 return NULL;
2495 }
2496 }
2497
2498 random = g_strdup_printf("%x", g_random_int());
2499 filename = g_build_filename(dirname, random, NULL);
2500
2501 #if GTK_CHECK_VERSION(2,2,0)
2502 #if GTK_CHECK_VERSION(2,4,0)
2503 format = gdk_pixbuf_get_file_info(path, &width, &height);
2504 #else
2505 loader = gdk_pixbuf_loader_new();
2506 if (g_file_get_contents(path, &contents, &length, NULL)) {
2507 gdk_pixbuf_loader_write(loader, contents, length, NULL);
2508 g_free(contents);
2509 }
2510 gdk_pixbuf_loader_close(loader, NULL);
2511 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
2512 width = gdk_pixbuf_get_width(pixbuf);
2513 height = gdk_pixbuf_get_height(pixbuf);
2514 format = gdk_pixbuf_loader_get_format(loader);
2515 g_object_unref(G_OBJECT(loader));
2516 #endif
2517 if (format == NULL)
2518 return NULL;
2519
2520 pixbuf_formats = gdk_pixbuf_format_get_extensions(format);
2521 prpl_formats = g_strsplit(prpl_info->icon_spec.format,",",0);
2522 if (str_array_match(pixbuf_formats, prpl_formats) && /* This is an acceptable format AND */
2523 (!(prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) || /* The prpl doesn't scale before it sends OR */
2524 (prpl_info->icon_spec.min_width <= width &&
2525 prpl_info->icon_spec.max_width >= width &&
2526 prpl_info->icon_spec.min_height <= height &&
2527 prpl_info->icon_spec.max_height >= height))) /* The icon is the correct size */
2528 #endif
2529 {
2530 FILE *image;
2531
2532 #if GTK_CHECK_VERSION(2,2,0)
2533 g_strfreev(prpl_formats);
2534 g_strfreev(pixbuf_formats);
2535 #endif
2536
2537 /* We don't need to scale the image, so copy it to the cache folder verbatim */
2538
2539 contents = NULL;
2540 if (!g_file_get_contents(path, &contents, &length, NULL) ||
2541 (image = g_fopen(filename, "wb")) == NULL)
2542 {
2543 g_free(random);
2544 g_free(filename);
2545 g_free(contents);
2546 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
2547 g_object_unref(G_OBJECT(pixbuf));
2548 #endif
2549 return NULL;
2550 }
2551
2552 if (fwrite(contents, 1, length, image) != length)
2553 {
2554 fclose(image);
2555 g_unlink(filename);
2556
2557 g_free(random);
2558 g_free(filename);
2559 g_free(contents);
2560 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
2561 g_object_unref(G_OBJECT(pixbuf));
2562 #endif
2563 return NULL;
2564 }
2565 fclose(image);
2566 g_free(contents);
2567
2568 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
2569 g_object_unref(G_OBJECT(pixbuf));
2570 #endif
2571 }
2572 #if GTK_CHECK_VERSION(2,2,0)
2573 else
2574 {
2575 int i;
2576 GError *error = NULL;
2577 GdkPixbuf *scale;
2578 g_strfreev(pixbuf_formats);
2579
2580 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2581 if (error) {
2582 gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message);
2583 g_error_free(error);
2584 g_free(random);
2585 g_free(filename);
2586 g_strfreev(prpl_formats);
2587 return NULL;
2588 }
2589
2590 if ((prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) &&
2591 (width < prpl_info->icon_spec.min_width ||
2592 width > prpl_info->icon_spec.max_width ||
2593 height < prpl_info->icon_spec.min_height ||
2594 height > prpl_info->icon_spec.max_height))
2595 {
2596 int new_width = width;
2597 int new_height = height;
2598
2599 gaim_buddy_icon_get_scale_size(&prpl_info->icon_spec, &new_width, &new_height);
2600
2601 scale = gdk_pixbuf_scale_simple(pixbuf, new_width, new_height,
2602 GDK_INTERP_HYPER);
2603 g_object_unref(G_OBJECT(pixbuf));
2604 pixbuf = scale;
2605 }
2606
2607 for (i = 0; prpl_formats[i]; i++) {
2608 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename);
2609 if (strcmp(prpl_formats[i], "png") == 0) {
2610 if (gdk_pixbuf_save(pixbuf, filename, prpl_formats[i],
2611 &error, "compression", "9", NULL))
2612 /* Success! */
2613 break;
2614 } else if (gdk_pixbuf_save(pixbuf, filename, prpl_formats[i],
2615 &error, NULL)) {
2616 /* Success! */
2617 break;
2618 }
2619 gaim_debug_warning("buddyicon", "Could not convert to %s: %s\n", prpl_formats[i], error->message);
2620 g_error_free(error);
2621 error = NULL;
2622 }
2623 g_strfreev(prpl_formats);
2624 g_object_unref(G_OBJECT(pixbuf));
2625 if (error) {
2626 gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message);
2627 g_error_free(error);
2628 g_free(random);
2629 g_free(filename);
2630 return NULL;
2631 }
2632 }
2633
2634 if (g_stat(filename, &st) != 0) {
2635 gaim_debug_error("buddyicon",
2636 "Could not stat '%s', which we just wrote to disk: %s\n",
2637 filename, strerror(errno));
2638 g_free(random);
2639 g_free(filename);
2640 return NULL;
2641 }
2642
2643 /* Check the file size */
2644 /*
2645 * TODO: If the file is too big, it would be cool if we checked if
2646 * the prpl supported jpeg, and then we could convert to that
2647 * and use a lower quality setting.
2648 */
2649 if ((prpl_info->icon_spec.max_filesize != 0) &&
2650 (st.st_size > prpl_info->icon_spec.max_filesize))
2651 {
2652 gchar *tmp;
2653 tmp = g_strdup_printf(_("The file '%s' is too large for %s. Please try a smaller image.\n"),
2654 path, plugin->info->name);
2655 gaim_notify_error(NULL, _("Icon Error"),
2656 _("Could not set icon"), tmp);
2657 gaim_debug_info("buddyicon",
2658 "'%s' was converted to an image which is %" G_GSIZE_FORMAT
2659 " bytes, but the maximum icon size for %s is %" G_GSIZE_FORMAT
2660 " bytes\n", path, st.st_size, plugin->info->name,
2661 prpl_info->icon_spec.max_filesize);
2662 g_free(tmp);
2663 g_free(random);
2664 g_free(filename);
2665 return NULL;
2666 }
2667
2668 g_free(filename);
2669 return random;
2670 #else
2671 /*
2672 * The chosen icon wasn't the right size, and we're using
2673 * GTK+ 2.0 so we can't scale it.
2674 */
2675 return NULL;
2676 #endif
2677 }
2678
2679 #if !GTK_CHECK_VERSION(2,6,0)
2680 static void
2681 _gdk_file_scale_size_prepared_cb (GdkPixbufLoader *loader,
2682 int width,
2683 int height,
2684 gpointer data)
2685 {
2686 struct {
2687 gint width;
2688 gint height;
2689 gboolean preserve_aspect_ratio;
2690 } *info = data;
2691
2692 g_return_if_fail (width > 0 && height > 0);
2693
2694 if (info->preserve_aspect_ratio &&
2695 (info->width > 0 || info->height > 0)) {
2696 if (info->width < 0)
2697 {
2698 width = width * (double)info->height/(double)height;
2699 height = info->height;
2700 }
2701 else if (info->height < 0)
2702 {
2703 height = height * (double)info->width/(double)width;
2704 width = info->width;
2705 }
2706 else if ((double)height * (double)info->width >
2707 (double)width * (double)info->height) {
2708 width = 0.5 + (double)width * (double)info->height / (double)height;
2709 height = info->height;
2710 } else {
2711 height = 0.5 + (double)height * (double)info->width / (double)width;
2712 width = info->width;
2713 }
2714 } else {
2715 if (info->width > 0)
2716 width = info->width;
2717 if (info->height > 0)
2718 height = info->height;
2719 }
2720
2721 #if GTK_CHECK_VERSION(2,2,0) /* 2.0 users are going to have very strangely sized things */
2722 gdk_pixbuf_loader_set_size (loader, width, height);
2723 #else
2724 #warning nosnilmot could not be bothered to fix this properly for you
2725 #warning ... good luck ... your images may end up strange sizes
2726 #endif
2727 }
2728
2729 GdkPixbuf *
2730 gdk_pixbuf_new_from_file_at_scale(const char *filename, int width, int height,
2731 gboolean preserve_aspect_ratio,
2732 GError **error)
2733 {
2734 GdkPixbufLoader *loader;
2735 GdkPixbuf *pixbuf;
2736 guchar buffer [4096];
2737 int length;
2738 FILE *f;
2739 struct {
2740 gint width;
2741 gint height;
2742 gboolean preserve_aspect_ratio;
2743 } info;
2744 GdkPixbufAnimation *animation;
2745 GdkPixbufAnimationIter *iter;
2746 gboolean has_frame;
2747
2748 g_return_val_if_fail (filename != NULL, NULL);
2749 g_return_val_if_fail (width > 0 || width == -1, NULL);
2750 g_return_val_if_fail (height > 0 || height == -1, NULL);
2751
2752 f = g_fopen (filename, "rb");
2753 if (!f) {
2754 gint save_errno = errno;
2755 gchar *display_name = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2756 g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno),
2757 _("Failed to open file '%s': %s"),
2758 display_name ? display_name : "(unknown)",
2759 g_strerror (save_errno));
2760 g_free (display_name);
2761 return NULL;
2762 }
2763
2764 loader = gdk_pixbuf_loader_new ();
2765
2766 info.width = width;
2767 info.height = height;
2768 info.preserve_aspect_ratio = preserve_aspect_ratio;
2769
2770 g_signal_connect (loader, "size-prepared", G_CALLBACK (_gdk_file_scale_size_prepared_cb), &info);
2771
2772 has_frame = FALSE;
2773 while (!has_frame && !feof (f) && !ferror (f)) {
2774 length = fread (buffer, 1, sizeof (buffer), f);
2775 if (length > 0)
2776 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
2777 gdk_pixbuf_loader_close (loader, NULL);
2778 fclose (f);
2779 g_object_unref (loader);
2780 return NULL;
2781 }
2782
2783 animation = gdk_pixbuf_loader_get_animation (loader);
2784 if (animation) {
2785 iter = gdk_pixbuf_animation_get_iter (animation, 0);
2786 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
2787 has_frame = TRUE;
2788 }
2789 g_object_unref (iter);
2790 }
2791 }
2792
2793 fclose (f);
2794
2795 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
2796 g_object_unref (loader);
2797 return NULL;
2798 }
2799
2800 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
2801
2802 if (!pixbuf) {
2803 gchar *display_name = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2804 g_object_unref (loader);
2805 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
2806 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
2807 display_name ? display_name : "(unknown)");
2808 g_free (display_name);
2809 return NULL;
2810 }
2811
2812 g_object_ref (pixbuf);
2813
2814 g_object_unref (loader);
2815
2816 return pixbuf;
2817 }
2818 #endif /* ! Gtk 2.6.0 */
2819
2820 void gaim_gtk_set_custom_buddy_icon(GaimAccount *account, const char *who, const char *filename)
2821 {
2822 GaimConversation *conv;
2823 GaimBuddy *buddy;
2824 GaimBlistNode *node;
2825 char *path = NULL;
2826
2827 buddy = gaim_find_buddy(account, who);
2828 if (!buddy) {
2829 gaim_debug_info("custom-icon", "You can only set custom icon for someone in your buddylist.\n");
2830 return;
2831 }
2832
2833 node = (GaimBlistNode*)gaim_buddy_get_contact(buddy);
2834 path = (char*)gaim_blist_node_get_string(node, "custom_buddy_icon");
2835 if (path) {
2836 struct stat st;
2837 if (g_stat(path, &st) == 0)
2838 g_unlink(path);
2839 path = NULL;
2840 }
2841
2842 if (filename) {
2843 char *newfile;
2844
2845 newfile = gaim_gtk_convert_buddy_icon(gaim_find_prpl(gaim_account_get_protocol_id(account)),
2846 filename);
2847 path = gaim_buddy_icons_get_full_path(newfile);
2848 g_free(newfile);
2849 }
2850
2851 gaim_blist_node_set_string(node, "custom_buddy_icon", path);
2852 g_free(path);
2853
2854 /* Update the conversation */
2855 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, who, account);
2856 if (conv)
2857 gaim_conversation_update(conv, GAIM_CONV_UPDATE_ICON);
2858
2859 /* Update the buddylist */
2860 if (buddy)
2861 gaim_blist_update_buddy_icon(buddy);
2862 }
2863
2864 char *gaim_gtk_make_pretty_arrows(const char *str)
2865 {
2866 char *ret;
2867 char **split = g_strsplit(str, "->", -1);
2868 ret = g_strjoinv("\342\207\250", split);
2869 g_strfreev(split);
2870
2871 split = g_strsplit(ret, "<-", -1);
2872 g_free(ret);
2873 ret = g_strjoinv("\342\207\246", split);
2874 g_strfreev(split);
2875
2876 return ret;
2877 }
2878
2879 void gaim_gtk_set_urgent(GtkWindow *window, gboolean urgent)
2880 {
2881 #if GTK_CHECK_VERSION(2,8,0)
2882 gtk_window_set_urgency_hint(window, urgent);
2883 #elif defined _WIN32
2884 gtkwgaim_window_flash(window, urgent);
2885 #else
2886 GdkWindow *gdkwin;
2887 XWMHints *hints;
2888
2889 g_return_if_fail(window != NULL);
2890
2891 gdkwin = GTK_WIDGET(window)->window;
2892
2893 g_return_if_fail(gdkwin != NULL);
2894
2895 hints = XGetWMHints(GDK_WINDOW_XDISPLAY(gdkwin),
2896 GDK_WINDOW_XWINDOW(gdkwin));
2897 if(!hints)
2898 hints = XAllocWMHints();
2899
2900 if (urgent)
2901 hints->flags |= XUrgencyHint;
2902 else
2903 hints->flags &= ~XUrgencyHint;
2904 XSetWMHints(GDK_WINDOW_XDISPLAY(gdkwin),
2905 GDK_WINDOW_XWINDOW(gdkwin), hints);
2906 XFree(hints);
2907 #endif
2908 }
2909
2910 GSList *minidialogs = NULL;
2911
2912 static void *
2913 gaim_gtk_utils_get_handle()
2914 {
2915 static int handle;
2916
2917 return &handle;
2918 }
2919
2920 static void connection_signed_off_cb(GaimConnection *gc)
2921 {
2922 GSList *list;
2923 for (list = minidialogs; list; list = list->next) {
2924 if (g_object_get_data(G_OBJECT(list->data), "gc") == gc) {
2925 gtk_widget_destroy(GTK_WIDGET(list->data));
2926 }
2927 }
2928 }
2929
2930 static void alert_killed_cb(GtkWidget *widget)
2931 {
2932 minidialogs = g_slist_remove(minidialogs, widget);
2933 }
2934
2935 void *gaim_gtk_make_mini_dialog(GaimConnection *gc, const char *icon_name,
2936 const char *primary, const char *secondary,
2937 void *user_data, ...)
2938 {
2939 GtkWidget *vbox;
2940 GtkWidget *hbox;
2941 GtkWidget *hbox2;
2942 GtkWidget *label;
2943 GtkWidget *button;
2944 GtkWidget *img = NULL;
2945 GtkSizeGroup *sg = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
2946 char label_text[2048];
2947 const char *button_text;
2948 GCallback callback;
2949 char *primary_esc, *secondary_esc;
2950 va_list args;
2951 static gboolean first_call = TRUE;
2952
2953 img = gtk_image_new_from_stock(icon_name, GTK_ICON_SIZE_BUTTON);
2954 gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
2955
2956 vbox = gtk_vbox_new(FALSE,0);
2957 gtk_container_set_border_width(GTK_CONTAINER(vbox), GAIM_HIG_BOX_SPACE);
2958
2959 g_object_set_data(G_OBJECT(vbox), "gc" ,gc);
2960 minidialogs = g_slist_prepend(minidialogs, vbox);
2961 g_signal_connect(G_OBJECT(vbox), "destroy", G_CALLBACK(alert_killed_cb), NULL);
2962
2963 if (first_call) {
2964 first_call = FALSE;
2965 gaim_signal_connect(gaim_connections_get_handle(), "signed-off",
2966 gaim_gtk_utils_get_handle(),
2967 GAIM_CALLBACK(connection_signed_off_cb), NULL);
2968 }
2969
2970 hbox = gtk_hbox_new(FALSE, 0);
2971 gtk_container_add(GTK_CONTAINER(vbox), hbox);
2972
2973 if (img != NULL)
2974 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
2975
2976 primary_esc = g_markup_escape_text(primary, -1);
2977
2978 if (secondary)
2979 secondary_esc = g_markup_escape_text(secondary, -1);
2980 g_snprintf(label_text, sizeof(label_text),
2981 "<span weight=\"bold\" size=\"smaller\">%s</span>%s<span size=\"smaller\">%s</span>",
2982 primary_esc, secondary ? "\n" : "", secondary?secondary_esc:"");
2983 g_free(primary_esc);
2984 label = gtk_label_new(NULL);
2985 gtk_widget_set_size_request(label, gaim_prefs_get_int("/gaim/gtk/blist/width")-25,-1);
2986 gtk_label_set_markup(GTK_LABEL(label), label_text);
2987 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
2988 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
2989 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
2990
2991 hbox2 = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
2992 gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
2993
2994 va_start(args, user_data);
2995 while ((button_text = va_arg(args, char*))) {
2996 callback = va_arg(args, GCallback);
2997 button = gtk_button_new();
2998
2999 if (callback)
3000 g_signal_connect_swapped(G_OBJECT(button), "clicked", callback, user_data);
3001 g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_destroy), vbox);
3002 hbox = gtk_hbox_new(FALSE, 0);
3003 gtk_container_add(GTK_CONTAINER(button), hbox);
3004 gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
3005 g_snprintf(label_text, sizeof(label_text),
3006 "<span size=\"smaller\">%s</span>", button_text);
3007 label = gtk_label_new(NULL);
3008 gtk_label_set_markup_with_mnemonic(GTK_LABEL(label), label_text);
3009 gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
3010 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
3011 gtk_box_pack_end(GTK_BOX(hbox2), button, FALSE, FALSE, 0);
3012 gtk_size_group_add_widget(sg, button);
3013 }
3014 va_end(args);
3015
3016 return vbox;
3017 }
3018
3019 /*
3020 * "This is so dead sexy."
3021 * "Two thumbs up."
3022 * "Best movie of the year."
3023 *
3024 * This is the function that handles CTRL+F searching in the buddy list.
3025 * It finds the top-most buddy/group/chat/whatever containing the
3026 * entered string.
3027 *
3028 * It's somewhat ineffecient, because we strip all the HTML from the
3029 * "name" column of the buddy list (because the GtkTreeModel does not
3030 * contain the screen name in a non-markedup format). But the alternative
3031 * is to add an extra column to the GtkTreeModel. And this function is
3032 * used rarely, so it shouldn't matter TOO much.
3033 */
3034 gboolean gaim_gtk_tree_view_search_equal_func(GtkTreeModel *model, gint column,
3035 const gchar *key, GtkTreeIter *iter, gpointer data)
3036 {
3037 gchar *enteredstring;
3038 gchar *tmp;
3039 gchar *withmarkup;
3040 gchar *nomarkup;
3041 gchar *normalized;
3042 gboolean result;
3043 size_t i;
3044 size_t len;
3045 PangoLogAttr *log_attrs;
3046 gchar *word;
3047
3048 if (strcasecmp(key, "Global Thermonuclear War") == 0)
3049 {
3050 gaim_notify_info(NULL, "WOPR",
3051 "Wouldn't you prefer a nice game of chess?", NULL);
3052 return FALSE;
3053 }
3054
3055 gtk_tree_model_get(model, iter, column, &withmarkup, -1);
3056 if (withmarkup == NULL) /* This is probably a separator */
3057 return TRUE;
3058
3059 tmp = g_utf8_normalize(key, -1, G_NORMALIZE_DEFAULT);
3060 enteredstring = g_utf8_casefold(tmp, -1);
3061 g_free(tmp);
3062
3063 nomarkup = gaim_markup_strip_html(withmarkup);
3064 tmp = g_utf8_normalize(nomarkup, -1, G_NORMALIZE_DEFAULT);
3065 g_free(nomarkup);
3066 normalized = g_utf8_casefold(tmp, -1);
3067 g_free(tmp);
3068
3069 if (gaim_str_has_prefix(normalized, enteredstring))
3070 {
3071 g_free(withmarkup);
3072 g_free(enteredstring);
3073 g_free(normalized);
3074 return FALSE;
3075 }
3076
3077
3078 /* Use Pango to separate by words. */
3079 len = g_utf8_strlen(normalized, -1);
3080 log_attrs = g_new(PangoLogAttr, len + 1);
3081
3082 pango_get_log_attrs(normalized, strlen(normalized), -1, NULL, log_attrs, len + 1);
3083
3084 word = normalized;
3085 result = TRUE;
3086 for (i = 0; i < (len - 1) ; i++)
3087 {
3088 if (log_attrs[i].is_word_start &&
3089 gaim_str_has_prefix(word, enteredstring))
3090 {
3091 result = FALSE;
3092 break;
3093 }
3094 word = g_utf8_next_char(word);
3095 }
3096 g_free(log_attrs);
3097
3098 /* The non-Pango version. */
3099 #if 0
3100 word = normalized;
3101 result = TRUE;
3102 while (word[0] != '\0')
3103 {
3104 gunichar c = g_utf8_get_char(word);
3105 if (!g_unichar_isalnum(c))
3106 {
3107 word = g_utf8_find_next_char(word, NULL);
3108 if (gaim_str_has_prefix(word, enteredstring))
3109 {
3110 result = FALSE;
3111 break;
3112 }
3113 }
3114 else
3115 word = g_utf8_find_next_char(word, NULL);
3116 }
3117 #endif
3118
3119 g_free(withmarkup);
3120 g_free(enteredstring);
3121 g_free(normalized);
3122
3123 return result;
3124 }
3125
3126
3127 #if !GTK_CHECK_VERSION(2,2,0)
3128 GtkTreePath *
3129 gtk_tree_path_new_from_indices (gint first_index, ...)
3130 {
3131 int arg;
3132 va_list args;
3133 GtkTreePath *path;
3134
3135 path = gtk_tree_path_new ();
3136
3137 va_start (args, first_index);
3138 arg = first_index;
3139
3140 while (arg != -1)
3141 {
3142 gtk_tree_path_append_index (path, arg);
3143 arg = va_arg (args, gint);
3144 }
3145
3146 va_end (args);
3147
3148 return path;
3149 }
3150 #endif