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