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/webkitnetworkrequest.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-time.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libmissioncontrol/mc-profile.h>
36 #include "empathy-theme-adium.h"
37 #include "empathy-smiley-manager.h"
38 #include "empathy-conf.h"
39 #include "empathy-ui-utils.h"
40 #include "empathy-plist.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
43 #include <libempathy/empathy-debug.h>
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
47 /* "Join" consecutive messages with timestamps within five minutes */
48 #define MESSAGE_JOIN_PERIOD 5*60
51 EmpathyAdiumData *data;
52 EmpathySmileyManager *smiley_manager;
53 EmpathyContact *last_contact;
54 time_t last_timestamp;
55 gboolean last_is_backlog;
59 guint notify_enable_webkit_developer_tools_id;
60 GtkWidget *inspector_window;
61 } EmpathyThemeAdiumPriv;
63 struct _EmpathyAdiumData {
67 gchar *default_avatar_filename;
68 gchar *default_incoming_avatar_filename;
69 gchar *default_outgoing_avatar_filename;
71 gchar *in_content_html;
73 gchar *in_context_html;
75 gchar *in_nextcontent_html;
76 gsize in_nextcontent_len;
77 gchar *in_nextcontext_html;
78 gsize in_nextcontext_len;
79 gchar *out_content_html;
80 gsize out_content_len;
81 gchar *out_context_html;
82 gsize out_context_len;
83 gchar *out_nextcontent_html;
84 gsize out_nextcontent_len;
85 gchar *out_nextcontext_html;
86 gsize out_nextcontext_len;
92 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
99 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
100 WEBKIT_TYPE_WEB_VIEW,
101 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
102 theme_adium_iface_init));
105 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
107 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
108 gboolean enable_webkit_developer_tools;
110 if (empathy_conf_get_bool (empathy_conf_get (), "/apps/empathy/conversation/enable_webkit_developer_tools", &enable_webkit_developer_tools) == FALSE)
113 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
114 "enable-developer-extras",
115 enable_webkit_developer_tools,
120 theme_adium_notify_enable_webkit_developer_tools_cb (EmpathyConf *conf,
124 EmpathyThemeAdium *theme = user_data;
126 theme_adium_update_enable_webkit_developer_tools (theme);
129 static WebKitNavigationResponse
130 theme_adium_navigation_requested_cb (WebKitWebView *view,
131 WebKitWebFrame *frame,
132 WebKitNetworkRequest *request,
137 uri = webkit_network_request_get_uri (request);
138 empathy_url_show (GTK_WIDGET (view), uri);
140 return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
144 theme_adium_hovering_over_link_cb (EmpathyThemeAdium *theme,
149 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
151 if (tp_strdiff (uri, priv->hovered_uri)) {
152 g_free (priv->hovered_uri);
153 priv->hovered_uri = g_strdup (uri);
158 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
161 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (user_data);
162 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
163 GtkClipboard *clipboard;
165 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
166 gtk_clipboard_set_text (clipboard, priv->hovered_uri, -1);
168 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
169 gtk_clipboard_set_text (clipboard, priv->hovered_uri, -1);
173 theme_adium_open_address_cb (GtkMenuItem *menuitem,
176 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (user_data);
177 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
179 empathy_url_show (GTK_WIDGET (menuitem), priv->hovered_uri);
183 theme_adium_populate_popup_cb (WebKitWebView *view,
187 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
188 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
193 gboolean is_link = FALSE;
194 gboolean developer_tools_enabled;
196 /* FIXME: WebKitGTK+'s context menu API clearly needs an
197 * overhaul. There is currently no way to know what is being
198 * clicked, to decide what features to provide. You either
199 * take what it gives you as a menu, or use hacks to figure
200 * out what to display. */
201 items = gtk_container_get_children (GTK_CONTAINER (menu));
202 item = GTK_WIDGET (g_list_nth_data (items, 0));
205 if (GTK_IS_IMAGE_MENU_ITEM (item)) {
206 icon = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (item));
207 gtk_image_get_stock (GTK_IMAGE (icon), &stock_id, NULL);
209 if ((!strcmp (stock_id, GTK_STOCK_OPEN)) && priv->hovered_uri)
213 /* Remove default menu items */
214 g_object_get (G_OBJECT (webkit_web_view_get_settings (view)),
215 "enable-developer-extras", &developer_tools_enabled, NULL);
216 if (!developer_tools_enabled)
217 gtk_container_foreach (GTK_CONTAINER (menu),
218 (GtkCallback) gtk_widget_destroy, NULL);
220 /* Select all item */
221 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
222 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
223 gtk_widget_show (item);
225 g_signal_connect_swapped (item, "activate",
226 G_CALLBACK (webkit_web_view_select_all),
230 if (webkit_web_view_can_copy_clipboard (view)) {
231 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
232 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
233 gtk_widget_show (item);
235 g_signal_connect_swapped (item, "activate",
236 G_CALLBACK (webkit_web_view_copy_clipboard),
240 /* Clear menu item */
241 item = gtk_separator_menu_item_new ();
242 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
243 gtk_widget_show (item);
245 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
246 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
247 gtk_widget_show (item);
249 g_signal_connect_swapped (item, "activate",
250 G_CALLBACK (empathy_chat_view_clear),
253 /* We will only add the following menu items if we are
254 * right-clicking a link */
259 item = gtk_separator_menu_item_new ();
260 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
261 gtk_widget_show (item);
263 /* Copy Link Address menu item */
264 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
265 g_signal_connect (item, "activate",
266 G_CALLBACK (theme_adium_copy_address_cb),
268 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
269 gtk_widget_show (item);
271 /* Open Link menu item */
272 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
273 g_signal_connect (item, "activate",
274 G_CALLBACK (theme_adium_open_address_cb),
276 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
277 gtk_widget_show (item);
281 theme_adium_parse_body (EmpathyThemeAdium *theme,
284 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
285 gboolean use_smileys = FALSE;
290 GMatchInfo *match_info;
295 empathy_conf_get_bool (empathy_conf_get (),
296 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
300 /* Replace smileys by a <img/> tag */
301 string = g_string_sized_new (strlen (text));
302 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
303 for (l = smileys; l; l = l->next) {
304 EmpathySmiley *smiley;
308 g_string_append_printf (string,
309 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
310 smiley->str, smiley->path, smiley->str);
314 str = g_markup_escape_text (smiley->str, -1);
315 g_string_append (string, str);
318 empathy_smiley_free (smiley);
320 g_slist_free (smileys);
323 text = ret = g_string_free (string, FALSE);
326 /* Add <a href></a> arround links */
327 uri_regex = empathy_uri_regex_dup_singleton ();
328 match = g_regex_match (uri_regex, text, 0, &match_info);
333 string = g_string_sized_new (strlen (text));
335 g_match_info_fetch_pos (match_info, 0, &s, &e);
338 /* Append the text between last link (or the
339 * start of the message) and this link */
340 g_string_append_len (string, text + last, s - last);
343 /* Append the link inside <a href=""></a> tag */
344 g_string_append (string, "<a href=\"");
345 g_string_append_len (string, text + s, e - s);
346 g_string_append (string, "\">");
347 g_string_append_len (string, text + s, e - s);
348 g_string_append (string, "</a>");
351 } while (g_match_info_next (match_info, NULL));
353 if (e < strlen (text)) {
354 /* Append the text after the last link */
355 g_string_append_len (string, text + e, strlen (text) - e);
359 text = ret = g_string_free (string, FALSE);
361 g_match_info_free (match_info);
362 g_regex_unref (uri_regex);
364 /* Replace \n by <br/> */
367 for (i = 0; text[i] != '\0'; i++) {
368 if (text[i] == '\n') {
370 string = g_string_sized_new (strlen (text));
372 g_string_append_len (string, text + prev, i - prev);
373 g_string_append (string, "<br/>");
378 g_string_append (string, text + prev);
380 text = ret = g_string_free (string, FALSE);
387 escape_and_append_len (GString *string, const gchar *str, gint len)
389 while (*str != '\0' && len != 0) {
393 g_string_append (string, "\\\\");
397 g_string_append (string, "\\\"");
400 /* Remove end of lines */
403 g_string_append_c (string, *str);
412 theme_adium_match (const gchar **str, const gchar *match)
416 len = strlen (match);
417 if (strncmp (*str, match, len) == 0) {
426 theme_adium_append_html (EmpathyThemeAdium *theme,
428 const gchar *html, gsize len,
429 const gchar *message,
430 const gchar *avatar_filename,
432 const gchar *contact_id,
433 const gchar *service_name,
434 const gchar *message_classes,
438 const gchar *cur = NULL;
441 /* Make some search-and-replace in the html code */
442 string = g_string_sized_new (len + strlen (message));
443 g_string_append_printf (string, "%s(\"", func);
444 for (cur = html; *cur != '\0'; cur++) {
445 const gchar *replace = NULL;
446 gchar *dup_replace = NULL;
448 if (theme_adium_match (&cur, "%message%")) {
450 } else if (theme_adium_match (&cur, "%messageClasses%")) {
451 replace = message_classes;
452 } else if (theme_adium_match (&cur, "%userIconPath%")) {
453 replace = avatar_filename;
454 } else if (theme_adium_match (&cur, "%sender%")) {
456 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
457 replace = contact_id;
458 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
459 /* %senderDisplayName% -
460 * "The serverside (remotely set) name of the sender,
461 * such as an MSN display name."
463 * We don't have access to that yet so we use local
466 } else if (theme_adium_match (&cur, "%service%")) {
467 replace = service_name;
468 } else if (theme_adium_match (&cur, "%shortTime%")) {
469 dup_replace = empathy_time_to_string_local (timestamp,
470 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
471 replace = dup_replace;
472 } else if (theme_adium_match (&cur, "%time")) {
473 gchar *format = NULL;
476 /* Time can be in 2 formats:
477 * %time% or %time{strftime format}%
478 * Extract the time format if provided. */
481 end = strstr (cur, "}%");
486 format = g_strndup (cur, end - cur);
492 dup_replace = empathy_time_to_string_local (timestamp,
493 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
494 replace = dup_replace;
497 escape_and_append_len (string, cur, 1);
501 /* Here we have a replacement to make */
502 escape_and_append_len (string, replace, -1);
503 g_free (dup_replace);
505 g_string_append (string, "\")");
507 script = g_string_free (string, FALSE);
508 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
513 theme_adium_append_message (EmpathyChatView *view,
516 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
517 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
518 EmpathyContact *sender;
519 EmpathyAccount *account;
520 McProfile *account_profile;
521 gchar *dup_body = NULL;
524 const gchar *contact_id;
525 EmpathyAvatar *avatar;
526 const gchar *avatar_filename = NULL;
531 const gchar *service_name;
532 GString *message_classes = NULL;
535 if (!priv->page_loaded) {
536 priv->message_queue = g_list_prepend (priv->message_queue,
541 /* Get information */
542 sender = empathy_message_get_sender (msg);
543 account = empathy_contact_get_account (sender);
544 account_profile = empathy_account_get_profile (account);
545 service_name = mc_profile_get_display_name (account_profile);
546 timestamp = empathy_message_get_timestamp (msg);
547 body = empathy_message_get_body (msg);
548 dup_body = theme_adium_parse_body (theme, body);
552 name = empathy_contact_get_name (sender);
553 contact_id = empathy_contact_get_id (sender);
555 /* If this is a /me, append an event */
556 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
559 str = g_strdup_printf ("%s %s", name, body);
560 empathy_chat_view_append_event (view, str);
566 /* Get the avatar filename, or a fallback */
567 avatar = empathy_contact_get_avatar (sender);
569 avatar_filename = avatar->filename;
571 if (!avatar_filename) {
572 if (empathy_contact_is_user (sender)) {
573 avatar_filename = priv->data->default_outgoing_avatar_filename;
575 avatar_filename = priv->data->default_incoming_avatar_filename;
577 if (!avatar_filename) {
578 if (!priv->data->default_avatar_filename) {
579 priv->data->default_avatar_filename =
580 empathy_filename_from_icon_name ("stock_person",
581 GTK_ICON_SIZE_DIALOG);
583 avatar_filename = priv->data->default_avatar_filename;
587 is_backlog = empathy_message_is_backlog (msg);
589 /* Get the right html/func to add the message */
590 func = "appendMessage";
592 message_classes = g_string_new ("message");
594 /* eventually append the "history" class */
596 g_string_append (message_classes, " history");
599 /* check the sender of the message and append the appropriate class */
600 if (empathy_contact_is_user (sender)) {
601 g_string_append (message_classes, " outgoing");
604 g_string_append (message_classes, " incoming");
608 * To mimick Adium's behavior, we only want to join messages
609 * sent by the same contact within a 5 minute time frame.
611 if (empathy_contact_equal (priv->last_contact, sender) &&
612 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
613 (is_backlog == priv->last_is_backlog)) {
614 /* the messages can be appended */
615 func = "appendNextMessage";
616 g_string_append (message_classes, " consecutive");
618 /* check who is the sender of the message to use the correct html file */
619 if (empathy_contact_is_user (sender)) {
620 /* check if this is a backlog message and use NextContext.html */
622 html = priv->data->out_nextcontext_html;
623 len = priv->data->out_nextcontext_len;
627 * html is null if this is not a backlog message or
628 * if we have to fallback (NextContext.html missing).
629 * use NextContent.html
632 html = priv->data->out_nextcontent_html;
633 len = priv->data->out_nextcontent_len;
638 html = priv->data->in_nextcontext_html;
639 len = priv->data->in_nextcontext_len;
643 html = priv->data->in_nextcontent_html;
644 len = priv->data->in_nextcontent_len;
650 * we have html == NULL here if:
651 * 1. the message didn't have to be appended because
652 * the sender was different or the timestamp was too far
653 * 2. NextContent.html file does not exist, so we must
654 * not forget to fallback to the correct Content.html
657 if (empathy_contact_is_user (sender)) {
659 html = priv->data->out_context_html;
660 len = priv->data->out_context_len;
664 html = priv->data->out_content_html;
665 len = priv->data->out_content_len;
670 html = priv->data->in_context_html;
671 len = priv->data->in_context_len;
675 html = priv->data->in_content_html;
676 len = priv->data->in_content_len;
681 theme_adium_append_html (theme, func, html, len, body, avatar_filename,
682 name, contact_id, service_name, message_classes->str,
685 /* Keep the sender of the last displayed message */
686 if (priv->last_contact) {
687 g_object_unref (priv->last_contact);
689 priv->last_contact = g_object_ref (sender);
690 priv->last_timestamp = timestamp;
691 priv->last_is_backlog = is_backlog;
694 g_string_free (message_classes, TRUE);
698 theme_adium_append_event (EmpathyChatView *view,
701 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
702 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
704 if (priv->data->status_html) {
705 theme_adium_append_html (theme, "appendMessage",
706 priv->data->status_html,
707 priv->data->status_len,
708 str, NULL, NULL, NULL, NULL, "event",
709 empathy_time_get_current ());
712 /* There is no last contact */
713 if (priv->last_contact) {
714 g_object_unref (priv->last_contact);
715 priv->last_contact = NULL;
720 theme_adium_scroll (EmpathyChatView *view,
721 gboolean allow_scrolling)
723 /* FIXME: Is it possible? I guess we need a js function, but I don't
728 theme_adium_scroll_down (EmpathyChatView *view)
730 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
734 theme_adium_get_has_selection (EmpathyChatView *view)
736 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
740 theme_adium_clear (EmpathyChatView *view)
742 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
745 priv->page_loaded = FALSE;
746 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
747 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
748 priv->data->template_html,
750 g_free (basedir_uri);
752 /* Clear last contact to avoid trying to add a 'joined'
753 * message when we don't have an insertion point. */
754 if (priv->last_contact) {
755 g_object_unref (priv->last_contact);
756 priv->last_contact = NULL;
761 theme_adium_find_previous (EmpathyChatView *view,
762 const gchar *search_criteria,
765 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
766 search_criteria, FALSE,
771 theme_adium_find_next (EmpathyChatView *view,
772 const gchar *search_criteria,
775 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
776 search_criteria, FALSE,
781 theme_adium_find_abilities (EmpathyChatView *view,
782 const gchar *search_criteria,
783 gboolean *can_do_previous,
784 gboolean *can_do_next)
786 /* FIXME: Does webkit provide an API for that? We have wrap=true in
787 * find_next and find_previous to work around this problem. */
789 *can_do_previous = TRUE;
795 theme_adium_highlight (EmpathyChatView *view,
798 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
799 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
801 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
806 theme_adium_copy_clipboard (EmpathyChatView *view)
808 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
812 theme_adium_iface_init (EmpathyChatViewIface *iface)
814 iface->append_message = theme_adium_append_message;
815 iface->append_event = theme_adium_append_event;
816 iface->scroll = theme_adium_scroll;
817 iface->scroll_down = theme_adium_scroll_down;
818 iface->get_has_selection = theme_adium_get_has_selection;
819 iface->clear = theme_adium_clear;
820 iface->find_previous = theme_adium_find_previous;
821 iface->find_next = theme_adium_find_next;
822 iface->find_abilities = theme_adium_find_abilities;
823 iface->highlight = theme_adium_highlight;
824 iface->copy_clipboard = theme_adium_copy_clipboard;
828 theme_adium_load_finished_cb (WebKitWebView *view,
829 WebKitWebFrame *frame,
832 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
833 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
835 DEBUG ("Page loaded");
836 priv->page_loaded = TRUE;
838 /* Display queued messages */
839 priv->message_queue = g_list_reverse (priv->message_queue);
840 while (priv->message_queue) {
841 EmpathyMessage *message = priv->message_queue->data;
843 theme_adium_append_message (chat_view, message);
844 priv->message_queue = g_list_remove (priv->message_queue, message);
845 g_object_unref (message);
850 theme_adium_finalize (GObject *object)
852 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
854 empathy_adium_data_unref (priv->data);
855 g_free (priv->hovered_uri);
857 empathy_conf_notify_remove (empathy_conf_get (),
858 priv->notify_enable_webkit_developer_tools_id);
860 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
864 theme_adium_dispose (GObject *object)
866 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
868 if (priv->smiley_manager) {
869 g_object_unref (priv->smiley_manager);
870 priv->smiley_manager = NULL;
873 if (priv->last_contact) {
874 g_object_unref (priv->last_contact);
875 priv->last_contact = NULL;
878 if (priv->inspector_window) {
879 gtk_widget_destroy (priv->inspector_window);
880 priv->inspector_window = NULL;
883 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
887 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
890 EmpathyThemeAdium *theme = user_data;
891 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
893 gtk_widget_show_all (priv->inspector_window);
899 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
902 EmpathyThemeAdium *theme = user_data;
903 EmpathyThemeAdiumPriv *priv;
905 /* We may be called too late - when the theme has already been
910 priv = GET_PRIV (theme);
912 if (priv->inspector_window) {
913 gtk_widget_hide (priv->inspector_window);
919 static WebKitWebView *
920 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
921 WebKitWebView *web_view,
924 EmpathyThemeAdiumPriv *priv = GET_PRIV (EMPATHY_THEME_ADIUM (web_view));
925 GtkWidget *scrolled_window;
926 GtkWidget *inspector_web_view;
928 if (!priv->inspector_window) {
929 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
930 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
933 g_signal_connect (priv->inspector_window, "delete-event",
934 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
936 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
937 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
938 gtk_container_add (GTK_CONTAINER (priv->inspector_window), scrolled_window);
940 inspector_web_view = webkit_web_view_new ();
941 gtk_container_add (GTK_CONTAINER (scrolled_window), inspector_web_view);
943 return WEBKIT_WEB_VIEW (inspector_web_view);
950 theme_adium_constructed (GObject *object)
952 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
954 const gchar *font_family = NULL;
956 WebKitWebSettings *webkit_settings;
958 /* Set default settings */
959 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
960 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
961 webkit_settings = webkit_web_settings_new ();
963 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
966 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
969 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "inspect-web-view",
970 G_CALLBACK (theme_adium_inspect_web_view_cb), NULL);
972 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "show-window",
973 G_CALLBACK (theme_adium_inspector_show_window_cb), object);
975 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "close-window",
976 G_CALLBACK (theme_adium_inspector_close_window_cb), NULL);
979 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
980 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
981 priv->data->template_html,
984 g_object_unref (webkit_settings);
985 g_free (basedir_uri);
989 theme_adium_get_property (GObject *object,
994 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
997 case PROP_ADIUM_DATA:
998 g_value_set_boxed (value, priv->data);
1001 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1007 theme_adium_set_property (GObject *object,
1009 const GValue *value,
1012 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1015 case PROP_ADIUM_DATA:
1016 g_assert (priv->data == NULL);
1017 priv->data = g_value_dup_boxed (value);
1020 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1026 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1028 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1030 object_class->finalize = theme_adium_finalize;
1031 object_class->dispose = theme_adium_dispose;
1032 object_class->constructed = theme_adium_constructed;
1033 object_class->get_property = theme_adium_get_property;
1034 object_class->set_property = theme_adium_set_property;
1036 g_object_class_install_property (object_class,
1038 g_param_spec_boxed ("adium-data",
1040 "Data for the adium theme",
1041 EMPATHY_TYPE_ADIUM_DATA,
1042 G_PARAM_CONSTRUCT_ONLY |
1044 G_PARAM_STATIC_STRINGS));
1046 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1050 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1052 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1053 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1057 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1059 g_signal_connect (theme, "load-finished",
1060 G_CALLBACK (theme_adium_load_finished_cb),
1062 g_signal_connect (theme, "navigation-requested",
1063 G_CALLBACK (theme_adium_navigation_requested_cb),
1065 g_signal_connect (theme, "populate-popup",
1066 G_CALLBACK (theme_adium_populate_popup_cb),
1068 g_signal_connect (theme, "hovering-over-link",
1069 G_CALLBACK (theme_adium_hovering_over_link_cb),
1072 priv->notify_enable_webkit_developer_tools_id =
1073 empathy_conf_notify_add (empathy_conf_get (),
1074 "/apps/empathy/conversation/enable_webkit_developer_tools",
1075 theme_adium_notify_enable_webkit_developer_tools_cb,
1078 theme_adium_update_enable_webkit_developer_tools (theme);
1082 empathy_theme_adium_new (EmpathyAdiumData *data)
1084 g_return_val_if_fail (data != NULL, NULL);
1086 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1092 empathy_adium_path_is_valid (const gchar *path)
1097 /* The theme is not valid if there is no Info.plist */
1098 file = g_build_filename (path, "Contents", "Info.plist",
1100 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1106 /* We ship a default Template.html as fallback if there is any problem
1107 * with the one inside the theme. The only other required file is
1108 * Content.html for incoming messages (outgoing fallback to use
1110 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1111 "Content.html", NULL);
1112 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1119 empathy_adium_info_new (const gchar *path)
1123 GHashTable *info = NULL;
1125 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1127 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1128 value = empathy_plist_parse_from_file (file);
1134 info = g_value_dup_boxed (value);
1135 tp_g_value_slice_free (value);
1137 /* Insert the theme's path into the hash table,
1138 * keys have to be dupped */
1139 tp_asv_set_string (info, g_strdup ("path"), path);
1145 empathy_adium_data_get_type (void)
1147 static GType type_id = 0;
1151 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1152 (GBoxedCopyFunc) empathy_adium_data_ref,
1153 (GBoxedFreeFunc) empathy_adium_data_unref);
1160 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1162 EmpathyAdiumData *data;
1164 gchar *template_html = NULL;
1166 gchar *footer_html = NULL;
1169 gchar **strv = NULL;
1174 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1176 data = g_slice_new0 (EmpathyAdiumData);
1177 data->ref_count = 1;
1178 data->path = g_strdup (path);
1179 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1180 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1181 data->info = g_hash_table_ref (info);
1183 /* Load html files */
1184 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1185 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1188 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1189 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1192 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1193 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1196 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1197 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1200 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1201 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1204 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1205 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1208 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1209 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1212 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1213 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1216 file = g_build_filename (data->basedir, "Status.html", NULL);
1217 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1220 file = g_build_filename (data->basedir, "Footer.html", NULL);
1221 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1224 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1225 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1226 data->default_incoming_avatar_filename = file;
1231 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1232 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1233 data->default_outgoing_avatar_filename = file;
1238 css_path = g_build_filename (data->basedir, "main.css", NULL);
1240 /* There is 2 formats for Template.html: The old one has 4 parameters,
1241 * the new one has 5 parameters. */
1242 file = g_build_filename (data->basedir, "Template.html", NULL);
1243 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1244 strv = g_strsplit (template_html, "%@", -1);
1245 len = g_strv_length (strv);
1249 if (len != 5 && len != 6) {
1250 /* Either the theme has no template or it don't have the good
1251 * number of parameters. Fallback to use our own template. */
1252 g_free (template_html);
1255 file = empathy_file_lookup ("Template.html", "data");
1256 g_file_get_contents (file, &template_html, &template_len, NULL);
1258 strv = g_strsplit (template_html, "%@", -1);
1259 len = g_strv_length (strv);
1262 /* Replace %@ with the needed information in the template html. */
1263 string = g_string_sized_new (template_len);
1264 g_string_append (string, strv[i++]);
1265 g_string_append (string, data->basedir);
1266 g_string_append (string, strv[i++]);
1268 const gchar *variant;
1270 /* We include main.css by default */
1271 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1272 g_string_append (string, strv[i++]);
1273 variant = tp_asv_get_string (data->info, "DefaultVariant");
1275 g_string_append (string, "Variants/");
1276 g_string_append (string, variant);
1277 g_string_append (string, ".css");
1280 /* FIXME: We should set main.css OR the variant css */
1281 g_string_append (string, css_path);
1283 g_string_append (string, strv[i++]);
1284 g_string_append (string, ""); /* We don't want header */
1285 g_string_append (string, strv[i++]);
1286 /* FIXME: We should replace adium %macros% in footer */
1288 g_string_append (string, footer_html);
1290 g_string_append (string, strv[i++]);
1291 data->template_html = g_string_free (string, FALSE);
1293 g_free (footer_html);
1294 g_free (template_html);
1302 empathy_adium_data_new (const gchar *path)
1304 EmpathyAdiumData *data;
1307 info = empathy_adium_info_new (path);
1308 data = empathy_adium_data_new_with_info (path, info);
1309 g_hash_table_unref (info);
1315 empathy_adium_data_ref (EmpathyAdiumData *data)
1317 g_return_val_if_fail (data != NULL, NULL);
1319 g_atomic_int_inc (&data->ref_count);
1325 empathy_adium_data_unref (EmpathyAdiumData *data)
1327 g_return_if_fail (data != NULL);
1329 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1330 g_free (data->path);
1331 g_free (data->basedir);
1332 g_free (data->template_html);
1333 g_free (data->in_content_html);
1334 g_free (data->in_nextcontent_html);
1335 g_free (data->in_context_html);
1336 g_free (data->in_nextcontext_html);
1337 g_free (data->out_content_html);
1338 g_free (data->out_nextcontent_html);
1339 g_free (data->out_context_html);
1340 g_free (data->out_nextcontext_html);
1341 g_free (data->default_avatar_filename);
1342 g_free (data->default_incoming_avatar_filename);
1343 g_free (data->default_outgoing_avatar_filename);
1344 g_free (data->status_html);
1345 g_hash_table_unref (data->info);
1346 g_slice_free (EmpathyAdiumData, data);
1351 empathy_adium_data_get_info (EmpathyAdiumData *data)
1353 g_return_val_if_fail (data != NULL, NULL);
1359 empathy_adium_data_get_path (EmpathyAdiumData *data)
1361 g_return_val_if_fail (data != NULL, NULL);