1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n.h>
27 #include <webkit/webkit.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
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;
57 guint notify_enable_webkit_developer_tools_id;
58 GtkWidget *inspector_window;
59 } EmpathyThemeAdiumPriv;
61 struct _EmpathyAdiumData {
65 gchar *default_avatar_filename;
66 gchar *default_incoming_avatar_filename;
67 gchar *default_outgoing_avatar_filename;
69 gchar *in_content_html;
71 gchar *in_context_html;
73 gchar *in_nextcontent_html;
74 gsize in_nextcontent_len;
75 gchar *in_nextcontext_html;
76 gsize in_nextcontext_len;
77 gchar *out_content_html;
78 gsize out_content_len;
79 gchar *out_context_html;
80 gsize out_context_len;
81 gchar *out_nextcontent_html;
82 gsize out_nextcontent_len;
83 gchar *out_nextcontext_html;
84 gsize out_nextcontext_len;
90 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
97 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
99 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
100 theme_adium_iface_init));
103 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
105 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
106 gboolean enable_webkit_developer_tools;
108 if (!empathy_conf_get_bool (empathy_conf_get (),
109 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
110 &enable_webkit_developer_tools)) {
114 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
115 "enable-developer-extras",
116 enable_webkit_developer_tools,
121 theme_adium_notify_enable_webkit_developer_tools_cb (EmpathyConf *conf,
125 EmpathyThemeAdium *theme = user_data;
127 theme_adium_update_enable_webkit_developer_tools (theme);
131 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView *view,
132 WebKitWebFrame *web_frame,
133 WebKitNetworkRequest *request,
134 WebKitWebNavigationAction *action,
135 WebKitWebPolicyDecision *decision,
140 /* Only call url_show on clicks */
141 if (webkit_web_navigation_action_get_reason (action) !=
142 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
143 webkit_web_policy_decision_use (decision);
147 uri = webkit_network_request_get_uri (request);
148 empathy_url_show (GTK_WIDGET (view), uri);
150 webkit_web_policy_decision_ignore (decision);
155 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
158 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
160 GtkClipboard *clipboard;
162 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
164 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
165 gtk_clipboard_set_text (clipboard, uri, -1);
167 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
168 gtk_clipboard_set_text (clipboard, uri, -1);
174 theme_adium_open_address_cb (GtkMenuItem *menuitem,
177 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
180 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
182 empathy_url_show (GTK_WIDGET (menuitem), uri);
188 theme_adium_parse_body (EmpathyThemeAdium *theme,
191 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
192 gboolean use_smileys = FALSE;
197 GMatchInfo *match_info;
202 empathy_conf_get_bool (empathy_conf_get (),
203 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
206 /* Add <a href></a> arround links */
207 uri_regex = empathy_uri_regex_dup_singleton ();
208 match = g_regex_match (uri_regex, text, 0, &match_info);
213 string = g_string_sized_new (strlen (text));
217 g_match_info_fetch_pos (match_info, 0, &s, &e);
220 /* Append the text between last link (or the
221 * start of the message) and this link */
222 g_string_append_len (string, text + last, s - last);
225 /* Append the link inside <a href=""></a> tag */
226 real_url = empathy_make_absolute_url_len (text + s, e - s);
228 g_string_append (string, "<a href=\"");
229 g_string_append (string, real_url);
230 g_string_append (string, "\">");
231 g_string_append_len (string, text + s, e - s);
232 g_string_append (string, "</a>");
236 } while (g_match_info_next (match_info, NULL));
238 if (e < (gint) strlen (text)) {
239 /* Append the text after the last link */
240 g_string_append_len (string, text + e, strlen (text) - e);
244 text = ret = g_string_free (string, FALSE);
245 } else if (use_smileys) {
246 /* Replace smileys by a <img/> tag */
247 string = g_string_sized_new (strlen (text));
248 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
249 for (l = smileys; l; l = l->next) {
250 EmpathySmiley *smiley;
254 g_string_append_printf (string,
255 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
256 smiley->str, smiley->path, smiley->str);
260 str = g_markup_escape_text (smiley->str, -1);
261 g_string_append (string, str);
264 empathy_smiley_free (smiley);
266 g_slist_free (smileys);
269 text = ret = g_string_free (string, FALSE);
272 g_match_info_free (match_info);
273 g_regex_unref (uri_regex);
275 /* Replace \n by <br/> */
278 for (i = 0; text[i] != '\0'; i++) {
279 if (text[i] == '\n') {
281 string = g_string_sized_new (strlen (text));
283 g_string_append_len (string, text + prev, i - prev);
284 g_string_append (string, "<br/>");
289 g_string_append (string, text + prev);
291 text = ret = g_string_free (string, FALSE);
298 escape_and_append_len (GString *string, const gchar *str, gint len)
300 while (*str != '\0' && len != 0) {
304 g_string_append (string, "\\\\");
308 g_string_append (string, "\\\"");
311 /* Remove end of lines */
314 g_string_append_c (string, *str);
323 theme_adium_match (const gchar **str, const gchar *match)
327 len = strlen (match);
328 if (strncmp (*str, match, len) == 0) {
337 theme_adium_append_html (EmpathyThemeAdium *theme,
339 const gchar *html, gsize len,
340 const gchar *message,
341 const gchar *avatar_filename,
343 const gchar *contact_id,
344 const gchar *service_name,
345 const gchar *message_classes,
349 const gchar *cur = NULL;
352 /* Make some search-and-replace in the html code */
353 string = g_string_sized_new (len + strlen (message));
354 g_string_append_printf (string, "%s(\"", func);
355 for (cur = html; *cur != '\0'; cur++) {
356 const gchar *replace = NULL;
357 gchar *dup_replace = NULL;
359 if (theme_adium_match (&cur, "%message%")) {
361 } else if (theme_adium_match (&cur, "%messageClasses%")) {
362 replace = message_classes;
363 } else if (theme_adium_match (&cur, "%userIconPath%")) {
364 replace = avatar_filename;
365 } else if (theme_adium_match (&cur, "%sender%")) {
367 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
368 replace = contact_id;
369 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
370 /* %senderDisplayName% -
371 * "The serverside (remotely set) name of the sender,
372 * such as an MSN display name."
374 * We don't have access to that yet so we use local
377 } else if (theme_adium_match (&cur, "%service%")) {
378 replace = service_name;
379 } else if (theme_adium_match (&cur, "%shortTime%")) {
380 dup_replace = empathy_time_to_string_local (timestamp,
381 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
382 replace = dup_replace;
383 } else if (theme_adium_match (&cur, "%time")) {
384 gchar *format = NULL;
386 /* Time can be in 2 formats:
387 * %time% or %time{strftime format}%
388 * Extract the time format if provided. */
391 end = strstr (cur, "}%");
396 format = g_strndup (cur, end - cur);
402 dup_replace = empathy_time_to_string_local (timestamp,
403 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
404 replace = dup_replace;
407 escape_and_append_len (string, cur, 1);
411 /* Here we have a replacement to make */
412 escape_and_append_len (string, replace, -1);
413 g_free (dup_replace);
415 g_string_append (string, "\")");
417 script = g_string_free (string, FALSE);
418 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
423 theme_adium_append_message (EmpathyChatView *view,
426 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
427 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
428 EmpathyContact *sender;
429 EmpathyAccount *account;
430 gchar *dup_body = NULL;
433 const gchar *contact_id;
434 EmpathyAvatar *avatar;
435 const gchar *avatar_filename = NULL;
440 const gchar *service_name;
441 GString *message_classes = NULL;
443 gboolean consecutive;
445 if (!priv->page_loaded) {
446 priv->message_queue = g_list_prepend (priv->message_queue,
451 /* Get information */
452 sender = empathy_message_get_sender (msg);
453 account = empathy_contact_get_account (sender);
454 service_name = empathy_protocol_name_to_display_name
455 (empathy_account_get_protocol (account));
456 if (service_name == NULL)
457 service_name = empathy_account_get_protocol (account);
458 timestamp = empathy_message_get_timestamp (msg);
459 body = empathy_message_get_body (msg);
460 dup_body = theme_adium_parse_body (theme, body);
464 name = empathy_contact_get_name (sender);
465 contact_id = empathy_contact_get_id (sender);
467 /* If this is a /me, append an event */
468 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
471 str = g_strdup_printf ("%s %s", name, body);
472 empathy_chat_view_append_event (view, str);
478 /* Get the avatar filename, or a fallback */
479 avatar = empathy_contact_get_avatar (sender);
481 avatar_filename = avatar->filename;
483 if (!avatar_filename) {
484 if (empathy_contact_is_user (sender)) {
485 avatar_filename = priv->data->default_outgoing_avatar_filename;
487 avatar_filename = priv->data->default_incoming_avatar_filename;
489 if (!avatar_filename) {
490 if (!priv->data->default_avatar_filename) {
491 priv->data->default_avatar_filename =
492 empathy_filename_from_icon_name ("stock_person",
493 GTK_ICON_SIZE_DIALOG);
495 avatar_filename = priv->data->default_avatar_filename;
499 /* We want to join this message with the last one if
500 * - senders are the same contact,
501 * - last message was recieved recently,
502 * - last message and this message both are/aren't backlog, and
503 * - DisableCombineConsecutive is not set in theme's settings */
504 is_backlog = empathy_message_is_backlog (msg);
505 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
506 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
507 (is_backlog == priv->last_is_backlog) &&
508 !tp_asv_get_boolean (priv->data->info,
509 "DisableCombineConsecutive", NULL);
511 /* Define message classes */
512 message_classes = g_string_new ("message");
514 g_string_append (message_classes, " history");
517 g_string_append (message_classes, " consecutive");
519 if (empathy_contact_is_user (sender)) {
520 g_string_append (message_classes, " outgoing");
522 g_string_append (message_classes, " incoming");
525 /* Define javascript function to use */
527 func = "appendNextMessage";
529 func = "appendMessage";
533 if (empathy_contact_is_user (sender)) {
536 html = priv->data->out_nextcontext_html;
537 len = priv->data->out_nextcontext_len;
540 /* Not backlog, or fallback if NextContext.html
543 html = priv->data->out_nextcontent_html;
544 len = priv->data->out_nextcontent_len;
548 /* Not consecutive, or fallback if NextContext.html and/or
549 * NextContent.html are missing */
552 html = priv->data->out_context_html;
553 len = priv->data->out_context_len;
557 html = priv->data->out_content_html;
558 len = priv->data->out_content_len;
563 /* Incoming, or fallback if outgoing files are missing */
567 html = priv->data->in_nextcontext_html;
568 len = priv->data->in_nextcontext_len;
571 /* Note backlog, or fallback if NextContext.html
574 html = priv->data->in_nextcontent_html;
575 len = priv->data->in_nextcontent_len;
579 /* Not consecutive, or fallback if NextContext.html and/or
580 * NextContent.html are missing */
583 html = priv->data->in_context_html;
584 len = priv->data->in_context_len;
588 html = priv->data->in_content_html;
589 len = priv->data->in_content_len;
595 theme_adium_append_html (theme, func, html, len, body,
596 avatar_filename, name, contact_id,
597 service_name, message_classes->str,
600 DEBUG ("Couldn't find HTML file for this message");
603 /* Keep the sender of the last displayed message */
604 if (priv->last_contact) {
605 g_object_unref (priv->last_contact);
607 priv->last_contact = g_object_ref (sender);
608 priv->last_timestamp = timestamp;
609 priv->last_is_backlog = is_backlog;
612 g_string_free (message_classes, TRUE);
616 theme_adium_append_event (EmpathyChatView *view,
619 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
620 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
622 if (priv->data->status_html) {
623 theme_adium_append_html (theme, "appendMessage",
624 priv->data->status_html,
625 priv->data->status_len,
626 str, NULL, NULL, NULL, NULL, "event",
627 empathy_time_get_current ());
630 /* There is no last contact */
631 if (priv->last_contact) {
632 g_object_unref (priv->last_contact);
633 priv->last_contact = NULL;
638 theme_adium_scroll (EmpathyChatView *view,
639 gboolean allow_scrolling)
641 /* FIXME: Is it possible? I guess we need a js function, but I don't
646 theme_adium_scroll_down (EmpathyChatView *view)
648 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
652 theme_adium_get_has_selection (EmpathyChatView *view)
654 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
658 theme_adium_clear (EmpathyChatView *view)
660 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
663 priv->page_loaded = FALSE;
664 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
665 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
666 priv->data->template_html,
668 g_free (basedir_uri);
670 /* Clear last contact to avoid trying to add a 'joined'
671 * message when we don't have an insertion point. */
672 if (priv->last_contact) {
673 g_object_unref (priv->last_contact);
674 priv->last_contact = NULL;
679 theme_adium_find_previous (EmpathyChatView *view,
680 const gchar *search_criteria,
683 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
684 search_criteria, FALSE,
689 theme_adium_find_next (EmpathyChatView *view,
690 const gchar *search_criteria,
693 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
694 search_criteria, FALSE,
699 theme_adium_find_abilities (EmpathyChatView *view,
700 const gchar *search_criteria,
701 gboolean *can_do_previous,
702 gboolean *can_do_next)
704 /* FIXME: Does webkit provide an API for that? We have wrap=true in
705 * find_next and find_previous to work around this problem. */
707 *can_do_previous = TRUE;
713 theme_adium_highlight (EmpathyChatView *view,
716 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
717 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
719 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
724 theme_adium_copy_clipboard (EmpathyChatView *view)
726 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
730 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
732 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
734 g_object_unref (hit_test_result);
738 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
740 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
741 WebKitHitTestResult *hit_test_result;
742 WebKitHitTestResultContext context;
746 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
747 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
750 menu = gtk_menu_new ();
752 /* Select all item */
753 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
754 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
756 g_signal_connect_swapped (item, "activate",
757 G_CALLBACK (webkit_web_view_select_all),
761 if (webkit_web_view_can_copy_clipboard (view)) {
762 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
763 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
765 g_signal_connect_swapped (item, "activate",
766 G_CALLBACK (webkit_web_view_copy_clipboard),
770 /* Clear menu item */
771 item = gtk_separator_menu_item_new ();
772 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
774 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
775 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
777 g_signal_connect_swapped (item, "activate",
778 G_CALLBACK (empathy_chat_view_clear),
781 /* We will only add the following menu items if we are
782 * right-clicking a link */
783 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
785 item = gtk_separator_menu_item_new ();
786 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
788 /* Copy Link Address menu item */
789 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
790 g_signal_connect (item, "activate",
791 G_CALLBACK (theme_adium_copy_address_cb),
793 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
795 /* Open Link menu item */
796 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
797 g_signal_connect (item, "activate",
798 G_CALLBACK (theme_adium_open_address_cb),
800 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
803 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
804 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
807 /* Display the menu */
808 gtk_widget_show_all (menu);
809 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
810 event->button, event->time);
814 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
816 if (event->button == 3) {
817 gboolean developer_tools_enabled;
819 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
820 "enable-developer-extras", &developer_tools_enabled, NULL);
822 /* We currently have no way to add an inspector menu
823 * item ourselves, so we disable our customized menu
824 * if the developer extras are enabled. */
825 if (!developer_tools_enabled) {
826 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
831 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
835 theme_adium_iface_init (EmpathyChatViewIface *iface)
837 iface->append_message = theme_adium_append_message;
838 iface->append_event = theme_adium_append_event;
839 iface->scroll = theme_adium_scroll;
840 iface->scroll_down = theme_adium_scroll_down;
841 iface->get_has_selection = theme_adium_get_has_selection;
842 iface->clear = theme_adium_clear;
843 iface->find_previous = theme_adium_find_previous;
844 iface->find_next = theme_adium_find_next;
845 iface->find_abilities = theme_adium_find_abilities;
846 iface->highlight = theme_adium_highlight;
847 iface->copy_clipboard = theme_adium_copy_clipboard;
851 theme_adium_load_finished_cb (WebKitWebView *view,
852 WebKitWebFrame *frame,
855 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
856 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
858 DEBUG ("Page loaded");
859 priv->page_loaded = TRUE;
861 /* Display queued messages */
862 priv->message_queue = g_list_reverse (priv->message_queue);
863 while (priv->message_queue) {
864 EmpathyMessage *message = priv->message_queue->data;
866 theme_adium_append_message (chat_view, message);
867 priv->message_queue = g_list_remove (priv->message_queue, message);
868 g_object_unref (message);
873 theme_adium_finalize (GObject *object)
875 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
877 empathy_adium_data_unref (priv->data);
879 empathy_conf_notify_remove (empathy_conf_get (),
880 priv->notify_enable_webkit_developer_tools_id);
882 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
886 theme_adium_dispose (GObject *object)
888 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
890 if (priv->smiley_manager) {
891 g_object_unref (priv->smiley_manager);
892 priv->smiley_manager = NULL;
895 if (priv->last_contact) {
896 g_object_unref (priv->last_contact);
897 priv->last_contact = NULL;
900 if (priv->inspector_window) {
901 gtk_widget_destroy (priv->inspector_window);
902 priv->inspector_window = NULL;
905 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
909 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
910 EmpathyThemeAdium *theme)
912 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
914 if (priv->inspector_window) {
915 gtk_widget_show_all (priv->inspector_window);
922 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
923 EmpathyThemeAdium *theme)
925 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
927 if (priv->inspector_window) {
928 gtk_widget_hide (priv->inspector_window);
934 static WebKitWebView *
935 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
936 WebKitWebView *web_view,
937 EmpathyThemeAdium *theme)
939 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
940 GtkWidget *scrolled_window;
941 GtkWidget *inspector_web_view;
943 if (!priv->inspector_window) {
944 /* Create main window */
945 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
946 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
948 g_signal_connect (priv->inspector_window, "delete-event",
949 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
951 /* Pack a scrolled window */
952 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
953 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
954 GTK_POLICY_AUTOMATIC,
955 GTK_POLICY_AUTOMATIC);
956 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
958 gtk_widget_show (scrolled_window);
960 /* Pack a webview in the scrolled window. That webview will be
961 * used to render the inspector tool. */
962 inspector_web_view = webkit_web_view_new ();
963 gtk_container_add (GTK_CONTAINER (scrolled_window),
965 gtk_widget_show (scrolled_window);
967 return WEBKIT_WEB_VIEW (inspector_web_view);
974 theme_adium_constructed (GObject *object)
976 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
978 const gchar *font_family = NULL;
980 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
981 WebKitWebSettings *webkit_settings;
982 WebKitWebInspector *webkit_inspector;
984 /* Set default settings */
985 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
986 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
987 webkit_settings = webkit_web_view_get_settings (webkit_view);
989 g_object_set (webkit_settings,
990 "default-font-family", font_family,
994 g_object_set (webkit_settings,
995 "default-font-size", font_size,
999 /* Setup webkit inspector */
1000 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1001 g_signal_connect (webkit_inspector, "inspect-web-view",
1002 G_CALLBACK (theme_adium_inspect_web_view_cb),
1004 g_signal_connect (webkit_inspector, "show-window",
1005 G_CALLBACK (theme_adium_inspector_show_window_cb),
1007 g_signal_connect (webkit_inspector, "close-window",
1008 G_CALLBACK (theme_adium_inspector_close_window_cb),
1012 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1013 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1014 priv->data->template_html,
1016 g_free (basedir_uri);
1020 theme_adium_get_property (GObject *object,
1025 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1028 case PROP_ADIUM_DATA:
1029 g_value_set_boxed (value, priv->data);
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1038 theme_adium_set_property (GObject *object,
1040 const GValue *value,
1043 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1046 case PROP_ADIUM_DATA:
1047 g_assert (priv->data == NULL);
1048 priv->data = g_value_dup_boxed (value);
1051 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1057 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1059 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1060 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1062 object_class->finalize = theme_adium_finalize;
1063 object_class->dispose = theme_adium_dispose;
1064 object_class->constructed = theme_adium_constructed;
1065 object_class->get_property = theme_adium_get_property;
1066 object_class->set_property = theme_adium_set_property;
1068 widget_class->button_press_event = theme_adium_button_press_event;
1070 g_object_class_install_property (object_class,
1072 g_param_spec_boxed ("adium-data",
1074 "Data for the adium theme",
1075 EMPATHY_TYPE_ADIUM_DATA,
1076 G_PARAM_CONSTRUCT_ONLY |
1078 G_PARAM_STATIC_STRINGS));
1080 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1084 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1086 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1087 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1091 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1093 g_signal_connect (theme, "load-finished",
1094 G_CALLBACK (theme_adium_load_finished_cb),
1096 g_signal_connect (theme, "navigation-policy-decision-requested",
1097 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1100 priv->notify_enable_webkit_developer_tools_id =
1101 empathy_conf_notify_add (empathy_conf_get (),
1102 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1103 theme_adium_notify_enable_webkit_developer_tools_cb,
1106 theme_adium_update_enable_webkit_developer_tools (theme);
1110 empathy_theme_adium_new (EmpathyAdiumData *data)
1112 g_return_val_if_fail (data != NULL, NULL);
1114 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1120 empathy_adium_path_is_valid (const gchar *path)
1125 /* The theme is not valid if there is no Info.plist */
1126 file = g_build_filename (path, "Contents", "Info.plist",
1128 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1134 /* We ship a default Template.html as fallback if there is any problem
1135 * with the one inside the theme. The only other required file is
1136 * Content.html for incoming messages (outgoing fallback to use
1138 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1139 "Content.html", NULL);
1140 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1147 empathy_adium_info_new (const gchar *path)
1151 GHashTable *info = NULL;
1153 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1155 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1156 value = empathy_plist_parse_from_file (file);
1162 info = g_value_dup_boxed (value);
1163 tp_g_value_slice_free (value);
1165 /* Insert the theme's path into the hash table,
1166 * keys have to be dupped */
1167 tp_asv_set_string (info, g_strdup ("path"), path);
1173 empathy_adium_data_get_type (void)
1175 static GType type_id = 0;
1179 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1180 (GBoxedCopyFunc) empathy_adium_data_ref,
1181 (GBoxedFreeFunc) empathy_adium_data_unref);
1188 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1190 EmpathyAdiumData *data;
1192 gchar *template_html = NULL;
1194 gchar *footer_html = NULL;
1197 gchar **strv = NULL;
1202 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1204 data = g_slice_new0 (EmpathyAdiumData);
1205 data->ref_count = 1;
1206 data->path = g_strdup (path);
1207 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1208 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1209 data->info = g_hash_table_ref (info);
1211 /* Load html files */
1212 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1213 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1216 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1217 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1220 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1221 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1224 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1225 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1228 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1229 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1232 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1233 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1236 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1237 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1240 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1241 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1244 file = g_build_filename (data->basedir, "Status.html", NULL);
1245 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1248 file = g_build_filename (data->basedir, "Footer.html", NULL);
1249 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1252 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1253 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1254 data->default_incoming_avatar_filename = file;
1259 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1260 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1261 data->default_outgoing_avatar_filename = file;
1266 css_path = g_build_filename (data->basedir, "main.css", NULL);
1268 /* There is 2 formats for Template.html: The old one has 4 parameters,
1269 * the new one has 5 parameters. */
1270 file = g_build_filename (data->basedir, "Template.html", NULL);
1271 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1272 strv = g_strsplit (template_html, "%@", -1);
1273 len = g_strv_length (strv);
1277 if (len != 5 && len != 6) {
1278 /* Either the theme has no template or it don't have the good
1279 * number of parameters. Fallback to use our own template. */
1280 g_free (template_html);
1283 file = empathy_file_lookup ("Template.html", "data");
1284 g_file_get_contents (file, &template_html, &template_len, NULL);
1286 strv = g_strsplit (template_html, "%@", -1);
1287 len = g_strv_length (strv);
1290 /* Replace %@ with the needed information in the template html. */
1291 string = g_string_sized_new (template_len);
1292 g_string_append (string, strv[i++]);
1293 g_string_append (string, data->basedir);
1294 g_string_append (string, strv[i++]);
1296 const gchar *variant;
1298 /* We include main.css by default */
1299 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1300 g_string_append (string, strv[i++]);
1301 variant = tp_asv_get_string (data->info, "DefaultVariant");
1303 g_string_append (string, "Variants/");
1304 g_string_append (string, variant);
1305 g_string_append (string, ".css");
1308 /* FIXME: We should set main.css OR the variant css */
1309 g_string_append (string, css_path);
1311 g_string_append (string, strv[i++]);
1312 g_string_append (string, ""); /* We don't want header */
1313 g_string_append (string, strv[i++]);
1314 /* FIXME: We should replace adium %macros% in footer */
1316 g_string_append (string, footer_html);
1318 g_string_append (string, strv[i++]);
1319 data->template_html = g_string_free (string, FALSE);
1321 g_free (footer_html);
1322 g_free (template_html);
1330 empathy_adium_data_new (const gchar *path)
1332 EmpathyAdiumData *data;
1335 info = empathy_adium_info_new (path);
1336 data = empathy_adium_data_new_with_info (path, info);
1337 g_hash_table_unref (info);
1343 empathy_adium_data_ref (EmpathyAdiumData *data)
1345 g_return_val_if_fail (data != NULL, NULL);
1347 g_atomic_int_inc (&data->ref_count);
1353 empathy_adium_data_unref (EmpathyAdiumData *data)
1355 g_return_if_fail (data != NULL);
1357 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1358 g_free (data->path);
1359 g_free (data->basedir);
1360 g_free (data->template_html);
1361 g_free (data->in_content_html);
1362 g_free (data->in_nextcontent_html);
1363 g_free (data->in_context_html);
1364 g_free (data->in_nextcontext_html);
1365 g_free (data->out_content_html);
1366 g_free (data->out_nextcontent_html);
1367 g_free (data->out_context_html);
1368 g_free (data->out_nextcontext_html);
1369 g_free (data->default_avatar_filename);
1370 g_free (data->default_incoming_avatar_filename);
1371 g_free (data->default_outgoing_avatar_filename);
1372 g_free (data->status_html);
1373 g_hash_table_unref (data->info);
1374 g_slice_free (EmpathyAdiumData, data);
1379 empathy_adium_data_get_info (EmpathyAdiumData *data)
1381 g_return_val_if_fail (data != NULL, NULL);
1387 empathy_adium_data_get_path (EmpathyAdiumData *data)
1389 g_return_val_if_fail (data != NULL, NULL);