]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-text-view.c
add myself to AUTHORS
[empathy.git] / libempathy-gtk / empathy-chat-text-view.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Richard Hult <richard@imendio.com>
23  *          Martyn Russell <martyn@imendio.com>
24  *          Xavier Claessens <xclaesse@gmail.com>
25  */
26
27 #include "config.h"
28
29 #include <sys/types.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include <glib/gi18n-lib.h>
34 #include <gtk/gtk.h>
35
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.h>
39
40 #include "empathy-chat-text-view.h"
41 #include "empathy-chat.h"
42 #include "empathy-conf.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-smiley-manager.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
47 #include <libempathy/empathy-debug.h>
48
49 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
50 #define TIMESTAMP_INTERVAL 300
51
52 #define MAX_LINES 800
53 #define MAX_SCROLL_TIME 0.4 /* seconds */
54 #define SCROLL_DELAY 33     /* milliseconds */
55
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
57
58 typedef struct {
59         GtkTextBuffer        *buffer;
60         guint                 scroll_timeout;
61         GTimer               *scroll_time;
62         GtkTextMark          *find_mark_previous;
63         GtkTextMark          *find_mark_next;
64         gboolean              find_wrapped;
65         gboolean              find_last_direction;
66         EmpathyContact       *last_contact;
67         time_t                last_timestamp;
68         gboolean              allow_scrolling;
69         guint                 notify_system_fonts_id;
70         EmpathySmileyManager *smiley_manager;
71         gboolean              only_if_date;
72 } EmpathyChatTextViewPriv;
73
74 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
75
76 static void chat_text_view_copy_clipboard (EmpathyChatView *view);
77
78 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
79                          GTK_TYPE_TEXT_VIEW,
80                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
81                                                 chat_text_view_iface_init));
82
83 enum {
84         PROP_0,
85         PROP_LAST_CONTACT,
86         PROP_ONLY_IF_DATE
87 };
88
89 static gboolean
90 chat_text_view_url_event_cb (GtkTextTag          *tag,
91                              GObject             *object,
92                              GdkEvent            *event,
93                              GtkTextIter         *iter,
94                              EmpathyChatTextView *view)
95 {
96         EmpathyChatTextViewPriv *priv;
97         GtkTextIter              start, end;
98         gchar                   *str;
99
100         priv = GET_PRIV (view);
101
102         /* If the link is being selected, don't do anything. */
103         gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
104         if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
105                 return FALSE;
106         }
107
108         if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
109                 start = end = *iter;
110
111                 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
112                     gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
113                             str = gtk_text_buffer_get_text (priv->buffer,
114                                                             &start,
115                                                             &end,
116                                                             FALSE);
117
118                             empathy_url_show (GTK_WIDGET (view), str);
119                             g_free (str);
120                     }
121         }
122
123         return FALSE;
124 }
125
126 static gboolean
127 chat_text_view_event_cb (EmpathyChatTextView *view,
128                          GdkEventMotion      *event,
129                          GtkTextTag          *tag)
130 {
131         static GdkCursor  *hand = NULL;
132         static GdkCursor  *beam = NULL;
133         GtkTextWindowType  type;
134         GtkTextIter        iter;
135         GdkWindow         *win;
136         gint               x, y, buf_x, buf_y;
137
138         type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
139                                               event->window);
140
141         if (type != GTK_TEXT_WINDOW_TEXT) {
142                 return FALSE;
143         }
144
145         /* Get where the pointer really is. */
146         win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
147         if (!win) {
148                 return FALSE;
149         }
150
151         gdk_window_get_pointer (win, &x, &y, NULL);
152
153         /* Get the iter where the cursor is at */
154         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
155                                                x, y,
156                                                &buf_x, &buf_y);
157
158         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
159                                             &iter,
160                                             buf_x, buf_y);
161
162         if (gtk_text_iter_has_tag (&iter, tag)) {
163                 if (!hand) {
164                         hand = gdk_cursor_new (GDK_HAND2);
165                         beam = gdk_cursor_new (GDK_XTERM);
166                 }
167                 gdk_window_set_cursor (win, hand);
168         } else {
169                 if (!beam) {
170                         beam = gdk_cursor_new (GDK_XTERM);
171                 }
172                 gdk_window_set_cursor (win, beam);
173         }
174
175         return FALSE;
176 }
177
178 static void
179 chat_text_view_create_tags (EmpathyChatTextView *view)
180 {
181         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
182         GtkTextTag              *tag;
183
184         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
185         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
186         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
187         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
188         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
189         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
190         gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
191
192         tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
193         g_signal_connect (tag, "event",
194                           G_CALLBACK (chat_text_view_url_event_cb),
195                           view);
196
197         g_signal_connect (view, "motion-notify-event",
198                           G_CALLBACK (chat_text_view_event_cb),
199                           tag);
200 }
201
202 static void
203 chat_text_view_system_font_update (EmpathyChatTextView *view)
204 {
205         PangoFontDescription *font_description = NULL;
206         gchar                *font_name;
207
208         if (empathy_conf_get_string (empathy_conf_get (),
209                                      "/desktop/gnome/interface/document_font_name",
210                                      &font_name) && font_name) {
211                                              font_description = pango_font_description_from_string (font_name);
212                                              g_free (font_name);
213                                      } else {
214                                              font_description = NULL;
215                                      }
216
217         gtk_widget_modify_font (GTK_WIDGET (view), font_description);
218
219         if (font_description) {
220                 pango_font_description_free (font_description);
221         }
222 }
223
224 static void
225 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
226                                       const gchar *key,
227                                       gpointer     user_data)
228 {
229         EmpathyChatTextView *view = user_data;
230
231         chat_text_view_system_font_update (view);
232 }
233
234 static void
235 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
236 {
237         empathy_url_show (GTK_WIDGET (menuitem), url);
238 }
239
240 static void
241 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
242 {
243         GtkClipboard *clipboard;
244
245         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
246         gtk_clipboard_set_text (clipboard, url, -1);
247
248         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
249         gtk_clipboard_set_text (clipboard, url, -1);
250 }
251
252 static void
253 chat_text_view_populate_popup (EmpathyChatTextView *view,
254                                GtkMenu        *menu,
255                                gpointer        user_data)
256 {
257         EmpathyChatTextViewPriv *priv;
258         GtkTextTagTable    *table;
259         GtkTextTag         *tag;
260         gint                x, y;
261         GtkTextIter         iter, start, end;
262         GtkWidget          *item;
263         gchar              *str = NULL;
264
265         priv = GET_PRIV (view);
266
267         /* Clear menu item */
268         if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
269                 item = gtk_separator_menu_item_new ();
270                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
271                 gtk_widget_show (item);
272
273                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
274                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
275                 gtk_widget_show (item);
276
277                 g_signal_connect_swapped (item, "activate",
278                                           G_CALLBACK (empathy_chat_view_clear),
279                                           view);
280         }
281
282         /* Link context menu items */
283         table = gtk_text_buffer_get_tag_table (priv->buffer);
284         tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
285
286         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
287
288         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
289                                                GTK_TEXT_WINDOW_WIDGET,
290                                                x, y,
291                                                &x, &y);
292
293         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
294
295         start = end = iter;
296
297         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
298             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
299                     str = gtk_text_buffer_get_text (priv->buffer,
300                                                     &start, &end, FALSE);
301             }
302
303         if (EMP_STR_EMPTY (str)) {
304                 g_free (str);
305                 return;
306         }
307
308         /* NOTE: Set data just to get the string freed when not needed. */
309         g_object_set_data_full (G_OBJECT (menu),
310                                 "url", str,
311                                 (GDestroyNotify) g_free);
312
313         item = gtk_separator_menu_item_new ();
314         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
315         gtk_widget_show (item);
316
317         item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
318         g_signal_connect (item, "activate",
319                           G_CALLBACK (chat_text_view_copy_address_cb),
320                           str);
321         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
322         gtk_widget_show (item);
323
324         item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
325         g_signal_connect (item, "activate",
326                           G_CALLBACK (chat_text_view_open_address_cb),
327                           str);
328         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329         gtk_widget_show (item);
330 }
331
332 static gboolean
333 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
334 {
335         GtkWidget *sw;
336
337         sw = gtk_widget_get_parent (GTK_WIDGET (view));
338         if (GTK_IS_SCROLLED_WINDOW (sw)) {
339                 GtkAdjustment *vadj;
340                 gdouble value;
341                 gdouble upper;
342                 gdouble page_size;
343
344                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
345                 value = gtk_adjustment_get_value (vadj);
346                 upper = gtk_adjustment_get_upper (vadj);
347                 page_size = gtk_adjustment_get_page_size (vadj);
348
349                 if (value < upper - page_size) {
350                         return FALSE;
351                 }
352         }
353
354         return TRUE;
355 }
356
357 static void
358 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
359 {
360         EmpathyChatTextViewPriv *priv;
361         GtkTextIter         top, bottom;
362         gint                line;
363         gint                remove_;
364         GtkTextTagTable    *table;
365         GtkTextTag         *tag;
366
367         priv = GET_PRIV (view);
368
369         gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
370         line = gtk_text_iter_get_line (&bottom);
371         if (line < MAX_LINES) {
372                 return;
373         }
374
375         remove_ = line - MAX_LINES;
376         gtk_text_buffer_get_start_iter (priv->buffer, &top);
377
378         bottom = top;
379         if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
380                 return;
381         }
382
383         /* Track backwords to a place where we can safely cut, we don't do it in
384           * the middle of a tag.
385           */
386         table = gtk_text_buffer_get_tag_table (priv->buffer);
387         tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
388         if (!tag) {
389                 return;
390         }
391
392         if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
393                 return;
394         }
395
396         if (!gtk_text_iter_equal (&top, &bottom)) {
397                 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
398         }
399 }
400
401 static void
402 chat_text_view_append_timestamp (EmpathyChatTextView *view,
403                                  time_t               timestamp,
404                                  gboolean             show_date)
405 {
406         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
407         GtkTextIter              iter;
408         gchar                   *tmp;
409         GString                 *str;
410
411         str = g_string_new ("- ");
412
413         /* Append date if needed */
414         if (show_date) {
415                 GDate *date;
416                 gchar  buf[256];
417
418                 date = g_date_new ();
419                 g_date_set_time_t (date, timestamp);
420                 /* Translators: timestamp displayed between conversations in
421                  * chat windows (strftime format string) */
422                 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
423                 g_string_append (str, buf);
424                 g_string_append (str, ", ");
425                 g_date_free (date);
426         }
427
428         /* Append time */
429         tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
430         g_string_append (str, tmp);
431         g_free (tmp);
432
433         g_string_append (str, " -\n");
434
435         /* Insert the string in the buffer */
436         empathy_chat_text_view_append_spacing (view);
437         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
438         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
439                                                   &iter,
440                                                   str->str, -1,
441                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
442                                                   NULL);
443
444         priv->last_timestamp = timestamp;
445
446         g_string_free (str, TRUE);
447 }
448
449 static void
450 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
451                                       time_t               timestamp)
452 {
453         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
454         GDate                   *date, *last_date;
455         gboolean                 append_date = FALSE;
456         gboolean                 append_time = FALSE;
457
458         /* Get the date from last message */
459         last_date = g_date_new ();
460         g_date_set_time_t (last_date, priv->last_timestamp);
461
462         /* Get the date of the message we are appending */
463         date = g_date_new ();
464         g_date_set_time_t (date, timestamp);
465
466         /* If last message was from another day we append date and time */
467         if (g_date_compare (date, last_date) > 0) {
468                 append_date = TRUE;
469                 append_time = TRUE;
470         }
471
472         g_date_free (last_date);
473         g_date_free (date);
474
475         /* If last message is 'old' append the time */
476         if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
477                 append_time = TRUE;
478         }
479
480         if (append_date || (!priv->only_if_date && append_time)) {
481                 chat_text_view_append_timestamp (view, timestamp, append_date);
482         }
483 }
484
485 static void
486 chat_text_view_size_allocate (GtkWidget     *widget,
487                               GtkAllocation *alloc)
488 {
489         gboolean down;
490
491         down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
492
493         GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
494
495         if (down) {
496                 GtkAdjustment *adj;
497
498                 adj = GTK_TEXT_VIEW (widget)->vadjustment;
499                 gtk_adjustment_set_value (adj,
500                                           gtk_adjustment_get_upper (adj) -
501                                           gtk_adjustment_get_page_size (adj));
502         }
503 }
504
505 static gboolean
506 chat_text_view_drag_motion (GtkWidget      *widget,
507                             GdkDragContext *context,
508                             gint            x,
509                             gint            y,
510                             guint           time_)
511 {
512         /* Don't handle drag motion, since we don't want the view to scroll as
513          * the result of dragging something across it. */
514
515         return FALSE;
516 }
517
518 static void
519 chat_text_view_get_property (GObject    *object,
520                              guint       param_id,
521                              GValue     *value,
522                              GParamSpec *pspec)
523 {
524         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
525
526         switch (param_id) {
527         case PROP_LAST_CONTACT:
528                 g_value_set_object (value, priv->last_contact);
529                 break;
530         case PROP_ONLY_IF_DATE:
531                 g_value_set_boolean (value, priv->only_if_date);
532                 break;
533         default:
534                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
535                 break;
536         };
537 }
538
539 static void
540 chat_text_view_set_property (GObject      *object,
541                              guint         param_id,
542                              const GValue *value,
543                              GParamSpec   *pspec)
544 {
545         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
546
547         switch (param_id) {
548         case PROP_ONLY_IF_DATE:
549                 priv->only_if_date = g_value_get_boolean (value);
550                 break;
551         default:
552                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
553                 break;
554         };
555 }
556
557 static void
558 chat_text_view_finalize (GObject *object)
559 {
560         EmpathyChatTextView     *view;
561         EmpathyChatTextViewPriv *priv;
562
563         view = EMPATHY_CHAT_TEXT_VIEW (object);
564         priv = GET_PRIV (view);
565
566         DEBUG ("%p", object);
567
568         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
569
570         if (priv->last_contact) {
571                 g_object_unref (priv->last_contact);
572         }
573         if (priv->scroll_time) {
574                 g_timer_destroy (priv->scroll_time);
575         }
576         if (priv->scroll_timeout) {
577                 g_source_remove (priv->scroll_timeout);
578         }
579         g_object_unref (priv->smiley_manager);
580
581         G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
582 }
583
584 static void
585 text_view_copy_clipboard (GtkTextView *text_view)
586 {
587         chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
588 }
589
590 static void
591 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
592 {
593         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
594         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
595         GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
596
597         object_class->finalize = chat_text_view_finalize;
598         object_class->get_property = chat_text_view_get_property;
599         object_class->set_property = chat_text_view_set_property;
600
601         widget_class->size_allocate = chat_text_view_size_allocate;
602         widget_class->drag_motion = chat_text_view_drag_motion;
603
604         text_view_class->copy_clipboard = text_view_copy_clipboard;
605
606         g_object_class_install_property (object_class,
607                                          PROP_LAST_CONTACT,
608                                          g_param_spec_object ("last-contact",
609                                                               "Last contact",
610                                                               "The sender of the last received message",
611                                                               EMPATHY_TYPE_CONTACT,
612                                                               G_PARAM_READABLE));
613         g_object_class_install_property (object_class,
614                                          PROP_ONLY_IF_DATE,
615                                          g_param_spec_boolean ("only-if-date",
616                                                               "Only if date",
617                                                               "Display timestamp only if the date changes",
618                                                               FALSE,
619                                                               G_PARAM_READWRITE));
620
621
622         g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
623 }
624
625 static void
626 empathy_chat_text_view_init (EmpathyChatTextView *view)
627 {
628         EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
629                 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
630
631         view->priv = priv;
632         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
633         priv->last_timestamp = 0;
634         priv->allow_scrolling = TRUE;
635         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
636
637         g_object_set (view,
638                       "wrap-mode", GTK_WRAP_WORD_CHAR,
639                       "editable", FALSE,
640                       "cursor-visible", FALSE,
641                       NULL);
642
643         priv->notify_system_fonts_id =
644                 empathy_conf_notify_add (empathy_conf_get (),
645                                          "/desktop/gnome/interface/document_font_name",
646                                          chat_text_view_notify_system_font_cb,
647                                          view);
648         chat_text_view_system_font_update (view);
649         chat_text_view_create_tags (view);
650
651         g_signal_connect (view,
652                           "populate-popup",
653                           G_CALLBACK (chat_text_view_populate_popup),
654                           NULL);
655 }
656
657 /* Code stolen from pidgin/gtkimhtml.c */
658 static gboolean
659 chat_text_view_scroll_cb (EmpathyChatTextView *view)
660 {
661         EmpathyChatTextViewPriv *priv;
662         GtkAdjustment      *adj;
663         gdouble             max_val;
664
665         priv = GET_PRIV (view);
666         adj = GTK_TEXT_VIEW (view)->vadjustment;
667         max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
668
669         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
670
671         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
672                 /* time's up. jump to the end and kill the timer */
673                 gtk_adjustment_set_value (adj, max_val);
674                 g_timer_destroy (priv->scroll_time);
675                 priv->scroll_time = NULL;
676                 priv->scroll_timeout = 0;
677                 return FALSE;
678         }
679
680         /* scroll by 1/3rd the remaining distance */
681         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
682         return TRUE;
683 }
684
685 static void
686 chat_text_view_scroll_down (EmpathyChatView *view)
687 {
688         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
689
690         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
691
692         if (!priv->allow_scrolling) {
693                 return;
694         }
695
696         DEBUG ("Scrolling down");
697
698         if (priv->scroll_time) {
699                 g_timer_reset (priv->scroll_time);
700         } else {
701                 priv->scroll_time = g_timer_new ();
702         }
703         if (!priv->scroll_timeout) {
704                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
705                                                       (GSourceFunc) chat_text_view_scroll_cb,
706                                                       view);
707         }
708 }
709
710 static void
711 chat_text_view_append_message (EmpathyChatView *view,
712                                EmpathyMessage  *msg)
713 {
714         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
715         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
716         gboolean                 bottom;
717         time_t                   timestamp;
718
719         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
720         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
721
722         if (!empathy_message_get_body (msg)) {
723                 return;
724         }
725
726         bottom = chat_text_view_is_scrolled_down (text_view);
727
728         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
729
730         timestamp = empathy_message_get_timestamp (msg);
731         chat_text_maybe_append_date_and_time (text_view, timestamp);
732         if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
733                 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
734                                                                          msg);
735         }
736
737         if (bottom) {
738                 chat_text_view_scroll_down (view);
739         }
740
741         if (priv->last_contact) {
742                 g_object_unref (priv->last_contact);
743         }
744         priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
745         g_object_notify (G_OBJECT (view), "last-contact");
746 }
747
748 static void
749 chat_text_view_append_event (EmpathyChatView *view,
750                              const gchar     *str)
751 {
752         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
753         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
754         gboolean                 bottom;
755         GtkTextIter              iter;
756         gchar                   *msg;
757
758
759         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
760         g_return_if_fail (!EMP_STR_EMPTY (str));
761
762         bottom = chat_text_view_is_scrolled_down (text_view);
763         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
764         chat_text_maybe_append_date_and_time (text_view,
765                                               empathy_time_get_current ());
766
767         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
768         msg = g_strdup_printf (" - %s\n", str);
769         gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
770                                                   msg, -1,
771                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
772                                                   NULL);
773         g_free (msg);
774
775         if (bottom) {
776                 chat_text_view_scroll_down (view);
777         }
778
779         if (priv->last_contact) {
780                 g_object_unref (priv->last_contact);
781                 priv->last_contact = NULL;
782                 g_object_notify (G_OBJECT (view), "last-contact");
783         }
784 }
785
786 static void
787 chat_text_view_scroll (EmpathyChatView *view,
788                        gboolean         allow_scrolling)
789 {
790         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
791
792         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
793
794         DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
795
796         priv->allow_scrolling = allow_scrolling;
797         if (allow_scrolling) {
798                 empathy_chat_view_scroll_down (view);
799         }
800 }
801
802 static gboolean
803 chat_text_view_get_has_selection (EmpathyChatView *view)
804 {
805         GtkTextBuffer *buffer;
806
807         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
808
809         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
810
811         return gtk_text_buffer_get_has_selection (buffer);
812 }
813
814 static void
815 chat_text_view_clear (EmpathyChatView *view)
816 {
817         GtkTextBuffer      *buffer;
818         EmpathyChatTextViewPriv *priv;
819
820         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
821
822         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
823         gtk_text_buffer_set_text (buffer, "", -1);
824
825         /* We set these back to the initial values so we get
826           * timestamps when clearing the window to know when
827           * conversations start.
828           */
829         priv = GET_PRIV (view);
830
831         priv->last_timestamp = 0;
832         if (priv->last_contact) {
833                 g_object_unref (priv->last_contact);
834                 priv->last_contact = NULL;
835         }
836 }
837
838 static gboolean
839 chat_text_view_find_previous (EmpathyChatView *view,
840                                 const gchar     *search_criteria,
841                                 gboolean         new_search)
842 {
843         EmpathyChatTextViewPriv *priv;
844         GtkTextBuffer      *buffer;
845         GtkTextIter         iter_at_mark;
846         GtkTextIter         iter_match_start;
847         GtkTextIter         iter_match_end;
848         gboolean            found;
849         gboolean            from_start = FALSE;
850
851         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
852         g_return_val_if_fail (search_criteria != NULL, FALSE);
853
854         priv = GET_PRIV (view);
855
856         buffer = priv->buffer;
857
858         if (EMP_STR_EMPTY (search_criteria)) {
859                 if (priv->find_mark_previous) {
860                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
861
862                         gtk_text_buffer_move_mark (buffer,
863                                                    priv->find_mark_previous,
864                                                    &iter_at_mark);
865                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
866                                                       priv->find_mark_previous,
867                                                       0.0,
868                                                       TRUE,
869                                                       0.0,
870                                                       0.0);
871                         gtk_text_buffer_select_range (buffer,
872                                                       &iter_at_mark,
873                                                       &iter_at_mark);
874                 }
875
876                 return FALSE;
877         }
878
879         if (new_search) {
880                 from_start = TRUE;
881         }
882
883         if (priv->find_mark_previous) {
884                 gtk_text_buffer_get_iter_at_mark (buffer,
885                                                   &iter_at_mark,
886                                                   priv->find_mark_previous);
887         } else {
888                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
889                 from_start = TRUE;
890         }
891
892         priv->find_last_direction = FALSE;
893
894         found = empathy_text_iter_backward_search (&iter_at_mark,
895                                                    search_criteria,
896                                                    &iter_match_start,
897                                                    &iter_match_end,
898                                                    NULL);
899
900         if (!found) {
901                 gboolean result = FALSE;
902
903                 if (from_start) {
904                         return result;
905                 }
906
907                 /* Here we wrap around. */
908                 if (!new_search && !priv->find_wrapped) {
909                         priv->find_wrapped = TRUE;
910                         result = chat_text_view_find_previous (view,
911                                                                  search_criteria,
912                                                                  FALSE);
913                         priv->find_wrapped = FALSE;
914                 }
915
916                 return result;
917         }
918
919         /* Set new mark and show on screen */
920         if (!priv->find_mark_previous) {
921                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
922                                                                         &iter_match_start,
923                                                                         TRUE);
924         } else {
925                 gtk_text_buffer_move_mark (buffer,
926                                            priv->find_mark_previous,
927                                            &iter_match_start);
928         }
929
930         if (!priv->find_mark_next) {
931                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
932                                                                     &iter_match_end,
933                                                                     TRUE);
934         } else {
935                 gtk_text_buffer_move_mark (buffer,
936                                            priv->find_mark_next,
937                                            &iter_match_end);
938         }
939
940         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
941                                       priv->find_mark_previous,
942                                       0.0,
943                                       TRUE,
944                                       0.5,
945                                       0.5);
946
947         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
948         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
949
950         return TRUE;
951 }
952
953 static gboolean
954 chat_text_view_find_next (EmpathyChatView *view,
955                             const gchar     *search_criteria,
956                             gboolean         new_search)
957 {
958         EmpathyChatTextViewPriv *priv;
959         GtkTextBuffer      *buffer;
960         GtkTextIter         iter_at_mark;
961         GtkTextIter         iter_match_start;
962         GtkTextIter         iter_match_end;
963         gboolean            found;
964         gboolean            from_start = FALSE;
965
966         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
967         g_return_val_if_fail (search_criteria != NULL, FALSE);
968
969         priv = GET_PRIV (view);
970
971         buffer = priv->buffer;
972
973         if (EMP_STR_EMPTY (search_criteria)) {
974                 if (priv->find_mark_next) {
975                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
976
977                         gtk_text_buffer_move_mark (buffer,
978                                                    priv->find_mark_next,
979                                                    &iter_at_mark);
980                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
981                                                       priv->find_mark_next,
982                                                       0.0,
983                                                       TRUE,
984                                                       0.0,
985                                                       0.0);
986                         gtk_text_buffer_select_range (buffer,
987                                                       &iter_at_mark,
988                                                       &iter_at_mark);
989                 }
990
991                 return FALSE;
992         }
993
994         if (new_search) {
995                 from_start = TRUE;
996         }
997
998         if (priv->find_mark_next) {
999                 gtk_text_buffer_get_iter_at_mark (buffer,
1000                                                   &iter_at_mark,
1001                                                   priv->find_mark_next);
1002         } else {
1003                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1004                 from_start = TRUE;
1005         }
1006
1007         priv->find_last_direction = TRUE;
1008
1009         found = empathy_text_iter_forward_search (&iter_at_mark,
1010                                                   search_criteria,
1011                                                   &iter_match_start,
1012                                                   &iter_match_end,
1013                                                   NULL);
1014
1015         if (!found) {
1016                 gboolean result = FALSE;
1017
1018                 if (from_start) {
1019                         return result;
1020                 }
1021
1022                 /* Here we wrap around. */
1023                 if (!new_search && !priv->find_wrapped) {
1024                         priv->find_wrapped = TRUE;
1025                         result = chat_text_view_find_next (view,
1026                                                              search_criteria,
1027                                                              FALSE);
1028                         priv->find_wrapped = FALSE;
1029                 }
1030
1031                 return result;
1032         }
1033
1034         /* Set new mark and show on screen */
1035         if (!priv->find_mark_next) {
1036                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1037                                                                     &iter_match_end,
1038                                                                     TRUE);
1039         } else {
1040                 gtk_text_buffer_move_mark (buffer,
1041                                            priv->find_mark_next,
1042                                            &iter_match_end);
1043         }
1044
1045         if (!priv->find_mark_previous) {
1046                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1047                                                                         &iter_match_start,
1048                                                                         TRUE);
1049         } else {
1050                 gtk_text_buffer_move_mark (buffer,
1051                                            priv->find_mark_previous,
1052                                            &iter_match_start);
1053         }
1054
1055         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1056                                       priv->find_mark_next,
1057                                       0.0,
1058                                       TRUE,
1059                                       0.5,
1060                                       0.5);
1061
1062         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1063         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1064
1065         return TRUE;
1066 }
1067
1068 static void
1069 chat_text_view_find_abilities (EmpathyChatView *view,
1070                                  const gchar    *search_criteria,
1071                                  gboolean       *can_do_previous,
1072                                  gboolean       *can_do_next)
1073 {
1074         EmpathyChatTextViewPriv *priv;
1075         GtkTextBuffer           *buffer;
1076         GtkTextIter              iter_at_mark;
1077         GtkTextIter              iter_match_start;
1078         GtkTextIter              iter_match_end;
1079
1080         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1081         g_return_if_fail (search_criteria != NULL);
1082         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1083
1084         priv = GET_PRIV (view);
1085
1086         buffer = priv->buffer;
1087
1088         if (can_do_previous) {
1089                 if (priv->find_mark_previous) {
1090                         gtk_text_buffer_get_iter_at_mark (buffer,
1091                                                           &iter_at_mark,
1092                                                           priv->find_mark_previous);
1093                 } else {
1094                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1095                 }
1096
1097                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1098                                                                       search_criteria,
1099                                                                       &iter_match_start,
1100                                                                       &iter_match_end,
1101                                                                       NULL);
1102         }
1103
1104         if (can_do_next) {
1105                 if (priv->find_mark_next) {
1106                         gtk_text_buffer_get_iter_at_mark (buffer,
1107                                                           &iter_at_mark,
1108                                                           priv->find_mark_next);
1109                 } else {
1110                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1111                 }
1112
1113                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1114                                                                  search_criteria,
1115                                                                  &iter_match_start,
1116                                                                  &iter_match_end,
1117                                                                  NULL);
1118         }
1119 }
1120
1121 static void
1122 chat_text_view_highlight (EmpathyChatView *view,
1123                             const gchar     *text)
1124 {
1125         GtkTextBuffer *buffer;
1126         GtkTextIter    iter;
1127         GtkTextIter    iter_start;
1128         GtkTextIter    iter_end;
1129         GtkTextIter    iter_match_start;
1130         GtkTextIter    iter_match_end;
1131         gboolean       found;
1132
1133         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1134
1135         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1136
1137         gtk_text_buffer_get_start_iter (buffer, &iter);
1138
1139         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1140         gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1141                                             &iter_start,
1142                                             &iter_end);
1143
1144         if (EMP_STR_EMPTY (text)) {
1145                 return;
1146         }
1147
1148         while (1) {
1149                 found = empathy_text_iter_forward_search (&iter,
1150                                                           text,
1151                                                           &iter_match_start,
1152                                                           &iter_match_end,
1153                                                           NULL);
1154
1155                 if (!found) {
1156                         break;
1157                 }
1158
1159                 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1160                                                    &iter_match_start,
1161                                                    &iter_match_end);
1162
1163                 iter = iter_match_end;
1164                 gtk_text_iter_forward_char (&iter);
1165         }
1166 }
1167
1168 static void
1169 chat_text_view_copy_clipboard (EmpathyChatView *view)
1170 {
1171         GtkTextBuffer *buffer;
1172         GtkTextIter start, iter, end;
1173         GtkClipboard  *clipboard;
1174         GdkPixbuf *pixbuf;
1175         gunichar c;
1176         GtkTextChildAnchor *anchor = NULL;
1177         GString *str;
1178         GList *list;
1179         gboolean ignore_newlines = FALSE;
1180
1181         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1182
1183         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1184         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1185
1186         if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1187                 return;
1188
1189         str = g_string_new ("");
1190
1191         for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1192                 c = gtk_text_iter_get_char (&iter);
1193                 /* 0xFFFC is the 'object replacement' unicode character,
1194                  * it indicates the presence of a pixbuf or a widget. */
1195                 if (c == 0xFFFC) {
1196                         ignore_newlines = FALSE;
1197                         if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1198                                 gchar *text;
1199                                 text = g_object_get_data (G_OBJECT(pixbuf),
1200                                                           "smiley_str");
1201                                 if (text)
1202                                         str = g_string_append (str, text);
1203                         } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1204                                 gchar *text;
1205                                 list = gtk_text_child_anchor_get_widgets (anchor);
1206                                 if (list) {
1207                                         text = g_object_get_data (G_OBJECT(list->data),
1208                                                                   "str_obj");
1209                                         if (text)
1210                                                 str = g_string_append (str, text);
1211                                 }
1212                                 g_list_free (list);
1213                         }
1214                 } else if (c == '\n') {
1215                         if (!ignore_newlines) {
1216                                 ignore_newlines = TRUE;
1217                                 str = g_string_append_unichar (str, c);
1218                         }
1219                 } else {
1220                         ignore_newlines = FALSE;
1221                         str = g_string_append_unichar (str, c);
1222                 }
1223         }
1224
1225         gtk_clipboard_set_text (clipboard, str->str, str->len);
1226         g_string_free (str, TRUE);
1227 }
1228
1229 static void
1230 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1231 {
1232         iface->append_message = chat_text_view_append_message;
1233         iface->append_event = chat_text_view_append_event;
1234         iface->scroll = chat_text_view_scroll;
1235         iface->scroll_down = chat_text_view_scroll_down;
1236         iface->get_has_selection = chat_text_view_get_has_selection;
1237         iface->clear = chat_text_view_clear;
1238         iface->find_previous = chat_text_view_find_previous;
1239         iface->find_next = chat_text_view_find_next;
1240         iface->find_abilities = chat_text_view_find_abilities;
1241         iface->highlight = chat_text_view_highlight;
1242         iface->copy_clipboard = chat_text_view_copy_clipboard;
1243 }
1244
1245 EmpathyContact *
1246 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1247 {
1248         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1249
1250         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1251
1252         return priv->last_contact;
1253 }
1254
1255 void
1256 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1257                                          gboolean             only_if_date)
1258 {
1259         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1260
1261         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1262
1263         if (only_if_date != priv->only_if_date) {
1264                 priv->only_if_date = only_if_date;
1265                 g_object_notify (G_OBJECT (view), "only-if-date");
1266         }
1267 }
1268
1269 static void
1270 chat_text_view_replace_link (const gchar *text,
1271                              gssize len,
1272                              gpointer match_data,
1273                              gpointer user_data)
1274 {
1275         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1276         GtkTextIter iter;
1277
1278         gtk_text_buffer_get_end_iter (buffer, &iter);
1279         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1280                                                   text, len,
1281                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1282                                                   NULL);
1283 }
1284
1285 static void
1286 chat_text_view_replace_smiley (const gchar *text,
1287                                gssize len,
1288                                gpointer match_data,
1289                                gpointer user_data)
1290 {
1291         EmpathySmileyHit *hit = match_data;
1292         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1293         GtkTextIter iter;
1294
1295         gtk_text_buffer_get_end_iter (buffer, &iter);
1296         gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1297 }
1298
1299 static void
1300 chat_text_view_replace_verbatim (const gchar *text,
1301                                  gssize len,
1302                                  gpointer match_data,
1303                                  gpointer user_data)
1304 {
1305         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1306         GtkTextIter iter;
1307
1308         gtk_text_buffer_get_end_iter (buffer, &iter);
1309         gtk_text_buffer_insert (buffer, &iter, text, len);
1310 }
1311
1312 static EmpathyStringParser string_parsers[] = {
1313         {empathy_string_match_link, chat_text_view_replace_link},
1314         {empathy_string_match_all, chat_text_view_replace_verbatim},
1315         {NULL, NULL}
1316 };
1317
1318 static EmpathyStringParser string_parsers_with_smiley[] = {
1319         {empathy_string_match_link, chat_text_view_replace_link},
1320         {empathy_string_match_smiley, chat_text_view_replace_smiley},
1321         {empathy_string_match_all, chat_text_view_replace_verbatim},
1322         {NULL, NULL}
1323 };
1324
1325 void
1326 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1327                                     const gchar         *body,
1328                                     const gchar         *tag)
1329 {
1330         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1331         EmpathyStringParser     *parsers;
1332         gboolean                 use_smileys;
1333         GtkTextIter              start_iter;
1334         GtkTextIter              iter;
1335         GtkTextMark             *mark;
1336
1337         /* Check if we have to parse smileys */
1338         empathy_conf_get_bool (empathy_conf_get (),
1339                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1340                                &use_smileys);
1341         if (use_smileys)
1342                 parsers = string_parsers_with_smiley;
1343         else
1344                 parsers = string_parsers;
1345
1346         /* Create a mark at the place we'll start inserting */
1347         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1348         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1349
1350         /* Parse text for links/smileys and insert in the buffer */
1351         empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1352
1353         /* Insert a newline after the text inserted */
1354         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1355         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1356
1357         /* Apply the style to the inserted text. */
1358         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1359         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1360         gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1361                                            &start_iter,
1362                                            &iter);
1363
1364         gtk_text_buffer_delete_mark (priv->buffer, mark);
1365 }
1366
1367 void
1368 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1369 {
1370         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1371         GtkTextIter              iter;
1372
1373         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1374         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1375                                                   &iter,
1376                                                   "\n",
1377                                                   -1,
1378                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1379                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1380                                                   NULL);
1381 }
1382
1383 GtkTextTag *
1384 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1385                                 const gchar         *tag_name,
1386                                 const gchar         *first_property_name,
1387                                 ...)
1388 {
1389         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1390         GtkTextTag              *tag;
1391         GtkTextTagTable         *table;
1392         va_list                  list;
1393
1394         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1395         g_return_val_if_fail (tag_name != NULL, NULL);
1396
1397         table = gtk_text_buffer_get_tag_table (priv->buffer);
1398         tag = gtk_text_tag_table_lookup (table, tag_name);
1399
1400         if (tag && first_property_name) {
1401                 va_start (list, first_property_name);
1402                 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1403                 va_end (list);
1404         }
1405
1406         return tag;
1407 }
1408