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