]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-adium.c
Update python bindings
[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                         /* Time can be in 2 formats:
352                          * %time% or %time{strftime format}%
353                          * Extract the time format if provided. */
354                         if (cur[1] == '{') {
355                                 cur += 2;
356                                 end = strstr (cur, "}%");
357                                 if (!end) {
358                                         /* Invalid string */
359                                         continue;
360                                 }
361                                 format = g_strndup (cur, end - cur);
362                                 cur = end + 1;
363                         } else {
364                                 cur++;
365                         }
366
367                         dup_replace = empathy_time_to_string_local (timestamp,
368                                 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
369                         replace = dup_replace;
370                         g_free (format);
371                 } else {
372                         escape_and_append_len (string, cur, 1);
373                         continue;
374                 }
375
376                 /* Here we have a replacement to make */
377                 escape_and_append_len (string, replace, -1);
378                 g_free (dup_replace);
379         }
380         g_string_append (string, "\")");
381
382         script = g_string_free (string, FALSE);
383         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (theme), script);
384         g_free (script);
385 }
386
387 static void
388 theme_adium_append_message (EmpathyChatView *view,
389                             EmpathyMessage  *msg)
390 {
391         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
392         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
393         EmpathyContact        *sender;
394         EmpathyAccount        *account;
395         gchar                 *dup_body = NULL;
396         const gchar           *body;
397         const gchar           *name;
398         const gchar           *contact_id;
399         EmpathyAvatar         *avatar;
400         const gchar           *avatar_filename = NULL;
401         time_t                 timestamp;
402         gchar                 *html = NULL;
403         gsize                  len = 0;
404         const gchar           *func;
405         const gchar           *service_name;
406         const gchar           *message_classes = NULL;
407
408         if (!priv->page_loaded) {
409                 priv->message_queue = g_list_prepend (priv->message_queue,
410                                                       g_object_ref (msg));
411                 return;
412         }
413
414         /* Get information */
415         sender = empathy_message_get_sender (msg);
416         account = empathy_contact_get_account (sender);
417         service_name = empathy_account_get_protocol (account);
418         timestamp = empathy_message_get_timestamp (msg);
419         body = empathy_message_get_body (msg);
420         dup_body = theme_adium_parse_body (theme, body);
421         if (dup_body) {
422                 body = dup_body;
423         }
424         name = empathy_contact_get_name (sender);
425         contact_id = empathy_contact_get_id (sender);
426
427         /* If this is a /me, append an event */
428         if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
429                 gchar *str;
430
431                 str = g_strdup_printf ("%s %s", name, body);
432                 empathy_chat_view_append_event (view, str);
433                 g_free (str);
434                 g_free (dup_body);
435                 return;
436         }
437
438         /* Get the avatar filename, or a fallback */
439         avatar = empathy_contact_get_avatar (sender);
440         if (avatar) {
441                 avatar_filename = avatar->filename;
442         }
443         if (!avatar_filename) {
444                 if (empathy_contact_is_user (sender)) {
445                         avatar_filename = priv->data->default_outgoing_avatar_filename;
446                 } else {
447                         avatar_filename = priv->data->default_incoming_avatar_filename;
448                 }
449                 if (!avatar_filename) {
450                         if (!priv->data->default_avatar_filename) {
451                                 priv->data->default_avatar_filename =
452                                         empathy_filename_from_icon_name ("stock_person",
453                                                                          GTK_ICON_SIZE_DIALOG);
454                         }
455                         avatar_filename = priv->data->default_avatar_filename;
456                 }
457         }
458
459         /* Get the right html/func to add the message */
460         func = "appendMessage";
461         /*
462          * To mimick Adium's behavior, we only want to join messages
463          * sent within a 5 minute time frame.
464          */
465         if (empathy_contact_equal (priv->last_contact, sender) &&
466             (timestamp - priv->last_timestamp < MESSAGE_JOIN_PERIOD)) {
467                 func = "appendNextMessage";
468                 if (empathy_contact_is_user (sender)) {
469                         message_classes = "consecutive incoming message";
470                         html = priv->data->out_nextcontent_html;
471                         len = priv->data->out_nextcontent_len;
472                 }
473                 if (!html) {
474                         message_classes = "consecutive message outgoing";
475                         html = priv->data->in_nextcontent_html;
476                         len = priv->data->in_nextcontent_len;
477                 }
478         }
479         if (!html) {
480                 if (empathy_contact_is_user (sender)) {
481                         if (!message_classes) {
482                                 message_classes = "incoming message";
483                         }
484                         html = priv->data->out_content_html;
485                         len = priv->data->out_content_len;
486                 }
487                 if (!html) {
488                         if (!message_classes) {
489                                 message_classes = "message outgoing";
490                         }
491                         html = priv->data->in_content_html;
492                         len = priv->data->in_content_len;
493                 }
494         }
495
496         theme_adium_append_html (theme, func, html, len, body, avatar_filename,
497                                  name, contact_id, service_name, message_classes,
498                                  timestamp);
499
500         /* Keep the sender of the last displayed message */
501         if (priv->last_contact) {
502                 g_object_unref (priv->last_contact);
503         }
504         priv->last_contact = g_object_ref (sender);
505         priv->last_timestamp = timestamp;
506
507         g_free (dup_body);
508 }
509
510 static void
511 theme_adium_append_event (EmpathyChatView *view,
512                           const gchar     *str)
513 {
514         EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
515         EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
516
517         if (priv->data->status_html) {
518                 theme_adium_append_html (theme, "appendMessage",
519                                          priv->data->status_html,
520                                          priv->data->status_len,
521                                          str, NULL, NULL, NULL, NULL, "event",
522                                          empathy_time_get_current ());
523         }
524
525         /* There is no last contact */
526         if (priv->last_contact) {
527                 g_object_unref (priv->last_contact);
528                 priv->last_contact = NULL;
529         }
530 }
531
532 static void
533 theme_adium_scroll (EmpathyChatView *view,
534                     gboolean         allow_scrolling)
535 {
536         /* FIXME: Is it possible? I guess we need a js function, but I don't
537          * see any... */
538 }
539
540 static void
541 theme_adium_scroll_down (EmpathyChatView *view)
542 {
543         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), "scrollToBottom()");
544 }
545
546 static gboolean
547 theme_adium_get_has_selection (EmpathyChatView *view)
548 {
549         return webkit_web_view_has_selection (WEBKIT_WEB_VIEW (view));
550 }
551
552 static void
553 theme_adium_clear (EmpathyChatView *view)
554 {
555         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
556         gchar *basedir_uri;
557
558         priv->page_loaded = FALSE;
559         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
560         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (view),
561                                           priv->data->template_html,
562                                           basedir_uri);
563         g_free (basedir_uri);
564 }
565
566 static gboolean
567 theme_adium_find_previous (EmpathyChatView *view,
568                            const gchar     *search_criteria,
569                            gboolean         new_search)
570 {
571         return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
572                                             search_criteria, FALSE,
573                                             FALSE, TRUE);
574 }
575
576 static gboolean
577 theme_adium_find_next (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                                             TRUE, TRUE);
584 }
585
586 static void
587 theme_adium_find_abilities (EmpathyChatView *view,
588                             const gchar    *search_criteria,
589                             gboolean       *can_do_previous,
590                             gboolean       *can_do_next)
591 {
592         /* FIXME: Does webkit provide an API for that? We have wrap=true in
593          * find_next and find_previous to work around this problem. */
594         if (can_do_previous)
595                 *can_do_previous = TRUE;
596         if (can_do_next)
597                 *can_do_next = TRUE;
598 }
599
600 static void
601 theme_adium_highlight (EmpathyChatView *view,
602                        const gchar     *text)
603 {
604         webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
605         webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
606                                            text, FALSE, 0);
607         webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
608                                                     TRUE);
609 }
610
611 static void
612 theme_adium_copy_clipboard (EmpathyChatView *view)
613 {
614         webkit_web_view_copy_clipboard (WEBKIT_WEB_VIEW (view));
615 }
616
617 static void
618 theme_adium_iface_init (EmpathyChatViewIface *iface)
619 {
620         iface->append_message = theme_adium_append_message;
621         iface->append_event = theme_adium_append_event;
622         iface->scroll = theme_adium_scroll;
623         iface->scroll_down = theme_adium_scroll_down;
624         iface->get_has_selection = theme_adium_get_has_selection;
625         iface->clear = theme_adium_clear;
626         iface->find_previous = theme_adium_find_previous;
627         iface->find_next = theme_adium_find_next;
628         iface->find_abilities = theme_adium_find_abilities;
629         iface->highlight = theme_adium_highlight;
630         iface->copy_clipboard = theme_adium_copy_clipboard;
631 }
632
633 static void
634 theme_adium_load_finished_cb (WebKitWebView  *view,
635                               WebKitWebFrame *frame,
636                               gpointer        user_data)
637 {
638         EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
639         EmpathyChatView       *chat_view = EMPATHY_CHAT_VIEW (view);
640
641         DEBUG ("Page loaded");
642         priv->page_loaded = TRUE;
643
644         /* Display queued messages */
645         priv->message_queue = g_list_reverse (priv->message_queue);
646         while (priv->message_queue) {
647                 EmpathyMessage *message = priv->message_queue->data;
648
649                 theme_adium_append_message (chat_view, message);
650                 priv->message_queue = g_list_remove (priv->message_queue, message);
651                 g_object_unref (message);
652         }
653 }
654
655 static void
656 theme_adium_finalize (GObject *object)
657 {
658         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
659
660         empathy_adium_data_unref (priv->data);
661
662         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
663 }
664
665 static void
666 theme_adium_dispose (GObject *object)
667 {
668         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
669
670         if (priv->smiley_manager) {
671                 g_object_unref (priv->smiley_manager);
672                 priv->smiley_manager = NULL;
673         }
674
675         if (priv->last_contact) {
676                 g_object_unref (priv->last_contact);
677                 priv->last_contact = NULL;
678         }
679
680         G_OBJECT_CLASS (empathy_theme_adium_parent_class)->dispose (object);
681 }
682
683 static void
684 theme_adium_constructed (GObject *object)
685 {
686         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
687         gchar                 *basedir_uri;
688         const gchar           *font_family = NULL;
689         gint                   font_size = 0;
690         WebKitWebSettings     *webkit_settings;
691
692         /* Set default settings */
693         font_family = tp_asv_get_string (priv->data->info, "DefaultFontFamily");
694         font_size = tp_asv_get_int32 (priv->data->info, "DefaultFontSize", NULL);
695         webkit_settings = webkit_web_settings_new ();
696         if (font_family) {
697                 g_object_set (G_OBJECT (webkit_settings), "default-font-family", font_family, NULL);
698         }
699         if (font_size) {
700                 g_object_set (G_OBJECT (webkit_settings), "default-font-size", font_size, NULL);
701         }
702         webkit_web_view_set_settings (WEBKIT_WEB_VIEW (object), webkit_settings);
703
704         /* Load template */
705         basedir_uri = g_strconcat ("file://", priv->data->basedir, NULL);
706         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (object),
707                                           priv->data->template_html,
708                                           basedir_uri);
709
710         g_object_unref (webkit_settings);
711         g_free (basedir_uri);
712 }
713
714 static void
715 theme_adium_get_property (GObject    *object,
716                           guint       param_id,
717                           GValue     *value,
718                           GParamSpec *pspec)
719 {
720         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
721
722         switch (param_id) {
723         case PROP_ADIUM_DATA:
724                 g_value_set_boxed (value, priv->data);
725                 break;
726         default:
727                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
728                 break;
729         };
730 }
731
732 static void
733 theme_adium_set_property (GObject      *object,
734                           guint         param_id,
735                           const GValue *value,
736                           GParamSpec   *pspec)
737 {
738         EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
739
740         switch (param_id) {
741         case PROP_ADIUM_DATA:
742                 g_assert (priv->data == NULL);
743                 priv->data = g_value_dup_boxed (value);
744                 break;
745         default:
746                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
747                 break;
748         };
749 }
750
751 static void
752 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
753 {
754         GObjectClass *object_class = G_OBJECT_CLASS (klass);
755
756         object_class->finalize = theme_adium_finalize;
757         object_class->dispose = theme_adium_dispose;
758         object_class->constructed = theme_adium_constructed;
759         object_class->get_property = theme_adium_get_property;
760         object_class->set_property = theme_adium_set_property;
761
762         g_object_class_install_property (object_class,
763                                          PROP_ADIUM_DATA,
764                                          g_param_spec_boxed ("adium-data",
765                                                              "The theme data",
766                                                              "Data for the adium theme",
767                                                               EMPATHY_TYPE_ADIUM_DATA,
768                                                               G_PARAM_CONSTRUCT_ONLY |
769                                                               G_PARAM_READWRITE |
770                                                               G_PARAM_STATIC_STRINGS));
771
772
773         g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
774 }
775
776 static void
777 empathy_theme_adium_init (EmpathyThemeAdium *theme)
778 {
779         EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
780                 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
781
782         theme->priv = priv;
783
784         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
785
786         g_signal_connect (theme, "load-finished",
787                           G_CALLBACK (theme_adium_load_finished_cb),
788                           NULL);
789         g_signal_connect (theme, "navigation-requested",
790                           G_CALLBACK (theme_adium_navigation_requested_cb),
791                           NULL);
792         g_signal_connect (theme, "populate-popup",
793                           G_CALLBACK (theme_adium_populate_popup_cb),
794                           NULL);
795 }
796
797 EmpathyThemeAdium *
798 empathy_theme_adium_new (EmpathyAdiumData *data)
799 {
800         g_return_val_if_fail (data != NULL, NULL);
801
802         return g_object_new (EMPATHY_TYPE_THEME_ADIUM,
803                              "adium-data", data,
804                              NULL);
805 }
806
807 gboolean
808 empathy_adium_path_is_valid (const gchar *path)
809 {
810         gboolean ret;
811         gchar   *file;
812
813         /* We ship a default Template.html as fallback if there is any problem
814          * with the one inside the theme. The only other required file is
815          * Content.html for incoming messages (outgoing fallback to use
816          * incoming). */
817         file = g_build_filename (path, "Contents", "Resources", "Incoming",
818                                  "Content.html", NULL);
819         ret = g_file_test (file, G_FILE_TEST_EXISTS);
820         g_free (file);
821
822         return ret;
823 }
824
825 GHashTable *
826 empathy_adium_info_new (const gchar *path)
827 {
828         gchar *file;
829         GValue *value;
830         GHashTable *info = NULL;
831
832         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
833
834         file = g_build_filename (path, "Contents", "Info.plist", NULL);
835         value = empathy_plist_parse_from_file (file);
836         g_free (file);
837
838         if (value) {
839                 info = g_value_dup_boxed (value);
840                 tp_g_value_slice_free (value);
841         }
842
843         return info;
844 }
845
846 GType
847 empathy_adium_data_get_type (void)
848 {
849   static GType type_id = 0;
850
851   if (!type_id)
852     {
853       type_id = g_boxed_type_register_static ("EmpathyAdiumData",
854           (GBoxedCopyFunc) empathy_adium_data_ref,
855           (GBoxedFreeFunc) empathy_adium_data_unref);
856     }
857
858   return type_id;
859 }
860
861 EmpathyAdiumData  *
862 empathy_adium_data_new_with_info (const gchar *path, GHashTable *info)
863 {
864         EmpathyAdiumData *data;
865         gchar            *file;
866         gchar            *template_html = NULL;
867         gsize             template_len;
868         gchar            *footer_html = NULL;
869         gsize             footer_len;
870         GString          *string;
871         gchar           **strv = NULL;
872         gchar            *css_path;
873         guint             len = 0;
874         guint             i = 0;
875
876         g_return_val_if_fail (empathy_adium_path_is_valid (path), NULL);
877
878         data = g_slice_new0 (EmpathyAdiumData);
879         data->ref_count = 1;
880         data->path = g_strdup (path);
881         data->basedir = g_strconcat (path, G_DIR_SEPARATOR_S "Contents"
882                 G_DIR_SEPARATOR_S "Resources" G_DIR_SEPARATOR_S, NULL);
883         data->info = g_hash_table_ref (info);
884
885         /* Load html files */
886         file = g_build_filename (data->basedir, "Incoming", "Content.html", NULL);
887         g_file_get_contents (file, &data->in_content_html, &data->in_content_len, NULL);
888         g_free (file);
889
890         file = g_build_filename (data->basedir, "Incoming", "NextContent.html", NULL);
891         g_file_get_contents (file, &data->in_nextcontent_html, &data->in_nextcontent_len, NULL);
892         g_free (file);
893
894         file = g_build_filename (data->basedir, "Outgoing", "Content.html", NULL);
895         g_file_get_contents (file, &data->out_content_html, &data->out_content_len, NULL);
896         g_free (file);
897
898         file = g_build_filename (data->basedir, "Outgoing", "NextContent.html", NULL);
899         g_file_get_contents (file, &data->out_nextcontent_html, &data->out_nextcontent_len, NULL);
900         g_free (file);
901
902         file = g_build_filename (data->basedir, "Status.html", NULL);
903         g_file_get_contents (file, &data->status_html, &data->status_len, NULL);
904         g_free (file);
905
906         file = g_build_filename (data->basedir, "Footer.html", NULL);
907         g_file_get_contents (file, &footer_html, &footer_len, NULL);
908         g_free (file);
909
910         file = g_build_filename (data->basedir, "Incoming", "buddy_icon.png", NULL);
911         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
912                 data->default_incoming_avatar_filename = file;
913         } else {
914                 g_free (file);
915         }
916
917         file = g_build_filename (data->basedir, "Outgoing", "buddy_icon.png", NULL);
918         if (g_file_test (file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
919                 data->default_outgoing_avatar_filename = file;
920         } else {
921                 g_free (file);
922         }
923
924         css_path = g_build_filename (data->basedir, "main.css", NULL);
925
926         /* There is 2 formats for Template.html: The old one has 4 parameters,
927          * the new one has 5 parameters. */
928         file = g_build_filename (data->basedir, "Template.html", NULL);
929         if (g_file_get_contents (file, &template_html, &template_len, NULL)) {
930                 strv = g_strsplit (template_html, "%@", -1);
931                 len = g_strv_length (strv);
932         }
933         g_free (file);
934
935         if (len != 5 && len != 6) {
936                 /* Either the theme has no template or it don't have the good
937                  * number of parameters. Fallback to use our own template. */
938                 g_free (template_html);
939                 g_strfreev (strv);
940
941                 file = empathy_file_lookup ("Template.html", "data");
942                 g_file_get_contents (file, &template_html, &template_len, NULL);
943                 g_free (file);
944                 strv = g_strsplit (template_html, "%@", -1);
945                 len = g_strv_length (strv);
946         }
947
948         /* Replace %@ with the needed information in the template html. */
949         string = g_string_sized_new (template_len);
950         g_string_append (string, strv[i++]);
951         g_string_append (string, data->basedir);
952         g_string_append (string, strv[i++]);
953         if (len == 6) {
954                 const gchar *variant;
955
956                 /* We include main.css by default */
957                 g_string_append_printf (string, "@import url(\"%s\");", css_path);
958                 g_string_append (string, strv[i++]);
959                 variant = tp_asv_get_string (data->info, "DefaultVariant");
960                 if (variant) {
961                         g_string_append (string, "Variants/");
962                         g_string_append (string, variant);
963                         g_string_append (string, ".css");
964                 }
965         } else {
966                 /* FIXME: We should set main.css OR the variant css */
967                 g_string_append (string, css_path);
968         }
969         g_string_append (string, strv[i++]);
970         g_string_append (string, ""); /* We don't want header */
971         g_string_append (string, strv[i++]);
972         /* FIXME: We should replace adium %macros% in footer */
973         if (footer_html) {
974                 g_string_append (string, footer_html);
975         }
976         g_string_append (string, strv[i++]);
977         data->template_html = g_string_free (string, FALSE);
978
979         g_free (footer_html);
980         g_free (template_html);
981         g_free (css_path);
982         g_strfreev (strv);
983
984         return data;
985 }
986
987 EmpathyAdiumData  *
988 empathy_adium_data_new (const gchar *path)
989 {
990         EmpathyAdiumData *data;
991         GHashTable *info;
992
993         info = empathy_adium_info_new (path);
994         data = empathy_adium_data_new_with_info (path, info);
995         g_hash_table_unref (info);
996
997         return data;
998 }
999
1000 EmpathyAdiumData  *
1001 empathy_adium_data_ref (EmpathyAdiumData *data)
1002 {
1003         g_return_val_if_fail (data != NULL, NULL);
1004
1005         data->ref_count++;
1006
1007         return data;
1008 }
1009
1010 void
1011 empathy_adium_data_unref (EmpathyAdiumData *data)
1012 {
1013         g_return_if_fail (data != NULL);
1014
1015         data->ref_count--;
1016         if (data->ref_count == 0) {
1017                 g_free (data->path);
1018                 g_free (data->basedir);
1019                 g_free (data->template_html);
1020                 g_free (data->in_content_html);
1021                 g_free (data->in_nextcontent_html);
1022                 g_free (data->out_content_html);
1023                 g_free (data->out_nextcontent_html);
1024                 g_free (data->default_avatar_filename);
1025                 g_free (data->default_incoming_avatar_filename);
1026                 g_free (data->default_outgoing_avatar_filename);
1027                 g_free (data->status_html);
1028                 g_hash_table_unref (data->info);
1029                 g_slice_free (EmpathyAdiumData, data);
1030         }
1031 }
1032
1033 GHashTable *
1034 empathy_adium_data_get_info (EmpathyAdiumData *data)
1035 {
1036         g_return_val_if_fail (data != NULL, NULL);
1037
1038         return data->info;
1039 }
1040
1041 const gchar *
1042 empathy_adium_data_get_path (EmpathyAdiumData *data)
1043 {
1044         g_return_val_if_fail (data != NULL, NULL);
1045
1046         return data->path;
1047 }
1048