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-gsettings.h>
36 #include <libempathy/empathy-time.h>
37 #include <libempathy/empathy-utils.h>
39 #include "empathy-theme-adium.h"
40 #include "empathy-smiley-manager.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-plist.h"
43 #include "empathy-string-parser.h"
44 #include "empathy-images.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
51 /* GConf key containing current value of font */
52 #define EMPATHY_GCONF_FONT_KEY_NAME "/desktop/gnome/interface/document_font_name"
53 #define BORING_DPI_DEFAULT 96
55 /* "Join" consecutive messages with timestamps within five minutes */
56 #define MESSAGE_JOIN_PERIOD 5*60
59 EmpathyAdiumData *data;
60 EmpathySmileyManager *smiley_manager;
61 EmpathyContact *last_contact;
62 time_t last_timestamp;
63 gboolean last_is_backlog;
66 GtkWidget *inspector_window;
67 GSettings *gsettings_chat;
68 } EmpathyThemeAdiumPriv;
70 struct _EmpathyAdiumData {
74 gchar *default_avatar_filename;
75 gchar *default_incoming_avatar_filename;
76 gchar *default_outgoing_avatar_filename;
78 gchar *in_content_html;
80 gchar *in_context_html;
82 gchar *in_nextcontent_html;
83 gsize in_nextcontent_len;
84 gchar *in_nextcontext_html;
85 gsize in_nextcontext_len;
86 gchar *out_content_html;
87 gsize out_content_len;
88 gchar *out_context_html;
89 gsize out_context_len;
90 gchar *out_nextcontent_html;
91 gsize out_nextcontent_len;
92 gchar *out_nextcontext_html;
93 gsize out_nextcontext_len;
99 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
106 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
107 WEBKIT_TYPE_WEB_VIEW,
108 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
109 theme_adium_iface_init));
112 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
114 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
115 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
116 gboolean enable_webkit_developer_tools;
118 enable_webkit_developer_tools = g_settings_get_boolean (
119 priv->gsettings_chat,
120 EMPATHY_PREFS_CHAT_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 (GSettings *gsettings,
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 GSettings *gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
261 /* Check if we have to parse smileys */
262 if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_CHAT_SHOW_SMILEYS))
263 parsers = string_parsers_with_smiley;
265 parsers = string_parsers;
267 /* Parse text and construct string with links and smileys replaced
268 * by html tags. Also escape text to make sure html code is
269 * displayed verbatim. */
270 string = g_string_sized_new (strlen (text));
271 empathy_string_parser_substr (text, -1, parsers, string);
273 g_object_unref (gsettings);
275 return g_string_free (string, FALSE);
279 escape_and_append_len (GString *string, const gchar *str, gint len)
281 while (*str != '\0' && len != 0) {
285 g_string_append (string, "\\\\");
289 g_string_append (string, "\\\"");
292 /* Remove end of lines */
295 g_string_append_c (string, *str);
304 theme_adium_match (const gchar **str, const gchar *match)
308 len = strlen (match);
309 if (strncmp (*str, match, len) == 0) {
318 theme_adium_append_html (EmpathyThemeAdium *theme,
320 const gchar *html, gsize len,
321 const gchar *message,
322 const gchar *avatar_filename,
324 const gchar *contact_id,
325 const gchar *service_name,
326 const gchar *message_classes,
330 const gchar *cur = NULL;
333 /* Make some search-and-replace in the html code */
334 string = g_string_sized_new (len + strlen (message));
335 g_string_append_printf (string, "%s(\"", func);
336 for (cur = html; *cur != '\0'; cur++) {
337 const gchar *replace = NULL;
338 gchar *dup_replace = NULL;
340 if (theme_adium_match (&cur, "%message%")) {
342 } else if (theme_adium_match (&cur, "%messageClasses%")) {
343 replace = message_classes;
344 } else if (theme_adium_match (&cur, "%userIconPath%")) {
345 replace = avatar_filename;
346 } else if (theme_adium_match (&cur, "%sender%")) {
348 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
349 replace = contact_id;
350 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
351 /* %senderDisplayName% -
352 * "The serverside (remotely set) name of the sender,
353 * such as an MSN display name."
355 * We don't have access to that yet so we use local
358 } else if (theme_adium_match (&cur, "%service%")) {
359 replace = service_name;
360 } else if (theme_adium_match (&cur, "%shortTime%")) {
361 dup_replace = empathy_time_to_string_local (timestamp,
362 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
363 replace = dup_replace;
364 } else if (theme_adium_match (&cur, "%time")) {
365 gchar *format = NULL;
367 /* Time can be in 2 formats:
368 * %time% or %time{strftime format}%
369 * Extract the time format if provided. */
372 end = strstr (cur, "}%");
377 format = g_strndup (cur, end - cur);
383 dup_replace = empathy_time_to_string_local (timestamp,
384 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
385 replace = dup_replace;
388 escape_and_append_len (string, cur, 1);
392 /* Here we have a replacement to make */
393 escape_and_append_len (string, replace, -1);
394 g_free (dup_replace);
396 g_string_append (string, "\")");
398 script = g_string_free (string, FALSE);
399 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
404 theme_adium_append_event_escaped (EmpathyChatView *view,
405 const gchar *escaped)
407 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
408 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
410 if (priv->data->status_html) {
411 theme_adium_append_html (theme, "appendMessage",
412 priv->data->status_html,
413 priv->data->status_len,
414 escaped, NULL, NULL, NULL, NULL,
415 "event", empathy_time_get_current ());
418 /* There is no last contact */
419 if (priv->last_contact) {
420 g_object_unref (priv->last_contact);
421 priv->last_contact = NULL;
426 theme_adium_append_message (EmpathyChatView *view,
429 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
430 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
431 EmpathyContact *sender;
436 const gchar *contact_id;
437 EmpathyAvatar *avatar;
438 const gchar *avatar_filename = NULL;
443 const gchar *service_name;
444 GString *message_classes = NULL;
446 gboolean consecutive;
448 if (!priv->page_loaded) {
449 priv->message_queue = g_list_prepend (priv->message_queue,
454 /* Get information */
455 sender = empathy_message_get_sender (msg);
456 account = empathy_contact_get_account (sender);
457 service_name = empathy_protocol_name_to_display_name
458 (tp_account_get_protocol (account));
459 if (service_name == NULL)
460 service_name = tp_account_get_protocol (account);
461 timestamp = empathy_message_get_timestamp (msg);
462 body = empathy_message_get_body (msg);
463 body_escaped = theme_adium_parse_body (body);
464 name = empathy_contact_get_name (sender);
465 contact_id = empathy_contact_get_id (sender);
467 /* If this is a /me, append an event */
468 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
471 str = g_strdup_printf ("%s %s", name, body_escaped);
472 theme_adium_append_event_escaped (view, str);
475 g_free (body_escaped);
479 /* Get the avatar filename, or a fallback */
480 avatar = empathy_contact_get_avatar (sender);
482 avatar_filename = avatar->filename;
484 if (!avatar_filename) {
485 if (empathy_contact_is_user (sender)) {
486 avatar_filename = priv->data->default_outgoing_avatar_filename;
488 avatar_filename = priv->data->default_incoming_avatar_filename;
490 if (!avatar_filename) {
491 if (!priv->data->default_avatar_filename) {
492 priv->data->default_avatar_filename =
493 empathy_filename_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
494 GTK_ICON_SIZE_DIALOG);
496 avatar_filename = priv->data->default_avatar_filename;
500 /* We want to join this message with the last one if
501 * - senders are the same contact,
502 * - last message was recieved recently,
503 * - last message and this message both are/aren't backlog, and
504 * - DisableCombineConsecutive is not set in theme's settings */
505 is_backlog = empathy_message_is_backlog (msg);
506 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
507 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
508 (is_backlog == priv->last_is_backlog) &&
509 !tp_asv_get_boolean (priv->data->info,
510 "DisableCombineConsecutive", NULL);
512 /* Define message classes */
513 message_classes = g_string_new ("message");
515 g_string_append (message_classes, " history");
518 g_string_append (message_classes, " consecutive");
520 if (empathy_contact_is_user (sender)) {
521 g_string_append (message_classes, " outgoing");
523 g_string_append (message_classes, " incoming");
526 /* Define javascript function to use */
528 func = "appendNextMessage";
530 func = "appendMessage";
534 if (empathy_contact_is_user (sender)) {
537 html = priv->data->out_nextcontext_html;
538 len = priv->data->out_nextcontext_len;
541 /* Not backlog, or fallback if NextContext.html
544 html = priv->data->out_nextcontent_html;
545 len = priv->data->out_nextcontent_len;
549 /* Not consecutive, or fallback if NextContext.html and/or
550 * NextContent.html are missing */
553 html = priv->data->out_context_html;
554 len = priv->data->out_context_len;
558 html = priv->data->out_content_html;
559 len = priv->data->out_content_len;
564 /* Incoming, or fallback if outgoing files are missing */
568 html = priv->data->in_nextcontext_html;
569 len = priv->data->in_nextcontext_len;
572 /* Note backlog, or fallback if NextContext.html
575 html = priv->data->in_nextcontent_html;
576 len = priv->data->in_nextcontent_len;
580 /* Not consecutive, or fallback if NextContext.html and/or
581 * NextContent.html are missing */
584 html = priv->data->in_context_html;
585 len = priv->data->in_context_len;
589 html = priv->data->in_content_html;
590 len = priv->data->in_content_len;
596 theme_adium_append_html (theme, func, html, len, body_escaped,
597 avatar_filename, name, contact_id,
598 service_name, message_classes->str,
601 DEBUG ("Couldn't find HTML file for this message");
604 /* Keep the sender of the last displayed message */
605 if (priv->last_contact) {
606 g_object_unref (priv->last_contact);
608 priv->last_contact = g_object_ref (sender);
609 priv->last_timestamp = timestamp;
610 priv->last_is_backlog = is_backlog;
612 g_free (body_escaped);
613 g_string_free (message_classes, TRUE);
617 theme_adium_append_event (EmpathyChatView *view,
622 str_escaped = g_markup_escape_text (str, -1);
623 theme_adium_append_event_escaped (view, str_escaped);
624 g_free (str_escaped);
628 theme_adium_scroll (EmpathyChatView *view,
629 gboolean allow_scrolling)
631 /* FIXME: Is it possible? I guess we need a js function, but I don't
636 theme_adium_scroll_down (EmpathyChatView *view)
638 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
642 theme_adium_get_has_selection (EmpathyChatView *view)
644 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
648 theme_adium_clear (EmpathyChatView *view)
650 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
653 priv->page_loaded = FALSE;
654 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
655 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
656 priv->data->template_html,
658 g_free (basedir_uri);
660 /* Clear last contact to avoid trying to add a 'joined'
661 * message when we don't have an insertion point. */
662 if (priv->last_contact) {
663 g_object_unref (priv->last_contact);
664 priv->last_contact = NULL;
669 theme_adium_find_previous (EmpathyChatView *view,
670 const gchar *search_criteria,
674 /* FIXME: Doesn't respect new_search */
675 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
676 search_criteria, match_case,
681 theme_adium_find_next (EmpathyChatView *view,
682 const gchar *search_criteria,
686 /* FIXME: Doesn't respect new_search */
687 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
688 search_criteria, match_case,
693 theme_adium_find_abilities (EmpathyChatView *view,
694 const gchar *search_criteria,
696 gboolean *can_do_previous,
697 gboolean *can_do_next)
699 /* FIXME: Does webkit provide an API for that? We have wrap=true in
700 * find_next and find_previous to work around this problem. */
702 *can_do_previous = TRUE;
708 theme_adium_highlight (EmpathyChatView *view,
712 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
713 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
714 text, match_case, 0);
715 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
720 theme_adium_copy_clipboard (EmpathyChatView *view)
722 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
726 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
728 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
730 g_object_unref (hit_test_result);
734 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
736 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
737 WebKitHitTestResult *hit_test_result;
738 WebKitHitTestResultContext context;
742 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
743 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
746 menu = gtk_menu_new ();
748 /* Select all item */
749 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
750 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
752 g_signal_connect_swapped (item, "activate",
753 G_CALLBACK (webkit_web_view_select_all),
757 if (webkit_web_view_can_copy_clipboard (view)) {
758 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
759 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
761 g_signal_connect_swapped (item, "activate",
762 G_CALLBACK (webkit_web_view_copy_clipboard),
766 /* Clear menu item */
767 item = gtk_separator_menu_item_new ();
768 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
770 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
771 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
773 g_signal_connect_swapped (item, "activate",
774 G_CALLBACK (empathy_chat_view_clear),
777 /* We will only add the following menu items if we are
778 * right-clicking a link */
779 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
781 item = gtk_separator_menu_item_new ();
782 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
784 /* Copy Link Address menu item */
785 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
786 g_signal_connect (item, "activate",
787 G_CALLBACK (theme_adium_copy_address_cb),
789 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
791 /* Open Link menu item */
792 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
793 g_signal_connect (item, "activate",
794 G_CALLBACK (theme_adium_open_address_cb),
796 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
799 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
800 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
803 /* Display the menu */
804 gtk_widget_show_all (menu);
805 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
806 event->button, event->time);
807 g_object_ref_sink (menu);
808 g_object_unref (menu);
812 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
814 if (event->button == 3) {
815 gboolean developer_tools_enabled;
817 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
818 "enable-developer-extras", &developer_tools_enabled, NULL);
820 /* We currently have no way to add an inspector menu
821 * item ourselves, so we disable our customized menu
822 * if the developer extras are enabled. */
823 if (!developer_tools_enabled) {
824 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
829 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
833 theme_adium_iface_init (EmpathyChatViewIface *iface)
835 iface->append_message = theme_adium_append_message;
836 iface->append_event = theme_adium_append_event;
837 iface->scroll = theme_adium_scroll;
838 iface->scroll_down = theme_adium_scroll_down;
839 iface->get_has_selection = theme_adium_get_has_selection;
840 iface->clear = theme_adium_clear;
841 iface->find_previous = theme_adium_find_previous;
842 iface->find_next = theme_adium_find_next;
843 iface->find_abilities = theme_adium_find_abilities;
844 iface->highlight = theme_adium_highlight;
845 iface->copy_clipboard = theme_adium_copy_clipboard;
849 theme_adium_load_finished_cb (WebKitWebView *view,
850 WebKitWebFrame *frame,
853 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
854 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
856 DEBUG ("Page loaded");
857 priv->page_loaded = TRUE;
859 /* Display queued messages */
860 priv->message_queue = g_list_reverse (priv->message_queue);
861 while (priv->message_queue) {
862 EmpathyMessage *message = priv->message_queue->data;
864 theme_adium_append_message (chat_view, message);
865 priv->message_queue = g_list_remove (priv->message_queue, message);
866 g_object_unref (message);
871 theme_adium_finalize (GObject *object)
873 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
875 empathy_adium_data_unref (priv->data);
876 g_object_unref (priv->gsettings_chat);
878 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
882 theme_adium_dispose (GObject *object)
884 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
886 if (priv->smiley_manager) {
887 g_object_unref (priv->smiley_manager);
888 priv->smiley_manager = NULL;
891 if (priv->last_contact) {
892 g_object_unref (priv->last_contact);
893 priv->last_contact = NULL;
896 if (priv->inspector_window) {
897 gtk_widget_destroy (priv->inspector_window);
898 priv->inspector_window = NULL;
901 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
905 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
906 EmpathyThemeAdium *theme)
908 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
910 if (priv->inspector_window) {
911 gtk_widget_show_all (priv->inspector_window);
918 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
919 EmpathyThemeAdium *theme)
921 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
923 if (priv->inspector_window) {
924 gtk_widget_hide (priv->inspector_window);
930 static WebKitWebView *
931 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
932 WebKitWebView *web_view,
933 EmpathyThemeAdium *theme)
935 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
936 GtkWidget *scrolled_window;
937 GtkWidget *inspector_web_view;
939 if (!priv->inspector_window) {
940 /* Create main window */
941 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
942 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
944 g_signal_connect (priv->inspector_window, "delete-event",
945 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
947 /* Pack a scrolled window */
948 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
949 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
950 GTK_POLICY_AUTOMATIC,
951 GTK_POLICY_AUTOMATIC);
952 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
954 gtk_widget_show (scrolled_window);
956 /* Pack a webview in the scrolled window. That webview will be
957 * used to render the inspector tool. */
958 inspector_web_view = webkit_web_view_new ();
959 gtk_container_add (GTK_CONTAINER (scrolled_window),
961 gtk_widget_show (scrolled_window);
963 return WEBKIT_WEB_VIEW (inspector_web_view);
969 static PangoFontDescription *
970 theme_adium_get_default_font (void)
972 GConfClient *gconf_client;
973 PangoFontDescription *pango_fd;
974 gchar *gconf_font_family;
976 gconf_client = gconf_client_get_default ();
977 if (gconf_client == NULL) {
980 gconf_font_family = gconf_client_get_string (gconf_client,
981 EMPATHY_GCONF_FONT_KEY_NAME,
983 if (gconf_font_family == NULL) {
984 g_object_unref (gconf_client);
987 pango_fd = pango_font_description_from_string (gconf_font_family);
988 g_free (gconf_font_family);
989 g_object_unref (gconf_client);
994 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
998 g_object_set (w_settings, "default-font-family", name, NULL);
999 g_object_set (w_settings, "default-font-size", size, NULL);
1003 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1005 PangoFontDescription *default_font_desc;
1006 GdkScreen *current_screen;
1008 gint pango_font_size = 0;
1010 default_font_desc = theme_adium_get_default_font ();
1011 if (default_font_desc == NULL)
1013 pango_font_size = pango_font_description_get_size (default_font_desc)
1015 if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1016 current_screen = gdk_screen_get_default ();
1017 if (current_screen != NULL) {
1018 dpi = gdk_screen_get_resolution (current_screen);
1020 dpi = BORING_DPI_DEFAULT;
1022 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1024 theme_adium_set_webkit_font (w_settings,
1025 pango_font_description_get_family (default_font_desc),
1027 pango_font_description_free (default_font_desc);
1031 theme_adium_constructed (GObject *object)
1033 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1035 const gchar *font_family = NULL;
1037 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
1038 WebKitWebSettings *webkit_settings;
1039 WebKitWebInspector *webkit_inspector;
1041 /* Set default settings */
1042 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1043 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1044 webkit_settings = webkit_web_view_get_settings (webkit_view);
1046 if (font_family && font_size) {
1047 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1049 theme_adium_set_default_font (webkit_settings);
1052 /* Setup webkit inspector */
1053 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1054 g_signal_connect (webkit_inspector, "inspect-web-view",
1055 G_CALLBACK (theme_adium_inspect_web_view_cb),
1057 g_signal_connect (webkit_inspector, "show-window",
1058 G_CALLBACK (theme_adium_inspector_show_window_cb),
1060 g_signal_connect (webkit_inspector, "close-window",
1061 G_CALLBACK (theme_adium_inspector_close_window_cb),
1065 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1066 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1067 priv->data->template_html,
1069 g_free (basedir_uri);
1073 theme_adium_get_property (GObject *object,
1078 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1081 case PROP_ADIUM_DATA:
1082 g_value_set_boxed (value, priv->data);
1085 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1091 theme_adium_set_property (GObject *object,
1093 const GValue *value,
1096 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1099 case PROP_ADIUM_DATA:
1100 g_assert (priv->data == NULL);
1101 priv->data = g_value_dup_boxed (value);
1104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1110 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1112 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1113 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1115 object_class->finalize = theme_adium_finalize;
1116 object_class->dispose = theme_adium_dispose;
1117 object_class->constructed = theme_adium_constructed;
1118 object_class->get_property = theme_adium_get_property;
1119 object_class->set_property = theme_adium_set_property;
1121 widget_class->button_press_event = theme_adium_button_press_event;
1123 g_object_class_install_property (object_class,
1125 g_param_spec_boxed ("adium-data",
1127 "Data for the adium theme",
1128 EMPATHY_TYPE_ADIUM_DATA,
1129 G_PARAM_CONSTRUCT_ONLY |
1131 G_PARAM_STATIC_STRINGS));
1133 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1137 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1139 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1140 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1144 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1146 g_signal_connect (theme, "load-finished",
1147 G_CALLBACK (theme_adium_load_finished_cb),
1149 g_signal_connect (theme, "navigation-policy-decision-requested",
1150 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1153 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1154 g_signal_connect (priv->gsettings_chat,
1155 "changed::" EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1156 G_CALLBACK (theme_adium_notify_enable_webkit_developer_tools_cb),
1159 theme_adium_update_enable_webkit_developer_tools (theme);
1163 empathy_theme_adium_new (EmpathyAdiumData *data)
1165 g_return_val_if_fail (data != NULL, NULL);
1167 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1173 empathy_adium_path_is_valid (const gchar *path)
1178 /* The theme is not valid if there is no Info.plist */
1179 file = g_build_filename (path, "Contents", "Info.plist",
1181 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1187 /* We ship a default Template.html as fallback if there is any problem
1188 * with the one inside the theme. The only other required file is
1189 * Content.html for incoming messages (outgoing fallback to use
1191 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1192 "Content.html", NULL);
1193 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1200 empathy_adium_info_new (const gchar *path)
1204 GHashTable *info = NULL;
1206 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1208 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1209 value = empathy_plist_parse_from_file (file);
1215 info = g_value_dup_boxed (value);
1216 tp_g_value_slice_free (value);
1218 /* Insert the theme's path into the hash table,
1219 * keys have to be dupped */
1220 tp_asv_set_string (info, g_strdup ("path"), path);
1226 empathy_adium_data_get_type (void)
1228 static GType type_id = 0;
1232 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1233 (GBoxedCopyFunc) empathy_adium_data_ref,
1234 (GBoxedFreeFunc) empathy_adium_data_unref);
1241 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1243 EmpathyAdiumData *data;
1245 gchar *template_html = NULL;
1247 gchar *footer_html = NULL;
1250 gchar **strv = NULL;
1255 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1257 data = g_slice_new0 (EmpathyAdiumData);
1258 data->ref_count = 1;
1259 data->path = g_strdup (path);
1260 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1261 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1262 data->info = g_hash_table_ref (info);
1264 /* Load html files */
1265 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1266 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1269 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1270 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1273 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1274 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1277 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1278 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1281 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1282 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1285 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1286 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1289 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1290 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1293 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1294 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1297 file = g_build_filename (data->basedir, "Status.html", NULL);
1298 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1301 file = g_build_filename (data->basedir, "Footer.html", NULL);
1302 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1305 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1306 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1307 data->default_incoming_avatar_filename = file;
1312 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1313 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1314 data->default_outgoing_avatar_filename = file;
1319 css_path = g_build_filename (data->basedir, "main.css", NULL);
1321 /* There is 2 formats for Template.html: The old one has 4 parameters,
1322 * the new one has 5 parameters. */
1323 file = g_build_filename (data->basedir, "Template.html", NULL);
1324 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1325 strv = g_strsplit (template_html, "%@", -1);
1326 len = g_strv_length (strv);
1330 if (len != 5 && len != 6) {
1331 /* Either the theme has no template or it don't have the good
1332 * number of parameters. Fallback to use our own template. */
1333 g_free (template_html);
1336 file = empathy_file_lookup ("Template.html", "data");
1337 g_file_get_contents (file, &template_html, &template_len, NULL);
1339 strv = g_strsplit (template_html, "%@", -1);
1340 len = g_strv_length (strv);
1343 /* Replace %@ with the needed information in the template html. */
1344 string = g_string_sized_new (template_len);
1345 g_string_append (string, strv[i++]);
1346 g_string_append (string, data->basedir);
1347 g_string_append (string, strv[i++]);
1349 const gchar *variant;
1351 /* We include main.css by default */
1352 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1353 g_string_append (string, strv[i++]);
1354 variant = tp_asv_get_string (data->info, "DefaultVariant");
1356 g_string_append (string, "Variants/");
1357 g_string_append (string, variant);
1358 g_string_append (string, ".css");
1361 /* FIXME: We should set main.css OR the variant css */
1362 g_string_append (string, css_path);
1364 g_string_append (string, strv[i++]);
1365 g_string_append (string, ""); /* We don't want header */
1366 g_string_append (string, strv[i++]);
1367 /* FIXME: We should replace adium %macros% in footer */
1369 g_string_append (string, footer_html);
1371 g_string_append (string, strv[i++]);
1372 data->template_html = g_string_free (string, FALSE);
1374 g_free (footer_html);
1375 g_free (template_html);
1383 empathy_adium_data_new (const gchar *path)
1385 EmpathyAdiumData *data;
1388 info = empathy_adium_info_new (path);
1389 data = empathy_adium_data_new_with_info (path, info);
1390 g_hash_table_unref (info);
1396 empathy_adium_data_ref (EmpathyAdiumData *data)
1398 g_return_val_if_fail (data != NULL, NULL);
1400 g_atomic_int_inc (&data->ref_count);
1406 empathy_adium_data_unref (EmpathyAdiumData *data)
1408 g_return_if_fail (data != NULL);
1410 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1411 g_free (data->path);
1412 g_free (data->basedir);
1413 g_free (data->template_html);
1414 g_free (data->in_content_html);
1415 g_free (data->in_nextcontent_html);
1416 g_free (data->in_context_html);
1417 g_free (data->in_nextcontext_html);
1418 g_free (data->out_content_html);
1419 g_free (data->out_nextcontent_html);
1420 g_free (data->out_context_html);
1421 g_free (data->out_nextcontext_html);
1422 g_free (data->default_avatar_filename);
1423 g_free (data->default_incoming_avatar_filename);
1424 g_free (data->default_outgoing_avatar_filename);
1425 g_free (data->status_html);
1426 g_hash_table_unref (data->info);
1427 g_slice_free (EmpathyAdiumData, data);
1432 empathy_adium_data_get_info (EmpathyAdiumData *data)
1434 g_return_val_if_fail (data != NULL, NULL);
1440 empathy_adium_data_get_path (EmpathyAdiumData *data)
1442 g_return_val_if_fail (data != NULL, NULL);