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