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