1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <webkit/webkitnetworkrequest.h>
27 #include <libempathy/empathy-time.h>
28 #include <libempathy/empathy-utils.h>
30 #include "empathy-theme-adium.h"
31 #include "empathy-smiley-manager.h"
32 #include "empathy-conf.h"
33 #include "empathy-ui-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
36 #include <libempathy/empathy-debug.h>
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeAdium)
41 EmpathySmileyManager *smiley_manager;
42 EmpathyContact *last_contact;
45 gchar *default_avatar_filename;
46 gchar *in_content_html;
48 gchar *in_nextcontent_html;
49 gsize in_nextcontent_len;
50 gchar *out_content_html;
51 gsize out_content_len;
52 gchar *out_nextcontent_html;
53 gsize out_nextcontent_len;
54 } EmpathyThemeAdiumPriv;
56 static void theme_adium_iface_init (EmpathyChatViewIface *iface);
58 G_DEFINE_TYPE_WITH_CODE (EmpathyThemeAdium, empathy_theme_adium,
60 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
61 theme_adium_iface_init));
64 theme_adium_load (EmpathyThemeAdium *theme)
66 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
76 /* FIXME: Find a better way to get the theme dir */
77 basedir = g_build_filename (g_get_home_dir (), "Contents", "Resources", NULL);
80 file = g_build_filename (basedir, "Template.html", NULL);
81 g_file_get_contents (file, &template_html, &template_len, NULL);
84 file = g_build_filename (basedir, "Incoming", "Content.html", NULL);
85 g_file_get_contents (file, &priv->in_content_html, &priv->in_content_len, NULL);
88 file = g_build_filename (basedir, "Incoming", "NextContent.html", NULL);
89 g_file_get_contents (file, &priv->in_nextcontent_html, &priv->in_nextcontent_len, NULL);
92 file = g_build_filename (basedir, "Outgoing", "Content.html", NULL);
93 g_file_get_contents (file, &priv->out_content_html, &priv->out_content_len, NULL);
96 file = g_build_filename (basedir, "Outgoing", "NextContent.html", NULL);
97 g_file_get_contents (file, &priv->out_nextcontent_html, &priv->out_nextcontent_len, NULL);
100 css_path = g_build_filename (basedir, "main.css", NULL);
102 /* Replace %@ with the needed information in the template html */
103 strv = g_strsplit (template_html, "%@", 5);
104 string = g_string_sized_new (template_len);
105 g_string_append (string, strv[0]);
106 g_string_append (string, basedir);
107 g_string_append (string, strv[1]);
108 g_string_append (string, css_path);
109 g_string_append (string, strv[2]);
110 g_string_append (string, ""); /* We don't want header */
111 g_string_append (string, strv[3]);
112 g_string_append (string, ""); /* We have no footer */
113 g_string_append (string, strv[4]);
114 content = g_string_free (string, FALSE);
116 /* Load the template */
117 webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (theme),
122 g_free (template_html);
127 static WebKitNavigationResponse
128 theme_adium_navigation_requested_cb (WebKitWebView *view,
129 WebKitWebFrame *frame,
130 WebKitNetworkRequest *request,
135 uri = webkit_network_request_get_uri (request);
136 empathy_url_show (GTK_WIDGET (view), uri);
138 return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
142 theme_adium_escape_script (const gchar *text)
144 const gchar *cur = text;
147 string = g_string_sized_new (strlen (text));
148 while (!G_STR_EMPTY (cur)) {
151 g_string_append (string, "\\\\");
154 g_string_append (string, "\\\"");
157 g_string_append (string, "<br/>");
162 g_string_append_c (string, *cur);
167 return g_string_free (string, FALSE);
171 theme_adium_parse_body (EmpathyThemeAdium *theme,
174 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
175 gboolean use_smileys = FALSE;
180 GMatchInfo *match_info;
184 const gchar *cur = text;
186 empathy_conf_get_bool (empathy_conf_get (),
187 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
191 /* Replace smileys by a <img/> tag */
192 string = g_string_sized_new (strlen (cur));
193 smileys = empathy_smiley_manager_parse (priv->smiley_manager, cur);
194 for (l = smileys; l; l = l->next) {
195 EmpathySmiley *smiley;
199 g_string_append_printf (string,
200 "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
201 smiley->str, smiley->path, smiley->str);
203 g_string_append (string, smiley->str);
205 empathy_smiley_free (smiley);
207 g_slist_free (smileys);
210 cur = ret = g_string_free (string, FALSE);
213 /* Add <a href></a> arround links */
214 uri_regex = empathy_uri_regex_dup_singleton ();
215 match = g_regex_match (uri_regex, cur, 0, &match_info);
220 string = g_string_sized_new (strlen (cur));
222 g_match_info_fetch_pos (match_info, 0, &s, &e);
225 /* Append the text between last link (or the
226 * start of the message) and this link */
227 g_string_append_len (string, cur + last, s - last);
230 /* Append the link inside <a href=""></a> tag */
231 g_string_append (string, "<a href=\"");
232 g_string_append_len (string, cur + s, e - s);
233 g_string_append (string, "\">");
234 g_string_append_len (string, cur + s, e - s);
235 g_string_append (string, "</a>");
238 } while (g_match_info_next (match_info, NULL));
240 if (e < strlen (cur)) {
241 /* Append the text after the last link */
242 g_string_append_len (string, cur + e, strlen (cur) - e);
246 cur = ret = g_string_free (string, FALSE);
248 g_match_info_free (match_info);
249 g_regex_unref (uri_regex);
251 /* Replace \n by \r so it will be replaced by <br/> */
253 for (i = 0; iter[i] != '\0'; i++) {
254 if (iter[i] == '\n') {
256 /* We have changes to make to the string but we
257 * are still using the original one.
259 cur = iter = ret = g_strdup (cur);
265 /* If we made changes to the text ret is now a newly allocated string,
266 * otherwise it is NULL to indicate that the original text can be
267 * used without modification */
272 theme_adium_scroll_down (EmpathyChatView *view)
274 /* Not implemented */
277 #define FOLLOW(cur, str) (!strncmp (cur, str, strlen (str)))
279 theme_adium_append_message (EmpathyChatView *view,
282 EmpathyThemeAdium *theme = EMPATHY_THEME_ADIUM (view);
283 EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
284 EmpathyContact *sender;
285 gchar *dup_body = NULL;
288 EmpathyAvatar *avatar;
289 const gchar *avatar_filename = NULL;
299 if (!priv->page_loaded) {
300 priv->message_queue = g_list_prepend (priv->message_queue,
305 /* Get information */
306 sender = empathy_message_get_sender (msg);
307 timestamp = empathy_message_get_timestamp (msg);
308 body = empathy_message_get_body (msg);
309 dup_body = theme_adium_parse_body (theme, body);
313 name = empathy_contact_get_name (sender);
314 avatar = empathy_contact_get_avatar (sender);
316 avatar_filename = avatar->filename;
318 if (!avatar_filename) {
319 if (!priv->default_avatar_filename) {
320 priv->default_avatar_filename =
321 empathy_filename_from_icon_name ("stock_person",
322 GTK_ICON_SIZE_DIALOG);
324 avatar_filename = priv->default_avatar_filename;
327 /* Get the right html/func to add the message */
328 if (priv->last_contact &&
329 empathy_contact_equal (priv->last_contact, sender)) {
330 func = "appendNextMessage";
331 if (empathy_contact_is_user (sender)) {
332 cur = priv->out_nextcontent_html;
333 len = priv->out_nextcontent_len;
336 cur = priv->in_nextcontent_html;
337 len = priv->in_nextcontent_len;
341 func = "appendMessage";
342 if (empathy_contact_is_user (sender)) {
343 cur = priv->out_content_html;
344 len = priv->out_content_len;
347 cur = priv->in_content_html;
348 len = priv->in_content_len;
352 /* Make some search-and-replace in the html code */
354 string = g_string_sized_new (len + strlen (body));
355 while ((cur = strchr (cur, '%'))) {
356 const gchar *replace = NULL;
357 gchar *dup_replace = NULL;
360 if (FOLLOW (cur, "%message%")) {
362 } else if (FOLLOW (cur, "%userIconPath%")) {
363 replace = avatar_filename;
364 } else if (FOLLOW (cur, "%sender%")) {
366 } else if (FOLLOW (cur, "%time")) {
367 gchar *format = NULL;
371 /* Extract the time format it provided. */
372 if (*(start = cur + strlen("%time")) == '{') {
374 end = strstr (start, "}%");
375 if (!end) /* Invalid string */
377 format = g_strndup (start, end - start);
381 dup_replace = empathy_time_to_string_local (timestamp,
382 format ? format : EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
383 replace = dup_replace;
390 /* Here we have a replacement to make */
391 g_string_append_len (string, prev, cur - prev);
392 g_string_append (string, replace);
393 g_free (dup_replace);
395 /* And update the pointers */
397 prev = cur = fin + 1;
399 prev = cur = strchr (cur + 1, '%') + 1;
402 g_string_append (string, prev);
404 /* Execute a js to add the message */
405 cur = g_string_free (string, FALSE);
406 escape = theme_adium_escape_script (cur);
407 script = g_strdup_printf("%s(\"%s\")", func, escape);
408 webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view), script);
410 /* Keep the sender of the last displayed message */
411 if (priv->last_contact) {
412 g_object_unref (priv->last_contact);
414 priv->last_contact = g_object_ref (sender);
423 theme_adium_append_event (EmpathyChatView *view,
426 /* Not implemented */
430 theme_adium_scroll (EmpathyChatView *view,
431 gboolean allow_scrolling)
433 /* Not implemented */
437 theme_adium_get_has_selection (EmpathyChatView *view)
439 /* Not implemented */
444 theme_adium_clear (EmpathyChatView *view)
446 /* Not implemented */
450 theme_adium_find_previous (EmpathyChatView *view,
451 const gchar *search_criteria,
454 /* Not implemented */
459 theme_adium_find_next (EmpathyChatView *view,
460 const gchar *search_criteria,
463 /* Not implemented */
468 theme_adium_find_abilities (EmpathyChatView *view,
469 const gchar *search_criteria,
470 gboolean *can_do_previous,
471 gboolean *can_do_next)
473 /* Not implemented */
477 theme_adium_highlight (EmpathyChatView *view,
480 /* Not implemented */
484 theme_adium_copy_clipboard (EmpathyChatView *view)
486 /* Not implemented */
490 theme_adium_iface_init (EmpathyChatViewIface *iface)
492 iface->append_message = theme_adium_append_message;
493 iface->append_event = theme_adium_append_event;
494 iface->scroll = theme_adium_scroll;
495 iface->scroll_down = theme_adium_scroll_down;
496 iface->get_has_selection = theme_adium_get_has_selection;
497 iface->clear = theme_adium_clear;
498 iface->find_previous = theme_adium_find_previous;
499 iface->find_next = theme_adium_find_next;
500 iface->find_abilities = theme_adium_find_abilities;
501 iface->highlight = theme_adium_highlight;
502 iface->copy_clipboard = theme_adium_copy_clipboard;
506 theme_adium_load_finished_cb (WebKitWebView *view,
507 WebKitWebFrame *frame,
510 EmpathyThemeAdiumPriv *priv = GET_PRIV (view);
511 EmpathyChatView *chat_view = EMPATHY_CHAT_VIEW (view);
513 DEBUG ("Page loaded");
514 priv->page_loaded = TRUE;
516 /* Display queued messages */
517 priv->message_queue = g_list_reverse (priv->message_queue);
518 while (priv->message_queue) {
519 EmpathyMessage *message = priv->message_queue->data;
521 theme_adium_append_message (chat_view, message);
522 priv->message_queue = g_list_remove (priv->message_queue, message);
523 g_object_unref (message);
528 theme_adium_finalize (GObject *object)
530 EmpathyThemeAdiumPriv *priv = GET_PRIV (object);
532 g_free (priv->in_content_html);
533 g_free (priv->in_nextcontent_html);
534 g_free (priv->out_content_html);
535 g_free (priv->out_nextcontent_html);
536 g_free (priv->default_avatar_filename);
537 g_object_unref (priv->smiley_manager);
539 if (priv->last_contact) {
540 g_object_unref (priv->last_contact);
543 G_OBJECT_CLASS (empathy_theme_adium_parent_class)->finalize (object);
547 empathy_theme_adium_class_init (EmpathyThemeAdiumClass *klass)
549 GObjectClass *object_class = G_OBJECT_CLASS (klass);
551 object_class->finalize = theme_adium_finalize;
553 g_type_class_add_private (object_class, sizeof (EmpathyThemeAdiumPriv));
557 empathy_theme_adium_init (EmpathyThemeAdium *theme)
559 EmpathyThemeAdiumPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
560 EMPATHY_TYPE_THEME_ADIUM, EmpathyThemeAdiumPriv);
564 priv->smiley_manager = empathy_smiley_manager_new ();
566 g_signal_connect (theme, "load-finished",
567 G_CALLBACK (theme_adium_load_finished_cb),
569 g_signal_connect (theme, "navigation-requested",
570 G_CALLBACK (theme_adium_navigation_requested_cb),
574 theme_adium_load (theme);
578 empathy_theme_adium_new (void)
580 return g_object_new (EMPATHY_TYPE_THEME_ADIUM, NULL);
584 empathy_theme_adium_is_valid (const gchar *path)