pidgin/gtkutils.c

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

mercurial