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