]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-adium.c
Merge branch 'subscription-msg-630707'
[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         return g_string_free (string, FALSE);
276 }
277
278 static void
279 escape_and_append_len (GString *string, const gchar *str, gint len)
280 {
281         while (*str != '\0' && len != 0) {
282                 switch (*str) {
283                 case '\\':
284                         /* \ becomes \\ */
285                         g_string_append (string, "\\\\");
286                         break;
287                 case '\"':
288                         /* " becomes \" */
289                         g_string_append (string, "\\\"");
290                         break;
291                 case '\n':
292                         /* Remove end of lines */
293                         break;
294                 default:
295                         g_string_append_c (string, *str);
296                 }
297
298                 str++;
299                 len--;
300         }
301 }
302
303 static gboolean
304 theme_adium_match (const gchar **str, const gchar *match)
305 {
306         gint len;
307
308         len = strlen (match);
309         if (strncmp (*str, match, len) == 0) {
310                 *str += len - 1;
311                 return TRUE;
312         }
313
314         return FALSE;
315 }
316
317 static void
318 theme_adium_append_html (EmpathyThemeAdium *theme,
319                          const gchar       *func,
320                          const gchar       *html, gsize len,
321                          const gchar       *message,
322                          const gchar       *avatar_filename,
323                          const gchar       *name,
324                          const gchar       *contact_id,
325                          const gchar       *service_name,
326                          const gchar       *message_classes,
327                          time_t             timestamp,
328                          gboolean           is_backlog)
329 {
330         GString     *string;
331         const gchar *cur = NULL;
332         gchar       *script;
333
334         /* Make some search-and-replace in the html code */
335         string = g_string_sized_new (len + strlen (message));
336         g_string_append_printf (string, "%s(\"", func);
337         for (cur = html; *cur != '\0'; cur++) {
338                 const gchar *replace = NULL;
339                 gchar       *dup_replace = NULL;
340
341                 if (theme_adium_match (&cur, "%message%")) {
342                         replace = message;
343                 } else if (theme_adium_match (&cur, "%messageClasses%")) {
344                         replace = message_classes;
345                 } else if (theme_adium_match (&cur, "%userIconPath%")) {
346                         replace = avatar_filename;
347                 } else if (theme_adium_match (&cur, "%sender%")) {
348                         replace = name;
349                 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
350                         replace = contact_id;
351                 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
352                         /* %senderDisplayName% -
353                          * "The serverside (remotely set) name of the sender,
354                          *  such as an MSN display name."
355                          *
356                          * We don't have access to that yet so we use local
357                          * alias instead.*/
358                         replace = name;
359                 } else if (theme_adium_match (&cur, "%service%")) {
360                         replace = service_name;
361                 } else if (theme_adium_match (&cur, "%shortTime%")) {
362                         dup_replace = empathy_time_to_string_local (timestamp,
363                                 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
364                         replace = dup_replace;
365                 } else if (theme_adium_match (&cur, "%time")) {
366                         gchar *format = NULL;
367                         gchar *end;
368                         /* Time can be in 2 formats:
369                          * %time% or %time{strftime format}%
370                          * Extract the time format if provided. */
371                         if (cur[1] == '{') {
372                                 cur += 2;
373                                 end = strstr (cur, "}%");
374                                 if (!end) {
375                                         /* Invalid string */
376                                         continue;
377                                 }
378                                 format = g_strndup (cur, end - cur);
379                                 cur = end + 1;
380                         } else {
381                                 cur++;
382                         }
383
384                         if (is_backlog) {
385                                 dup_replace = empathy_time_to_string_local (timestamp,
386                                         format ? format : EMPATHY_TIME_DATE_FORMAT_DISPLAY_SHORT);
387                         } else {
388                                 dup_replace = empathy_time_to_string_local (timestamp,
389                                         format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
390                         }
391                         replace = dup_replace;
392                         g_free (format);
393                 } else {
394                         escape_and_append_len (string, cur, 1);
395                         continue;
396                 }
397
398                 /* Here we have a replacement to make */
399                 escape_and_append_len (string, replace, -1);
400                 g_free (dup_replace);
401         }
402         g_string_append (string, "\")");
403
404         script = g_string_free (string, FALSE);
405         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
406         g_free (script);
407 }
408
409 static void
410 theme_adium_append_event_escaped (EmpathyChatView *view,
411                                   const gchar     *escaped)
412 {
413         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
414         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
415
416         if (priv->data->status_html) {
417                 theme_adium_append_html (theme, "appendMessage",
418                                          priv->data->status_html,
419                                          priv->data->status_len,
420                                          escaped, NULL, NULL, NULL, NULL,
421                                          "event", empathy_time_get_current (), FALSE);
422         }
423
424         /* There is no last contact */
425         if (priv->last_contact) {
426                 g_object_unref (priv->last_contact);
427                 priv->last_contact = NULL;
428         }
429 }
430
431 static void
432 theme_adium_append_message (EmpathyChatView *view,
433                             EmpathyMessage  *msg)
434 {
435         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
436         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
437         EmpathyContact        *sender;
438         TpAccount             *account;
439         gchar                 *body_escaped;
440         const gchar           *body;
441         const gchar           *name;
442         const gchar           *contact_id;
443         EmpathyAvatar         *avatar;
444         const gchar           *avatar_filename = NULL;
445         time_t                 timestamp;
446         gchar                 *html = NULL;
447         gsize                  len = 0;
448         const gchar           *func;
449         const gchar           *service_name;
450         GString               *message_classes = NULL;
451         gboolean               is_backlog;
452         gboolean               consecutive;
453
454         if (!priv->page_loaded) {
455                 priv->message_queue = g_list_prepend (priv->message_queue,
456                                                       g_object_ref (msg));
457                 return;
458         }
459
460         /* Get information */
461         sender = empathy_message_get_sender (msg);
462         account = empathy_contact_get_account (sender);
463         service_name = empathy_protocol_name_to_display_name
464                 (tp_account_get_protocol (account));
465         if (service_name == NULL)
466                 service_name = tp_account_get_protocol (account);
467         timestamp = empathy_message_get_timestamp (msg);
468         body = empathy_message_get_body (msg);
469         body_escaped = theme_adium_parse_body (body);
470         name = empathy_contact_get_alias (sender);
471         contact_id = empathy_contact_get_id (sender);
472
473         /* If this is a /me, append an event */
474         if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
475                 gchar *str;
476
477                 str = g_strdup_printf ("%s %s", name, body_escaped);
478                 theme_adium_append_event_escaped (view, str);
479
480                 g_free (str);
481                 g_free (body_escaped);
482                 return;
483         }
484
485         /* Get the avatar filename, or a fallback */
486         avatar = empathy_contact_get_avatar (sender);
487         if (avatar) {
488                 avatar_filename = avatar->filename;
489         }
490         if (!avatar_filename) {
491                 if (empathy_contact_is_user (sender)) {
492                         avatar_filename = priv->data->default_outgoing_avatar_filename;
493                 } else {
494                         avatar_filename = priv->data->default_incoming_avatar_filename;
495                 }
496                 if (!avatar_filename) {
497                         if (!priv->data->default_avatar_filename) {
498                                 priv->data->default_avatar_filename =
499                                         empathy_filename_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
500                                                                          GTK_ICON_SIZE_DIALOG);
501                         }
502                         avatar_filename = priv->data->default_avatar_filename;
503                 }
504         }
505
506         /* We want to join this message with the last one if
507          * - senders are the same contact,
508          * - last message was recieved recently,
509          * - last message and this message both are/aren't backlog, and
510          * - DisableCombineConsecutive is not set in theme's settings */
511         is_backlog = empathy_message_is_backlog (msg);
512         consecutive = empathy_contact_equal (priv->last_contact, sender) &&
513                 (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD) &&
514                 (is_backlog == priv->last_is_backlog) &&
515                 !tp_asv_get_boolean (priv->data->info,
516                                      "DisableCombineConsecutive", NULL);
517
518         /* Define message classes */
519         message_classes = g_string_new ("message");
520         if (is_backlog) {
521                 g_string_append (message_classes, " history");
522         }
523         if (consecutive) {
524                 g_string_append (message_classes, " consecutive");
525         }
526         if (empathy_contact_is_user (sender)) {
527                 g_string_append (message_classes, " outgoing");
528         } else {
529                 g_string_append (message_classes, " incoming");
530         }
531
532         /* Define javascript function to use */
533         if (consecutive) {
534                 func = "appendNextMessage";
535         } else {
536                 func = "appendMessage";
537         }
538
539         /* Outgoing */
540         if (empathy_contact_is_user (sender)) {
541                 if (consecutive) {
542                         if (is_backlog) {
543                                 html = priv->data->out_nextcontext_html;
544                                 len = priv->data->out_nextcontext_len;
545                         }
546
547                         /* Not backlog, or fallback if NextContext.html
548                          * is missing */
549                         if (html == NULL) {
550                                 html = priv->data->out_nextcontent_html;
551                                 len = priv->data->out_nextcontent_len;
552                         }
553                 }
554
555                 /* Not consecutive, or fallback if NextContext.html and/or
556                  * NextContent.html are missing */
557                 if (html == NULL) {
558                         if (is_backlog) {
559                                 html = priv->data->out_context_html;
560                                 len = priv->data->out_context_len;
561                         }
562
563                         if (html == NULL) {
564                                 html = priv->data->out_content_html;
565                                 len = priv->data->out_content_len;
566                         }
567                 }
568         }
569
570         /* Incoming, or fallback if outgoing files are missing */
571         if (html == NULL) {
572                 if (consecutive) {
573                         if (is_backlog) {
574                                 html = priv->data->in_nextcontext_html;
575                                 len = priv->data->in_nextcontext_len;
576                         }
577
578                         /* Note backlog, or fallback if NextContext.html
579                          * is missing */
580                         if (html == NULL) {
581                                 html = priv->data->in_nextcontent_html;
582                                 len = priv->data->in_nextcontent_len;
583                         }
584                 }
585
586                 /* Not consecutive, or fallback if NextContext.html and/or
587                  * NextContent.html are missing */
588                 if (html == NULL) {
589                         if (is_backlog) {
590                                 html = priv->data->in_context_html;
591                                 len = priv->data->in_context_len;
592                         }
593
594                         if (html == NULL) {
595                                 html = priv->data->in_content_html;
596                                 len = priv->data->in_content_len;
597                         }
598                 }
599         }
600
601         if (html != NULL) {
602                 theme_adium_append_html (theme, func, html, len, body_escaped,
603                                          avatar_filename, name, contact_id,
604                                          service_name, message_classes->str,
605                                          timestamp, is_backlog);
606         } else {
607                 DEBUG ("Couldn't find HTML file for this message");
608         }
609
610         /* Keep the sender of the last displayed message */
611         if (priv->last_contact) {
612                 g_object_unref (priv->last_contact);
613         }
614         priv->last_contact = g_object_ref (sender);
615         priv->last_timestamp = timestamp;
616         priv->last_is_backlog = is_backlog;
617
618         g_free (body_escaped);
619         g_string_free (message_classes, TRUE);
620 }
621
622 static void
623 theme_adium_append_event (EmpathyChatView *view,
624                           const gchar     *str)
625 {
626         gchar *str_escaped;
627
628         str_escaped = g_markup_escape_text (str, -1);
629         theme_adium_append_event_escaped (view, str_escaped);
630         g_free (str_escaped);
631 }
632
633 static void
634 theme_adium_scroll (EmpathyChatView *view,
635                     gboolean         allow_scrolling)
636 {
637         /* FIXME: Is it possible? I guess we need a js function, but I don't
638          * see any... */
639 }
640
641 static void
642 theme_adium_scroll_down (EmpathyChatView *view)
643 {
644         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
645 }
646
647 static gboolean
648 theme_adium_get_has_selection (EmpathyChatView *view)
649 {
650         return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
651 }
652
653 static void
654 theme_adium_clear (EmpathyChatView *view)
655 {
656         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
657         gchar *basedir_uri;
658
659         priv->page_loaded = FALSE;
660         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
661         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
662                                           priv->data->template_html,
663                                           basedir_uri);
664         g_free (basedir_uri);
665
666         /* Clear last contact to avoid trying to add a 'joined'
667          * message when we don't have an insertion point. */
668         if (priv->last_contact) {
669                 g_object_unref (priv->last_contact);
670                 priv->last_contact = NULL;
671         }
672 }
673
674 static gboolean
675 theme_adium_find_previous (EmpathyChatView *view,
676                            const gchar     *search_criteria,
677                            gboolean         new_search,
678                            gboolean         match_case)
679 {
680         /* FIXME: Doesn't respect new_search */
681         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
682                                             search_criteria, match_case,
683                                             FALSE, TRUE);
684 }
685
686 static gboolean
687 theme_adium_find_next (EmpathyChatView *view,
688                        const gchar     *search_criteria,
689                        gboolean         new_search,
690                        gboolean         match_case)
691 {
692         /* FIXME: Doesn't respect new_search */
693         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
694                                             search_criteria, match_case,
695                                             TRUE, TRUE);
696 }
697
698 static void
699 theme_adium_find_abilities (EmpathyChatView *view,
700                             const gchar    *search_criteria,
701                             gboolean        match_case,
702                             gboolean       *can_do_previous,
703                             gboolean       *can_do_next)
704 {
705         /* FIXME: Does webkit provide an API for that? We have wrap=true in
706          * find_next and find_previous to work around this problem. */
707         if (can_do_previous)
708                 *can_do_previous = TRUE;
709         if (can_do_next)
710                 *can_do_next = TRUE;
711 }
712
713 static void
714 theme_adium_highlight (EmpathyChatView *view,
715                        const gchar     *text,
716                        gboolean         match_case)
717 {
718         webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
719         webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
720                                            text, match_case, 0);
721         webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
722                                                     TRUE);
723 }
724
725 static void
726 theme_adium_copy_clipboard (EmpathyChatView *view)
727 {
728         webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
729 }
730
731 static void
732 theme_adium_context_menu_selection_done_cb (GtkMenuShell *menu, gpointer user_data)
733 {
734         WebKitHitTestResult *hit_test_result = WEBKIT_HIT_TEST_RESULT (user_data);
735
736         g_object_unref (hit_test_result);
737 }
738
739 static void
740 theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *event)
741 {
742         WebKitWebView              *view = WEBKIT_WEB_VIEW (theme);
743         WebKitHitTestResult        *hit_test_result;
744         WebKitHitTestResultContext  context;
745         GtkWidget                  *menu;
746         GtkWidget                  *item;
747
748         hit_test_result = webkit_web_view_get_hit_test_result (view, event);
749         g_object_get (G_OBJECT (hit_test_result), "context", &context, NULL);
750
751         /* The menu */
752         menu = gtk_menu_new ();
753
754         /* Select all item */
755         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
756         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
757
758         g_signal_connect_swapped (item, "activate",
759                                   G_CALLBACK (webkit_web_view_select_all),
760                                   view);
761
762         /* Copy menu item */
763         if (webkit_web_view_can_copy_clipboard (view)) {
764                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
765                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
766
767                 g_signal_connect_swapped (item, "activate",
768                                           G_CALLBACK (webkit_web_view_copy_clipboard),
769                                           view);
770         }
771
772         /* Clear menu item */
773         item = gtk_separator_menu_item_new ();
774         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
775
776         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
777         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
778
779         g_signal_connect_swapped (item, "activate",
780                                   G_CALLBACK (empathy_chat_view_clear),
781                                   view);
782
783         /* We will only add the following menu items if we are
784          * right-clicking a link */
785         if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
786                 /* Separator */
787                 item = gtk_separator_menu_item_new ();
788                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
789
790                 /* Copy Link Address menu item */
791                 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
792                 g_signal_connect (item, "activate",
793                                   G_CALLBACK (theme_adium_copy_address_cb),
794                                   hit_test_result);
795                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
796
797                 /* Open Link menu item */
798                 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
799                 g_signal_connect (item, "activate",
800                                   G_CALLBACK (theme_adium_open_address_cb),
801                                   hit_test_result);
802                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
803         }
804
805         g_signal_connect (GTK_MENU_SHELL (menu), "selection-done",
806                           G_CALLBACK (theme_adium_context_menu_selection_done_cb),
807                           hit_test_result);
808
809         /* Display the menu */
810         gtk_widget_show_all (menu);
811         gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
812                         event->button, event->time);
813         g_object_ref_sink (menu);
814         g_object_unref (menu);
815 }
816
817 static gboolean
818 theme_adium_button_press_event (GtkWidget *widget, GdkEventButton *event)
819 {
820         if (event->button == 3) {
821                 gboolean developer_tools_enabled;
822
823                 g_object_get (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (widget))),
824                               "enable-developer-extras", &developer_tools_enabled, NULL);
825
826                 /* We currently have no way to add an inspector menu
827                  * item ourselves, so we disable our customized menu
828                  * if the developer extras are enabled. */
829                 if (!developer_tools_enabled) {
830                         theme_adium_context_menu_for_event (EMPATHY_THEME_ADIUM (widget), event);
831                         return TRUE;
832                 }
833         }
834
835         return GTK_WIDGET_CLASS (empathy_theme_adium_parent_class)->button_press_event (widget, event);
836 }
837
838 static void
839 theme_adium_iface_init (EmpathyChatViewIface *iface)
840 {
841         iface->append_message = theme_adium_append_message;
842         iface->append_event = theme_adium_append_event;
843         iface->scroll = theme_adium_scroll;
844         iface->scroll_down = theme_adium_scroll_down;
845         iface->get_has_selection = theme_adium_get_has_selection;
846         iface->clear = theme_adium_clear;
847         iface->find_previous = theme_adium_find_previous;
848         iface->find_next = theme_adium_find_next;
849         iface->find_abilities = theme_adium_find_abilities;
850         iface->highlight = theme_adium_highlight;
851         iface->copy_clipboard = theme_adium_copy_clipboard;
852 }
853
854 static void
855 theme_adium_load_finished_cb (WebKitWebView  *view,
856                               WebKitWebFrame *frame,
857                               gpointer        user_data)
858 {
859         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
860         EmpathyChatView       *chat_view = EMPATHY_CHAT_VIEW (view);
861
862         DEBUG ("Page loaded");
863         priv->page_loaded = TRUE;
864
865         /* Display queued messages */
866         priv->message_queue = g_list_reverse (priv->message_queue);
867         while (priv->message_queue) {
868                 EmpathyMessage *message = priv->message_queue->data;
869
870                 theme_adium_append_message (chat_view, message);
871                 priv->message_queue = g_list_remove (priv->message_queue, message);
872                 g_object_unref (message);
873         }
874 }
875
876 static void
877 theme_adium_finalize (GObject *object)
878 {
879         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
880
881         empathy_adium_data_unref (priv->data);
882         g_object_unref (priv->gsettings_chat);
883
884         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
885 }
886
887 static void
888 theme_adium_dispose (GObject *object)
889 {
890         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
891
892         if (priv->smiley_manager) {
893                 g_object_unref (priv->smiley_manager);
894                 priv->smiley_manager = NULL;
895         }
896
897         if (priv->last_contact) {
898                 g_object_unref (priv->last_contact);
899                 priv->last_contact = NULL;
900         }
901
902         if (priv->inspector_window) {
903                 gtk_widget_destroy (priv->inspector_window);
904                 priv->inspector_window = NULL;
905         }
906
907         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
908 }
909
910 static gboolean
911 theme_adium_inspector_show_window_cb (WebKitWebInspector *inspector,
912                                       EmpathyThemeAdium  *theme)
913 {
914         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
915
916         if (priv->inspector_window) {
917                 gtk_widget_show_all (priv->inspector_window);
918         }
919
920         return TRUE;
921 }
922
923 static gboolean
924 theme_adium_inspector_close_window_cb (WebKitWebInspector *inspector,
925                                        EmpathyThemeAdium  *theme)
926 {
927         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
928
929         if (priv->inspector_window) {
930                 gtk_widget_hide (priv->inspector_window);
931         }
932
933         return TRUE;
934 }
935
936 static WebKitWebView *
937 theme_adium_inspect_web_view_cb (WebKitWebInspector *inspector,
938                                  WebKitWebView      *web_view,
939                                  EmpathyThemeAdium  *theme)
940 {
941         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
942         GtkWidget             *scrolled_window;
943         GtkWidget             *inspector_web_view;
944
945         if (!priv->inspector_window) {
946                 /* Create main window */
947                 priv->inspector_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
948                 gtk_window_set_default_size (GTK_WINDOW (priv->inspector_window),
949                                              800, 600);
950                 g_signal_connect (priv->inspector_window, "delete-event",
951                                   G_CALLBACK (gtk_widget_hide_on_delete), NULL);
952
953                 /* Pack a scrolled window */
954                 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
955                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
956                                                 GTK_POLICY_AUTOMATIC,
957                                                 GTK_POLICY_AUTOMATIC);
958                 gtk_container_add (GTK_CONTAINER (priv->inspector_window),
959                                    scrolled_window);
960                 gtk_widget_show  (scrolled_window);
961
962                 /* Pack a webview in the scrolled window. That webview will be
963                  * used to render the inspector tool.  */
964                 inspector_web_view = webkit_web_view_new ();
965                 gtk_container_add (GTK_CONTAINER (scrolled_window),
966                                    inspector_web_view);
967                 gtk_widget_show (scrolled_window);
968
969                 return WEBKIT_WEB_VIEW (inspector_web_view);
970         }
971
972         return NULL;
973 }
974
975 static PangoFontDescription *
976 theme_adium_get_default_font (void)
977 {
978         GConfClient *gconf_client;
979         PangoFontDescription *pango_fd;
980         gchar *gconf_font_family;
981
982         gconf_client = gconf_client_get_default ();
983         if (gconf_client == NULL) {
984                 return NULL;
985         }
986         gconf_font_family = gconf_client_get_string (gconf_client,
987                      EMPATHY_GCONF_FONT_KEY_NAME,
988                      NULL);
989         if (gconf_font_family == NULL) {
990                 g_object_unref (gconf_client);
991                 return NULL;
992         }
993         pango_fd = pango_font_description_from_string (gconf_font_family);
994         g_free (gconf_font_family);
995         g_object_unref (gconf_client);
996         return pango_fd;
997 }
998
999 static void
1000 theme_adium_set_webkit_font (WebKitWebSettings *w_settings,
1001                              const gchar *name,
1002                              gint size)
1003 {
1004         g_object_set (w_settings, "default-font-family", name, NULL);
1005         g_object_set (w_settings, "default-font-size", size, NULL);
1006 }
1007
1008 static void
1009 theme_adium_set_default_font (WebKitWebSettings *w_settings)
1010 {
1011         PangoFontDescription *default_font_desc;
1012         GdkScreen *current_screen;
1013         gdouble dpi = 0;
1014         gint pango_font_size = 0;
1015
1016         default_font_desc = theme_adium_get_default_font ();
1017         if (default_font_desc == NULL)
1018                 return ;
1019         pango_font_size = pango_font_description_get_size (default_font_desc)
1020                 / PANGO_SCALE ;
1021         if (pango_font_description_get_size_is_absolute (default_font_desc)) {
1022                 current_screen = gdk_screen_get_default ();
1023                 if (current_screen != NULL) {
1024                         dpi = gdk_screen_get_resolution (current_screen);
1025                 } else {
1026                         dpi = BORING_DPI_DEFAULT;
1027                 }
1028                 pango_font_size = (gint) (pango_font_size / (dpi / 72));
1029         }
1030         theme_adium_set_webkit_font (w_settings,
1031                 pango_font_description_get_family (default_font_desc),
1032                 pango_font_size);
1033         pango_font_description_free (default_font_desc);
1034 }
1035
1036 static void
1037 theme_adium_constructed (GObject *object)
1038 {
1039         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1040         gchar                 *basedir_uri;
1041         const gchar           *font_family = NULL;
1042         gint                   font_size = 0;
1043         WebKitWebView         *webkit_view = WEBKIT_WEB_VIEW (object);
1044         WebKitWebSettings     *webkit_settings;
1045         WebKitWebInspector    *webkit_inspector;
1046
1047         /* Set default settings */
1048         font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
1049         font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
1050         webkit_settings = webkit_web_view_get_settings (webkit_view);
1051
1052         if (font_family && font_size) {
1053                 theme_adium_set_webkit_font (webkit_settings, font_family, font_size);
1054         } else {
1055                 theme_adium_set_default_font (webkit_settings);
1056         }
1057
1058         /* Setup webkit inspector */
1059         webkit_inspector = webkit_web_view_get_inspector (webkit_view);
1060         g_signal_connect (webkit_inspector, "inspect-web-view",
1061                           G_CALLBACK (theme_adium_inspect_web_view_cb),
1062                           object);
1063         g_signal_connect (webkit_inspector, "show-window",
1064                           G_CALLBACK (theme_adium_inspector_show_window_cb),
1065                           object);
1066         g_signal_connect (webkit_inspector, "close-window",
1067                           G_CALLBACK (theme_adium_inspector_close_window_cb),
1068                           object);
1069
1070         /* Load template */
1071         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
1072         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
1073                                           priv->data->template_html,
1074                                           basedir_uri);
1075         g_free (basedir_uri);
1076 }
1077
1078 static void
1079 theme_adium_get_property (GObject    *object,
1080                           guint       param_id,
1081                           GValue     *value,
1082                           GParamSpec *pspec)
1083 {
1084         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1085
1086         switch (param_id) {
1087         case PROP_ADIUM_DATA:
1088                 g_value_set_boxed (value, priv->data);
1089                 break;
1090         default:
1091                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1092                 break;
1093         };
1094 }
1095
1096 static void
1097 theme_adium_set_property (GObject      *object,
1098                           guint         param_id,
1099                           const GValue *value,
1100                           GParamSpec   *pspec)
1101 {
1102         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
1103
1104         switch (param_id) {
1105         case PROP_ADIUM_DATA:
1106                 g_assert (priv->data == NULL);
1107                 priv->data = g_value_dup_boxed (value);
1108                 break;
1109         default:
1110                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1111                 break;
1112         };
1113 }
1114
1115 static void
1116 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
1117 {
1118         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1119         GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
1120
1121         object_class->finalize = theme_adium_finalize;
1122         object_class->dispose = theme_adium_dispose;
1123         object_class->constructed = theme_adium_constructed;
1124         object_class->get_property = theme_adium_get_property;
1125         object_class->set_property = theme_adium_set_property;
1126
1127         widget_class->button_press_event = theme_adium_button_press_event;
1128
1129         g_object_class_install_property (object_class,
1130                                          PROP_ADIUM_DATA,
1131                                          g_param_spec_boxed ("adium-data",
1132                                                              "The theme data",
1133                                                              "Data for the adium theme",
1134                                                               EMPATHY_TYPE_ADIUM_DATA,
1135                                                               G_PARAM_CONSTRUCT_ONLY |
1136                                                               G_PARAM_READWRITE |
1137                                                               G_PARAM_STATIC_STRINGS));
1138
1139         g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
1140 }
1141
1142 static void
1143 empathy_theme_adium_init (EmpathyThemeAdium *theme)
1144 {
1145         EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
1146                 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
1147
1148         theme->priv = priv;
1149
1150         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
1151
1152         g_signal_connect (theme, "load-finished",
1153                           G_CALLBACK (theme_adium_load_finished_cb),
1154                           NULL);
1155         g_signal_connect (theme, "navigation-policy-decision-requested",
1156                           G_CALLBACK (theme_adium_navigation_policy_decision_requested_cb),
1157                           NULL);
1158
1159         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1160         g_signal_connect (priv->gsettings_chat,
1161                 "changed::" EMPATHY_PREFS_CHAT_WEBKIT_DEVELOPER_TOOLS,
1162                 G_CALLBACK (theme_adium_notify_enable_webkit_developer_tools_cb),
1163                 theme);
1164
1165         theme_adium_update_enable_webkit_developer_tools (theme);
1166 }
1167
1168 EmpathyThemeAdium *
1169 empathy_theme_adium_new (EmpathyAdiumData *data)
1170 {
1171         g_return_val_if_fail (data != NULL, NULL);
1172
1173         return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
1174                              "adium-data", data,
1175                              NULL);
1176 }
1177
1178 gboolean
1179 empathy_adium_path_is_valid (const gchar *path)
1180 {
1181         gboolean ret;
1182         gchar   *file;
1183
1184         /* The theme is not valid if there is no Info.plist */
1185         file = g_build_filename (path, "Contents", "Info.plist",
1186                                  NULL);
1187         ret = g_file_test (file, G_FILE_TEST_EXISTS);
1188         g_free (file);
1189
1190         if (ret == FALSE)
1191                 return ret;
1192
1193         /* We ship a default Template.html as fallback if there is any problem
1194          * with the one inside the theme. The only other required file is
1195          * Content.html for incoming messages (outgoing fallback to use
1196          * incoming). */
1197         file = g_build_filename (path, "Contents", "Resources", "Incoming",
1198                                  "Content.html", NULL);
1199         ret = g_file_test (file, G_FILE_TEST_EXISTS);
1200         g_free (file);
1201
1202         return ret;
1203 }
1204
1205 GHashTable *
1206 empathy_adium_info_new (const gchar *path)
1207 {
1208         gchar *file;
1209         GValue *value;
1210         GHashTable *info = NULL;
1211
1212         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1213
1214         file = g_build_filename (path, "Contents", "Info.plist", NULL);
1215         value = empathy_plist_parse_from_file (file);
1216         g_free (file);
1217
1218         if (value == NULL)
1219                 return NULL;
1220
1221         info = g_value_dup_boxed (value);
1222         tp_g_value_slice_free (value);
1223
1224         /* Insert the theme's path into the hash table,
1225          * keys have to be dupped */
1226         tp_asv_set_string (info, g_strdup ("path"), path);
1227
1228         return info;
1229 }
1230
1231 GType
1232 empathy_adium_data_get_type (void)
1233 {
1234   static GType type_id = 0;
1235
1236   if (!type_id)
1237     {
1238       type_id = g_boxed_type_register_static ("EmpathyAdiumData",
1239           (GBoxedCopyFunc) empathy_adium_data_ref,
1240           (GBoxedFreeFunc) empathy_adium_data_unref);
1241     }
1242
1243   return type_id;
1244 }
1245
1246 EmpathyAdiumData  *
1247 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
1248 {
1249         EmpathyAdiumData *data;
1250         gchar            *file;
1251         gchar            *template_html = NULL;
1252         gsize             template_len;
1253         gchar            *footer_html = NULL;
1254         gsize             footer_len;
1255         GString          *string;
1256         gchar           **strv = NULL;
1257         gchar            *css_path;
1258         guint             len = 0;
1259         guint             i = 0;
1260
1261         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
1262
1263         data = g_slice_new0 (EmpathyAdiumData);
1264         data->ref_count = 1;
1265         data->path = g_strdup (path);
1266         data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
1267                 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
1268         data->info = g_hash_table_ref (info);
1269
1270         /* Load html files */
1271         file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
1272         g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
1273         g_free (file);
1274
1275         file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
1276         g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
1277         g_free (file);
1278
1279         file = g_build_filename (data->basedir, "Incoming", "Context.html", NULL);
1280         g_file_get_contents (file, &data->in_context_html, &data->in_context_len, NULL);
1281         g_free (file);
1282
1283         file = g_build_filename (data->basedir, "Incoming", "NextContext.html", NULL);
1284         g_file_get_contents (file, &data->in_nextcontext_html, &data->in_nextcontext_len, NULL);
1285         g_free (file);
1286
1287         file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
1288         g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
1289         g_free (file);
1290
1291         file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
1292         g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
1293         g_free (file);
1294
1295         file = g_build_filename (data->basedir, "Outgoing", "Context.html", NULL);
1296         g_file_get_contents (file, &data->out_context_html, &data->out_context_len, NULL);
1297         g_free (file);
1298
1299         file = g_build_filename (data->basedir, "Outgoing", "NextContext.html", NULL);
1300         g_file_get_contents (file, &data->out_nextcontext_html, &data->out_nextcontext_len, NULL);
1301         g_free (file);
1302
1303         file = g_build_filename (data->basedir, "Status.html", NULL);
1304         g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
1305         g_free (file);
1306
1307         file = g_build_filename (data->basedir, "Footer.html", NULL);
1308         g_file_get_contents (file, &footer_html, &footer_len, NULL);
1309         g_free (file);
1310
1311         file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
1312         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1313                 data->default_incoming_avatar_filename = file;
1314         } else {
1315                 g_free (file);
1316         }
1317
1318         file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
1319         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1320                 data->default_outgoing_avatar_filename = file;
1321         } else {
1322                 g_free (file);
1323         }
1324
1325         css_path = g_build_filename (data->basedir, "main.css", NULL);
1326
1327         /* There is 2 formats for Template.html: The old one has 4 parameters,
1328          * the new one has 5 parameters. */
1329         file = g_build_filename (data->basedir, "Template.html", NULL);
1330         if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
1331                 strv = g_strsplit (template_html, "%@", -1);
1332                 len = g_strv_length (strv);
1333         }
1334         g_free (file);
1335
1336         if (len != 5 && len != 6) {
1337                 /* Either the theme has no template or it don't have the good
1338                  * number of parameters. Fallback to use our own template. */
1339                 g_free (template_html);
1340                 g_strfreev (strv);
1341
1342                 file = empathy_file_lookup ("Template.html", "data");
1343                 g_file_get_contents (file, &template_html, &template_len, NULL);
1344                 g_free (file);
1345                 strv = g_strsplit (template_html, "%@", -1);
1346                 len = g_strv_length (strv);
1347         }
1348
1349         /* Replace %@ with the needed information in the template html. */
1350         string = g_string_sized_new (template_len);
1351         g_string_append (string, strv[i++]);
1352         g_string_append (string, data->basedir);
1353         g_string_append (string, strv[i++]);
1354         if (len == 6) {
1355                 const gchar *variant;
1356
1357                 /* We include main.css by default */
1358                 g_string_append_printf (string, "@import url(\"%s\");", css_path);
1359                 g_string_append (string, strv[i++]);
1360                 variant = tp_asv_get_string (data->info, "DefaultVariant");
1361                 if (variant) {
1362                         g_string_append (string, "Variants/");
1363                         g_string_append (string, variant);
1364                         g_string_append (string, ".css");
1365                 }
1366         } else {
1367                 /* FIXME: We should set main.css OR the variant css */
1368                 g_string_append (string, css_path);
1369         }
1370         g_string_append (string, strv[i++]);
1371         g_string_append (string, ""); /* We don't want header */
1372         g_string_append (string, strv[i++]);
1373         /* FIXME: We should replace adium %macros% in footer */
1374         if (footer_html) {
1375                 g_string_append (string, footer_html);
1376         }
1377         g_string_append (string, strv[i++]);
1378         data->template_html = g_string_free (string, FALSE);
1379
1380         g_free (footer_html);
1381         g_free (template_html);
1382         g_free (css_path);
1383         g_strfreev (strv);
1384
1385         return data;
1386 }
1387
1388 EmpathyAdiumData  *
1389 empathy_adium_data_new (const gchar *path)
1390 {
1391         EmpathyAdiumData *data;
1392         GHashTable *info;
1393
1394         info = empathy_adium_info_new (path);
1395         data = empathy_adium_data_new_with_info (path, info);
1396         g_hash_table_unref (info);
1397
1398         return data;
1399 }
1400
1401 EmpathyAdiumData  *
1402 empathy_adium_data_ref (EmpathyAdiumData *data)
1403 {
1404         g_return_val_if_fail (data != NULL, NULL);
1405
1406         g_atomic_int_inc (&data->ref_count);
1407
1408         return data;
1409 }
1410
1411 void
1412 empathy_adium_data_unref (EmpathyAdiumData *data)
1413 {
1414         g_return_if_fail (data != NULL);
1415
1416         if (g_atomic_int_dec_and_test (&data->ref_count)) {
1417                 g_free (data->path);
1418                 g_free (data->basedir);
1419                 g_free (data->template_html);
1420                 g_free (data->in_content_html);
1421                 g_free (data->in_nextcontent_html);
1422                 g_free (data->in_context_html);
1423                 g_free (data->in_nextcontext_html);
1424                 g_free (data->out_content_html);
1425                 g_free (data->out_nextcontent_html);
1426                 g_free (data->out_context_html);
1427                 g_free (data->out_nextcontext_html);
1428                 g_free (data->default_avatar_filename);
1429                 g_free (data->default_incoming_avatar_filename);
1430                 g_free (data->default_outgoing_avatar_filename);
1431                 g_free (data->status_html);
1432                 g_hash_table_unref (data->info);
1433                 g_slice_free (EmpathyAdiumData, data);
1434         }
1435 }
1436
1437 GHashTable *
1438 empathy_adium_data_get_info (EmpathyAdiumData *data)
1439 {
1440         g_return_val_if_fail (data != NULL, NULL);
1441
1442         return data->info;
1443 }
1444
1445 const gchar *
1446 empathy_adium_data_get_path (EmpathyAdiumData *data)
1447 {
1448         g_return_val_if_fail (data != NULL, NULL);
1449
1450         return data->path;
1451 }
1452