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