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