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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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>
34 #include <gtk/gtkbutton.h>
35 #include <gtk/gtkimage.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkmenuitem.h>
38 #include <gtk/gtkimagemenuitem.h>
39 #include <gtk/gtkstock.h>
40 #include <gtk/gtkscrolledwindow.h>
41 #include <gtk/gtksizegroup.h>
42 #include <glade/glade.h>
44 #include <telepathy-glib/util.h>
45 #include <libmissioncontrol/mc-account.h>
47 #include <libempathy/empathy-utils.h>
49 #include "empathy-chat-text-view.h"
50 #include "empathy-chat.h"
51 #include "empathy-conf.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-smiley-manager.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
58 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
59 #define TIMESTAMP_INTERVAL 300
62 #define MAX_SCROLL_TIME 0.4 /* seconds */
63 #define SCROLL_DELAY 33 /* milliseconds */
65 #define SCHEMES "(https?|ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
67 #define BODY "([^\\ ]+)"
68 #define END_BODY "([^\\ ]*[^,;\?><()\\ \"\\.])"
69 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
70 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
71 "|((www|ftp)\\."END_BODY")"
72 static GRegex *uri_regex = NULL;
74 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
77 GtkTextBuffer *buffer;
80 GtkTextMark *find_mark_previous;
81 GtkTextMark *find_mark_next;
82 gboolean find_wrapped;
83 gboolean find_last_direction;
84 EmpathyContact *last_contact;
85 time_t last_timestamp;
86 gboolean allow_scrolling;
87 guint notify_system_fonts_id;
88 EmpathySmileyManager *smiley_manager;
89 gboolean only_if_date;
90 } EmpathyChatTextViewPriv;
92 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
94 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
96 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
97 chat_text_view_iface_init));
106 chat_text_view_url_event_cb (GtkTextTag *tag,
110 EmpathyChatTextView *view)
112 EmpathyChatTextViewPriv *priv;
113 GtkTextIter start, end;
116 priv = GET_PRIV (view);
118 /* If the link is being selected, don't do anything. */
119 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
120 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
124 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
127 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
128 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
129 str = gtk_text_buffer_get_text (priv->buffer,
134 empathy_url_show (GTK_WIDGET (view), str);
143 chat_text_view_event_cb (EmpathyChatTextView *view,
144 GdkEventMotion *event,
147 static GdkCursor *hand = NULL;
148 static GdkCursor *beam = NULL;
149 GtkTextWindowType type;
152 gint x, y, buf_x, buf_y;
154 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
157 if (type != GTK_TEXT_WINDOW_TEXT) {
161 /* Get where the pointer really is. */
162 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
167 gdk_window_get_pointer (win, &x, &y, NULL);
169 /* Get the iter where the cursor is at */
170 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
174 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
178 if (gtk_text_iter_has_tag (&iter, tag)) {
180 hand = gdk_cursor_new (GDK_HAND2);
181 beam = gdk_cursor_new (GDK_XTERM);
183 gdk_window_set_cursor (win, hand);
186 beam = gdk_cursor_new (GDK_XTERM);
188 gdk_window_set_cursor (win, beam);
195 chat_text_view_create_tags (EmpathyChatTextView *view)
197 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
200 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
201 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
202 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
203 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
204 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
205 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
206 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
208 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
209 g_signal_connect (tag, "event",
210 G_CALLBACK (chat_text_view_url_event_cb),
213 g_signal_connect (view, "motion-notify-event",
214 G_CALLBACK (chat_text_view_event_cb),
219 chat_text_view_system_font_update (EmpathyChatTextView *view)
221 PangoFontDescription *font_description = NULL;
224 if (empathy_conf_get_string (empathy_conf_get (),
225 "/desktop/gnome/interface/document_font_name",
226 &font_name) && font_name) {
227 font_description = pango_font_description_from_string (font_name);
230 font_description = NULL;
233 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
235 if (font_description) {
236 pango_font_description_free (font_description);
241 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
245 EmpathyChatTextView *view = user_data;
247 chat_text_view_system_font_update (view);
251 chat_text_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatTextView *view)
253 empathy_chat_view_clear (EMPATHY_CHAT_VIEW (view));
257 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
259 empathy_url_show (GTK_WIDGET (menuitem), url);
263 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
265 GtkClipboard *clipboard;
267 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
268 gtk_clipboard_set_text (clipboard, url, -1);
270 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
271 gtk_clipboard_set_text (clipboard, url, -1);
275 chat_text_view_populate_popup (EmpathyChatTextView *view,
279 EmpathyChatTextViewPriv *priv;
280 GtkTextTagTable *table;
283 GtkTextIter iter, start, end;
287 priv = GET_PRIV (view);
289 /* Clear menu item */
290 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
291 item = gtk_menu_item_new ();
292 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
293 gtk_widget_show (item);
295 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
296 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
297 gtk_widget_show (item);
299 g_signal_connect (item,
301 G_CALLBACK (chat_text_view_clear_view_cb),
305 /* Link context menu items */
306 table = gtk_text_buffer_get_tag_table (priv->buffer);
307 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
309 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
311 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
312 GTK_TEXT_WINDOW_WIDGET,
316 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
320 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
321 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
322 str = gtk_text_buffer_get_text (priv->buffer,
323 &start, &end, FALSE);
326 if (G_STR_EMPTY (str)) {
331 /* NOTE: Set data just to get the string freed when not needed. */
332 g_object_set_data_full (G_OBJECT (menu),
334 (GDestroyNotify) g_free);
336 item = gtk_menu_item_new ();
337 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
338 gtk_widget_show (item);
340 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
341 g_signal_connect (item,
343 G_CALLBACK (chat_text_view_copy_address_cb),
345 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
346 gtk_widget_show (item);
348 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
349 g_signal_connect (item,
351 G_CALLBACK (chat_text_view_open_address_cb),
353 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
354 gtk_widget_show (item);
358 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
362 sw = gtk_widget_get_parent (GTK_WIDGET (view));
363 if (GTK_IS_SCROLLED_WINDOW (sw)) {
366 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
368 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
377 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
379 EmpathyChatTextViewPriv *priv;
380 GtkTextIter top, bottom;
383 GtkTextTagTable *table;
386 priv = GET_PRIV (view);
388 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
389 line = gtk_text_iter_get_line (&bottom);
390 if (line < MAX_LINES) {
394 remove = line - MAX_LINES;
395 gtk_text_buffer_get_start_iter (priv->buffer, &top);
398 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
402 /* Track backwords to a place where we can safely cut, we don't do it in
403 * the middle of a tag.
405 table = gtk_text_buffer_get_tag_table (priv->buffer);
406 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
411 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
415 if (!gtk_text_iter_equal (&top, &bottom)) {
416 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
421 chat_text_view_append_timestamp (EmpathyChatTextView *view,
425 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
430 str = g_string_new ("- ");
432 /* Append date if needed */
437 date = g_date_new ();
438 g_date_set_time_t (date, timestamp);
439 g_date_strftime (buf, 256, _("%A %d %B %Y"), date);
440 g_string_append (str, buf);
441 g_string_append (str, ", ");
446 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
447 g_string_append (str, tmp);
450 g_string_append (str, " -\n");
452 /* Insert the string in the buffer */
453 empathy_chat_text_view_append_spacing (view);
454 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
455 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
458 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
461 priv->last_timestamp = timestamp;
463 g_string_free (str, TRUE);
467 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
470 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
471 GDate *date, *last_date;
472 gboolean append_date = FALSE;
473 gboolean append_time = FALSE;
475 /* Get the date from last message */
476 last_date = g_date_new ();
477 g_date_set_time_t (last_date, priv->last_timestamp);
479 /* Get the date of the message we are appending */
480 date = g_date_new ();
481 g_date_set_time_t (date, timestamp);
483 /* If last message was from another day we append date and time */
484 if (g_date_compare (date, last_date) > 0) {
489 g_date_free (last_date);
492 /* If last message is 'old' append the time */
493 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
497 if (append_date || (!priv->only_if_date && append_time)) {
498 chat_text_view_append_timestamp (view, timestamp, append_date);
503 chat_text_view_size_allocate (GtkWidget *widget,
504 GtkAllocation *alloc)
508 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
510 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
515 adj = GTK_TEXT_VIEW (widget)->vadjustment;
516 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
521 chat_text_view_drag_motion (GtkWidget *widget,
522 GdkDragContext *context,
527 /* Don't handle drag motion, since we don't want the view to scroll as
528 * the result of dragging something across it. */
534 chat_text_view_get_property (GObject *object,
539 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
542 case PROP_LAST_CONTACT:
543 g_value_set_object (value, priv->last_contact);
545 case PROP_ONLY_IF_DATE:
546 g_value_set_boolean (value, priv->only_if_date);
549 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
555 chat_text_view_set_property (GObject *object,
560 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
563 case PROP_ONLY_IF_DATE:
564 priv->only_if_date = g_value_get_boolean (value);
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
573 chat_text_view_finalize (GObject *object)
575 EmpathyChatTextView *view;
576 EmpathyChatTextViewPriv *priv;
578 view = EMPATHY_CHAT_TEXT_VIEW (object);
579 priv = GET_PRIV (view);
581 DEBUG ("%p", object);
583 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
585 if (priv->last_contact) {
586 g_object_unref (priv->last_contact);
588 if (priv->scroll_time) {
589 g_timer_destroy (priv->scroll_time);
591 if (priv->scroll_timeout) {
592 g_source_remove (priv->scroll_timeout);
595 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
599 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
601 GObjectClass *object_class = G_OBJECT_CLASS (klass);
602 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
604 object_class->finalize = chat_text_view_finalize;
605 object_class->get_property = chat_text_view_get_property;
606 object_class->set_property = chat_text_view_set_property;
608 widget_class->size_allocate = chat_text_view_size_allocate;
609 widget_class->drag_motion = chat_text_view_drag_motion;
611 g_object_class_install_property (object_class,
613 g_param_spec_object ("last-contact",
615 "The sender of the last received message",
616 EMPATHY_TYPE_CONTACT,
618 g_object_class_install_property (object_class,
620 g_param_spec_boolean ("only-if-date",
622 "Display timestamp only if the date changes",
627 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
631 empathy_chat_text_view_init (EmpathyChatTextView *view)
633 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
634 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
637 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
638 priv->last_timestamp = 0;
639 priv->allow_scrolling = TRUE;
640 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
643 "wrap-mode", GTK_WRAP_WORD_CHAR,
645 "cursor-visible", FALSE,
648 priv->notify_system_fonts_id =
649 empathy_conf_notify_add (empathy_conf_get (),
650 "/desktop/gnome/interface/document_font_name",
651 chat_text_view_notify_system_font_cb,
653 chat_text_view_system_font_update (view);
654 chat_text_view_create_tags (view);
656 g_signal_connect (view,
658 G_CALLBACK (chat_text_view_populate_popup),
662 /* Code stolen from pidgin/gtkimhtml.c */
664 chat_text_view_scroll_cb (EmpathyChatTextView *view)
666 EmpathyChatTextViewPriv *priv;
670 priv = GET_PRIV (view);
671 adj = GTK_TEXT_VIEW (view)->vadjustment;
672 max_val = adj->upper - adj->page_size;
674 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
676 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
677 /* time's up. jump to the end and kill the timer */
678 gtk_adjustment_set_value (adj, max_val);
679 g_timer_destroy (priv->scroll_time);
680 priv->scroll_time = NULL;
681 priv->scroll_timeout = 0;
685 /* scroll by 1/3rd the remaining distance */
686 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
691 chat_text_view_scroll_down (EmpathyChatView *view)
693 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
695 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
697 if (!priv->allow_scrolling) {
701 DEBUG ("Scrolling down");
703 if (priv->scroll_time) {
704 g_timer_reset (priv->scroll_time);
706 priv->scroll_time = g_timer_new();
708 if (!priv->scroll_timeout) {
709 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
710 (GSourceFunc) chat_text_view_scroll_cb,
716 chat_text_view_append_message (EmpathyChatView *view,
719 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
720 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
724 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
725 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
727 if (!empathy_message_get_body (msg)) {
731 bottom = chat_text_view_is_scrolled_down (text_view);
733 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
735 timestamp = empathy_message_get_timestamp (msg);
736 chat_text_maybe_append_date_and_time (text_view, timestamp);
737 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
738 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
743 chat_text_view_scroll_down (view);
746 if (priv->last_contact) {
747 g_object_unref (priv->last_contact);
749 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
750 g_object_notify (G_OBJECT (view), "last-contact");
754 chat_text_view_append_event (EmpathyChatView *view,
757 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
758 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
764 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
765 g_return_if_fail (!G_STR_EMPTY (str));
767 bottom = chat_text_view_is_scrolled_down (text_view);
768 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
769 chat_text_maybe_append_date_and_time (text_view,
770 empathy_time_get_current ());
772 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
773 msg = g_strdup_printf (" - %s\n", str);
774 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
776 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
781 chat_text_view_scroll_down (view);
784 if (priv->last_contact) {
785 g_object_unref (priv->last_contact);
786 priv->last_contact = NULL;
787 g_object_notify (G_OBJECT (view), "last-contact");
792 chat_text_view_scroll (EmpathyChatView *view,
793 gboolean allow_scrolling)
795 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
797 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
799 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
801 priv->allow_scrolling = allow_scrolling;
802 if (allow_scrolling) {
803 empathy_chat_view_scroll_down (view);
808 chat_text_view_get_has_selection (EmpathyChatView *view)
810 GtkTextBuffer *buffer;
812 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
814 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
816 return gtk_text_buffer_get_has_selection (buffer);
820 chat_text_view_clear (EmpathyChatView *view)
822 GtkTextBuffer *buffer;
823 EmpathyChatTextViewPriv *priv;
825 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
827 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
828 gtk_text_buffer_set_text (buffer, "", -1);
830 /* We set these back to the initial values so we get
831 * timestamps when clearing the window to know when
832 * conversations start.
834 priv = GET_PRIV (view);
836 priv->last_timestamp = 0;
840 chat_text_view_find_previous (EmpathyChatView *view,
841 const gchar *search_criteria,
844 EmpathyChatTextViewPriv *priv;
845 GtkTextBuffer *buffer;
846 GtkTextIter iter_at_mark;
847 GtkTextIter iter_match_start;
848 GtkTextIter iter_match_end;
850 gboolean from_start = FALSE;
852 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
853 g_return_val_if_fail (search_criteria != NULL, FALSE);
855 priv = GET_PRIV (view);
857 buffer = priv->buffer;
859 if (G_STR_EMPTY (search_criteria)) {
860 if (priv->find_mark_previous) {
861 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
863 gtk_text_buffer_move_mark (buffer,
864 priv->find_mark_previous,
866 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
867 priv->find_mark_previous,
872 gtk_text_buffer_select_range (buffer,
884 if (priv->find_mark_previous) {
885 gtk_text_buffer_get_iter_at_mark (buffer,
887 priv->find_mark_previous);
889 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
893 priv->find_last_direction = FALSE;
895 found = empathy_text_iter_backward_search (&iter_at_mark,
902 gboolean result = FALSE;
908 /* Here we wrap around. */
909 if (!new_search && !priv->find_wrapped) {
910 priv->find_wrapped = TRUE;
911 result = chat_text_view_find_previous (view,
914 priv->find_wrapped = FALSE;
920 /* Set new mark and show on screen */
921 if (!priv->find_mark_previous) {
922 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
926 gtk_text_buffer_move_mark (buffer,
927 priv->find_mark_previous,
931 if (!priv->find_mark_next) {
932 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
936 gtk_text_buffer_move_mark (buffer,
937 priv->find_mark_next,
941 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
942 priv->find_mark_previous,
948 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
949 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
955 chat_text_view_find_next (EmpathyChatView *view,
956 const gchar *search_criteria,
959 EmpathyChatTextViewPriv *priv;
960 GtkTextBuffer *buffer;
961 GtkTextIter iter_at_mark;
962 GtkTextIter iter_match_start;
963 GtkTextIter iter_match_end;
965 gboolean from_start = FALSE;
967 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
968 g_return_val_if_fail (search_criteria != NULL, FALSE);
970 priv = GET_PRIV (view);
972 buffer = priv->buffer;
974 if (G_STR_EMPTY (search_criteria)) {
975 if (priv->find_mark_next) {
976 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
978 gtk_text_buffer_move_mark (buffer,
979 priv->find_mark_next,
981 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
982 priv->find_mark_next,
987 gtk_text_buffer_select_range (buffer,
999 if (priv->find_mark_next) {
1000 gtk_text_buffer_get_iter_at_mark (buffer,
1002 priv->find_mark_next);
1004 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1008 priv->find_last_direction = TRUE;
1010 found = empathy_text_iter_forward_search (&iter_at_mark,
1017 gboolean result = FALSE;
1023 /* Here we wrap around. */
1024 if (!new_search && !priv->find_wrapped) {
1025 priv->find_wrapped = TRUE;
1026 result = chat_text_view_find_next (view,
1029 priv->find_wrapped = FALSE;
1035 /* Set new mark and show on screen */
1036 if (!priv->find_mark_next) {
1037 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1041 gtk_text_buffer_move_mark (buffer,
1042 priv->find_mark_next,
1046 if (!priv->find_mark_previous) {
1047 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1051 gtk_text_buffer_move_mark (buffer,
1052 priv->find_mark_previous,
1056 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1057 priv->find_mark_next,
1063 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1064 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1070 chat_text_view_find_abilities (EmpathyChatView *view,
1071 const gchar *search_criteria,
1072 gboolean *can_do_previous,
1073 gboolean *can_do_next)
1075 EmpathyChatTextViewPriv *priv;
1076 GtkTextBuffer *buffer;
1077 GtkTextIter iter_at_mark;
1078 GtkTextIter iter_match_start;
1079 GtkTextIter iter_match_end;
1081 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1082 g_return_if_fail (search_criteria != NULL);
1083 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1085 priv = GET_PRIV (view);
1087 buffer = priv->buffer;
1089 if (can_do_previous) {
1090 if (priv->find_mark_previous) {
1091 gtk_text_buffer_get_iter_at_mark (buffer,
1093 priv->find_mark_previous);
1095 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1098 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1106 if (priv->find_mark_next) {
1107 gtk_text_buffer_get_iter_at_mark (buffer,
1109 priv->find_mark_next);
1111 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1114 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1123 chat_text_view_highlight (EmpathyChatView *view,
1126 GtkTextBuffer *buffer;
1128 GtkTextIter iter_start;
1129 GtkTextIter iter_end;
1130 GtkTextIter iter_match_start;
1131 GtkTextIter iter_match_end;
1134 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1136 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1138 gtk_text_buffer_get_start_iter (buffer, &iter);
1140 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1141 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1145 if (G_STR_EMPTY (text)) {
1150 found = empathy_text_iter_forward_search (&iter,
1160 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1164 iter = iter_match_end;
1165 gtk_text_iter_forward_char (&iter);
1170 chat_text_view_copy_clipboard (EmpathyChatView *view)
1172 GtkTextBuffer *buffer;
1173 GtkClipboard *clipboard;
1175 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1177 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1178 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1180 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1184 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1186 iface->append_message = chat_text_view_append_message;
1187 iface->append_event = chat_text_view_append_event;
1188 iface->scroll = chat_text_view_scroll;
1189 iface->scroll_down = chat_text_view_scroll_down;
1190 iface->get_has_selection = chat_text_view_get_has_selection;
1191 iface->clear = chat_text_view_clear;
1192 iface->find_previous = chat_text_view_find_previous;
1193 iface->find_next = chat_text_view_find_next;
1194 iface->find_abilities = chat_text_view_find_abilities;
1195 iface->highlight = chat_text_view_highlight;
1196 iface->copy_clipboard = chat_text_view_copy_clipboard;
1200 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1202 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1204 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1206 return priv->last_contact;
1210 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1211 gboolean only_if_date)
1213 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1215 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1217 if (only_if_date != priv->only_if_date) {
1218 priv->only_if_date = only_if_date;
1219 g_object_notify (G_OBJECT (view), "only-if-date");
1224 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1228 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1229 gboolean use_smileys = FALSE;
1230 GSList *smileys, *l;
1232 empathy_conf_get_bool (empathy_conf_get (),
1233 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1237 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1241 smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1242 for (l = smileys; l; l = l->next) {
1243 EmpathySmiley *smiley;
1246 if (smiley->pixbuf) {
1247 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1249 gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1251 empathy_smiley_free (smiley);
1253 g_slist_free (smileys);
1257 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1261 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1262 GtkTextIter start_iter, end_iter;
1265 GMatchInfo *match_info;
1271 priv = GET_PRIV (view);
1273 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1274 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1277 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
1280 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1281 match = g_match_info_next (match_info, NULL)) {
1282 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1286 tmp = empathy_substring (body, last, s);
1288 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1289 chat_text_view_insert_text_with_emoticons (view,
1295 tmp = empathy_substring (body, s, e);
1297 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1298 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1302 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1308 g_match_info_free (match_info);
1310 if (last < strlen (body)) {
1311 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1312 chat_text_view_insert_text_with_emoticons (view,
1317 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1318 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1320 /* Apply the style to the inserted text. */
1321 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1322 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1324 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1329 gtk_text_buffer_delete_mark (priv->buffer, mark);
1333 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1335 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1338 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1339 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1343 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1344 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1349 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1350 const gchar *tag_name,
1351 const gchar *first_property_name,
1354 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1356 GtkTextTagTable *table;
1359 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1360 g_return_val_if_fail (tag_name != NULL, NULL);
1362 table = gtk_text_buffer_get_tag_table (priv->buffer);
1363 tag = gtk_text_tag_table_lookup (table, tag_name);
1365 if (tag && first_property_name) {
1366 va_start (list, first_property_name);
1367 g_object_set_valist (G_OBJECT (tag), first_property_name, list);