]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-text-view.c
Remove useless glade include
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatTextView *view)
235 {
236         empathy_chat_view_clear (EMPATHY_CHAT_VIEW (view));
237 }
238
239 static void
240 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
241 {
242         empathy_url_show (GTK_WIDGET (menuitem), url);
243 }
244
245 static void
246 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
247 {
248         GtkClipboard *clipboard;
249         
250         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
251         gtk_clipboard_set_text (clipboard, url, -1);
252         
253         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
254         gtk_clipboard_set_text (clipboard, url, -1);
255 }
256
257 static void
258 chat_text_view_populate_popup (EmpathyChatTextView *view,
259                           GtkMenu        *menu,
260                           gpointer        user_data)
261 {
262         EmpathyChatTextViewPriv *priv;
263         GtkTextTagTable    *table;
264         GtkTextTag         *tag;
265         gint                x, y;
266         GtkTextIter         iter, start, end;
267         GtkWidget          *item;
268         gchar              *str = NULL;
269         
270         priv = GET_PRIV (view);
271         
272         /* Clear menu item */
273         if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
274                 item = gtk_menu_item_new ();
275                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
276                 gtk_widget_show (item);
277                 
278                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
279                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
280                 gtk_widget_show (item);
281                 
282                 g_signal_connect (item,
283                                   "activate",
284                                   G_CALLBACK (chat_text_view_clear_view_cb),
285                                   view);
286         }
287         
288         /* Link context menu items */
289         table = gtk_text_buffer_get_tag_table (priv->buffer);
290         tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
291         
292         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
293         
294         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
295                                                GTK_TEXT_WINDOW_WIDGET,
296                                                x, y,
297                                                &x, &y);
298         
299         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
300         
301         start = end = iter;
302         
303         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
304             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
305                     str = gtk_text_buffer_get_text (priv->buffer,
306                                                     &start, &end, FALSE);
307             }
308         
309         if (EMP_STR_EMPTY (str)) {
310                 g_free (str);
311                 return;
312         }
313         
314         /* NOTE: Set data just to get the string freed when not needed. */
315         g_object_set_data_full (G_OBJECT (menu),
316                                 "url", str,
317                                 (GDestroyNotify) g_free);
318         
319         item = gtk_menu_item_new ();
320         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
321         gtk_widget_show (item);
322         
323         item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
324         g_signal_connect (item,
325                           "activate",
326                           G_CALLBACK (chat_text_view_copy_address_cb),
327                           str);
328         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329         gtk_widget_show (item);
330         
331         item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
332         g_signal_connect (item,
333                           "activate",
334                           G_CALLBACK (chat_text_view_open_address_cb),
335                           str);
336         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
337         gtk_widget_show (item);
338 }
339
340 static gboolean
341 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
342 {
343         GtkWidget *sw;
344         
345         sw = gtk_widget_get_parent (GTK_WIDGET (view));
346         if (GTK_IS_SCROLLED_WINDOW (sw)) {
347                 GtkAdjustment *vadj;
348                 
349                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
350                 
351                 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
352                         return FALSE;
353                 }
354         }
355         
356         return TRUE;
357 }
358
359 static void
360 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
361 {
362         EmpathyChatTextViewPriv *priv;
363         GtkTextIter         top, bottom;
364         gint                line;
365         gint                remove;
366         GtkTextTagTable    *table;
367         GtkTextTag         *tag;
368         
369         priv = GET_PRIV (view);
370         
371         gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
372         line = gtk_text_iter_get_line (&bottom);
373         if (line < MAX_LINES) {
374                 return;
375         }
376         
377         remove = line - MAX_LINES;
378         gtk_text_buffer_get_start_iter (priv->buffer, &top);
379         
380         bottom = top;
381         if (!gtk_text_iter_forward_lines (&bottom, remove)) {
382                 return;
383         }
384         
385         /* Track backwords to a place where we can safely cut, we don't do it in
386           * the middle of a tag.
387           */
388         table = gtk_text_buffer_get_tag_table (priv->buffer);
389         tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
390         if (!tag) {
391                 return;
392         }
393         
394         if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
395                 return;
396         }
397         
398         if (!gtk_text_iter_equal (&top, &bottom)) {
399                 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
400         }
401 }
402
403 static void
404 chat_text_view_append_timestamp (EmpathyChatTextView *view,
405                                  time_t               timestamp,
406                                  gboolean             show_date)
407 {
408         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
409         GtkTextIter              iter;
410         gchar                   *tmp;
411         GString                 *str;
412
413         str = g_string_new ("- ");
414
415         /* Append date if needed */
416         if (show_date) {
417                 GDate *date;
418                 gchar  buf[256];
419
420                 date = g_date_new ();
421                 g_date_set_time_t (date, timestamp);
422                 /* Translators: timestamp displayed between conversations in
423                  * chat windows (strftime format string) */
424                 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
425                 g_string_append (str, buf);
426                 g_string_append (str, ", ");
427                 g_date_free (date);
428         }
429
430         /* Append time */
431         tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
432         g_string_append (str, tmp);
433         g_free (tmp);
434
435         g_string_append (str, " -\n");
436
437         /* Insert the string in the buffer */
438         empathy_chat_text_view_append_spacing (view);
439         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
440         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
441                                                   &iter,
442                                                   str->str, -1,
443                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
444                                                   NULL);
445
446         priv->last_timestamp = timestamp;
447
448         g_string_free (str, TRUE);      
449 }
450
451 static void
452 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
453                                       time_t               timestamp)
454 {
455         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
456         GDate                   *date, *last_date;
457         gboolean                 append_date = FALSE;
458         gboolean                 append_time = FALSE;
459
460         /* Get the date from last message */
461         last_date = g_date_new ();
462         g_date_set_time_t (last_date, priv->last_timestamp);
463
464         /* Get the date of the message we are appending */
465         date = g_date_new ();
466         g_date_set_time_t (date, timestamp);
467
468         /* If last message was from another day we append date and time */
469         if (g_date_compare (date, last_date) > 0) {
470                 append_date = TRUE;
471                 append_time = TRUE;
472         }
473         
474         g_date_free (last_date);
475         g_date_free (date);
476
477         /* If last message is 'old' append the time */
478         if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
479                 append_time = TRUE;
480         }
481
482         if (append_date || (!priv->only_if_date && append_time)) {
483                 chat_text_view_append_timestamp (view, timestamp, append_date);
484         }
485 }
486
487 static void
488 chat_text_view_size_allocate (GtkWidget     *widget,
489                               GtkAllocation *alloc)
490 {
491         gboolean down;
492         
493         down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
494         
495         GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
496         
497         if (down) {
498                 GtkAdjustment *adj;
499                 
500                 adj = GTK_TEXT_VIEW (widget)->vadjustment;
501                 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
502         }
503 }
504
505 static gboolean
506 chat_text_view_drag_motion (GtkWidget      *widget,
507                             GdkDragContext *context,
508                             gint            x,
509                             gint            y,
510                             guint           time)
511 {
512         /* Don't handle drag motion, since we don't want the view to scroll as
513          * the result of dragging something across it. */
514         
515         return FALSE;
516 }
517
518 static void
519 chat_text_view_get_property (GObject    *object,
520                              guint       param_id,
521                              GValue     *value,
522                              GParamSpec *pspec)
523 {
524         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
525
526         switch (param_id) {
527         case PROP_LAST_CONTACT:
528                 g_value_set_object (value, priv->last_contact);
529                 break;
530         case PROP_ONLY_IF_DATE:
531                 g_value_set_boolean (value, priv->only_if_date);
532                 break;
533         default:
534                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
535                 break;
536         };
537 }
538
539 static void
540 chat_text_view_set_property (GObject      *object,
541                              guint         param_id,
542                              const GValue *value,
543                              GParamSpec   *pspec)
544 {
545         EmpathyChatTextViewPriv *priv = GET_PRIV (object);
546
547         switch (param_id) {
548         case PROP_ONLY_IF_DATE:
549                 priv->only_if_date = g_value_get_boolean (value);
550                 break;
551         default:
552                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
553                 break;
554         };
555 }
556
557 static void
558 chat_text_view_finalize (GObject *object)
559 {
560         EmpathyChatTextView     *view;
561         EmpathyChatTextViewPriv *priv;
562         
563         view = EMPATHY_CHAT_TEXT_VIEW (object);
564         priv = GET_PRIV (view);
565         
566         DEBUG ("%p", object);
567         
568         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
569         
570         if (priv->last_contact) {
571                 g_object_unref (priv->last_contact);
572         }
573         if (priv->scroll_time) {
574                 g_timer_destroy (priv->scroll_time);
575         }
576         if (priv->scroll_timeout) {
577                 g_source_remove (priv->scroll_timeout);
578         }
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 = adj->upper - adj->page_size;
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 }
823
824 static gboolean
825 chat_text_view_find_previous (EmpathyChatView *view,
826                                 const gchar     *search_criteria,
827                                 gboolean         new_search)
828 {
829         EmpathyChatTextViewPriv *priv;
830         GtkTextBuffer      *buffer;
831         GtkTextIter         iter_at_mark;
832         GtkTextIter         iter_match_start;
833         GtkTextIter         iter_match_end;
834         gboolean            found;
835         gboolean            from_start = FALSE;
836         
837         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
838         g_return_val_if_fail (search_criteria != NULL, FALSE);
839         
840         priv = GET_PRIV (view);
841         
842         buffer = priv->buffer;
843         
844         if (EMP_STR_EMPTY (search_criteria)) {
845                 if (priv->find_mark_previous) {
846                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
847                         
848                         gtk_text_buffer_move_mark (buffer,
849                                                    priv->find_mark_previous,
850                                                    &iter_at_mark);
851                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
852                                                       priv->find_mark_previous,
853                                                       0.0,
854                                                       TRUE,
855                                                       0.0,
856                                                       0.0);
857                         gtk_text_buffer_select_range (buffer,
858                                                       &iter_at_mark,
859                                                       &iter_at_mark);
860                 }
861                 
862                 return FALSE;
863         }
864         
865         if (new_search) {
866                 from_start = TRUE;
867         }
868         
869         if (priv->find_mark_previous) {
870                 gtk_text_buffer_get_iter_at_mark (buffer,
871                                                   &iter_at_mark,
872                                                   priv->find_mark_previous);
873         } else {
874                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
875                 from_start = TRUE;
876         }
877         
878         priv->find_last_direction = FALSE;
879         
880         found = empathy_text_iter_backward_search (&iter_at_mark,
881                                                    search_criteria,
882                                                    &iter_match_start,
883                                                    &iter_match_end,
884                                                    NULL);
885         
886         if (!found) {
887                 gboolean result = FALSE;
888                 
889                 if (from_start) {
890                         return result;
891                 }
892                 
893                 /* Here we wrap around. */
894                 if (!new_search && !priv->find_wrapped) {
895                         priv->find_wrapped = TRUE;
896                         result = chat_text_view_find_previous (view,
897                                                                  search_criteria, 
898                                                                  FALSE);
899                         priv->find_wrapped = FALSE;
900                 }
901                 
902                 return result;
903         }
904         
905         /* Set new mark and show on screen */
906         if (!priv->find_mark_previous) {
907                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
908                                                                         &iter_match_start,
909                                                                         TRUE);
910         } else {
911                 gtk_text_buffer_move_mark (buffer,
912                                            priv->find_mark_previous,
913                                            &iter_match_start);
914         }
915         
916         if (!priv->find_mark_next) {
917                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
918                                                                     &iter_match_end,
919                                                                     TRUE);
920         } else {
921                 gtk_text_buffer_move_mark (buffer,
922                                            priv->find_mark_next,
923                                            &iter_match_end);
924         }
925         
926         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
927                                       priv->find_mark_previous,
928                                       0.0,
929                                       TRUE,
930                                       0.5,
931                                       0.5);
932         
933         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
934         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
935         
936         return TRUE;
937 }
938
939 static gboolean
940 chat_text_view_find_next (EmpathyChatView *view,
941                             const gchar     *search_criteria,
942                             gboolean         new_search)
943 {
944         EmpathyChatTextViewPriv *priv;
945         GtkTextBuffer      *buffer;
946         GtkTextIter         iter_at_mark;
947         GtkTextIter         iter_match_start;
948         GtkTextIter         iter_match_end;
949         gboolean            found;
950         gboolean            from_start = FALSE;
951         
952         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
953         g_return_val_if_fail (search_criteria != NULL, FALSE);
954         
955         priv = GET_PRIV (view);
956         
957         buffer = priv->buffer;
958         
959         if (EMP_STR_EMPTY (search_criteria)) {
960                 if (priv->find_mark_next) {
961                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
962                         
963                         gtk_text_buffer_move_mark (buffer,
964                                                    priv->find_mark_next,
965                                                    &iter_at_mark);
966                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
967                                                       priv->find_mark_next,
968                                                       0.0,
969                                                       TRUE,
970                                                       0.0,
971                                                       0.0);
972                         gtk_text_buffer_select_range (buffer,
973                                                       &iter_at_mark,
974                                                       &iter_at_mark);
975                 }
976                 
977                 return FALSE;
978         }
979         
980         if (new_search) {
981                 from_start = TRUE;
982         }
983         
984         if (priv->find_mark_next) {
985                 gtk_text_buffer_get_iter_at_mark (buffer,
986                                                   &iter_at_mark,
987                                                   priv->find_mark_next);
988         } else {
989                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
990                 from_start = TRUE;
991         }
992         
993         priv->find_last_direction = TRUE;
994         
995         found = empathy_text_iter_forward_search (&iter_at_mark,
996                                                   search_criteria,
997                                                   &iter_match_start,
998                                                   &iter_match_end,
999                                                   NULL);
1000         
1001         if (!found) {
1002                 gboolean result = FALSE;
1003                 
1004                 if (from_start) {
1005                         return result;
1006                 }
1007                 
1008                 /* Here we wrap around. */
1009                 if (!new_search && !priv->find_wrapped) {
1010                         priv->find_wrapped = TRUE;
1011                         result = chat_text_view_find_next (view, 
1012                                                              search_criteria, 
1013                                                              FALSE);
1014                         priv->find_wrapped = FALSE;
1015                 }
1016                 
1017                 return result;
1018         }
1019         
1020         /* Set new mark and show on screen */
1021         if (!priv->find_mark_next) {
1022                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1023                                                                     &iter_match_end,
1024                                                                     TRUE);
1025         } else {
1026                 gtk_text_buffer_move_mark (buffer,
1027                                            priv->find_mark_next,
1028                                            &iter_match_end);
1029         }
1030         
1031         if (!priv->find_mark_previous) {
1032                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1033                                                                         &iter_match_start,
1034                                                                         TRUE);
1035         } else {
1036                 gtk_text_buffer_move_mark (buffer,
1037                                            priv->find_mark_previous,
1038                                            &iter_match_start);
1039         }
1040         
1041         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1042                                       priv->find_mark_next,
1043                                       0.0,
1044                                       TRUE,
1045                                       0.5,
1046                                       0.5);
1047         
1048         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1049         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1050         
1051         return TRUE;
1052 }
1053
1054 static void
1055 chat_text_view_find_abilities (EmpathyChatView *view,
1056                                  const gchar    *search_criteria,
1057                                  gboolean       *can_do_previous,
1058                                  gboolean       *can_do_next)
1059 {
1060         EmpathyChatTextViewPriv *priv;
1061         GtkTextBuffer           *buffer;
1062         GtkTextIter              iter_at_mark;
1063         GtkTextIter              iter_match_start;
1064         GtkTextIter              iter_match_end;
1065         
1066         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1067         g_return_if_fail (search_criteria != NULL);
1068         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1069         
1070         priv = GET_PRIV (view);
1071         
1072         buffer = priv->buffer;
1073         
1074         if (can_do_previous) {
1075                 if (priv->find_mark_previous) {
1076                         gtk_text_buffer_get_iter_at_mark (buffer,
1077                                                           &iter_at_mark,
1078                                                           priv->find_mark_previous);
1079                 } else {
1080                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1081                 }
1082                 
1083                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1084                                                                       search_criteria,
1085                                                                       &iter_match_start,
1086                                                                       &iter_match_end,
1087                                                                       NULL);
1088         }
1089         
1090         if (can_do_next) {
1091                 if (priv->find_mark_next) {
1092                         gtk_text_buffer_get_iter_at_mark (buffer,
1093                                                           &iter_at_mark,
1094                                                           priv->find_mark_next);
1095                 } else {
1096                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1097                 }
1098                 
1099                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1100                                                                  search_criteria,
1101                                                                  &iter_match_start,
1102                                                                  &iter_match_end,
1103                                                                  NULL);
1104         }
1105 }
1106
1107 static void
1108 chat_text_view_highlight (EmpathyChatView *view,
1109                             const gchar     *text)
1110 {
1111         GtkTextBuffer *buffer;
1112         GtkTextIter    iter;
1113         GtkTextIter    iter_start;
1114         GtkTextIter    iter_end;
1115         GtkTextIter    iter_match_start;
1116         GtkTextIter    iter_match_end;
1117         gboolean       found;
1118         
1119         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1120         
1121         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1122         
1123         gtk_text_buffer_get_start_iter (buffer, &iter);
1124         
1125         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1126         gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1127                                             &iter_start,
1128                                             &iter_end);
1129         
1130         if (EMP_STR_EMPTY (text)) {
1131                 return;
1132         }
1133         
1134         while (1) {
1135                 found = empathy_text_iter_forward_search (&iter,
1136                                                           text,
1137                                                           &iter_match_start,
1138                                                           &iter_match_end,
1139                                                           NULL);
1140                 
1141                 if (!found) {
1142                         break;
1143                 }
1144                 
1145                 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1146                                                    &iter_match_start,
1147                                                    &iter_match_end);
1148                 
1149                 iter = iter_match_end;
1150                 gtk_text_iter_forward_char (&iter);
1151         }
1152 }
1153
1154 static void
1155 chat_text_view_copy_clipboard (EmpathyChatView *view)
1156 {
1157         GtkTextBuffer *buffer;
1158         GtkClipboard  *clipboard;
1159         
1160         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1161         
1162         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1163         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1164         
1165         gtk_text_buffer_copy_clipboard (buffer, clipboard);
1166 }
1167
1168 static void
1169 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1170 {
1171         iface->append_message = chat_text_view_append_message;
1172         iface->append_event = chat_text_view_append_event;
1173         iface->scroll = chat_text_view_scroll;
1174         iface->scroll_down = chat_text_view_scroll_down;
1175         iface->get_has_selection = chat_text_view_get_has_selection;
1176         iface->clear = chat_text_view_clear;
1177         iface->find_previous = chat_text_view_find_previous;
1178         iface->find_next = chat_text_view_find_next;
1179         iface->find_abilities = chat_text_view_find_abilities;
1180         iface->highlight = chat_text_view_highlight;
1181         iface->copy_clipboard = chat_text_view_copy_clipboard;
1182 }
1183
1184 EmpathyContact *
1185 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1186 {
1187         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1188         
1189         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1190         
1191         return priv->last_contact;
1192 }
1193
1194 void
1195 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1196                                          gboolean             only_if_date)
1197 {
1198         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1199         
1200         g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1201
1202         if (only_if_date != priv->only_if_date) {
1203                 priv->only_if_date = only_if_date;
1204                 g_object_notify (G_OBJECT (view), "only-if-date");
1205         }
1206 }
1207
1208 static void
1209 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1210                                            GtkTextIter         *iter,
1211                                            const gchar         *str)
1212 {
1213         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1214         gboolean                 use_smileys = FALSE;
1215         GSList                  *smileys, *l;
1216
1217         empathy_conf_get_bool (empathy_conf_get (),
1218                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1219                                &use_smileys);
1220
1221         if (!use_smileys) {
1222                 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1223                 return;
1224         }
1225
1226         smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1227         for (l = smileys; l; l = l->next) {
1228                 EmpathySmiley *smiley;
1229
1230                 smiley = l->data;
1231                 if (smiley->pixbuf) {
1232                         gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1233                 } else {
1234                         gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1235                 }
1236                 empathy_smiley_free (smiley);
1237         }
1238         g_slist_free (smileys);
1239 }
1240
1241 void
1242 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1243                                     const gchar         *body,
1244                                     const gchar         *tag)
1245 {
1246         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1247         GtkTextIter              start_iter, end_iter;
1248         GtkTextMark             *mark;
1249         GtkTextIter              iter;
1250         GRegex                  *uri_regex;
1251         GMatchInfo              *match_info;
1252         gboolean                 match;
1253         gint                     last = 0;
1254         gint                     s = 0, e = 0;
1255         gchar                   *tmp;
1256
1257         priv = GET_PRIV (view);
1258
1259         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1260         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1261
1262         uri_regex = empathy_uri_regex_dup_singleton ();
1263         for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1264              match = g_match_info_next (match_info, NULL)) {
1265                 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1266                         continue;
1267
1268                 if (s > last) {
1269                         tmp = empathy_substring (body, last, s);
1270
1271                         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1272                         chat_text_view_insert_text_with_emoticons (view,
1273                                                                    &iter,
1274                                                                    tmp);
1275                         g_free (tmp);
1276                 }
1277
1278                 tmp = empathy_substring (body, s, e);
1279
1280                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1281                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1282                                                           &iter,
1283                                                           tmp,
1284                                                           -1,
1285                                                           EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1286                                                           NULL);
1287
1288                 g_free (tmp);
1289                 last = e;
1290         }
1291         g_match_info_free (match_info);
1292         g_regex_unref (uri_regex);
1293
1294         if (last < strlen (body)) {
1295                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1296                 chat_text_view_insert_text_with_emoticons (view,
1297                                                            &iter,
1298                                                            body + last);
1299         }
1300
1301         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1302         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1303
1304         /* Apply the style to the inserted text. */
1305         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1306         gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1307
1308         gtk_text_buffer_apply_tag_by_name (priv->buffer,
1309                                            tag,
1310                                            &start_iter,
1311                                            &end_iter);
1312
1313         gtk_text_buffer_delete_mark (priv->buffer, mark);
1314 }
1315
1316 void
1317 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1318 {
1319         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1320         GtkTextIter              iter;
1321
1322         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1323         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1324                                                   &iter,
1325                                                   "\n",
1326                                                   -1,
1327                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1328                                                   EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1329                                                   NULL);
1330 }
1331
1332 GtkTextTag *
1333 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1334                                 const gchar         *tag_name,
1335                                 const gchar         *first_property_name,
1336                                 ...)
1337 {
1338         EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1339         GtkTextTag              *tag;
1340         GtkTextTagTable         *table;
1341         va_list                  list;
1342
1343         g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1344         g_return_val_if_fail (tag_name != NULL, NULL);
1345
1346         table = gtk_text_buffer_get_tag_table (priv->buffer);
1347         tag = gtk_text_tag_table_lookup (table, tag_name);
1348
1349         if (tag && first_property_name) {
1350                 va_start (list, first_property_name);
1351                 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1352                 va_end (list);
1353         }
1354
1355         return tag;
1356 }
1357