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,
191 gboolean is_link = FALSE;
192 gboolean developer_tools_enabled;
194 /* FIXME: WebKitGTK+'s context menu API clearly needs an
195 * overhaul. There is currently no way to know what is being
196 * clicked, to decide what features to provide. You either
197 * take what it gives you as a menu, or use hacks to figure
198 * out what to display. */
199 items = gtk_container_get_children (GTK_CONTAINER (menu));
200 item = GTK_WIDGET (g_list_nth_data (items, 0));
203 if (GTK_IS_IMAGE_MENU_ITEM (item)) {
204 icon = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (item));
205 gtk_image_get_stock (GTK_IMAGE (icon), &stock_id, NULL);
207 if (!strcmp (stock_id, GTK_STOCK_OPEN))
211 /* Remove default menu items */
212 g_object_get (G_OBJECT (webkit_web_view_get_settings (view)),
213 "enable-developer-extras", &developer_tools_enabled, NULL);
214 if (!developer_tools_enabled)
215 gtk_container_foreach (GTK_CONTAINER (menu),
216 (GtkCallback) gtk_widget_destroy, NULL);
218 /* Select all item */
219 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
220 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
221 gtk_widget_show (item);
223 g_signal_connect_swapped (item, "activate",
224 G_CALLBACK (webkit_web_view_select_all),
228 if (webkit_web_view_can_copy_clipboard (view)) {
229 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
230 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
231 gtk_widget_show (item);
233 g_signal_connect_swapped (item, "activate",
234 G_CALLBACK (webkit_web_view_copy_clipboard),
238 /* Clear menu item */
239 item = gtk_separator_menu_item_new ();
240 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
241 gtk_widget_show (item);
243 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
244 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
245 gtk_widget_show (item);
247 g_signal_connect_swapped (item, "activate",
248 G_CALLBACK (empathy_chat_view_clear),
251 /* We will only add the following menu items if we are
252 * right-clicking a link */
257 item = gtk_separator_menu_item_new ();
258 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
259 gtk_widget_show (item);
261 /* Copy Link Address menu item */
262 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
263 g_signal_connect (item, "activate",
264 G_CALLBACK (theme_adium_copy_address_cb),
266 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
267 gtk_widget_show (item);
269 /* Open Link menu item */
270 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
271 g_signal_connect (item, "activate",
272 G_CALLBACK (theme_adium_open_address_cb),
274 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
275 gtk_widget_show (item);
279 theme_adium_parse_body (EmpathyThemeAdium *theme,
282 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
283 gboolean use_smileys = FALSE;
288 GMatchInfo *match_info;
293 empathy_conf_get_bool (empathy_conf_get (),
294 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
298 /* Replace smileys by a <img/> tag */
299 string = g_string_sized_new (strlen (text));
300 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
301 for (l = smileys; l; l = l->next) {
302 EmpathySmiley *smiley;
306 g_string_append_printf (string,
307 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
308 smiley->str, smiley->path, smiley->str);
312 str = g_markup_escape_text (smiley->str, -1);
313 g_string_append (string, str);
316 empathy_smiley_free (smiley);
318 g_slist_free (smileys);
321 text = ret = g_string_free (string, FALSE);
324 /* Add <a href></a> arround links */
325 uri_regex = empathy_uri_regex_dup_singleton ();
326 match = g_regex_match (uri_regex, text, 0, &match_info);
331 string = g_string_sized_new (strlen (text));
333 g_match_info_fetch_pos (match_info, 0, &s, &e);
336 /* Append the text between last link (or the
337 * start of the message) and this link */
338 g_string_append_len (string, text + last, s - last);
341 /* Append the link inside <a href=""></a> tag */
342 g_string_append (string, "<a href=\"");
343 g_string_append_len (string, text + s, e - s);
344 g_string_append (string, "\">");
345 g_string_append_len (string, text + s, e - s);
346 g_string_append (string, "</a>");
349 } while (g_match_info_next (match_info, NULL));
351 if (e < strlen (text)) {
352 /* Append the text after the last link */
353 g_string_append_len (string, text + e, strlen (text) - e);
357 text = ret = g_string_free (string, FALSE);
359 g_match_info_free (match_info);
360 g_regex_unref (uri_regex);
362 /* Replace \n by <br/> */
365 for (i = 0; text[i] != '\0'; i++) {
366 if (text[i] == '\n') {
368 string = g_string_sized_new (strlen (text));
370 g_string_append_len (string, text + prev, i - prev);
371 g_string_append (string, "<br/>");
376 g_string_append (string, text + prev);
378 text = ret = g_string_free (string, FALSE);
385 escape_and_append_len (GString *string, const gchar *str, gint len)
387 while (*str != '\0' && len != 0) {
391 g_string_append (string, "\\\\");
395 g_string_append (string, "\\\"");
398 /* Remove end of lines */
401 g_string_append_c (string, *str);
410 theme_adium_match (const gchar **str, const gchar *match)
414 len = strlen (match);
415 if (strncmp (*str, match, len) == 0) {
424 theme_adium_append_html (EmpathyThemeAdium *theme,
426 const gchar *html, gsize len,
427 const gchar *message,
428 const gchar *avatar_filename,
430 const gchar *contact_id,
431 const gchar *service_name,
432 const gchar *message_classes,
436 const gchar *cur = NULL;
439 /* Make some search-and-replace in the html code */
440 string = g_string_sized_new (len + strlen (message));
441 g_string_append_printf (string, "%s(\"", func);
442 for (cur = html; *cur != '\0'; cur++) {
443 const gchar *replace = NULL;
444 gchar *dup_replace = NULL;
446 if (theme_adium_match (&cur, "%message%")) {
448 } else if (theme_adium_match (&cur, "%messageClasses%")) {
449 replace = message_classes;
450 } else if (theme_adium_match (&cur, "%userIconPath%")) {
451 replace = avatar_filename;
452 } else if (theme_adium_match (&cur, "%sender%")) {
454 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
455 replace = contact_id;
456 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
457 /* %senderDisplayName% -
458 * "The serverside (remotely set) name of the sender,
459 * such as an MSN display name."
461 * We don't have access to that yet so we use local
464 } else if (theme_adium_match (&cur, "%service%")) {
465 replace = service_name;
466 } else if (theme_adium_match (&cur, "%shortTime%")) {
467 dup_replace = empathy_time_to_string_local (timestamp,
468 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
469 replace = dup_replace;
470 } else if (theme_adium_match (&cur, "%time")) {
471 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 McProfile *account_profile;
519 gchar *dup_body = NULL;
522 const gchar *contact_id;
523 EmpathyAvatar *avatar;
524 const gchar *avatar_filename = NULL;
529 const gchar *service_name;
530 GString *message_classes = NULL;
533 if (!priv->page_loaded) {
534 priv->message_queue = g_list_prepend (priv->message_queue,
539 /* Get information */
540 sender = empathy_message_get_sender (msg);
541 account = empathy_contact_get_account (sender);
542 account_profile = empathy_account_get_profile (account);
543 service_name = mc_profile_get_display_name (account_profile);
544 timestamp = empathy_message_get_timestamp (msg);
545 body = empathy_message_get_body (msg);
546 dup_body = theme_adium_parse_body (theme, body);
550 name = empathy_contact_get_name (sender);
551 contact_id = empathy_contact_get_id (sender);
553 /* If this is a /me, append an event */
554 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
557 str = g_strdup_printf ("%s %s", name, body);
558 empathy_chat_view_append_event (view, str);
564 /* Get the avatar filename, or a fallback */
565 avatar = empathy_contact_get_avatar (sender);
567 avatar_filename = avatar->filename;
569 if (!avatar_filename) {
570 if (empathy_contact_is_user (sender)) {
571 avatar_filename = priv->data->default_outgoing_avatar_filename;
573 avatar_filename = priv->data->default_incoming_avatar_filename;
575 if (!avatar_filename) {
576 if (!priv->data->default_avatar_filename) {
577 priv->data->default_avatar_filename =
578 empathy_filename_from_icon_name ("stock_person",
579 GTK_ICON_SIZE_DIALOG);
581 avatar_filename = priv->data->default_avatar_filename;
585 is_backlog = empathy_message_is_backlog (msg);
587 /* Get the right html/func to add the message */
588 func = "appendMessage";
590 message_classes = g_string_new ("message");
592 /* eventually append the "history" class */
594 g_string_append (message_classes, " history");
597 /* check the sender of the message and append the appropriate class */
598 if (empathy_contact_is_user (sender)) {
599 g_string_append (message_classes, " outgoing");
602 g_string_append (message_classes, " incoming");
606 * To mimick Adium's behavior, we only want to join messages
607 * sent by the same contact within a 5 minute time frame.
609 if (empathy_contact_equal (priv->last_contact, sender) &&
610 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
611 (is_backlog == priv->last_is_backlog)) {
612 /* the messages can be appended */
613 func = "appendNextMessage";
614 g_string_append (message_classes, " consecutive");
616 /* check who is the sender of the message to use the correct html file */
617 if (empathy_contact_is_user (sender)) {
618 /* check if this is a backlog message and use NextContext.html */
620 html = priv->data->out_nextcontext_html;
621 len = priv->data->out_nextcontext_len;
625 * html is null if this is not a backlog message or
626 * if we have to fallback (NextContext.html missing).
627 * use NextContent.html
630 html = priv->data->out_nextcontent_html;
631 len = priv->data->out_nextcontent_len;
636 html = priv->data->in_nextcontext_html;
637 len = priv->data->in_nextcontext_len;
641 html = priv->data->in_nextcontent_html;
642 len = priv->data->in_nextcontent_len;
648 * we have html == NULL here if:
649 * 1. the message didn't have to be appended because
650 * the sender was different or the timestamp was too far
651 * 2. NextContent.html file does not exist, so we must
652 * not forget to fallback to the correct Content.html
655 if (empathy_contact_is_user (sender)) {
657 html = priv->data->out_context_html;
658 len = priv->data->out_context_len;
662 html = priv->data->out_content_html;
663 len = priv->data->out_content_len;
668 html = priv->data->in_context_html;
669 len = priv->data->in_context_len;
673 html = priv->data->in_content_html;
674 len = priv->data->in_content_len;
679 theme_adium_append_html (theme, func, html, len, body, avatar_filename,
680 name, contact_id, service_name, message_classes->str,
683 /* Keep the sender of the last displayed message */
684 if (priv->last_contact) {
685 g_object_unref (priv->last_contact);
687 priv->last_contact = g_object_ref (sender);
688 priv->last_timestamp = timestamp;
689 priv->last_is_backlog = is_backlog;
692 g_string_free (message_classes, TRUE);
696 theme_adium_append_event (EmpathyChatView *view,
699 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
700 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
702 if (priv->data->status_html) {
703 theme_adium_append_html (theme, "appendMessage",
704 priv->data->status_html,
705 priv->data->status_len,
706 str, NULL, NULL, NULL, NULL, "event",
707 empathy_time_get_current ());
710 /* There is no last contact */
711 if (priv->last_contact) {
712 g_object_unref (priv->last_contact);
713 priv->last_contact = NULL;
718 theme_adium_scroll (EmpathyChatView *view,
719 gboolean allow_scrolling)
721 /* FIXME: Is it possible? I guess we need a js function, but I don't
726 theme_adium_scroll_down (EmpathyChatView *view)
728 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
732 theme_adium_get_has_selection (EmpathyChatView *view)
734 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
738 theme_adium_clear (EmpathyChatView *view)
740 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
743 priv->page_loaded = FALSE;
744 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
745 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
746 priv->data->template_html,
748 g_free (basedir_uri);
750 /* Clear last contact to avoid trying to add a 'joined'
751 * message when we don't have an insertion point. */
752 if (priv->last_contact) {
753 g_object_unref (priv->last_contact);
754 priv->last_contact = NULL;
759 theme_adium_find_previous (EmpathyChatView *view,
760 const gchar *search_criteria,
763 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
764 search_criteria, FALSE,
769 theme_adium_find_next (EmpathyChatView *view,
770 const gchar *search_criteria,
773 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
774 search_criteria, FALSE,
779 theme_adium_find_abilities (EmpathyChatView *view,
780 const gchar *search_criteria,
781 gboolean *can_do_previous,
782 gboolean *can_do_next)
784 /* FIXME: Does webkit provide an API for that? We have wrap=true in
785 * find_next and find_previous to work around this problem. */
787 *can_do_previous = TRUE;
793 theme_adium_highlight (EmpathyChatView *view,
796 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
797 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
799 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
804 theme_adium_copy_clipboard (EmpathyChatView *view)
806 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
810 theme_adium_iface_init (EmpathyChatViewIface *iface)
812 iface->append_message = theme_adium_append_message;
813 iface->append_event = theme_adium_append_event;
814 iface->scroll = theme_adium_scroll;
815 iface->scroll_down = theme_adium_scroll_down;
816 iface->get_has_selection = theme_adium_get_has_selection;
817 iface->clear = theme_adium_clear;
818 iface->find_previous = theme_adium_find_previous;
819 iface->find_next = theme_adium_find_next;
820 iface->find_abilities = theme_adium_find_abilities;
821 iface->highlight = theme_adium_highlight;
822 iface->copy_clipboard = theme_adium_copy_clipboard;
826 theme_adium_load_finished_cb (WebKitWebView *view,
827 WebKitWebFrame *frame,
830 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
831 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
833 DEBUG ("Page loaded");
834 priv->page_loaded = TRUE;
836 /* Display queued messages */
837 priv->message_queue = g_list_reverse (priv->message_queue);
838 while (priv->message_queue) {
839 EmpathyMessage *message = priv->message_queue->data;
841 theme_adium_append_message (chat_view, message);
842 priv->message_queue = g_list_remove (priv->message_queue, message);
843 g_object_unref (message);
848 theme_adium_finalize (GObject *object)
850 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
852 empathy_adium_data_unref (priv->data);
853 g_free (priv->hovered_uri);
855 empathy_conf_notify_remove (empathy_conf_get (),
856 priv->notify_enable_webkit_developer_tools_id);
858 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
862 theme_adium_dispose (GObject *object)
864 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
866 if (priv->smiley_manager) {
867 g_object_unref (priv->smiley_manager);
868 priv->smiley_manager = NULL;
871 if (priv->last_contact) {
872 g_object_unref (priv->last_contact);
873 priv->last_contact = NULL;
876 if (priv->inspector_window) {
877 gtk_widget_destroy (priv->inspector_window);
878 priv->inspector_window = NULL;
881 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
885 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
888 EmpathyThemeAdium *theme = user_data;
889 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
891 gtk_widget_show_all (priv->inspector_window);
897 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
900 EmpathyThemeAdium *theme = user_data;
901 EmpathyThemeAdiumPriv *priv;
903 /* We may be called too late - when the theme has already been
908 priv = GET_PRIV (theme);
910 if (priv->inspector_window) {
911 gtk_widget_hide (priv->inspector_window);
917 static WebKitWebView *
918 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
919 WebKitWebView *web_view,
922 EmpathyThemeAdiumPriv *priv = GET_PRIV (EMPATHY_THEME_ADIUM (web_view));
923 GtkWidget *scrolled_window;
924 GtkWidget *inspector_web_view;
926 if (!priv->inspector_window) {
927 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
928 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
931 g_signal_connect (priv->inspector_window, "delete-event",
932 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
934 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
935 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
936 gtk_container_add (GTK_CONTAINER (priv->inspector_window), scrolled_window);
938 inspector_web_view = webkit_web_view_new ();
939 gtk_container_add (GTK_CONTAINER (scrolled_window), inspector_web_view);
941 return WEBKIT_WEB_VIEW (inspector_web_view);
948 theme_adium_constructed (GObject *object)
950 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
952 const gchar *font_family = NULL;
954 WebKitWebSettings *webkit_settings;
956 /* Set default settings */
957 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
958 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
959 webkit_settings = webkit_web_settings_new ();
961 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
964 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
967 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "inspect-web-view",
968 G_CALLBACK (theme_adium_inspect_web_view_cb), NULL);
970 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "show-window",
971 G_CALLBACK (theme_adium_inspector_show_window_cb), object);
973 g_signal_connect (webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (object)), "close-window",
974 G_CALLBACK (theme_adium_inspector_close_window_cb), NULL);
977 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
978 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
979 priv->data->template_html,
982 g_object_unref (webkit_settings);
983 g_free (basedir_uri);
987 theme_adium_get_property (GObject *object,
992 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
995 case PROP_ADIUM_DATA:
996 g_value_set_boxed (value, priv->data);
999 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1005 theme_adium_set_property (GObject *object,
1007 const GValue *value,
1010 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1013 case PROP_ADIUM_DATA:
1014 g_assert (priv->data == NULL);
1015 priv->data = g_value_dup_boxed (value);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1024 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1026 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1028 object_class->finalize = theme_adium_finalize;
1029 object_class->dispose = theme_adium_dispose;
1030 object_class->constructed = theme_adium_constructed;
1031 object_class->get_property = theme_adium_get_property;
1032 object_class->set_property = theme_adium_set_property;
1034 g_object_class_install_property (object_class,
1036 g_param_spec_boxed ("adium-data",
1038 "Data for the adium theme",
1039 EMPATHY_TYPE_ADIUM_DATA,
1040 G_PARAM_CONSTRUCT_ONLY |
1042 G_PARAM_STATIC_STRINGS));
1044 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1048 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1050 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1051 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1055 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1057 g_signal_connect (theme, "load-finished",
1058 G_CALLBACK (theme_adium_load_finished_cb),
1060 g_signal_connect (theme, "navigation-requested",
1061 G_CALLBACK (theme_adium_navigation_requested_cb),
1063 g_signal_connect (theme, "populate-popup",
1064 G_CALLBACK (theme_adium_populate_popup_cb),
1066 g_signal_connect (theme, "hovering-over-link",
1067 G_CALLBACK (theme_adium_hovering_over_link_cb),
1070 priv->notify_enable_webkit_developer_tools_id =
1071 empathy_conf_notify_add (empathy_conf_get (),
1072 "/apps/empathy/conversation/enable_webkit_developer_tools",
1073 theme_adium_notify_enable_webkit_developer_tools_cb,
1076 theme_adium_update_enable_webkit_developer_tools (theme);
1080 empathy_theme_adium_new (EmpathyAdiumData *data)
1082 g_return_val_if_fail (data != NULL, NULL);
1084 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1090 empathy_adium_path_is_valid (const gchar *path)
1095 /* The theme is not valid if there is no Info.plist */
1096 file = g_build_filename (path, "Contents", "Info.plist",
1098 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1104 /* We ship a default Template.html as fallback if there is any problem
1105 * with the one inside the theme. The only other required file is
1106 * Content.html for incoming messages (outgoing fallback to use
1108 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1109 "Content.html", NULL);
1110 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1117 empathy_adium_info_new (const gchar *path)
1121 GHashTable *info = NULL;
1123 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1125 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1126 value = empathy_plist_parse_from_file (file);
1132 info = g_value_dup_boxed (value);
1133 tp_g_value_slice_free (value);
1135 /* Insert the theme's path into the hash table,
1136 * keys have to be dupped */
1137 tp_asv_set_string (info, g_strdup ("path"), path);
1143 empathy_adium_data_get_type (void)
1145 static GType type_id = 0;
1149 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1150 (GBoxedCopyFunc) empathy_adium_data_ref,
1151 (GBoxedFreeFunc) empathy_adium_data_unref);
1158 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1160 EmpathyAdiumData *data;
1162 gchar *template_html = NULL;
1164 gchar *footer_html = NULL;
1167 gchar **strv = NULL;
1172 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1174 data = g_slice_new0 (EmpathyAdiumData);
1175 data->ref_count = 1;
1176 data->path = g_strdup (path);
1177 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1178 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1179 data->info = g_hash_table_ref (info);
1181 /* Load html files */
1182 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1183 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1186 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1187 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1190 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1191 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1194 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1195 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1198 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1199 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1202 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1203 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1206 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1207 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1210 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1211 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1214 file = g_build_filename (data->basedir, "Status.html", NULL);
1215 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1218 file = g_build_filename (data->basedir, "Footer.html", NULL);
1219 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1222 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1223 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1224 data->default_incoming_avatar_filename = file;
1229 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1230 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1231 data->default_outgoing_avatar_filename = file;
1236 css_path = g_build_filename (data->basedir, "main.css", NULL);
1238 /* There is 2 formats for Template.html: The old one has 4 parameters,
1239 * the new one has 5 parameters. */
1240 file = g_build_filename (data->basedir, "Template.html", NULL);
1241 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1242 strv = g_strsplit (template_html, "%@", -1);
1243 len = g_strv_length (strv);
1247 if (len != 5 && len != 6) {
1248 /* Either the theme has no template or it don't have the good
1249 * number of parameters. Fallback to use our own template. */
1250 g_free (template_html);
1253 file = empathy_file_lookup ("Template.html", "data");
1254 g_file_get_contents (file, &template_html, &template_len, NULL);
1256 strv = g_strsplit (template_html, "%@", -1);
1257 len = g_strv_length (strv);
1260 /* Replace %@ with the needed information in the template html. */
1261 string = g_string_sized_new (template_len);
1262 g_string_append (string, strv[i++]);
1263 g_string_append (string, data->basedir);
1264 g_string_append (string, strv[i++]);
1266 const gchar *variant;
1268 /* We include main.css by default */
1269 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1270 g_string_append (string, strv[i++]);
1271 variant = tp_asv_get_string (data->info, "DefaultVariant");
1273 g_string_append (string, "Variants/");
1274 g_string_append (string, variant);
1275 g_string_append (string, ".css");
1278 /* FIXME: We should set main.css OR the variant css */
1279 g_string_append (string, css_path);
1281 g_string_append (string, strv[i++]);
1282 g_string_append (string, ""); /* We don't want header */
1283 g_string_append (string, strv[i++]);
1284 /* FIXME: We should replace adium %macros% in footer */
1286 g_string_append (string, footer_html);
1288 g_string_append (string, strv[i++]);
1289 data->template_html = g_string_free (string, FALSE);
1291 g_free (footer_html);
1292 g_free (template_html);
1300 empathy_adium_data_new (const gchar *path)
1302 EmpathyAdiumData *data;
1305 info = empathy_adium_info_new (path);
1306 data = empathy_adium_data_new_with_info (path, info);
1307 g_hash_table_unref (info);
1313 empathy_adium_data_ref (EmpathyAdiumData *data)
1315 g_return_val_if_fail (data != NULL, NULL);
1317 g_atomic_int_inc (&data->ref_count);
1323 empathy_adium_data_unref (EmpathyAdiumData *data)
1325 g_return_if_fail (data != NULL);
1327 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1328 g_free (data->path);
1329 g_free (data->basedir);
1330 g_free (data->template_html);
1331 g_free (data->in_content_html);
1332 g_free (data->in_nextcontent_html);
1333 g_free (data->in_context_html);
1334 g_free (data->in_nextcontext_html);
1335 g_free (data->out_content_html);
1336 g_free (data->out_nextcontent_html);
1337 g_free (data->out_context_html);
1338 g_free (data->out_nextcontext_html);
1339 g_free (data->default_avatar_filename);
1340 g_free (data->default_incoming_avatar_filename);
1341 g_free (data->default_outgoing_avatar_filename);
1342 g_free (data->status_html);
1343 g_hash_table_unref (data->info);
1344 g_slice_free (EmpathyAdiumData, data);
1349 empathy_adium_data_get_info (EmpathyAdiumData *data)
1351 g_return_val_if_fail (data != NULL, NULL);
1357 empathy_adium_data_get_path (EmpathyAdiumData *data)
1359 g_return_val_if_fail (data != NULL, NULL);