]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-text-view.c
Fix some build errors when GSEAL is enabled
[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         g_string_free (str, TRUE);
446 }
447
448 static void
449 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
450                                       time_t               timestamp)
451 {
452         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
453         GDate                   *date, *last_date;
454         gboolean                 append_date = FALSE;
455         gboolean                 append_time = FALSE;
456
457         /* Get the date from last message */
458         last_date = g_date_new ();
459         g_date_set_time_t (last_date, priv->last_timestamp);
460
461         /* Get the date of the message we are appending */
462         date = g_date_new ();
463         g_date_set_time_t (date, timestamp);
464
465         /* If last message was from another day we append date and time */
466         if (g_date_compare (date, last_date) > 0) {
467                 append_date = TRUE;
468                 append_time = TRUE;
469         }
470
471         g_date_free (last_date);
472         g_date_free (date);
473
474         /* If last message is 'old' append the time */
475         if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
476                 append_time = TRUE;
477         }
478
479         if (append_date || (!priv->only_if_date && append_time)) {
480                 chat_text_view_append_timestamp (view, timestamp, append_date);
481         }
482 }
483
484 static void
485 chat_text_view_size_allocate (GtkWidget     *widget,
486                               GtkAllocation *alloc)
487 {
488         gboolean down;
489
490         down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
491
492         GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
493
494         if (down) {
495                 GtkAdjustment *adj;
496                 GtkWidget *sw;
497
498                 sw = gtk_widget_get_parent (widget);
499                 if (!GTK_IS_SCROLLED_WINDOW (sw))
500                         return;
501
502                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
503                 gtk_adjustment_set_value (adj,
504                                           gtk_adjustment_get_upper (adj) -
505                                           gtk_adjustment_get_page_size (adj));
506         }
507 }
508
509 static gboolean
510 chat_text_view_drag_motion (GtkWidget      *widget,
511                             GdkDragContext *context,
512                             gint            x,
513                             gint            y,
514                             guint           time_)
515 {
516         /* Don't handle drag motion, since we don't want the view to scroll as
517          * the result of dragging something across it. */
518
519         return FALSE;
520 }
521
522 static void
523 chat_text_view_get_property (GObject    *object,
524                              guint       param_id,
525                              GValue     *value,
526                              GParamSpec *pspec)
527 {
528         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
529
530         switch (param_id) {
531         case PROP_LAST_CONTACT:
532                 g_value_set_object (value, priv->last_contact);
533                 break;
534         case PROP_ONLY_IF_DATE:
535                 g_value_set_boolean (value, priv->only_if_date);
536                 break;
537         default:
538                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
539                 break;
540         };
541 }
542
543 static void
544 chat_text_view_set_property (GObject      *object,
545                              guint         param_id,
546                              const GValue *value,
547                              GParamSpec   *pspec)
548 {
549         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
550
551         switch (param_id) {
552         case PROP_ONLY_IF_DATE:
553                 priv->only_if_date = g_value_get_boolean (value);
554                 break;
555         default:
556                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
557                 break;
558         };
559 }
560
561 static void
562 chat_text_view_finalize (GObject *object)
563 {
564         EmpathyChatTextView     *view;
565         EmpathyChatTextViewPriv *priv;
566
567         view = EMPATHY_CHAT_TEXT_VIEW (object);
568         priv = GET_PRIV (view);
569
570         DEBUG ("%p", object);
571
572         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
573
574         if (priv->last_contact) {
575                 g_object_unref (priv->last_contact);
576         }
577         if (priv->scroll_time) {
578                 g_timer_destroy (priv->scroll_time);
579         }
580         if (priv->scroll_timeout) {
581                 g_source_remove (priv->scroll_timeout);
582         }
583         g_object_unref (priv->smiley_manager);
584
585         G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
586 }
587
588 static void
589 text_view_copy_clipboard (GtkTextView *text_view)
590 {
591         chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
592 }
593
594 static void
595 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
596 {
597         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
598         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
599         GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
600
601         object_class->finalize = chat_text_view_finalize;
602         object_class->get_property = chat_text_view_get_property;
603         object_class->set_property = chat_text_view_set_property;
604
605         widget_class->size_allocate = chat_text_view_size_allocate;
606         widget_class->drag_motion = chat_text_view_drag_motion;
607
608         text_view_class->copy_clipboard = text_view_copy_clipboard;
609
610         g_object_class_install_property (object_class,
611                                          PROP_LAST_CONTACT,
612                                          g_param_spec_object ("last-contact",
613                                                               "Last contact",
614                                                               "The sender of the last received message",
615                                                               EMPATHY_TYPE_CONTACT,
616                                                               G_PARAM_READABLE));
617         g_object_class_install_property (object_class,
618                                          PROP_ONLY_IF_DATE,
619                                          g_param_spec_boolean ("only-if-date",
620                                                               "Only if date",
621                                                               "Display timestamp only if the date changes",
622                                                               FALSE,
623                                                               G_PARAM_READWRITE));
624
625
626         g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
627 }
628
629 static void
630 empathy_chat_text_view_init (EmpathyChatTextView *view)
631 {
632         EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
633                 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
634
635         view->priv = priv;
636         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
637         priv->last_timestamp = 0;
638         priv->allow_scrolling = TRUE;
639         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
640
641         g_object_set (view,
642                       "wrap-mode", GTK_WRAP_WORD_CHAR,
643                       "editable", FALSE,
644                       "cursor-visible", FALSE,
645                       NULL);
646
647         priv->notify_system_fonts_id =
648                 empathy_conf_notify_add (empathy_conf_get (),
649                                          "/desktop/gnome/interface/document_font_name",
650                                          chat_text_view_notify_system_font_cb,
651                                          view);
652         chat_text_view_system_font_update (view);
653         chat_text_view_create_tags (view);
654
655         g_signal_connect (view,
656                           "populate-popup",
657                           G_CALLBACK (chat_text_view_populate_popup),
658                           NULL);
659 }
660
661 static void
662 chat_text_view_scroll_stop (EmpathyChatTextView *view)
663 {
664         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
665
666         g_timer_destroy (priv->scroll_time);
667         priv->scroll_time = NULL;
668
669         g_source_remove (priv->scroll_timeout);
670         priv->scroll_timeout = 0;
671 }
672
673 /* Code stolen from pidgin/gtkimhtml.c */
674 static gboolean
675 chat_text_view_scroll_cb (EmpathyChatTextView *view)
676 {
677         EmpathyChatTextViewPriv *priv;
678         GtkAdjustment      *adj;
679         gdouble             max_val;
680         GtkWidget          *sw;
681
682         priv = GET_PRIV (view);
683
684         sw = gtk_widget_get_parent (GTK_WIDGET (view));
685         if (!GTK_IS_SCROLLED_WINDOW (sw)) {
686                 chat_text_view_scroll_stop (view);
687                 return FALSE;
688         }
689
690         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
691         max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
692
693         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
694
695         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
696                 /* time's up. jump to the end and kill the timer */
697                 gtk_adjustment_set_value (adj, max_val);
698                 chat_text_view_scroll_stop (view);
699                 return FALSE;
700         }
701
702         /* scroll by 1/3rd the remaining distance */
703         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
704         return TRUE;
705 }
706
707 static void
708 chat_text_view_scroll_down (EmpathyChatView *view)
709 {
710         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
711
712         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
713
714         if (!priv->allow_scrolling) {
715                 return;
716         }
717
718         DEBUG ("Scrolling down");
719
720         if (priv->scroll_time) {
721                 g_timer_reset (priv->scroll_time);
722         } else {
723                 priv->scroll_time = g_timer_new ();
724         }
725         if (!priv->scroll_timeout) {
726                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
727                                                       (GSourceFunc) chat_text_view_scroll_cb,
728                                                       view);
729         }
730 }
731
732 static void
733 chat_text_view_append_message (EmpathyChatView *view,
734                                EmpathyMessage  *msg)
735 {
736         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
737         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
738         gboolean                 bottom;
739         time_t                   timestamp;
740
741         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
742         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
743
744         if (!empathy_message_get_body (msg)) {
745                 return;
746         }
747
748         bottom = chat_text_view_is_scrolled_down (text_view);
749
750         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
751
752         timestamp = empathy_message_get_timestamp (msg);
753         chat_text_maybe_append_date_and_time (text_view, timestamp);
754         if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
755                 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
756                                                                          msg);
757         }
758
759         if (bottom) {
760                 chat_text_view_scroll_down (view);
761         }
762
763         if (priv->last_contact) {
764                 g_object_unref (priv->last_contact);
765         }
766         priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
767         g_object_notify (G_OBJECT (view), "last-contact");
768
769         priv->last_timestamp = timestamp;
770 }
771
772 static void
773 chat_text_view_append_event (EmpathyChatView *view,
774                              const gchar     *str)
775 {
776         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
777         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
778         gboolean                 bottom;
779         GtkTextIter              iter;
780         gchar                   *msg;
781
782
783         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
784         g_return_if_fail (!EMP_STR_EMPTY (str));
785
786         bottom = chat_text_view_is_scrolled_down (text_view);
787         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
788         chat_text_maybe_append_date_and_time (text_view,
789                                               empathy_time_get_current ());
790
791         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
792         msg = g_strdup_printf (" - %s\n", str);
793         gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
794                                                   msg, -1,
795                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
796                                                   NULL);
797         g_free (msg);
798
799         if (bottom) {
800                 chat_text_view_scroll_down (view);
801         }
802
803         if (priv->last_contact) {
804                 g_object_unref (priv->last_contact);
805                 priv->last_contact = NULL;
806                 g_object_notify (G_OBJECT (view), "last-contact");
807         }
808 }
809
810 static void
811 chat_text_view_scroll (EmpathyChatView *view,
812                        gboolean         allow_scrolling)
813 {
814         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
815
816         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
817
818         DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
819
820         priv->allow_scrolling = allow_scrolling;
821         if (allow_scrolling) {
822                 empathy_chat_view_scroll_down (view);
823         }
824 }
825
826 static gboolean
827 chat_text_view_get_has_selection (EmpathyChatView *view)
828 {
829         GtkTextBuffer *buffer;
830
831         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
832
833         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
834
835         return gtk_text_buffer_get_has_selection (buffer);
836 }
837
838 static void
839 chat_text_view_clear (EmpathyChatView *view)
840 {
841         GtkTextBuffer      *buffer;
842         EmpathyChatTextViewPriv *priv;
843
844         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
845
846         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
847         gtk_text_buffer_set_text (buffer, "", -1);
848
849         /* We set these back to the initial values so we get
850           * timestamps when clearing the window to know when
851           * conversations start.
852           */
853         priv = GET_PRIV (view);
854
855         priv->last_timestamp = 0;
856         if (priv->last_contact) {
857                 g_object_unref (priv->last_contact);
858                 priv->last_contact = NULL;
859         }
860 }
861
862 static gboolean
863 chat_text_view_find_previous (EmpathyChatView *view,
864                                 const gchar     *search_criteria,
865                                 gboolean         new_search,
866                                 gboolean         match_case)
867 {
868         EmpathyChatTextViewPriv *priv;
869         GtkTextBuffer      *buffer;
870         GtkTextIter         iter_at_mark;
871         GtkTextIter         iter_match_start;
872         GtkTextIter         iter_match_end;
873         gboolean            found;
874         gboolean            from_start = FALSE;
875
876         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
877         g_return_val_if_fail (search_criteria != NULL, FALSE);
878
879         priv = GET_PRIV (view);
880
881         buffer = priv->buffer;
882
883         if (EMP_STR_EMPTY (search_criteria)) {
884                 if (priv->find_mark_previous) {
885                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
886
887                         gtk_text_buffer_move_mark (buffer,
888                                                    priv->find_mark_previous,
889                                                    &iter_at_mark);
890                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
891                                                       priv->find_mark_previous,
892                                                       0.0,
893                                                       TRUE,
894                                                       0.0,
895                                                       0.0);
896                         gtk_text_buffer_select_range (buffer,
897                                                       &iter_at_mark,
898                                                       &iter_at_mark);
899                 }
900
901                 return FALSE;
902         }
903
904         if (new_search) {
905                 from_start = TRUE;
906         }
907
908         if (!new_search && priv->find_mark_previous) {
909                 gtk_text_buffer_get_iter_at_mark (buffer,
910                                                   &iter_at_mark,
911                                                   priv->find_mark_previous);
912         } else {
913                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
914                 from_start = TRUE;
915         }
916
917         priv->find_last_direction = FALSE;
918
919         /* Use the standard GTK+ method for case sensitive searches. It can't do
920          * case insensitive searches (see bug #61852), so keep the custom method
921          * around for case insensitive searches. */
922         if (match_case) {
923                 found = gtk_text_iter_backward_search (&iter_at_mark,
924                                                        search_criteria,
925                                                        0, /* no text search flags, we want exact matches */
926                                                        &iter_match_start,
927                                                        &iter_match_end,
928                                                        NULL);
929         } else {
930                 found = empathy_text_iter_backward_search (&iter_at_mark,
931                                                            search_criteria,
932                                                            &iter_match_start,
933                                                            &iter_match_end,
934                                                            NULL);
935         }
936
937         if (!found) {
938                 gboolean result = FALSE;
939
940                 if (from_start) {
941                         return result;
942                 }
943
944                 /* Here we wrap around. */
945                 if (!new_search && !priv->find_wrapped) {
946                         priv->find_wrapped = TRUE;
947                         result = chat_text_view_find_previous (view,
948                                                                  search_criteria,
949                                                                  FALSE,
950                                                                  match_case);
951                         priv->find_wrapped = FALSE;
952                 }
953
954                 return result;
955         }
956
957         /* Set new mark and show on screen */
958         if (!priv->find_mark_previous) {
959                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
960                                                                         &iter_match_start,
961                                                                         TRUE);
962         } else {
963                 gtk_text_buffer_move_mark (buffer,
964                                            priv->find_mark_previous,
965                                            &iter_match_start);
966         }
967
968         if (!priv->find_mark_next) {
969                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
970                                                                     &iter_match_end,
971                                                                     TRUE);
972         } else {
973                 gtk_text_buffer_move_mark (buffer,
974                                            priv->find_mark_next,
975                                            &iter_match_end);
976         }
977
978         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
979                                       priv->find_mark_previous,
980                                       0.0,
981                                       TRUE,
982                                       0.5,
983                                       0.5);
984
985         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
986         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
987
988         return TRUE;
989 }
990
991 static gboolean
992 chat_text_view_find_next (EmpathyChatView *view,
993                             const gchar     *search_criteria,
994                             gboolean         new_search,
995                             gboolean         match_case)
996 {
997         EmpathyChatTextViewPriv *priv;
998         GtkTextBuffer      *buffer;
999         GtkTextIter         iter_at_mark;
1000         GtkTextIter         iter_match_start;
1001         GtkTextIter         iter_match_end;
1002         gboolean            found;
1003         gboolean            from_start = FALSE;
1004
1005         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
1006         g_return_val_if_fail (search_criteria != NULL, FALSE);
1007
1008         priv = GET_PRIV (view);
1009
1010         buffer = priv->buffer;
1011
1012         if (EMP_STR_EMPTY (search_criteria)) {
1013                 if (priv->find_mark_next) {
1014                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1015
1016                         gtk_text_buffer_move_mark (buffer,
1017                                                    priv->find_mark_next,
1018                                                    &iter_at_mark);
1019                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1020                                                       priv->find_mark_next,
1021                                                       0.0,
1022                                                       TRUE,
1023                                                       0.0,
1024                                                       0.0);
1025                         gtk_text_buffer_select_range (buffer,
1026                                                       &iter_at_mark,
1027                                                       &iter_at_mark);
1028                 }
1029
1030                 return FALSE;
1031         }
1032
1033         if (new_search) {
1034                 from_start = TRUE;
1035         }
1036
1037         if (!new_search && priv->find_mark_next) {
1038                 gtk_text_buffer_get_iter_at_mark (buffer,
1039                                                   &iter_at_mark,
1040                                                   priv->find_mark_next);
1041         } else {
1042                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1043                 from_start = TRUE;
1044         }
1045
1046         priv->find_last_direction = TRUE;
1047
1048         /* Use the standard GTK+ method for case sensitive searches. It can't do
1049          * case insensitive searches (see bug #61852), so keep the custom method
1050          * around for case insensitive searches. */
1051         if (match_case) {
1052                 found = gtk_text_iter_forward_search (&iter_at_mark,
1053                                                       search_criteria,
1054                                                       0,
1055                                                       &iter_match_start,
1056                                                       &iter_match_end,
1057                                                       NULL);
1058         } else {
1059                 found = empathy_text_iter_forward_search (&iter_at_mark,
1060                                                           search_criteria,
1061                                                           &iter_match_start,
1062                                                           &iter_match_end,
1063                                                           NULL);
1064         }
1065
1066         if (!found) {
1067                 gboolean result = FALSE;
1068
1069                 if (from_start) {
1070                         return result;
1071                 }
1072
1073                 /* Here we wrap around. */
1074                 if (!new_search && !priv->find_wrapped) {
1075                         priv->find_wrapped = TRUE;
1076                         result = chat_text_view_find_next (view,
1077                                                              search_criteria,
1078                                                              FALSE,
1079                                                              match_case);
1080                         priv->find_wrapped = FALSE;
1081                 }
1082
1083                 return result;
1084         }
1085
1086         /* Set new mark and show on screen */
1087         if (!priv->find_mark_next) {
1088                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1089                                                                     &iter_match_end,
1090                                                                     TRUE);
1091         } else {
1092                 gtk_text_buffer_move_mark (buffer,
1093                                            priv->find_mark_next,
1094                                            &iter_match_end);
1095         }
1096
1097         if (!priv->find_mark_previous) {
1098                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1099                                                                         &iter_match_start,
1100                                                                         TRUE);
1101         } else {
1102                 gtk_text_buffer_move_mark (buffer,
1103                                            priv->find_mark_previous,
1104                                            &iter_match_start);
1105         }
1106
1107         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1108                                       priv->find_mark_next,
1109                                       0.0,
1110                                       TRUE,
1111                                       0.5,
1112                                       0.5);
1113
1114         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1115         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1116
1117         return TRUE;
1118 }
1119
1120 static void
1121 chat_text_view_find_abilities (EmpathyChatView *view,
1122                                  const gchar    *search_criteria,
1123                                  gboolean        match_case,
1124                                  gboolean       *can_do_previous,
1125                                  gboolean       *can_do_next)
1126 {
1127         EmpathyChatTextViewPriv *priv;
1128         GtkTextBuffer           *buffer;
1129         GtkTextIter              iter_at_mark;
1130         GtkTextIter              iter_match_start;
1131         GtkTextIter              iter_match_end;
1132
1133         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1134         g_return_if_fail (search_criteria != NULL);
1135         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1136
1137         priv = GET_PRIV (view);
1138
1139         buffer = priv->buffer;
1140
1141         if (can_do_previous) {
1142                 if (priv->find_mark_previous) {
1143                         gtk_text_buffer_get_iter_at_mark (buffer,
1144                                                           &iter_at_mark,
1145                                                           priv->find_mark_previous);
1146                 } else {
1147                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1148                 }
1149
1150                 if (match_case) {
1151                         *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1152                                                                           search_criteria,
1153                                                                           0,
1154                                                                           &iter_match_start,
1155                                                                           &iter_match_end,
1156                                                                           NULL);
1157                 } else {
1158                         *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1159                                                                               search_criteria,
1160                                                                               &iter_match_start,
1161                                                                               &iter_match_end,
1162                                                                               NULL);
1163                 }
1164         }
1165
1166         if (can_do_next) {
1167                 if (priv->find_mark_next) {
1168                         gtk_text_buffer_get_iter_at_mark (buffer,
1169                                                           &iter_at_mark,
1170                                                           priv->find_mark_next);
1171                 } else {
1172                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1173                 }
1174
1175                 if (match_case) {
1176                         *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1177                                                                      search_criteria,
1178                                                                      0,
1179                                                                      &iter_match_start,
1180                                                                      &iter_match_end,
1181                                                                      NULL);
1182                 } else {
1183                         *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1184                                                                          search_criteria,
1185                                                                          &iter_match_start,
1186                                                                          &iter_match_end,
1187                                                                          NULL);
1188                 }
1189         }
1190 }
1191
1192 static void
1193 chat_text_view_highlight (EmpathyChatView *view,
1194                             const gchar     *text,
1195                             gboolean         match_case)
1196 {
1197         GtkTextBuffer *buffer;
1198         GtkTextIter    iter;
1199         GtkTextIter    iter_start;
1200         GtkTextIter    iter_end;
1201         GtkTextIter    iter_match_start;
1202         GtkTextIter    iter_match_end;
1203         gboolean       found;
1204
1205         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1206
1207         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1208
1209         gtk_text_buffer_get_start_iter (buffer, &iter);
1210
1211         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1212         gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1213                                             &iter_start,
1214                                             &iter_end);
1215
1216         if (EMP_STR_EMPTY (text)) {
1217                 return;
1218         }
1219
1220         while (1) {
1221                 if (match_case) {
1222                         found = gtk_text_iter_forward_search (&iter,
1223                                                               text,
1224                                                               0,
1225                                                               &iter_match_start,
1226                                                               &iter_match_end,
1227                                                               NULL);
1228                 } else {
1229                         found = empathy_text_iter_forward_search (&iter,
1230                                                                   text,
1231                                                                   &iter_match_start,
1232                                                                   &iter_match_end,
1233                                                                   NULL);
1234                 }
1235                 if (!found) {
1236                         break;
1237                 }
1238
1239                 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1240                                                    &iter_match_start,
1241                                                    &iter_match_end);
1242
1243                 iter = iter_match_end;
1244         }
1245 }
1246
1247 static void
1248 chat_text_view_copy_clipboard (EmpathyChatView *view)
1249 {
1250         GtkTextBuffer *buffer;
1251         GtkTextIter start, iter, end;
1252         GtkClipboard  *clipboard;
1253         GdkPixbuf *pixbuf;
1254         gunichar c;
1255         GtkTextChildAnchor *anchor = NULL;
1256         GString *str;
1257         GList *list;
1258         gboolean ignore_newlines = FALSE;
1259
1260         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1261
1262         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1263         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1264
1265         if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1266                 return;
1267
1268         str = g_string_new ("");
1269
1270         for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1271                 c = gtk_text_iter_get_char (&iter);
1272                 /* 0xFFFC is the 'object replacement' unicode character,
1273                  * it indicates the presence of a pixbuf or a widget. */
1274                 if (c == 0xFFFC) {
1275                         ignore_newlines = FALSE;
1276                         if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1277                                 gchar *text;
1278                                 text = g_object_get_data (G_OBJECT(pixbuf),
1279                                                           "smiley_str");
1280                                 if (text)
1281                                         str = g_string_append (str, text);
1282                         } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1283                                 gchar *text;
1284                                 list = gtk_text_child_anchor_get_widgets (anchor);
1285                                 if (list) {
1286                                         text = g_object_get_data (G_OBJECT(list->data),
1287                                                                   "str_obj");
1288                                         if (text)
1289                                                 str = g_string_append (str, text);
1290                                 }
1291                                 g_list_free (list);
1292                         }
1293                 } else if (c == '\n') {
1294                         if (!ignore_newlines) {
1295                                 ignore_newlines = TRUE;
1296                                 str = g_string_append_unichar (str, c);
1297                         }
1298                 } else {
1299                         ignore_newlines = FALSE;
1300                         str = g_string_append_unichar (str, c);
1301                 }
1302         }
1303
1304         gtk_clipboard_set_text (clipboard, str->str, str->len);
1305         g_string_free (str, TRUE);
1306 }
1307
1308 static void
1309 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1310 {
1311         iface->append_message = chat_text_view_append_message;
1312         iface->append_event = chat_text_view_append_event;
1313         iface->scroll = chat_text_view_scroll;
1314         iface->scroll_down = chat_text_view_scroll_down;
1315         iface->get_has_selection = chat_text_view_get_has_selection;
1316         iface->clear = chat_text_view_clear;
1317         iface->find_previous = chat_text_view_find_previous;
1318         iface->find_next = chat_text_view_find_next;
1319         iface->find_abilities = chat_text_view_find_abilities;
1320         iface->highlight = chat_text_view_highlight;
1321         iface->copy_clipboard = chat_text_view_copy_clipboard;
1322 }
1323
1324 EmpathyContact *
1325 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1326 {
1327         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1328
1329         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1330
1331         return priv->last_contact;
1332 }
1333
1334 time_t
1335 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1336 {
1337         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1338
1339         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1340
1341         return priv->last_timestamp;
1342 }
1343
1344 void
1345 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1346                                          gboolean             only_if_date)
1347 {
1348         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1349
1350         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1351
1352         if (only_if_date != priv->only_if_date) {
1353                 priv->only_if_date = only_if_date;
1354                 g_object_notify (G_OBJECT (view), "only-if-date");
1355         }
1356 }
1357
1358 static void
1359 chat_text_view_replace_link (const gchar *text,
1360                              gssize len,
1361                              gpointer match_data,
1362                              gpointer user_data)
1363 {
1364         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1365         GtkTextIter iter;
1366
1367         gtk_text_buffer_get_end_iter (buffer, &iter);
1368         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1369                                                   text, len,
1370                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1371                                                   NULL);
1372 }
1373
1374 static void
1375 chat_text_view_replace_smiley (const gchar *text,
1376                                gssize len,
1377                                gpointer match_data,
1378                                gpointer user_data)
1379 {
1380         EmpathySmileyHit *hit = match_data;
1381         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1382         GtkTextIter iter;
1383
1384         gtk_text_buffer_get_end_iter (buffer, &iter);
1385         gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1386 }
1387
1388 static void
1389 chat_text_view_replace_verbatim (const gchar *text,
1390                                  gssize len,
1391                                  gpointer match_data,
1392                                  gpointer user_data)
1393 {
1394         GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1395         GtkTextIter iter;
1396
1397         gtk_text_buffer_get_end_iter (buffer, &iter);
1398         gtk_text_buffer_insert (buffer, &iter, text, len);
1399 }
1400
1401 static EmpathyStringParser string_parsers[] = {
1402         {empathy_string_match_link, chat_text_view_replace_link},
1403         {empathy_string_match_all, chat_text_view_replace_verbatim},
1404         {NULL, NULL}
1405 };
1406
1407 static EmpathyStringParser string_parsers_with_smiley[] = {
1408         {empathy_string_match_link, chat_text_view_replace_link},
1409         {empathy_string_match_smiley, chat_text_view_replace_smiley},
1410         {empathy_string_match_all, chat_text_view_replace_verbatim},
1411         {NULL, NULL}
1412 };
1413
1414 void
1415 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1416                                     const gchar         *body,
1417                                     const gchar         *tag)
1418 {
1419         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1420         EmpathyStringParser     *parsers;
1421         gboolean                 use_smileys;
1422         GtkTextIter              start_iter;
1423         GtkTextIter              iter;
1424         GtkTextMark             *mark;
1425
1426         /* Check if we have to parse smileys */
1427         empathy_conf_get_bool (empathy_conf_get (),
1428                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1429                                &use_smileys);
1430         if (use_smileys)
1431                 parsers = string_parsers_with_smiley;
1432         else
1433                 parsers = string_parsers;
1434
1435         /* Create a mark at the place we'll start inserting */
1436         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1437         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1438
1439         /* Parse text for links/smileys and insert in the buffer */
1440         empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1441
1442         /* Insert a newline after the text inserted */
1443         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1444         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1445
1446         /* Apply the style to the inserted text. */
1447         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1448         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1449         gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1450                                            &start_iter,
1451                                            &iter);
1452
1453         gtk_text_buffer_delete_mark (priv->buffer, mark);
1454 }
1455
1456 void
1457 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1458 {
1459         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1460         GtkTextIter              iter;
1461
1462         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1463         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1464                                                   &iter,
1465                                                   "\n",
1466                                                   -1,
1467                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1468                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1469                                                   NULL);
1470 }
1471
1472 GtkTextTag *
1473 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1474                                 const gchar         *tag_name,
1475                                 const gchar         *first_property_name,
1476                                 ...)
1477 {
1478         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1479         GtkTextTag              *tag;
1480         GtkTextTagTable         *table;
1481         va_list                  list;
1482
1483         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1484         g_return_val_if_fail (tag_name != NULL, NULL);
1485
1486         table = gtk_text_buffer_get_tag_table (priv->buffer);
1487         tag = gtk_text_tag_table_lookup (table, tag_name);
1488
1489         if (tag && first_property_name) {
1490                 va_start (list, first_property_name);
1491                 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1492                 va_end (list);
1493         }
1494
1495         return tag;
1496 }
1497