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