1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n.h>
27 #include <webkit/webkit.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
31 #include <gconf/gconf-client.h>
32 #include <pango/pango.h>
35 #include <libempathy/empathy-time.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-theme-adium.h"
39 #include "empathy-smiley-manager.h"
40 #include "empathy-conf.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-plist.h"
43 #include "empathy-string-parser.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
46 #include <libempathy/empathy-debug.h>
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
50 /* GConf key containing current value of font */
51 #define EMPATHY_GCONF_FONT_KEY_NAME "/desktop/gnome/interface/font_name"
52 #define BORING_DPI_DEFAULT 96
54 /* "Join" consecutive messages with timestamps within five minutes */
55 #define MESSAGE_JOIN_PERIOD 5*60
58 EmpathyAdiumData *data;
59 EmpathySmileyManager *smiley_manager;
60 EmpathyContact *last_contact;
61 time_t last_timestamp;
62 gboolean last_is_backlog;
65 guint notify_enable_webkit_developer_tools_id;
66 GtkWidget *inspector_window;
67 } EmpathyThemeAdiumPriv;
69 struct _EmpathyAdiumData {
73 gchar *default_avatar_filename;
74 gchar *default_incoming_avatar_filename;
75 gchar *default_outgoing_avatar_filename;
77 gchar *in_content_html;
79 gchar *in_context_html;
81 gchar *in_nextcontent_html;
82 gsize in_nextcontent_len;
83 gchar *in_nextcontext_html;
84 gsize in_nextcontext_len;
85 gchar *out_content_html;
86 gsize out_content_len;
87 gchar *out_context_html;
88 gsize out_context_len;
89 gchar *out_nextcontent_html;
90 gsize out_nextcontent_len;
91 gchar *out_nextcontext_html;
92 gsize out_nextcontext_len;
98 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
105 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
106 WEBKIT_TYPE_WEB_VIEW,
107 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
108 theme_adium_iface_init));
111 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
113 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
114 gboolean enable_webkit_developer_tools;
116 if (!empathy_conf_get_bool (empathy_conf_get (),
117 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
118 &enable_webkit_developer_tools)) {
122 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
123 "enable-developer-extras",
124 enable_webkit_developer_tools,
129 theme_adium_notify_enable_webkit_developer_tools_cb (EmpathyConf *conf,
133 EmpathyThemeAdium *theme = user_data;
135 theme_adium_update_enable_webkit_developer_tools (theme);
139 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView *view,
140 WebKitWebFrame *web_frame,
141 WebKitNetworkRequest *request,
142 WebKitWebNavigationAction *action,
143 WebKitWebPolicyDecision *decision,
148 /* Only call url_show on clicks */
149 if (webkit_web_navigation_action_get_reason (action) !=
150 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
151 webkit_web_policy_decision_use (decision);
155 uri = webkit_network_request_get_uri (request);
156 empathy_url_show (GTK_WIDGET (view), uri);
158 webkit_web_policy_decision_ignore (decision);
163 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
166 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
168 GtkClipboard *clipboard;
170 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
172 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
173 gtk_clipboard_set_text (clipboard, uri, -1);
175 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
176 gtk_clipboard_set_text (clipboard, uri, -1);
182 theme_adium_open_address_cb (GtkMenuItem *menuitem,
185 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
188 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
190 empathy_url_show (GTK_WIDGET (menuitem), uri);
196 theme_adium_match_newline (const gchar *text,
198 EmpathyStringReplace replace_func,
199 EmpathyStringParser *sub_parsers,
202 GString *string = user_data;
210 /* Replace \n by <br/> */
211 for (i = 0; i < len && text[i] != '\0'; i++) {
212 if (text[i] == '\n') {
213 empathy_string_parser_substr (text + prev,
214 i - prev, sub_parsers,
216 g_string_append (string, "<br/>");
220 empathy_string_parser_substr (text + prev, i - prev,
221 sub_parsers, user_data);
225 theme_adium_replace_link (const gchar *text,
230 GString *string = user_data;
234 real_url = empathy_make_absolute_url_len (text, len);
236 /* The thing we are making a link of may contain
237 * characters which need escaping */
238 escaped = g_markup_escape_text (text, len);
240 /* Append the link inside <a href=""></a> tag */
241 g_string_append_printf (string, "<a href=\"%s\">%s</a>",
249 theme_adium_replace_smiley (const gchar *text,
254 EmpathySmileyHit *hit = match_data;
255 GString *string = user_data;
257 /* Replace smiley by a <img/> tag */
258 g_string_append_printf (string,
259 "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
260 hit->path, (int)len, text, (int)len, text);
264 theme_adium_replace_escaped (const gchar *text,
269 GString *string = user_data;
272 escaped = g_markup_escape_text (text, len);
273 g_string_append (string, escaped);
277 static EmpathyStringParser string_parsers[] = {
278 {empathy_string_match_link, theme_adium_replace_link},
279 {theme_adium_match_newline, NULL},
280 {empathy_string_match_all, theme_adium_replace_escaped},
284 static EmpathyStringParser string_parsers_with_smiley[] = {
285 {empathy_string_match_link, theme_adium_replace_link},
286 {empathy_string_match_smiley, theme_adium_replace_smiley},
287 {theme_adium_match_newline, NULL},
288 {empathy_string_match_all, theme_adium_replace_escaped},
293 theme_adium_parse_body (const gchar *text)
295 EmpathyStringParser *parsers;
297 gboolean use_smileys;
299 /* Check if we have to parse smileys */
300 empathy_conf_get_bool (empathy_conf_get (),
301 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
304 parsers = string_parsers_with_smiley;
306 parsers = string_parsers;
308 /* Parse text and construct string with links and smileys replaced
309 * by html tags. Also escape text to make sure html code is
310 * displayed verbatim. */
311 string = g_string_sized_new (strlen (text));
312 empathy_string_parser_substr (text, -1, parsers, string);
314 return g_string_free (string, FALSE);
318 escape_and_append_len (GString *string, const gchar *str, gint len)
320 while (*str != '\0' && len != 0) {
324 g_string_append (string, "\\\\");
328 g_string_append (string, "\\\"");
331 /* Remove end of lines */
334 g_string_append_c (string, *str);
343 theme_adium_match (const gchar **str, const gchar *match)
347 len = strlen (match);
348 if (strncmp (*str, match, len) == 0) {
357 theme_adium_append_html (EmpathyThemeAdium *theme,
359 const gchar *html, gsize len,
360 const gchar *message,
361 const gchar *avatar_filename,
363 const gchar *contact_id,
364 const gchar *service_name,
365 const gchar *message_classes,
369 const gchar *cur = NULL;
372 /* Make some search-and-replace in the html code */
373 string = g_string_sized_new (len + strlen (message));
374 g_string_append_printf (string, "%s(\"", func);
375 for (cur = html; *cur != '\0'; cur++) {
376 const gchar *replace = NULL;
377 gchar *dup_replace = NULL;
379 if (theme_adium_match (&cur, "%message%")) {
381 } else if (theme_adium_match (&cur, "%messageClasses%")) {
382 replace = message_classes;
383 } else if (theme_adium_match (&cur, "%userIconPath%")) {
384 replace = avatar_filename;
385 } else if (theme_adium_match (&cur, "%sender%")) {
387 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
388 replace = contact_id;
389 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
390 /* %senderDisplayName% -
391 * "The serverside (remotely set) name of the sender,
392 * such as an MSN display name."
394 * We don't have access to that yet so we use local
397 } else if (theme_adium_match (&cur, "%service%")) {
398 replace = service_name;
399 } else if (theme_adium_match (&cur, "%shortTime%")) {
400 dup_replace = empathy_time_to_string_local (timestamp,
401 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
402 replace = dup_replace;
403 } else if (theme_adium_match (&cur, "%time")) {
404 gchar *format = NULL;
406 /* Time can be in 2 formats:
407 * %time% or %time{strftime format}%
408 * Extract the time format if provided. */
411 end = strstr (cur, "}%");
416 format = g_strndup (cur, end - cur);
422 dup_replace = empathy_time_to_string_local (timestamp,
423 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
424 replace = dup_replace;
427 escape_and_append_len (string, cur, 1);
431 /* Here we have a replacement to make */
432 escape_and_append_len (string, replace, -1);
433 g_free (dup_replace);
435 g_string_append (string, "\")");
437 script = g_string_free (string, FALSE);
438 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
443 theme_adium_append_event_escaped (EmpathyChatView *view,
444 const gchar *escaped)
446 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
447 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
449 if (priv->data->status_html) {
450 theme_adium_append_html (theme, "appendMessage",
451 priv->data->status_html,
452 priv->data->status_len,
453 escaped, NULL, NULL, NULL, NULL,
454 "event", empathy_time_get_current ());
457 /* There is no last contact */
458 if (priv->last_contact) {
459 g_object_unref (priv->last_contact);
460 priv->last_contact = NULL;
465 theme_adium_append_message (EmpathyChatView *view,
468 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
469 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
470 EmpathyContact *sender;
475 const gchar *contact_id;
476 EmpathyAvatar *avatar;
477 const gchar *avatar_filename = NULL;
482 const gchar *service_name;
483 GString *message_classes = NULL;
485 gboolean consecutive;
487 if (!priv->page_loaded) {
488 priv->message_queue = g_list_prepend (priv->message_queue,
493 /* Get information */
494 sender = empathy_message_get_sender (msg);
495 account = empathy_contact_get_account (sender);
496 service_name = empathy_protocol_name_to_display_name
497 (tp_account_get_protocol (account));
498 if (service_name == NULL)
499 service_name = tp_account_get_protocol (account);
500 timestamp = empathy_message_get_timestamp (msg);
501 body = empathy_message_get_body (msg);
502 body_escaped = theme_adium_parse_body (body);
503 name = empathy_contact_get_name (sender);
504 contact_id = empathy_contact_get_id (sender);
506 /* If this is a /me, append an event */
507 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
510 str = g_strdup_printf ("%s %s", name, body_escaped);
511 theme_adium_append_event_escaped (view, str);
514 g_free (body_escaped);
518 /* Get the avatar filename, or a fallback */
519 avatar = empathy_contact_get_avatar (sender);
521 avatar_filename = avatar->filename;
523 if (!avatar_filename) {
524 if (empathy_contact_is_user (sender)) {
525 avatar_filename = priv->data->default_outgoing_avatar_filename;
527 avatar_filename = priv->data->default_incoming_avatar_filename;
529 if (!avatar_filename) {
530 if (!priv->data->default_avatar_filename) {
531 priv->data->default_avatar_filename =
532 empathy_filename_from_icon_name ("stock_person",
533 GTK_ICON_SIZE_DIALOG);
535 avatar_filename = priv->data->default_avatar_filename;
539 /* We want to join this message with the last one if
540 * - senders are the same contact,
541 * - last message was recieved recently,
542 * - last message and this message both are/aren't backlog, and
543 * - DisableCombineConsecutive is not set in theme's settings */
544 is_backlog = empathy_message_is_backlog (msg);
545 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
546 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
547 (is_backlog == priv->last_is_backlog) &&
548 !tp_asv_get_boolean (priv->data->info,
549 "DisableCombineConsecutive", NULL);
551 /* Define message classes */
552 message_classes = g_string_new ("message");
554 g_string_append (message_classes, " history");
557 g_string_append (message_classes, " consecutive");
559 if (empathy_contact_is_user (sender)) {
560 g_string_append (message_classes, " outgoing");
562 g_string_append (message_classes, " incoming");
565 /* Define javascript function to use */
567 func = "appendNextMessage";
569 func = "appendMessage";
573 if (empathy_contact_is_user (sender)) {
576 html = priv->data->out_nextcontext_html;
577 len = priv->data->out_nextcontext_len;
580 /* Not backlog, or fallback if NextContext.html
583 html = priv->data->out_nextcontent_html;
584 len = priv->data->out_nextcontent_len;
588 /* Not consecutive, or fallback if NextContext.html and/or
589 * NextContent.html are missing */
592 html = priv->data->out_context_html;
593 len = priv->data->out_context_len;
597 html = priv->data->out_content_html;
598 len = priv->data->out_content_len;
603 /* Incoming, or fallback if outgoing files are missing */
607 html = priv->data->in_nextcontext_html;
608 len = priv->data->in_nextcontext_len;
611 /* Note backlog, or fallback if NextContext.html
614 html = priv->data->in_nextcontent_html;
615 len = priv->data->in_nextcontent_len;
619 /* Not consecutive, or fallback if NextContext.html and/or
620 * NextContent.html are missing */
623 html = priv->data->in_context_html;
624 len = priv->data->in_context_len;
628 html = priv->data->in_content_html;
629 len = priv->data->in_content_len;
635 theme_adium_append_html (theme, func, html, len, body_escaped,
636 avatar_filename, name, contact_id,
637 service_name, message_classes->str,
640 DEBUG ("Couldn't find HTML file for this message");
643 /* Keep the sender of the last displayed message */
644 if (priv->last_contact) {
645 g_object_unref (priv->last_contact);
647 priv->last_contact = g_object_ref (sender);
648 priv->last_timestamp = timestamp;
649 priv->last_is_backlog = is_backlog;
651 g_free (body_escaped);
652 g_string_free (message_classes, TRUE);
656 theme_adium_append_event (EmpathyChatView *view,
661 str_escaped = g_markup_escape_text (str, -1);
662 theme_adium_append_event_escaped (view, str_escaped);
663 g_free (str_escaped);
667 theme_adium_scroll (EmpathyChatView *view,
668 gboolean allow_scrolling)
670 /* FIXME: Is it possible? I guess we need a js function, but I don't
675 theme_adium_scroll_down (EmpathyChatView *view)
677 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
681 theme_adium_get_has_selection (EmpathyChatView *view)
683 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
687 theme_adium_clear (EmpathyChatView *view)
689 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
692 priv->page_loaded = FALSE;
693 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
694 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
695 priv->data->template_html,
697 g_free (basedir_uri);
699 /* Clear last contact to avoid trying to add a 'joined'
700 * message when we don't have an insertion point. */
701 if (priv->last_contact) {
702 g_object_unref (priv->last_contact);
703 priv->last_contact = NULL;
708 theme_adium_find_previous (EmpathyChatView *view,
709 const gchar *search_criteria,
713 /* FIXME: Doesn't respect new_search */
714 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
715 search_criteria, match_case,
720 theme_adium_find_next (EmpathyChatView *view,
721 const gchar *search_criteria,
725 /* FIXME: Doesn't respect new_search */
726 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
727 search_criteria, match_case,
732 theme_adium_find_abilities (EmpathyChatView *view,
733 const gchar *search_criteria,
735 gboolean *can_do_previous,
736 gboolean *can_do_next)
738 /* FIXME: Does webkit provide an API for that? We have wrap=true in
739 * find_next and find_previous to work around this problem. */
741 *can_do_previous = TRUE;
747 theme_adium_highlight (EmpathyChatView *view,
751 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
752 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
753 text, match_case, 0);
754 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
759 theme_adium_copy_clipboard (EmpathyChatView *view)
761 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
765 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
767 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
769 g_object_unref (hit_test_result);
773 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
775 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
776 WebKitHitTestResult *hit_test_result;
777 WebKitHitTestResultContext context;
781 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
782 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
785 menu = gtk_menu_new ();
787 /* Select all item */
788 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
789 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
791 g_signal_connect_swapped (item, "activate",
792 G_CALLBACK (webkit_web_view_select_all),
796 if (webkit_web_view_can_copy_clipboard (view)) {
797 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
798 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
800 g_signal_connect_swapped (item, "activate",
801 G_CALLBACK (webkit_web_view_copy_clipboard),
805 /* Clear menu item */
806 item = gtk_separator_menu_item_new ();
807 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
809 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
810 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
812 g_signal_connect_swapped (item, "activate",
813 G_CALLBACK (empathy_chat_view_clear),
816 /* We will only add the following menu items if we are
817 * right-clicking a link */
818 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
820 item = gtk_separator_menu_item_new ();
821 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
823 /* Copy Link Address menu item */
824 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
825 g_signal_connect (item, "activate",
826 G_CALLBACK (theme_adium_copy_address_cb),
828 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
830 /* Open Link menu item */
831 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
832 g_signal_connect (item, "activate",
833 G_CALLBACK (theme_adium_open_address_cb),
835 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
838 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
839 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
842 /* Display the menu */
843 gtk_widget_show_all (menu);
844 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
845 event->button, event->time);
846 g_object_ref_sink (menu);
847 g_object_unref (menu);
851 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
853 if (event->button == 3) {
854 gboolean developer_tools_enabled;
856 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
857 "enable-developer-extras", &developer_tools_enabled, NULL);
859 /* We currently have no way to add an inspector menu
860 * item ourselves, so we disable our customized menu
861 * if the developer extras are enabled. */
862 if (!developer_tools_enabled) {
863 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
868 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
872 theme_adium_iface_init (EmpathyChatViewIface *iface)
874 iface->append_message = theme_adium_append_message;
875 iface->append_event = theme_adium_append_event;
876 iface->scroll = theme_adium_scroll;
877 iface->scroll_down = theme_adium_scroll_down;
878 iface->get_has_selection = theme_adium_get_has_selection;
879 iface->clear = theme_adium_clear;
880 iface->find_previous = theme_adium_find_previous;
881 iface->find_next = theme_adium_find_next;
882 iface->find_abilities = theme_adium_find_abilities;
883 iface->highlight = theme_adium_highlight;
884 iface->copy_clipboard = theme_adium_copy_clipboard;
888 theme_adium_load_finished_cb (WebKitWebView *view,
889 WebKitWebFrame *frame,
892 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
893 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
895 DEBUG ("Page loaded");
896 priv->page_loaded = TRUE;
898 /* Display queued messages */
899 priv->message_queue = g_list_reverse (priv->message_queue);
900 while (priv->message_queue) {
901 EmpathyMessage *message = priv->message_queue->data;
903 theme_adium_append_message (chat_view, message);
904 priv->message_queue = g_list_remove (priv->message_queue, message);
905 g_object_unref (message);
910 theme_adium_finalize (GObject *object)
912 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
914 empathy_adium_data_unref (priv->data);
916 empathy_conf_notify_remove (empathy_conf_get (),
917 priv->notify_enable_webkit_developer_tools_id);
919 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
923 theme_adium_dispose (GObject *object)
925 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
927 if (priv->smiley_manager) {
928 g_object_unref (priv->smiley_manager);
929 priv->smiley_manager = NULL;
932 if (priv->last_contact) {
933 g_object_unref (priv->last_contact);
934 priv->last_contact = NULL;
937 if (priv->inspector_window) {
938 gtk_widget_destroy (priv->inspector_window);
939 priv->inspector_window = NULL;
942 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
946 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
947 EmpathyThemeAdium *theme)
949 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
951 if (priv->inspector_window) {
952 gtk_widget_show_all (priv->inspector_window);
959 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
960 EmpathyThemeAdium *theme)
962 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
964 if (priv->inspector_window) {
965 gtk_widget_hide (priv->inspector_window);
971 static WebKitWebView *
972 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
973 WebKitWebView *web_view,
974 EmpathyThemeAdium *theme)
976 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
977 GtkWidget *scrolled_window;
978 GtkWidget *inspector_web_view;
980 if (!priv->inspector_window) {
981 /* Create main window */
982 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
983 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
985 g_signal_connect (priv->inspector_window, "delete-event",
986 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
988 /* Pack a scrolled window */
989 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
990 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
991 GTK_POLICY_AUTOMATIC,
992 GTK_POLICY_AUTOMATIC);
993 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
995 gtk_widget_show (scrolled_window);
997 /* Pack a webview in the scrolled window. That webview will be
998 * used to render the inspector tool. */
999 inspector_web_view = webkit_web_view_new ();
1000 gtk_container_add (GTK_CONTAINER (scrolled_window),
1001 inspector_web_view);
1002 gtk_widget_show (scrolled_window);
1004 return WEBKIT_WEB_VIEW (inspector_web_view);
1010 static PangoFontDescription *
1011 theme_adium_get_default_font (void)
1013 GConfClient *gconf_client;
1014 PangoFontDescription *pango_fd;
1015 gchar *gconf_font_family;
1017 gconf_client = gconf_client_get_default ();
1018 if (gconf_client == NULL) {
1021 gconf_font_family = gconf_client_get_string (gconf_client,
1022 EMPATHY_GCONF_FONT_KEY_NAME,
1024 if (gconf_font_family == NULL) {
1025 g_object_unref (gconf_client);
1028 pango_fd = pango_font_description_from_string (gconf_font_family);
1029 g_free (gconf_font_family);
1030 g_object_unref (gconf_client);
1035 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1039 g_object_set (w_settings, "default-font-family", name, NULL);
1040 g_object_set (w_settings, "default-font-size", size, NULL);
1044 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1046 PangoFontDescription *default_font_desc;
1047 GdkScreen *current_screen;
1049 gint pango_font_size = 0;
1051 default_font_desc = theme_adium_get_default_font ();
1052 if (default_font_desc == NULL)
1054 pango_font_size = pango_font_description_get_size (default_font_desc)
1056 if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1057 current_screen = gdk_screen_get_default ();
1058 if (current_screen != NULL) {
1059 dpi = gdk_screen_get_resolution (current_screen);
1061 dpi = BORING_DPI_DEFAULT;
1063 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1065 theme_adium_set_webkit_font (w_settings,
1066 pango_font_description_get_family (default_font_desc),
1068 pango_font_description_free (default_font_desc);
1072 theme_adium_constructed (GObject *object)
1074 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1076 const gchar *font_family = NULL;
1078 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
1079 WebKitWebSettings *webkit_settings;
1080 WebKitWebInspector *webkit_inspector;
1082 /* Set default settings */
1083 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1084 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1085 webkit_settings = webkit_web_view_get_settings (webkit_view);
1087 if (font_family && font_size) {
1088 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1090 theme_adium_set_default_font (webkit_settings);
1093 /* Setup webkit inspector */
1094 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1095 g_signal_connect (webkit_inspector, "inspect-web-view",
1096 G_CALLBACK (theme_adium_inspect_web_view_cb),
1098 g_signal_connect (webkit_inspector, "show-window",
1099 G_CALLBACK (theme_adium_inspector_show_window_cb),
1101 g_signal_connect (webkit_inspector, "close-window",
1102 G_CALLBACK (theme_adium_inspector_close_window_cb),
1106 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1107 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1108 priv->data->template_html,
1110 g_free (basedir_uri);
1114 theme_adium_get_property (GObject *object,
1119 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1122 case PROP_ADIUM_DATA:
1123 g_value_set_boxed (value, priv->data);
1126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1132 theme_adium_set_property (GObject *object,
1134 const GValue *value,
1137 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1140 case PROP_ADIUM_DATA:
1141 g_assert (priv->data == NULL);
1142 priv->data = g_value_dup_boxed (value);
1145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1151 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1154 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1156 object_class->finalize = theme_adium_finalize;
1157 object_class->dispose = theme_adium_dispose;
1158 object_class->constructed = theme_adium_constructed;
1159 object_class->get_property = theme_adium_get_property;
1160 object_class->set_property = theme_adium_set_property;
1162 widget_class->button_press_event = theme_adium_button_press_event;
1164 g_object_class_install_property (object_class,
1166 g_param_spec_boxed ("adium-data",
1168 "Data for the adium theme",
1169 EMPATHY_TYPE_ADIUM_DATA,
1170 G_PARAM_CONSTRUCT_ONLY |
1172 G_PARAM_STATIC_STRINGS));
1174 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1178 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1180 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1181 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1185 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1187 g_signal_connect (theme, "load-finished",
1188 G_CALLBACK (theme_adium_load_finished_cb),
1190 g_signal_connect (theme, "navigation-policy-decision-requested",
1191 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1194 priv->notify_enable_webkit_developer_tools_id =
1195 empathy_conf_notify_add (empathy_conf_get (),
1196 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1197 theme_adium_notify_enable_webkit_developer_tools_cb,
1200 theme_adium_update_enable_webkit_developer_tools (theme);
1204 empathy_theme_adium_new (EmpathyAdiumData *data)
1206 g_return_val_if_fail (data != NULL, NULL);
1208 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1214 empathy_adium_path_is_valid (const gchar *path)
1219 /* The theme is not valid if there is no Info.plist */
1220 file = g_build_filename (path, "Contents", "Info.plist",
1222 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1228 /* We ship a default Template.html as fallback if there is any problem
1229 * with the one inside the theme. The only other required file is
1230 * Content.html for incoming messages (outgoing fallback to use
1232 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1233 "Content.html", NULL);
1234 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1241 empathy_adium_info_new (const gchar *path)
1245 GHashTable *info = NULL;
1247 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1249 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1250 value = empathy_plist_parse_from_file (file);
1256 info = g_value_dup_boxed (value);
1257 tp_g_value_slice_free (value);
1259 /* Insert the theme's path into the hash table,
1260 * keys have to be dupped */
1261 tp_asv_set_string (info, g_strdup ("path"), path);
1267 empathy_adium_data_get_type (void)
1269 static GType type_id = 0;
1273 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1274 (GBoxedCopyFunc) empathy_adium_data_ref,
1275 (GBoxedFreeFunc) empathy_adium_data_unref);
1282 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1284 EmpathyAdiumData *data;
1286 gchar *template_html = NULL;
1288 gchar *footer_html = NULL;
1291 gchar **strv = NULL;
1296 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1298 data = g_slice_new0 (EmpathyAdiumData);
1299 data->ref_count = 1;
1300 data->path = g_strdup (path);
1301 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1302 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1303 data->info = g_hash_table_ref (info);
1305 /* Load html files */
1306 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1307 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1310 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1311 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1314 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1315 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1318 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1319 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1322 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1323 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1326 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1327 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1330 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1331 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1334 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1335 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1338 file = g_build_filename (data->basedir, "Status.html", NULL);
1339 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1342 file = g_build_filename (data->basedir, "Footer.html", NULL);
1343 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1346 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1347 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1348 data->default_incoming_avatar_filename = file;
1353 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1354 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1355 data->default_outgoing_avatar_filename = file;
1360 css_path = g_build_filename (data->basedir, "main.css", NULL);
1362 /* There is 2 formats for Template.html: The old one has 4 parameters,
1363 * the new one has 5 parameters. */
1364 file = g_build_filename (data->basedir, "Template.html", NULL);
1365 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1366 strv = g_strsplit (template_html, "%@", -1);
1367 len = g_strv_length (strv);
1371 if (len != 5 && len != 6) {
1372 /* Either the theme has no template or it don't have the good
1373 * number of parameters. Fallback to use our own template. */
1374 g_free (template_html);
1377 file = empathy_file_lookup ("Template.html", "data");
1378 g_file_get_contents (file, &template_html, &template_len, NULL);
1380 strv = g_strsplit (template_html, "%@", -1);
1381 len = g_strv_length (strv);
1384 /* Replace %@ with the needed information in the template html. */
1385 string = g_string_sized_new (template_len);
1386 g_string_append (string, strv[i++]);
1387 g_string_append (string, data->basedir);
1388 g_string_append (string, strv[i++]);
1390 const gchar *variant;
1392 /* We include main.css by default */
1393 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1394 g_string_append (string, strv[i++]);
1395 variant = tp_asv_get_string (data->info, "DefaultVariant");
1397 g_string_append (string, "Variants/");
1398 g_string_append (string, variant);
1399 g_string_append (string, ".css");
1402 /* FIXME: We should set main.css OR the variant css */
1403 g_string_append (string, css_path);
1405 g_string_append (string, strv[i++]);
1406 g_string_append (string, ""); /* We don't want header */
1407 g_string_append (string, strv[i++]);
1408 /* FIXME: We should replace adium %macros% in footer */
1410 g_string_append (string, footer_html);
1412 g_string_append (string, strv[i++]);
1413 data->template_html = g_string_free (string, FALSE);
1415 g_free (footer_html);
1416 g_free (template_html);
1424 empathy_adium_data_new (const gchar *path)
1426 EmpathyAdiumData *data;
1429 info = empathy_adium_info_new (path);
1430 data = empathy_adium_data_new_with_info (path, info);
1431 g_hash_table_unref (info);
1437 empathy_adium_data_ref (EmpathyAdiumData *data)
1439 g_return_val_if_fail (data != NULL, NULL);
1441 g_atomic_int_inc (&data->ref_count);
1447 empathy_adium_data_unref (EmpathyAdiumData *data)
1449 g_return_if_fail (data != NULL);
1451 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1452 g_free (data->path);
1453 g_free (data->basedir);
1454 g_free (data->template_html);
1455 g_free (data->in_content_html);
1456 g_free (data->in_nextcontent_html);
1457 g_free (data->in_context_html);
1458 g_free (data->in_nextcontext_html);
1459 g_free (data->out_content_html);
1460 g_free (data->out_nextcontent_html);
1461 g_free (data->out_context_html);
1462 g_free (data->out_nextcontext_html);
1463 g_free (data->default_avatar_filename);
1464 g_free (data->default_incoming_avatar_filename);
1465 g_free (data->default_outgoing_avatar_filename);
1466 g_free (data->status_html);
1467 g_hash_table_unref (data->info);
1468 g_slice_free (EmpathyAdiumData, data);
1473 empathy_adium_data_get_info (EmpathyAdiumData *data)
1475 g_return_val_if_fail (data != NULL, NULL);
1481 empathy_adium_data_get_path (EmpathyAdiumData *data)
1483 g_return_val_if_fail (data != NULL, NULL);