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