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