]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-text-view.c
Updated Oriya Translation
[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,
499                                           gtk_adjustment_get_upper (adj) -
500                                           gtk_adjustment_get_page_size (adj));
501         }
502 }
503
504 static gboolean
505 chat_text_view_drag_motion (GtkWidget      *widget,
506                             GdkDragContext *context,
507                             gint            x,
508                             gint            y,
509                             guint           time)
510 {
511         /* Don't handle drag motion, since we don't want the view to scroll as
512          * the result of dragging something across it. */
513
514         return FALSE;
515 }
516
517 static void
518 chat_text_view_get_property (GObject    *object,
519                              guint       param_id,
520                              GValue     *value,
521                              GParamSpec *pspec)
522 {
523         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
524
525         switch (param_id) {
526         case PROP_LAST_CONTACT:
527                 g_value_set_object (value, priv->last_contact);
528                 break;
529         case PROP_ONLY_IF_DATE:
530                 g_value_set_boolean (value, priv->only_if_date);
531                 break;
532         default:
533                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
534                 break;
535         };
536 }
537
538 static void
539 chat_text_view_set_property (GObject      *object,
540                              guint         param_id,
541                              const GValue *value,
542                              GParamSpec   *pspec)
543 {
544         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
545
546         switch (param_id) {
547         case PROP_ONLY_IF_DATE:
548                 priv->only_if_date = g_value_get_boolean (value);
549                 break;
550         default:
551                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
552                 break;
553         };
554 }
555
556 static void
557 chat_text_view_finalize (GObject *object)
558 {
559         EmpathyChatTextView     *view;
560         EmpathyChatTextViewPriv *priv;
561
562         view = EMPATHY_CHAT_TEXT_VIEW (object);
563         priv = GET_PRIV (view);
564
565         DEBUG ("%p", object);
566
567         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
568
569         if (priv->last_contact) {
570                 g_object_unref (priv->last_contact);
571         }
572         if (priv->scroll_time) {
573                 g_timer_destroy (priv->scroll_time);
574         }
575         if (priv->scroll_timeout) {
576                 g_source_remove (priv->scroll_timeout);
577         }
578         g_object_unref (priv->smiley_manager);
579
580         G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
581 }
582
583 static void
584 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
585 {
586         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
587         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
588
589         object_class->finalize = chat_text_view_finalize;
590         object_class->get_property = chat_text_view_get_property;
591         object_class->set_property = chat_text_view_set_property;
592
593         widget_class->size_allocate = chat_text_view_size_allocate;
594         widget_class->drag_motion = chat_text_view_drag_motion;
595
596         g_object_class_install_property (object_class,
597                                          PROP_LAST_CONTACT,
598                                          g_param_spec_object ("last-contact",
599                                                               "Last contact",
600                                                               "The sender of the last received message",
601                                                               EMPATHY_TYPE_CONTACT,
602                                                               G_PARAM_READABLE));
603         g_object_class_install_property (object_class,
604                                          PROP_ONLY_IF_DATE,
605                                          g_param_spec_boolean ("only-if-date",
606                                                               "Only if date",
607                                                               "Display timestamp only if the date changes",
608                                                               FALSE,
609                                                               G_PARAM_READWRITE));
610
611
612         g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
613 }
614
615 static void
616 empathy_chat_text_view_init (EmpathyChatTextView *view)
617 {
618         EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
619                 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
620
621         view->priv = priv;
622         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
623         priv->last_timestamp = 0;
624         priv->allow_scrolling = TRUE;
625         priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
626
627         g_object_set (view,
628                       "wrap-mode", GTK_WRAP_WORD_CHAR,
629                       "editable", FALSE,
630                       "cursor-visible", FALSE,
631                       NULL);
632
633         priv->notify_system_fonts_id =
634                 empathy_conf_notify_add (empathy_conf_get (),
635                                          "/desktop/gnome/interface/document_font_name",
636                                          chat_text_view_notify_system_font_cb,
637                                          view);
638         chat_text_view_system_font_update (view);
639         chat_text_view_create_tags (view);
640
641         g_signal_connect (view,
642                           "populate-popup",
643                           G_CALLBACK (chat_text_view_populate_popup),
644                           NULL);
645 }
646
647 /* Code stolen from pidgin/gtkimhtml.c */
648 static gboolean
649 chat_text_view_scroll_cb (EmpathyChatTextView *view)
650 {
651         EmpathyChatTextViewPriv *priv;
652         GtkAdjustment      *adj;
653         gdouble             max_val;
654
655         priv = GET_PRIV (view);
656         adj = GTK_TEXT_VIEW (view)->vadjustment;
657         max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
658
659         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
660
661         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
662                 /* time's up. jump to the end and kill the timer */
663                 gtk_adjustment_set_value (adj, max_val);
664                 g_timer_destroy (priv->scroll_time);
665                 priv->scroll_time = NULL;
666                 priv->scroll_timeout = 0;
667                 return FALSE;
668         }
669
670         /* scroll by 1/3rd the remaining distance */
671         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
672         return TRUE;
673 }
674
675 static void
676 chat_text_view_scroll_down (EmpathyChatView *view)
677 {
678         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
679
680         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
681
682         if (!priv->allow_scrolling) {
683                 return;
684         }
685
686         DEBUG ("Scrolling down");
687
688         if (priv->scroll_time) {
689                 g_timer_reset (priv->scroll_time);
690         } else {
691                 priv->scroll_time = g_timer_new ();
692         }
693         if (!priv->scroll_timeout) {
694                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
695                                                       (GSourceFunc) chat_text_view_scroll_cb,
696                                                       view);
697         }
698 }
699
700 static void
701 chat_text_view_append_message (EmpathyChatView *view,
702                                EmpathyMessage  *msg)
703 {
704         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
705         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
706         gboolean                 bottom;
707         time_t                   timestamp;
708
709         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
710         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
711
712         if (!empathy_message_get_body (msg)) {
713                 return;
714         }
715
716         bottom = chat_text_view_is_scrolled_down (text_view);
717
718         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
719
720         timestamp = empathy_message_get_timestamp (msg);
721         chat_text_maybe_append_date_and_time (text_view, timestamp);
722         if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
723                 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
724                                                                          msg);
725         }
726
727         if (bottom) {
728                 chat_text_view_scroll_down (view);
729         }
730
731         if (priv->last_contact) {
732                 g_object_unref (priv->last_contact);
733         }
734         priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
735         g_object_notify (G_OBJECT (view), "last-contact");
736 }
737
738 static void
739 chat_text_view_append_event (EmpathyChatView *view,
740                              const gchar     *str)
741 {
742         EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
743         EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
744         gboolean                 bottom;
745         GtkTextIter              iter;
746         gchar                   *msg;
747
748
749         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
750         g_return_if_fail (!EMP_STR_EMPTY (str));
751
752         bottom = chat_text_view_is_scrolled_down (text_view);
753         chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
754         chat_text_maybe_append_date_and_time (text_view,
755                                               empathy_time_get_current ());
756
757         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
758         msg = g_strdup_printf (" - %s\n", str);
759         gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
760                                                   msg, -1,
761                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
762                                                   NULL);
763         g_free (msg);
764
765         if (bottom) {
766                 chat_text_view_scroll_down (view);
767         }
768
769         if (priv->last_contact) {
770                 g_object_unref (priv->last_contact);
771                 priv->last_contact = NULL;
772                 g_object_notify (G_OBJECT (view), "last-contact");
773         }
774 }
775
776 static void
777 chat_text_view_scroll (EmpathyChatView *view,
778                        gboolean         allow_scrolling)
779 {
780         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
781
782         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
783
784         DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
785
786         priv->allow_scrolling = allow_scrolling;
787         if (allow_scrolling) {
788                 empathy_chat_view_scroll_down (view);
789         }
790 }
791
792 static gboolean
793 chat_text_view_get_has_selection (EmpathyChatView *view)
794 {
795         GtkTextBuffer *buffer;
796
797         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
798
799         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
800
801         return gtk_text_buffer_get_has_selection (buffer);
802 }
803
804 static void
805 chat_text_view_clear (EmpathyChatView *view)
806 {
807         GtkTextBuffer      *buffer;
808         EmpathyChatTextViewPriv *priv;
809
810         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
811
812         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
813         gtk_text_buffer_set_text (buffer, "", -1);
814
815         /* We set these back to the initial values so we get
816           * timestamps when clearing the window to know when
817           * conversations start.
818           */
819         priv = GET_PRIV (view);
820
821         priv->last_timestamp = 0;
822         if (priv->last_contact) {
823                 g_object_unref (priv->last_contact);
824                 priv->last_contact = NULL;
825         }
826 }
827
828 static gboolean
829 chat_text_view_find_previous (EmpathyChatView *view,
830                                 const gchar     *search_criteria,
831                                 gboolean         new_search)
832 {
833         EmpathyChatTextViewPriv *priv;
834         GtkTextBuffer      *buffer;
835         GtkTextIter         iter_at_mark;
836         GtkTextIter         iter_match_start;
837         GtkTextIter         iter_match_end;
838         gboolean            found;
839         gboolean            from_start = FALSE;
840
841         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
842         g_return_val_if_fail (search_criteria != NULL, FALSE);
843
844         priv = GET_PRIV (view);
845
846         buffer = priv->buffer;
847
848         if (EMP_STR_EMPTY (search_criteria)) {
849                 if (priv->find_mark_previous) {
850                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
851
852                         gtk_text_buffer_move_mark (buffer,
853                                                    priv->find_mark_previous,
854                                                    &iter_at_mark);
855                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
856                                                       priv->find_mark_previous,
857                                                       0.0,
858                                                       TRUE,
859                                                       0.0,
860                                                       0.0);
861                         gtk_text_buffer_select_range (buffer,
862                                                       &iter_at_mark,
863                                                       &iter_at_mark);
864                 }
865
866                 return FALSE;
867         }
868
869         if (new_search) {
870                 from_start = TRUE;
871         }
872
873         if (priv->find_mark_previous) {
874                 gtk_text_buffer_get_iter_at_mark (buffer,
875                                                   &iter_at_mark,
876                                                   priv->find_mark_previous);
877         } else {
878                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
879                 from_start = TRUE;
880         }
881
882         priv->find_last_direction = FALSE;
883
884         found = empathy_text_iter_backward_search (&iter_at_mark,
885                                                    search_criteria,
886                                                    &iter_match_start,
887                                                    &iter_match_end,
888                                                    NULL);
889
890         if (!found) {
891                 gboolean result = FALSE;
892
893                 if (from_start) {
894                         return result;
895                 }
896
897                 /* Here we wrap around. */
898                 if (!new_search && !priv->find_wrapped) {
899                         priv->find_wrapped = TRUE;
900                         result = chat_text_view_find_previous (view,
901                                                                  search_criteria,
902                                                                  FALSE);
903                         priv->find_wrapped = FALSE;
904                 }
905
906                 return result;
907         }
908
909         /* Set new mark and show on screen */
910         if (!priv->find_mark_previous) {
911                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
912                                                                         &iter_match_start,
913                                                                         TRUE);
914         } else {
915                 gtk_text_buffer_move_mark (buffer,
916                                            priv->find_mark_previous,
917                                            &iter_match_start);
918         }
919
920         if (!priv->find_mark_next) {
921                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
922                                                                     &iter_match_end,
923                                                                     TRUE);
924         } else {
925                 gtk_text_buffer_move_mark (buffer,
926                                            priv->find_mark_next,
927                                            &iter_match_end);
928         }
929
930         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
931                                       priv->find_mark_previous,
932                                       0.0,
933                                       TRUE,
934                                       0.5,
935                                       0.5);
936
937         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
938         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
939
940         return TRUE;
941 }
942
943 static gboolean
944 chat_text_view_find_next (EmpathyChatView *view,
945                             const gchar     *search_criteria,
946                             gboolean         new_search)
947 {
948         EmpathyChatTextViewPriv *priv;
949         GtkTextBuffer      *buffer;
950         GtkTextIter         iter_at_mark;
951         GtkTextIter         iter_match_start;
952         GtkTextIter         iter_match_end;
953         gboolean            found;
954         gboolean            from_start = FALSE;
955
956         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
957         g_return_val_if_fail (search_criteria != NULL, FALSE);
958
959         priv = GET_PRIV (view);
960
961         buffer = priv->buffer;
962
963         if (EMP_STR_EMPTY (search_criteria)) {
964                 if (priv->find_mark_next) {
965                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
966
967                         gtk_text_buffer_move_mark (buffer,
968                                                    priv->find_mark_next,
969                                                    &iter_at_mark);
970                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
971                                                       priv->find_mark_next,
972                                                       0.0,
973                                                       TRUE,
974                                                       0.0,
975                                                       0.0);
976                         gtk_text_buffer_select_range (buffer,
977                                                       &iter_at_mark,
978                                                       &iter_at_mark);
979                 }
980
981                 return FALSE;
982         }
983
984         if (new_search) {
985                 from_start = TRUE;
986         }
987
988         if (priv->find_mark_next) {
989                 gtk_text_buffer_get_iter_at_mark (buffer,
990                                                   &iter_at_mark,
991                                                   priv->find_mark_next);
992         } else {
993                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
994                 from_start = TRUE;
995         }
996
997         priv->find_last_direction = TRUE;
998
999         found = empathy_text_iter_forward_search (&iter_at_mark,
1000                                                   search_criteria,
1001                                                   &iter_match_start,
1002                                                   &iter_match_end,
1003                                                   NULL);
1004
1005         if (!found) {
1006                 gboolean result = FALSE;
1007
1008                 if (from_start) {
1009                         return result;
1010                 }
1011
1012                 /* Here we wrap around. */
1013                 if (!new_search && !priv->find_wrapped) {
1014                         priv->find_wrapped = TRUE;
1015                         result = chat_text_view_find_next (view,
1016                                                              search_criteria,
1017                                                              FALSE);
1018                         priv->find_wrapped = FALSE;
1019                 }
1020
1021                 return result;
1022         }
1023
1024         /* Set new mark and show on screen */
1025         if (!priv->find_mark_next) {
1026                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1027                                                                     &iter_match_end,
1028                                                                     TRUE);
1029         } else {
1030                 gtk_text_buffer_move_mark (buffer,
1031                                            priv->find_mark_next,
1032                                            &iter_match_end);
1033         }
1034
1035         if (!priv->find_mark_previous) {
1036                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1037                                                                         &iter_match_start,
1038                                                                         TRUE);
1039         } else {
1040                 gtk_text_buffer_move_mark (buffer,
1041                                            priv->find_mark_previous,
1042                                            &iter_match_start);
1043         }
1044
1045         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1046                                       priv->find_mark_next,
1047                                       0.0,
1048                                       TRUE,
1049                                       0.5,
1050                                       0.5);
1051
1052         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1053         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1054
1055         return TRUE;
1056 }
1057
1058 static void
1059 chat_text_view_find_abilities (EmpathyChatView *view,
1060                                  const gchar    *search_criteria,
1061                                  gboolean       *can_do_previous,
1062                                  gboolean       *can_do_next)
1063 {
1064         EmpathyChatTextViewPriv *priv;
1065         GtkTextBuffer           *buffer;
1066         GtkTextIter              iter_at_mark;
1067         GtkTextIter              iter_match_start;
1068         GtkTextIter              iter_match_end;
1069
1070         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1071         g_return_if_fail (search_criteria != NULL);
1072         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1073
1074         priv = GET_PRIV (view);
1075
1076         buffer = priv->buffer;
1077
1078         if (can_do_previous) {
1079                 if (priv->find_mark_previous) {
1080                         gtk_text_buffer_get_iter_at_mark (buffer,
1081                                                           &iter_at_mark,
1082                                                           priv->find_mark_previous);
1083                 } else {
1084                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1085                 }
1086
1087                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1088                                                                       search_criteria,
1089                                                                       &iter_match_start,
1090                                                                       &iter_match_end,
1091                                                                       NULL);
1092         }
1093
1094         if (can_do_next) {
1095                 if (priv->find_mark_next) {
1096                         gtk_text_buffer_get_iter_at_mark (buffer,
1097                                                           &iter_at_mark,
1098                                                           priv->find_mark_next);
1099                 } else {
1100                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1101                 }
1102
1103                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1104                                                                  search_criteria,
1105                                                                  &iter_match_start,
1106                                                                  &iter_match_end,
1107                                                                  NULL);
1108         }
1109 }
1110
1111 static void
1112 chat_text_view_highlight (EmpathyChatView *view,
1113                             const gchar     *text)
1114 {
1115         GtkTextBuffer *buffer;
1116         GtkTextIter    iter;
1117         GtkTextIter    iter_start;
1118         GtkTextIter    iter_end;
1119         GtkTextIter    iter_match_start;
1120         GtkTextIter    iter_match_end;
1121         gboolean       found;
1122
1123         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1124
1125         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1126
1127         gtk_text_buffer_get_start_iter (buffer, &iter);
1128
1129         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1130         gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1131                                             &iter_start,
1132                                             &iter_end);
1133
1134         if (EMP_STR_EMPTY (text)) {
1135                 return;
1136         }
1137
1138         while (1) {
1139                 found = empathy_text_iter_forward_search (&iter,
1140                                                           text,
1141                                                           &iter_match_start,
1142                                                           &iter_match_end,
1143                                                           NULL);
1144
1145                 if (!found) {
1146                         break;
1147                 }
1148
1149                 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1150                                                    &iter_match_start,
1151                                                    &iter_match_end);
1152
1153                 iter = iter_match_end;
1154                 gtk_text_iter_forward_char (&iter);
1155         }
1156 }
1157
1158 static void
1159 chat_text_view_copy_clipboard (EmpathyChatView *view)
1160 {
1161         GtkTextBuffer *buffer;
1162         GtkClipboard  *clipboard;
1163
1164         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1165
1166         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1167         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1168
1169         gtk_text_buffer_copy_clipboard (buffer, clipboard);
1170 }
1171
1172 static void
1173 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1174 {
1175         iface->append_message = chat_text_view_append_message;
1176         iface->append_event = chat_text_view_append_event;
1177         iface->scroll = chat_text_view_scroll;
1178         iface->scroll_down = chat_text_view_scroll_down;
1179         iface->get_has_selection = chat_text_view_get_has_selection;
1180         iface->clear = chat_text_view_clear;
1181         iface->find_previous = chat_text_view_find_previous;
1182         iface->find_next = chat_text_view_find_next;
1183         iface->find_abilities = chat_text_view_find_abilities;
1184         iface->highlight = chat_text_view_highlight;
1185         iface->copy_clipboard = chat_text_view_copy_clipboard;
1186 }
1187
1188 EmpathyContact *
1189 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1190 {
1191         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1192
1193         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1194
1195         return priv->last_contact;
1196 }
1197
1198 void
1199 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1200                                          gboolean             only_if_date)
1201 {
1202         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1203
1204         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1205
1206         if (only_if_date != priv->only_if_date) {
1207                 priv->only_if_date = only_if_date;
1208                 g_object_notify (G_OBJECT (view), "only-if-date");
1209         }
1210 }
1211
1212 static void
1213 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1214                                            GtkTextIter         *iter,
1215                                            const gchar         *str)
1216 {
1217         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1218         gboolean                 use_smileys = FALSE;
1219         GSList                  *smileys, *l;
1220
1221         empathy_conf_get_bool (empathy_conf_get (),
1222                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1223                                &use_smileys);
1224
1225         if (!use_smileys) {
1226                 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1227                 return;
1228         }
1229
1230         smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1231         for (l = smileys; l; l = l->next) {
1232                 EmpathySmiley *smiley;
1233
1234                 smiley = l->data;
1235                 if (smiley->pixbuf) {
1236                         gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1237                 } else {
1238                         gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1239                 }
1240                 empathy_smiley_free (smiley);
1241         }
1242         g_slist_free (smileys);
1243 }
1244
1245 void
1246 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1247                                     const gchar         *body,
1248                                     const gchar         *tag)
1249 {
1250         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1251         GtkTextIter              start_iter, end_iter;
1252         GtkTextMark             *mark;
1253         GtkTextIter              iter;
1254         GRegex                  *uri_regex;
1255         GMatchInfo              *match_info;
1256         gboolean                 match;
1257         gint                     last = 0;
1258         gint                     s = 0, e = 0;
1259         gchar                   *tmp;
1260
1261         priv = GET_PRIV (view);
1262
1263         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1264         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1265
1266         uri_regex = empathy_uri_regex_dup_singleton ();
1267         for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1268              match = g_match_info_next (match_info, NULL)) {
1269                 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1270                         continue;
1271
1272                 if (s > last) {
1273                         tmp = empathy_substring (body, last, s);
1274
1275                         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1276                         chat_text_view_insert_text_with_emoticons (view,
1277                                                                    &iter,
1278                                                                    tmp);
1279                         g_free (tmp);
1280                 }
1281
1282                 tmp = empathy_substring (body, s, e);
1283
1284                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1285                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1286                                                           &iter,
1287                                                           tmp,
1288                                                           -1,
1289                                                           EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1290                                                           NULL);
1291
1292                 g_free (tmp);
1293                 last = e;
1294         }
1295         g_match_info_free (match_info);
1296         g_regex_unref (uri_regex);
1297
1298         if (last < strlen (body)) {
1299                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1300                 chat_text_view_insert_text_with_emoticons (view,
1301                                                            &iter,
1302                                                            body + last);
1303         }
1304
1305         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1306         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1307
1308         /* Apply the style to the inserted text. */
1309         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1310         gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1311
1312         gtk_text_buffer_apply_tag_by_name (priv->buffer,
1313                                            tag,
1314                                            &start_iter,
1315                                            &end_iter);
1316
1317         gtk_text_buffer_delete_mark (priv->buffer, mark);
1318 }
1319
1320 void
1321 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1322 {
1323         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1324         GtkTextIter              iter;
1325
1326         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1327         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1328                                                   &iter,
1329                                                   "\n",
1330                                                   -1,
1331                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1332                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1333                                                   NULL);
1334 }
1335
1336 GtkTextTag *
1337 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1338                                 const gchar         *tag_name,
1339                                 const gchar         *first_property_name,
1340                                 ...)
1341 {
1342         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1343         GtkTextTag              *tag;
1344         GtkTextTagTable         *table;
1345         va_list                  list;
1346
1347         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1348         g_return_val_if_fail (tag_name != NULL, NULL);
1349
1350         table = gtk_text_buffer_get_tag_table (priv->buffer);
1351         tag = gtk_text_tag_table_lookup (table, tag_name);
1352
1353         if (tag && first_property_name) {
1354                 va_start (list, first_property_name);
1355                 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1356                 va_end (list);
1357         }
1358
1359         return tag;
1360 }
1361