2 * Copyright (C) 2008-2009 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include "empathy-webkit-utils.h"
24 #include <glib/gi18n-lib.h>
26 #include "empathy-smiley-manager.h"
27 #include "empathy-string-parser.h"
28 #include "empathy-theme-adium.h"
29 #include "empathy-ui-utils.h"
31 #define BORING_DPI_DEFAULT 96
34 empathy_webkit_match_newline (const gchar
*text
,
36 TpawStringReplace replace_func
,
37 TpawStringParser
*sub_parsers
,
40 GString
*string
= user_data
;
47 /* Replace \n by <br/> */
48 for (i
= 0; i
< len
&& text
[i
] != '\0'; i
++)
52 tpaw_string_parser_substr (text
+ prev
, i
- prev
,
53 sub_parsers
, user_data
);
54 g_string_append (string
, "<br/>");
59 tpaw_string_parser_substr (text
+ prev
, i
- prev
,
60 sub_parsers
, user_data
);
64 empathy_webkit_replace_smiley (const gchar
*text
,
69 EmpathySmileyHit
*hit
= match_data
;
70 GString
*string
= user_data
;
72 /* Replace smiley by a <img/> tag */
73 g_string_append_printf (string
,
74 "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
75 hit
->path
, (int)len
, text
, (int)len
, text
);
78 static TpawStringParser string_parsers
[] = {
79 { tpaw_string_match_link
, tpaw_string_replace_link
},
80 { empathy_webkit_match_newline
, NULL
},
81 { tpaw_string_match_all
, tpaw_string_replace_escaped
},
85 static TpawStringParser string_parsers_with_smiley
[] = {
86 { tpaw_string_match_link
, tpaw_string_replace_link
},
87 { empathy_string_match_smiley
, empathy_webkit_replace_smiley
},
88 { empathy_webkit_match_newline
, NULL
},
89 { tpaw_string_match_all
, tpaw_string_replace_escaped
},
94 empathy_webkit_get_string_parser (gboolean smileys
)
97 return string_parsers_with_smiley
;
99 return string_parsers
;
103 webkit_get_font_family (GValue
*value
,
107 PangoFontDescription
*font
= pango_font_description_from_string (
108 g_variant_get_string (variant
, NULL
));
113 g_value_set_string (value
, pango_font_description_get_family (font
));
114 pango_font_description_free (font
);
120 webkit_get_font_size (GValue
*value
,
124 PangoFontDescription
*font
= pango_font_description_from_string (
125 g_variant_get_string (variant
, NULL
));
131 size
= pango_font_description_get_size (font
) / PANGO_SCALE
;
133 if (pango_font_description_get_size_is_absolute (font
))
135 GdkScreen
*screen
= gdk_screen_get_default ();
139 dpi
= gdk_screen_get_resolution (screen
);
141 dpi
= BORING_DPI_DEFAULT
;
143 size
= (gint
) (size
/ (dpi
/ 72));
146 g_value_set_int (value
, size
);
147 pango_font_description_free (font
);
153 empathy_webkit_bind_font_setting (WebKitWebView
*webview
,
154 GSettings
*gsettings
,
157 WebKitWebSettings
*settings
= webkit_web_view_get_settings (webview
);
159 g_settings_bind_with_mapping (gsettings
, key
,
160 settings
, "default-font-family",
162 webkit_get_font_family
,
166 g_settings_bind_with_mapping (gsettings
, key
,
167 settings
, "default-font-size",
169 webkit_get_font_size
,
175 empathy_webkit_copy_address_cb (GtkMenuItem
*menuitem
,
178 WebKitHitTestResult
*hit_test_result
= WEBKIT_HIT_TEST_RESULT (user_data
);
180 GtkClipboard
*clipboard
;
182 g_object_get (G_OBJECT (hit_test_result
),
186 clipboard
= gtk_clipboard_get (GDK_SELECTION_CLIPBOARD
);
187 gtk_clipboard_set_text (clipboard
, uri
, -1);
189 clipboard
= gtk_clipboard_get (GDK_SELECTION_PRIMARY
);
190 gtk_clipboard_set_text (clipboard
, uri
, -1);
196 empathy_webkit_open_address_cb (GtkMenuItem
*menuitem
,
199 WebKitHitTestResult
*hit_test_result
= WEBKIT_HIT_TEST_RESULT (user_data
);
202 g_object_get (G_OBJECT (hit_test_result
),
206 empathy_url_show (GTK_WIDGET (menuitem
), uri
);
212 empathy_webkit_inspect_cb (GtkMenuItem
*menuitem
,
215 empathy_webkit_show_inspector (view
);
219 empathy_webkit_context_menu_selection_done_cb (GtkMenuShell
*menu
,
222 WebKitHitTestResult
*hit_test_result
= WEBKIT_HIT_TEST_RESULT (user_data
);
224 g_object_unref (hit_test_result
);
228 empathy_webkit_create_context_menu (WebKitWebView
*view
,
229 WebKitHitTestResult
*hit_test_result
,
230 EmpathyWebKitMenuFlags flags
)
232 WebKitHitTestResultContext context
;
236 g_object_get (G_OBJECT (hit_test_result
),
241 menu
= empathy_context_menu_new (GTK_WIDGET (view
));
243 /* Select all item */
244 item
= gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL
, NULL
);
245 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
247 g_signal_connect_swapped (item
, "activate",
248 G_CALLBACK (webkit_web_view_select_all
),
252 if (webkit_web_view_can_copy_clipboard (view
))
254 item
= gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY
, NULL
);
255 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
257 g_signal_connect_swapped (item
, "activate",
258 G_CALLBACK (webkit_web_view_copy_clipboard
),
262 /* Clear menu item */
263 if (flags
& EMPATHY_WEBKIT_MENU_CLEAR
)
265 item
= gtk_separator_menu_item_new ();
266 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
268 item
= gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR
, NULL
);
269 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
271 g_signal_connect_swapped (item
, "activate",
272 G_CALLBACK (empathy_theme_adium_clear
),
276 /* We will only add the following menu items if we are
277 * right-clicking a link */
278 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK
)
281 item
= gtk_separator_menu_item_new ();
282 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
284 /* Copy Link Address menu item */
285 item
= gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
286 g_signal_connect (item
, "activate",
287 G_CALLBACK (empathy_webkit_copy_address_cb
),
289 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
291 /* Open Link menu item */
292 item
= gtk_menu_item_new_with_mnemonic (_("_Open Link"));
293 g_signal_connect (item
, "activate",
294 G_CALLBACK (empathy_webkit_open_address_cb
),
296 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu
), item
);
299 if ((flags
& EMPATHY_WEBKIT_MENU_INSPECT
) != 0)
302 item
= gtk_separator_menu_item_new ();
303 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
306 item
= gtk_menu_item_new_with_mnemonic (_("Inspect HTML"));
307 g_signal_connect (item
, "activate",
308 G_CALLBACK (empathy_webkit_inspect_cb
), view
);
309 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
312 g_signal_connect (GTK_MENU_SHELL (menu
), "selection-done",
313 G_CALLBACK (empathy_webkit_context_menu_selection_done_cb
),
314 g_object_ref (hit_test_result
));
320 empathy_webkit_context_menu_for_event (WebKitWebView
*view
,
321 GdkEventButton
*event
,
322 EmpathyWebKitMenuFlags flags
)
325 WebKitHitTestResult
*hit_test_result
;
327 hit_test_result
= webkit_web_view_get_hit_test_result (view
, event
);
329 menu
= empathy_webkit_create_context_menu (view
, hit_test_result
, flags
);
331 gtk_widget_show_all (menu
);
332 gtk_menu_popup (GTK_MENU (menu
), NULL
, NULL
, NULL
, NULL
,
333 event
->button
, event
->time
);
335 g_object_unref (hit_test_result
);
339 empathy_webkit_show_inspector (WebKitWebView
*view
)
341 WebKitWebInspector
*inspector
;
343 g_object_set (G_OBJECT (webkit_web_view_get_settings (view
)),
344 "enable-developer-extras", TRUE
, NULL
);
346 inspector
= webkit_web_view_get_inspector (view
);
347 webkit_web_inspector_show (inspector
);