1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
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>
29 #include <sys/types.h>
33 #include <glib/gi18n-lib.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-gsettings.h>
39 #include <libempathy/empathy-utils.h>
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"
47 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
48 #include <libempathy/empathy-debug.h>
50 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
51 #define TIMESTAMP_INTERVAL 300
54 #define MAX_SCROLL_TIME 0.4 /* seconds */
55 #define SCROLL_DELAY 33 /* milliseconds */
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
60 GtkTextBuffer *buffer;
63 GtkTextMark *find_mark_previous;
64 GtkTextMark *find_mark_next;
65 gboolean find_wrapped;
66 gboolean find_last_direction;
67 EmpathyContact *last_contact;
68 time_t last_timestamp;
69 gboolean allow_scrolling;
70 guint notify_system_fonts_id;
71 GSettings *gsettings_desktop;
72 GSettings *gsettings_chat;
73 EmpathySmileyManager *smiley_manager;
74 gboolean only_if_date;
75 } EmpathyChatTextViewPriv;
77 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
79 static void chat_text_view_copy_clipboard (EmpathyChatView *view);
81 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
83 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
84 chat_text_view_iface_init));
93 chat_text_view_url_event_cb (GtkTextTag *tag,
97 EmpathyChatTextView *view)
99 EmpathyChatTextViewPriv *priv;
100 GtkTextIter start, end;
103 priv = GET_PRIV (view);
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)) {
111 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
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,
121 empathy_url_show (GTK_WIDGET (view), str);
130 chat_text_view_event_cb (EmpathyChatTextView *view,
131 GdkEventMotion *event,
134 static GdkCursor *hand = NULL;
135 static GdkCursor *beam = NULL;
136 GtkTextWindowType type;
139 gint x, y, buf_x, buf_y;
141 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
144 if (type != GTK_TEXT_WINDOW_TEXT) {
148 /* Get where the pointer really is. */
149 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
154 gdk_window_get_pointer (win, &x, &y, NULL);
156 /* Get the iter where the cursor is at */
157 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
161 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
165 if (gtk_text_iter_has_tag (&iter, tag)) {
167 hand = gdk_cursor_new (GDK_HAND2);
168 beam = gdk_cursor_new (GDK_XTERM);
170 gdk_window_set_cursor (win, hand);
173 beam = gdk_cursor_new (GDK_XTERM);
175 gdk_window_set_cursor (win, beam);
182 chat_text_view_create_tags (EmpathyChatTextView *view)
184 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
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);
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),
200 g_signal_connect (view, "motion-notify-event",
201 G_CALLBACK (chat_text_view_event_cb),
206 chat_text_view_system_font_update (EmpathyChatTextView *view)
208 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
209 PangoFontDescription *font_description = NULL;
212 font_name = g_settings_get_string (priv->gsettings_desktop,
213 EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME);
215 if (font_name != NULL) {
216 font_description = pango_font_description_from_string (font_name);
219 font_description = NULL;
222 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
224 if (font_description) {
225 pango_font_description_free (font_description);
230 chat_text_view_notify_system_font_cb (GSettings *gsettings,
232 EmpathyChatTextView *self)
234 chat_text_view_system_font_update (self);
238 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
240 empathy_url_show (GTK_WIDGET (menuitem), url);
244 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
246 GtkClipboard *clipboard;
248 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
249 gtk_clipboard_set_text (clipboard, url, -1);
251 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
252 gtk_clipboard_set_text (clipboard, url, -1);
256 chat_text_view_populate_popup (EmpathyChatTextView *view,
260 EmpathyChatTextViewPriv *priv;
261 GtkTextTagTable *table;
264 GtkTextIter iter, start, end;
268 priv = GET_PRIV (view);
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);
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);
280 g_signal_connect_swapped (item, "activate",
281 G_CALLBACK (empathy_chat_view_clear),
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);
289 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
291 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
292 GTK_TEXT_WINDOW_WIDGET,
296 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
300 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
301 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
302 str = gtk_text_buffer_get_text (priv->buffer,
303 &start, &end, FALSE);
306 if (EMP_STR_EMPTY (str)) {
311 /* NOTE: Set data just to get the string freed when not needed. */
312 g_object_set_data_full (G_OBJECT (menu),
314 (GDestroyNotify) g_free);
316 item = gtk_separator_menu_item_new ();
317 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
318 gtk_widget_show (item);
320 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
321 g_signal_connect (item, "activate",
322 G_CALLBACK (chat_text_view_copy_address_cb),
324 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
325 gtk_widget_show (item);
327 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
328 g_signal_connect (item, "activate",
329 G_CALLBACK (chat_text_view_open_address_cb),
331 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
332 gtk_widget_show (item);
336 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
343 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
344 value = gtk_adjustment_get_value (vadj);
345 upper = gtk_adjustment_get_upper (vadj);
346 page_size = gtk_adjustment_get_page_size (vadj);
348 if (value < upper - page_size) {
356 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
358 EmpathyChatTextViewPriv *priv;
359 GtkTextIter top, bottom;
362 GtkTextTagTable *table;
365 priv = GET_PRIV (view);
367 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
368 line = gtk_text_iter_get_line (&bottom);
369 if (line < MAX_LINES) {
373 remove_ = line - MAX_LINES;
374 gtk_text_buffer_get_start_iter (priv->buffer, &top);
377 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
381 /* Track backwords to a place where we can safely cut, we don't do it in
382 * the middle of a tag.
384 table = gtk_text_buffer_get_tag_table (priv->buffer);
385 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
390 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
394 if (!gtk_text_iter_equal (&top, &bottom)) {
395 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
400 chat_text_view_append_timestamp (EmpathyChatTextView *view,
404 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
409 str = g_string_new ("- ");
411 /* Append date if needed */
416 date = g_date_new ();
417 g_date_set_time_t (date, timestamp);
418 /* Translators: timestamp displayed between conversations in
419 * chat windows (strftime format string) */
420 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
421 g_string_append (str, buf);
422 g_string_append (str, ", ");
427 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
428 g_string_append (str, tmp);
431 g_string_append (str, " -\n");
433 /* Insert the string in the buffer */
434 empathy_chat_text_view_append_spacing (view);
435 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
436 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
439 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
442 g_string_free (str, TRUE);
446 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
449 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
450 GDate *date, *last_date;
451 gboolean append_date = FALSE;
452 gboolean append_time = FALSE;
454 /* Get the date from last message */
455 last_date = g_date_new ();
456 g_date_set_time_t (last_date, priv->last_timestamp);
458 /* Get the date of the message we are appending */
459 date = g_date_new ();
460 g_date_set_time_t (date, timestamp);
462 /* If last message was from another day we append date and time */
463 if (g_date_compare (date, last_date) > 0) {
468 g_date_free (last_date);
471 /* If last message is 'old' append the time */
472 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
476 if (append_date || (!priv->only_if_date && append_time)) {
477 chat_text_view_append_timestamp (view, timestamp, append_date);
482 chat_text_view_size_allocate (GtkWidget *widget,
483 GtkAllocation *alloc)
487 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
489 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
494 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (widget));
495 gtk_adjustment_set_value (adj,
496 gtk_adjustment_get_upper (adj) -
497 gtk_adjustment_get_page_size (adj));
502 chat_text_view_drag_motion (GtkWidget *widget,
503 GdkDragContext *context,
508 /* Don't handle drag motion, since we don't want the view to scroll as
509 * the result of dragging something across it. */
515 chat_text_view_get_property (GObject *object,
520 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
523 case PROP_LAST_CONTACT:
524 g_value_set_object (value, priv->last_contact);
526 case PROP_ONLY_IF_DATE:
527 g_value_set_boolean (value, priv->only_if_date);
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
536 chat_text_view_set_property (GObject *object,
541 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
544 case PROP_ONLY_IF_DATE:
545 priv->only_if_date = g_value_get_boolean (value);
548 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
554 chat_text_view_finalize (GObject *object)
556 EmpathyChatTextView *view;
557 EmpathyChatTextViewPriv *priv;
559 view = EMPATHY_CHAT_TEXT_VIEW (object);
560 priv = GET_PRIV (view);
562 DEBUG ("%p", object);
564 g_object_unref (priv->gsettings_chat);
565 g_object_unref (priv->gsettings_desktop);
567 if (priv->last_contact) {
568 g_object_unref (priv->last_contact);
570 if (priv->scroll_time) {
571 g_timer_destroy (priv->scroll_time);
573 if (priv->scroll_timeout) {
574 g_source_remove (priv->scroll_timeout);
576 g_object_unref (priv->smiley_manager);
578 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
582 text_view_copy_clipboard (GtkTextView *text_view)
584 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
588 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
590 GObjectClass *object_class = G_OBJECT_CLASS (klass);
591 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
592 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
594 object_class->finalize = chat_text_view_finalize;
595 object_class->get_property = chat_text_view_get_property;
596 object_class->set_property = chat_text_view_set_property;
598 widget_class->size_allocate = chat_text_view_size_allocate;
599 widget_class->drag_motion = chat_text_view_drag_motion;
601 text_view_class->copy_clipboard = text_view_copy_clipboard;
603 g_object_class_install_property (object_class,
605 g_param_spec_object ("last-contact",
607 "The sender of the last received message",
608 EMPATHY_TYPE_CONTACT,
610 g_object_class_install_property (object_class,
612 g_param_spec_boolean ("only-if-date",
614 "Display timestamp only if the date changes",
619 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
623 empathy_chat_text_view_init (EmpathyChatTextView *view)
625 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
626 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
629 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
630 priv->last_timestamp = 0;
631 priv->allow_scrolling = TRUE;
632 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
635 "wrap-mode", GTK_WRAP_WORD_CHAR,
637 "cursor-visible", FALSE,
640 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
642 priv->gsettings_desktop = g_settings_new (
643 EMPATHY_PREFS_DESKTOP_INTERFACE_SCHEMA);
644 g_signal_connect (priv->gsettings_desktop,
645 "changed::" EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME,
646 G_CALLBACK (chat_text_view_notify_system_font_cb),
648 chat_text_view_system_font_update (view);
649 chat_text_view_create_tags (view);
651 g_signal_connect (view,
653 G_CALLBACK (chat_text_view_populate_popup),
658 chat_text_view_scroll_stop (EmpathyChatTextView *view)
660 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
662 g_timer_destroy (priv->scroll_time);
663 priv->scroll_time = NULL;
665 g_source_remove (priv->scroll_timeout);
666 priv->scroll_timeout = 0;
669 /* Code stolen from pidgin/gtkimhtml.c */
671 chat_text_view_scroll_cb (EmpathyChatTextView *view)
673 EmpathyChatTextViewPriv *priv;
677 priv = GET_PRIV (view);
679 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
680 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
682 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
684 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
685 /* time's up. jump to the end and kill the timer */
686 gtk_adjustment_set_value (adj, max_val);
687 chat_text_view_scroll_stop (view);
691 /* scroll by 1/3rd the remaining distance */
692 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
697 chat_text_view_scroll_down (EmpathyChatView *view)
699 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
701 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
703 if (!priv->allow_scrolling) {
707 DEBUG ("Scrolling down");
709 if (priv->scroll_time) {
710 g_timer_reset (priv->scroll_time);
712 priv->scroll_time = g_timer_new ();
714 if (!priv->scroll_timeout) {
715 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
716 (GSourceFunc) chat_text_view_scroll_cb,
722 chat_text_view_append_message (EmpathyChatView *view,
725 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
726 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
730 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
731 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
733 if (!empathy_message_get_body (msg)) {
737 bottom = chat_text_view_is_scrolled_down (text_view);
739 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
741 timestamp = empathy_message_get_timestamp (msg);
742 chat_text_maybe_append_date_and_time (text_view, timestamp);
743 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
744 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
749 chat_text_view_scroll_down (view);
752 if (priv->last_contact) {
753 g_object_unref (priv->last_contact);
755 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
756 g_object_notify (G_OBJECT (view), "last-contact");
758 priv->last_timestamp = timestamp;
762 chat_text_view_append_event (EmpathyChatView *view,
765 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
766 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
772 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
773 g_return_if_fail (!EMP_STR_EMPTY (str));
775 bottom = chat_text_view_is_scrolled_down (text_view);
776 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
777 chat_text_maybe_append_date_and_time (text_view,
778 empathy_time_get_current ());
780 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
781 msg = g_strdup_printf (" - %s\n", str);
782 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
784 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
789 chat_text_view_scroll_down (view);
792 if (priv->last_contact) {
793 g_object_unref (priv->last_contact);
794 priv->last_contact = NULL;
795 g_object_notify (G_OBJECT (view), "last-contact");
800 chat_text_view_scroll (EmpathyChatView *view,
801 gboolean allow_scrolling)
803 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
805 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
807 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
809 priv->allow_scrolling = allow_scrolling;
810 if (allow_scrolling) {
811 empathy_chat_view_scroll_down (view);
816 chat_text_view_get_has_selection (EmpathyChatView *view)
818 GtkTextBuffer *buffer;
820 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
822 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
824 return gtk_text_buffer_get_has_selection (buffer);
828 chat_text_view_clear (EmpathyChatView *view)
830 GtkTextBuffer *buffer;
831 EmpathyChatTextViewPriv *priv;
833 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
835 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
836 gtk_text_buffer_set_text (buffer, "", -1);
838 /* We set these back to the initial values so we get
839 * timestamps when clearing the window to know when
840 * conversations start.
842 priv = GET_PRIV (view);
844 priv->last_timestamp = 0;
845 if (priv->last_contact) {
846 g_object_unref (priv->last_contact);
847 priv->last_contact = NULL;
852 chat_text_view_find_previous (EmpathyChatView *view,
853 const gchar *search_criteria,
857 EmpathyChatTextViewPriv *priv;
858 GtkTextBuffer *buffer;
859 GtkTextIter iter_at_mark;
860 GtkTextIter iter_match_start;
861 GtkTextIter iter_match_end;
863 gboolean from_start = FALSE;
865 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
866 g_return_val_if_fail (search_criteria != NULL, FALSE);
868 priv = GET_PRIV (view);
870 buffer = priv->buffer;
872 if (EMP_STR_EMPTY (search_criteria)) {
873 if (priv->find_mark_previous) {
874 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
876 gtk_text_buffer_move_mark (buffer,
877 priv->find_mark_previous,
879 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
880 priv->find_mark_previous,
885 gtk_text_buffer_select_range (buffer,
897 if (!new_search && priv->find_mark_previous) {
898 gtk_text_buffer_get_iter_at_mark (buffer,
900 priv->find_mark_previous);
902 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
906 priv->find_last_direction = FALSE;
908 /* Use the standard GTK+ method for case sensitive searches. It can't do
909 * case insensitive searches (see bug #61852), so keep the custom method
910 * around for case insensitive searches. */
912 found = gtk_text_iter_backward_search (&iter_at_mark,
914 0, /* no text search flags, we want exact matches */
919 found = empathy_text_iter_backward_search (&iter_at_mark,
927 gboolean result = FALSE;
933 /* Here we wrap around. */
934 if (!new_search && !priv->find_wrapped) {
935 priv->find_wrapped = TRUE;
936 result = chat_text_view_find_previous (view,
940 priv->find_wrapped = FALSE;
946 /* Set new mark and show on screen */
947 if (!priv->find_mark_previous) {
948 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
952 gtk_text_buffer_move_mark (buffer,
953 priv->find_mark_previous,
957 if (!priv->find_mark_next) {
958 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
962 gtk_text_buffer_move_mark (buffer,
963 priv->find_mark_next,
967 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
968 priv->find_mark_previous,
974 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
975 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
981 chat_text_view_find_next (EmpathyChatView *view,
982 const gchar *search_criteria,
986 EmpathyChatTextViewPriv *priv;
987 GtkTextBuffer *buffer;
988 GtkTextIter iter_at_mark;
989 GtkTextIter iter_match_start;
990 GtkTextIter iter_match_end;
992 gboolean from_start = FALSE;
994 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
995 g_return_val_if_fail (search_criteria != NULL, FALSE);
997 priv = GET_PRIV (view);
999 buffer = priv->buffer;
1001 if (EMP_STR_EMPTY (search_criteria)) {
1002 if (priv->find_mark_next) {
1003 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1005 gtk_text_buffer_move_mark (buffer,
1006 priv->find_mark_next,
1008 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1009 priv->find_mark_next,
1014 gtk_text_buffer_select_range (buffer,
1026 if (!new_search && priv->find_mark_next) {
1027 gtk_text_buffer_get_iter_at_mark (buffer,
1029 priv->find_mark_next);
1031 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1035 priv->find_last_direction = TRUE;
1037 /* Use the standard GTK+ method for case sensitive searches. It can't do
1038 * case insensitive searches (see bug #61852), so keep the custom method
1039 * around for case insensitive searches. */
1041 found = gtk_text_iter_forward_search (&iter_at_mark,
1048 found = empathy_text_iter_forward_search (&iter_at_mark,
1056 gboolean result = FALSE;
1062 /* Here we wrap around. */
1063 if (!new_search && !priv->find_wrapped) {
1064 priv->find_wrapped = TRUE;
1065 result = chat_text_view_find_next (view,
1069 priv->find_wrapped = FALSE;
1075 /* Set new mark and show on screen */
1076 if (!priv->find_mark_next) {
1077 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1081 gtk_text_buffer_move_mark (buffer,
1082 priv->find_mark_next,
1086 if (!priv->find_mark_previous) {
1087 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1091 gtk_text_buffer_move_mark (buffer,
1092 priv->find_mark_previous,
1096 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1097 priv->find_mark_next,
1103 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1104 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1110 chat_text_view_find_abilities (EmpathyChatView *view,
1111 const gchar *search_criteria,
1112 gboolean match_case,
1113 gboolean *can_do_previous,
1114 gboolean *can_do_next)
1116 EmpathyChatTextViewPriv *priv;
1117 GtkTextBuffer *buffer;
1118 GtkTextIter iter_at_mark;
1119 GtkTextIter iter_match_start;
1120 GtkTextIter iter_match_end;
1122 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1123 g_return_if_fail (search_criteria != NULL);
1124 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1126 priv = GET_PRIV (view);
1128 buffer = priv->buffer;
1130 if (can_do_previous) {
1131 if (priv->find_mark_previous) {
1132 gtk_text_buffer_get_iter_at_mark (buffer,
1134 priv->find_mark_previous);
1136 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1140 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1147 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1156 if (priv->find_mark_next) {
1157 gtk_text_buffer_get_iter_at_mark (buffer,
1159 priv->find_mark_next);
1161 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1165 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1172 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1182 chat_text_view_highlight (EmpathyChatView *view,
1184 gboolean match_case)
1186 GtkTextBuffer *buffer;
1188 GtkTextIter iter_start;
1189 GtkTextIter iter_end;
1190 GtkTextIter iter_match_start;
1191 GtkTextIter iter_match_end;
1194 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1196 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1198 gtk_text_buffer_get_start_iter (buffer, &iter);
1200 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1201 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1205 if (EMP_STR_EMPTY (text)) {
1211 found = gtk_text_iter_forward_search (&iter,
1218 found = empathy_text_iter_forward_search (&iter,
1228 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1232 iter = iter_match_end;
1237 chat_text_view_copy_clipboard (EmpathyChatView *view)
1239 GtkTextBuffer *buffer;
1240 GtkTextIter start, iter, end;
1241 GtkClipboard *clipboard;
1244 GtkTextChildAnchor *anchor = NULL;
1247 gboolean ignore_newlines = FALSE;
1249 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1251 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1252 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1254 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1257 str = g_string_new ("");
1259 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1260 c = gtk_text_iter_get_char (&iter);
1261 /* 0xFFFC is the 'object replacement' unicode character,
1262 * it indicates the presence of a pixbuf or a widget. */
1264 ignore_newlines = FALSE;
1265 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1267 text = g_object_get_data (G_OBJECT(pixbuf),
1270 str = g_string_append (str, text);
1271 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1273 list = gtk_text_child_anchor_get_widgets (anchor);
1275 text = g_object_get_data (G_OBJECT(list->data),
1278 str = g_string_append (str, text);
1282 } else if (c == '\n') {
1283 if (!ignore_newlines) {
1284 ignore_newlines = TRUE;
1285 str = g_string_append_unichar (str, c);
1288 ignore_newlines = FALSE;
1289 str = g_string_append_unichar (str, c);
1293 gtk_clipboard_set_text (clipboard, str->str, str->len);
1294 g_string_free (str, TRUE);
1298 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1300 iface->append_message = chat_text_view_append_message;
1301 iface->append_event = chat_text_view_append_event;
1302 iface->scroll = chat_text_view_scroll;
1303 iface->scroll_down = chat_text_view_scroll_down;
1304 iface->get_has_selection = chat_text_view_get_has_selection;
1305 iface->clear = chat_text_view_clear;
1306 iface->find_previous = chat_text_view_find_previous;
1307 iface->find_next = chat_text_view_find_next;
1308 iface->find_abilities = chat_text_view_find_abilities;
1309 iface->highlight = chat_text_view_highlight;
1310 iface->copy_clipboard = chat_text_view_copy_clipboard;
1314 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1316 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1318 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1320 return priv->last_contact;
1324 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1326 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1328 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1330 return priv->last_timestamp;
1334 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1335 gboolean only_if_date)
1337 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1339 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1341 if (only_if_date != priv->only_if_date) {
1342 priv->only_if_date = only_if_date;
1343 g_object_notify (G_OBJECT (view), "only-if-date");
1348 chat_text_view_replace_link (const gchar *text,
1350 gpointer match_data,
1353 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1356 gtk_text_buffer_get_end_iter (buffer, &iter);
1357 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1359 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1364 chat_text_view_replace_smiley (const gchar *text,
1366 gpointer match_data,
1369 EmpathySmileyHit *hit = match_data;
1370 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1373 gtk_text_buffer_get_end_iter (buffer, &iter);
1374 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1378 chat_text_view_replace_verbatim (const gchar *text,
1380 gpointer match_data,
1383 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1386 gtk_text_buffer_get_end_iter (buffer, &iter);
1387 gtk_text_buffer_insert (buffer, &iter, text, len);
1390 static EmpathyStringParser string_parsers[] = {
1391 {empathy_string_match_link, chat_text_view_replace_link},
1392 {empathy_string_match_all, chat_text_view_replace_verbatim},
1396 static EmpathyStringParser string_parsers_with_smiley[] = {
1397 {empathy_string_match_link, chat_text_view_replace_link},
1398 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1399 {empathy_string_match_all, chat_text_view_replace_verbatim},
1404 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1408 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1409 EmpathyStringParser *parsers;
1410 gboolean use_smileys;
1411 GtkTextIter start_iter;
1415 /* Check if we have to parse smileys */
1416 use_smileys = g_settings_get_boolean (priv->gsettings_chat,
1417 EMPATHY_PREFS_CHAT_SHOW_SMILEYS);
1420 parsers = string_parsers_with_smiley;
1422 parsers = string_parsers;
1424 /* Create a mark at the place we'll start inserting */
1425 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1426 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1428 /* Parse text for links/smileys and insert in the buffer */
1429 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1431 /* Insert a newline after the text inserted */
1432 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1433 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1435 /* Apply the style to the inserted text. */
1436 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1437 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1438 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1442 gtk_text_buffer_delete_mark (priv->buffer, mark);
1446 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1448 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1451 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1452 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1456 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1457 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1462 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1463 const gchar *tag_name,
1464 const gchar *first_property_name,
1467 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1469 GtkTextTagTable *table;
1472 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1473 g_return_val_if_fail (tag_name != NULL, NULL);
1475 table = gtk_text_buffer_get_tag_table (priv->buffer);
1476 tag = gtk_text_tag_table_lookup (table, tag_name);
1478 if (tag && first_property_name) {
1479 va_start (list, first_property_name);
1480 g_object_set_valist (G_OBJECT (tag), first_property_name, list);