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