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