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_smiley (const gchar *text,
230 EmpathySmileyHit *hit = match_data;
231 GString *string = user_data;
233 /* Replace smiley by a <img/> tag */
234 g_string_append_printf (string,
235 "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
236 hit->path, (int)len, text, (int)len, text);
239 static EmpathyStringParser string_parsers[] = {
240 {empathy_string_match_link, empathy_string_replace_link},
241 {theme_adium_match_newline, NULL},
242 {empathy_string_match_all, empathy_string_replace_escaped},
246 static EmpathyStringParser string_parsers_with_smiley[] = {
247 {empathy_string_match_link, empathy_string_replace_link},
248 {empathy_string_match_smiley, theme_adium_replace_smiley},
249 {theme_adium_match_newline, NULL},
250 {empathy_string_match_all, empathy_string_replace_escaped},
255 theme_adium_parse_body (const gchar *text)
257 EmpathyStringParser *parsers;
259 gboolean use_smileys;
261 /* Check if we have to parse smileys */
262 empathy_conf_get_bool (empathy_conf_get (),
263 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
266 parsers = string_parsers_with_smiley;
268 parsers = string_parsers;
270 /* Parse text and construct string with links and smileys replaced
271 * by html tags. Also escape text to make sure html code is
272 * displayed verbatim. */
273 string = g_string_sized_new (strlen (text));
274 empathy_string_parser_substr (text, -1, parsers, string);
276 return g_string_free (string, FALSE);
280 escape_and_append_len (GString *string, const gchar *str, gint len)
282 while (*str != '\0' && len != 0) {
286 g_string_append (string, "\\\\");
290 g_string_append (string, "\\\"");
293 /* Remove end of lines */
296 g_string_append_c (string, *str);
305 theme_adium_match (const gchar **str, const gchar *match)
309 len = strlen (match);
310 if (strncmp (*str, match, len) == 0) {
319 theme_adium_append_html (EmpathyThemeAdium *theme,
321 const gchar *html, gsize len,
322 const gchar *message,
323 const gchar *avatar_filename,
325 const gchar *contact_id,
326 const gchar *service_name,
327 const gchar *message_classes,
331 const gchar *cur = NULL;
334 /* Make some search-and-replace in the html code */
335 string = g_string_sized_new (len + strlen (message));
336 g_string_append_printf (string, "%s(\"", func);
337 for (cur = html; *cur != '\0'; cur++) {
338 const gchar *replace = NULL;
339 gchar *dup_replace = NULL;
341 if (theme_adium_match (&cur, "%message%")) {
343 } else if (theme_adium_match (&cur, "%messageClasses%")) {
344 replace = message_classes;
345 } else if (theme_adium_match (&cur, "%userIconPath%")) {
346 replace = avatar_filename;
347 } else if (theme_adium_match (&cur, "%sender%")) {
349 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
350 replace = contact_id;
351 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
352 /* %senderDisplayName% -
353 * "The serverside (remotely set) name of the sender,
354 * such as an MSN display name."
356 * We don't have access to that yet so we use local
359 } else if (theme_adium_match (&cur, "%service%")) {
360 replace = service_name;
361 } else if (theme_adium_match (&cur, "%shortTime%")) {
362 dup_replace = empathy_time_to_string_local (timestamp,
363 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
364 replace = dup_replace;
365 } else if (theme_adium_match (&cur, "%time")) {
366 gchar *format = NULL;
368 /* Time can be in 2 formats:
369 * %time% or %time{strftime format}%
370 * Extract the time format if provided. */
373 end = strstr (cur, "}%");
378 format = g_strndup (cur, end - cur);
384 dup_replace = empathy_time_to_string_local (timestamp,
385 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
386 replace = dup_replace;
389 escape_and_append_len (string, cur, 1);
393 /* Here we have a replacement to make */
394 escape_and_append_len (string, replace, -1);
395 g_free (dup_replace);
397 g_string_append (string, "\")");
399 script = g_string_free (string, FALSE);
400 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
405 theme_adium_append_event_escaped (EmpathyChatView *view,
406 const gchar *escaped)
408 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
409 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
411 if (priv->data->status_html) {
412 theme_adium_append_html (theme, "appendMessage",
413 priv->data->status_html,
414 priv->data->status_len,
415 escaped, NULL, NULL, NULL, NULL,
416 "event", empathy_time_get_current ());
419 /* There is no last contact */
420 if (priv->last_contact) {
421 g_object_unref (priv->last_contact);
422 priv->last_contact = NULL;
427 theme_adium_append_message (EmpathyChatView *view,
430 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
431 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
432 EmpathyContact *sender;
437 const gchar *contact_id;
438 EmpathyAvatar *avatar;
439 const gchar *avatar_filename = NULL;
444 const gchar *service_name;
445 GString *message_classes = NULL;
447 gboolean consecutive;
449 if (!priv->page_loaded) {
450 priv->message_queue = g_list_prepend (priv->message_queue,
455 /* Get information */
456 sender = empathy_message_get_sender (msg);
457 account = empathy_contact_get_account (sender);
458 service_name = empathy_protocol_name_to_display_name
459 (tp_account_get_protocol (account));
460 if (service_name == NULL)
461 service_name = tp_account_get_protocol (account);
462 timestamp = empathy_message_get_timestamp (msg);
463 body = empathy_message_get_body (msg);
464 body_escaped = theme_adium_parse_body (body);
465 name = empathy_contact_get_name (sender);
466 contact_id = empathy_contact_get_id (sender);
468 /* If this is a /me, append an event */
469 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
472 str = g_strdup_printf ("%s %s", name, body_escaped);
473 theme_adium_append_event_escaped (view, str);
476 g_free (body_escaped);
480 /* Get the avatar filename, or a fallback */
481 avatar = empathy_contact_get_avatar (sender);
483 avatar_filename = avatar->filename;
485 if (!avatar_filename) {
486 if (empathy_contact_is_user (sender)) {
487 avatar_filename = priv->data->default_outgoing_avatar_filename;
489 avatar_filename = priv->data->default_incoming_avatar_filename;
491 if (!avatar_filename) {
492 if (!priv->data->default_avatar_filename) {
493 priv->data->default_avatar_filename =
494 empathy_filename_from_icon_name ("stock_person",
495 GTK_ICON_SIZE_DIALOG);
497 avatar_filename = priv->data->default_avatar_filename;
501 /* We want to join this message with the last one if
502 * - senders are the same contact,
503 * - last message was recieved recently,
504 * - last message and this message both are/aren't backlog, and
505 * - DisableCombineConsecutive is not set in theme's settings */
506 is_backlog = empathy_message_is_backlog (msg);
507 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
508 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
509 (is_backlog == priv->last_is_backlog) &&
510 !tp_asv_get_boolean (priv->data->info,
511 "DisableCombineConsecutive", NULL);
513 /* Define message classes */
514 message_classes = g_string_new ("message");
516 g_string_append (message_classes, " history");
519 g_string_append (message_classes, " consecutive");
521 if (empathy_contact_is_user (sender)) {
522 g_string_append (message_classes, " outgoing");
524 g_string_append (message_classes, " incoming");
527 /* Define javascript function to use */
529 func = "appendNextMessage";
531 func = "appendMessage";
535 if (empathy_contact_is_user (sender)) {
538 html = priv->data->out_nextcontext_html;
539 len = priv->data->out_nextcontext_len;
542 /* Not backlog, or fallback if NextContext.html
545 html = priv->data->out_nextcontent_html;
546 len = priv->data->out_nextcontent_len;
550 /* Not consecutive, or fallback if NextContext.html and/or
551 * NextContent.html are missing */
554 html = priv->data->out_context_html;
555 len = priv->data->out_context_len;
559 html = priv->data->out_content_html;
560 len = priv->data->out_content_len;
565 /* Incoming, or fallback if outgoing files are missing */
569 html = priv->data->in_nextcontext_html;
570 len = priv->data->in_nextcontext_len;
573 /* Note backlog, or fallback if NextContext.html
576 html = priv->data->in_nextcontent_html;
577 len = priv->data->in_nextcontent_len;
581 /* Not consecutive, or fallback if NextContext.html and/or
582 * NextContent.html are missing */
585 html = priv->data->in_context_html;
586 len = priv->data->in_context_len;
590 html = priv->data->in_content_html;
591 len = priv->data->in_content_len;
597 theme_adium_append_html (theme, func, html, len, body_escaped,
598 avatar_filename, name, contact_id,
599 service_name, message_classes->str,
602 DEBUG ("Couldn't find HTML file for this message");
605 /* Keep the sender of the last displayed message */
606 if (priv->last_contact) {
607 g_object_unref (priv->last_contact);
609 priv->last_contact = g_object_ref (sender);
610 priv->last_timestamp = timestamp;
611 priv->last_is_backlog = is_backlog;
613 g_free (body_escaped);
614 g_string_free (message_classes, TRUE);
618 theme_adium_append_event (EmpathyChatView *view,
623 str_escaped = g_markup_escape_text (str, -1);
624 theme_adium_append_event_escaped (view, str_escaped);
625 g_free (str_escaped);
629 theme_adium_scroll (EmpathyChatView *view,
630 gboolean allow_scrolling)
632 /* FIXME: Is it possible? I guess we need a js function, but I don't
637 theme_adium_scroll_down (EmpathyChatView *view)
639 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
643 theme_adium_get_has_selection (EmpathyChatView *view)
645 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
649 theme_adium_clear (EmpathyChatView *view)
651 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
654 priv->page_loaded = FALSE;
655 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
656 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
657 priv->data->template_html,
659 g_free (basedir_uri);
661 /* Clear last contact to avoid trying to add a 'joined'
662 * message when we don't have an insertion point. */
663 if (priv->last_contact) {
664 g_object_unref (priv->last_contact);
665 priv->last_contact = NULL;
670 theme_adium_find_previous (EmpathyChatView *view,
671 const gchar *search_criteria,
675 /* FIXME: Doesn't respect new_search */
676 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
677 search_criteria, match_case,
682 theme_adium_find_next (EmpathyChatView *view,
683 const gchar *search_criteria,
687 /* FIXME: Doesn't respect new_search */
688 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
689 search_criteria, match_case,
694 theme_adium_find_abilities (EmpathyChatView *view,
695 const gchar *search_criteria,
697 gboolean *can_do_previous,
698 gboolean *can_do_next)
700 /* FIXME: Does webkit provide an API for that? We have wrap=true in
701 * find_next and find_previous to work around this problem. */
703 *can_do_previous = TRUE;
709 theme_adium_highlight (EmpathyChatView *view,
713 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
714 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
715 text, match_case, 0);
716 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
721 theme_adium_copy_clipboard (EmpathyChatView *view)
723 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
727 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
729 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
731 g_object_unref (hit_test_result);
735 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
737 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
738 WebKitHitTestResult *hit_test_result;
739 WebKitHitTestResultContext context;
743 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
744 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
747 menu = gtk_menu_new ();
749 /* Select all item */
750 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
751 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
753 g_signal_connect_swapped (item, "activate",
754 G_CALLBACK (webkit_web_view_select_all),
758 if (webkit_web_view_can_copy_clipboard (view)) {
759 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
760 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
762 g_signal_connect_swapped (item, "activate",
763 G_CALLBACK (webkit_web_view_copy_clipboard),
767 /* Clear menu item */
768 item = gtk_separator_menu_item_new ();
769 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
771 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
772 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
774 g_signal_connect_swapped (item, "activate",
775 G_CALLBACK (empathy_chat_view_clear),
778 /* We will only add the following menu items if we are
779 * right-clicking a link */
780 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
782 item = gtk_separator_menu_item_new ();
783 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
785 /* Copy Link Address menu item */
786 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
787 g_signal_connect (item, "activate",
788 G_CALLBACK (theme_adium_copy_address_cb),
790 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
792 /* Open Link menu item */
793 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
794 g_signal_connect (item, "activate",
795 G_CALLBACK (theme_adium_open_address_cb),
797 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
800 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
801 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
804 /* Display the menu */
805 gtk_widget_show_all (menu);
806 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
807 event->button, event->time);
808 g_object_ref_sink (menu);
809 g_object_unref (menu);
813 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
815 if (event->button == 3) {
816 gboolean developer_tools_enabled;
818 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
819 "enable-developer-extras", &developer_tools_enabled, NULL);
821 /* We currently have no way to add an inspector menu
822 * item ourselves, so we disable our customized menu
823 * if the developer extras are enabled. */
824 if (!developer_tools_enabled) {
825 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
830 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
834 theme_adium_iface_init (EmpathyChatViewIface *iface)
836 iface->append_message = theme_adium_append_message;
837 iface->append_event = theme_adium_append_event;
838 iface->scroll = theme_adium_scroll;
839 iface->scroll_down = theme_adium_scroll_down;
840 iface->get_has_selection = theme_adium_get_has_selection;
841 iface->clear = theme_adium_clear;
842 iface->find_previous = theme_adium_find_previous;
843 iface->find_next = theme_adium_find_next;
844 iface->find_abilities = theme_adium_find_abilities;
845 iface->highlight = theme_adium_highlight;
846 iface->copy_clipboard = theme_adium_copy_clipboard;
850 theme_adium_load_finished_cb (WebKitWebView *view,
851 WebKitWebFrame *frame,
854 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
855 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
857 DEBUG ("Page loaded");
858 priv->page_loaded = TRUE;
860 /* Display queued messages */
861 priv->message_queue = g_list_reverse (priv->message_queue);
862 while (priv->message_queue) {
863 EmpathyMessage *message = priv->message_queue->data;
865 theme_adium_append_message (chat_view, message);
866 priv->message_queue = g_list_remove (priv->message_queue, message);
867 g_object_unref (message);
872 theme_adium_finalize (GObject *object)
874 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
876 empathy_adium_data_unref (priv->data);
878 empathy_conf_notify_remove (empathy_conf_get (),
879 priv->notify_enable_webkit_developer_tools_id);
881 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
885 theme_adium_dispose (GObject *object)
887 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
889 if (priv->smiley_manager) {
890 g_object_unref (priv->smiley_manager);
891 priv->smiley_manager = NULL;
894 if (priv->last_contact) {
895 g_object_unref (priv->last_contact);
896 priv->last_contact = NULL;
899 if (priv->inspector_window) {
900 gtk_widget_destroy (priv->inspector_window);
901 priv->inspector_window = NULL;
904 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
908 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
909 EmpathyThemeAdium *theme)
911 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
913 if (priv->inspector_window) {
914 gtk_widget_show_all (priv->inspector_window);
921 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
922 EmpathyThemeAdium *theme)
924 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
926 if (priv->inspector_window) {
927 gtk_widget_hide (priv->inspector_window);
933 static WebKitWebView *
934 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
935 WebKitWebView *web_view,
936 EmpathyThemeAdium *theme)
938 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
939 GtkWidget *scrolled_window;
940 GtkWidget *inspector_web_view;
942 if (!priv->inspector_window) {
943 /* Create main window */
944 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
945 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
947 g_signal_connect (priv->inspector_window, "delete-event",
948 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
950 /* Pack a scrolled window */
951 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
952 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
953 GTK_POLICY_AUTOMATIC,
954 GTK_POLICY_AUTOMATIC);
955 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
957 gtk_widget_show (scrolled_window);
959 /* Pack a webview in the scrolled window. That webview will be
960 * used to render the inspector tool. */
961 inspector_web_view = webkit_web_view_new ();
962 gtk_container_add (GTK_CONTAINER (scrolled_window),
964 gtk_widget_show (scrolled_window);
966 return WEBKIT_WEB_VIEW (inspector_web_view);
972 static PangoFontDescription *
973 theme_adium_get_default_font (void)
975 GConfClient *gconf_client;
976 PangoFontDescription *pango_fd;
977 gchar *gconf_font_family;
979 gconf_client = gconf_client_get_default ();
980 if (gconf_client == NULL) {
983 gconf_font_family = gconf_client_get_string (gconf_client,
984 EMPATHY_GCONF_FONT_KEY_NAME,
986 if (gconf_font_family == NULL) {
987 g_object_unref (gconf_client);
990 pango_fd = pango_font_description_from_string (gconf_font_family);
991 g_free (gconf_font_family);
992 g_object_unref (gconf_client);
997 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1001 g_object_set (w_settings, "default-font-family", name, NULL);
1002 g_object_set (w_settings, "default-font-size", size, NULL);
1006 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1008 PangoFontDescription *default_font_desc;
1009 GdkScreen *current_screen;
1011 gint pango_font_size = 0;
1013 default_font_desc = theme_adium_get_default_font ();
1014 if (default_font_desc == NULL)
1016 pango_font_size = pango_font_description_get_size (default_font_desc)
1018 if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1019 current_screen = gdk_screen_get_default ();
1020 if (current_screen != NULL) {
1021 dpi = gdk_screen_get_resolution (current_screen);
1023 dpi = BORING_DPI_DEFAULT;
1025 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1027 theme_adium_set_webkit_font (w_settings,
1028 pango_font_description_get_family (default_font_desc),
1030 pango_font_description_free (default_font_desc);
1034 theme_adium_constructed (GObject *object)
1036 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1038 const gchar *font_family = NULL;
1040 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
1041 WebKitWebSettings *webkit_settings;
1042 WebKitWebInspector *webkit_inspector;
1044 /* Set default settings */
1045 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1046 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1047 webkit_settings = webkit_web_view_get_settings (webkit_view);
1049 if (font_family && font_size) {
1050 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1052 theme_adium_set_default_font (webkit_settings);
1055 /* Setup webkit inspector */
1056 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1057 g_signal_connect (webkit_inspector, "inspect-web-view",
1058 G_CALLBACK (theme_adium_inspect_web_view_cb),
1060 g_signal_connect (webkit_inspector, "show-window",
1061 G_CALLBACK (theme_adium_inspector_show_window_cb),
1063 g_signal_connect (webkit_inspector, "close-window",
1064 G_CALLBACK (theme_adium_inspector_close_window_cb),
1068 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1069 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1070 priv->data->template_html,
1072 g_free (basedir_uri);
1076 theme_adium_get_property (GObject *object,
1081 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1084 case PROP_ADIUM_DATA:
1085 g_value_set_boxed (value, priv->data);
1088 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1094 theme_adium_set_property (GObject *object,
1096 const GValue *value,
1099 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1102 case PROP_ADIUM_DATA:
1103 g_assert (priv->data == NULL);
1104 priv->data = g_value_dup_boxed (value);
1107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1113 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1115 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1116 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1118 object_class->finalize = theme_adium_finalize;
1119 object_class->dispose = theme_adium_dispose;
1120 object_class->constructed = theme_adium_constructed;
1121 object_class->get_property = theme_adium_get_property;
1122 object_class->set_property = theme_adium_set_property;
1124 widget_class->button_press_event = theme_adium_button_press_event;
1126 g_object_class_install_property (object_class,
1128 g_param_spec_boxed ("adium-data",
1130 "Data for the adium theme",
1131 EMPATHY_TYPE_ADIUM_DATA,
1132 G_PARAM_CONSTRUCT_ONLY |
1134 G_PARAM_STATIC_STRINGS));
1136 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1140 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1142 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1143 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1147 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1149 g_signal_connect (theme, "load-finished",
1150 G_CALLBACK (theme_adium_load_finished_cb),
1152 g_signal_connect (theme, "navigation-policy-decision-requested",
1153 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1156 priv->notify_enable_webkit_developer_tools_id =
1157 empathy_conf_notify_add (empathy_conf_get (),
1158 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1159 theme_adium_notify_enable_webkit_developer_tools_cb,
1162 theme_adium_update_enable_webkit_developer_tools (theme);
1166 empathy_theme_adium_new (EmpathyAdiumData *data)
1168 g_return_val_if_fail (data != NULL, NULL);
1170 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1176 empathy_adium_path_is_valid (const gchar *path)
1181 /* The theme is not valid if there is no Info.plist */
1182 file = g_build_filename (path, "Contents", "Info.plist",
1184 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1190 /* We ship a default Template.html as fallback if there is any problem
1191 * with the one inside the theme. The only other required file is
1192 * Content.html for incoming messages (outgoing fallback to use
1194 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1195 "Content.html", NULL);
1196 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1203 empathy_adium_info_new (const gchar *path)
1207 GHashTable *info = NULL;
1209 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1211 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1212 value = empathy_plist_parse_from_file (file);
1218 info = g_value_dup_boxed (value);
1219 tp_g_value_slice_free (value);
1221 /* Insert the theme's path into the hash table,
1222 * keys have to be dupped */
1223 tp_asv_set_string (info, g_strdup ("path"), path);
1229 empathy_adium_data_get_type (void)
1231 static GType type_id = 0;
1235 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1236 (GBoxedCopyFunc) empathy_adium_data_ref,
1237 (GBoxedFreeFunc) empathy_adium_data_unref);
1244 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1246 EmpathyAdiumData *data;
1248 gchar *template_html = NULL;
1250 gchar *footer_html = NULL;
1253 gchar **strv = NULL;
1258 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1260 data = g_slice_new0 (EmpathyAdiumData);
1261 data->ref_count = 1;
1262 data->path = g_strdup (path);
1263 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1264 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1265 data->info = g_hash_table_ref (info);
1267 /* Load html files */
1268 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1269 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1272 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1273 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1276 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1277 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1280 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1281 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1284 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1285 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1288 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1289 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1292 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1293 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1296 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1297 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1300 file = g_build_filename (data->basedir, "Status.html", NULL);
1301 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1304 file = g_build_filename (data->basedir, "Footer.html", NULL);
1305 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1308 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1309 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1310 data->default_incoming_avatar_filename = file;
1315 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1316 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1317 data->default_outgoing_avatar_filename = file;
1322 css_path = g_build_filename (data->basedir, "main.css", NULL);
1324 /* There is 2 formats for Template.html: The old one has 4 parameters,
1325 * the new one has 5 parameters. */
1326 file = g_build_filename (data->basedir, "Template.html", NULL);
1327 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1328 strv = g_strsplit (template_html, "%@", -1);
1329 len = g_strv_length (strv);
1333 if (len != 5 && len != 6) {
1334 /* Either the theme has no template or it don't have the good
1335 * number of parameters. Fallback to use our own template. */
1336 g_free (template_html);
1339 file = empathy_file_lookup ("Template.html", "data");
1340 g_file_get_contents (file, &template_html, &template_len, NULL);
1342 strv = g_strsplit (template_html, "%@", -1);
1343 len = g_strv_length (strv);
1346 /* Replace %@ with the needed information in the template html. */
1347 string = g_string_sized_new (template_len);
1348 g_string_append (string, strv[i++]);
1349 g_string_append (string, data->basedir);
1350 g_string_append (string, strv[i++]);
1352 const gchar *variant;
1354 /* We include main.css by default */
1355 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1356 g_string_append (string, strv[i++]);
1357 variant = tp_asv_get_string (data->info, "DefaultVariant");
1359 g_string_append (string, "Variants/");
1360 g_string_append (string, variant);
1361 g_string_append (string, ".css");
1364 /* FIXME: We should set main.css OR the variant css */
1365 g_string_append (string, css_path);
1367 g_string_append (string, strv[i++]);
1368 g_string_append (string, ""); /* We don't want header */
1369 g_string_append (string, strv[i++]);
1370 /* FIXME: We should replace adium %macros% in footer */
1372 g_string_append (string, footer_html);
1374 g_string_append (string, strv[i++]);
1375 data->template_html = g_string_free (string, FALSE);
1377 g_free (footer_html);
1378 g_free (template_html);
1386 empathy_adium_data_new (const gchar *path)
1388 EmpathyAdiumData *data;
1391 info = empathy_adium_info_new (path);
1392 data = empathy_adium_data_new_with_info (path, info);
1393 g_hash_table_unref (info);
1399 empathy_adium_data_ref (EmpathyAdiumData *data)
1401 g_return_val_if_fail (data != NULL, NULL);
1403 g_atomic_int_inc (&data->ref_count);
1409 empathy_adium_data_unref (EmpathyAdiumData *data)
1411 g_return_if_fail (data != NULL);
1413 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1414 g_free (data->path);
1415 g_free (data->basedir);
1416 g_free (data->template_html);
1417 g_free (data->in_content_html);
1418 g_free (data->in_nextcontent_html);
1419 g_free (data->in_context_html);
1420 g_free (data->in_nextcontext_html);
1421 g_free (data->out_content_html);
1422 g_free (data->out_nextcontent_html);
1423 g_free (data->out_context_html);
1424 g_free (data->out_nextcontext_html);
1425 g_free (data->default_avatar_filename);
1426 g_free (data->default_incoming_avatar_filename);
1427 g_free (data->default_outgoing_avatar_filename);
1428 g_free (data->status_html);
1429 g_hash_table_unref (data->info);
1430 g_slice_free (EmpathyAdiumData, data);
1435 empathy_adium_data_get_info (EmpathyAdiumData *data)
1437 g_return_val_if_fail (data != NULL, NULL);
1443 empathy_adium_data_get_path (EmpathyAdiumData *data)
1445 g_return_val_if_fail (data != NULL, NULL);