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