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