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