1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n.h>
27 #include <webkit/webkit.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
31 #include <gconf/gconf-client.h>
32 #include <pango/pango.h>
35 #include <libempathy/empathy-time.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-theme-adium.h"
39 #include "empathy-smiley-manager.h"
40 #include "empathy-conf.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-plist.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
45 #include <libempathy/empathy-debug.h>
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
49 /* GConf key containing current value of font */
50 #define EMPATHY_GCONF_FONT_KEY_NAME "/desktop/gnome/interface/font_name"
51 #define BORING_DPI_DEFAULT 96
53 /* "Join" consecutive messages with timestamps within five minutes */
54 #define MESSAGE_JOIN_PERIOD 5*60
57 EmpathyAdiumData *data;
58 EmpathySmileyManager *smiley_manager;
59 EmpathyContact *last_contact;
60 time_t last_timestamp;
61 gboolean last_is_backlog;
64 guint notify_enable_webkit_developer_tools_id;
65 GtkWidget *inspector_window;
66 } EmpathyThemeAdiumPriv;
68 struct _EmpathyAdiumData {
72 gchar *default_avatar_filename;
73 gchar *default_incoming_avatar_filename;
74 gchar *default_outgoing_avatar_filename;
76 gchar *in_content_html;
78 gchar *in_context_html;
80 gchar *in_nextcontent_html;
81 gsize in_nextcontent_len;
82 gchar *in_nextcontext_html;
83 gsize in_nextcontext_len;
84 gchar *out_content_html;
85 gsize out_content_len;
86 gchar *out_context_html;
87 gsize out_context_len;
88 gchar *out_nextcontent_html;
89 gsize out_nextcontent_len;
90 gchar *out_nextcontext_html;
91 gsize out_nextcontext_len;
97 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
104 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
105 WEBKIT_TYPE_WEB_VIEW,
106 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
107 theme_adium_iface_init));
110 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
112 WebKitWebView *web_view = WEBKIT_WEB_VIEW (theme);
113 gboolean enable_webkit_developer_tools;
115 if (!empathy_conf_get_bool (empathy_conf_get (),
116 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
117 &enable_webkit_developer_tools)) {
121 g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
122 "enable-developer-extras",
123 enable_webkit_developer_tools,
128 theme_adium_notify_enable_webkit_developer_tools_cb (EmpathyConf *conf,
132 EmpathyThemeAdium *theme = user_data;
134 theme_adium_update_enable_webkit_developer_tools (theme);
138 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView *view,
139 WebKitWebFrame *web_frame,
140 WebKitNetworkRequest *request,
141 WebKitWebNavigationAction *action,
142 WebKitWebPolicyDecision *decision,
147 /* Only call url_show on clicks */
148 if (webkit_web_navigation_action_get_reason (action) !=
149 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
150 webkit_web_policy_decision_use (decision);
154 uri = webkit_network_request_get_uri (request);
155 empathy_url_show (GTK_WIDGET (view), uri);
157 webkit_web_policy_decision_ignore (decision);
162 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
165 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
167 GtkClipboard *clipboard;
169 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
171 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
172 gtk_clipboard_set_text (clipboard, uri, -1);
174 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
175 gtk_clipboard_set_text (clipboard, uri, -1);
181 theme_adium_open_address_cb (GtkMenuItem *menuitem,
184 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
187 g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
189 empathy_url_show (GTK_WIDGET (menuitem), uri);
195 theme_adium_parser_newline (GString *string,
207 /* Replace \n by <br/> */
208 for (i = 0; i < len && text[i] != '\0'; i++) {
209 if (text[i] == '\n') {
210 empathy_string_parser_substr (string, text + prev,
211 i - prev, user_data);
212 g_string_append (string, "<br/>");
216 empathy_string_parser_substr (string, text + prev, i - prev, user_data);
220 theme_adium_parser_smiley (GString *string,
225 gboolean use_smileys = FALSE;
228 empathy_conf_get_bool (empathy_conf_get (),
229 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
233 EmpathySmileyManager *smiley_manager;
236 smiley_manager = empathy_smiley_manager_dup_singleton ();
237 hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
239 for (l = hits; l; l = l->next) {
240 EmpathySmileyHit *hit = l->data;
242 if (hit->start > last) {
243 /* Append the text between last smiley (or the
244 * start of the message) and this smiley */
245 empathy_string_parser_substr (string, text + last,
250 /* Replace smileys by a <img/> tag */
251 g_string_append (string, "<abbr title=\"");
252 g_string_append_len (string, text + hit->start,
253 hit->end - hit->start);
254 g_string_append_printf (string, "\"><img src=\"%s\" alt=\"",
256 g_string_append_len (string, text + hit->start,
257 hit->end - hit->start);
258 g_string_append (string, "\"/></abbr>");
262 empathy_smiley_hit_free (hit);
265 g_object_unref (smiley_manager);
268 empathy_string_parser_substr (string, text + last, len - last, user_data);
271 static EmpathyStringParser string_parsers[] = {
272 empathy_string_parser_link,
273 theme_adium_parser_smiley,
274 theme_adium_parser_newline,
275 empathy_string_parser_escape,
280 theme_adium_parse_body (const gchar *text)
284 /* We parse text in 4 steps: url, smiley, newline, escape.
285 * For each step, we detect the position of tokens in the text, and
286 * we give text between each token to the next level parser.
288 * For example the string "Hello :)\n www.test.com"
289 * 1) The url parser detects "www.test.com" and gives "Hello :)\n " to
290 * the smiley parser, then insert the <a> tag for the link.
291 * 2) The smiley parser will detect ":)". It first gives "Hello "
292 * to the newline parser, then insert the <img/> tag for the smiley,
293 * and finally give "\n " to the newline parser.
294 * 3a) The newline parser gets "Hello " and escape it.
295 * 3b) The newline parser gets "\n " and replace to "<br/> ".
298 string = g_string_sized_new (strlen (text));
299 empathy_string_parser_substr (string, text, -1, string_parsers);
301 return g_string_free (string, FALSE);
305 escape_and_append_len (GString *string, const gchar *str, gint len)
307 while (*str != '\0' && len != 0) {
311 g_string_append (string, "\\\\");
315 g_string_append (string, "\\\"");
318 /* Remove end of lines */
321 g_string_append_c (string, *str);
330 theme_adium_match (const gchar **str, const gchar *match)
334 len = strlen (match);
335 if (strncmp (*str, match, len) == 0) {
344 theme_adium_append_html (EmpathyThemeAdium *theme,
346 const gchar *html, gsize len,
347 const gchar *message,
348 const gchar *avatar_filename,
350 const gchar *contact_id,
351 const gchar *service_name,
352 const gchar *message_classes,
356 const gchar *cur = NULL;
359 /* Make some search-and-replace in the html code */
360 string = g_string_sized_new (len + strlen (message));
361 g_string_append_printf (string, "%s(\"", func);
362 for (cur = html; *cur != '\0'; cur++) {
363 const gchar *replace = NULL;
364 gchar *dup_replace = NULL;
366 if (theme_adium_match (&cur, "%message%")) {
368 } else if (theme_adium_match (&cur, "%messageClasses%")) {
369 replace = message_classes;
370 } else if (theme_adium_match (&cur, "%userIconPath%")) {
371 replace = avatar_filename;
372 } else if (theme_adium_match (&cur, "%sender%")) {
374 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
375 replace = contact_id;
376 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
377 /* %senderDisplayName% -
378 * "The serverside (remotely set) name of the sender,
379 * such as an MSN display name."
381 * We don't have access to that yet so we use local
384 } else if (theme_adium_match (&cur, "%service%")) {
385 replace = service_name;
386 } else if (theme_adium_match (&cur, "%shortTime%")) {
387 dup_replace = empathy_time_to_string_local (timestamp,
388 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
389 replace = dup_replace;
390 } else if (theme_adium_match (&cur, "%time")) {
391 gchar *format = NULL;
393 /* Time can be in 2 formats:
394 * %time% or %time{strftime format}%
395 * Extract the time format if provided. */
398 end = strstr (cur, "}%");
403 format = g_strndup (cur, end - cur);
409 dup_replace = empathy_time_to_string_local (timestamp,
410 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
411 replace = dup_replace;
414 escape_and_append_len (string, cur, 1);
418 /* Here we have a replacement to make */
419 escape_and_append_len (string, replace, -1);
420 g_free (dup_replace);
422 g_string_append (string, "\")");
424 script = g_string_free (string, FALSE);
425 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
430 theme_adium_append_message (EmpathyChatView *view,
433 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
434 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
435 EmpathyContact *sender;
440 const gchar *contact_id;
441 EmpathyAvatar *avatar;
442 const gchar *avatar_filename = NULL;
447 const gchar *service_name;
448 GString *message_classes = NULL;
450 gboolean consecutive;
452 if (!priv->page_loaded) {
453 priv->message_queue = g_list_prepend (priv->message_queue,
458 /* Get information */
459 sender = empathy_message_get_sender (msg);
460 account = empathy_contact_get_account (sender);
461 service_name = empathy_protocol_name_to_display_name
462 (tp_account_get_protocol (account));
463 if (service_name == NULL)
464 service_name = tp_account_get_protocol (account);
465 timestamp = empathy_message_get_timestamp (msg);
466 body = empathy_message_get_body (msg);
467 body_escaped = theme_adium_parse_body (body);
468 name = empathy_contact_get_name (sender);
469 contact_id = empathy_contact_get_id (sender);
471 /* If this is a /me, append an event */
472 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
475 str = g_strdup_printf ("%s %s", name, body_escaped);
476 empathy_chat_view_append_event (view, str);
478 g_free (body_escaped);
482 /* Get the avatar filename, or a fallback */
483 avatar = empathy_contact_get_avatar (sender);
485 avatar_filename = avatar->filename;
487 if (!avatar_filename) {
488 if (empathy_contact_is_user (sender)) {
489 avatar_filename = priv->data->default_outgoing_avatar_filename;
491 avatar_filename = priv->data->default_incoming_avatar_filename;
493 if (!avatar_filename) {
494 if (!priv->data->default_avatar_filename) {
495 priv->data->default_avatar_filename =
496 empathy_filename_from_icon_name ("stock_person",
497 GTK_ICON_SIZE_DIALOG);
499 avatar_filename = priv->data->default_avatar_filename;
503 /* We want to join this message with the last one if
504 * - senders are the same contact,
505 * - last message was recieved recently,
506 * - last message and this message both are/aren't backlog, and
507 * - DisableCombineConsecutive is not set in theme's settings */
508 is_backlog = empathy_message_is_backlog (msg);
509 consecutive = empathy_contact_equal (priv->last_contact, sender) &&
510 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
511 (is_backlog == priv->last_is_backlog) &&
512 !tp_asv_get_boolean (priv->data->info,
513 "DisableCombineConsecutive", NULL);
515 /* Define message classes */
516 message_classes = g_string_new ("message");
518 g_string_append (message_classes, " history");
521 g_string_append (message_classes, " consecutive");
523 if (empathy_contact_is_user (sender)) {
524 g_string_append (message_classes, " outgoing");
526 g_string_append (message_classes, " incoming");
529 /* Define javascript function to use */
531 func = "appendNextMessage";
533 func = "appendMessage";
537 if (empathy_contact_is_user (sender)) {
540 html = priv->data->out_nextcontext_html;
541 len = priv->data->out_nextcontext_len;
544 /* Not backlog, or fallback if NextContext.html
547 html = priv->data->out_nextcontent_html;
548 len = priv->data->out_nextcontent_len;
552 /* Not consecutive, or fallback if NextContext.html and/or
553 * NextContent.html are missing */
556 html = priv->data->out_context_html;
557 len = priv->data->out_context_len;
561 html = priv->data->out_content_html;
562 len = priv->data->out_content_len;
567 /* Incoming, or fallback if outgoing files are missing */
571 html = priv->data->in_nextcontext_html;
572 len = priv->data->in_nextcontext_len;
575 /* Note backlog, or fallback if NextContext.html
578 html = priv->data->in_nextcontent_html;
579 len = priv->data->in_nextcontent_len;
583 /* Not consecutive, or fallback if NextContext.html and/or
584 * NextContent.html are missing */
587 html = priv->data->in_context_html;
588 len = priv->data->in_context_len;
592 html = priv->data->in_content_html;
593 len = priv->data->in_content_len;
599 theme_adium_append_html (theme, func, html, len, body_escaped,
600 avatar_filename, name, contact_id,
601 service_name, message_classes->str,
604 DEBUG ("Couldn't find HTML file for this message");
607 /* Keep the sender of the last displayed message */
608 if (priv->last_contact) {
609 g_object_unref (priv->last_contact);
611 priv->last_contact = g_object_ref (sender);
612 priv->last_timestamp = timestamp;
613 priv->last_is_backlog = is_backlog;
615 g_free (body_escaped);
616 g_string_free (message_classes, TRUE);
620 theme_adium_append_event (EmpathyChatView *view,
623 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
624 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
626 if (priv->data->status_html) {
629 str_escaped = g_markup_escape_text (str, -1);
630 theme_adium_append_html (theme, "appendMessage",
631 priv->data->status_html,
632 priv->data->status_len,
633 str_escaped, NULL, NULL, NULL, NULL,
634 "event", empathy_time_get_current ());
635 g_free (str_escaped);
638 /* There is no last contact */
639 if (priv->last_contact) {
640 g_object_unref (priv->last_contact);
641 priv->last_contact = NULL;
646 theme_adium_scroll (EmpathyChatView *view,
647 gboolean allow_scrolling)
649 /* FIXME: Is it possible? I guess we need a js function, but I don't
654 theme_adium_scroll_down (EmpathyChatView *view)
656 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
660 theme_adium_get_has_selection (EmpathyChatView *view)
662 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
666 theme_adium_clear (EmpathyChatView *view)
668 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
671 priv->page_loaded = FALSE;
672 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
673 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
674 priv->data->template_html,
676 g_free (basedir_uri);
678 /* Clear last contact to avoid trying to add a 'joined'
679 * message when we don't have an insertion point. */
680 if (priv->last_contact) {
681 g_object_unref (priv->last_contact);
682 priv->last_contact = NULL;
687 theme_adium_find_previous (EmpathyChatView *view,
688 const gchar *search_criteria,
691 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
692 search_criteria, FALSE,
697 theme_adium_find_next (EmpathyChatView *view,
698 const gchar *search_criteria,
701 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
702 search_criteria, FALSE,
707 theme_adium_find_abilities (EmpathyChatView *view,
708 const gchar *search_criteria,
709 gboolean *can_do_previous,
710 gboolean *can_do_next)
712 /* FIXME: Does webkit provide an API for that? We have wrap=true in
713 * find_next and find_previous to work around this problem. */
715 *can_do_previous = TRUE;
721 theme_adium_highlight (EmpathyChatView *view,
724 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
725 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
727 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
732 theme_adium_copy_clipboard (EmpathyChatView *view)
734 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
738 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
740 WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
742 g_object_unref (hit_test_result);
746 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
748 WebKitWebView *view = WEBKIT_WEB_VIEW (theme);
749 WebKitHitTestResult *hit_test_result;
750 WebKitHitTestResultContext context;
754 hit_test_result = webkit_web_view_get_hit_test_result (view, event);
755 g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
758 menu = gtk_menu_new ();
760 /* Select all item */
761 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
762 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
764 g_signal_connect_swapped (item, "activate",
765 G_CALLBACK (webkit_web_view_select_all),
769 if (webkit_web_view_can_copy_clipboard (view)) {
770 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
771 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
773 g_signal_connect_swapped (item, "activate",
774 G_CALLBACK (webkit_web_view_copy_clipboard),
778 /* Clear menu item */
779 item = gtk_separator_menu_item_new ();
780 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
782 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
783 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
785 g_signal_connect_swapped (item, "activate",
786 G_CALLBACK (empathy_chat_view_clear),
789 /* We will only add the following menu items if we are
790 * right-clicking a link */
791 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
793 item = gtk_separator_menu_item_new ();
794 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
796 /* Copy Link Address menu item */
797 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
798 g_signal_connect (item, "activate",
799 G_CALLBACK (theme_adium_copy_address_cb),
801 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
803 /* Open Link menu item */
804 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
805 g_signal_connect (item, "activate",
806 G_CALLBACK (theme_adium_open_address_cb),
808 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
811 g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
812 G_CALLBACK (theme_adium_context_menu_selection_done_cb),
815 /* Display the menu */
816 gtk_widget_show_all (menu);
817 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
818 event->button, event->time);
822 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
824 if (event->button == 3) {
825 gboolean developer_tools_enabled;
827 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
828 "enable-developer-extras", &developer_tools_enabled, NULL);
830 /* We currently have no way to add an inspector menu
831 * item ourselves, so we disable our customized menu
832 * if the developer extras are enabled. */
833 if (!developer_tools_enabled) {
834 theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
839 return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
843 theme_adium_iface_init (EmpathyChatViewIface *iface)
845 iface->append_message = theme_adium_append_message;
846 iface->append_event = theme_adium_append_event;
847 iface->scroll = theme_adium_scroll;
848 iface->scroll_down = theme_adium_scroll_down;
849 iface->get_has_selection = theme_adium_get_has_selection;
850 iface->clear = theme_adium_clear;
851 iface->find_previous = theme_adium_find_previous;
852 iface->find_next = theme_adium_find_next;
853 iface->find_abilities = theme_adium_find_abilities;
854 iface->highlight = theme_adium_highlight;
855 iface->copy_clipboard = theme_adium_copy_clipboard;
859 theme_adium_load_finished_cb (WebKitWebView *view,
860 WebKitWebFrame *frame,
863 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
864 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
866 DEBUG ("Page loaded");
867 priv->page_loaded = TRUE;
869 /* Display queued messages */
870 priv->message_queue = g_list_reverse (priv->message_queue);
871 while (priv->message_queue) {
872 EmpathyMessage *message = priv->message_queue->data;
874 theme_adium_append_message (chat_view, message);
875 priv->message_queue = g_list_remove (priv->message_queue, message);
876 g_object_unref (message);
881 theme_adium_finalize (GObject *object)
883 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
885 empathy_adium_data_unref (priv->data);
887 empathy_conf_notify_remove (empathy_conf_get (),
888 priv->notify_enable_webkit_developer_tools_id);
890 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
894 theme_adium_dispose (GObject *object)
896 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
898 if (priv->smiley_manager) {
899 g_object_unref (priv->smiley_manager);
900 priv->smiley_manager = NULL;
903 if (priv->last_contact) {
904 g_object_unref (priv->last_contact);
905 priv->last_contact = NULL;
908 if (priv->inspector_window) {
909 gtk_widget_destroy (priv->inspector_window);
910 priv->inspector_window = NULL;
913 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
917 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
918 EmpathyThemeAdium *theme)
920 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
922 if (priv->inspector_window) {
923 gtk_widget_show_all (priv->inspector_window);
930 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
931 EmpathyThemeAdium *theme)
933 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
935 if (priv->inspector_window) {
936 gtk_widget_hide (priv->inspector_window);
942 static WebKitWebView *
943 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
944 WebKitWebView *web_view,
945 EmpathyThemeAdium *theme)
947 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
948 GtkWidget *scrolled_window;
949 GtkWidget *inspector_web_view;
951 if (!priv->inspector_window) {
952 /* Create main window */
953 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
954 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
956 g_signal_connect (priv->inspector_window, "delete-event",
957 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
959 /* Pack a scrolled window */
960 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
961 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
962 GTK_POLICY_AUTOMATIC,
963 GTK_POLICY_AUTOMATIC);
964 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
966 gtk_widget_show (scrolled_window);
968 /* Pack a webview in the scrolled window. That webview will be
969 * used to render the inspector tool. */
970 inspector_web_view = webkit_web_view_new ();
971 gtk_container_add (GTK_CONTAINER (scrolled_window),
973 gtk_widget_show (scrolled_window);
975 return WEBKIT_WEB_VIEW (inspector_web_view);
981 static PangoFontDescription *
982 theme_adium_get_default_font (void)
984 GConfClient *gconf_client;
985 PangoFontDescription *pango_fd;
986 gchar *gconf_font_family;
988 gconf_client = gconf_client_get_default ();
989 if (gconf_client == NULL) {
992 gconf_font_family = gconf_client_get_string (gconf_client,
993 EMPATHY_GCONF_FONT_KEY_NAME,
995 if (gconf_font_family == NULL) {
996 g_object_unref (gconf_client);
999 pango_fd = pango_font_description_from_string (gconf_font_family);
1000 g_free (gconf_font_family);
1001 g_object_unref (gconf_client);
1006 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1010 g_object_set (w_settings, "default-font-family", name, NULL);
1011 g_object_set (w_settings, "default-font-size", size, NULL);
1015 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1017 PangoFontDescription *default_font_desc;
1018 GdkScreen *current_screen;
1020 gint pango_font_size = 0;
1022 default_font_desc = theme_adium_get_default_font ();
1023 if (default_font_desc == NULL)
1025 pango_font_size = pango_font_description_get_size (default_font_desc)
1027 if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1028 current_screen = gdk_screen_get_default ();
1029 if (current_screen != NULL) {
1030 dpi = gdk_screen_get_resolution (current_screen);
1032 dpi = BORING_DPI_DEFAULT;
1034 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1036 theme_adium_set_webkit_font (w_settings,
1037 pango_font_description_get_family (default_font_desc),
1039 pango_font_description_free (default_font_desc);
1043 theme_adium_constructed (GObject *object)
1045 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1047 const gchar *font_family = NULL;
1049 WebKitWebView *webkit_view = WEBKIT_WEB_VIEW (object);
1050 WebKitWebSettings *webkit_settings;
1051 WebKitWebInspector *webkit_inspector;
1053 /* Set default settings */
1054 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1055 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1056 webkit_settings = webkit_web_view_get_settings (webkit_view);
1058 if (font_family && font_size) {
1059 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1061 theme_adium_set_default_font (webkit_settings);
1064 /* Setup webkit inspector */
1065 webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1066 g_signal_connect (webkit_inspector, "inspect-web-view",
1067 G_CALLBACK (theme_adium_inspect_web_view_cb),
1069 g_signal_connect (webkit_inspector, "show-window",
1070 G_CALLBACK (theme_adium_inspector_show_window_cb),
1072 g_signal_connect (webkit_inspector, "close-window",
1073 G_CALLBACK (theme_adium_inspector_close_window_cb),
1077 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1078 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1079 priv->data->template_html,
1081 g_free (basedir_uri);
1085 theme_adium_get_property (GObject *object,
1090 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1093 case PROP_ADIUM_DATA:
1094 g_value_set_boxed (value, priv->data);
1097 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1103 theme_adium_set_property (GObject *object,
1105 const GValue *value,
1108 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1111 case PROP_ADIUM_DATA:
1112 g_assert (priv->data == NULL);
1113 priv->data = g_value_dup_boxed (value);
1116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1122 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1124 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1125 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1127 object_class->finalize = theme_adium_finalize;
1128 object_class->dispose = theme_adium_dispose;
1129 object_class->constructed = theme_adium_constructed;
1130 object_class->get_property = theme_adium_get_property;
1131 object_class->set_property = theme_adium_set_property;
1133 widget_class->button_press_event = theme_adium_button_press_event;
1135 g_object_class_install_property (object_class,
1137 g_param_spec_boxed ("adium-data",
1139 "Data for the adium theme",
1140 EMPATHY_TYPE_ADIUM_DATA,
1141 G_PARAM_CONSTRUCT_ONLY |
1143 G_PARAM_STATIC_STRINGS));
1145 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1149 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1151 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1152 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1156 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1158 g_signal_connect (theme, "load-finished",
1159 G_CALLBACK (theme_adium_load_finished_cb),
1161 g_signal_connect (theme, "navigation-policy-decision-requested",
1162 G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1165 priv->notify_enable_webkit_developer_tools_id =
1166 empathy_conf_notify_add (empathy_conf_get (),
1167 EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1168 theme_adium_notify_enable_webkit_developer_tools_cb,
1171 theme_adium_update_enable_webkit_developer_tools (theme);
1175 empathy_theme_adium_new (EmpathyAdiumData *data)
1177 g_return_val_if_fail (data != NULL, NULL);
1179 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1185 empathy_adium_path_is_valid (const gchar *path)
1190 /* The theme is not valid if there is no Info.plist */
1191 file = g_build_filename (path, "Contents", "Info.plist",
1193 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1199 /* We ship a default Template.html as fallback if there is any problem
1200 * with the one inside the theme. The only other required file is
1201 * Content.html for incoming messages (outgoing fallback to use
1203 file = g_build_filename (path, "Contents", "Resources", "Incoming",
1204 "Content.html", NULL);
1205 ret = g_file_test (file, G_FILE_TEST_EXISTS);
1212 empathy_adium_info_new (const gchar *path)
1216 GHashTable *info = NULL;
1218 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1220 file = g_build_filename (path, "Contents", "Info.plist", NULL);
1221 value = empathy_plist_parse_from_file (file);
1227 info = g_value_dup_boxed (value);
1228 tp_g_value_slice_free (value);
1230 /* Insert the theme's path into the hash table,
1231 * keys have to be dupped */
1232 tp_asv_set_string (info, g_strdup ("path"), path);
1238 empathy_adium_data_get_type (void)
1240 static GType type_id = 0;
1244 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1245 (GBoxedCopyFunc) empathy_adium_data_ref,
1246 (GBoxedFreeFunc) empathy_adium_data_unref);
1253 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1255 EmpathyAdiumData *data;
1257 gchar *template_html = NULL;
1259 gchar *footer_html = NULL;
1262 gchar **strv = NULL;
1267 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1269 data = g_slice_new0 (EmpathyAdiumData);
1270 data->ref_count = 1;
1271 data->path = g_strdup (path);
1272 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1273 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1274 data->info = g_hash_table_ref (info);
1276 /* Load html files */
1277 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1278 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1281 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1282 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1285 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1286 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1289 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1290 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1293 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1294 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1297 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1298 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1301 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1302 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1305 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1306 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1309 file = g_build_filename (data->basedir, "Status.html", NULL);
1310 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1313 file = g_build_filename (data->basedir, "Footer.html", NULL);
1314 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1317 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1318 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1319 data->default_incoming_avatar_filename = file;
1324 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1325 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1326 data->default_outgoing_avatar_filename = file;
1331 css_path = g_build_filename (data->basedir, "main.css", NULL);
1333 /* There is 2 formats for Template.html: The old one has 4 parameters,
1334 * the new one has 5 parameters. */
1335 file = g_build_filename (data->basedir, "Template.html", NULL);
1336 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1337 strv = g_strsplit (template_html, "%@", -1);
1338 len = g_strv_length (strv);
1342 if (len != 5 && len != 6) {
1343 /* Either the theme has no template or it don't have the good
1344 * number of parameters. Fallback to use our own template. */
1345 g_free (template_html);
1348 file = empathy_file_lookup ("Template.html", "data");
1349 g_file_get_contents (file, &template_html, &template_len, NULL);
1351 strv = g_strsplit (template_html, "%@", -1);
1352 len = g_strv_length (strv);
1355 /* Replace %@ with the needed information in the template html. */
1356 string = g_string_sized_new (template_len);
1357 g_string_append (string, strv[i++]);
1358 g_string_append (string, data->basedir);
1359 g_string_append (string, strv[i++]);
1361 const gchar *variant;
1363 /* We include main.css by default */
1364 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1365 g_string_append (string, strv[i++]);
1366 variant = tp_asv_get_string (data->info, "DefaultVariant");
1368 g_string_append (string, "Variants/");
1369 g_string_append (string, variant);
1370 g_string_append (string, ".css");
1373 /* FIXME: We should set main.css OR the variant css */
1374 g_string_append (string, css_path);
1376 g_string_append (string, strv[i++]);
1377 g_string_append (string, ""); /* We don't want header */
1378 g_string_append (string, strv[i++]);
1379 /* FIXME: We should replace adium %macros% in footer */
1381 g_string_append (string, footer_html);
1383 g_string_append (string, strv[i++]);
1384 data->template_html = g_string_free (string, FALSE);
1386 g_free (footer_html);
1387 g_free (template_html);
1395 empathy_adium_data_new (const gchar *path)
1397 EmpathyAdiumData *data;
1400 info = empathy_adium_info_new (path);
1401 data = empathy_adium_data_new_with_info (path, info);
1402 g_hash_table_unref (info);
1408 empathy_adium_data_ref (EmpathyAdiumData *data)
1410 g_return_val_if_fail (data != NULL, NULL);
1412 g_atomic_int_inc (&data->ref_count);
1418 empathy_adium_data_unref (EmpathyAdiumData *data)
1420 g_return_if_fail (data != NULL);
1422 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1423 g_free (data->path);
1424 g_free (data->basedir);
1425 g_free (data->template_html);
1426 g_free (data->in_content_html);
1427 g_free (data->in_nextcontent_html);
1428 g_free (data->in_context_html);
1429 g_free (data->in_nextcontext_html);
1430 g_free (data->out_content_html);
1431 g_free (data->out_nextcontent_html);
1432 g_free (data->out_context_html);
1433 g_free (data->out_nextcontext_html);
1434 g_free (data->default_avatar_filename);
1435 g_free (data->default_incoming_avatar_filename);
1436 g_free (data->default_outgoing_avatar_filename);
1437 g_free (data->status_html);
1438 g_hash_table_unref (data->info);
1439 g_slice_free (EmpathyAdiumData, data);
1444 empathy_adium_data_get_info (EmpathyAdiumData *data)
1446 g_return_val_if_fail (data != NULL, NULL);
1452 empathy_adium_data_get_path (EmpathyAdiumData *data)
1454 g_return_val_if_fail (data != NULL, NULL);