1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007 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 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
36 #include <glade/glade.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/gossip-conf.h>
42 #include <libempathy/empathy-marshal.h>
44 #include "gossip-chat.h"
45 #include "gossip-chat-window.h"
46 #include "gossip-geometry.h"
47 #include "gossip-preferences.h"
48 #include "gossip-spell.h"
49 //#include "gossip-spell-dialog.h"
50 #include "gossip-ui-utils.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
54 #define DEBUG_DOMAIN "Chat"
56 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
59 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
63 #define COMPOSING_STOP_TIMEOUT 5
65 struct _GossipChatPriv {
66 EmpathyContactManager *manager;
67 EmpathyTpChat *tp_chat;
68 GossipChatWindow *window;
69 GtkTooltips *tooltips;
70 guint composing_stop_timeout_id;
73 GSList *sent_messages;
74 gint sent_messages_index;
77 /* Used to automatically shrink a window that has temporarily
78 * grown due to long input.
81 gint default_window_height;
82 gint last_input_height;
83 gboolean vscroll_visible;
94 static void gossip_chat_class_init (GossipChatClass *klass);
95 static void gossip_chat_init (GossipChat *chat);
96 static void chat_finalize (GObject *object);
97 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
99 static void chat_send (GossipChat *chat,
101 static void chat_input_text_view_send (GossipChat *chat);
102 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
103 GossipMessage *message,
105 void chat_sent_message_add (GossipChat *chat,
107 const gchar * chat_sent_message_get_next (GossipChat *chat);
108 const gchar * chat_sent_message_get_last (GossipChat *chat);
109 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
112 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
114 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
117 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
119 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
120 GtkAllocation *allocation,
122 static void chat_text_view_realize_cb (GtkWidget *widget,
124 static void chat_text_populate_popup_cb (GtkTextView *view,
127 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
128 GossipChatSpell *chat_spell);
129 static GossipChatSpell *chat_spell_new (GossipChat *chat,
133 static void chat_spell_free (GossipChatSpell *chat_spell);
134 static void chat_composing_start (GossipChat *chat);
135 static void chat_composing_stop (GossipChat *chat);
136 static void chat_composing_remove_timeout (GossipChat *chat);
137 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
138 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
139 GossipContact *contact,
140 TelepathyChannelChatState state,
142 static gboolean chat_scroll_down_idle_func (GossipChat *chat);
152 static guint signals[LAST_SIGNAL] = { 0 };
154 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
157 gossip_chat_class_init (GossipChatClass *klass)
159 GObjectClass *object_class;
161 object_class = G_OBJECT_CLASS (klass);
163 object_class->finalize = chat_finalize;
166 g_signal_new ("composing",
167 G_OBJECT_CLASS_TYPE (object_class),
171 g_cclosure_marshal_VOID__BOOLEAN,
175 signals[NEW_MESSAGE] =
176 g_signal_new ("new-message",
177 G_OBJECT_CLASS_TYPE (object_class),
181 empathy_marshal_VOID__OBJECT_BOOLEAN,
183 2, GOSSIP_TYPE_MESSAGE, G_TYPE_BOOLEAN);
185 signals[NAME_CHANGED] =
186 g_signal_new ("name-changed",
187 G_OBJECT_CLASS_TYPE (object_class),
191 g_cclosure_marshal_VOID__POINTER,
195 signals[STATUS_CHANGED] =
196 g_signal_new ("status-changed",
197 G_OBJECT_CLASS_TYPE (object_class),
201 g_cclosure_marshal_VOID__VOID,
205 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
209 gossip_chat_init (GossipChat *chat)
211 GossipChatPriv *priv;
212 GtkTextBuffer *buffer;
214 chat->view = gossip_chat_view_new ();
215 chat->input_text_view = gtk_text_view_new ();
217 chat->is_first_char = TRUE;
219 g_object_set (chat->input_text_view,
220 "pixels-above-lines", 2,
221 "pixels-below-lines", 2,
222 "pixels-inside-wrap", 1,
225 "wrap-mode", GTK_WRAP_WORD_CHAR,
228 priv = GET_PRIV (chat);
230 priv->manager = empathy_contact_manager_new ();
231 priv->tooltips = gtk_tooltips_new ();
232 priv->default_window_height = -1;
233 priv->vscroll_visible = FALSE;
234 priv->sensitive = TRUE;
235 priv->sent_messages = NULL;
236 priv->sent_messages_index = -1;
238 g_signal_connect (chat->input_text_view,
240 G_CALLBACK (chat_input_key_press_event_cb),
243 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
244 g_signal_connect (buffer,
246 G_CALLBACK (chat_input_text_buffer_changed_cb),
248 g_signal_connect (chat->view,
250 G_CALLBACK (chat_text_view_focus_in_event_cb),
253 g_signal_connect (chat->input_text_view,
255 G_CALLBACK (chat_text_view_size_allocate_cb),
258 g_signal_connect (chat->input_text_view,
260 G_CALLBACK (chat_text_view_realize_cb),
263 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
265 G_CALLBACK (chat_text_populate_popup_cb),
268 /* create misspelt words identification tag */
269 gtk_text_buffer_create_tag (buffer,
271 "underline", PANGO_UNDERLINE_ERROR,
276 /* Turn off scrolling temporarily */
277 gossip_chat_view_scroll (chat->view, FALSE);
280 /* Add messages from last conversation */
281 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
282 messages = gossip_log_get_last_for_contact (log_manager, priv->contact);
283 num_messages = g_list_length (messages);
285 for (l = messages, i = 0; l; l = l->next, i++) {
288 if (num_messages - i > 10) {
292 sender = gossip_message_get_sender (message);
293 if (gossip_contact_equal (priv->own_contact, sender)) {
294 gossip_chat_view_append_message_from_self (view,
299 gossip_chat_view_append_message_from_other (view,
306 g_list_foreach (messages, (GFunc) g_object_unref, NULL);
307 g_list_free (messages);
309 /* Turn back on scrolling */
310 gossip_chat_view_scroll (chat->view, TRUE);
312 /* Scroll to the most recent messages, we reference the chat
313 * for the duration of the scroll func.
315 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
316 g_object_ref (chat));
321 chat_finalize (GObject *object)
324 GossipChatPriv *priv;
326 chat = GOSSIP_CHAT (object);
327 priv = GET_PRIV (chat);
329 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
331 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
332 g_slist_free (priv->sent_messages);
334 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
335 g_list_free (priv->compositors);
337 chat_composing_remove_timeout (chat);
338 g_object_unref (chat->account);
339 g_object_unref (priv->manager);
342 g_object_unref (priv->tp_chat);
345 if (priv->scroll_idle_id) {
346 g_source_remove (priv->scroll_idle_id);
351 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
355 chat_destroy_cb (EmpathyTpChat *tp_chat,
358 GossipChatPriv *priv;
361 priv = GET_PRIV (chat);
364 g_object_unref (priv->tp_chat);
365 priv->tp_chat = NULL;
368 gossip_chat_view_append_event (chat->view, _("Disconnected"));
370 widget = gossip_chat_get_widget (chat);
371 gtk_widget_set_sensitive (widget, FALSE);
372 priv->sensitive = FALSE;
376 chat_send (GossipChat *chat,
379 GossipChatPriv *priv;
380 //GossipLogManager *log_manager;
381 GossipMessage *message;
382 GossipContact *own_contact;
384 priv = GET_PRIV (chat);
386 if (G_STR_EMPTY (msg)) {
390 chat_sent_message_add (chat, msg);
392 if (g_str_has_prefix (msg, "/clear")) {
393 gossip_chat_view_clear (chat->view);
397 /* FIXME: add here something to let group/privrate chat handle
398 * some special messages */
400 /* FIXME: gossip_app_set_not_away ();*/
402 own_contact = empathy_contact_manager_get_user (priv->manager, chat->account);
403 message = gossip_message_new (msg);
404 gossip_message_set_sender (message, own_contact);
406 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
407 //gossip_log_message_for_contact (log_manager, message, FALSE);
409 empathy_tp_chat_send (priv->tp_chat, message);
411 g_object_unref (message);
415 chat_input_text_view_send (GossipChat *chat)
417 GossipChatPriv *priv;
418 GtkTextBuffer *buffer;
419 GtkTextIter start, end;
422 priv = GET_PRIV (chat);
424 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
426 gtk_text_buffer_get_bounds (buffer, &start, &end);
427 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
429 /* clear the input field */
430 gtk_text_buffer_set_text (buffer, "", -1);
432 chat_send (chat, msg);
436 chat->is_first_char = TRUE;
440 chat_message_received_cb (EmpathyTpChat *tp_chat,
441 GossipMessage *message,
444 GossipChatPriv *priv;
445 //GossipLogManager *log_manager;
446 GossipContact *sender;
448 priv = GET_PRIV (chat);
450 sender = gossip_message_get_sender (message);
451 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
452 gossip_contact_get_name (sender));
455 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
456 gossip_log_message_for_contact (log_manager, message, TRUE);
458 gossip_chat_view_append_message (chat->view, message);
460 if (gossip_chat_should_play_sound (chat)) {
461 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
464 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
468 chat_sent_message_add (GossipChat *chat,
471 GossipChatPriv *priv;
475 priv = GET_PRIV (chat);
477 /* Save the sent message in our repeat buffer */
478 list = priv->sent_messages;
480 /* Remove any other occurances of this msg */
481 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
482 list = g_slist_remove_link (list, item);
484 g_slist_free1 (item);
487 /* Trim the list to the last 10 items */
488 while (g_slist_length (list) > 10) {
489 item = g_slist_last (list);
491 list = g_slist_remove_link (list, item);
493 g_slist_free1 (item);
497 /* Add new message */
498 list = g_slist_prepend (list, g_strdup (str));
500 /* Set list and reset the index */
501 priv->sent_messages = list;
502 priv->sent_messages_index = -1;
506 chat_sent_message_get_next (GossipChat *chat)
508 GossipChatPriv *priv;
511 priv = GET_PRIV (chat);
513 if (!priv->sent_messages) {
514 gossip_debug (DEBUG_DOMAIN,
515 "No sent messages, next message is NULL");
519 max = g_slist_length (priv->sent_messages) - 1;
521 if (priv->sent_messages_index < max) {
522 priv->sent_messages_index++;
525 gossip_debug (DEBUG_DOMAIN,
526 "Returning next message index:%d",
527 priv->sent_messages_index);
529 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
533 chat_sent_message_get_last (GossipChat *chat)
535 GossipChatPriv *priv;
537 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
539 priv = GET_PRIV (chat);
541 if (!priv->sent_messages) {
542 gossip_debug (DEBUG_DOMAIN,
543 "No sent messages, last message is NULL");
547 if (priv->sent_messages_index >= 0) {
548 priv->sent_messages_index--;
551 gossip_debug (DEBUG_DOMAIN,
552 "Returning last message index:%d",
553 priv->sent_messages_index);
555 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
559 chat_input_key_press_event_cb (GtkWidget *widget,
563 GossipChatPriv *priv;
566 GtkWidget *text_view_sw;
568 priv = GET_PRIV (chat);
570 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
574 /* Catch ctrl+up/down so we can traverse messages we sent */
575 if ((event->state & GDK_CONTROL_MASK) &&
576 (event->keyval == GDK_Up ||
577 event->keyval == GDK_Down)) {
578 GtkTextBuffer *buffer;
581 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
583 if (event->keyval == GDK_Up) {
584 str = chat_sent_message_get_next (chat);
586 str = chat_sent_message_get_last (chat);
589 g_signal_handlers_block_by_func (buffer,
590 chat_input_text_buffer_changed_cb,
592 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
593 g_signal_handlers_unblock_by_func (buffer,
594 chat_input_text_buffer_changed_cb,
600 /* Catch enter but not ctrl/shift-enter */
601 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
604 /* This is to make sure that kinput2 gets the enter. And if
605 * it's handled there we shouldn't send on it. This is because
606 * kinput2 uses Enter to commit letters. See:
607 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
610 view = GTK_TEXT_VIEW (chat->input_text_view);
611 if (gtk_im_context_filter_keypress (view->im_context, event)) {
612 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
616 chat_input_text_view_send (chat);
620 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
622 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
623 /* Newline for shift-enter. */
626 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
627 event->keyval == GDK_Page_Up) {
628 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
629 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
633 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
634 event->keyval == GDK_Page_Down) {
635 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
636 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
637 gtk_adjustment_set_value (adj, val);
646 chat_text_view_focus_in_event_cb (GtkWidget *widget,
650 gtk_widget_grab_focus (chat->input_text_view);
656 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
659 GossipChatPriv *priv;
660 GtkTextIter start, end;
662 gboolean spell_checker = FALSE;
664 priv = GET_PRIV (chat);
666 if (gtk_text_buffer_get_char_count (buffer) == 0) {
667 chat_composing_stop (chat);
669 chat_composing_start (chat);
672 gossip_conf_get_bool (gossip_conf_get (),
673 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
676 if (chat->is_first_char) {
680 GtkAllocation *allocation;
682 /* Save the window's size */
683 dialog = gossip_chat_window_get_dialog (priv->window);
684 gtk_window_get_size (GTK_WINDOW (dialog),
685 NULL, &window_height);
687 gtk_widget_size_request (chat->input_text_view, &req);
689 allocation = >K_WIDGET (chat->view)->allocation;
691 priv->default_window_height = window_height;
692 priv->last_input_height = req.height;
693 priv->padding_height = window_height - req.height - allocation->height;
695 chat->is_first_char = FALSE;
698 gtk_text_buffer_get_start_iter (buffer, &start);
700 if (!spell_checker) {
701 gtk_text_buffer_get_end_iter (buffer, &end);
702 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
706 if (!gossip_spell_supported ()) {
710 /* NOTE: this is really inefficient, we shouldn't have to
711 reiterate the whole buffer each time and check each work
714 gboolean correct = FALSE;
717 if (gtk_text_iter_is_start (&start)) {
720 if (!gtk_text_iter_forward_word_end (&end)) {
721 /* no whole word yet */
725 if (!gtk_text_iter_forward_word_end (&end)) {
726 /* must be the end of the buffer */
731 gtk_text_iter_backward_word_start (&start);
734 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
736 /* spell check string */
737 if (!gossip_chat_get_is_command (str)) {
738 correct = gossip_spell_check (str);
744 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
746 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
751 /* set start iter to the end iters position */
763 chat_change_size_in_idle_cb (ChangeSizeData *data)
765 gtk_window_resize (GTK_WINDOW (data->window),
766 data->width, data->height);
772 chat_text_view_scroll_hide_cb (GtkWidget *widget,
775 GossipChatPriv *priv;
778 priv = GET_PRIV (chat);
780 priv->vscroll_visible = FALSE;
781 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
783 sw = gtk_widget_get_parent (chat->input_text_view);
784 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
787 g_object_set (sw, "height-request", -1, NULL);
791 chat_text_view_size_allocate_cb (GtkWidget *widget,
792 GtkAllocation *allocation,
795 GossipChatPriv *priv;
798 ChangeSizeData *data;
801 GtkAllocation *view_allocation;
806 priv = GET_PRIV (chat);
808 if (priv->default_window_height <= 0) {
812 sw = gtk_widget_get_parent (widget);
813 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
816 priv->vscroll_visible = TRUE;
817 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
818 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
820 GTK_POLICY_AUTOMATIC);
821 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
822 g_signal_connect (vscroll, "hide",
823 G_CALLBACK (chat_text_view_scroll_hide_cb),
827 if (priv->last_input_height <= allocation->height) {
828 priv->last_input_height = allocation->height;
832 diff = priv->last_input_height - allocation->height;
833 priv->last_input_height = allocation->height;
835 view_allocation = >K_WIDGET (chat->view)->allocation;
837 dialog = gossip_chat_window_get_dialog (priv->window);
838 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
840 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
842 if (new_height <= priv->default_window_height) {
843 window_height = priv->default_window_height;
845 window_height = new_height;
848 if (current_height <= window_height) {
852 /* Restore the window's size */
853 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
855 data = g_new0 (ChangeSizeData, 1);
856 data->window = dialog;
858 data->height = window_height;
860 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
861 (GSourceFunc) chat_change_size_in_idle_cb,
866 chat_text_view_realize_cb (GtkWidget *widget,
869 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
870 gtk_widget_grab_focus (widget);
874 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
877 GtkTextBuffer *buffer;
881 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
883 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
885 gtk_text_buffer_get_end_iter (buffer, &iter);
886 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
888 gtk_text_buffer_get_end_iter (buffer, &iter);
889 gtk_text_buffer_insert (buffer, &iter, " ", -1);
893 chat_text_populate_popup_cb (GtkTextView *view,
897 GossipChatPriv *priv;
898 GtkTextBuffer *buffer;
899 GtkTextTagTable *table;
902 GtkTextIter iter, start, end;
905 GossipChatSpell *chat_spell;
906 GtkWidget *smiley_menu;
908 priv = GET_PRIV (chat);
910 /* Add the emoticon menu. */
911 item = gtk_separator_menu_item_new ();
912 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
913 gtk_widget_show (item);
915 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
916 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
917 gtk_widget_show (item);
919 smiley_menu = gossip_chat_view_get_smiley_menu (
920 G_CALLBACK (chat_insert_smiley_activate_cb),
923 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
925 /* Add the spell check menu item. */
926 buffer = gtk_text_view_get_buffer (view);
927 table = gtk_text_buffer_get_tag_table (buffer);
929 tag = gtk_text_tag_table_lookup (table, "misspelled");
931 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
933 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
934 GTK_TEXT_WINDOW_WIDGET,
938 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
942 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
943 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
945 str = gtk_text_buffer_get_text (buffer,
946 &start, &end, FALSE);
949 if (G_STR_EMPTY (str)) {
953 chat_spell = chat_spell_new (chat, str, start, end);
955 g_object_set_data_full (G_OBJECT (menu),
956 "chat_spell", chat_spell,
957 (GDestroyNotify) chat_spell_free);
959 item = gtk_separator_menu_item_new ();
960 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
961 gtk_widget_show (item);
963 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
964 g_signal_connect (item,
966 G_CALLBACK (chat_text_check_word_spelling_cb),
968 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
969 gtk_widget_show (item);
973 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
974 GossipChatSpell *chat_spell)
976 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
982 static GossipChatSpell *
983 chat_spell_new (GossipChat *chat,
988 GossipChatSpell *chat_spell;
990 chat_spell = g_new0 (GossipChatSpell, 1);
992 chat_spell->chat = g_object_ref (chat);
993 chat_spell->word = g_strdup (word);
994 chat_spell->start = start;
995 chat_spell->end = end;
1001 chat_spell_free (GossipChatSpell *chat_spell)
1003 g_object_unref (chat_spell->chat);
1004 g_free (chat_spell->word);
1005 g_free (chat_spell);
1009 chat_composing_start (GossipChat *chat)
1011 GossipChatPriv *priv;
1013 priv = GET_PRIV (chat);
1015 if (priv->composing_stop_timeout_id) {
1016 /* Just restart the timeout */
1017 chat_composing_remove_timeout (chat);
1019 empathy_tp_chat_set_state (priv->tp_chat,
1020 TP_CHANNEL_CHAT_STATE_COMPOSING);
1023 priv->composing_stop_timeout_id = g_timeout_add (
1024 1000 * COMPOSING_STOP_TIMEOUT,
1025 (GSourceFunc) chat_composing_stop_timeout_cb,
1030 chat_composing_stop (GossipChat *chat)
1032 GossipChatPriv *priv;
1034 priv = GET_PRIV (chat);
1036 chat_composing_remove_timeout (chat);
1037 empathy_tp_chat_set_state (priv->tp_chat,
1038 TP_CHANNEL_CHAT_STATE_ACTIVE);
1042 chat_composing_remove_timeout (GossipChat *chat)
1044 GossipChatPriv *priv;
1046 priv = GET_PRIV (chat);
1048 if (priv->composing_stop_timeout_id) {
1049 g_source_remove (priv->composing_stop_timeout_id);
1050 priv->composing_stop_timeout_id = 0;
1055 chat_composing_stop_timeout_cb (GossipChat *chat)
1057 GossipChatPriv *priv;
1059 priv = GET_PRIV (chat);
1061 priv->composing_stop_timeout_id = 0;
1062 empathy_tp_chat_set_state (priv->tp_chat,
1063 TP_CHANNEL_CHAT_STATE_PAUSED);
1069 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1070 GossipContact *contact,
1071 TelepathyChannelChatState state,
1074 GossipChatPriv *priv;
1076 gboolean was_composing;
1078 priv = GET_PRIV (chat);
1080 was_composing = (priv->compositors != NULL);
1082 /* Find the contact in the list. After that l is the list elem or NULL */
1083 for (l = priv->compositors; l; l = l->next) {
1084 if (gossip_contact_equal (contact, l->data)) {
1090 case TP_CHANNEL_CHAT_STATE_GONE:
1091 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1092 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1093 /* Contact is not composing */
1095 priv->compositors = g_list_remove_link (priv->compositors, l);
1096 g_object_unref (l->data);
1100 case TP_CHANNEL_CHAT_STATE_PAUSED:
1101 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1102 /* Contact is composing */
1104 priv->compositors = g_list_prepend (priv->compositors,
1105 g_object_ref (contact));
1109 g_assert_not_reached ();
1112 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1113 was_composing ? "yes" : "no",
1114 priv->compositors ? "yes" : "no");
1116 if ((was_composing && !priv->compositors) ||
1117 (!was_composing && priv->compositors)) {
1118 /* Composing state changed */
1119 g_signal_emit (chat, signals[COMPOSING], 0,
1120 (gboolean) priv->compositors);
1124 /* Scroll down after the back-log has been received. */
1126 chat_scroll_down_idle_func (GossipChat *chat)
1128 GossipChatPriv *priv;
1130 priv = GET_PRIV (chat);
1132 gossip_chat_scroll_down (chat);
1133 g_object_unref (chat);
1135 priv->scroll_idle_id = 0;
1141 gossip_chat_get_is_command (const gchar *str)
1143 g_return_val_if_fail (str != NULL, FALSE);
1145 if (str[0] != '/') {
1149 if (g_str_has_prefix (str, "/me")) {
1152 else if (g_str_has_prefix (str, "/nick")) {
1155 else if (g_str_has_prefix (str, "/topic")) {
1163 gossip_chat_correct_word (GossipChat *chat,
1166 const gchar *new_word)
1168 GtkTextBuffer *buffer;
1170 g_return_if_fail (chat != NULL);
1171 g_return_if_fail (new_word != NULL);
1173 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1175 gtk_text_buffer_delete (buffer, &start, &end);
1176 gtk_text_buffer_insert (buffer, &start,
1182 gossip_chat_get_name (GossipChat *chat)
1184 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1186 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1187 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1194 gossip_chat_get_tooltip (GossipChat *chat)
1196 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1198 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1199 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1206 gossip_chat_get_status_icon_name (GossipChat *chat)
1208 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1210 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1211 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1218 gossip_chat_get_widget (GossipChat *chat)
1220 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1222 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1223 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1230 gossip_chat_is_group_chat (GossipChat *chat)
1232 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1234 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1235 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1242 gossip_chat_is_connected (GossipChat *chat)
1244 GossipChatPriv *priv;
1246 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1248 priv = GET_PRIV (chat);
1250 return (priv->tp_chat != NULL);
1254 gossip_chat_save_geometry (GossipChat *chat,
1260 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1264 gossip_chat_load_geometry (GossipChat *chat,
1270 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1274 gossip_chat_set_tp_chat (GossipChat *chat,
1275 EmpathyTpChat *tp_chat)
1277 GossipChatPriv *priv;
1280 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1281 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1283 priv = GET_PRIV (chat);
1285 if (tp_chat == priv->tp_chat) {
1289 if (priv->tp_chat) {
1290 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1291 chat_message_received_cb,
1293 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1296 g_object_unref (priv->tp_chat);
1300 priv->tp_chat = g_object_ref (tp_chat);
1301 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1303 g_signal_connect (tp_chat, "message-received",
1304 G_CALLBACK (chat_message_received_cb),
1306 g_signal_connect (tp_chat, "chat-state-changed",
1307 G_CALLBACK (chat_state_changed_cb),
1309 g_signal_connect (tp_chat, "destroy",
1310 G_CALLBACK (chat_destroy_cb),
1313 empathy_tp_chat_request_pending (tp_chat);
1315 if (!priv->sensitive) {
1316 widget = gossip_chat_get_widget (chat);
1317 gtk_widget_set_sensitive (widget, TRUE);
1318 gossip_chat_view_append_event (chat->view, _("Connected"));
1319 priv->sensitive = TRUE;
1324 gossip_chat_get_id (GossipChat *chat)
1326 GossipChatPriv *priv;
1328 priv = GET_PRIV (chat);
1334 gossip_chat_clear (GossipChat *chat)
1336 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1338 gossip_chat_view_clear (chat->view);
1342 gossip_chat_set_window (GossipChat *chat,
1343 GossipChatWindow *window)
1345 GossipChatPriv *priv;
1347 priv = GET_PRIV (chat);
1348 priv->window = window;
1352 gossip_chat_get_window (GossipChat *chat)
1354 GossipChatPriv *priv;
1356 priv = GET_PRIV (chat);
1358 return priv->window;
1362 gossip_chat_scroll_down (GossipChat *chat)
1364 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1366 gossip_chat_view_scroll_down (chat->view);
1370 gossip_chat_cut (GossipChat *chat)
1372 GtkTextBuffer *buffer;
1374 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1376 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1377 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1378 GtkClipboard *clipboard;
1380 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1382 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1387 gossip_chat_copy (GossipChat *chat)
1389 GtkTextBuffer *buffer;
1391 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1393 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1394 gossip_chat_view_copy_clipboard (chat->view);
1398 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1399 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1400 GtkClipboard *clipboard;
1402 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1404 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1409 gossip_chat_paste (GossipChat *chat)
1411 GtkTextBuffer *buffer;
1412 GtkClipboard *clipboard;
1414 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1416 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1417 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1419 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1423 gossip_chat_present (GossipChat *chat)
1425 GossipChatPriv *priv;
1427 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1429 priv = GET_PRIV (chat);
1431 if (priv->window == NULL) {
1432 GossipChatWindow *window;
1434 window = gossip_chat_window_get_default ();
1436 window = gossip_chat_window_new ();
1439 gossip_chat_window_add_chat (window, chat);
1442 gossip_chat_window_switch_to_chat (priv->window, chat);
1443 gossip_window_present (
1444 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1447 gtk_widget_grab_focus (chat->input_text_view);
1451 gossip_chat_should_play_sound (GossipChat *chat)
1453 GossipChatWindow *window;
1454 gboolean play = TRUE;
1456 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1458 window = gossip_chat_get_window (chat);
1463 play = !gossip_chat_window_has_focus (window);
1469 gossip_chat_should_highlight_nick (GossipMessage *message)
1471 GossipContact *my_contact;
1472 const gchar *msg, *to;
1473 gchar *cf_msg, *cf_to;
1477 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1479 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1483 msg = gossip_message_get_body (message);
1488 my_contact = gossip_contact_get_user (gossip_message_get_sender (message));
1489 to = gossip_contact_get_name (my_contact);
1494 cf_msg = g_utf8_casefold (msg, -1);
1495 cf_to = g_utf8_casefold (to, -1);
1497 ch = strstr (cf_msg, cf_to);
1503 /* Not first in the message */
1504 if ((*(ch - 1) != ' ') &&
1505 (*(ch - 1) != ',') &&
1506 (*(ch - 1) != '.')) {
1511 ch = ch + strlen (cf_to);
1512 if (ch >= cf_msg + strlen (cf_msg)) {