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