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;
57 } EmpathyThemeAdiumPriv;
59 struct _EmpathyAdiumData {
63 gchar *default_avatar_filename;
64 gchar *default_incoming_avatar_filename;
65 gchar *default_outgoing_avatar_filename;
67 gchar *in_content_html;
69 gchar *in_nextcontent_html;
70 gsize in_nextcontent_len;
71 gchar *out_content_html;
72 gsize out_content_len;
73 gchar *out_nextcontent_html;
74 gsize out_nextcontent_len;
80 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
87 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
90 theme_adium_iface_init));
92 static WebKitNavigationResponse
93 theme_adium_navigation_requested_cb (WebKitWebView *view,
94 WebKitWebFrame *frame,
95 WebKitNetworkRequest *request,
100 uri = webkit_network_request_get_uri (request);
101 empathy_url_show (GTK_WIDGET (view), uri);
103 return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
107 theme_adium_populate_popup_cb (WebKitWebView *view,
113 /* Remove default menu items */
114 gtk_container_foreach (GTK_CONTAINER (menu),
115 (GtkCallback) gtk_widget_destroy, NULL);
117 /* Select all item */
118 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
119 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
120 gtk_widget_show (item);
122 g_signal_connect_swapped (item, "activate",
123 G_CALLBACK (webkit_web_view_select_all),
127 if (webkit_web_view_can_copy_clipboard (view)) {
128 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
129 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
130 gtk_widget_show (item);
132 g_signal_connect_swapped (item, "activate",
133 G_CALLBACK (webkit_web_view_copy_clipboard),
137 /* Clear menu item */
138 item = gtk_separator_menu_item_new ();
139 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
140 gtk_widget_show (item);
142 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
143 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
144 gtk_widget_show (item);
146 g_signal_connect_swapped (item, "activate",
147 G_CALLBACK (empathy_chat_view_clear),
150 /* FIXME: Add open_link and copy_link when those bugs are fixed:
151 * https://bugs.webkit.org/show_bug.cgi?id=16092
152 * https://bugs.webkit.org/show_bug.cgi?id=16562
157 theme_adium_parse_body (EmpathyThemeAdium *theme,
160 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
161 gboolean use_smileys = FALSE;
166 GMatchInfo *match_info;
171 empathy_conf_get_bool (empathy_conf_get (),
172 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
176 /* Replace smileys by a <img/> tag */
177 string = g_string_sized_new (strlen (text));
178 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
179 for (l = smileys; l; l = l->next) {
180 EmpathySmiley *smiley;
184 g_string_append_printf (string,
185 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
186 smiley->str, smiley->path, smiley->str);
190 str = g_markup_escape_text (smiley->str, -1);
191 g_string_append (string, str);
194 empathy_smiley_free (smiley);
196 g_slist_free (smileys);
199 text = ret = g_string_free (string, FALSE);
202 /* Add <a href></a> arround links */
203 uri_regex = empathy_uri_regex_dup_singleton ();
204 match = g_regex_match (uri_regex, text, 0, &match_info);
209 string = g_string_sized_new (strlen (text));
211 g_match_info_fetch_pos (match_info, 0, &s, &e);
214 /* Append the text between last link (or the
215 * start of the message) and this link */
216 g_string_append_len (string, text + last, s - last);
219 /* Append the link inside <a href=""></a> tag */
220 g_string_append (string, "<a href=\"");
221 g_string_append_len (string, text + s, e - s);
222 g_string_append (string, "\">");
223 g_string_append_len (string, text + s, e - s);
224 g_string_append (string, "</a>");
227 } while (g_match_info_next (match_info, NULL));
229 if (e < strlen (text)) {
230 /* Append the text after the last link */
231 g_string_append_len (string, text + e, strlen (text) - e);
235 text = ret = g_string_free (string, FALSE);
237 g_match_info_free (match_info);
238 g_regex_unref (uri_regex);
240 /* Replace \n by <br/> */
243 for (i = 0; text[i] != '\0'; i++) {
244 if (text[i] == '\n') {
246 string = g_string_sized_new (strlen (text));
248 g_string_append_len (string, text + prev, i - prev);
249 g_string_append (string, "<br/>");
254 g_string_append (string, text + prev);
256 text = ret = g_string_free (string, FALSE);
263 escape_and_append_len (GString *string, const gchar *str, gint len)
265 while (*str != '\0' && len != 0) {
269 g_string_append (string, "\\\\");
273 g_string_append (string, "\\\"");
276 /* Remove end of lines */
279 g_string_append_c (string, *str);
288 theme_adium_match (const gchar **str, const gchar *match)
292 len = strlen (match);
293 if (strncmp (*str, match, len) == 0) {
302 theme_adium_append_html (EmpathyThemeAdium *theme,
304 const gchar *html, gsize len,
305 const gchar *message,
306 const gchar *avatar_filename,
308 const gchar *contact_id,
309 const gchar *service_name,
310 const gchar *message_classes,
314 const gchar *cur = NULL;
317 /* Make some search-and-replace in the html code */
318 string = g_string_sized_new (len + strlen (message));
319 g_string_append_printf (string, "%s(\"", func);
320 for (cur = html; *cur != '\0'; cur++) {
321 const gchar *replace = NULL;
322 gchar *dup_replace = NULL;
324 if (theme_adium_match (&cur, "%message%")) {
326 } else if (theme_adium_match (&cur, "%messageClasses%")) {
327 replace = message_classes;
328 } else if (theme_adium_match (&cur, "%userIconPath%")) {
329 replace = avatar_filename;
330 } else if (theme_adium_match (&cur, "%sender%")) {
332 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
333 replace = contact_id;
334 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
335 /* %senderDisplayName% -
336 * "The serverside (remotely set) name of the sender,
337 * such as an MSN display name."
339 * We don't have access to that yet so we use local
342 } else if (theme_adium_match (&cur, "%service%")) {
343 replace = service_name;
344 } else if (theme_adium_match (&cur, "%shortTime%")) {
345 dup_replace = empathy_time_to_string_local (timestamp,
346 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
347 replace = dup_replace;
348 } else if (theme_adium_match (&cur, "%time")) {
349 gchar *format = NULL;
351 /* Time can be in 2 formats:
352 * %time% or %time{strftime format}%
353 * Extract the time format if provided. */
356 end = strstr (cur, "}%");
361 format = g_strndup (cur, end - cur);
367 dup_replace = empathy_time_to_string_local (timestamp,
368 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
369 replace = dup_replace;
372 escape_and_append_len (string, cur, 1);
376 /* Here we have a replacement to make */
377 escape_and_append_len (string, replace, -1);
378 g_free (dup_replace);
380 g_string_append (string, "\")");
382 script = g_string_free (string, FALSE);
383 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
388 theme_adium_append_message (EmpathyChatView *view,
391 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
392 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
393 EmpathyContact *sender;
394 EmpathyAccount *account;
395 gchar *dup_body = NULL;
398 const gchar *contact_id;
399 EmpathyAvatar *avatar;
400 const gchar *avatar_filename = NULL;
405 const gchar *service_name;
406 const gchar *message_classes = NULL;
408 if (!priv->page_loaded) {
409 priv->message_queue = g_list_prepend (priv->message_queue,
414 /* Get information */
415 sender = empathy_message_get_sender (msg);
416 account = empathy_contact_get_account (sender);
417 service_name = empathy_account_get_protocol (account);
418 timestamp = empathy_message_get_timestamp (msg);
419 body = empathy_message_get_body (msg);
420 dup_body = theme_adium_parse_body (theme, body);
424 name = empathy_contact_get_name (sender);
425 contact_id = empathy_contact_get_id (sender);
427 /* If this is a /me, append an event */
428 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
431 str = g_strdup_printf ("%s %s", name, body);
432 empathy_chat_view_append_event (view, str);
438 /* Get the avatar filename, or a fallback */
439 avatar = empathy_contact_get_avatar (sender);
441 avatar_filename = avatar->filename;
443 if (!avatar_filename) {
444 if (empathy_contact_is_user (sender)) {
445 avatar_filename = priv->data->default_outgoing_avatar_filename;
447 avatar_filename = priv->data->default_incoming_avatar_filename;
449 if (!avatar_filename) {
450 if (!priv->data->default_avatar_filename) {
451 priv->data->default_avatar_filename =
452 empathy_filename_from_icon_name ("stock_person",
453 GTK_ICON_SIZE_DIALOG);
455 avatar_filename = priv->data->default_avatar_filename;
459 /* Get the right html/func to add the message */
460 func = "appendMessage";
462 * To mimick Adium's behavior, we only want to join messages
463 * sent within a 5 minute time frame.
465 if (empathy_contact_equal (priv->last_contact, sender) &&
466 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD)) {
467 func = "appendNextMessage";
468 if (empathy_contact_is_user (sender)) {
469 message_classes = "consecutive incoming message";
470 html = priv->data->out_nextcontent_html;
471 len = priv->data->out_nextcontent_len;
474 message_classes = "consecutive message outgoing";
475 html = priv->data->in_nextcontent_html;
476 len = priv->data->in_nextcontent_len;
480 if (empathy_contact_is_user (sender)) {
481 if (!message_classes) {
482 message_classes = "incoming message";
484 html = priv->data->out_content_html;
485 len = priv->data->out_content_len;
488 if (!message_classes) {
489 message_classes = "message outgoing";
491 html = priv->data->in_content_html;
492 len = priv->data->in_content_len;
496 theme_adium_append_html (theme, func, html, len, body, avatar_filename,
497 name, contact_id, service_name, message_classes,
500 /* Keep the sender of the last displayed message */
501 if (priv->last_contact) {
502 g_object_unref (priv->last_contact);
504 priv->last_contact = g_object_ref (sender);
505 priv->last_timestamp = timestamp;
511 theme_adium_append_event (EmpathyChatView *view,
514 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
515 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
517 if (priv->data->status_html) {
518 theme_adium_append_html (theme, "appendMessage",
519 priv->data->status_html,
520 priv->data->status_len,
521 str, NULL, NULL, NULL, NULL, "event",
522 empathy_time_get_current ());
525 /* There is no last contact */
526 if (priv->last_contact) {
527 g_object_unref (priv->last_contact);
528 priv->last_contact = NULL;
533 theme_adium_scroll (EmpathyChatView *view,
534 gboolean allow_scrolling)
536 /* FIXME: Is it possible? I guess we need a js function, but I don't
541 theme_adium_scroll_down (EmpathyChatView *view)
543 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
547 theme_adium_get_has_selection (EmpathyChatView *view)
549 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
553 theme_adium_clear (EmpathyChatView *view)
555 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
558 priv->page_loaded = FALSE;
559 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
560 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
561 priv->data->template_html,
563 g_free (basedir_uri);
567 theme_adium_find_previous (EmpathyChatView *view,
568 const gchar *search_criteria,
571 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
572 search_criteria, FALSE,
577 theme_adium_find_next (EmpathyChatView *view,
578 const gchar *search_criteria,
581 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
582 search_criteria, FALSE,
587 theme_adium_find_abilities (EmpathyChatView *view,
588 const gchar *search_criteria,
589 gboolean *can_do_previous,
590 gboolean *can_do_next)
592 /* FIXME: Does webkit provide an API for that? We have wrap=true in
593 * find_next and find_previous to work around this problem. */
595 *can_do_previous = TRUE;
601 theme_adium_highlight (EmpathyChatView *view,
604 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
605 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
607 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
612 theme_adium_copy_clipboard (EmpathyChatView *view)
614 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
618 theme_adium_iface_init (EmpathyChatViewIface *iface)
620 iface->append_message = theme_adium_append_message;
621 iface->append_event = theme_adium_append_event;
622 iface->scroll = theme_adium_scroll;
623 iface->scroll_down = theme_adium_scroll_down;
624 iface->get_has_selection = theme_adium_get_has_selection;
625 iface->clear = theme_adium_clear;
626 iface->find_previous = theme_adium_find_previous;
627 iface->find_next = theme_adium_find_next;
628 iface->find_abilities = theme_adium_find_abilities;
629 iface->highlight = theme_adium_highlight;
630 iface->copy_clipboard = theme_adium_copy_clipboard;
634 theme_adium_load_finished_cb (WebKitWebView *view,
635 WebKitWebFrame *frame,
638 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
639 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
641 DEBUG ("Page loaded");
642 priv->page_loaded = TRUE;
644 /* Display queued messages */
645 priv->message_queue = g_list_reverse (priv->message_queue);
646 while (priv->message_queue) {
647 EmpathyMessage *message = priv->message_queue->data;
649 theme_adium_append_message (chat_view, message);
650 priv->message_queue = g_list_remove (priv->message_queue, message);
651 g_object_unref (message);
656 theme_adium_finalize (GObject *object)
658 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
660 empathy_adium_data_unref (priv->data);
662 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
666 theme_adium_dispose (GObject *object)
668 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
670 if (priv->smiley_manager) {
671 g_object_unref (priv->smiley_manager);
672 priv->smiley_manager = NULL;
675 if (priv->last_contact) {
676 g_object_unref (priv->last_contact);
677 priv->last_contact = NULL;
680 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
684 theme_adium_constructed (GObject *object)
686 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
688 const gchar *font_family = NULL;
690 WebKitWebSettings *webkit_settings;
692 /* Set default settings */
693 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
694 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
695 webkit_settings = webkit_web_settings_new ();
697 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
700 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
702 webkit_web_view_set_settings (WEBKIT_WEB_VIEW (object), webkit_settings);
705 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
706 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
707 priv->data->template_html,
710 g_object_unref (webkit_settings);
711 g_free (basedir_uri);
715 theme_adium_get_property (GObject *object,
720 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
723 case PROP_ADIUM_DATA:
724 g_value_set_boxed (value, priv->data);
727 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
733 theme_adium_set_property (GObject *object,
738 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
741 case PROP_ADIUM_DATA:
742 g_assert (priv->data == NULL);
743 priv->data = g_value_dup_boxed (value);
746 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
752 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
754 GObjectClass *object_class = G_OBJECT_CLASS (klass);
756 object_class->finalize = theme_adium_finalize;
757 object_class->dispose = theme_adium_dispose;
758 object_class->constructed = theme_adium_constructed;
759 object_class->get_property = theme_adium_get_property;
760 object_class->set_property = theme_adium_set_property;
762 g_object_class_install_property (object_class,
764 g_param_spec_boxed ("adium-data",
766 "Data for the adium theme",
767 EMPATHY_TYPE_ADIUM_DATA,
768 G_PARAM_CONSTRUCT_ONLY |
770 G_PARAM_STATIC_STRINGS));
773 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
777 empathy_theme_adium_init (EmpathyThemeAdium *theme)
779 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
780 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
784 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
786 g_signal_connect (theme, "load-finished",
787 G_CALLBACK (theme_adium_load_finished_cb),
789 g_signal_connect (theme, "navigation-requested",
790 G_CALLBACK (theme_adium_navigation_requested_cb),
792 g_signal_connect (theme, "populate-popup",
793 G_CALLBACK (theme_adium_populate_popup_cb),
798 empathy_theme_adium_new (EmpathyAdiumData *data)
800 g_return_val_if_fail (data != NULL, NULL);
802 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
808 empathy_adium_path_is_valid (const gchar *path)
813 /* We ship a default Template.html as fallback if there is any problem
814 * with the one inside the theme. The only other required file is
815 * Content.html for incoming messages (outgoing fallback to use
817 file = g_build_filename (path, "Contents", "Resources", "Incoming",
818 "Content.html", NULL);
819 ret = g_file_test (file, G_FILE_TEST_EXISTS);
826 empathy_adium_info_new (const gchar *path)
830 GHashTable *info = NULL;
832 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
834 file = g_build_filename (path, "Contents", "Info.plist", NULL);
835 value = empathy_plist_parse_from_file (file);
839 info = g_value_dup_boxed (value);
840 tp_g_value_slice_free (value);
847 empathy_adium_data_get_type (void)
849 static GType type_id = 0;
853 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
854 (GBoxedCopyFunc) empathy_adium_data_ref,
855 (GBoxedFreeFunc) empathy_adium_data_unref);
862 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
864 EmpathyAdiumData *data;
866 gchar *template_html = NULL;
868 gchar *footer_html = NULL;
876 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
878 data = g_slice_new0 (EmpathyAdiumData);
880 data->path = g_strdup (path);
881 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
882 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
883 data->info = g_hash_table_ref (info);
885 /* Load html files */
886 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
887 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
890 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
891 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
894 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
895 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
898 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
899 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
902 file = g_build_filename (data->basedir, "Status.html", NULL);
903 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
906 file = g_build_filename (data->basedir, "Footer.html", NULL);
907 g_file_get_contents (file, &footer_html, &footer_len, NULL);
910 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
911 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
912 data->default_incoming_avatar_filename = file;
917 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
918 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
919 data->default_outgoing_avatar_filename = file;
924 css_path = g_build_filename (data->basedir, "main.css", NULL);
926 /* There is 2 formats for Template.html: The old one has 4 parameters,
927 * the new one has 5 parameters. */
928 file = g_build_filename (data->basedir, "Template.html", NULL);
929 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
930 strv = g_strsplit (template_html, "%@", -1);
931 len = g_strv_length (strv);
935 if (len != 5 && len != 6) {
936 /* Either the theme has no template or it don't have the good
937 * number of parameters. Fallback to use our own template. */
938 g_free (template_html);
941 file = empathy_file_lookup ("Template.html", "data");
942 g_file_get_contents (file, &template_html, &template_len, NULL);
944 strv = g_strsplit (template_html, "%@", -1);
945 len = g_strv_length (strv);
948 /* Replace %@ with the needed information in the template html. */
949 string = g_string_sized_new (template_len);
950 g_string_append (string, strv[i++]);
951 g_string_append (string, data->basedir);
952 g_string_append (string, strv[i++]);
954 const gchar *variant;
956 /* We include main.css by default */
957 g_string_append_printf (string, "@import url(\"%s\");", css_path);
958 g_string_append (string, strv[i++]);
959 variant = tp_asv_get_string (data->info, "DefaultVariant");
961 g_string_append (string, "Variants/");
962 g_string_append (string, variant);
963 g_string_append (string, ".css");
966 /* FIXME: We should set main.css OR the variant css */
967 g_string_append (string, css_path);
969 g_string_append (string, strv[i++]);
970 g_string_append (string, ""); /* We don't want header */
971 g_string_append (string, strv[i++]);
972 /* FIXME: We should replace adium %macros% in footer */
974 g_string_append (string, footer_html);
976 g_string_append (string, strv[i++]);
977 data->template_html = g_string_free (string, FALSE);
979 g_free (footer_html);
980 g_free (template_html);
988 empathy_adium_data_new (const gchar *path)
990 EmpathyAdiumData *data;
993 info = empathy_adium_info_new (path);
994 data = empathy_adium_data_new_with_info (path, info);
995 g_hash_table_unref (info);
1001 empathy_adium_data_ref (EmpathyAdiumData *data)
1003 g_return_val_if_fail (data != NULL, NULL);
1011 empathy_adium_data_unref (EmpathyAdiumData *data)
1013 g_return_if_fail (data != NULL);
1016 if (data->ref_count == 0) {
1017 g_free (data->path);
1018 g_free (data->basedir);
1019 g_free (data->template_html);
1020 g_free (data->in_content_html);
1021 g_free (data->in_nextcontent_html);
1022 g_free (data->out_content_html);
1023 g_free (data->out_nextcontent_html);
1024 g_free (data->default_avatar_filename);
1025 g_free (data->default_incoming_avatar_filename);
1026 g_free (data->default_outgoing_avatar_filename);
1027 g_free (data->status_html);
1028 g_hash_table_unref (data->info);
1029 g_slice_free (EmpathyAdiumData, data);
1034 empathy_adium_data_get_info (EmpathyAdiumData *data)
1036 g_return_val_if_fail (data != NULL, NULL);
1042 empathy_adium_data_get_path (EmpathyAdiumData *data)
1044 g_return_val_if_fail (data != NULL, NULL);