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>
35 #include "empathy-theme-adium.h"
36 #include "empathy-smiley-manager.h"
37 #include "empathy-conf.h"
38 #include "empathy-ui-utils.h"
39 #include "empathy-plist.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
42 #include <libempathy/empathy-debug.h>
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
46 /* "Join" consecutive messages with timestamps within five minutes */
47 #define MESSAGE_JOIN_PERIOD 5*60
50 EmpathyAdiumData *data;
51 EmpathySmileyManager *smiley_manager;
52 EmpathyContact *last_contact;
53 time_t last_timestamp;
54 gboolean last_is_backlog;
58 guint notify_enable_webkit_developer_tools_id;
59 GtkWidget *inspector_window;
60 } EmpathyThemeAdiumPriv;
62 struct _EmpathyAdiumData {
66 gchar *default_avatar_filename;
67 gchar *default_incoming_avatar_filename;
68 gchar *default_outgoing_avatar_filename;
70 gchar *in_content_html;
72 gchar *in_context_html;
74 gchar *in_nextcontent_html;
75 gsize in_nextcontent_len;
76 gchar *in_nextcontext_html;
77 gsize in_nextcontext_len;
78 gchar *out_content_html;
79 gsize out_content_len;
80 gchar *out_context_html;
81 gsize out_context_len;
82 gchar *out_nextcontent_html;
83 gsize out_nextcontent_len;
84 gchar *out_nextcontext_html;
85 gsize out_nextcontext_len;
91 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
98 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
100 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
101 theme_adium_iface_init));
104 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
106 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
107 gboolean enable_webkit_developer_tools;
109 if (empathy_conf_get_bool (empathy_conf_get (), "/apps/empathy/conversation/enable_webkit_developer_tools", &enable_webkit_developer_tools) == FALSE)
112 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
113 "enable-developer-extras",
114 enable_webkit_developer_tools,
119 theme_adium_notify_enable_webkit_developer_tools_cb (EmpathyConf *conf,
123 EmpathyThemeAdium *theme = user_data;
125 theme_adium_update_enable_webkit_developer_tools (theme);
128 static WebKitNavigationResponse
129 theme_adium_navigation_requested_cb (WebKitWebView *view,
130 WebKitWebFrame *frame,
131 WebKitNetworkRequest *request,
136 uri = webkit_network_request_get_uri (request);
137 empathy_url_show (GTK_WIDGET (view), uri);
139 return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
143 theme_adium_hovering_over_link_cb (EmpathyThemeAdium *theme,
148 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
150 if (tp_strdiff (uri, priv->hovered_uri)) {
151 g_free (priv->hovered_uri);
152 priv->hovered_uri = g_strdup (uri);
157 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
160 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (user_data);
161 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
162 GtkClipboard *clipboard;
164 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
165 gtk_clipboard_set_text (clipboard, priv->hovered_uri, -1);
167 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
168 gtk_clipboard_set_text (clipboard, priv->hovered_uri, -1);
172 theme_adium_open_address_cb (GtkMenuItem *menuitem,
175 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (user_data);
176 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
178 empathy_url_show (GTK_WIDGET (menuitem), priv->hovered_uri);
182 theme_adium_populate_popup_cb (WebKitWebView *view,
186 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
187 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
192 gboolean is_link = FALSE;
193 gboolean developer_tools_enabled;
195 /* FIXME: WebKitGTK+'s context menu API clearly needs an
196 * overhaul. There is currently no way to know what is being
197 * clicked, to decide what features to provide. You either
198 * take what it gives you as a menu, or use hacks to figure
199 * out what to display. */
200 items = gtk_container_get_children (GTK_CONTAINER (menu));
201 item = GTK_WIDGET (g_list_nth_data (items, 0));
204 if (GTK_IS_IMAGE_MENU_ITEM (item)) {
205 icon = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (item));
206 gtk_image_get_stock (GTK_IMAGE (icon), &stock_id, NULL);
208 if ((!strcmp (stock_id, GTK_STOCK_OPEN)) && priv->hovered_uri)
212 /* Remove default menu items */
213 g_object_get (G_OBJECT (webkit_web_view_get_settings (view)),
214 "enable-developer-extras", &developer_tools_enabled, NULL);
215 if (!developer_tools_enabled)
216 gtk_container_foreach (GTK_CONTAINER (menu),
217 (GtkCallback) gtk_widget_destroy, NULL);
219 /* Select all item */
220 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
221 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
222 gtk_widget_show (item);
224 g_signal_connect_swapped (item, "activate",
225 G_CALLBACK (webkit_web_view_select_all),
229 if (webkit_web_view_can_copy_clipboard (view)) {
230 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
231 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
232 gtk_widget_show (item);
234 g_signal_connect_swapped (item, "activate",
235 G_CALLBACK (webkit_web_view_copy_clipboard),
239 /* Clear menu item */
240 item = gtk_separator_menu_item_new ();
241 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
242 gtk_widget_show (item);
244 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
245 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
246 gtk_widget_show (item);
248 g_signal_connect_swapped (item, "activate",
249 G_CALLBACK (empathy_chat_view_clear),
252 /* We will only add the following menu items if we are
253 * right-clicking a link */
258 item = gtk_separator_menu_item_new ();
259 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
260 gtk_widget_show (item);
262 /* Copy Link Address menu item */
263 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
264 g_signal_connect (item, "activate",
265 G_CALLBACK (theme_adium_copy_address_cb),
267 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
268 gtk_widget_show (item);
270 /* Open Link menu item */
271 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
272 g_signal_connect (item, "activate",
273 G_CALLBACK (theme_adium_open_address_cb),
275 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
276 gtk_widget_show (item);
280 theme_adium_parse_body (EmpathyThemeAdium *theme,
283 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
284 gboolean use_smileys = FALSE;
289 GMatchInfo *match_info;
294 empathy_conf_get_bool (empathy_conf_get (),
295 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
299 /* Replace smileys by a <img/> tag */
300 string = g_string_sized_new (strlen (text));
301 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
302 for (l = smileys; l; l = l->next) {
303 EmpathySmiley *smiley;
307 g_string_append_printf (string,
308 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
309 smiley->str, smiley->path, smiley->str);
313 str = g_markup_escape_text (smiley->str, -1);
314 g_string_append (string, str);
317 empathy_smiley_free (smiley);
319 g_slist_free (smileys);
322 text = ret = g_string_free (string, FALSE);
325 /* Add <a href></a> arround links */
326 uri_regex = empathy_uri_regex_dup_singleton ();
327 match = g_regex_match (uri_regex, text, 0, &match_info);
332 string = g_string_sized_new (strlen (text));
334 g_match_info_fetch_pos (match_info, 0, &s, &e);
337 /* Append the text between last link (or the
338 * start of the message) and this link */
339 g_string_append_len (string, text + last, s - last);
342 /* Append the link inside <a href=""></a> tag */
343 g_string_append (string, "<a href=\"");
344 g_string_append_len (string, text + s, e - s);
345 g_string_append (string, "\">");
346 g_string_append_len (string, text + s, e - s);
347 g_string_append (string, "</a>");
350 } while (g_match_info_next (match_info, NULL));
352 if (e < strlen (text)) {
353 /* Append the text after the last link */
354 g_string_append_len (string, text + e, strlen (text) - e);
358 text = ret = g_string_free (string, FALSE);
360 g_match_info_free (match_info);
361 g_regex_unref (uri_regex);
363 /* Replace \n by <br/> */
366 for (i = 0; text[i] != '\0'; i++) {
367 if (text[i] == '\n') {
369 string = g_string_sized_new (strlen (text));
371 g_string_append_len (string, text + prev, i - prev);
372 g_string_append (string, "<br/>");
377 g_string_append (string, text + prev);
379 text = ret = g_string_free (string, FALSE);
386 escape_and_append_len (GString *string, const gchar *str, gint len)
388 while (*str != '\0' && len != 0) {
392 g_string_append (string, "\\\\");
396 g_string_append (string, "\\\"");
399 /* Remove end of lines */
402 g_string_append_c (string, *str);
411 theme_adium_match (const gchar **str, const gchar *match)
415 len = strlen (match);
416 if (strncmp (*str, match, len) == 0) {
425 theme_adium_append_html (EmpathyThemeAdium *theme,
427 const gchar *html, gsize len,
428 const gchar *message,
429 const gchar *avatar_filename,
431 const gchar *contact_id,
432 const gchar *service_name,
433 const gchar *message_classes,
437 const gchar *cur = NULL;
440 /* Make some search-and-replace in the html code */
441 string = g_string_sized_new (len + strlen (message));
442 g_string_append_printf (string, "%s(\"", func);
443 for (cur = html; *cur != '\0'; cur++) {
444 const gchar *replace = NULL;
445 gchar *dup_replace = NULL;
447 if (theme_adium_match (&cur, "%message%")) {
449 } else if (theme_adium_match (&cur, "%messageClasses%")) {
450 replace = message_classes;
451 } else if (theme_adium_match (&cur, "%userIconPath%")) {
452 replace = avatar_filename;
453 } else if (theme_adium_match (&cur, "%sender%")) {
455 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
456 replace = contact_id;
457 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
458 /* %senderDisplayName% -
459 * "The serverside (remotely set) name of the sender,
460 * such as an MSN display name."
462 * We don't have access to that yet so we use local
465 } else if (theme_adium_match (&cur, "%service%")) {
466 replace = service_name;
467 } else if (theme_adium_match (&cur, "%shortTime%")) {
468 dup_replace = empathy_time_to_string_local (timestamp,
469 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
470 replace = dup_replace;
471 } else if (theme_adium_match (&cur, "%time")) {
472 gchar *format = NULL;
474 /* Time can be in 2 formats:
475 * %time% or %time{strftime format}%
476 * Extract the time format if provided. */
479 end = strstr (cur, "}%");
484 format = g_strndup (cur, end - cur);
490 dup_replace = empathy_time_to_string_local (timestamp,
491 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
492 replace = dup_replace;
495 escape_and_append_len (string, cur, 1);
499 /* Here we have a replacement to make */
500 escape_and_append_len (string, replace, -1);
501 g_free (dup_replace);
503 g_string_append (string, "\")");
505 script = g_string_free (string, FALSE);
506 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
511 theme_adium_append_message (EmpathyChatView *view,
514 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
515 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
516 EmpathyContact *sender;
517 EmpathyAccount *account;
518 gchar *dup_body = NULL;
521 const gchar *contact_id;
522 EmpathyAvatar *avatar;
523 const gchar *avatar_filename = NULL;
528 const gchar *service_name;
529 GString *message_classes = NULL;
532 if (!priv->page_loaded) {
533 priv->message_queue = g_list_prepend (priv->message_queue,
538 /* Get information */
539 sender = empathy_message_get_sender (msg);
540 account = empathy_contact_get_account (sender);
541 service_name = empathy_account_get_protocol (account);
542 timestamp = empathy_message_get_timestamp (msg);
543 body = empathy_message_get_body (msg);
544 dup_body = theme_adium_parse_body (theme, body);
548 name = empathy_contact_get_name (sender);
549 contact_id = empathy_contact_get_id (sender);
551 /* If this is a /me, append an event */
552 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
555 str = g_strdup_printf ("%s %s", name, body);
556 empathy_chat_view_append_event (view, str);
562 /* Get the avatar filename, or a fallback */
563 avatar = empathy_contact_get_avatar (sender);
565 avatar_filename = avatar->filename;
567 if (!avatar_filename) {
568 if (empathy_contact_is_user (sender)) {
569 avatar_filename = priv->data->default_outgoing_avatar_filename;
571 avatar_filename = priv->data->default_incoming_avatar_filename;
573 if (!avatar_filename) {
574 if (!priv->data->default_avatar_filename) {
575 priv->data->default_avatar_filename =
576 empathy_filename_from_icon_name ("stock_person",
577 GTK_ICON_SIZE_DIALOG);
579 avatar_filename = priv->data->default_avatar_filename;
583 is_backlog = empathy_message_is_backlog (msg);
585 /* Get the right html/func to add the message */
586 func = "appendMessage";
588 message_classes = g_string_new ("message");
590 /* eventually append the "history" class */
592 g_string_append (message_classes, " history");
595 /* check the sender of the message and append the appropriate class */
596 if (empathy_contact_is_user (sender)) {
597 g_string_append (message_classes, " outgoing");
600 g_string_append (message_classes, " incoming");
604 * To mimick Adium's behavior, we only want to join messages
605 * sent by the same contact within a 5 minute time frame.
607 if (empathy_contact_equal (priv->last_contact, sender) &&
608 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
609 (is_backlog == priv->last_is_backlog)) {
610 /* the messages can be appended */
611 func = "appendNextMessage";
612 g_string_append (message_classes, " consecutive");
614 /* check who is the sender of the message to use the correct html file */
615 if (empathy_contact_is_user (sender)) {
616 /* check if this is a backlog message and use NextContext.html */
618 html = priv->data->out_nextcontext_html;
619 len = priv->data->out_nextcontext_len;
623 * html is null if this is not a backlog message or
624 * if we have to fallback (NextContext.html missing).
625 * use NextContent.html
628 html = priv->data->out_nextcontent_html;
629 len = priv->data->out_nextcontent_len;
634 html = priv->data->in_nextcontext_html;
635 len = priv->data->in_nextcontext_len;
639 html = priv->data->in_nextcontent_html;
640 len = priv->data->in_nextcontent_len;
646 * we have html == NULL here if:
647 * 1. the message didn't have to be appended because
648 * the sender was different or the timestamp was too far
649 * 2. NextContent.html file does not exist, so we must
650 * not forget to fallback to the correct Content.html
653 if (empathy_contact_is_user (sender)) {
655 html = priv->data->out_context_html;
656 len = priv->data->out_context_len;
660 html = priv->data->out_content_html;
661 len = priv->data->out_content_len;
666 html = priv->data->in_context_html;
667 len = priv->data->in_context_len;
671 html = priv->data->in_content_html;
672 len = priv->data->in_content_len;
677 theme_adium_append_html (theme, func, html, len, body, avatar_filename,
678 name, contact_id, service_name, message_classes->str,
681 /* Keep the sender of the last displayed message */
682 if (priv->last_contact) {
683 g_object_unref (priv->last_contact);
685 priv->last_contact = g_object_ref (sender);
686 priv->last_timestamp = timestamp;
687 priv->last_is_backlog = is_backlog;
690 g_string_free (message_classes, TRUE);
694 theme_adium_append_event (EmpathyChatView *view,
697 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
698 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
700 if (priv->data->status_html) {
701 theme_adium_append_html (theme, "appendMessage",
702 priv->data->status_html,
703 priv->data->status_len,
704 str, NULL, NULL, NULL, NULL, "event",
705 empathy_time_get_current ());
708 /* There is no last contact */
709 if (priv->last_contact) {
710 g_object_unref (priv->last_contact);
711 priv->last_contact = NULL;
716 theme_adium_scroll (EmpathyChatView *view,
717 gboolean allow_scrolling)
719 /* FIXME: Is it possible? I guess we need a js function, but I don't
724 theme_adium_scroll_down (EmpathyChatView *view)
726 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
730 theme_adium_get_has_selection (EmpathyChatView *view)
732 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
736 theme_adium_clear (EmpathyChatView *view)
738 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
741 priv->page_loaded = FALSE;
742 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
743 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
744 priv->data->template_html,
746 g_free (basedir_uri);
748 /* Clear last contact to avoid trying to add a 'joined'
749 * message when we don't have an insertion point. */
750 if (priv->last_contact) {
751 g_object_unref (priv->last_contact);
752 priv->last_contact = NULL;
757 theme_adium_find_previous (EmpathyChatView *view,
758 const gchar *search_criteria,
761 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
762 search_criteria, FALSE,
767 theme_adium_find_next (EmpathyChatView *view,
768 const gchar *search_criteria,
771 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
772 search_criteria, FALSE,
777 theme_adium_find_abilities (EmpathyChatView *view,
778 const gchar *search_criteria,
779 gboolean *can_do_previous,
780 gboolean *can_do_next)
782 /* FIXME: Does webkit provide an API for that? We have wrap=true in
783 * find_next and find_previous to work around this problem. */
785 *can_do_previous = TRUE;
791 theme_adium_highlight (EmpathyChatView *view,
794 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
795 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
797 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
802 theme_adium_copy_clipboard (EmpathyChatView *view)
804 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
808 theme_adium_iface_init (EmpathyChatViewIface *iface)
810 iface->append_message = theme_adium_append_message;
811 iface->append_event = theme_adium_append_event;
812 iface->scroll = theme_adium_scroll;
813 iface->scroll_down = theme_adium_scroll_down;
814 iface->get_has_selection = theme_adium_get_has_selection;
815 iface->clear = theme_adium_clear;
816 iface->find_previous = theme_adium_find_previous;
817 iface->find_next = theme_adium_find_next;
818 iface->find_abilities = theme_adium_find_abilities;
819 iface->highlight = theme_adium_highlight;
820 iface->copy_clipboard = theme_adium_copy_clipboard;
824 theme_adium_load_finished_cb (WebKitWebView *view,
825 WebKitWebFrame *frame,
828 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
829 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
831 DEBUG ("Page loaded");
832 priv->page_loaded = TRUE;
834 /* Display queued messages */
835 priv->message_queue = g_list_reverse (priv->message_queue);
836 while (priv->message_queue) {
837 EmpathyMessage *message = priv->message_queue->data;
839 theme_adium_append_message (chat_view, message);
840 priv->message_queue = g_list_remove (priv->message_queue, message);
841 g_object_unref (message);
846 theme_adium_finalize (GObject *object)
848 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
850 empathy_adium_data_unref (priv->data);
851 g_free (priv->hovered_uri);
853 empathy_conf_notify_remove (empathy_conf_get (),
854 priv->notify_enable_webkit_developer_tools_id);
856 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
860 theme_adium_dispose (GObject *object)
862 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
864 if (priv->smiley_manager) {
865 g_object_unref (priv->smiley_manager);
866 priv->smiley_manager = NULL;
869 if (priv->last_contact) {
870 g_object_unref (priv->last_contact);
871 priv->last_contact = NULL;
874 if (priv->inspector_window) {
875 gtk_widget_destroy (priv->inspector_window);
876 priv->inspector_window = NULL;
879 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
883 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
886 EmpathyThemeAdium *theme = user_data;
887 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
889 gtk_widget_show_all (priv->inspector_window);
895 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
898 EmpathyThemeAdium *theme = user_data;
899 EmpathyThemeAdiumPriv *priv;
901 /* We may be called too late - when the theme has already been
906 priv = GET_PRIV (theme);
908 if (priv->inspector_window) {
909 gtk_widget_hide (priv->inspector_window);
915 static WebKitWebView *
916 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
917 WebKitWebView *web_view,
920 EmpathyThemeAdiumPriv *priv = GET_PRIV (EMPATHY_THEME_ADIUM (web_view));
921 GtkWidget *scrolled_window;
922 GtkWidget *inspector_web_view;
924 if (!priv->inspector_window) {
925 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
926 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
929 g_signal_connect (priv->inspector_window, "delete-event",
930 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
932 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
933 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
934 gtk_container_add (GTK_CONTAINER (priv->inspector_window), scrolled_window);
936 inspector_web_view = webkit_web_view_new ();
937 gtk_container_add (GTK_CONTAINER (scrolled_window), inspector_web_view);
939 return WEBKIT_WEB_VIEW (inspector_web_view);
946 theme_adium_constructed (GObject *object)
948 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
950 const gchar *font_family = NULL;
952 WebKitWebSettings *webkit_settings;
954 /* Set default settings */
955 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
956 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
957 webkit_settings = webkit_web_settings_new ();
959 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
962 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
965 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "inspect-web-view",
966 G_CALLBACK (theme_adium_inspect_web_view_cb), NULL);
968 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "show-window",
969 G_CALLBACK (theme_adium_inspector_show_window_cb), object);
971 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "close-window",
972 G_CALLBACK (theme_adium_inspector_close_window_cb), NULL);
975 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
976 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
977 priv->data->template_html,
980 g_object_unref (webkit_settings);
981 g_free (basedir_uri);
985 theme_adium_get_property (GObject *object,
990 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
993 case PROP_ADIUM_DATA:
994 g_value_set_boxed (value, priv->data);
997 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1003 theme_adium_set_property (GObject *object,
1005 const GValue *value,
1008 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1011 case PROP_ADIUM_DATA:
1012 g_assert (priv->data == NULL);
1013 priv->data = g_value_dup_boxed (value);
1016 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1022 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1024 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1026 object_class->finalize = theme_adium_finalize;
1027 object_class->dispose = theme_adium_dispose;
1028 object_class->constructed = theme_adium_constructed;
1029 object_class->get_property = theme_adium_get_property;
1030 object_class->set_property = theme_adium_set_property;
1032 g_object_class_install_property (object_class,
1034 g_param_spec_boxed ("adium-data",
1036 "Data for the adium theme",
1037 EMPATHY_TYPE_ADIUM_DATA,
1038 G_PARAM_CONSTRUCT_ONLY |
1040 G_PARAM_STATIC_STRINGS));
1042 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1046 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1048 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1049 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1053 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1055 g_signal_connect (theme, "load-finished",
1056 G_CALLBACK (theme_adium_load_finished_cb),
1058 g_signal_connect (theme, "navigation-requested",
1059 G_CALLBACK (theme_adium_navigation_requested_cb),
1061 g_signal_connect (theme, "populate-popup",
1062 G_CALLBACK (theme_adium_populate_popup_cb),
1064 g_signal_connect (theme, "hovering-over-link",
1065 G_CALLBACK (theme_adium_hovering_over_link_cb),
1068 priv->notify_enable_webkit_developer_tools_id =
1069 empathy_conf_notify_add (empathy_conf_get (),
1070 "/apps/empathy/conversation/enable_webkit_developer_tools",
1071 theme_adium_notify_enable_webkit_developer_tools_cb,
1074 theme_adium_update_enable_webkit_developer_tools (theme);
1078 empathy_theme_adium_new (EmpathyAdiumData *data)
1080 g_return_val_if_fail (data != NULL, NULL);
1082 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1088 empathy_adium_path_is_valid (const gchar *path)
1093 /* The theme is not valid if there is no Info.plist */
1094 file = g_build_filename (path, "Contents", "Info.plist",
1096 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1102 /* We ship a default Template.html as fallback if there is any problem
1103 * with the one inside the theme. The only other required file is
1104 * Content.html for incoming messages (outgoing fallback to use
1106 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1107 "Content.html", NULL);
1108 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1115 empathy_adium_info_new (const gchar *path)
1119 GHashTable *info = NULL;
1121 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1123 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1124 value = empathy_plist_parse_from_file (file);
1130 info = g_value_dup_boxed (value);
1131 tp_g_value_slice_free (value);
1133 /* Insert the theme's path into the hash table,
1134 * keys have to be dupped */
1135 tp_asv_set_string (info, g_strdup ("path"), path);
1141 empathy_adium_data_get_type (void)
1143 static GType type_id = 0;
1147 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1148 (GBoxedCopyFunc) empathy_adium_data_ref,
1149 (GBoxedFreeFunc) empathy_adium_data_unref);
1156 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1158 EmpathyAdiumData *data;
1160 gchar *template_html = NULL;
1162 gchar *footer_html = NULL;
1165 gchar **strv = NULL;
1170 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1172 data = g_slice_new0 (EmpathyAdiumData);
1173 data->ref_count = 1;
1174 data->path = g_strdup (path);
1175 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1176 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1177 data->info = g_hash_table_ref (info);
1179 /* Load html files */
1180 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1181 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1184 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1185 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1188 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1189 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1192 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1193 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1196 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1197 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1200 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1201 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1204 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1205 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1208 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1209 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1212 file = g_build_filename (data->basedir, "Status.html", NULL);
1213 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1216 file = g_build_filename (data->basedir, "Footer.html", NULL);
1217 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1220 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1221 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1222 data->default_incoming_avatar_filename = file;
1227 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1228 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1229 data->default_outgoing_avatar_filename = file;
1234 css_path = g_build_filename (data->basedir, "main.css", NULL);
1236 /* There is 2 formats for Template.html: The old one has 4 parameters,
1237 * the new one has 5 parameters. */
1238 file = g_build_filename (data->basedir, "Template.html", NULL);
1239 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1240 strv = g_strsplit (template_html, "%@", -1);
1241 len = g_strv_length (strv);
1245 if (len != 5 && len != 6) {
1246 /* Either the theme has no template or it don't have the good
1247 * number of parameters. Fallback to use our own template. */
1248 g_free (template_html);
1251 file = empathy_file_lookup ("Template.html", "data");
1252 g_file_get_contents (file, &template_html, &template_len, NULL);
1254 strv = g_strsplit (template_html, "%@", -1);
1255 len = g_strv_length (strv);
1258 /* Replace %@ with the needed information in the template html. */
1259 string = g_string_sized_new (template_len);
1260 g_string_append (string, strv[i++]);
1261 g_string_append (string, data->basedir);
1262 g_string_append (string, strv[i++]);
1264 const gchar *variant;
1266 /* We include main.css by default */
1267 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1268 g_string_append (string, strv[i++]);
1269 variant = tp_asv_get_string (data->info, "DefaultVariant");
1271 g_string_append (string, "Variants/");
1272 g_string_append (string, variant);
1273 g_string_append (string, ".css");
1276 /* FIXME: We should set main.css OR the variant css */
1277 g_string_append (string, css_path);
1279 g_string_append (string, strv[i++]);
1280 g_string_append (string, ""); /* We don't want header */
1281 g_string_append (string, strv[i++]);
1282 /* FIXME: We should replace adium %macros% in footer */
1284 g_string_append (string, footer_html);
1286 g_string_append (string, strv[i++]);
1287 data->template_html = g_string_free (string, FALSE);
1289 g_free (footer_html);
1290 g_free (template_html);
1298 empathy_adium_data_new (const gchar *path)
1300 EmpathyAdiumData *data;
1303 info = empathy_adium_info_new (path);
1304 data = empathy_adium_data_new_with_info (path, info);
1305 g_hash_table_unref (info);
1311 empathy_adium_data_ref (EmpathyAdiumData *data)
1313 g_return_val_if_fail (data != NULL, NULL);
1315 g_atomic_int_inc (&data->ref_count);
1321 empathy_adium_data_unref (EmpathyAdiumData *data)
1323 g_return_if_fail (data != NULL);
1325 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1326 g_free (data->path);
1327 g_free (data->basedir);
1328 g_free (data->template_html);
1329 g_free (data->in_content_html);
1330 g_free (data->in_nextcontent_html);
1331 g_free (data->in_context_html);
1332 g_free (data->in_nextcontext_html);
1333 g_free (data->out_content_html);
1334 g_free (data->out_nextcontent_html);
1335 g_free (data->out_context_html);
1336 g_free (data->out_nextcontext_html);
1337 g_free (data->default_avatar_filename);
1338 g_free (data->default_incoming_avatar_filename);
1339 g_free (data->default_outgoing_avatar_filename);
1340 g_free (data->status_html);
1341 g_hash_table_unref (data->info);
1342 g_slice_free (EmpathyAdiumData, data);
1347 empathy_adium_data_get_info (EmpathyAdiumData *data)
1349 g_return_val_if_fail (data != NULL, NULL);
1355 empathy_adium_data_get_path (EmpathyAdiumData *data)
1357 g_return_val_if_fail (data != NULL, NULL);