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