comparison pidgin/gtkutils.c @ 25368:1260a3fb60f4

propagate from branch 'im.pidgin.pidgin' (head b8d6086aef6b2e65d86e8ce60220ab7f460d5079) to branch 'im.pidgin.pidgin.next.minor' (head c165595260a2efa0ca23704ada46a303e0412e19)
author Richard Laager <rlaager@wiktel.com>
date Fri, 12 Dec 2008 20:03:58 +0000
parents 8792c97479fd 367b3ddcf5c3
children ca01403251f1
comparison
equal deleted inserted replaced
24661:d1d9d085d626 25368:1260a3fb60f4
54 #include "prpl.h" 54 #include "prpl.h"
55 #include "request.h" 55 #include "request.h"
56 #include "signals.h" 56 #include "signals.h"
57 #include "util.h" 57 #include "util.h"
58 58
59 #include "gtkaccount.h"
60 #include "gtkprefs.h"
61
59 #include "gtkconv.h" 62 #include "gtkconv.h"
60 #include "gtkdialogs.h" 63 #include "gtkdialogs.h"
61 #include "gtkimhtml.h" 64 #include "gtkimhtml.h"
62 #include "gtkimhtmltoolbar.h" 65 #include "gtkimhtmltoolbar.h"
63 #include "pidginstock.h" 66 #include "pidginstock.h"
69 GtkWidget *menu; 72 GtkWidget *menu;
70 gint default_item; 73 gint default_item;
71 } AopMenu; 74 } AopMenu;
72 75
73 static guint accels_save_timer = 0; 76 static guint accels_save_timer = 0;
77 static GList *gnome_url_handlers = NULL;
74 78
75 static gboolean 79 static gboolean
76 url_clicked_idle_cb(gpointer data) 80 url_clicked_idle_cb(gpointer data)
77 { 81 {
78 purple_notify_uri(NULL, data); 82 purple_notify_uri(NULL, data);
79 g_free(data); 83 g_free(data);
80 return FALSE; 84 return FALSE;
81 } 85 }
82 86
83 static void 87 static gboolean
84 url_clicked_cb(GtkWidget *w, const char *uri) 88 url_clicked_cb(GtkIMHtml *unused, GtkIMHtmlLink *link)
85 { 89 {
90 const char *uri = gtk_imhtml_link_get_url(link);
86 g_idle_add(url_clicked_idle_cb, g_strdup(uri)); 91 g_idle_add(url_clicked_idle_cb, g_strdup(uri));
92 return TRUE;
87 } 93 }
88 94
89 static GtkIMHtmlFuncs gtkimhtml_cbs = { 95 static GtkIMHtmlFuncs gtkimhtml_cbs = {
90 (GtkIMHtmlGetImageFunc)purple_imgstore_find_by_id, 96 (GtkIMHtmlGetImageFunc)purple_imgstore_find_by_id,
91 (GtkIMHtmlGetImageDataFunc)purple_imgstore_get_data, 97 (GtkIMHtmlGetImageDataFunc)purple_imgstore_get_data,
99 pidgin_setup_imhtml(GtkWidget *imhtml) 105 pidgin_setup_imhtml(GtkWidget *imhtml)
100 { 106 {
101 PangoFontDescription *desc = NULL; 107 PangoFontDescription *desc = NULL;
102 g_return_if_fail(imhtml != NULL); 108 g_return_if_fail(imhtml != NULL);
103 g_return_if_fail(GTK_IS_IMHTML(imhtml)); 109 g_return_if_fail(GTK_IS_IMHTML(imhtml));
104
105 g_signal_connect(G_OBJECT(imhtml), "url_clicked",
106 G_CALLBACK(url_clicked_cb), NULL);
107 110
108 pidgin_themes_smiley_themeize(imhtml); 111 pidgin_themes_smiley_themeize(imhtml);
109 112
110 gtk_imhtml_set_funcs(GTK_IMHTML(imhtml), &gtkimhtml_cbs); 113 gtk_imhtml_set_funcs(GTK_IMHTML(imhtml), &gtkimhtml_cbs);
111 114
3478 g_object_ref(pixbuf); 3481 g_object_ref(pixbuf);
3479 g_object_unref(loader); 3482 g_object_unref(loader);
3480 return pixbuf; 3483 return pixbuf;
3481 } 3484 }
3482 3485
3486 static void url_copy(GtkWidget *w, gchar *url)
3487 {
3488 GtkClipboard *clipboard;
3489
3490 clipboard = gtk_widget_get_clipboard(w, GDK_SELECTION_PRIMARY);
3491 gtk_clipboard_set_text(clipboard, url, -1);
3492
3493 clipboard = gtk_widget_get_clipboard(w, GDK_SELECTION_CLIPBOARD);
3494 gtk_clipboard_set_text(clipboard, url, -1);
3495 }
3496
3497 static gboolean
3498 link_context_menu(GtkIMHtml *imhtml, GtkIMHtmlLink *link, GtkWidget *menu)
3499 {
3500 GtkWidget *img, *item;
3501 const char *url;
3502
3503 url = gtk_imhtml_link_get_url(link);
3504
3505 /* Open Link */
3506 img = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
3507 item = gtk_image_menu_item_new_with_mnemonic(_("_Open Link"));
3508 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img);
3509 g_signal_connect_swapped(G_OBJECT(item), "activate", G_CALLBACK(gtk_imhtml_link_activate), link);
3510 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
3511
3512 /* Copy Link Location */
3513 img = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
3514 item = gtk_image_menu_item_new_with_mnemonic(_("_Copy Link Location"));
3515 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img);
3516 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(url_copy), (gpointer)url);
3517 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
3518
3519 return TRUE;
3520 }
3521
3522 static gboolean
3523 copy_email_address(GtkIMHtml *imhtml, GtkIMHtmlLink *link, GtkWidget *menu)
3524 {
3525 GtkWidget *img, *item;
3526 const char *text;
3527 char *address;
3528 #define MAILTOSIZE (sizeof("mailto:") - 1)
3529
3530 text = gtk_imhtml_link_get_url(link);
3531 g_return_val_if_fail(text && strlen(text) > MAILTOSIZE, FALSE);
3532 address = (char*)text + MAILTOSIZE;
3533
3534 /* Copy Email Address */
3535 img = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
3536 item = gtk_image_menu_item_new_with_mnemonic(_("_Copy Email Address"));
3537 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img);
3538 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(url_copy), address);
3539 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
3540
3541 return TRUE;
3542 }
3543
3544 /* XXX: The following two functions are for demonstration purposes only! */
3545 static gboolean
3546 open_dialog(GtkIMHtml *imhtml, GtkIMHtmlLink *link)
3547 {
3548 const char *url;
3549 const char *str;
3550
3551 url = gtk_imhtml_link_get_url(link);
3552 if (!url || strlen(url) < sizeof("open://"))
3553 return FALSE;
3554
3555 str = url + sizeof("open://") - 1;
3556
3557 if (strcmp(str, "accounts") == 0)
3558 pidgin_accounts_window_show();
3559 else if (strcmp(str, "prefs") == 0)
3560 pidgin_prefs_show();
3561 else
3562 return FALSE;
3563 return TRUE;
3564 }
3565
3566 static gboolean
3567 dummy(GtkIMHtml *imhtml, GtkIMHtmlLink *link, GtkWidget *menu)
3568 {
3569 return TRUE;
3570 }
3571
3572 static gboolean
3573 register_gnome_url_handlers(void)
3574 {
3575 char *tmp;
3576 char *err;
3577 char *c;
3578 char *start;
3579
3580 tmp = g_find_program_in_path("gconftool-2");
3581 if (tmp == NULL)
3582 return FALSE;
3583
3584 tmp = NULL;
3585 if (!g_spawn_command_line_sync("gconftool-2 --all-dirs /desktop/gnome/url-handlers",
3586 &tmp, &err, NULL, NULL))
3587 {
3588 g_free(tmp);
3589 g_free(err);
3590 g_return_val_if_reached(FALSE);
3591 }
3592 g_free(err);
3593 err = NULL;
3594
3595 for (c = start = tmp ; *c ; c++)
3596 {
3597 /* Skip leading spaces. */
3598 if (c == start && *c == ' ')
3599 start = c + 1;
3600 else if (*c == '\n')
3601 {
3602 *c = '\0';
3603 if (g_str_has_prefix(start, "/desktop/gnome/url-handlers/"))
3604 {
3605 char *cmd;
3606 char *tmp2 = NULL;
3607 char *protocol;
3608
3609 /* If there is an enabled boolean, honor it. */
3610 cmd = g_strdup_printf("gconftool-2 -g %s/enabled", start);
3611 if (g_spawn_command_line_sync(cmd, &tmp2, &err, NULL, NULL))
3612 {
3613 g_free(err);
3614 err = NULL;
3615 if (!strcmp(tmp2, "false\n"))
3616 {
3617 g_free(tmp2);
3618 g_free(cmd);
3619 start = c + 1;
3620 continue;
3621 }
3622 }
3623 g_free(cmd);
3624 g_free(tmp2);
3625
3626 start += sizeof("/desktop/gnome/url-handlers/") - 1;
3627
3628 protocol = g_strdup_printf("%s:", start);
3629 gnome_url_handlers = g_list_prepend(gnome_url_handlers, protocol);
3630 gtk_imhtml_class_register_protocol(protocol, url_clicked_cb, link_context_menu);
3631 }
3632 start = c + 1;
3633 }
3634 }
3635 g_free(tmp);
3636
3637 return (gnome_url_handlers != NULL);
3638 }
3639
3640 void pidgin_utils_init(void)
3641 {
3642 gtk_imhtml_class_register_protocol("http://", url_clicked_cb, link_context_menu);
3643 gtk_imhtml_class_register_protocol("https://", url_clicked_cb, link_context_menu);
3644 gtk_imhtml_class_register_protocol("ftp://", url_clicked_cb, link_context_menu);
3645 gtk_imhtml_class_register_protocol("gopher://", url_clicked_cb, link_context_menu);
3646 gtk_imhtml_class_register_protocol("mailto:", url_clicked_cb, copy_email_address);
3647
3648 /* Example custom URL handler. */
3649 gtk_imhtml_class_register_protocol("open://", open_dialog, dummy);
3650
3651 /* If we're under GNOME, try registering the system URL handlers. */
3652 if (purple_running_gnome())
3653 register_gnome_url_handlers();
3654 }
3655
3656 void pidgin_utils_uninit(void)
3657 {
3658 gtk_imhtml_class_register_protocol("open://", NULL, NULL);
3659
3660 /* If we have GNOME handlers registered, unregister them. */
3661 if (gnome_url_handlers)
3662 {
3663 GList *l;
3664 for (l = gnome_url_handlers ; l ; l = l->next)
3665 {
3666 gtk_imhtml_class_register_protocol((char *)l->data, NULL, NULL);
3667 g_free(l->data);
3668 }
3669 g_list_free(gnome_url_handlers);
3670 gnome_url_handlers = NULL;
3671 return;
3672 }
3673
3674 gtk_imhtml_class_register_protocol("http://", NULL, NULL);
3675 gtk_imhtml_class_register_protocol("https://", NULL, NULL);
3676 gtk_imhtml_class_register_protocol("ftp://", NULL, NULL);
3677 gtk_imhtml_class_register_protocol("mailto:", NULL, NULL);
3678 gtk_imhtml_class_register_protocol("gopher://", NULL, NULL);
3679 }
3680