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>
35 #include <gconf/gconf-client.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-gsettings.h>
40 #include <libempathy/empathy-utils.h>
42 #include "empathy-chat-text-view.h"
43 #include "empathy-chat.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-smiley-manager.h"
46 #include "empathy-string-parser.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
49 #include <libempathy/empathy-debug.h>
51 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
52 #define TIMESTAMP_INTERVAL 300
55 #define MAX_SCROLL_TIME 0.4 /* seconds */
56 #define SCROLL_DELAY 33 /* milliseconds */
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
61 GtkTextBuffer *buffer;
64 GtkTextMark *find_mark_previous;
65 GtkTextMark *find_mark_next;
66 gboolean find_wrapped;
67 gboolean find_last_direction;
68 EmpathyContact *last_contact;
69 time_t last_timestamp;
70 gboolean allow_scrolling;
71 guint notify_system_fonts_id;
72 GConfClient *gconf_client;
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 = gconf_client_get_string (priv->gconf_client,
213 "/desktop/gnome/interface/document_font_name",
216 if (font_name != NULL) {
217 font_description = pango_font_description_from_string (font_name);
220 font_description = NULL;
223 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
225 if (font_description) {
226 pango_font_description_free (font_description);
231 chat_text_view_notify_system_font_cb (GConfClient *conf,
236 EmpathyChatTextView *view = user_data;
238 chat_text_view_system_font_update (view);
242 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
244 empathy_url_show (GTK_WIDGET (menuitem), url);
248 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
250 GtkClipboard *clipboard;
252 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
253 gtk_clipboard_set_text (clipboard, url, -1);
255 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
256 gtk_clipboard_set_text (clipboard, url, -1);
260 chat_text_view_populate_popup (EmpathyChatTextView *view,
264 EmpathyChatTextViewPriv *priv;
265 GtkTextTagTable *table;
268 GtkTextIter iter, start, end;
272 priv = GET_PRIV (view);
274 /* Clear menu item */
275 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
276 item = gtk_separator_menu_item_new ();
277 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
278 gtk_widget_show (item);
280 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
281 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
282 gtk_widget_show (item);
284 g_signal_connect_swapped (item, "activate",
285 G_CALLBACK (empathy_chat_view_clear),
289 /* Link context menu items */
290 table = gtk_text_buffer_get_tag_table (priv->buffer);
291 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
293 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
295 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
296 GTK_TEXT_WINDOW_WIDGET,
300 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
304 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
305 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
306 str = gtk_text_buffer_get_text (priv->buffer,
307 &start, &end, FALSE);
310 if (EMP_STR_EMPTY (str)) {
315 /* NOTE: Set data just to get the string freed when not needed. */
316 g_object_set_data_full (G_OBJECT (menu),
318 (GDestroyNotify) g_free);
320 item = gtk_separator_menu_item_new ();
321 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
322 gtk_widget_show (item);
324 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
325 g_signal_connect (item, "activate",
326 G_CALLBACK (chat_text_view_copy_address_cb),
328 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329 gtk_widget_show (item);
331 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
332 g_signal_connect (item, "activate",
333 G_CALLBACK (chat_text_view_open_address_cb),
335 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
336 gtk_widget_show (item);
340 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
347 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
348 value = gtk_adjustment_get_value (vadj);
349 upper = gtk_adjustment_get_upper (vadj);
350 page_size = gtk_adjustment_get_page_size (vadj);
352 if (value < upper - page_size) {
360 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
362 EmpathyChatTextViewPriv *priv;
363 GtkTextIter top, bottom;
366 GtkTextTagTable *table;
369 priv = GET_PRIV (view);
371 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
372 line = gtk_text_iter_get_line (&bottom);
373 if (line < MAX_LINES) {
377 remove_ = line - MAX_LINES;
378 gtk_text_buffer_get_start_iter (priv->buffer, &top);
381 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
385 /* Track backwords to a place where we can safely cut, we don't do it in
386 * the middle of a tag.
388 table = gtk_text_buffer_get_tag_table (priv->buffer);
389 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
394 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
398 if (!gtk_text_iter_equal (&top, &bottom)) {
399 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
404 chat_text_view_append_timestamp (EmpathyChatTextView *view,
408 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
413 str = g_string_new ("- ");
415 /* Append date if needed */
420 date = g_date_new ();
421 g_date_set_time_t (date, timestamp);
422 /* Translators: timestamp displayed between conversations in
423 * chat windows (strftime format string) */
424 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
425 g_string_append (str, buf);
426 g_string_append (str, ", ");
431 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
432 g_string_append (str, tmp);
435 g_string_append (str, " -\n");
437 /* Insert the string in the buffer */
438 empathy_chat_text_view_append_spacing (view);
439 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
440 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
443 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
446 g_string_free (str, TRUE);
450 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
453 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
454 GDate *date, *last_date;
455 gboolean append_date = FALSE;
456 gboolean append_time = FALSE;
458 /* Get the date from last message */
459 last_date = g_date_new ();
460 g_date_set_time_t (last_date, priv->last_timestamp);
462 /* Get the date of the message we are appending */
463 date = g_date_new ();
464 g_date_set_time_t (date, timestamp);
466 /* If last message was from another day we append date and time */
467 if (g_date_compare (date, last_date) > 0) {
472 g_date_free (last_date);
475 /* If last message is 'old' append the time */
476 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
480 if (append_date || (!priv->only_if_date && append_time)) {
481 chat_text_view_append_timestamp (view, timestamp, append_date);
486 chat_text_view_size_allocate (GtkWidget *widget,
487 GtkAllocation *alloc)
491 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
493 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
498 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (widget));
499 gtk_adjustment_set_value (adj,
500 gtk_adjustment_get_upper (adj) -
501 gtk_adjustment_get_page_size (adj));
506 chat_text_view_drag_motion (GtkWidget *widget,
507 GdkDragContext *context,
512 /* Don't handle drag motion, since we don't want the view to scroll as
513 * the result of dragging something across it. */
519 chat_text_view_get_property (GObject *object,
524 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
527 case PROP_LAST_CONTACT:
528 g_value_set_object (value, priv->last_contact);
530 case PROP_ONLY_IF_DATE:
531 g_value_set_boolean (value, priv->only_if_date);
534 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
540 chat_text_view_set_property (GObject *object,
545 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
548 case PROP_ONLY_IF_DATE:
549 priv->only_if_date = g_value_get_boolean (value);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
558 chat_text_view_finalize (GObject *object)
560 EmpathyChatTextView *view;
561 EmpathyChatTextViewPriv *priv;
563 view = EMPATHY_CHAT_TEXT_VIEW (object);
564 priv = GET_PRIV (view);
566 DEBUG ("%p", object);
568 gconf_client_notify_remove (priv->gconf_client,
569 priv->notify_system_fonts_id);
570 g_object_unref (priv->gconf_client);
572 if (priv->last_contact) {
573 g_object_unref (priv->last_contact);
575 if (priv->scroll_time) {
576 g_timer_destroy (priv->scroll_time);
578 if (priv->scroll_timeout) {
579 g_source_remove (priv->scroll_timeout);
581 g_object_unref (priv->smiley_manager);
583 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
587 text_view_copy_clipboard (GtkTextView *text_view)
589 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
593 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
595 GObjectClass *object_class = G_OBJECT_CLASS (klass);
596 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
597 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
599 object_class->finalize = chat_text_view_finalize;
600 object_class->get_property = chat_text_view_get_property;
601 object_class->set_property = chat_text_view_set_property;
603 widget_class->size_allocate = chat_text_view_size_allocate;
604 widget_class->drag_motion = chat_text_view_drag_motion;
606 text_view_class->copy_clipboard = text_view_copy_clipboard;
608 g_object_class_install_property (object_class,
610 g_param_spec_object ("last-contact",
612 "The sender of the last received message",
613 EMPATHY_TYPE_CONTACT,
615 g_object_class_install_property (object_class,
617 g_param_spec_boolean ("only-if-date",
619 "Display timestamp only if the date changes",
624 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
628 empathy_chat_text_view_init (EmpathyChatTextView *view)
630 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
631 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
634 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
635 priv->last_timestamp = 0;
636 priv->allow_scrolling = TRUE;
637 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
640 "wrap-mode", GTK_WRAP_WORD_CHAR,
642 "cursor-visible", FALSE,
645 priv->gconf_client = gconf_client_get_default ();
646 gconf_client_add_dir (priv->gconf_client,
647 "/desktop/gnome/interface",
648 GCONF_CLIENT_PRELOAD_ONELEVEL,
650 priv->notify_system_fonts_id =
651 gconf_client_notify_add (priv->gconf_client,
652 "/desktop/gnome/interface/document_font_name",
653 chat_text_view_notify_system_font_cb,
655 chat_text_view_system_font_update (view);
656 chat_text_view_create_tags (view);
658 g_signal_connect (view,
660 G_CALLBACK (chat_text_view_populate_popup),
665 chat_text_view_scroll_stop (EmpathyChatTextView *view)
667 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
669 g_timer_destroy (priv->scroll_time);
670 priv->scroll_time = NULL;
672 g_source_remove (priv->scroll_timeout);
673 priv->scroll_timeout = 0;
676 /* Code stolen from pidgin/gtkimhtml.c */
678 chat_text_view_scroll_cb (EmpathyChatTextView *view)
680 EmpathyChatTextViewPriv *priv;
684 priv = GET_PRIV (view);
686 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
687 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
689 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
691 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
692 /* time's up. jump to the end and kill the timer */
693 gtk_adjustment_set_value (adj, max_val);
694 chat_text_view_scroll_stop (view);
698 /* scroll by 1/3rd the remaining distance */
699 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
704 chat_text_view_scroll_down (EmpathyChatView *view)
706 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
708 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
710 if (!priv->allow_scrolling) {
714 DEBUG ("Scrolling down");
716 if (priv->scroll_time) {
717 g_timer_reset (priv->scroll_time);
719 priv->scroll_time = g_timer_new ();
721 if (!priv->scroll_timeout) {
722 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
723 (GSourceFunc) chat_text_view_scroll_cb,
729 chat_text_view_append_message (EmpathyChatView *view,
732 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
733 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
737 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
738 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
740 if (!empathy_message_get_body (msg)) {
744 bottom = chat_text_view_is_scrolled_down (text_view);
746 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
748 timestamp = empathy_message_get_timestamp (msg);
749 chat_text_maybe_append_date_and_time (text_view, timestamp);
750 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
751 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
756 chat_text_view_scroll_down (view);
759 if (priv->last_contact) {
760 g_object_unref (priv->last_contact);
762 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
763 g_object_notify (G_OBJECT (view), "last-contact");
765 priv->last_timestamp = timestamp;
769 chat_text_view_append_event (EmpathyChatView *view,
772 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
773 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
779 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
780 g_return_if_fail (!EMP_STR_EMPTY (str));
782 bottom = chat_text_view_is_scrolled_down (text_view);
783 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
784 chat_text_maybe_append_date_and_time (text_view,
785 empathy_time_get_current ());
787 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
788 msg = g_strdup_printf (" - %s\n", str);
789 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
791 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
796 chat_text_view_scroll_down (view);
799 if (priv->last_contact) {
800 g_object_unref (priv->last_contact);
801 priv->last_contact = NULL;
802 g_object_notify (G_OBJECT (view), "last-contact");
807 chat_text_view_scroll (EmpathyChatView *view,
808 gboolean allow_scrolling)
810 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
812 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
814 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
816 priv->allow_scrolling = allow_scrolling;
817 if (allow_scrolling) {
818 empathy_chat_view_scroll_down (view);
823 chat_text_view_get_has_selection (EmpathyChatView *view)
825 GtkTextBuffer *buffer;
827 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
829 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
831 return gtk_text_buffer_get_has_selection (buffer);
835 chat_text_view_clear (EmpathyChatView *view)
837 GtkTextBuffer *buffer;
838 EmpathyChatTextViewPriv *priv;
840 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
842 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
843 gtk_text_buffer_set_text (buffer, "", -1);
845 /* We set these back to the initial values so we get
846 * timestamps when clearing the window to know when
847 * conversations start.
849 priv = GET_PRIV (view);
851 priv->last_timestamp = 0;
852 if (priv->last_contact) {
853 g_object_unref (priv->last_contact);
854 priv->last_contact = NULL;
859 chat_text_view_find_previous (EmpathyChatView *view,
860 const gchar *search_criteria,
864 EmpathyChatTextViewPriv *priv;
865 GtkTextBuffer *buffer;
866 GtkTextIter iter_at_mark;
867 GtkTextIter iter_match_start;
868 GtkTextIter iter_match_end;
870 gboolean from_start = FALSE;
872 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
873 g_return_val_if_fail (search_criteria != NULL, FALSE);
875 priv = GET_PRIV (view);
877 buffer = priv->buffer;
879 if (EMP_STR_EMPTY (search_criteria)) {
880 if (priv->find_mark_previous) {
881 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
883 gtk_text_buffer_move_mark (buffer,
884 priv->find_mark_previous,
886 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
887 priv->find_mark_previous,
892 gtk_text_buffer_select_range (buffer,
904 if (!new_search && priv->find_mark_previous) {
905 gtk_text_buffer_get_iter_at_mark (buffer,
907 priv->find_mark_previous);
909 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
913 priv->find_last_direction = FALSE;
915 /* Use the standard GTK+ method for case sensitive searches. It can't do
916 * case insensitive searches (see bug #61852), so keep the custom method
917 * around for case insensitive searches. */
919 found = gtk_text_iter_backward_search (&iter_at_mark,
921 0, /* no text search flags, we want exact matches */
926 found = empathy_text_iter_backward_search (&iter_at_mark,
934 gboolean result = FALSE;
940 /* Here we wrap around. */
941 if (!new_search && !priv->find_wrapped) {
942 priv->find_wrapped = TRUE;
943 result = chat_text_view_find_previous (view,
947 priv->find_wrapped = FALSE;
953 /* Set new mark and show on screen */
954 if (!priv->find_mark_previous) {
955 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
959 gtk_text_buffer_move_mark (buffer,
960 priv->find_mark_previous,
964 if (!priv->find_mark_next) {
965 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
969 gtk_text_buffer_move_mark (buffer,
970 priv->find_mark_next,
974 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
975 priv->find_mark_previous,
981 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
982 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
988 chat_text_view_find_next (EmpathyChatView *view,
989 const gchar *search_criteria,
993 EmpathyChatTextViewPriv *priv;
994 GtkTextBuffer *buffer;
995 GtkTextIter iter_at_mark;
996 GtkTextIter iter_match_start;
997 GtkTextIter iter_match_end;
999 gboolean from_start = FALSE;
1001 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
1002 g_return_val_if_fail (search_criteria != NULL, FALSE);
1004 priv = GET_PRIV (view);
1006 buffer = priv->buffer;
1008 if (EMP_STR_EMPTY (search_criteria)) {
1009 if (priv->find_mark_next) {
1010 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1012 gtk_text_buffer_move_mark (buffer,
1013 priv->find_mark_next,
1015 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1016 priv->find_mark_next,
1021 gtk_text_buffer_select_range (buffer,
1033 if (!new_search && priv->find_mark_next) {
1034 gtk_text_buffer_get_iter_at_mark (buffer,
1036 priv->find_mark_next);
1038 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1042 priv->find_last_direction = TRUE;
1044 /* Use the standard GTK+ method for case sensitive searches. It can't do
1045 * case insensitive searches (see bug #61852), so keep the custom method
1046 * around for case insensitive searches. */
1048 found = gtk_text_iter_forward_search (&iter_at_mark,
1055 found = empathy_text_iter_forward_search (&iter_at_mark,
1063 gboolean result = FALSE;
1069 /* Here we wrap around. */
1070 if (!new_search && !priv->find_wrapped) {
1071 priv->find_wrapped = TRUE;
1072 result = chat_text_view_find_next (view,
1076 priv->find_wrapped = FALSE;
1082 /* Set new mark and show on screen */
1083 if (!priv->find_mark_next) {
1084 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1088 gtk_text_buffer_move_mark (buffer,
1089 priv->find_mark_next,
1093 if (!priv->find_mark_previous) {
1094 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1098 gtk_text_buffer_move_mark (buffer,
1099 priv->find_mark_previous,
1103 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1104 priv->find_mark_next,
1110 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1111 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1117 chat_text_view_find_abilities (EmpathyChatView *view,
1118 const gchar *search_criteria,
1119 gboolean match_case,
1120 gboolean *can_do_previous,
1121 gboolean *can_do_next)
1123 EmpathyChatTextViewPriv *priv;
1124 GtkTextBuffer *buffer;
1125 GtkTextIter iter_at_mark;
1126 GtkTextIter iter_match_start;
1127 GtkTextIter iter_match_end;
1129 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1130 g_return_if_fail (search_criteria != NULL);
1131 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1133 priv = GET_PRIV (view);
1135 buffer = priv->buffer;
1137 if (can_do_previous) {
1138 if (priv->find_mark_previous) {
1139 gtk_text_buffer_get_iter_at_mark (buffer,
1141 priv->find_mark_previous);
1143 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1147 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1154 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1163 if (priv->find_mark_next) {
1164 gtk_text_buffer_get_iter_at_mark (buffer,
1166 priv->find_mark_next);
1168 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1172 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1179 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1189 chat_text_view_highlight (EmpathyChatView *view,
1191 gboolean match_case)
1193 GtkTextBuffer *buffer;
1195 GtkTextIter iter_start;
1196 GtkTextIter iter_end;
1197 GtkTextIter iter_match_start;
1198 GtkTextIter iter_match_end;
1201 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1203 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1205 gtk_text_buffer_get_start_iter (buffer, &iter);
1207 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1208 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1212 if (EMP_STR_EMPTY (text)) {
1218 found = gtk_text_iter_forward_search (&iter,
1225 found = empathy_text_iter_forward_search (&iter,
1235 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1239 iter = iter_match_end;
1244 chat_text_view_copy_clipboard (EmpathyChatView *view)
1246 GtkTextBuffer *buffer;
1247 GtkTextIter start, iter, end;
1248 GtkClipboard *clipboard;
1251 GtkTextChildAnchor *anchor = NULL;
1254 gboolean ignore_newlines = FALSE;
1256 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1258 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1259 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1261 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1264 str = g_string_new ("");
1266 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1267 c = gtk_text_iter_get_char (&iter);
1268 /* 0xFFFC is the 'object replacement' unicode character,
1269 * it indicates the presence of a pixbuf or a widget. */
1271 ignore_newlines = FALSE;
1272 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1274 text = g_object_get_data (G_OBJECT(pixbuf),
1277 str = g_string_append (str, text);
1278 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1280 list = gtk_text_child_anchor_get_widgets (anchor);
1282 text = g_object_get_data (G_OBJECT(list->data),
1285 str = g_string_append (str, text);
1289 } else if (c == '\n') {
1290 if (!ignore_newlines) {
1291 ignore_newlines = TRUE;
1292 str = g_string_append_unichar (str, c);
1295 ignore_newlines = FALSE;
1296 str = g_string_append_unichar (str, c);
1300 gtk_clipboard_set_text (clipboard, str->str, str->len);
1301 g_string_free (str, TRUE);
1305 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1307 iface->append_message = chat_text_view_append_message;
1308 iface->append_event = chat_text_view_append_event;
1309 iface->scroll = chat_text_view_scroll;
1310 iface->scroll_down = chat_text_view_scroll_down;
1311 iface->get_has_selection = chat_text_view_get_has_selection;
1312 iface->clear = chat_text_view_clear;
1313 iface->find_previous = chat_text_view_find_previous;
1314 iface->find_next = chat_text_view_find_next;
1315 iface->find_abilities = chat_text_view_find_abilities;
1316 iface->highlight = chat_text_view_highlight;
1317 iface->copy_clipboard = chat_text_view_copy_clipboard;
1321 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1323 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1325 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1327 return priv->last_contact;
1331 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1333 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1335 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1337 return priv->last_timestamp;
1341 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1342 gboolean only_if_date)
1344 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1346 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1348 if (only_if_date != priv->only_if_date) {
1349 priv->only_if_date = only_if_date;
1350 g_object_notify (G_OBJECT (view), "only-if-date");
1355 chat_text_view_replace_link (const gchar *text,
1357 gpointer match_data,
1360 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1363 gtk_text_buffer_get_end_iter (buffer, &iter);
1364 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1366 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1371 chat_text_view_replace_smiley (const gchar *text,
1373 gpointer match_data,
1376 EmpathySmileyHit *hit = match_data;
1377 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1380 gtk_text_buffer_get_end_iter (buffer, &iter);
1381 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1385 chat_text_view_replace_verbatim (const gchar *text,
1387 gpointer match_data,
1390 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1393 gtk_text_buffer_get_end_iter (buffer, &iter);
1394 gtk_text_buffer_insert (buffer, &iter, text, len);
1397 static EmpathyStringParser string_parsers[] = {
1398 {empathy_string_match_link, chat_text_view_replace_link},
1399 {empathy_string_match_all, chat_text_view_replace_verbatim},
1403 static EmpathyStringParser string_parsers_with_smiley[] = {
1404 {empathy_string_match_link, chat_text_view_replace_link},
1405 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1406 {empathy_string_match_all, chat_text_view_replace_verbatim},
1411 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1415 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1416 EmpathyStringParser *parsers;
1417 gboolean use_smileys;
1418 GtkTextIter start_iter;
1421 GSettings *gsettings_chat;
1423 /* Check if we have to parse smileys */
1424 gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1425 use_smileys = g_settings_get_boolean (gsettings_chat,
1426 EMPATHY_PREFS_CHAT_SHOW_SMILEYS);
1429 parsers = string_parsers_with_smiley;
1431 parsers = string_parsers;
1433 /* Create a mark at the place we'll start inserting */
1434 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1435 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1437 /* Parse text for links/smileys and insert in the buffer */
1438 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1440 /* Insert a newline after the text inserted */
1441 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1442 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1444 /* Apply the style to the inserted text. */
1445 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1446 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1447 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1451 gtk_text_buffer_delete_mark (priv->buffer, mark);
1453 g_object_unref (gsettings_chat);
1457 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1459 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1462 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1463 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1467 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1468 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1473 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1474 const gchar *tag_name,
1475 const gchar *first_property_name,
1478 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1480 GtkTextTagTable *table;
1483 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1484 g_return_val_if_fail (tag_name != NULL, NULL);
1486 table = gtk_text_buffer_get_tag_table (priv->buffer);
1487 tag = gtk_text_tag_table_lookup (table, tag_name);
1489 if (tag && first_property_name) {
1490 va_start (list, first_property_name);
1491 g_object_set_valist (G_OBJECT (tag), first_property_name, list);