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 (5 * G_TIME_SPAN_MINUTE)
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 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;
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_device_position (win, event->device, &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_override_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 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);
293 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
294 GTK_TEXT_WINDOW_WIDGET,
298 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
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);
308 if (EMP_STR_EMPTY (str)) {
313 /* NOTE: Set data just to get the string freed when not needed. */
314 g_object_set_data_full (G_OBJECT (menu),
316 (GDestroyNotify) g_free);
318 item = gtk_separator_menu_item_new ();
319 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
320 gtk_widget_show (item);
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),
326 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
327 gtk_widget_show (item);
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),
333 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
334 gtk_widget_show (item);
338 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
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);
350 if (value < upper - page_size) {
358 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
360 EmpathyChatTextViewPriv *priv;
361 GtkTextIter top, bottom;
364 GtkTextTagTable *table;
367 priv = GET_PRIV (view);
369 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
370 line = gtk_text_iter_get_line (&bottom);
371 if (line < MAX_LINES) {
375 remove_ = line - MAX_LINES;
376 gtk_text_buffer_get_start_iter (priv->buffer, &top);
379 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
383 /* Track backwords to a place where we can safely cut, we don't do it in
384 * the middle of a tag.
386 table = gtk_text_buffer_get_tag_table (priv->buffer);
387 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
392 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
396 if (!gtk_text_iter_equal (&top, &bottom)) {
397 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
402 chat_text_view_append_timestamp (EmpathyChatTextView *view,
406 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
411 str = g_string_new ("- ");
413 /* Append date if needed */
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, ", ");
424 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
425 g_string_append (str, tmp);
428 g_string_append (str, " -\n");
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,
436 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
439 g_string_free (str, TRUE);
443 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
446 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
447 GDateTime *date, *last_date;
448 gboolean append_date = FALSE;
449 gboolean append_time = FALSE;
452 /* Get the date from last message */
453 last_date = g_date_time_new_from_unix_utc (priv->last_timestamp);
455 /* Get the date of the message we are appending */
456 date = g_date_time_new_from_unix_utc (timestamp);
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) {
465 g_date_time_unref (last_date);
466 g_date_time_unref (date);
468 /* If last message is 'old' append the time */
469 if (delta >= TIMESTAMP_INTERVAL) {
473 if (append_date || (!priv->only_if_date && append_time)) {
474 chat_text_view_append_timestamp (view, timestamp, append_date);
479 chat_text_view_size_allocate (GtkWidget *widget,
480 GtkAllocation *alloc)
484 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
486 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
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));
499 chat_text_view_drag_motion (GtkWidget *widget,
500 GdkDragContext *context,
505 /* Don't handle drag motion, since we don't want the view to scroll as
506 * the result of dragging something across it. */
512 chat_text_view_get_property (GObject *object,
517 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
520 case PROP_LAST_CONTACT:
521 g_value_set_object (value, priv->last_contact);
523 case PROP_ONLY_IF_DATE:
524 g_value_set_boolean (value, priv->only_if_date);
527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
533 chat_text_view_set_property (GObject *object,
538 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
541 case PROP_ONLY_IF_DATE:
542 priv->only_if_date = g_value_get_boolean (value);
545 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
551 chat_text_view_finalize (GObject *object)
553 EmpathyChatTextView *view;
554 EmpathyChatTextViewPriv *priv;
556 view = EMPATHY_CHAT_TEXT_VIEW (object);
557 priv = GET_PRIV (view);
559 DEBUG ("%p", object);
561 g_object_unref (priv->gsettings_chat);
562 g_object_unref (priv->gsettings_desktop);
564 if (priv->last_contact) {
565 g_object_unref (priv->last_contact);
567 if (priv->scroll_time) {
568 g_timer_destroy (priv->scroll_time);
570 if (priv->scroll_timeout) {
571 g_source_remove (priv->scroll_timeout);
573 g_object_unref (priv->smiley_manager);
575 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
579 text_view_copy_clipboard (GtkTextView *text_view)
581 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
585 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
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);
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;
595 widget_class->size_allocate = chat_text_view_size_allocate;
596 widget_class->drag_motion = chat_text_view_drag_motion;
598 text_view_class->copy_clipboard = text_view_copy_clipboard;
600 g_object_class_install_property (object_class,
602 g_param_spec_object ("last-contact",
604 "The sender of the last received message",
605 EMPATHY_TYPE_CONTACT,
607 g_object_class_install_property (object_class,
609 g_param_spec_boolean ("only-if-date",
611 "Display timestamp only if the date changes",
616 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
620 empathy_chat_text_view_init (EmpathyChatTextView *view)
622 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
623 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
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 ();
632 "wrap-mode", GTK_WRAP_WORD_CHAR,
634 "cursor-visible", FALSE,
637 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
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),
645 chat_text_view_system_font_update (view);
646 chat_text_view_create_tags (view);
648 g_signal_connect (view,
650 G_CALLBACK (chat_text_view_populate_popup),
655 chat_text_view_scroll_stop (EmpathyChatTextView *view)
657 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
659 g_timer_destroy (priv->scroll_time);
660 priv->scroll_time = NULL;
662 g_source_remove (priv->scroll_timeout);
663 priv->scroll_timeout = 0;
666 /* Code stolen from pidgin/gtkimhtml.c */
668 chat_text_view_scroll_cb (EmpathyChatTextView *view)
670 EmpathyChatTextViewPriv *priv;
674 priv = GET_PRIV (view);
676 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
677 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
679 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
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);
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));
694 chat_text_view_scroll_down (EmpathyChatView *view)
696 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
698 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
700 if (!priv->allow_scrolling) {
704 DEBUG ("Scrolling down");
706 if (priv->scroll_time) {
707 g_timer_reset (priv->scroll_time);
709 priv->scroll_time = g_timer_new ();
711 if (!priv->scroll_timeout) {
712 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
713 (GSourceFunc) chat_text_view_scroll_cb,
719 chat_text_view_append_message (EmpathyChatView *view,
722 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
723 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
727 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
728 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
730 if (!empathy_message_get_body (msg)) {
734 bottom = chat_text_view_is_scrolled_down (text_view);
736 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
738 timestamp = empathy_message_get_timestamp (msg);
739 chat_text_maybe_append_date_and_time (text_view, timestamp);
740 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
741 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
746 chat_text_view_scroll_down (view);
749 if (priv->last_contact) {
750 g_object_unref (priv->last_contact);
752 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
753 g_object_notify (G_OBJECT (view), "last-contact");
755 priv->last_timestamp = timestamp;
759 chat_text_view_append_event (EmpathyChatView *view,
762 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
763 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
769 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
770 g_return_if_fail (!EMP_STR_EMPTY (str));
772 bottom = chat_text_view_is_scrolled_down (text_view);
773 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
774 chat_text_maybe_append_date_and_time (text_view,
775 empathy_time_get_current ());
777 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
778 msg = g_strdup_printf (" - %s\n", str);
779 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
781 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
786 chat_text_view_scroll_down (view);
789 if (priv->last_contact) {
790 g_object_unref (priv->last_contact);
791 priv->last_contact = NULL;
792 g_object_notify (G_OBJECT (view), "last-contact");
797 chat_text_view_scroll (EmpathyChatView *view,
798 gboolean allow_scrolling)
800 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
802 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
804 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
806 priv->allow_scrolling = allow_scrolling;
807 if (allow_scrolling) {
808 empathy_chat_view_scroll_down (view);
813 chat_text_view_get_has_selection (EmpathyChatView *view)
815 GtkTextBuffer *buffer;
817 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
819 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
821 return gtk_text_buffer_get_has_selection (buffer);
825 chat_text_view_clear (EmpathyChatView *view)
827 GtkTextBuffer *buffer;
828 EmpathyChatTextViewPriv *priv;
830 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
832 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
833 gtk_text_buffer_set_text (buffer, "", -1);
835 /* We set these back to the initial values so we get
836 * timestamps when clearing the window to know when
837 * conversations start.
839 priv = GET_PRIV (view);
841 priv->last_timestamp = 0;
842 if (priv->last_contact) {
843 g_object_unref (priv->last_contact);
844 priv->last_contact = NULL;
849 chat_text_view_find_previous (EmpathyChatView *view,
850 const gchar *search_criteria,
854 EmpathyChatTextViewPriv *priv;
855 GtkTextBuffer *buffer;
856 GtkTextIter iter_at_mark;
857 GtkTextIter iter_match_start;
858 GtkTextIter iter_match_end;
860 gboolean from_start = FALSE;
862 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
863 g_return_val_if_fail (search_criteria != NULL, FALSE);
865 priv = GET_PRIV (view);
867 buffer = priv->buffer;
869 if (EMP_STR_EMPTY (search_criteria)) {
870 if (priv->find_mark_previous) {
871 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
873 gtk_text_buffer_move_mark (buffer,
874 priv->find_mark_previous,
876 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
877 priv->find_mark_previous,
882 gtk_text_buffer_select_range (buffer,
894 if (!new_search && priv->find_mark_previous) {
895 gtk_text_buffer_get_iter_at_mark (buffer,
897 priv->find_mark_previous);
899 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
903 priv->find_last_direction = FALSE;
905 /* Use the standard GTK+ method for case sensitive searches. It can't do
906 * case insensitive searches (see bug #61852), so keep the custom method
907 * around for case insensitive searches. */
909 found = gtk_text_iter_backward_search (&iter_at_mark,
911 0, /* no text search flags, we want exact matches */
916 found = empathy_text_iter_backward_search (&iter_at_mark,
924 gboolean result = FALSE;
930 /* Here we wrap around. */
931 if (!new_search && !priv->find_wrapped) {
932 priv->find_wrapped = TRUE;
933 result = chat_text_view_find_previous (view,
937 priv->find_wrapped = FALSE;
943 /* Set new mark and show on screen */
944 if (!priv->find_mark_previous) {
945 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
949 gtk_text_buffer_move_mark (buffer,
950 priv->find_mark_previous,
954 if (!priv->find_mark_next) {
955 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
959 gtk_text_buffer_move_mark (buffer,
960 priv->find_mark_next,
964 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
965 priv->find_mark_previous,
971 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
972 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
978 chat_text_view_find_next (EmpathyChatView *view,
979 const gchar *search_criteria,
983 EmpathyChatTextViewPriv *priv;
984 GtkTextBuffer *buffer;
985 GtkTextIter iter_at_mark;
986 GtkTextIter iter_match_start;
987 GtkTextIter iter_match_end;
989 gboolean from_start = FALSE;
991 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
992 g_return_val_if_fail (search_criteria != NULL, FALSE);
994 priv = GET_PRIV (view);
996 buffer = priv->buffer;
998 if (EMP_STR_EMPTY (search_criteria)) {
999 if (priv->find_mark_next) {
1000 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1002 gtk_text_buffer_move_mark (buffer,
1003 priv->find_mark_next,
1005 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1006 priv->find_mark_next,
1011 gtk_text_buffer_select_range (buffer,
1023 if (!new_search && priv->find_mark_next) {
1024 gtk_text_buffer_get_iter_at_mark (buffer,
1026 priv->find_mark_next);
1028 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1032 priv->find_last_direction = TRUE;
1034 /* Use the standard GTK+ method for case sensitive searches. It can't do
1035 * case insensitive searches (see bug #61852), so keep the custom method
1036 * around for case insensitive searches. */
1038 found = gtk_text_iter_forward_search (&iter_at_mark,
1045 found = empathy_text_iter_forward_search (&iter_at_mark,
1053 gboolean result = FALSE;
1059 /* Here we wrap around. */
1060 if (!new_search && !priv->find_wrapped) {
1061 priv->find_wrapped = TRUE;
1062 result = chat_text_view_find_next (view,
1066 priv->find_wrapped = FALSE;
1072 /* Set new mark and show on screen */
1073 if (!priv->find_mark_next) {
1074 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1078 gtk_text_buffer_move_mark (buffer,
1079 priv->find_mark_next,
1083 if (!priv->find_mark_previous) {
1084 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1088 gtk_text_buffer_move_mark (buffer,
1089 priv->find_mark_previous,
1093 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1094 priv->find_mark_next,
1100 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1101 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1107 chat_text_view_find_abilities (EmpathyChatView *view,
1108 const gchar *search_criteria,
1109 gboolean match_case,
1110 gboolean *can_do_previous,
1111 gboolean *can_do_next)
1113 EmpathyChatTextViewPriv *priv;
1114 GtkTextBuffer *buffer;
1115 GtkTextIter iter_at_mark;
1116 GtkTextIter iter_match_start;
1117 GtkTextIter iter_match_end;
1119 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1120 g_return_if_fail (search_criteria != NULL);
1121 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1123 priv = GET_PRIV (view);
1125 buffer = priv->buffer;
1127 if (can_do_previous) {
1128 if (priv->find_mark_previous) {
1129 gtk_text_buffer_get_iter_at_mark (buffer,
1131 priv->find_mark_previous);
1133 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1137 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1144 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1153 if (priv->find_mark_next) {
1154 gtk_text_buffer_get_iter_at_mark (buffer,
1156 priv->find_mark_next);
1158 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1162 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1169 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1179 chat_text_view_highlight (EmpathyChatView *view,
1181 gboolean match_case)
1183 GtkTextBuffer *buffer;
1185 GtkTextIter iter_start;
1186 GtkTextIter iter_end;
1187 GtkTextIter iter_match_start;
1188 GtkTextIter iter_match_end;
1191 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1193 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1195 gtk_text_buffer_get_start_iter (buffer, &iter);
1197 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1198 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1202 if (EMP_STR_EMPTY (text)) {
1208 found = gtk_text_iter_forward_search (&iter,
1215 found = empathy_text_iter_forward_search (&iter,
1225 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1229 iter = iter_match_end;
1234 chat_text_view_copy_clipboard (EmpathyChatView *view)
1236 GtkTextBuffer *buffer;
1237 GtkTextIter start, iter, end;
1238 GtkClipboard *clipboard;
1241 GtkTextChildAnchor *anchor = NULL;
1244 gboolean ignore_newlines = FALSE;
1246 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1248 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1249 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1251 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1254 str = g_string_new ("");
1256 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1257 c = gtk_text_iter_get_char (&iter);
1258 /* 0xFFFC is the 'object replacement' unicode character,
1259 * it indicates the presence of a pixbuf or a widget. */
1261 ignore_newlines = FALSE;
1262 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1264 text = g_object_get_data (G_OBJECT(pixbuf),
1267 str = g_string_append (str, text);
1268 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1270 list = gtk_text_child_anchor_get_widgets (anchor);
1272 text = g_object_get_data (G_OBJECT(list->data),
1275 str = g_string_append (str, text);
1279 } else if (c == '\n') {
1280 if (!ignore_newlines) {
1281 ignore_newlines = TRUE;
1282 str = g_string_append_unichar (str, c);
1285 ignore_newlines = FALSE;
1286 str = g_string_append_unichar (str, c);
1290 gtk_clipboard_set_text (clipboard, str->str, str->len);
1291 g_string_free (str, TRUE);
1295 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1297 iface->append_message = chat_text_view_append_message;
1298 iface->append_event = chat_text_view_append_event;
1299 iface->scroll = chat_text_view_scroll;
1300 iface->scroll_down = chat_text_view_scroll_down;
1301 iface->get_has_selection = chat_text_view_get_has_selection;
1302 iface->clear = chat_text_view_clear;
1303 iface->find_previous = chat_text_view_find_previous;
1304 iface->find_next = chat_text_view_find_next;
1305 iface->find_abilities = chat_text_view_find_abilities;
1306 iface->highlight = chat_text_view_highlight;
1307 iface->copy_clipboard = chat_text_view_copy_clipboard;
1311 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1313 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1315 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1317 return priv->last_contact;
1321 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1323 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1325 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1327 return priv->last_timestamp;
1331 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1332 gboolean only_if_date)
1334 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1336 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1338 if (only_if_date != priv->only_if_date) {
1339 priv->only_if_date = only_if_date;
1340 g_object_notify (G_OBJECT (view), "only-if-date");
1345 chat_text_view_replace_link (const gchar *text,
1347 gpointer match_data,
1350 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1353 gtk_text_buffer_get_end_iter (buffer, &iter);
1354 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1356 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1361 chat_text_view_replace_smiley (const gchar *text,
1363 gpointer match_data,
1366 EmpathySmileyHit *hit = match_data;
1367 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1370 gtk_text_buffer_get_end_iter (buffer, &iter);
1371 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1375 chat_text_view_replace_verbatim (const gchar *text,
1377 gpointer match_data,
1380 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1383 gtk_text_buffer_get_end_iter (buffer, &iter);
1384 gtk_text_buffer_insert (buffer, &iter, text, len);
1387 static EmpathyStringParser string_parsers[] = {
1388 {empathy_string_match_link, chat_text_view_replace_link},
1389 {empathy_string_match_all, chat_text_view_replace_verbatim},
1393 static EmpathyStringParser string_parsers_with_smiley[] = {
1394 {empathy_string_match_link, chat_text_view_replace_link},
1395 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1396 {empathy_string_match_all, chat_text_view_replace_verbatim},
1401 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1405 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1406 EmpathyStringParser *parsers;
1407 gboolean use_smileys;
1408 GtkTextIter start_iter;
1412 /* Check if we have to parse smileys */
1413 use_smileys = g_settings_get_boolean (priv->gsettings_chat,
1414 EMPATHY_PREFS_CHAT_SHOW_SMILEYS);
1417 parsers = string_parsers_with_smiley;
1419 parsers = string_parsers;
1421 /* Create a mark at the place we'll start inserting */
1422 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1423 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1425 /* Parse text for links/smileys and insert in the buffer */
1426 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1428 /* Insert a newline after the text inserted */
1429 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1430 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1432 /* Apply the style to the inserted text. */
1433 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1434 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1435 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1439 gtk_text_buffer_delete_mark (priv->buffer, mark);
1443 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1445 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1448 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1449 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1453 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1454 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1459 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1460 const gchar *tag_name,
1461 const gchar *first_property_name,
1464 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1466 GtkTextTagTable *table;
1469 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1470 g_return_val_if_fail (tag_name != NULL, NULL);
1472 table = gtk_text_buffer_get_tag_table (priv->buffer);
1473 tag = gtk_text_tag_table_lookup (table, tag_name);
1475 if (tag && first_property_name) {
1476 va_start (list, first_property_name);
1477 g_object_set_valist (G_OBJECT (tag), first_property_name, list);