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?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
67 #define BODY "([^\\ \\n]+)"
68 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\n])"
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 (EMP_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 /* Translators: timestamp displayed between conversations in
440 * chat windows (strftime format string) */
441 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
442 g_string_append (str, buf);
443 g_string_append (str, ", ");
448 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
449 g_string_append (str, tmp);
452 g_string_append (str, " -\n");
454 /* Insert the string in the buffer */
455 empathy_chat_text_view_append_spacing (view);
456 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
457 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
460 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
463 priv->last_timestamp = timestamp;
465 g_string_free (str, TRUE);
469 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
472 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
473 GDate *date, *last_date;
474 gboolean append_date = FALSE;
475 gboolean append_time = FALSE;
477 /* Get the date from last message */
478 last_date = g_date_new ();
479 g_date_set_time_t (last_date, priv->last_timestamp);
481 /* Get the date of the message we are appending */
482 date = g_date_new ();
483 g_date_set_time_t (date, timestamp);
485 /* If last message was from another day we append date and time */
486 if (g_date_compare (date, last_date) > 0) {
491 g_date_free (last_date);
494 /* If last message is 'old' append the time */
495 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
499 if (append_date || (!priv->only_if_date && append_time)) {
500 chat_text_view_append_timestamp (view, timestamp, append_date);
505 chat_text_view_size_allocate (GtkWidget *widget,
506 GtkAllocation *alloc)
510 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
512 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
517 adj = GTK_TEXT_VIEW (widget)->vadjustment;
518 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
523 chat_text_view_drag_motion (GtkWidget *widget,
524 GdkDragContext *context,
529 /* Don't handle drag motion, since we don't want the view to scroll as
530 * the result of dragging something across it. */
536 chat_text_view_get_property (GObject *object,
541 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
544 case PROP_LAST_CONTACT:
545 g_value_set_object (value, priv->last_contact);
547 case PROP_ONLY_IF_DATE:
548 g_value_set_boolean (value, priv->only_if_date);
551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
557 chat_text_view_set_property (GObject *object,
562 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
565 case PROP_ONLY_IF_DATE:
566 priv->only_if_date = g_value_get_boolean (value);
569 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
575 chat_text_view_finalize (GObject *object)
577 EmpathyChatTextView *view;
578 EmpathyChatTextViewPriv *priv;
580 view = EMPATHY_CHAT_TEXT_VIEW (object);
581 priv = GET_PRIV (view);
583 DEBUG ("%p", object);
585 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
587 if (priv->last_contact) {
588 g_object_unref (priv->last_contact);
590 if (priv->scroll_time) {
591 g_timer_destroy (priv->scroll_time);
593 if (priv->scroll_timeout) {
594 g_source_remove (priv->scroll_timeout);
597 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
601 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
603 GObjectClass *object_class = G_OBJECT_CLASS (klass);
604 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
606 object_class->finalize = chat_text_view_finalize;
607 object_class->get_property = chat_text_view_get_property;
608 object_class->set_property = chat_text_view_set_property;
610 widget_class->size_allocate = chat_text_view_size_allocate;
611 widget_class->drag_motion = chat_text_view_drag_motion;
613 g_object_class_install_property (object_class,
615 g_param_spec_object ("last-contact",
617 "The sender of the last received message",
618 EMPATHY_TYPE_CONTACT,
620 g_object_class_install_property (object_class,
622 g_param_spec_boolean ("only-if-date",
624 "Display timestamp only if the date changes",
629 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
633 empathy_chat_text_view_init (EmpathyChatTextView *view)
635 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
636 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
639 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
640 priv->last_timestamp = 0;
641 priv->allow_scrolling = TRUE;
642 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
645 "wrap-mode", GTK_WRAP_WORD_CHAR,
647 "cursor-visible", FALSE,
650 priv->notify_system_fonts_id =
651 empathy_conf_notify_add (empathy_conf_get (),
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),
664 /* Code stolen from pidgin/gtkimhtml.c */
666 chat_text_view_scroll_cb (EmpathyChatTextView *view)
668 EmpathyChatTextViewPriv *priv;
672 priv = GET_PRIV (view);
673 adj = GTK_TEXT_VIEW (view)->vadjustment;
674 max_val = adj->upper - adj->page_size;
676 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
678 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
679 /* time's up. jump to the end and kill the timer */
680 gtk_adjustment_set_value (adj, max_val);
681 g_timer_destroy (priv->scroll_time);
682 priv->scroll_time = NULL;
683 priv->scroll_timeout = 0;
687 /* scroll by 1/3rd the remaining distance */
688 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
693 chat_text_view_scroll_down (EmpathyChatView *view)
695 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
697 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
699 if (!priv->allow_scrolling) {
703 DEBUG ("Scrolling down");
705 if (priv->scroll_time) {
706 g_timer_reset (priv->scroll_time);
708 priv->scroll_time = g_timer_new();
710 if (!priv->scroll_timeout) {
711 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
712 (GSourceFunc) chat_text_view_scroll_cb,
718 chat_text_view_append_message (EmpathyChatView *view,
721 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
722 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
726 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
727 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
729 if (!empathy_message_get_body (msg)) {
733 bottom = chat_text_view_is_scrolled_down (text_view);
735 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
737 timestamp = empathy_message_get_timestamp (msg);
738 chat_text_maybe_append_date_and_time (text_view, timestamp);
739 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
740 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
745 chat_text_view_scroll_down (view);
748 if (priv->last_contact) {
749 g_object_unref (priv->last_contact);
751 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
752 g_object_notify (G_OBJECT (view), "last-contact");
756 chat_text_view_append_event (EmpathyChatView *view,
759 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
760 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
766 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
767 g_return_if_fail (!EMP_STR_EMPTY (str));
769 bottom = chat_text_view_is_scrolled_down (text_view);
770 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
771 chat_text_maybe_append_date_and_time (text_view,
772 empathy_time_get_current ());
774 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
775 msg = g_strdup_printf (" - %s\n", str);
776 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
778 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
783 chat_text_view_scroll_down (view);
786 if (priv->last_contact) {
787 g_object_unref (priv->last_contact);
788 priv->last_contact = NULL;
789 g_object_notify (G_OBJECT (view), "last-contact");
794 chat_text_view_scroll (EmpathyChatView *view,
795 gboolean allow_scrolling)
797 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
799 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
801 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
803 priv->allow_scrolling = allow_scrolling;
804 if (allow_scrolling) {
805 empathy_chat_view_scroll_down (view);
810 chat_text_view_get_has_selection (EmpathyChatView *view)
812 GtkTextBuffer *buffer;
814 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
816 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
818 return gtk_text_buffer_get_has_selection (buffer);
822 chat_text_view_clear (EmpathyChatView *view)
824 GtkTextBuffer *buffer;
825 EmpathyChatTextViewPriv *priv;
827 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
829 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
830 gtk_text_buffer_set_text (buffer, "", -1);
832 /* We set these back to the initial values so we get
833 * timestamps when clearing the window to know when
834 * conversations start.
836 priv = GET_PRIV (view);
838 priv->last_timestamp = 0;
842 chat_text_view_find_previous (EmpathyChatView *view,
843 const gchar *search_criteria,
846 EmpathyChatTextViewPriv *priv;
847 GtkTextBuffer *buffer;
848 GtkTextIter iter_at_mark;
849 GtkTextIter iter_match_start;
850 GtkTextIter iter_match_end;
852 gboolean from_start = FALSE;
854 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
855 g_return_val_if_fail (search_criteria != NULL, FALSE);
857 priv = GET_PRIV (view);
859 buffer = priv->buffer;
861 if (EMP_STR_EMPTY (search_criteria)) {
862 if (priv->find_mark_previous) {
863 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
865 gtk_text_buffer_move_mark (buffer,
866 priv->find_mark_previous,
868 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
869 priv->find_mark_previous,
874 gtk_text_buffer_select_range (buffer,
886 if (priv->find_mark_previous) {
887 gtk_text_buffer_get_iter_at_mark (buffer,
889 priv->find_mark_previous);
891 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
895 priv->find_last_direction = FALSE;
897 found = empathy_text_iter_backward_search (&iter_at_mark,
904 gboolean result = FALSE;
910 /* Here we wrap around. */
911 if (!new_search && !priv->find_wrapped) {
912 priv->find_wrapped = TRUE;
913 result = chat_text_view_find_previous (view,
916 priv->find_wrapped = FALSE;
922 /* Set new mark and show on screen */
923 if (!priv->find_mark_previous) {
924 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
928 gtk_text_buffer_move_mark (buffer,
929 priv->find_mark_previous,
933 if (!priv->find_mark_next) {
934 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
938 gtk_text_buffer_move_mark (buffer,
939 priv->find_mark_next,
943 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
944 priv->find_mark_previous,
950 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
951 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
957 chat_text_view_find_next (EmpathyChatView *view,
958 const gchar *search_criteria,
961 EmpathyChatTextViewPriv *priv;
962 GtkTextBuffer *buffer;
963 GtkTextIter iter_at_mark;
964 GtkTextIter iter_match_start;
965 GtkTextIter iter_match_end;
967 gboolean from_start = FALSE;
969 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
970 g_return_val_if_fail (search_criteria != NULL, FALSE);
972 priv = GET_PRIV (view);
974 buffer = priv->buffer;
976 if (EMP_STR_EMPTY (search_criteria)) {
977 if (priv->find_mark_next) {
978 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
980 gtk_text_buffer_move_mark (buffer,
981 priv->find_mark_next,
983 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
984 priv->find_mark_next,
989 gtk_text_buffer_select_range (buffer,
1001 if (priv->find_mark_next) {
1002 gtk_text_buffer_get_iter_at_mark (buffer,
1004 priv->find_mark_next);
1006 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1010 priv->find_last_direction = TRUE;
1012 found = empathy_text_iter_forward_search (&iter_at_mark,
1019 gboolean result = FALSE;
1025 /* Here we wrap around. */
1026 if (!new_search && !priv->find_wrapped) {
1027 priv->find_wrapped = TRUE;
1028 result = chat_text_view_find_next (view,
1031 priv->find_wrapped = FALSE;
1037 /* Set new mark and show on screen */
1038 if (!priv->find_mark_next) {
1039 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1043 gtk_text_buffer_move_mark (buffer,
1044 priv->find_mark_next,
1048 if (!priv->find_mark_previous) {
1049 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1053 gtk_text_buffer_move_mark (buffer,
1054 priv->find_mark_previous,
1058 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1059 priv->find_mark_next,
1065 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1066 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1072 chat_text_view_find_abilities (EmpathyChatView *view,
1073 const gchar *search_criteria,
1074 gboolean *can_do_previous,
1075 gboolean *can_do_next)
1077 EmpathyChatTextViewPriv *priv;
1078 GtkTextBuffer *buffer;
1079 GtkTextIter iter_at_mark;
1080 GtkTextIter iter_match_start;
1081 GtkTextIter iter_match_end;
1083 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1084 g_return_if_fail (search_criteria != NULL);
1085 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1087 priv = GET_PRIV (view);
1089 buffer = priv->buffer;
1091 if (can_do_previous) {
1092 if (priv->find_mark_previous) {
1093 gtk_text_buffer_get_iter_at_mark (buffer,
1095 priv->find_mark_previous);
1097 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1100 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1108 if (priv->find_mark_next) {
1109 gtk_text_buffer_get_iter_at_mark (buffer,
1111 priv->find_mark_next);
1113 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1116 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1125 chat_text_view_highlight (EmpathyChatView *view,
1128 GtkTextBuffer *buffer;
1130 GtkTextIter iter_start;
1131 GtkTextIter iter_end;
1132 GtkTextIter iter_match_start;
1133 GtkTextIter iter_match_end;
1136 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1138 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1140 gtk_text_buffer_get_start_iter (buffer, &iter);
1142 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1143 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1147 if (EMP_STR_EMPTY (text)) {
1152 found = empathy_text_iter_forward_search (&iter,
1162 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1166 iter = iter_match_end;
1167 gtk_text_iter_forward_char (&iter);
1172 chat_text_view_copy_clipboard (EmpathyChatView *view)
1174 GtkTextBuffer *buffer;
1175 GtkClipboard *clipboard;
1177 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1179 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1180 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1182 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1186 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1188 iface->append_message = chat_text_view_append_message;
1189 iface->append_event = chat_text_view_append_event;
1190 iface->scroll = chat_text_view_scroll;
1191 iface->scroll_down = chat_text_view_scroll_down;
1192 iface->get_has_selection = chat_text_view_get_has_selection;
1193 iface->clear = chat_text_view_clear;
1194 iface->find_previous = chat_text_view_find_previous;
1195 iface->find_next = chat_text_view_find_next;
1196 iface->find_abilities = chat_text_view_find_abilities;
1197 iface->highlight = chat_text_view_highlight;
1198 iface->copy_clipboard = chat_text_view_copy_clipboard;
1202 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1204 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1206 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1208 return priv->last_contact;
1212 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1213 gboolean only_if_date)
1215 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1217 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1219 if (only_if_date != priv->only_if_date) {
1220 priv->only_if_date = only_if_date;
1221 g_object_notify (G_OBJECT (view), "only-if-date");
1226 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1230 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1231 gboolean use_smileys = FALSE;
1232 GSList *smileys, *l;
1234 empathy_conf_get_bool (empathy_conf_get (),
1235 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1239 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1243 smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1244 for (l = smileys; l; l = l->next) {
1245 EmpathySmiley *smiley;
1248 if (smiley->pixbuf) {
1249 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1251 gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1253 empathy_smiley_free (smiley);
1255 g_slist_free (smileys);
1259 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1263 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1264 GtkTextIter start_iter, end_iter;
1267 GMatchInfo *match_info;
1273 priv = GET_PRIV (view);
1275 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1276 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1279 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
1282 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1283 match = g_match_info_next (match_info, NULL)) {
1284 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1288 tmp = empathy_substring (body, last, s);
1290 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1291 chat_text_view_insert_text_with_emoticons (view,
1297 tmp = empathy_substring (body, s, e);
1299 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1300 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1304 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1310 g_match_info_free (match_info);
1312 if (last < strlen (body)) {
1313 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1314 chat_text_view_insert_text_with_emoticons (view,
1319 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1320 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1322 /* Apply the style to the inserted text. */
1323 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1324 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1326 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1331 gtk_text_buffer_delete_mark (priv->buffer, mark);
1335 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1337 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1340 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1341 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1345 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1346 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1351 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1352 const gchar *tag_name,
1353 const gchar *first_property_name,
1356 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1358 GtkTextTagTable *table;
1361 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1362 g_return_val_if_fail (tag_name != NULL, NULL);
1364 table = gtk_text_buffer_get_tag_table (priv->buffer);
1365 tag = gtk_text_tag_table_lookup (table, tag_name);
1367 if (tag && first_property_name) {
1368 va_start (list, first_property_name);
1369 g_object_set_valist (G_OBJECT (tag), first_property_name, list);