]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-adium.c
Merge remote branch 'vminko/fix-633481-v2'
[empathy.git] / libempathy-gtk / empathy-theme-adium.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2008-2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #include <webkit/webkit.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
30
31 #include <gconf/gconf-client.h>
32 #include <pango/pango.h>
33 #include <gdk/gdk.h>
34
35 #include <libempathy/empathy-gsettings.h>
36 #include <libempathy/empathy-time.h>
37 #include <libempathy/empathy-utils.h>
38
39 #include "empathy-theme-adium.h"
40 #include "empathy-smiley-manager.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-plist.h"
43 #include "empathy-string-parser.h"
44 #include "empathy-images.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
47 #include <libempathy/empathy-debug.h>
48
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
50
51 /* GConf key containing current value of font */
52 #define EMPATHY_GCONF_FONT_KEY_NAME "/desktop/gnome/interface/document_font_name"
53 #define BORING_DPI_DEFAULT 96
54
55 /* "Join" consecutive messages with timestamps within five minutes */
56 #define MESSAGE_JOIN_PERIOD 5*60
57
58 typedef struct {
59         EmpathyAdiumData     *data;
60         EmpathySmileyManager *smiley_manager;
61         EmpathyContact       *last_contact;
62         time_t                last_timestamp;
63         gboolean              last_is_backlog;
64         gboolean              page_loaded;
65         GList                *message_queue;
66         GtkWidget            *inspector_window;
67         GSettings            *gsettings_chat;
68 } EmpathyThemeAdiumPriv;
69
70 struct _EmpathyAdiumData {
71         gint  ref_count;
72         gchar *path;
73         gchar *basedir;
74         gchar *default_avatar_filename;
75         gchar *default_incoming_avatar_filename;
76         gchar *default_outgoing_avatar_filename;
77         gchar *template_html;
78         gchar *in_content_html;
79         gsize  in_content_len;
80         gchar *in_context_html;
81         gsize  in_context_len;
82         gchar *in_nextcontent_html;
83         gsize  in_nextcontent_len;
84         gchar *in_nextcontext_html;
85         gsize  in_nextcontext_len;
86         gchar *out_content_html;
87         gsize  out_content_len;
88         gchar *out_context_html;
89         gsize  out_context_len;
90         gchar *out_nextcontent_html;
91         gsize  out_nextcontent_len;
92         gchar *out_nextcontext_html;
93         gsize  out_nextcontext_len;
94         gchar *status_html;
95         gsize  status_len;
96         GHashTable *info;
97 };
98
99 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
100
101 enum {
102         PROP_0,
103         PROP_ADIUM_DATA,
104 };
105
106 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
107                          WEBKIT_TYPE_WEB_VIEW,
108                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
109                                                 theme_adium_iface_init));
110
111 static void
112 theme_adium_update_enable_webkit_developer_tools (EmpathyThemeAdium *theme)
113 {
114         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
115         WebKitWebView  *web_view = WEBKIT_WEB_VIEW (theme);
116         gboolean        enable_webkit_developer_tools;
117
118         enable_webkit_developer_tools = g_settings_get_boolean (
119                         priv->gsettings_chat,
120                         EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS);
121
122         g_object_set (G_OBJECT (webkit_web_view_get_settings (web_view)),
123                       "enable-developer-extras",
124                       enable_webkit_developer_tools,
125                       NULL);
126 }
127
128 static void
129 theme_adium_notify_enable_webkit_developer_tools_cb (GSettings   *gsettings,
130                                                      const gchar *key,
131                                                      gpointer     user_data)
132 {
133         EmpathyThemeAdium  *theme = user_data;
134
135         theme_adium_update_enable_webkit_developer_tools (theme);
136 }
137
138 static gboolean
139 theme_adium_navigation_policy_decision_requested_cb (WebKitWebView             *view,
140                                                      WebKitWebFrame            *web_frame,
141                                                      WebKitNetworkRequest      *request,
142                                                      WebKitWebNavigationAction *action,
143                                                      WebKitWebPolicyDecision   *decision,
144                                                      gpointer                   data)
145 {
146         const gchar *uri;
147
148         /* Only call url_show on clicks */
149         if (webkit_web_navigation_action_get_reason (action) !=
150             WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
151                 webkit_web_policy_decision_use (decision);
152                 return TRUE;
153         }
154
155         uri = webkit_network_request_get_uri (request);
156         empathy_url_show (GTK_WIDGET (view), uri);
157
158         webkit_web_policy_decision_ignore (decision);
159         return TRUE;
160 }
161
162 static void
163 theme_adium_copy_address_cb (GtkMenuItem *menuitem,
164                              gpointer     user_data)
165 {
166         WebKitHitTestResult   *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
167         gchar                 *uri;
168         GtkClipboard          *clipboard;
169
170         g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
171
172         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
173         gtk_clipboard_set_text (clipboard, uri, -1);
174
175         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
176         gtk_clipboard_set_text (clipboard, uri, -1);
177
178         g_free (uri);
179 }
180
181 static void
182 theme_adium_open_address_cb (GtkMenuItem *menuitem,
183                              gpointer     user_data)
184 {
185         WebKitHitTestResult   *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
186         gchar                 *uri;
187
188         g_object_get (G_OBJECT (hit_test_result), "link-uri", &uri, NULL);
189
190         empathy_url_show (GTK_WIDGET (menuitem), uri);
191
192         g_free (uri);
193 }
194
195 static void
196 theme_adium_match_newline (const gchar *text,
197                            gssize len,
198                            EmpathyStringReplace replace_func,
199                            EmpathyStringParser *sub_parsers,
200                            gpointer user_data)
201 {
202         GString *string = user_data;
203         gint i;
204         gint prev = 0;
205
206         if (len < 0) {
207                 len = G_MAXSSIZE;
208         }
209
210         /* Replace \n by <br/> */
211         for (i = 0; i < len && text[i] != '\0'; i++) {
212                 if (text[i] == '\n') {
213                         empathy_string_parser_substr (text + prev,
214                                                       i - prev, sub_parsers,
215                                                       user_data);
216                         g_string_append (string, "<br/>");
217                         prev = i + 1;
218                 }
219         }
220         empathy_string_parser_substr (text + prev, i - prev,
221                                       sub_parsers, user_data);
222 }
223
224 static void
225 theme_adium_replace_smiley (const gchar *text,
226                             gssize len,
227                             gpointer match_data,
228                             gpointer user_data)
229 {
230         EmpathySmileyHit *hit = match_data;
231         GString *string = user_data;
232
233         /* Replace smiley by a <img/> tag */
234         g_string_append_printf (string,
235                                 "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
236                                 hit->path, (int)len, text, (int)len, text);
237 }
238
239 static EmpathyStringParser string_parsers[] = {
240         {empathy_string_match_link, empathy_string_replace_link},
241         {theme_adium_match_newline, NULL},
242         {empathy_string_match_all, empathy_string_replace_escaped},
243         {NULL, NULL}
244 };
245
246 static EmpathyStringParser string_parsers_with_smiley[] = {
247         {empathy_string_match_link, empathy_string_replace_link},
248         {empathy_string_match_smiley, theme_adium_replace_smiley},
249         {theme_adium_match_newline, NULL},
250         {empathy_string_match_all, empathy_string_replace_escaped},
251         {NULL, NULL}
252 };
253
254 static gchar *
255 theme_adium_parse_body (const gchar *text)
256 {
257         EmpathyStringParser *parsers;
258         GString *string;
259         GSettings *gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
260
261         /* Check if we have to parse smileys */
262         if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_CHAT_SHOW_SMILEYS))
263                 parsers = string_parsers_with_smiley;
264         else
265                 parsers = string_parsers;
266
267         /* Parse text and construct string with links and smileys replaced
268          * by html tags. Also escape text to make sure html code is
269          * displayed verbatim. */
270         string = g_string_sized_new (strlen (text));
271         empathy_string_parser_substr (text, -1, parsers, string);
272
273         g_object_unref (gsettings);
274
275         /* Wrap body in order to make tabs and multiple spaces displayed
276          * properly. See bug #625745. */
277         g_string_prepend (string, "<div style=\"display: inline; "
278                                                "white-space: pre-wrap\"'>");
279         g_string_append (string, "</div>");
280
281         return g_string_free (string, FALSE);
282 }
283
284 static void
285 escape_and_append_len (GString *string, const gchar *str, gint len)
286 {
287         while (*str != '\0' && len != 0) {
288                 switch (*str) {
289                 case '\\':
290                         /* \ becomes \\ */
291                         g_string_append (string, "\\\\");
292                         break;
293                 case '\"':
294                         /* " becomes \" */
295                         g_string_append (string, "\\\"");
296                         break;
297                 case '\n':
298                         /* Remove end of lines */
299                         break;
300                 default:
301                         g_string_append_c (string, *str);
302                 }
303
304                 str++;
305                 len--;
306         }
307 }
308
309 static gboolean
310 theme_adium_match (const gchar **str, const gchar *match)
311 {
312         gint len;
313
314         len = strlen (match);
315         if (strncmp (*str, match, len) == 0) {
316                 *str += len - 1;
317                 return TRUE;
318         }
319
320         return FALSE;
321 }
322
323 static void
324 theme_adium_append_html (EmpathyThemeAdium *theme,
325                          const gchar       *func,
326                          const gchar       *html, gsize len,
327                          const gchar       *message,
328                          const gchar       *avatar_filename,
329                          const gchar       *name,
330                          const gchar       *contact_id,
331                          const gchar       *service_name,
332                          const gchar       *message_classes,
333                          time_t             timestamp,
334                          gboolean           is_backlog)
335 {
336         GString     *string;
337         const gchar *cur = NULL;
338         gchar       *script;
339
340         /* Make some search-and-replace in the html code */
341         string = g_string_sized_new (len + strlen (message));
342         g_string_append_printf (string, "%s(\"", func);
343         for (cur = html; *cur != '\0'; cur++) {
344                 const gchar *replace = NULL;
345                 gchar       *dup_replace = NULL;
346
347                 if (theme_adium_match (&cur, "%message%")) {
348                         replace = message;
349                 } else if (theme_adium_match (&cur, "%messageClasses%")) {
350                         replace = message_classes;
351                 } else if (theme_adium_match (&cur, "%userIconPath%")) {
352                         replace = avatar_filename;
353                 } else if (theme_adium_match (&cur, "%sender%")) {
354                         replace = name;
355                 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
356                         replace = contact_id;
357                 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
358                         /* %senderDisplayName% -
359                          * "The serverside (remotely set) name of the sender,
360                          *  such as an MSN display name."
361                          *
362                          * We don't have access to that yet so we use local
363                          * alias instead.*/
364                         replace = name;
365                 } else if (theme_adium_match (&cur, "%service%")) {
366                         replace = service_name;
367                 } else if (theme_adium_match (&cur, "%shortTime%")) {
368                         dup_replace = empathy_time_to_string_local (timestamp,
369                                 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
370                         replace = dup_replace;
371                 } else if (theme_adium_match (&cur, "%time")) {
372                         gchar *format = NULL;
373                         gchar *end;
374                         /* Time can be in 2 formats:
375                          * %time% or %time{strftime format}%
376                          * Extract the time format if provided. */
377                         if (cur[1] == '{') {
378                                 cur += 2;
379                                 end = strstr (cur, "}%");
380                                 if (!end) {
381                                         /* Invalid string */
382                                         continue;
383                                 }
384                                 format = g_strndup (cur, end - cur);
385                                 cur = end + 1;
386                         } else {
387                                 cur++;
388                         }
389
390                         if (is_backlog) {
391                                 dup_replace = empathy_time_to_string_local (timestamp,
392                                         format ? format : EMPATHY_TIME_DATE_FORMAT_DISPLAY_SHORT);
393                         } else {
394                                 dup_replace = empathy_time_to_string_local (timestamp,
395                                         format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
396                         }
397                         replace = dup_replace;
398                         g_free (format);
399                 } else {
400                         escape_and_append_len (string, cur, 1);
401                         continue;
402                 }
403
404                 /* Here we have a replacement to make */
405                 escape_and_append_len (string, replace, -1);
406                 g_free (dup_replace);
407         }
408         g_string_append (string, "\")");
409
410         script = g_string_free (string, FALSE);
411         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
412         g_free (script);
413 }
414
415 static void
416 theme_adium_append_event_escaped (EmpathyChatView *view,
417                                   const gchar     *escaped)
418 {
419         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
420         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
421
422         if (priv->data->status_html) {
423                 theme_adium_append_html (theme, "appendMessage",
424                                          priv->data->status_html,
425                                          priv->data->status_len,
426                                          escaped, NULL, NULL, NULL, NULL,
427                                          "event", empathy_time_get_current (), FALSE);
428         }
429
430         /* There is no last contact */
431         if (priv->last_contact) {
432                 g_object_unref (priv->last_contact);
433                 priv->last_contact = NULL;
434         }
435 }
436
437 static void
438 theme_adium_append_message (EmpathyChatView *view,
439                             EmpathyMessage  *msg)
440 {
441         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
442         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
443         EmpathyContact        *sender;
444         TpAccount             *account;
445         gchar                 *body_escaped;
446         const gchar           *body;
447         const gchar           *name;
448         const gchar           *contact_id;
449         EmpathyAvatar         *avatar;
450         const gchar           *avatar_filename = NULL;
451         time_t                 timestamp;
452         gchar                 *html = NULL;
453         gsize                  len = 0;
454         const gchar           *func;
455         const gchar           *service_name;
456         GString               *message_classes = NULL;
457         gboolean               is_backlog;
458         gboolean               consecutive;
459
460         if (!priv->page_loaded) {
461                 priv->message_queue = g_list_prepend (priv->message_queue,
462                                                       g_object_ref (msg));
463                 return;
464         }
465
466         /* Get information */
467         sender = empathy_message_get_sender (msg);
468         account = empathy_contact_get_account (sender);
469         service_name = empathy_protocol_name_to_display_name
470                 (tp_account_get_protocol (account));
471         if (service_name == NULL)
472                 service_name = tp_account_get_protocol (account);
473         timestamp = empathy_message_get_timestamp (msg);
474         body = empathy_message_get_body (msg);
475         body_escaped = theme_adium_parse_body (body);
476         name = empathy_contact_get_alias (sender);
477         contact_id = empathy_contact_get_id (sender);
478
479         /* If this is a /me, append an event */
480         if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
481                 gchar *str;
482
483                 str = g_strdup_printf ("%s %s", name, body_escaped);
484                 theme_adium_append_event_escaped (view, str);
485
486                 g_free (str);
487                 g_free (body_escaped);
488                 return;
489         }
490
491         /* Get the avatar filename, or a fallback */
492         avatar = empathy_contact_get_avatar (sender);
493         if (avatar) {
494                 avatar_filename = avatar->filename;
495         }
496         if (!avatar_filename) {
497                 if (empathy_contact_is_user (sender)) {
498                         avatar_filename = priv->data->default_outgoing_avatar_filename;
499                 } else {
500                         avatar_filename = priv->data->default_incoming_avatar_filename;
501                 }
502                 if (!avatar_filename) {
503                         if (!priv->data->default_avatar_filename) {
504                                 priv->data->default_avatar_filename =
505                                         empathy_filename_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
506                                                                          GTK_ICON_SIZE_DIALOG);
507                         }
508                         avatar_filename = priv->data->default_avatar_filename;
509                 }
510         }
511
512         /* We want to join this message with the last one if
513          * - senders are the same contact,
514          * - last message was recieved recently,
515          * - last message and this message both are/aren't backlog, and
516          * - DisableCombineConsecutive is not set in theme's settings */
517         is_backlog = empathy_message_is_backlog (msg);
518         consecutive = empathy_contact_equal (priv->last_contact, sender) &&
519                 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
520                 (is_backlog == priv->last_is_backlog) &&
521                 !tp_asv_get_boolean (priv->data->info,
522                                      "DisableCombineConsecutive", NULL);
523
524         /* Define message classes */
525         message_classes = g_string_new ("message");
526         if (is_backlog) {
527                 g_string_append (message_classes, " history");
528         }
529         if (consecutive) {
530                 g_string_append (message_classes, " consecutive");
531         }
532         if (empathy_contact_is_user (sender)) {
533                 g_string_append (message_classes, " outgoing");
534         } else {
535                 g_string_append (message_classes, " incoming");
536         }
537
538         /* Define javascript function to use */
539         if (consecutive) {
540                 func = "appendNextMessage";
541         } else {
542                 func = "appendMessage";
543         }
544
545         /* Outgoing */
546         if (empathy_contact_is_user (sender)) {
547                 if (consecutive) {
548                         if (is_backlog) {
549                                 html = priv->data->out_nextcontext_html;
550                                 len = priv->data->out_nextcontext_len;
551                         }
552
553                         /* Not backlog, or fallback if NextContext.html
554                          * is missing */
555                         if (html == NULL) {
556                                 html = priv->data->out_nextcontent_html;
557                                 len = priv->data->out_nextcontent_len;
558                         }
559                 }
560
561                 /* Not consecutive, or fallback if NextContext.html and/or
562                  * NextContent.html are missing */
563                 if (html == NULL) {
564                         if (is_backlog) {
565                                 html = priv->data->out_context_html;
566                                 len = priv->data->out_context_len;
567                         }
568
569                         if (html == NULL) {
570                                 html = priv->data->out_content_html;
571                                 len = priv->data->out_content_len;
572                         }
573                 }
574         }
575
576         /* Incoming, or fallback if outgoing files are missing */
577         if (html == NULL) {
578                 if (consecutive) {
579                         if (is_backlog) {
580                                 html = priv->data->in_nextcontext_html;
581                                 len = priv->data->in_nextcontext_len;
582                         }
583
584                         /* Note backlog, or fallback if NextContext.html
585                          * is missing */
586                         if (html == NULL) {
587                                 html = priv->data->in_nextcontent_html;
588                                 len = priv->data->in_nextcontent_len;
589                         }
590                 }
591
592                 /* Not consecutive, or fallback if NextContext.html and/or
593                  * NextContent.html are missing */
594                 if (html == NULL) {
595                         if (is_backlog) {
596                                 html = priv->data->in_context_html;
597                                 len = priv->data->in_context_len;
598                         }
599
600                         if (html == NULL) {
601                                 html = priv->data->in_content_html;
602                                 len = priv->data->in_content_len;
603                         }
604                 }
605         }
606
607         if (html != NULL) {
608                 theme_adium_append_html (theme, func, html, len, body_escaped,
609                                          avatar_filename, name, contact_id,
610                                          service_name, message_classes->str,
611                                          timestamp, is_backlog);
612         } else {
613                 DEBUG ("Couldn't find HTML file for this message");
614         }
615
616         /* Keep the sender of the last displayed message */
617         if (priv->last_contact) {
618                 g_object_unref (priv->last_contact);
619         }
620         priv->last_contact = g_object_ref (sender);
621         priv->last_timestamp = timestamp;
622         priv->last_is_backlog = is_backlog;
623
624         g_free (body_escaped);
625         g_string_free (message_classes, TRUE);
626 }
627
628 static void
629 theme_adium_append_event (EmpathyChatView *view,
630                           const gchar     *str)
631 {
632         gchar *str_escaped;
633
634         str_escaped = g_markup_escape_text (str, -1);
635         theme_adium_append_event_escaped (view, str_escaped);
636         g_free (str_escaped);
637 }
638
639 static void
640 theme_adium_scroll (EmpathyChatView *view,
641                     gboolean         allow_scrolling)
642 {
643         /* FIXME: Is it possible? I guess we need a js function, but I don't
644          * see any... */
645 }
646
647 static void
648 theme_adium_scroll_down (EmpathyChatView *view)
649 {
650         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
651 }
652
653 static gboolean
654 theme_adium_get_has_selection (EmpathyChatView *view)
655 {
656         return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
657 }
658
659 static void
660 theme_adium_clear (EmpathyChatView *view)
661 {
662         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
663         gchar *basedir_uri;
664
665         priv->page_loaded = FALSE;
666         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
667         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
668                                           priv->data->template_html,
669                                           basedir_uri);
670         g_free (basedir_uri);
671
672         /* Clear last contact to avoid trying to add a 'joined'
673          * message when we don't have an insertion point. */
674         if (priv->last_contact) {
675                 g_object_unref (priv->last_contact);
676                 priv->last_contact = NULL;
677         }
678 }
679
680 static gboolean
681 theme_adium_find_previous (EmpathyChatView *view,
682                            const gchar     *search_criteria,
683                            gboolean         new_search,
684                            gboolean         match_case)
685 {
686         /* FIXME: Doesn't respect new_search */
687         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
688                                             search_criteria, match_case,
689                                             FALSE, TRUE);
690 }
691
692 static gboolean
693 theme_adium_find_next (EmpathyChatView *view,
694                        const gchar     *search_criteria,
695                        gboolean         new_search,
696                        gboolean         match_case)
697 {
698         /* FIXME: Doesn't respect new_search */
699         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
700                                             search_criteria, match_case,
701                                             TRUE, TRUE);
702 }
703
704 static void
705 theme_adium_find_abilities (EmpathyChatView *view,
706                             const gchar    *search_criteria,
707                             gboolean        match_case,
708                             gboolean       *can_do_previous,
709                             gboolean       *can_do_next)
710 {
711         /* FIXME: Does webkit provide an API for that? We have wrap=true in
712          * find_next and find_previous to work around this problem. */
713         if (can_do_previous)
714                 *can_do_previous = TRUE;
715         if (can_do_next)
716                 *can_do_next = TRUE;
717 }
718
719 static void
720 theme_adium_highlight (EmpathyChatView *view,
721                        const gchar     *text,
722                        gboolean         match_case)
723 {
724         webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
725         webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
726                                            text, match_case, 0);
727         webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
728                                                     TRUE);
729 }
730
731 static void
732 theme_adium_copy_clipboard (EmpathyChatView *view)
733 {
734         webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
735 }
736
737 static void
738 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
739 {
740         WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
741
742         g_object_unref (hit_test_result);
743 }
744
745 static void
746 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
747 {
748         WebKitWebView              *view = WEBKIT_WEB_VIEW (theme);
749         WebKitHitTestResult        *hit_test_result;
750         WebKitHitTestResultContext  context;
751         GtkWidget                  *menu;
752         GtkWidget                  *item;
753
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);
756
757         /* The menu */
758         menu = gtk_menu_new ();
759
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);
763
764         g_signal_connect_swapped (item, "activate",
765                                   G_CALLBACK (webkit_web_view_select_all),
766                                   view);
767
768         /* Copy menu item */
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);
772
773                 g_signal_connect_swapped (item, "activate",
774                                           G_CALLBACK (webkit_web_view_copy_clipboard),
775                                           view);
776         }
777
778         /* Clear menu item */
779         item = gtk_separator_menu_item_new ();
780         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
781
782         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
783         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
784
785         g_signal_connect_swapped (item, "activate",
786                                   G_CALLBACK (empathy_chat_view_clear),
787                                   view);
788
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) {
792                 /* Separator */
793                 item = gtk_separator_menu_item_new ();
794                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
795
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),
800                                   hit_test_result);
801                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
802
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),
807                                   hit_test_result);
808                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
809         }
810
811         g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
812                           G_CALLBACK (theme_adium_context_menu_selection_done_cb),
813                           hit_test_result);
814
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);
819         g_object_ref_sink (menu);
820         g_object_unref (menu);
821 }
822
823 static gboolean
824 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
825 {
826         if (event->button == 3) {
827                 gboolean developer_tools_enabled;
828
829                 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
830                               "enable-developer-extras", &developer_tools_enabled, NULL);
831
832                 /* We currently have no way to add an inspector menu
833                  * item ourselves, so we disable our customized menu
834                  * if the developer extras are enabled. */
835                 if (!developer_tools_enabled) {
836                         theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
837                         return TRUE;
838                 }
839         }
840
841         return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
842 }
843
844 static void
845 theme_adium_iface_init (EmpathyChatViewIface *iface)
846 {
847         iface->append_message = theme_adium_append_message;
848         iface->append_event = theme_adium_append_event;
849         iface->scroll = theme_adium_scroll;
850         iface->scroll_down = theme_adium_scroll_down;
851         iface->get_has_selection = theme_adium_get_has_selection;
852         iface->clear = theme_adium_clear;
853         iface->find_previous = theme_adium_find_previous;
854         iface->find_next = theme_adium_find_next;
855         iface->find_abilities = theme_adium_find_abilities;
856         iface->highlight = theme_adium_highlight;
857         iface->copy_clipboard = theme_adium_copy_clipboard;
858 }
859
860 static void
861 theme_adium_load_finished_cb (WebKitWebView  *view,
862                               WebKitWebFrame *frame,
863                               gpointer        user_data)
864 {
865         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
866         EmpathyChatView       *chat_view = EMPATHY_CHAT_VIEW (view);
867
868         DEBUG ("Page loaded");
869         priv->page_loaded = TRUE;
870
871         /* Display queued messages */
872         priv->message_queue = g_list_reverse (priv->message_queue);
873         while (priv->message_queue) {
874                 EmpathyMessage *message = priv->message_queue->data;
875
876                 theme_adium_append_message (chat_view, message);
877                 priv->message_queue = g_list_remove (priv->message_queue, message);
878                 g_object_unref (message);
879         }
880 }
881
882 static void
883 theme_adium_finalize (GObject *object)
884 {
885         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
886
887         empathy_adium_data_unref (priv->data);
888         g_object_unref (priv->gsettings_chat);
889
890         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
891 }
892
893 static void
894 theme_adium_dispose (GObject *object)
895 {
896         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
897
898         if (priv->smiley_manager) {
899                 g_object_unref (priv->smiley_manager);
900                 priv->smiley_manager = NULL;
901         }
902
903         if (priv->last_contact) {
904                 g_object_unref (priv->last_contact);
905                 priv->last_contact = NULL;
906         }
907
908         if (priv->inspector_window) {
909                 gtk_widget_destroy (priv->inspector_window);
910                 priv->inspector_window = NULL;
911         }
912
913         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
914 }
915
916 static gboolean
917 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
918                                       EmpathyThemeAdium  *theme)
919 {
920         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
921
922         if (priv->inspector_window) {
923                 gtk_widget_show_all (priv->inspector_window);
924         }
925
926         return TRUE;
927 }
928
929 static gboolean
930 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
931                                        EmpathyThemeAdium  *theme)
932 {
933         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
934
935         if (priv->inspector_window) {
936                 gtk_widget_hide (priv->inspector_window);
937         }
938
939         return TRUE;
940 }
941
942 static WebKitWebView *
943 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
944                                  WebKitWebView      *web_view,
945                                  EmpathyThemeAdium  *theme)
946 {
947         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
948         GtkWidget             *scrolled_window;
949         GtkWidget             *inspector_web_view;
950
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),
955                                              800, 600);
956                 g_signal_connect (priv->inspector_window, "delete-event",
957                                   G_CALLBACK (gtk_widget_hide_on_delete), NULL);
958
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),
965                                    scrolled_window);
966                 gtk_widget_show  (scrolled_window);
967
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),
972                                    inspector_web_view);
973                 gtk_widget_show (scrolled_window);
974
975                 return WEBKIT_WEB_VIEW (inspector_web_view);
976         }
977
978         return NULL;
979 }
980
981 static PangoFontDescription *
982 theme_adium_get_default_font (void)
983 {
984         GConfClient *gconf_client;
985         PangoFontDescription *pango_fd;
986         gchar *gconf_font_family;
987
988         gconf_client = gconf_client_get_default ();
989         if (gconf_client == NULL) {
990                 return NULL;
991         }
992         gconf_font_family = gconf_client_get_string (gconf_client,
993                      EMPATHY_GCONF_FONT_KEY_NAME,
994                      NULL);
995         if (gconf_font_family == NULL) {
996                 g_object_unref (gconf_client);
997                 return NULL;
998         }
999         pango_fd = pango_font_description_from_string (gconf_font_family);
1000         g_free (gconf_font_family);
1001         g_object_unref (gconf_client);
1002         return pango_fd;
1003 }
1004
1005 static void
1006 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1007                              const gchar *name,
1008                              gint size)
1009 {
1010         g_object_set (w_settings, "default-font-family", name, NULL);
1011         g_object_set (w_settings, "default-font-size", size, NULL);
1012 }
1013
1014 static void
1015 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1016 {
1017         PangoFontDescription *default_font_desc;
1018         GdkScreen *current_screen;
1019         gdouble dpi = 0;
1020         gint pango_font_size = 0;
1021
1022         default_font_desc = theme_adium_get_default_font ();
1023         if (default_font_desc == NULL)
1024                 return ;
1025         pango_font_size = pango_font_description_get_size (default_font_desc)
1026                 / PANGO_SCALE ;
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);
1031                 } else {
1032                         dpi = BORING_DPI_DEFAULT;
1033                 }
1034                 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1035         }
1036         theme_adium_set_webkit_font (w_settings,
1037                 pango_font_description_get_family (default_font_desc),
1038                 pango_font_size);
1039         pango_font_description_free (default_font_desc);
1040 }
1041
1042 static void
1043 theme_adium_constructed (GObject *object)
1044 {
1045         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1046         gchar                 *basedir_uri;
1047         const gchar           *font_family = NULL;
1048         gint                   font_size = 0;
1049         WebKitWebView         *webkit_view = WEBKIT_WEB_VIEW (object);
1050         WebKitWebSettings     *webkit_settings;
1051         WebKitWebInspector    *webkit_inspector;
1052
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);
1057
1058         if (font_family && font_size) {
1059                 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1060         } else {
1061                 theme_adium_set_default_font (webkit_settings);
1062         }
1063
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),
1068                           object);
1069         g_signal_connect (webkit_inspector, "show-window",
1070                           G_CALLBACK (theme_adium_inspector_show_window_cb),
1071                           object);
1072         g_signal_connect (webkit_inspector, "close-window",
1073                           G_CALLBACK (theme_adium_inspector_close_window_cb),
1074                           object);
1075
1076         /* Load template */
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,
1080                                           basedir_uri);
1081         g_free (basedir_uri);
1082 }
1083
1084 static void
1085 theme_adium_get_property (GObject    *object,
1086                           guint       param_id,
1087                           GValue     *value,
1088                           GParamSpec *pspec)
1089 {
1090         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1091
1092         switch (param_id) {
1093         case PROP_ADIUM_DATA:
1094                 g_value_set_boxed (value, priv->data);
1095                 break;
1096         default:
1097                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1098                 break;
1099         };
1100 }
1101
1102 static void
1103 theme_adium_set_property (GObject      *object,
1104                           guint         param_id,
1105                           const GValue *value,
1106                           GParamSpec   *pspec)
1107 {
1108         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1109
1110         switch (param_id) {
1111         case PROP_ADIUM_DATA:
1112                 g_assert (priv->data == NULL);
1113                 priv->data = g_value_dup_boxed (value);
1114                 break;
1115         default:
1116                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1117                 break;
1118         };
1119 }
1120
1121 static void
1122 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1123 {
1124         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1125         GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1126
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;
1132
1133         widget_class->button_press_event = theme_adium_button_press_event;
1134
1135         g_object_class_install_property (object_class,
1136                                          PROP_ADIUM_DATA,
1137                                          g_param_spec_boxed ("adium-data",
1138                                                              "The theme data",
1139                                                              "Data for the adium theme",
1140                                                               EMPATHY_TYPE_ADIUM_DATA,
1141                                                               G_PARAM_CONSTRUCT_ONLY |
1142                                                               G_PARAM_READWRITE |
1143                                                               G_PARAM_STATIC_STRINGS));
1144
1145         g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1146 }
1147
1148 static void
1149 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1150 {
1151         EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1152                 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1153
1154         theme->priv = priv;
1155
1156         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1157
1158         g_signal_connect (theme, "load-finished",
1159                           G_CALLBACK (theme_adium_load_finished_cb),
1160                           NULL);
1161         g_signal_connect (theme, "navigation-policy-decision-requested",
1162                           G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1163                           NULL);
1164
1165         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1166         g_signal_connect (priv->gsettings_chat,
1167                 "changed::" EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1168                 G_CALLBACK (theme_adium_notify_enable_webkit_developer_tools_cb),
1169                 theme);
1170
1171         theme_adium_update_enable_webkit_developer_tools (theme);
1172 }
1173
1174 EmpathyThemeAdium *
1175 empathy_theme_adium_new (EmpathyAdiumData *data)
1176 {
1177         g_return_val_if_fail (data != NULL, NULL);
1178
1179         return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1180                              "adium-data", data,
1181                              NULL);
1182 }
1183
1184 gboolean
1185 empathy_adium_path_is_valid (const gchar *path)
1186 {
1187         gboolean ret;
1188         gchar   *file;
1189
1190         /* The theme is not valid if there is no Info.plist */
1191         file = g_build_filename (path, "Contents", "Info.plist",
1192                                  NULL);
1193         ret = g_file_test (file, G_FILE_TEST_EXISTS);
1194         g_free (file);
1195
1196         if (ret == FALSE)
1197                 return ret;
1198
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
1202          * incoming). */
1203         file = g_build_filename (path, "Contents", "Resources", "Incoming",
1204                                  "Content.html", NULL);
1205         ret = g_file_test (file, G_FILE_TEST_EXISTS);
1206         g_free (file);
1207
1208         return ret;
1209 }
1210
1211 GHashTable *
1212 empathy_adium_info_new (const gchar *path)
1213 {
1214         gchar *file;
1215         GValue *value;
1216         GHashTable *info = NULL;
1217
1218         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1219
1220         file = g_build_filename (path, "Contents", "Info.plist", NULL);
1221         value = empathy_plist_parse_from_file (file);
1222         g_free (file);
1223
1224         if (value == NULL)
1225                 return NULL;
1226
1227         info = g_value_dup_boxed (value);
1228         tp_g_value_slice_free (value);
1229
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);
1233
1234         return info;
1235 }
1236
1237 GType
1238 empathy_adium_data_get_type (void)
1239 {
1240   static GType type_id = 0;
1241
1242   if (!type_id)
1243     {
1244       type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1245           (GBoxedCopyFunc) empathy_adium_data_ref,
1246           (GBoxedFreeFunc) empathy_adium_data_unref);
1247     }
1248
1249   return type_id;
1250 }
1251
1252 EmpathyAdiumData  *
1253 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1254 {
1255         EmpathyAdiumData *data;
1256         gchar            *file;
1257         gchar            *template_html = NULL;
1258         gsize             template_len;
1259         gchar            *footer_html = NULL;
1260         gsize             footer_len;
1261         GString          *string;
1262         gchar           **strv = NULL;
1263         gchar            *css_path;
1264         guint             len = 0;
1265         guint             i = 0;
1266
1267         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1268
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);
1275
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);
1279         g_free (file);
1280
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);
1283         g_free (file);
1284
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);
1287         g_free (file);
1288
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);
1291         g_free (file);
1292
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);
1295         g_free (file);
1296
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);
1299         g_free (file);
1300
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);
1303         g_free (file);
1304
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);
1307         g_free (file);
1308
1309         file = g_build_filename (data->basedir, "Status.html", NULL);
1310         g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1311         g_free (file);
1312
1313         file = g_build_filename (data->basedir, "Footer.html", NULL);
1314         g_file_get_contents (file, &footer_html, &footer_len, NULL);
1315         g_free (file);
1316
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;
1320         } else {
1321                 g_free (file);
1322         }
1323
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;
1327         } else {
1328                 g_free (file);
1329         }
1330
1331         css_path = g_build_filename (data->basedir, "main.css", NULL);
1332
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);
1339         }
1340         g_free (file);
1341
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);
1346                 g_strfreev (strv);
1347
1348                 file = empathy_file_lookup ("Template.html", "data");
1349                 g_file_get_contents (file, &template_html, &template_len, NULL);
1350                 g_free (file);
1351                 strv = g_strsplit (template_html, "%@", -1);
1352                 len = g_strv_length (strv);
1353         }
1354
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++]);
1360         if (len == 6) {
1361                 const gchar *variant;
1362
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");
1367                 if (variant) {
1368                         g_string_append (string, "Variants/");
1369                         g_string_append (string, variant);
1370                         g_string_append (string, ".css");
1371                 }
1372         } else {
1373                 /* FIXME: We should set main.css OR the variant css */
1374                 g_string_append (string, css_path);
1375         }
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 */
1380         if (footer_html) {
1381                 g_string_append (string, footer_html);
1382         }
1383         g_string_append (string, strv[i++]);
1384         data->template_html = g_string_free (string, FALSE);
1385
1386         g_free (footer_html);
1387         g_free (template_html);
1388         g_free (css_path);
1389         g_strfreev (strv);
1390
1391         return data;
1392 }
1393
1394 EmpathyAdiumData  *
1395 empathy_adium_data_new (const gchar *path)
1396 {
1397         EmpathyAdiumData *data;
1398         GHashTable *info;
1399
1400         info = empathy_adium_info_new (path);
1401         data = empathy_adium_data_new_with_info (path, info);
1402         g_hash_table_unref (info);
1403
1404         return data;
1405 }
1406
1407 EmpathyAdiumData  *
1408 empathy_adium_data_ref (EmpathyAdiumData *data)
1409 {
1410         g_return_val_if_fail (data != NULL, NULL);
1411
1412         g_atomic_int_inc (&data->ref_count);
1413
1414         return data;
1415 }
1416
1417 void
1418 empathy_adium_data_unref (EmpathyAdiumData *data)
1419 {
1420         g_return_if_fail (data != NULL);
1421
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);
1440         }
1441 }
1442
1443 GHashTable *
1444 empathy_adium_data_get_info (EmpathyAdiumData *data)
1445 {
1446         g_return_val_if_fail (data != NULL, NULL);
1447
1448         return data->info;
1449 }
1450
1451 const gchar *
1452 empathy_adium_data_get_path (EmpathyAdiumData *data)
1453 {
1454         g_return_val_if_fail (data != NULL, NULL);
1455
1456         return data->path;
1457 }
1458