]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-text-view.c
Always use "Apply" button if we are not connected or editing an account
[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                                 gboolean         match_case)
843 {
844         EmpathyChatTextViewPriv *priv;
845         GtkTextBuffer      *buffer;
846         GtkTextIter         iter_at_mark;
847         GtkTextIter         iter_match_start;
848         GtkTextIter         iter_match_end;
849         gboolean            found;
850         gboolean            from_start = FALSE;
851
852         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
853         g_return_val_if_fail (search_criteria != NULL, FALSE);
854
855         priv = GET_PRIV (view);
856
857         buffer = priv->buffer;
858
859         if (EMP_STR_EMPTY (search_criteria)) {
860                 if (priv->find_mark_previous) {
861                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
862
863                         gtk_text_buffer_move_mark (buffer,
864                                                    priv->find_mark_previous,
865                                                    &iter_at_mark);
866                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
867                                                       priv->find_mark_previous,
868                                                       0.0,
869                                                       TRUE,
870                                                       0.0,
871                                                       0.0);
872                         gtk_text_buffer_select_range (buffer,
873                                                       &iter_at_mark,
874                                                       &iter_at_mark);
875                 }
876
877                 return FALSE;
878         }
879
880         if (new_search) {
881                 from_start = TRUE;
882         }
883
884         if (!new_search && priv->find_mark_previous) {
885                 gtk_text_buffer_get_iter_at_mark (buffer,
886                                                   &iter_at_mark,
887                                                   priv->find_mark_previous);
888         } else {
889                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
890                 from_start = TRUE;
891         }
892
893         priv->find_last_direction = FALSE;
894
895         /* Use the standard GTK+ method for case sensitive searches. It can't do
896          * case insensitive searches (see bug #61852), so keep the custom method
897          * around for case insensitive searches. */
898         if (match_case) {
899                 found = gtk_text_iter_backward_search (&iter_at_mark,
900                                                        search_criteria,
901                                                        0, /* no text search flags, we want exact matches */
902                                                        &iter_match_start,
903                                                        &iter_match_end,
904                                                        NULL);
905         } else {
906                 found = empathy_text_iter_backward_search (&iter_at_mark,
907                                                            search_criteria,
908                                                            &iter_match_start,
909                                                            &iter_match_end,
910                                                            NULL);
911         }
912
913         if (!found) {
914                 gboolean result = FALSE;
915
916                 if (from_start) {
917                         return result;
918                 }
919
920                 /* Here we wrap around. */
921                 if (!new_search && !priv->find_wrapped) {
922                         priv->find_wrapped = TRUE;
923                         result = chat_text_view_find_previous (view,
924                                                                  search_criteria,
925                                                                  FALSE,
926                                                                  match_case);
927                         priv->find_wrapped = FALSE;
928                 }
929
930                 return result;
931         }
932
933         /* Set new mark and show on screen */
934         if (!priv->find_mark_previous) {
935                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
936                                                                         &iter_match_start,
937                                                                         TRUE);
938         } else {
939                 gtk_text_buffer_move_mark (buffer,
940                                            priv->find_mark_previous,
941                                            &iter_match_start);
942         }
943
944         if (!priv->find_mark_next) {
945                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
946                                                                     &iter_match_end,
947                                                                     TRUE);
948         } else {
949                 gtk_text_buffer_move_mark (buffer,
950                                            priv->find_mark_next,
951                                            &iter_match_end);
952         }
953
954         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
955                                       priv->find_mark_previous,
956                                       0.0,
957                                       TRUE,
958                                       0.5,
959                                       0.5);
960
961         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
962         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
963
964         return TRUE;
965 }
966
967 static gboolean
968 chat_text_view_find_next (EmpathyChatView *view,
969                             const gchar     *search_criteria,
970                             gboolean         new_search,
971                             gboolean         match_case)
972 {
973         EmpathyChatTextViewPriv *priv;
974         GtkTextBuffer      *buffer;
975         GtkTextIter         iter_at_mark;
976         GtkTextIter         iter_match_start;
977         GtkTextIter         iter_match_end;
978         gboolean            found;
979         gboolean            from_start = FALSE;
980
981         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
982         g_return_val_if_fail (search_criteria != NULL, FALSE);
983
984         priv = GET_PRIV (view);
985
986         buffer = priv->buffer;
987
988         if (EMP_STR_EMPTY (search_criteria)) {
989                 if (priv->find_mark_next) {
990                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
991
992                         gtk_text_buffer_move_mark (buffer,
993                                                    priv->find_mark_next,
994                                                    &iter_at_mark);
995                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
996                                                       priv->find_mark_next,
997                                                       0.0,
998                                                       TRUE,
999                                                       0.0,
1000                                                       0.0);
1001                         gtk_text_buffer_select_range (buffer,
1002                                                       &iter_at_mark,
1003                                                       &iter_at_mark);
1004                 }
1005
1006                 return FALSE;
1007         }
1008
1009         if (new_search) {
1010                 from_start = TRUE;
1011         }
1012
1013         if (!new_search && priv->find_mark_next) {
1014                 gtk_text_buffer_get_iter_at_mark (buffer,
1015                                                   &iter_at_mark,
1016                                                   priv->find_mark_next);
1017         } else {
1018                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1019                 from_start = TRUE;
1020         }
1021
1022         priv->find_last_direction = TRUE;
1023
1024         /* Use the standard GTK+ method for case sensitive searches. It can't do
1025          * case insensitive searches (see bug #61852), so keep the custom method
1026          * around for case insensitive searches. */
1027         if (match_case) {
1028                 found = gtk_text_iter_forward_search (&iter_at_mark,
1029                                                       search_criteria,
1030                                                       0,
1031                                                       &iter_match_start,
1032                                                       &iter_match_end,
1033                                                       NULL);
1034         } else {
1035                 found = empathy_text_iter_forward_search (&iter_at_mark,
1036                                                           search_criteria,
1037                                                           &iter_match_start,
1038                                                           &iter_match_end,
1039                                                           NULL);
1040         }
1041
1042         if (!found) {
1043                 gboolean result = FALSE;
1044
1045                 if (from_start) {
1046                         return result;
1047                 }
1048
1049                 /* Here we wrap around. */
1050                 if (!new_search && !priv->find_wrapped) {
1051                         priv->find_wrapped = TRUE;
1052                         result = chat_text_view_find_next (view,
1053                                                              search_criteria,
1054                                                              FALSE,
1055                                                              match_case);
1056                         priv->find_wrapped = FALSE;
1057                 }
1058
1059                 return result;
1060         }
1061
1062         /* Set new mark and show on screen */
1063         if (!priv->find_mark_next) {
1064                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1065                                                                     &iter_match_end,
1066                                                                     TRUE);
1067         } else {
1068                 gtk_text_buffer_move_mark (buffer,
1069                                            priv->find_mark_next,
1070                                            &iter_match_end);
1071         }
1072
1073         if (!priv->find_mark_previous) {
1074                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1075                                                                         &iter_match_start,
1076                                                                         TRUE);
1077         } else {
1078                 gtk_text_buffer_move_mark (buffer,
1079                                            priv->find_mark_previous,
1080                                            &iter_match_start);
1081         }
1082
1083         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1084                                       priv->find_mark_next,
1085                                       0.0,
1086                                       TRUE,
1087                                       0.5,
1088                                       0.5);
1089
1090         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1091         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1092
1093         return TRUE;
1094 }
1095
1096 static void
1097 chat_text_view_find_abilities (EmpathyChatView *view,
1098                                  const gchar    *search_criteria,
1099                                  gboolean        match_case,
1100                                  gboolean       *can_do_previous,
1101                                  gboolean       *can_do_next)
1102 {
1103         EmpathyChatTextViewPriv *priv;
1104         GtkTextBuffer           *buffer;
1105         GtkTextIter              iter_at_mark;
1106         GtkTextIter              iter_match_start;
1107         GtkTextIter              iter_match_end;
1108
1109         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1110         g_return_if_fail (search_criteria != NULL);
1111         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1112
1113         priv = GET_PRIV (view);
1114
1115         buffer = priv->buffer;
1116
1117         if (can_do_previous) {
1118                 if (priv->find_mark_previous) {
1119                         gtk_text_buffer_get_iter_at_mark (buffer,
1120                                                           &iter_at_mark,
1121                                                           priv->find_mark_previous);
1122                 } else {
1123                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1124                 }
1125
1126                 if (match_case) {
1127                         *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1128                                                                           search_criteria,
1129                                                                           0,
1130                                                                           &iter_match_start,
1131                                                                           &iter_match_end,
1132                                                                           NULL);
1133                 } else {
1134                         *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1135                                                                               search_criteria,
1136                                                                               &iter_match_start,
1137                                                                               &iter_match_end,
1138                                                                               NULL);
1139                 }
1140         }
1141
1142         if (can_do_next) {
1143                 if (priv->find_mark_next) {
1144                         gtk_text_buffer_get_iter_at_mark (buffer,
1145                                                           &iter_at_mark,
1146                                                           priv->find_mark_next);
1147                 } else {
1148                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1149                 }
1150
1151                 if (match_case) {
1152                         *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1153                                                                      search_criteria,
1154                                                                      0,
1155                                                                      &iter_match_start,
1156                                                                      &iter_match_end,
1157                                                                      NULL);
1158                 } else {
1159                         *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1160                                                                          search_criteria,
1161                                                                          &iter_match_start,
1162                                                                          &iter_match_end,
1163                                                                          NULL);
1164                 }
1165         }
1166 }
1167
1168 static void
1169 chat_text_view_highlight (EmpathyChatView *view,
1170                             const gchar     *text,
1171                             gboolean         match_case)
1172 {
1173         GtkTextBuffer *buffer;
1174         GtkTextIter    iter;
1175         GtkTextIter    iter_start;
1176         GtkTextIter    iter_end;
1177         GtkTextIter    iter_match_start;
1178         GtkTextIter    iter_match_end;
1179         gboolean       found;
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
1185         gtk_text_buffer_get_start_iter (buffer, &iter);
1186
1187         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1188         gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1189                                             &iter_start,
1190                                             &iter_end);
1191
1192         if (EMP_STR_EMPTY (text)) {
1193                 return;
1194         }
1195
1196         while (1) {
1197                 if (match_case) {
1198                         found = gtk_text_iter_forward_search (&iter,
1199                                                               text,
1200                                                               0,
1201                                                               &iter_match_start,
1202                                                               &iter_match_end,
1203                                                               NULL);
1204                 } else {
1205                         found = empathy_text_iter_forward_search (&iter,
1206                                                                   text,
1207                                                                   &iter_match_start,
1208                                                                   &iter_match_end,
1209                                                                   NULL);
1210                 }
1211                 if (!found) {
1212                         break;
1213                 }
1214
1215                 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1216                                                    &iter_match_start,
1217                                                    &iter_match_end);
1218
1219                 iter = iter_match_end;
1220         }
1221 }
1222
1223 static void
1224 chat_text_view_copy_clipboard (EmpathyChatView *view)
1225 {
1226         GtkTextBuffer *buffer;
1227         GtkTextIter start, iter, end;
1228         GtkClipboard  *clipboard;
1229         GdkPixbuf *pixbuf;
1230         gunichar c;
1231         GtkTextChildAnchor *anchor = NULL;
1232         GString *str;
1233         GList *list;
1234         gboolean ignore_newlines = FALSE;
1235
1236         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1237
1238         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1239         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1240
1241         if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1242                 return;
1243
1244         str = g_string_new ("");
1245
1246         for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1247                 c = gtk_text_iter_get_char (&iter);
1248                 /* 0xFFFC is the 'object replacement' unicode character,
1249                  * it indicates the presence of a pixbuf or a widget. */
1250                 if (c == 0xFFFC) {
1251                         ignore_newlines = FALSE;
1252                         if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1253                                 gchar *text;
1254                                 text = g_object_get_data (G_OBJECT(pixbuf),
1255                                                           "smiley_str");
1256                                 if (text)
1257                                         str = g_string_append (str, text);
1258                         } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1259                                 gchar *text;
1260                                 list = gtk_text_child_anchor_get_widgets (anchor);
1261                                 if (list) {
1262                                         text = g_object_get_data (G_OBJECT(list->data),
1263                                                                   "str_obj");
1264                                         if (text)
1265                                                 str = g_string_append (str, text);
1266                                 }
1267                                 g_list_free (list);
1268                         }
1269                 } else if (c == '\n') {
1270                         if (!ignore_newlines) {
1271                                 ignore_newlines = TRUE;
1272                                 str = g_string_append_unichar (str, c);
1273                         }
1274                 } else {
1275                         ignore_newlines = FALSE;
1276                         str = g_string_append_unichar (str, c);
1277                 }
1278         }
1279
1280         gtk_clipboard_set_text (clipboard, str->str, str->len);
1281         g_string_free (str, TRUE);
1282 }
1283
1284 static void
1285 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1286 {
1287         iface->append_message = chat_text_view_append_message;
1288         iface->append_event = chat_text_view_append_event;
1289         iface->scroll = chat_text_view_scroll;
1290         iface->scroll_down = chat_text_view_scroll_down;
1291         iface->get_has_selection = chat_text_view_get_has_selection;
1292         iface->clear = chat_text_view_clear;
1293         iface->find_previous = chat_text_view_find_previous;
1294         iface->find_next = chat_text_view_find_next;
1295         iface->find_abilities = chat_text_view_find_abilities;
1296         iface->highlight = chat_text_view_highlight;
1297         iface->copy_clipboard = chat_text_view_copy_clipboard;
1298 }
1299
1300 EmpathyContact *
1301 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1302 {
1303         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1304
1305         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1306
1307         return priv->last_contact;
1308 }
1309
1310 void
1311 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1312                                          gboolean             only_if_date)
1313 {
1314         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1315
1316         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1317
1318         if (only_if_date != priv->only_if_date) {
1319                 priv->only_if_date = only_if_date;
1320                 g_object_notify (G_OBJECT (view), "only-if-date");
1321         }
1322 }
1323
1324 static void
1325 chat_text_view_replace_link (const gchar *text,
1326                              gssize len,
1327                              gpointer match_data,
1328                              gpointer user_data)
1329 {
1330         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1331         GtkTextIter iter;
1332
1333         gtk_text_buffer_get_end_iter (buffer, &iter);
1334         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1335                                                   text, len,
1336                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1337                                                   NULL);
1338 }
1339
1340 static void
1341 chat_text_view_replace_smiley (const gchar *text,
1342                                gssize len,
1343                                gpointer match_data,
1344                                gpointer user_data)
1345 {
1346         EmpathySmileyHit *hit = match_data;
1347         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1348         GtkTextIter iter;
1349
1350         gtk_text_buffer_get_end_iter (buffer, &iter);
1351         gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1352 }
1353
1354 static void
1355 chat_text_view_replace_verbatim (const gchar *text,
1356                                  gssize len,
1357                                  gpointer match_data,
1358                                  gpointer user_data)
1359 {
1360         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1361         GtkTextIter iter;
1362
1363         gtk_text_buffer_get_end_iter (buffer, &iter);
1364         gtk_text_buffer_insert (buffer, &iter, text, len);
1365 }
1366
1367 static EmpathyStringParser string_parsers[] = {
1368         {empathy_string_match_link, chat_text_view_replace_link},
1369         {empathy_string_match_all, chat_text_view_replace_verbatim},
1370         {NULL, NULL}
1371 };
1372
1373 static EmpathyStringParser string_parsers_with_smiley[] = {
1374         {empathy_string_match_link, chat_text_view_replace_link},
1375         {empathy_string_match_smiley, chat_text_view_replace_smiley},
1376         {empathy_string_match_all, chat_text_view_replace_verbatim},
1377         {NULL, NULL}
1378 };
1379
1380 void
1381 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1382                                     const gchar         *body,
1383                                     const gchar         *tag)
1384 {
1385         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1386         EmpathyStringParser     *parsers;
1387         gboolean                 use_smileys;
1388         GtkTextIter              start_iter;
1389         GtkTextIter              iter;
1390         GtkTextMark             *mark;
1391
1392         /* Check if we have to parse smileys */
1393         empathy_conf_get_bool (empathy_conf_get (),
1394                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1395                                &use_smileys);
1396         if (use_smileys)
1397                 parsers = string_parsers_with_smiley;
1398         else
1399                 parsers = string_parsers;
1400
1401         /* Create a mark at the place we'll start inserting */
1402         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1403         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1404
1405         /* Parse text for links/smileys and insert in the buffer */
1406         empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1407
1408         /* Insert a newline after the text inserted */
1409         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1410         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1411
1412         /* Apply the style to the inserted text. */
1413         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1414         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1415         gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1416                                            &start_iter,
1417                                            &iter);
1418
1419         gtk_text_buffer_delete_mark (priv->buffer, mark);
1420 }
1421
1422 void
1423 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1424 {
1425         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1426         GtkTextIter              iter;
1427
1428         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1429         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1430                                                   &iter,
1431                                                   "\n",
1432                                                   -1,
1433                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1434                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1435                                                   NULL);
1436 }
1437
1438 GtkTextTag *
1439 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1440                                 const gchar         *tag_name,
1441                                 const gchar         *first_property_name,
1442                                 ...)
1443 {
1444         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1445         GtkTextTag              *tag;
1446         GtkTextTagTable         *table;
1447         va_list                  list;
1448
1449         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1450         g_return_val_if_fail (tag_name != NULL, NULL);
1451
1452         table = gtk_text_buffer_get_tag_table (priv->buffer);
1453         tag = gtk_text_tag_table_lookup (table, tag_name);
1454
1455         if (tag && first_property_name) {
1456                 va_start (list, first_property_name);
1457                 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1458                 va_end (list);
1459         }
1460
1461         return tag;
1462 }
1463