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