Added functions to determine if a contact has video capabilities
[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/webkitnetworkrequest.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/util.h>
30
31
32 #include <libempathy/empathy-time.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libmissioncontrol/mc-profile.h>
35
36 #include "empathy-theme-adium.h"
37 #include "empathy-smiley-manager.h"
38 #include "empathy-conf.h"
39 #include "empathy-ui-utils.h"
40 #include "empathy-plist.h"
41
42 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
43 #include <libempathy/empathy-debug.h>
44
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
46
47 /* "Join" consecutive messages with timestamps within five minutes */
48 #define MESSAGE_JOIN_PERIOD 5*60
49
50 typedef struct {
51         EmpathyAdiumData     *data;
52         EmpathySmileyManager *smiley_manager;
53         EmpathyContact       *last_contact;
54         time_t                last_timestamp;
55         gboolean              page_loaded;
56         GList                *message_queue;
57 } EmpathyThemeAdiumPriv;
58
59 struct _EmpathyAdiumData {
60         guint  ref_count;
61         gchar *path;
62         gchar *basedir;
63         gchar *default_avatar_filename;
64         gchar *default_incoming_avatar_filename;
65         gchar *default_outgoing_avatar_filename;
66         gchar *template_html;
67         gchar *in_content_html;
68         gsize  in_content_len;
69         gchar *in_nextcontent_html;
70         gsize  in_nextcontent_len;
71         gchar *out_content_html;
72         gsize  out_content_len;
73         gchar *out_nextcontent_html;
74         gsize  out_nextcontent_len;
75         gchar *status_html;
76         gsize  status_len;
77         GHashTable *info;
78 };
79
80 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
81
82 enum {
83         PROP_0,
84         PROP_ADIUM_DATA,
85 };
86
87 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
88                          WEBKIT_TYPE_WEB_VIEW,
89                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
90                                                 theme_adium_iface_init));
91
92 static WebKitNavigationResponse
93 theme_adium_navigation_requested_cb (WebKitWebView        *view,
94                                      WebKitWebFrame       *frame,
95                                      WebKitNetworkRequest *request,
96                                      gpointer              user_data)
97 {
98         const gchar *uri;
99
100         uri = webkit_network_request_get_uri (request);
101         empathy_url_show (GTK_WIDGET (view), uri);
102
103         return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
104 }
105
106 static void
107 theme_adium_populate_popup_cb (WebKitWebView *view,
108                                GtkMenu       *menu,
109                                gpointer       user_data)
110 {
111         GtkWidget *item;
112
113         /* Remove default menu items */
114         gtk_container_foreach (GTK_CONTAINER (menu),
115                 (GtkCallback) gtk_widget_destroy, NULL);
116
117         /* Select all item */
118         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
119         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
120         gtk_widget_show (item);
121
122         g_signal_connect_swapped (item, "activate",
123                                   G_CALLBACK (webkit_web_view_select_all),
124                                   view);
125
126         /* Copy menu item */
127         if (webkit_web_view_can_copy_clipboard (view)) {
128                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
129                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
130                 gtk_widget_show (item);
131
132                 g_signal_connect_swapped (item, "activate",
133                                           G_CALLBACK (webkit_web_view_copy_clipboard),
134                                           view);
135         }
136
137         /* Clear menu item */
138         item = gtk_separator_menu_item_new ();
139         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
140         gtk_widget_show (item);
141
142         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
143         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
144         gtk_widget_show (item);
145
146         g_signal_connect_swapped (item, "activate",
147                                   G_CALLBACK (empathy_chat_view_clear),
148                                   view);
149
150         /* FIXME: Add open_link and copy_link when those bugs are fixed:
151          * https://bugs.webkit.org/show_bug.cgi?id=16092
152          * https://bugs.webkit.org/show_bug.cgi?id=16562
153          */
154 }
155
156 static gchar *
157 theme_adium_parse_body (EmpathyThemeAdium *theme,
158                         const gchar       *text)
159 {
160         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
161         gboolean               use_smileys = FALSE;
162         GSList                *smileys, *l;
163         GString               *string;
164         gint                   i;
165         GRegex                *uri_regex;
166         GMatchInfo            *match_info;
167         gboolean               match;
168         gchar                 *ret = NULL;
169         gint                   prev;
170
171         empathy_conf_get_bool (empathy_conf_get (),
172                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
173                                &use_smileys);
174
175         if (use_smileys) {
176                 /* Replace smileys by a <img/> tag */
177                 string = g_string_sized_new (strlen (text));
178                 smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
179                 for (l = smileys; l; l = l->next) {
180                         EmpathySmiley *smiley;
181
182                         smiley = l->data;
183                         if (smiley->path) {
184                                 g_string_append_printf (string,
185                                                         "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
186                                                         smiley->str, smiley->path, smiley->str);
187                         } else {
188                                 gchar *str;
189
190                                 str = g_markup_escape_text (smiley->str, -1);
191                                 g_string_append (string, str);
192                                 g_free (str);
193                         }
194                         empathy_smiley_free (smiley);
195                 }
196                 g_slist_free (smileys);
197
198                 g_free (ret);
199                 text = ret = g_string_free (string, FALSE);
200         }
201
202         /* Add <a href></a> arround links */
203         uri_regex = empathy_uri_regex_dup_singleton ();
204         match = g_regex_match (uri_regex, text, 0, &match_info);
205         if (match) {
206                 gint last = 0;
207                 gint s = 0, e = 0;
208
209                 string = g_string_sized_new (strlen (text));
210                 do {
211                         g_match_info_fetch_pos (match_info, 0, &s, &e);
212
213                         if (s > last) {
214                                 /* Append the text between last link (or the
215                                  * start of the message) and this link */
216                                 g_string_append_len (string, text + last, s - last);
217                         }
218
219                         /* Append the link inside <a href=""></a> tag */
220                         g_string_append (string, "<a href=\"");
221                         g_string_append_len (string, text + s, e - s);
222                         g_string_append (string, "\">");
223                         g_string_append_len (string, text + s, e - s);
224                         g_string_append (string, "</a>");
225
226                         last = e;
227                 } while (g_match_info_next (match_info, NULL));
228
229                 if (e < strlen (text)) {
230                         /* Append the text after the last link */
231                         g_string_append_len (string, text + e, strlen (text) - e);
232                 }
233
234                 g_free (ret);
235                 text = ret = g_string_free (string, FALSE);
236         }
237         g_match_info_free (match_info);
238         g_regex_unref (uri_regex);
239
240         /* Replace \n by <br/> */
241         string = NULL;
242         prev = 0;
243         for (i = 0; text[i] != '\0'; i++) {
244                 if (text[i] == '\n') {
245                         if (!string ) {
246                                 string = g_string_sized_new (strlen (text));
247                         }
248                         g_string_append_len (string, text + prev, i - prev);
249                         g_string_append (string, "<br/>");
250                         prev = i + 1;
251                 }
252         }
253         if (string) {
254                 g_string_append (string, text + prev);
255                 g_free (ret);
256                 text = ret = g_string_free (string, FALSE);
257         }
258
259         return ret;
260 }
261
262 static void
263 escape_and_append_len (GString *string, const gchar *str, gint len)
264 {
265         while (*str != '\0' && len != 0) {
266                 switch (*str) {
267                 case '\\':
268                         /* \ becomes \\ */
269                         g_string_append (string, "\\\\");
270                         break;
271                 case '\"':
272                         /* " becomes \" */
273                         g_string_append (string, "\\\"");
274                         break;
275                 case '\n':
276                         /* Remove end of lines */
277                         break;
278                 default:
279                         g_string_append_c (string, *str);
280                 }
281
282                 str++;
283                 len--;
284         }
285 }
286
287 static gboolean
288 theme_adium_match (const gchar **str, const gchar *match)
289 {
290         gint len;
291
292         len = strlen (match);
293         if (strncmp (*str, match, len) == 0) {
294                 *str += len - 1;
295                 return TRUE;
296         }
297
298         return FALSE;
299 }
300
301 static void
302 theme_adium_append_html (EmpathyThemeAdium *theme,
303                          const gchar       *func,
304                          const gchar       *html, gsize len,
305                          const gchar       *message,
306                          const gchar       *avatar_filename,
307                          const gchar       *name,
308                          const gchar       *contact_id,
309                          const gchar       *service_name,
310                          const gchar       *message_classes,
311                          time_t             timestamp)
312 {
313         GString     *string;
314         const gchar *cur = NULL;
315         gchar       *script;
316
317         /* Make some search-and-replace in the html code */
318         string = g_string_sized_new (len + strlen (message));
319         g_string_append_printf (string, "%s(\"", func);
320         for (cur = html; *cur != '\0'; cur++) {
321                 const gchar *replace = NULL;
322                 gchar       *dup_replace = NULL;
323
324                 if (theme_adium_match (&cur, "%message%")) {
325                         replace = message;
326                 } else if (theme_adium_match (&cur, "%messageClasses%")) {
327                         replace = message_classes;
328                 } else if (theme_adium_match (&cur, "%userIconPath%")) {
329                         replace = avatar_filename;
330                 } else if (theme_adium_match (&cur, "%sender%")) {
331                         replace = name;
332                 } else if (theme_adium_match (&cur, "%senderScreenName%")) {
333                         replace = contact_id;
334                 } else if (theme_adium_match (&cur, "%senderDisplayName%")) {
335                         /* %senderDisplayName% -
336                          * "The serverside (remotely set) name of the sender,
337                          *  such as an MSN display name."
338                          *
339                          * We don't have access to that yet so we use local
340                          * alias instead.*/
341                         replace = name;
342                 } else if (theme_adium_match (&cur, "%service%")) {
343                         replace = service_name;
344                 } else if (theme_adium_match (&cur, "%shortTime%")) {
345                         dup_replace = empathy_time_to_string_local (timestamp,
346                                 EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
347                         replace = dup_replace;
348                 } else if (theme_adium_match (&cur, "%time")) {
349                         gchar *format = NULL;
350                         gchar *end;
351
352                         /* Time can be in 2 formats:
353                          * %time% or %time{strftime format}%
354                          * Extract the time format if provided. */
355                         if (cur[1] == '{') {
356                                 cur += 2;
357                                 end = strstr (cur, "}%");
358                                 if (!end) {
359                                         /* Invalid string */
360                                         continue;
361                                 }
362                                 format = g_strndup (cur, end - cur);
363                                 cur = end + 1;
364                         } else {
365                                 cur++;
366                         }
367
368                         dup_replace = empathy_time_to_string_local (timestamp,
369                                 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
370                         replace = dup_replace;
371                         g_free (format);
372                 } else {
373                         escape_and_append_len (string, cur, 1);
374                         continue;
375                 }
376
377                 /* Here we have a replacement to make */
378                 escape_and_append_len (string, replace, -1);
379                 g_free (dup_replace);
380         }
381         g_string_append (string, "\")");
382
383         script = g_string_free (string, FALSE);
384         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
385         g_free (script);
386 }
387
388 static void
389 theme_adium_append_message (EmpathyChatView *view,
390                             EmpathyMessage  *msg)
391 {
392         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
393         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
394         EmpathyContact        *sender;
395         EmpathyAccount        *account;
396         McProfile             *account_profile;
397         gchar                 *dup_body = NULL;
398         const gchar           *body;
399         const gchar           *name;
400         const gchar           *contact_id;
401         EmpathyAvatar         *avatar;
402         const gchar           *avatar_filename = NULL;
403         time_t                 timestamp;
404         gchar                 *html = NULL;
405         gsize                  len = 0;
406         const gchar           *func;
407         const gchar           *service_name;
408         const gchar           *message_classes = NULL;
409
410         if (!priv->page_loaded) {
411                 priv->message_queue = g_list_prepend (priv->message_queue,
412                                                       g_object_ref (msg));
413                 return;
414         }
415
416         /* Get information */
417         sender = empathy_message_get_sender (msg);
418         account = empathy_contact_get_account (sender);
419         account_profile = empathy_account_get_profile (account);
420         service_name = mc_profile_get_display_name (account_profile);
421         timestamp = empathy_message_get_timestamp (msg);
422         body = empathy_message_get_body (msg);
423         dup_body = theme_adium_parse_body (theme, body);
424         if (dup_body) {
425                 body = dup_body;
426         }
427         name = empathy_contact_get_name (sender);
428         contact_id = empathy_contact_get_id (sender);
429
430         /* If this is a /me, append an event */
431         if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
432                 gchar *str;
433
434                 str = g_strdup_printf ("%s %s", name, body);
435                 empathy_chat_view_append_event (view, str);
436                 g_free (str);
437                 g_free (dup_body);
438                 return;
439         }
440
441         /* Get the avatar filename, or a fallback */
442         avatar = empathy_contact_get_avatar (sender);
443         if (avatar) {
444                 avatar_filename = avatar->filename;
445         }
446         if (!avatar_filename) {
447                 if (empathy_contact_is_user (sender)) {
448                         avatar_filename = priv->data->default_outgoing_avatar_filename;
449                 } else {
450                         avatar_filename = priv->data->default_incoming_avatar_filename;
451                 }
452                 if (!avatar_filename) {
453                         if (!priv->data->default_avatar_filename) {
454                                 priv->data->default_avatar_filename =
455                                         empathy_filename_from_icon_name ("stock_person",
456                                                                          GTK_ICON_SIZE_DIALOG);
457                         }
458                         avatar_filename = priv->data->default_avatar_filename;
459                 }
460         }
461
462         /* Get the right html/func to add the message */
463         func = "appendMessage";
464         /*
465          * To mimick Adium's behavior, we only want to join messages
466          * sent within a 5 minute time frame.
467          */
468         if (empathy_contact_equal (priv->last_contact, sender) &&
469             (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD)) {
470                 func = "appendNextMessage";
471                 if (empathy_contact_is_user (sender)) {
472                         message_classes = "consecutive incoming message";
473                         html = priv->data->out_nextcontent_html;
474                         len = priv->data->out_nextcontent_len;
475                 }
476                 if (!html) {
477                         message_classes = "consecutive message outgoing";
478                         html = priv->data->in_nextcontent_html;
479                         len = priv->data->in_nextcontent_len;
480                 }
481         }
482         if (!html) {
483                 if (empathy_contact_is_user (sender)) {
484                         if (!message_classes) {
485                                 message_classes = "incoming message";
486                         }
487                         html = priv->data->out_content_html;
488                         len = priv->data->out_content_len;
489                 }
490                 if (!html) {
491                         if (!message_classes) {
492                                 message_classes = "message outgoing";
493                         }
494                         html = priv->data->in_content_html;
495                         len = priv->data->in_content_len;
496                 }
497         }
498
499         theme_adium_append_html (theme, func, html, len, body, avatar_filename,
500                                  name, contact_id, service_name, message_classes,
501                                  timestamp);
502
503         /* Keep the sender of the last displayed message */
504         if (priv->last_contact) {
505                 g_object_unref (priv->last_contact);
506         }
507         priv->last_contact = g_object_ref (sender);
508         priv->last_timestamp = timestamp;
509
510         g_free (dup_body);
511 }
512
513 static void
514 theme_adium_append_event (EmpathyChatView *view,
515                           const gchar     *str)
516 {
517         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
518         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
519
520         if (priv->data->status_html) {
521                 theme_adium_append_html (theme, "appendMessage",
522                                          priv->data->status_html,
523                                          priv->data->status_len,
524                                          str, NULL, NULL, NULL, NULL, "event",
525                                          empathy_time_get_current ());
526         }
527
528         /* There is no last contact */
529         if (priv->last_contact) {
530                 g_object_unref (priv->last_contact);
531                 priv->last_contact = NULL;
532         }
533 }
534
535 static void
536 theme_adium_scroll (EmpathyChatView *view,
537                     gboolean         allow_scrolling)
538 {
539         /* FIXME: Is it possible? I guess we need a js function, but I don't
540          * see any... */
541 }
542
543 static void
544 theme_adium_scroll_down (EmpathyChatView *view)
545 {
546         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
547 }
548
549 static gboolean
550 theme_adium_get_has_selection (EmpathyChatView *view)
551 {
552         return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
553 }
554
555 static void
556 theme_adium_clear (EmpathyChatView *view)
557 {
558         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
559         gchar *basedir_uri;
560
561         priv->page_loaded = FALSE;
562         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
563         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
564                                           priv->data->template_html,
565                                           basedir_uri);
566         g_free (basedir_uri);
567
568         /* Clear last contact to avoid trying to add a 'joined'
569          * message when we don't have an insertion point. */
570         if (priv->last_contact) {
571                 g_object_unref (priv->last_contact);
572                 priv->last_contact = NULL;
573         }
574 }
575
576 static gboolean
577 theme_adium_find_previous (EmpathyChatView *view,
578                            const gchar     *search_criteria,
579                            gboolean         new_search)
580 {
581         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
582                                             search_criteria, FALSE,
583                                             FALSE, TRUE);
584 }
585
586 static gboolean
587 theme_adium_find_next (EmpathyChatView *view,
588                        const gchar     *search_criteria,
589                        gboolean         new_search)
590 {
591         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
592                                             search_criteria, FALSE,
593                                             TRUE, TRUE);
594 }
595
596 static void
597 theme_adium_find_abilities (EmpathyChatView *view,
598                             const gchar    *search_criteria,
599                             gboolean       *can_do_previous,
600                             gboolean       *can_do_next)
601 {
602         /* FIXME: Does webkit provide an API for that? We have wrap=true in
603          * find_next and find_previous to work around this problem. */
604         if (can_do_previous)
605                 *can_do_previous = TRUE;
606         if (can_do_next)
607                 *can_do_next = TRUE;
608 }
609
610 static void
611 theme_adium_highlight (EmpathyChatView *view,
612                        const gchar     *text)
613 {
614         webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
615         webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
616                                            text, FALSE, 0);
617         webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
618                                                     TRUE);
619 }
620
621 static void
622 theme_adium_copy_clipboard (EmpathyChatView *view)
623 {
624         webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
625 }
626
627 static void
628 theme_adium_iface_init (EmpathyChatViewIface *iface)
629 {
630         iface->append_message = theme_adium_append_message;
631         iface->append_event = theme_adium_append_event;
632         iface->scroll = theme_adium_scroll;
633         iface->scroll_down = theme_adium_scroll_down;
634         iface->get_has_selection = theme_adium_get_has_selection;
635         iface->clear = theme_adium_clear;
636         iface->find_previous = theme_adium_find_previous;
637         iface->find_next = theme_adium_find_next;
638         iface->find_abilities = theme_adium_find_abilities;
639         iface->highlight = theme_adium_highlight;
640         iface->copy_clipboard = theme_adium_copy_clipboard;
641 }
642
643 static void
644 theme_adium_load_finished_cb (WebKitWebView  *view,
645                               WebKitWebFrame *frame,
646                               gpointer        user_data)
647 {
648         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
649         EmpathyChatView       *chat_view = EMPATHY_CHAT_VIEW (view);
650
651         DEBUG ("Page loaded");
652         priv->page_loaded = TRUE;
653
654         /* Display queued messages */
655         priv->message_queue = g_list_reverse (priv->message_queue);
656         while (priv->message_queue) {
657                 EmpathyMessage *message = priv->message_queue->data;
658
659                 theme_adium_append_message (chat_view, message);
660                 priv->message_queue = g_list_remove (priv->message_queue, message);
661                 g_object_unref (message);
662         }
663 }
664
665 static void
666 theme_adium_finalize (GObject *object)
667 {
668         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
669
670         empathy_adium_data_unref (priv->data);
671
672         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
673 }
674
675 static void
676 theme_adium_dispose (GObject *object)
677 {
678         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
679
680         if (priv->smiley_manager) {
681                 g_object_unref (priv->smiley_manager);
682                 priv->smiley_manager = NULL;
683         }
684
685         if (priv->last_contact) {
686                 g_object_unref (priv->last_contact);
687                 priv->last_contact = NULL;
688         }
689
690         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
691 }
692
693 static void
694 theme_adium_constructed (GObject *object)
695 {
696         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
697         gchar                 *basedir_uri;
698         const gchar           *font_family = NULL;
699         gint                   font_size = 0;
700         WebKitWebSettings     *webkit_settings;
701
702         /* Set default settings */
703         font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
704         font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
705         webkit_settings = webkit_web_settings_new ();
706         if (font_family) {
707                 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
708         }
709         if (font_size) {
710                 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
711         }
712         webkit_web_view_set_settings (WEBKIT_WEB_VIEW (object), webkit_settings);
713
714         /* Load template */
715         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
716         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
717                                           priv->data->template_html,
718                                           basedir_uri);
719
720         g_object_unref (webkit_settings);
721         g_free (basedir_uri);
722 }
723
724 static void
725 theme_adium_get_property (GObject    *object,
726                           guint       param_id,
727                           GValue     *value,
728                           GParamSpec *pspec)
729 {
730         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
731
732         switch (param_id) {
733         case PROP_ADIUM_DATA:
734                 g_value_set_boxed (value, priv->data);
735                 break;
736         default:
737                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
738                 break;
739         };
740 }
741
742 static void
743 theme_adium_set_property (GObject      *object,
744                           guint         param_id,
745                           const GValue *value,
746                           GParamSpec   *pspec)
747 {
748         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
749
750         switch (param_id) {
751         case PROP_ADIUM_DATA:
752                 g_assert (priv->data == NULL);
753                 priv->data = g_value_dup_boxed (value);
754                 break;
755         default:
756                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
757                 break;
758         };
759 }
760
761 static void
762 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
763 {
764         GObjectClass *object_class = G_OBJECT_CLASS (klass);
765
766         object_class->finalize = theme_adium_finalize;
767         object_class->dispose = theme_adium_dispose;
768         object_class->constructed = theme_adium_constructed;
769         object_class->get_property = theme_adium_get_property;
770         object_class->set_property = theme_adium_set_property;
771
772         g_object_class_install_property (object_class,
773                                          PROP_ADIUM_DATA,
774                                          g_param_spec_boxed ("adium-data",
775                                                              "The theme data",
776                                                              "Data for the adium theme",
777                                                               EMPATHY_TYPE_ADIUM_DATA,
778                                                               G_PARAM_CONSTRUCT_ONLY |
779                                                               G_PARAM_READWRITE |
780                                                               G_PARAM_STATIC_STRINGS));
781
782
783         g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
784 }
785
786 static void
787 empathy_theme_adium_init (EmpathyThemeAdium *theme)
788 {
789         EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
790                 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
791
792         theme->priv = priv;
793
794         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
795
796         g_signal_connect (theme, "load-finished",
797                           G_CALLBACK (theme_adium_load_finished_cb),
798                           NULL);
799         g_signal_connect (theme, "navigation-requested",
800                           G_CALLBACK (theme_adium_navigation_requested_cb),
801                           NULL);
802         g_signal_connect (theme, "populate-popup",
803                           G_CALLBACK (theme_adium_populate_popup_cb),
804                           NULL);
805 }
806
807 EmpathyThemeAdium *
808 empathy_theme_adium_new (EmpathyAdiumData *data)
809 {
810         g_return_val_if_fail (data != NULL, NULL);
811
812         return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
813                              "adium-data", data,
814                              NULL);
815 }
816
817 gboolean
818 empathy_adium_path_is_valid (const gchar *path)
819 {
820         gboolean ret;
821         gchar   *file;
822
823         /* We ship a default Template.html as fallback if there is any problem
824          * with the one inside the theme. The only other required file is
825          * Content.html for incoming messages (outgoing fallback to use
826          * incoming). */
827         file = g_build_filename (path, "Contents", "Resources", "Incoming",
828                                  "Content.html", NULL);
829         ret = g_file_test (file, G_FILE_TEST_EXISTS);
830         g_free (file);
831
832         return ret;
833 }
834
835 GHashTable *
836 empathy_adium_info_new (const gchar *path)
837 {
838         gchar *file;
839         GValue *value;
840         GHashTable *info = NULL;
841
842         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
843
844         file = g_build_filename (path, "Contents", "Info.plist", NULL);
845         value = empathy_plist_parse_from_file (file);
846         g_free (file);
847
848         if (value) {
849                 info = g_value_dup_boxed (value);
850                 tp_g_value_slice_free (value);
851         }
852
853         return info;
854 }
855
856 GType
857 empathy_adium_data_get_type (void)
858 {
859   static GType type_id = 0;
860
861   if (!type_id)
862     {
863       type_id = g_boxed_type_register_static ("EmpathyAdiumData",
864           (GBoxedCopyFunc) empathy_adium_data_ref,
865           (GBoxedFreeFunc) empathy_adium_data_unref);
866     }
867
868   return type_id;
869 }
870
871 EmpathyAdiumData  *
872 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
873 {
874         EmpathyAdiumData *data;
875         gchar            *file;
876         gchar            *template_html = NULL;
877         gsize             template_len;
878         gchar            *footer_html = NULL;
879         gsize             footer_len;
880         GString          *string;
881         gchar           **strv = NULL;
882         gchar            *css_path;
883         guint             len = 0;
884         guint             i = 0;
885
886         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
887
888         data = g_slice_new0 (EmpathyAdiumData);
889         data->ref_count = 1;
890         data->path = g_strdup (path);
891         data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
892                 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
893         data->info = g_hash_table_ref (info);
894
895         /* Load html files */
896         file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
897         g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
898         g_free (file);
899
900         file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
901         g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
902         g_free (file);
903
904         file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
905         g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
906         g_free (file);
907
908         file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
909         g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
910         g_free (file);
911
912         file = g_build_filename (data->basedir, "Status.html", NULL);
913         g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
914         g_free (file);
915
916         file = g_build_filename (data->basedir, "Footer.html", NULL);
917         g_file_get_contents (file, &footer_html, &footer_len, NULL);
918         g_free (file);
919
920         file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
921         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
922                 data->default_incoming_avatar_filename = file;
923         } else {
924                 g_free (file);
925         }
926
927         file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
928         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
929                 data->default_outgoing_avatar_filename = file;
930         } else {
931                 g_free (file);
932         }
933
934         css_path = g_build_filename (data->basedir, "main.css", NULL);
935
936         /* There is 2 formats for Template.html: The old one has 4 parameters,
937          * the new one has 5 parameters. */
938         file = g_build_filename (data->basedir, "Template.html", NULL);
939         if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
940                 strv = g_strsplit (template_html, "%@", -1);
941                 len = g_strv_length (strv);
942         }
943         g_free (file);
944
945         if (len != 5 && len != 6) {
946                 /* Either the theme has no template or it don't have the good
947                  * number of parameters. Fallback to use our own template. */
948                 g_free (template_html);
949                 g_strfreev (strv);
950
951                 file = empathy_file_lookup ("Template.html", "data");
952                 g_file_get_contents (file, &template_html, &template_len, NULL);
953                 g_free (file);
954                 strv = g_strsplit (template_html, "%@", -1);
955                 len = g_strv_length (strv);
956         }
957
958         /* Replace %@ with the needed information in the template html. */
959         string = g_string_sized_new (template_len);
960         g_string_append (string, strv[i++]);
961         g_string_append (string, data->basedir);
962         g_string_append (string, strv[i++]);
963         if (len == 6) {
964                 const gchar *variant;
965
966                 /* We include main.css by default */
967                 g_string_append_printf (string, "@import url(\"%s\");", css_path);
968                 g_string_append (string, strv[i++]);
969                 variant = tp_asv_get_string (data->info, "DefaultVariant");
970                 if (variant) {
971                         g_string_append (string, "Variants/");
972                         g_string_append (string, variant);
973                         g_string_append (string, ".css");
974                 }
975         } else {
976                 /* FIXME: We should set main.css OR the variant css */
977                 g_string_append (string, css_path);
978         }
979         g_string_append (string, strv[i++]);
980         g_string_append (string, ""); /* We don't want header */
981         g_string_append (string, strv[i++]);
982         /* FIXME: We should replace adium %macros% in footer */
983         if (footer_html) {
984                 g_string_append (string, footer_html);
985         }
986         g_string_append (string, strv[i++]);
987         data->template_html = g_string_free (string, FALSE);
988
989         g_free (footer_html);
990         g_free (template_html);
991         g_free (css_path);
992         g_strfreev (strv);
993
994         return data;
995 }
996
997 EmpathyAdiumData  *
998 empathy_adium_data_new (const gchar *path)
999 {
1000         EmpathyAdiumData *data;
1001         GHashTable *info;
1002
1003         info = empathy_adium_info_new (path);
1004         data = empathy_adium_data_new_with_info (path, info);
1005         g_hash_table_unref (info);
1006
1007         return data;
1008 }
1009
1010 EmpathyAdiumData  *
1011 empathy_adium_data_ref (EmpathyAdiumData *data)
1012 {
1013         g_return_val_if_fail (data != NULL, NULL);
1014
1015         data->ref_count++;
1016
1017         return data;
1018 }
1019
1020 void
1021 empathy_adium_data_unref (EmpathyAdiumData *data)
1022 {
1023         g_return_if_fail (data != NULL);
1024
1025         data->ref_count--;
1026         if (data->ref_count == 0) {
1027                 g_free (data->path);
1028                 g_free (data->basedir);
1029                 g_free (data->template_html);
1030                 g_free (data->in_content_html);
1031                 g_free (data->in_nextcontent_html);
1032                 g_free (data->out_content_html);
1033                 g_free (data->out_nextcontent_html);
1034                 g_free (data->default_avatar_filename);
1035                 g_free (data->default_incoming_avatar_filename);
1036                 g_free (data->default_outgoing_avatar_filename);
1037                 g_free (data->status_html);
1038                 g_hash_table_unref (data->info);
1039                 g_slice_free (EmpathyAdiumData, data);
1040         }
1041 }
1042
1043 GHashTable *
1044 empathy_adium_data_get_info (EmpathyAdiumData *data)
1045 {
1046         g_return_val_if_fail (data != NULL, NULL);
1047
1048         return data->info;
1049 }
1050
1051 const gchar *
1052 empathy_adium_data_get_path (EmpathyAdiumData *data)
1053 {
1054         g_return_val_if_fail (data != NULL, NULL);
1055
1056         return data->path;
1057 }
1058