1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n.h>
27 #include <webkit/webkitnetworkrequest.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-time.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libmissioncontrol/mc-profile.h>
36 #include "empathy-theme-adium.h"
37 #include "empathy-smiley-manager.h"
38 #include "empathy-conf.h"
39 #include "empathy-ui-utils.h"
40 #include "empathy-plist.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
43 #include <libempathy/empathy-debug.h>
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
47 /* "Join" consecutive messages with timestamps within five minutes */
48 #define MESSAGE_JOIN_PERIOD 5*60
51 EmpathyAdiumData *data;
52 EmpathySmileyManager *smiley_manager;
53 EmpathyContact *last_contact;
54 time_t last_timestamp;
55 gboolean last_is_backlog;
58 } EmpathyThemeAdiumPriv;
60 struct _EmpathyAdiumData {
64 gchar *default_avatar_filename;
65 gchar *default_incoming_avatar_filename;
66 gchar *default_outgoing_avatar_filename;
68 gchar *in_content_html;
70 gchar *in_context_html;
72 gchar *in_nextcontent_html;
73 gsize in_nextcontent_len;
74 gchar *in_nextcontext_html;
75 gsize in_nextcontext_len;
76 gchar *out_content_html;
77 gsize out_content_len;
78 gchar *out_context_html;
79 gsize out_context_len;
80 gchar *out_nextcontent_html;
81 gsize out_nextcontent_len;
82 gchar *out_nextcontext_html;
83 gsize out_nextcontext_len;
89 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
96 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
98 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
99 theme_adium_iface_init));
101 static WebKitNavigationResponse
102 theme_adium_navigation_requested_cb (WebKitWebView *view,
103 WebKitWebFrame *frame,
104 WebKitNetworkRequest *request,
109 uri = webkit_network_request_get_uri (request);
110 empathy_url_show (GTK_WIDGET (view), uri);
112 return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
116 theme_adium_populate_popup_cb (WebKitWebView *view,
122 /* Remove default menu items */
123 gtk_container_foreach (GTK_CONTAINER (menu),
124 (GtkCallback) gtk_widget_destroy, NULL);
126 /* Select all item */
127 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
128 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
129 gtk_widget_show (item);
131 g_signal_connect_swapped (item, "activate",
132 G_CALLBACK (webkit_web_view_select_all),
136 if (webkit_web_view_can_copy_clipboard (view)) {
137 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
138 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
139 gtk_widget_show (item);
141 g_signal_connect_swapped (item, "activate",
142 G_CALLBACK (webkit_web_view_copy_clipboard),
146 /* Clear menu item */
147 item = gtk_separator_menu_item_new ();
148 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
149 gtk_widget_show (item);
151 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
152 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
153 gtk_widget_show (item);
155 g_signal_connect_swapped (item, "activate",
156 G_CALLBACK (empathy_chat_view_clear),
159 /* FIXME: Add open_link and copy_link when those bugs are fixed:
160 * https://bugs.webkit.org/show_bug.cgi?id=16092
161 * https://bugs.webkit.org/show_bug.cgi?id=16562
166 theme_adium_parse_body (EmpathyThemeAdium *theme,
169 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
170 gboolean use_smileys = FALSE;
175 GMatchInfo *match_info;
180 empathy_conf_get_bool (empathy_conf_get (),
181 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
185 /* Replace smileys by a <img/> tag */
186 string = g_string_sized_new (strlen (text));
187 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
188 for (l = smileys; l; l = l->next) {
189 EmpathySmiley *smiley;
193 g_string_append_printf (string,
194 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
195 smiley->str, smiley->path, smiley->str);
199 str = g_markup_escape_text (smiley->str, -1);
200 g_string_append (string, str);
203 empathy_smiley_free (smiley);
205 g_slist_free (smileys);
208 text = ret = g_string_free (string, FALSE);
211 /* Add <a href></a> arround links */
212 uri_regex = empathy_uri_regex_dup_singleton ();
213 match = g_regex_match (uri_regex, text, 0, &match_info);
218 string = g_string_sized_new (strlen (text));
220 g_match_info_fetch_pos (match_info, 0, &s, &e);
223 /* Append the text between last link (or the
224 * start of the message) and this link */
225 g_string_append_len (string, text + last, s - last);
228 /* Append the link inside <a href=""></a> tag */
229 g_string_append (string, "<a href=\"");
230 g_string_append_len (string, text + s, e - s);
231 g_string_append (string, "\">");
232 g_string_append_len (string, text + s, e - s);
233 g_string_append (string, "</a>");
236 } while (g_match_info_next (match_info, NULL));
238 if (e < 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);
246 g_match_info_free (match_info);
247 g_regex_unref (uri_regex);
249 /* Replace \n by <br/> */
252 for (i = 0; text[i] != '\0'; i++) {
253 if (text[i] == '\n') {
255 string = g_string_sized_new (strlen (text));
257 g_string_append_len (string, text + prev, i - prev);
258 g_string_append (string, "<br/>");
263 g_string_append (string, text + prev);
265 text = ret = g_string_free (string, FALSE);
272 escape_and_append_len (GString *string, const gchar *str, gint len)
274 while (*str != '\0' && len != 0) {
278 g_string_append (string, "\\\\");
282 g_string_append (string, "\\\"");
285 /* Remove end of lines */
288 g_string_append_c (string, *str);
297 theme_adium_match (const gchar **str, const gchar *match)
301 len = strlen (match);
302 if (strncmp (*str, match, len) == 0) {
311 theme_adium_append_html (EmpathyThemeAdium *theme,
313 const gchar *html, gsize len,
314 const gchar *message,
315 const gchar *avatar_filename,
317 const gchar *contact_id,
318 const gchar *service_name,
319 const gchar *message_classes,
323 const gchar *cur = NULL;
326 /* Make some search-and-replace in the html code */
327 string = g_string_sized_new (len + strlen (message));
328 g_string_append_printf (string, "%s(\"", func);
329 for (cur = html; *cur != '\0'; cur++) {
330 const gchar *replace = NULL;
331 gchar *dup_replace = NULL;
333 if (theme_adium_match (&cur, "%message%")) {
335 } else if (theme_adium_match (&cur, "%messageClasses%")) {
336 replace = message_classes;
337 } else if (theme_adium_match (&cur, "%userIconPath%")) {
338 replace = avatar_filename;
339 } else if (theme_adium_match (&cur, "%sender%")) {
341 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
342 replace = contact_id;
343 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
344 /* %senderDisplayName% -
345 * "The serverside (remotely set) name of the sender,
346 * such as an MSN display name."
348 * We don't have access to that yet so we use local
351 } else if (theme_adium_match (&cur, "%service%")) {
352 replace = service_name;
353 } else if (theme_adium_match (&cur, "%shortTime%")) {
354 dup_replace = empathy_time_to_string_local (timestamp,
355 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
356 replace = dup_replace;
357 } else if (theme_adium_match (&cur, "%time")) {
358 gchar *format = NULL;
361 /* Time can be in 2 formats:
362 * %time% or %time{strftime format}%
363 * Extract the time format if provided. */
366 end = strstr (cur, "}%");
371 format = g_strndup (cur, end - cur);
377 dup_replace = empathy_time_to_string_local (timestamp,
378 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
379 replace = dup_replace;
382 escape_and_append_len (string, cur, 1);
386 /* Here we have a replacement to make */
387 escape_and_append_len (string, replace, -1);
388 g_free (dup_replace);
390 g_string_append (string, "\")");
392 script = g_string_free (string, FALSE);
393 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
398 theme_adium_append_message (EmpathyChatView *view,
401 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
402 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
403 EmpathyContact *sender;
404 EmpathyAccount *account;
405 McProfile *account_profile;
406 gchar *dup_body = NULL;
409 const gchar *contact_id;
410 EmpathyAvatar *avatar;
411 const gchar *avatar_filename = NULL;
416 const gchar *service_name;
417 GString *message_classes = NULL;
420 if (!priv->page_loaded) {
421 priv->message_queue = g_list_prepend (priv->message_queue,
426 /* Get information */
427 sender = empathy_message_get_sender (msg);
428 account = empathy_contact_get_account (sender);
429 account_profile = empathy_account_get_profile (account);
430 service_name = mc_profile_get_display_name (account_profile);
431 timestamp = empathy_message_get_timestamp (msg);
432 body = empathy_message_get_body (msg);
433 dup_body = theme_adium_parse_body (theme, body);
437 name = empathy_contact_get_name (sender);
438 contact_id = empathy_contact_get_id (sender);
440 /* If this is a /me, append an event */
441 if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
444 str = g_strdup_printf ("%s %s", name, body);
445 empathy_chat_view_append_event (view, str);
451 /* Get the avatar filename, or a fallback */
452 avatar = empathy_contact_get_avatar (sender);
454 avatar_filename = avatar->filename;
456 if (!avatar_filename) {
457 if (empathy_contact_is_user (sender)) {
458 avatar_filename = priv->data->default_outgoing_avatar_filename;
460 avatar_filename = priv->data->default_incoming_avatar_filename;
462 if (!avatar_filename) {
463 if (!priv->data->default_avatar_filename) {
464 priv->data->default_avatar_filename =
465 empathy_filename_from_icon_name ("stock_person",
466 GTK_ICON_SIZE_DIALOG);
468 avatar_filename = priv->data->default_avatar_filename;
472 is_backlog = empathy_message_is_backlog (msg);
474 /* Get the right html/func to add the message */
475 func = "appendMessage";
477 message_classes = g_string_new ("message");
479 /* eventually append the "history" class */
481 g_string_append (message_classes, " history");
484 /* check the sender of the message and append the appropriate class */
485 if (empathy_contact_is_user (sender)) {
486 g_string_append (message_classes, " outgoing");
489 g_string_append (message_classes, " incoming");
493 * To mimick Adium's behavior, we only want to join messages
494 * sent by the same contact within a 5 minute time frame.
496 if (empathy_contact_equal (priv->last_contact, sender) &&
497 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
498 (is_backlog == priv->last_is_backlog)) {
499 /* the messages can be appended */
500 func = "appendNextMessage";
501 g_string_append (message_classes, " consecutive");
503 /* check who is the sender of the message to use the correct html file */
504 if (empathy_contact_is_user (sender)) {
505 /* check if this is a backlog message and use NextContext.html */
507 html = priv->data->out_nextcontext_html;
508 len = priv->data->out_nextcontext_len;
512 * html is null if this is not a backlog message or
513 * if we have to fallback (NextContext.html missing).
514 * use NextContent.html
517 html = priv->data->out_nextcontent_html;
518 len = priv->data->out_nextcontent_len;
523 html = priv->data->in_nextcontext_html;
524 len = priv->data->in_nextcontext_len;
528 html = priv->data->in_nextcontent_html;
529 len = priv->data->in_nextcontent_len;
535 * we have html == NULL here if:
536 * 1. the message didn't have to be appended because
537 * the sender was different or the timestamp was too far
538 * 2. NextContent.html file does not exist, so we must
539 * not forget to fallback to the correct Content.html
542 if (empathy_contact_is_user (sender)) {
544 html = priv->data->out_context_html;
545 len = priv->data->out_context_len;
549 html = priv->data->out_content_html;
550 len = priv->data->out_content_len;
555 html = priv->data->in_context_html;
556 len = priv->data->in_context_len;
560 html = priv->data->in_content_html;
561 len = priv->data->in_content_len;
566 theme_adium_append_html (theme, func, html, len, body, avatar_filename,
567 name, contact_id, service_name, message_classes->str,
570 /* Keep the sender of the last displayed message */
571 if (priv->last_contact) {
572 g_object_unref (priv->last_contact);
574 priv->last_contact = g_object_ref (sender);
575 priv->last_timestamp = timestamp;
576 priv->last_is_backlog = is_backlog;
579 g_string_free (message_classes, TRUE);
583 theme_adium_append_event (EmpathyChatView *view,
586 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
587 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
589 if (priv->data->status_html) {
590 theme_adium_append_html (theme, "appendMessage",
591 priv->data->status_html,
592 priv->data->status_len,
593 str, NULL, NULL, NULL, NULL, "event",
594 empathy_time_get_current ());
597 /* There is no last contact */
598 if (priv->last_contact) {
599 g_object_unref (priv->last_contact);
600 priv->last_contact = NULL;
605 theme_adium_scroll (EmpathyChatView *view,
606 gboolean allow_scrolling)
608 /* FIXME: Is it possible? I guess we need a js function, but I don't
613 theme_adium_scroll_down (EmpathyChatView *view)
615 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
619 theme_adium_get_has_selection (EmpathyChatView *view)
621 return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
625 theme_adium_clear (EmpathyChatView *view)
627 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
630 priv->page_loaded = FALSE;
631 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
632 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
633 priv->data->template_html,
635 g_free (basedir_uri);
637 /* Clear last contact to avoid trying to add a 'joined'
638 * message when we don't have an insertion point. */
639 if (priv->last_contact) {
640 g_object_unref (priv->last_contact);
641 priv->last_contact = NULL;
646 theme_adium_find_previous (EmpathyChatView *view,
647 const gchar *search_criteria,
650 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
651 search_criteria, FALSE,
656 theme_adium_find_next (EmpathyChatView *view,
657 const gchar *search_criteria,
660 return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
661 search_criteria, FALSE,
666 theme_adium_find_abilities (EmpathyChatView *view,
667 const gchar *search_criteria,
668 gboolean *can_do_previous,
669 gboolean *can_do_next)
671 /* FIXME: Does webkit provide an API for that? We have wrap=true in
672 * find_next and find_previous to work around this problem. */
674 *can_do_previous = TRUE;
680 theme_adium_highlight (EmpathyChatView *view,
683 webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
684 webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
686 webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
691 theme_adium_copy_clipboard (EmpathyChatView *view)
693 webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
697 theme_adium_iface_init (EmpathyChatViewIface *iface)
699 iface->append_message = theme_adium_append_message;
700 iface->append_event = theme_adium_append_event;
701 iface->scroll = theme_adium_scroll;
702 iface->scroll_down = theme_adium_scroll_down;
703 iface->get_has_selection = theme_adium_get_has_selection;
704 iface->clear = theme_adium_clear;
705 iface->find_previous = theme_adium_find_previous;
706 iface->find_next = theme_adium_find_next;
707 iface->find_abilities = theme_adium_find_abilities;
708 iface->highlight = theme_adium_highlight;
709 iface->copy_clipboard = theme_adium_copy_clipboard;
713 theme_adium_load_finished_cb (WebKitWebView *view,
714 WebKitWebFrame *frame,
717 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
718 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
720 DEBUG ("Page loaded");
721 priv->page_loaded = TRUE;
723 /* Display queued messages */
724 priv->message_queue = g_list_reverse (priv->message_queue);
725 while (priv->message_queue) {
726 EmpathyMessage *message = priv->message_queue->data;
728 theme_adium_append_message (chat_view, message);
729 priv->message_queue = g_list_remove (priv->message_queue, message);
730 g_object_unref (message);
735 theme_adium_finalize (GObject *object)
737 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
739 empathy_adium_data_unref (priv->data);
741 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
745 theme_adium_dispose (GObject *object)
747 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
749 if (priv->smiley_manager) {
750 g_object_unref (priv->smiley_manager);
751 priv->smiley_manager = NULL;
754 if (priv->last_contact) {
755 g_object_unref (priv->last_contact);
756 priv->last_contact = NULL;
759 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
763 theme_adium_constructed (GObject *object)
765 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
767 const gchar *font_family = NULL;
769 WebKitWebSettings *webkit_settings;
771 /* Set default settings */
772 font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
773 font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
774 webkit_settings = webkit_web_settings_new ();
776 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
779 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
781 webkit_web_view_set_settings (WEBKIT_WEB_VIEW (object), webkit_settings);
784 basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
785 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
786 priv->data->template_html,
789 g_object_unref (webkit_settings);
790 g_free (basedir_uri);
794 theme_adium_get_property (GObject *object,
799 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
802 case PROP_ADIUM_DATA:
803 g_value_set_boxed (value, priv->data);
806 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
812 theme_adium_set_property (GObject *object,
817 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
820 case PROP_ADIUM_DATA:
821 g_assert (priv->data == NULL);
822 priv->data = g_value_dup_boxed (value);
825 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
831 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
833 GObjectClass *object_class = G_OBJECT_CLASS (klass);
835 object_class->finalize = theme_adium_finalize;
836 object_class->dispose = theme_adium_dispose;
837 object_class->constructed = theme_adium_constructed;
838 object_class->get_property = theme_adium_get_property;
839 object_class->set_property = theme_adium_set_property;
841 g_object_class_install_property (object_class,
843 g_param_spec_boxed ("adium-data",
845 "Data for the adium theme",
846 EMPATHY_TYPE_ADIUM_DATA,
847 G_PARAM_CONSTRUCT_ONLY |
849 G_PARAM_STATIC_STRINGS));
852 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
856 empathy_theme_adium_init (EmpathyThemeAdium *theme)
858 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
859 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
863 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
865 g_signal_connect (theme, "load-finished",
866 G_CALLBACK (theme_adium_load_finished_cb),
868 g_signal_connect (theme, "navigation-requested",
869 G_CALLBACK (theme_adium_navigation_requested_cb),
871 g_signal_connect (theme, "populate-popup",
872 G_CALLBACK (theme_adium_populate_popup_cb),
877 empathy_theme_adium_new (EmpathyAdiumData *data)
879 g_return_val_if_fail (data != NULL, NULL);
881 return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
887 empathy_adium_path_is_valid (const gchar *path)
892 /* The theme is not valid if there is no Info.plist */
893 file = g_build_filename (path, "Contents", "Info.plist",
895 ret = g_file_test (file, G_FILE_TEST_EXISTS);
901 /* We ship a default Template.html as fallback if there is any problem
902 * with the one inside the theme. The only other required file is
903 * Content.html for incoming messages (outgoing fallback to use
905 file = g_build_filename (path, "Contents", "Resources", "Incoming",
906 "Content.html", NULL);
907 ret = g_file_test (file, G_FILE_TEST_EXISTS);
914 empathy_adium_info_new (const gchar *path)
918 GHashTable *info = NULL;
920 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
922 file = g_build_filename (path, "Contents", "Info.plist", NULL);
923 value = empathy_plist_parse_from_file (file);
929 info = g_value_dup_boxed (value);
930 tp_g_value_slice_free (value);
932 /* Insert the theme's path into the hash table,
933 * keys have to be dupped */
934 tp_asv_set_string (info, g_strdup ("path"), path);
940 empathy_adium_data_get_type (void)
942 static GType type_id = 0;
946 type_id = g_boxed_type_register_static ("EmpathyAdiumData",
947 (GBoxedCopyFunc) empathy_adium_data_ref,
948 (GBoxedFreeFunc) empathy_adium_data_unref);
955 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
957 EmpathyAdiumData *data;
959 gchar *template_html = NULL;
961 gchar *footer_html = NULL;
969 g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
971 data = g_slice_new0 (EmpathyAdiumData);
973 data->path = g_strdup (path);
974 data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
975 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
976 data->info = g_hash_table_ref (info);
978 /* Load html files */
979 file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
980 g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
983 file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
984 g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
987 file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
988 g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
991 file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
992 g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
995 file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
996 g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
999 file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1000 g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1003 file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1004 g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1007 file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1008 g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1011 file = g_build_filename (data->basedir, "Status.html", NULL);
1012 g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1015 file = g_build_filename (data->basedir, "Footer.html", NULL);
1016 g_file_get_contents (file, &footer_html, &footer_len, NULL);
1019 file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1020 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1021 data->default_incoming_avatar_filename = file;
1026 file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1027 if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1028 data->default_outgoing_avatar_filename = file;
1033 css_path = g_build_filename (data->basedir, "main.css", NULL);
1035 /* There is 2 formats for Template.html: The old one has 4 parameters,
1036 * the new one has 5 parameters. */
1037 file = g_build_filename (data->basedir, "Template.html", NULL);
1038 if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1039 strv = g_strsplit (template_html, "%@", -1);
1040 len = g_strv_length (strv);
1044 if (len != 5 && len != 6) {
1045 /* Either the theme has no template or it don't have the good
1046 * number of parameters. Fallback to use our own template. */
1047 g_free (template_html);
1050 file = empathy_file_lookup ("Template.html", "data");
1051 g_file_get_contents (file, &template_html, &template_len, NULL);
1053 strv = g_strsplit (template_html, "%@", -1);
1054 len = g_strv_length (strv);
1057 /* Replace %@ with the needed information in the template html. */
1058 string = g_string_sized_new (template_len);
1059 g_string_append (string, strv[i++]);
1060 g_string_append (string, data->basedir);
1061 g_string_append (string, strv[i++]);
1063 const gchar *variant;
1065 /* We include main.css by default */
1066 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1067 g_string_append (string, strv[i++]);
1068 variant = tp_asv_get_string (data->info, "DefaultVariant");
1070 g_string_append (string, "Variants/");
1071 g_string_append (string, variant);
1072 g_string_append (string, ".css");
1075 /* FIXME: We should set main.css OR the variant css */
1076 g_string_append (string, css_path);
1078 g_string_append (string, strv[i++]);
1079 g_string_append (string, ""); /* We don't want header */
1080 g_string_append (string, strv[i++]);
1081 /* FIXME: We should replace adium %macros% in footer */
1083 g_string_append (string, footer_html);
1085 g_string_append (string, strv[i++]);
1086 data->template_html = g_string_free (string, FALSE);
1088 g_free (footer_html);
1089 g_free (template_html);
1097 empathy_adium_data_new (const gchar *path)
1099 EmpathyAdiumData *data;
1102 info = empathy_adium_info_new (path);
1103 data = empathy_adium_data_new_with_info (path, info);
1104 g_hash_table_unref (info);
1110 empathy_adium_data_ref (EmpathyAdiumData *data)
1112 g_return_val_if_fail (data != NULL, NULL);
1114 g_atomic_int_inc (&data->ref_count);
1120 empathy_adium_data_unref (EmpathyAdiumData *data)
1122 g_return_if_fail (data != NULL);
1124 if (g_atomic_int_dec_and_test (&data->ref_count)) {
1125 g_free (data->path);
1126 g_free (data->basedir);
1127 g_free (data->template_html);
1128 g_free (data->in_content_html);
1129 g_free (data->in_nextcontent_html);
1130 g_free (data->in_context_html);
1131 g_free (data->in_nextcontext_html);
1132 g_free (data->out_content_html);
1133 g_free (data->out_nextcontent_html);
1134 g_free (data->out_context_html);
1135 g_free (data->out_nextcontext_html);
1136 g_free (data->default_avatar_filename);
1137 g_free (data->default_incoming_avatar_filename);
1138 g_free (data->default_outgoing_avatar_filename);
1139 g_free (data->status_html);
1140 g_hash_table_unref (data->info);
1141 g_slice_free (EmpathyAdiumData, data);
1146 empathy_adium_data_get_info (EmpathyAdiumData *data)
1148 g_return_val_if_fail (data != NULL, NULL);
1154 empathy_adium_data_get_path (EmpathyAdiumData *data)
1156 g_return_val_if_fail (data != NULL, NULL);