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 <pango/pango.h>
34 #include <libempathy/empathy-gsettings.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-ui-utils.h"
41 #include "empathy-plist.h"
42 #include "empathy-string-parser.h"
43 #include "empathy-images.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 #define BORING_DPI_DEFAULT 96
52 /* "Join" consecutive messages with timestamps within five minutes */
53 #define MESSAGE_JOIN_PERIOD 5*60
56 EmpathyAdiumData *data;
57 EmpathySmileyManager *smiley_manager;
58 EmpathyContact *last_contact;
59 time_t last_timestamp;
60 gboolean last_is_backlog;
63 GtkWidget *inspector_window;
64 GSettings *gsettings_chat;
65 } EmpathyThemeAdiumPriv;
67 struct _EmpathyAdiumData {
71 gchar *default_avatar_filename;
72 gchar *default_incoming_avatar_filename;
73 gchar *default_outgoing_avatar_filename;
75 gchar *in_content_html;
77 gchar *in_context_html;
79 gchar *in_nextcontent_html;
80 gsize in_nextcontent_len;
81 gchar *in_nextcontext_html;
82 gsize in_nextcontext_len;
83 gchar *out_content_html;
84 gsize out_content_len;
85 gchar *out_context_html;
86 gsize out_context_len;
87 gchar *out_nextcontent_html;
88 gsize out_nextcontent_len;
89 gchar *out_nextcontext_html;
90 gsize out_nextcontext_len;
96 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
103 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
104 WEBKIT_TYPE_WEB_VIEW,
105 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
106 theme_adium_iface_init));
109 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
111 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
112 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
113 gboolean enable_webkit_developer_tools;
115 enable_webkit_developer_tools = g_settings_get_boolean (
116 priv->gsettings_chat,
117 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS);
119 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
120 "enable-developer-extras",
121 enable_webkit_developer_tools,
126 theme_adium_notify_enable_webkit_developer_tools_cb (GSettings *gsettings,
130 EmpathyThemeAdium *theme = user_data;
132 theme_adium_update_enable_webkit_developer_tools (theme);
136 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView *view,
137 WebKitWebFrame *web_frame,
138 WebKitNetworkRequest *request,
139 WebKitWebNavigationAction *action,
140 WebKitWebPolicyDecision *decision,
145 /* Only call url_show on clicks */
146 if (webkit_web_navigation_action_get_reason (action) !=
147 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
148 webkit_web_policy_decision_use (decision);
152 uri = webkit_network_request_get_uri (request);
153 empathy_url_show (GTK_WIDGET (view), uri);
155 webkit_web_policy_decision_ignore (decision);
160 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
163 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
165 GtkClipboard *clipboard;
167 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
169 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
170 gtk_clipboard_set_text (clipboard, uri, -1);
172 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
173 gtk_clipboard_set_text (clipboard, uri, -1);
179 theme_adium_open_address_cb (GtkMenuItem *menuitem,
182 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
185 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
187 empathy_url_show (GTK_WIDGET (menuitem), uri);
193 theme_adium_match_newline (const gchar *text,
195 EmpathyStringReplace replace_func,
196 EmpathyStringParser *sub_parsers,
199 GString *string = user_data;
207 /* Replace \n by <br/> */
208 for (i = 0; i < len && text[i] != '\0'; i++) {
209 if (text[i] == '\n') {
210 empathy_string_parser_substr (text + prev,
211 i - prev, sub_parsers,
213 g_string_append (string, "<br/>");
217 empathy_string_parser_substr (text + prev, i - prev,
218 sub_parsers, user_data);
222 theme_adium_replace_smiley (const gchar *text,
227 EmpathySmileyHit *hit = match_data;
228 GString *string = user_data;
230 /* Replace smiley by a <img/> tag */
231 g_string_append_printf (string,
232 "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
233 hit->path, (int)len, text, (int)len, text);
236 static EmpathyStringParser string_parsers[] = {
237 {empathy_string_match_link, empathy_string_replace_link},
238 {theme_adium_match_newline, NULL},
239 {empathy_string_match_all, empathy_string_replace_escaped},
243 static EmpathyStringParser string_parsers_with_smiley[] = {
244 {empathy_string_match_link, empathy_string_replace_link},
245 {empathy_string_match_smiley, theme_adium_replace_smiley},
246 {theme_adium_match_newline, NULL},
247 {empathy_string_match_all, empathy_string_replace_escaped},
252 theme_adium_parse_body (EmpathyThemeAdium *self,
255 EmpathyThemeAdiumPriv *priv = GET_PRIV (self);
256 EmpathyStringParser *parsers;
259 /* Check if we have to parse smileys */
260 if (g_settings_get_boolean (priv->gsettings_chat,
261 EMPATHY_PREFS_CHAT_SHOW_SMILEYS))
262 parsers = string_parsers_with_smiley;
264 parsers = string_parsers;
266 /* Parse text and construct string with links and smileys replaced
267 * by html tags. Also escape text to make sure html code is
268 * displayed verbatim. */
269 string = g_string_sized_new (strlen (text));
270 empathy_string_parser_substr (text, -1, parsers, string);
272 /* Wrap body in order to make tabs and multiple spaces displayed
273 * properly. See bug #625745. */
274 g_string_prepend (string, "<div style=\"display: inline; "
275 "white-space: pre-wrap\"'>");
276 g_string_append (string, "</div>");
278 return g_string_free (string, FALSE);
282 escape_and_append_len (GString *string, const gchar *str, gint len)
284 while (*str != '\0' && len != 0) {
288 g_string_append (string, "\\\\");
292 g_string_append (string, "\\\"");
295 /* Remove end of lines */
298 g_string_append_c (string, *str);
307 theme_adium_match (const gchar **str, const gchar *match)
311 len = strlen (match);
312 if (strncmp (*str, match, len) == 0) {
321 theme_adium_append_html (EmpathyThemeAdium *theme,
323 const gchar *html, gsize len,
324 const gchar *message,
325 const gchar *avatar_filename,
327 const gchar *contact_id,
328 const gchar *service_name,
329 const gchar *message_classes,
334 const gchar *cur = NULL;
337 /* Make some search-and-replace in the html code */
338 string = g_string_sized_new (len + strlen (message));
339 g_string_append_printf (string, "%s(\"", func);
340 for (cur = html; *cur != '\0'; cur++) {
341 const gchar *replace = NULL;
342 gchar *dup_replace = NULL;
344 if (theme_adium_match (&cur, "%message%")) {
346 } else if (theme_adium_match (&cur, "%messageClasses%")) {
347 replace = message_classes;
348 } else if (theme_adium_match (&cur, "%userIconPath%")) {
349 replace = avatar_filename;
350 } else if (theme_adium_match (&cur, "%sender%")) {
352 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
353 replace = contact_id;
354 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
355 /* %senderDisplayName% -
356 * "The serverside (remotely set) name of the sender,
357 * such as an MSN display name."
359 * We don't have access to that yet so we use local
362 } else if (theme_adium_match (&cur, "%service%")) {
363 replace = service_name;
364 } else if (theme_adium_match (&cur, "%shortTime%")) {
365 dup_replace = empathy_time_to_string_local (timestamp,
366 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
367 replace = dup_replace;
368 } else if (theme_adium_match (&cur, "%time")) {
369 gchar *format = NULL;
371 /* Time can be in 2 formats:
372 * %time% or %time{strftime format}%
373 * Extract the time format if provided. */
376 end = strstr (cur, "}%");
381 format = g_strndup (cur, end - cur);
388 dup_replace = empathy_time_to_string_local (timestamp,
389 format ? format : EMPATHY_TIME_DATE_FORMAT_DISPLAY_SHORT);
391 dup_replace = empathy_time_to_string_local (timestamp,
392 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
394 replace = dup_replace;
397 escape_and_append_len (string, cur, 1);
401 /* Here we have a replacement to make */
402 escape_and_append_len (string, replace, -1);
403 g_free (dup_replace);
405 g_string_append (string, "\")");
407 script = g_string_free (string, FALSE);
408 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
413 theme_adium_append_event_escaped (EmpathyChatView *view,
414 const gchar *escaped)
416 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
417 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
419 if (priv->data->status_html) {
420 theme_adium_append_html (theme, "appendMessage",
421 priv->data->status_html,
422 priv->data->status_len,
423 escaped, NULL, NULL, NULL, NULL,
424 "event", empathy_time_get_current (), FALSE);
427 /* There is no last contact */
428 if (priv->last_contact) {
429 g_object_unref (priv->last_contact);
430 priv->last_contact = NULL;
435 theme_adium_append_message (EmpathyChatView *view,
438 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
439 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
440 EmpathyContact *sender;
445 const gchar *contact_id;
446 EmpathyAvatar *avatar;
447 const gchar *avatar_filename = NULL;
452 const gchar *service_name;
453 GString *message_classes = NULL;
455 gboolean consecutive;
457 if (!priv->page_loaded) {
458 priv->message_queue = g_list_prepend (priv->message_queue,
463 /* Get information */
464 sender = empathy_message_get_sender (msg);
465 account = empathy_contact_get_account (sender);
466 service_name = empathy_protocol_name_to_display_name
467 (tp_account_get_protocol (account));
468 if (service_name == NULL)
469 service_name = tp_account_get_protocol (account);
470 timestamp = empathy_message_get_timestamp (msg);
471 body = empathy_message_get_body (msg);
472 body_escaped = theme_adium_parse_body (theme, body);
473 name = empathy_contact_get_alias (sender);
474 contact_id = empathy_contact_get_id (sender);
476 /* If this is a /me, append an event */
477 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
480 str = g_strdup_printf ("%s %s", name, body_escaped);
481 theme_adium_append_event_escaped (view, str);
484 g_free (body_escaped);
488 /* Get the avatar filename, or a fallback */
489 avatar = empathy_contact_get_avatar (sender);
491 avatar_filename = avatar->filename;
493 if (!avatar_filename) {
494 if (empathy_contact_is_user (sender)) {
495 avatar_filename = priv->data->default_outgoing_avatar_filename;
497 avatar_filename = priv->data->default_incoming_avatar_filename;
499 if (!avatar_filename) {
500 if (!priv->data->default_avatar_filename) {
501 priv->data->default_avatar_filename =
502 empathy_filename_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
503 GTK_ICON_SIZE_DIALOG);
505 avatar_filename = priv->data->default_avatar_filename;
509 /* We want to join this message with the last one if
510 * - senders are the same contact,
511 * - last message was recieved recently,
512 * - last message and this message both are/aren't backlog, and
513 * - DisableCombineConsecutive is not set in theme's settings */
514 is_backlog = empathy_message_is_backlog (msg);
515 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
516 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
517 (is_backlog == priv->last_is_backlog) &&
518 !tp_asv_get_boolean (priv->data->info,
519 "DisableCombineConsecutive", NULL);
521 /* Define message classes */
522 message_classes = g_string_new ("message");
524 g_string_append (message_classes, " history");
527 g_string_append (message_classes, " consecutive");
529 if (empathy_contact_is_user (sender)) {
530 g_string_append (message_classes, " outgoing");
532 g_string_append (message_classes, " incoming");
535 /* Define javascript function to use */
537 func = "appendNextMessage";
539 func = "appendMessage";
543 if (empathy_contact_is_user (sender)) {
546 html = priv->data->out_nextcontext_html;
547 len = priv->data->out_nextcontext_len;
550 /* Not backlog, or fallback if NextContext.html
553 html = priv->data->out_nextcontent_html;
554 len = priv->data->out_nextcontent_len;
558 /* Not consecutive, or fallback if NextContext.html and/or
559 * NextContent.html are missing */
562 html = priv->data->out_context_html;
563 len = priv->data->out_context_len;
567 html = priv->data->out_content_html;
568 len = priv->data->out_content_len;
573 /* Incoming, or fallback if outgoing files are missing */
577 html = priv->data->in_nextcontext_html;
578 len = priv->data->in_nextcontext_len;
581 /* Note backlog, or fallback if NextContext.html
584 html = priv->data->in_nextcontent_html;
585 len = priv->data->in_nextcontent_len;
589 /* Not consecutive, or fallback if NextContext.html and/or
590 * NextContent.html are missing */
593 html = priv->data->in_context_html;
594 len = priv->data->in_context_len;
598 html = priv->data->in_content_html;
599 len = priv->data->in_content_len;
605 theme_adium_append_html (theme, func, html, len, body_escaped,
606 avatar_filename, name, contact_id,
607 service_name, message_classes->str,
608 timestamp, is_backlog);
610 DEBUG ("Couldn't find HTML file for this message");
613 /* Keep the sender of the last displayed message */
614 if (priv->last_contact) {
615 g_object_unref (priv->last_contact);
617 priv->last_contact = g_object_ref (sender);
618 priv->last_timestamp = timestamp;
619 priv->last_is_backlog = is_backlog;
621 g_free (body_escaped);
622 g_string_free (message_classes, TRUE);
626 theme_adium_append_event (EmpathyChatView *view,
631 str_escaped = g_markup_escape_text (str, -1);
632 theme_adium_append_event_escaped (view, str_escaped);
633 g_free (str_escaped);
637 theme_adium_scroll (EmpathyChatView *view,
638 gboolean allow_scrolling)
640 /* FIXME: Is it possible? I guess we need a js function, but I don't
645 theme_adium_scroll_down (EmpathyChatView *view)
647 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
651 theme_adium_get_has_selection (EmpathyChatView *view)
653 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
657 theme_adium_clear (EmpathyChatView *view)
659 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
662 priv->page_loaded = FALSE;
663 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
664 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
665 priv->data->template_html,
667 g_free (basedir_uri);
669 /* Clear last contact to avoid trying to add a 'joined'
670 * message when we don't have an insertion point. */
671 if (priv->last_contact) {
672 g_object_unref (priv->last_contact);
673 priv->last_contact = NULL;
678 theme_adium_find_previous (EmpathyChatView *view,
679 const gchar *search_criteria,
683 /* FIXME: Doesn't respect new_search */
684 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
685 search_criteria, match_case,
690 theme_adium_find_next (EmpathyChatView *view,
691 const gchar *search_criteria,
695 /* FIXME: Doesn't respect new_search */
696 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
697 search_criteria, match_case,
702 theme_adium_find_abilities (EmpathyChatView *view,
703 const gchar *search_criteria,
705 gboolean *can_do_previous,
706 gboolean *can_do_next)
708 /* FIXME: Does webkit provide an API for that? We have wrap=true in
709 * find_next and find_previous to work around this problem. */
711 *can_do_previous = TRUE;
717 theme_adium_highlight (EmpathyChatView *view,
721 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
722 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
723 text, match_case, 0);
724 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
729 theme_adium_copy_clipboard (EmpathyChatView *view)
731 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
735 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
737 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
739 g_object_unref (hit_test_result);
743 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
745 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
746 WebKitHitTestResult *hit_test_result;
747 WebKitHitTestResultContext context;
751 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
752 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
755 menu = empathy_context_menu_new (GTK_WIDGET (view));
757 /* Select all item */
758 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
759 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
761 g_signal_connect_swapped (item, "activate",
762 G_CALLBACK (webkit_web_view_select_all),
766 if (webkit_web_view_can_copy_clipboard (view)) {
767 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
768 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
770 g_signal_connect_swapped (item, "activate",
771 G_CALLBACK (webkit_web_view_copy_clipboard),
775 /* Clear menu item */
776 item = gtk_separator_menu_item_new ();
777 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
779 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
780 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
782 g_signal_connect_swapped (item, "activate",
783 G_CALLBACK (empathy_chat_view_clear),
786 /* We will only add the following menu items if we are
787 * right-clicking a link */
788 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
790 item = gtk_separator_menu_item_new ();
791 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
793 /* Copy Link Address menu item */
794 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
795 g_signal_connect (item, "activate",
796 G_CALLBACK (theme_adium_copy_address_cb),
798 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
800 /* Open Link menu item */
801 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
802 g_signal_connect (item, "activate",
803 G_CALLBACK (theme_adium_open_address_cb),
805 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
808 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
809 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
812 /* Display the menu */
813 gtk_widget_show_all (menu);
814 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
815 event->button, event->time);
819 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
821 if (event->button == 3) {
822 gboolean developer_tools_enabled;
824 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
825 "enable-developer-extras", &developer_tools_enabled, NULL);
827 /* We currently have no way to add an inspector menu
828 * item ourselves, so we disable our customized menu
829 * if the developer extras are enabled. */
830 if (!developer_tools_enabled) {
831 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
836 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
840 theme_adium_iface_init (EmpathyChatViewIface *iface)
842 iface->append_message = theme_adium_append_message;
843 iface->append_event = theme_adium_append_event;
844 iface->scroll = theme_adium_scroll;
845 iface->scroll_down = theme_adium_scroll_down;
846 iface->get_has_selection = theme_adium_get_has_selection;
847 iface->clear = theme_adium_clear;
848 iface->find_previous = theme_adium_find_previous;
849 iface->find_next = theme_adium_find_next;
850 iface->find_abilities = theme_adium_find_abilities;
851 iface->highlight = theme_adium_highlight;
852 iface->copy_clipboard = theme_adium_copy_clipboard;
856 theme_adium_load_finished_cb (WebKitWebView *view,
857 WebKitWebFrame *frame,
860 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
861 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
863 DEBUG ("Page loaded");
864 priv->page_loaded = TRUE;
866 /* Display queued messages */
867 priv->message_queue = g_list_reverse (priv->message_queue);
868 while (priv->message_queue) {
869 EmpathyMessage *message = priv->message_queue->data;
871 theme_adium_append_message (chat_view, message);
872 priv->message_queue = g_list_remove (priv->message_queue, message);
873 g_object_unref (message);
878 theme_adium_finalize (GObject *object)
880 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
882 empathy_adium_data_unref (priv->data);
883 g_object_unref (priv->gsettings_chat);
885 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
889 theme_adium_dispose (GObject *object)
891 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
893 if (priv->smiley_manager) {
894 g_object_unref (priv->smiley_manager);
895 priv->smiley_manager = NULL;
898 if (priv->last_contact) {
899 g_object_unref (priv->last_contact);
900 priv->last_contact = NULL;
903 if (priv->inspector_window) {
904 gtk_widget_destroy (priv->inspector_window);
905 priv->inspector_window = NULL;
908 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
912 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
913 EmpathyThemeAdium *theme)
915 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
917 if (priv->inspector_window) {
918 gtk_widget_show_all (priv->inspector_window);
925 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
926 EmpathyThemeAdium *theme)
928 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
930 if (priv->inspector_window) {
931 gtk_widget_hide (priv->inspector_window);
937 static WebKitWebView *
938 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
939 WebKitWebView *web_view,
940 EmpathyThemeAdium *theme)
942 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
943 GtkWidget *scrolled_window;
944 GtkWidget *inspector_web_view;
946 if (!priv->inspector_window) {
947 /* Create main window */
948 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
949 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
951 g_signal_connect (priv->inspector_window, "delete-event",
952 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
954 /* Pack a scrolled window */
955 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
956 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
957 GTK_POLICY_AUTOMATIC,
958 GTK_POLICY_AUTOMATIC);
959 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
961 gtk_widget_show (scrolled_window);
963 /* Pack a webview in the scrolled window. That webview will be
964 * used to render the inspector tool. */
965 inspector_web_view = webkit_web_view_new ();
966 gtk_container_add (GTK_CONTAINER (scrolled_window),
968 gtk_widget_show (scrolled_window);
970 return WEBKIT_WEB_VIEW (inspector_web_view);
976 static PangoFontDescription *
977 theme_adium_get_default_font (void)
979 GSettings *gsettings;
980 PangoFontDescription *pango_fd;
983 gsettings = g_settings_new (EMPATHY_PREFS_DESKTOP_INTERFACE_SCHEMA);
985 font_family = g_settings_get_string (gsettings,
986 EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME);
988 if (font_family == NULL)
991 pango_fd = pango_font_description_from_string (font_family);
992 g_free (font_family);
993 g_object_unref (gsettings);
998 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1002 g_object_set (w_settings, "default-font-family", name, NULL);
1003 g_object_set (w_settings, "default-font-size", size, NULL);
1007 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1009 PangoFontDescription *default_font_desc;
1010 GdkScreen *current_screen;
1012 gint pango_font_size = 0;
1014 default_font_desc = theme_adium_get_default_font ();
1015 if (default_font_desc == NULL)
1017 pango_font_size = pango_font_description_get_size (default_font_desc)
1019 if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1020 current_screen = gdk_screen_get_default ();
1021 if (current_screen != NULL) {
1022 dpi = gdk_screen_get_resolution (current_screen);
1024 dpi = BORING_DPI_DEFAULT;
1026 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1028 theme_adium_set_webkit_font (w_settings,
1029 pango_font_description_get_family (default_font_desc),
1031 pango_font_description_free (default_font_desc);
1035 theme_adium_constructed (GObject *object)
1037 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1039 const gchar *font_family = NULL;
1041 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
1042 WebKitWebSettings *webkit_settings;
1043 WebKitWebInspector *webkit_inspector;
1045 /* Set default settings */
1046 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1047 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1048 webkit_settings = webkit_web_view_get_settings (webkit_view);
1050 if (font_family && font_size) {
1051 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1053 theme_adium_set_default_font (webkit_settings);
1056 /* Setup webkit inspector */
1057 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1058 g_signal_connect (webkit_inspector, "inspect-web-view",
1059 G_CALLBACK (theme_adium_inspect_web_view_cb),
1061 g_signal_connect (webkit_inspector, "show-window",
1062 G_CALLBACK (theme_adium_inspector_show_window_cb),
1064 g_signal_connect (webkit_inspector, "close-window",
1065 G_CALLBACK (theme_adium_inspector_close_window_cb),
1069 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1070 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1071 priv->data->template_html,
1073 g_free (basedir_uri);
1077 theme_adium_get_property (GObject *object,
1082 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1085 case PROP_ADIUM_DATA:
1086 g_value_set_boxed (value, priv->data);
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1095 theme_adium_set_property (GObject *object,
1097 const GValue *value,
1100 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1103 case PROP_ADIUM_DATA:
1104 g_assert (priv->data == NULL);
1105 priv->data = g_value_dup_boxed (value);
1108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1114 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1116 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1117 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1119 object_class->finalize = theme_adium_finalize;
1120 object_class->dispose = theme_adium_dispose;
1121 object_class->constructed = theme_adium_constructed;
1122 object_class->get_property = theme_adium_get_property;
1123 object_class->set_property = theme_adium_set_property;
1125 widget_class->button_press_event = theme_adium_button_press_event;
1127 g_object_class_install_property (object_class,
1129 g_param_spec_boxed ("adium-data",
1131 "Data for the adium theme",
1132 EMPATHY_TYPE_ADIUM_DATA,
1133 G_PARAM_CONSTRUCT_ONLY |
1135 G_PARAM_STATIC_STRINGS));
1137 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1141 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1143 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1144 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1148 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1150 g_signal_connect (theme, "load-finished",
1151 G_CALLBACK (theme_adium_load_finished_cb),
1153 g_signal_connect (theme, "navigation-policy-decision-requested",
1154 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1157 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1158 g_signal_connect (priv->gsettings_chat,
1159 "changed::" EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1160 G_CALLBACK (theme_adium_notify_enable_webkit_developer_tools_cb),
1163 theme_adium_update_enable_webkit_developer_tools (theme);
1167 empathy_theme_adium_new (EmpathyAdiumData *data)
1169 g_return_val_if_fail (data != NULL, NULL);
1171 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1177 empathy_adium_path_is_valid (const gchar *path)
1182 /* The theme is not valid if there is no Info.plist */
1183 file = g_build_filename (path, "Contents", "Info.plist",
1185 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1191 /* We ship a default Template.html as fallback if there is any problem
1192 * with the one inside the theme. The only other required file is
1193 * Content.html for incoming messages (outgoing fallback to use
1195 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1196 "Content.html", NULL);
1197 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1204 empathy_adium_info_new (const gchar *path)
1208 GHashTable *info = NULL;
1210 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1212 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1213 value = empathy_plist_parse_from_file (file);
1219 info = g_value_dup_boxed (value);
1220 tp_g_value_slice_free (value);
1222 /* Insert the theme's path into the hash table,
1223 * keys have to be dupped */
1224 tp_asv_set_string (info, g_strdup ("path"), path);
1230 empathy_adium_data_get_type (void)
1232 static GType type_id = 0;
1236 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1237 (GBoxedCopyFunc) empathy_adium_data_ref,
1238 (GBoxedFreeFunc) empathy_adium_data_unref);
1245 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1247 EmpathyAdiumData *data;
1249 gchar *template_html = NULL;
1251 gchar *footer_html = NULL;
1254 gchar **strv = NULL;
1259 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1261 data = g_slice_new0 (EmpathyAdiumData);
1262 data->ref_count = 1;
1263 data->path = g_strdup (path);
1264 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1265 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1266 data->info = g_hash_table_ref (info);
1268 /* Load html files */
1269 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1270 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1273 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1274 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1277 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1278 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1281 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1282 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1285 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1286 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1289 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1290 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1293 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1294 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1297 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1298 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1301 file = g_build_filename (data->basedir, "Status.html", NULL);
1302 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1305 file = g_build_filename (data->basedir, "Footer.html", NULL);
1306 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1309 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1310 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1311 data->default_incoming_avatar_filename = file;
1316 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1317 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1318 data->default_outgoing_avatar_filename = file;
1323 css_path = g_build_filename (data->basedir, "main.css", NULL);
1325 /* There is 2 formats for Template.html: The old one has 4 parameters,
1326 * the new one has 5 parameters. */
1327 file = g_build_filename (data->basedir, "Template.html", NULL);
1328 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1329 strv = g_strsplit (template_html, "%@", -1);
1330 len = g_strv_length (strv);
1334 if (len != 5 && len != 6) {
1335 /* Either the theme has no template or it don't have the good
1336 * number of parameters. Fallback to use our own template. */
1337 g_free (template_html);
1340 file = empathy_file_lookup ("Template.html", "data");
1341 g_file_get_contents (file, &template_html, &template_len, NULL);
1343 strv = g_strsplit (template_html, "%@", -1);
1344 len = g_strv_length (strv);
1347 /* Replace %@ with the needed information in the template html. */
1348 string = g_string_sized_new (template_len);
1349 g_string_append (string, strv[i++]);
1350 g_string_append (string, data->basedir);
1351 g_string_append (string, strv[i++]);
1353 const gchar *variant;
1355 /* We include main.css by default */
1356 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1357 g_string_append (string, strv[i++]);
1358 variant = tp_asv_get_string (data->info, "DefaultVariant");
1360 g_string_append (string, "Variants/");
1361 g_string_append (string, variant);
1362 g_string_append (string, ".css");
1365 /* FIXME: We should set main.css OR the variant css */
1366 g_string_append (string, css_path);
1368 g_string_append (string, strv[i++]);
1369 g_string_append (string, ""); /* We don't want header */
1370 g_string_append (string, strv[i++]);
1371 /* FIXME: We should replace adium %macros% in footer */
1373 g_string_append (string, footer_html);
1375 g_string_append (string, strv[i++]);
1376 data->template_html = g_string_free (string, FALSE);
1378 g_free (footer_html);
1379 g_free (template_html);
1387 empathy_adium_data_new (const gchar *path)
1389 EmpathyAdiumData *data;
1392 info = empathy_adium_info_new (path);
1393 data = empathy_adium_data_new_with_info (path, info);
1394 g_hash_table_unref (info);
1400 empathy_adium_data_ref (EmpathyAdiumData *data)
1402 g_return_val_if_fail (data != NULL, NULL);
1404 g_atomic_int_inc (&data->ref_count);
1410 empathy_adium_data_unref (EmpathyAdiumData *data)
1412 g_return_if_fail (data != NULL);
1414 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1415 g_free (data->path);
1416 g_free (data->basedir);
1417 g_free (data->template_html);
1418 g_free (data->in_content_html);
1419 g_free (data->in_nextcontent_html);
1420 g_free (data->in_context_html);
1421 g_free (data->in_nextcontext_html);
1422 g_free (data->out_content_html);
1423 g_free (data->out_nextcontent_html);
1424 g_free (data->out_context_html);
1425 g_free (data->out_nextcontext_html);
1426 g_free (data->default_avatar_filename);
1427 g_free (data->default_incoming_avatar_filename);
1428 g_free (data->default_outgoing_avatar_filename);
1429 g_free (data->status_html);
1430 g_hash_table_unref (data->info);
1431 g_slice_free (EmpathyAdiumData, data);
1436 empathy_adium_data_get_info (EmpathyAdiumData *data)
1438 g_return_val_if_fail (data != NULL, NULL);
1444 empathy_adium_data_get_path (EmpathyAdiumData *data)
1446 g_return_val_if_fail (data != NULL, NULL);