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 = g_object_ref_sink (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);
340 g_object_unref (priv->tooltips);
343 g_object_unref (priv->tp_chat);
346 if (priv->scroll_idle_id) {
347 g_source_remove (priv->scroll_idle_id);
352 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
356 chat_destroy_cb (EmpathyTpChat *tp_chat,
359 GossipChatPriv *priv;
362 priv = GET_PRIV (chat);
365 g_object_unref (priv->tp_chat);
366 priv->tp_chat = NULL;
369 gossip_chat_view_append_event (chat->view, _("Disconnected"));
371 widget = gossip_chat_get_widget (chat);
372 gtk_widget_set_sensitive (widget, FALSE);
373 priv->sensitive = FALSE;
377 chat_send (GossipChat *chat,
380 GossipChatPriv *priv;
381 //GossipLogManager *log_manager;
382 GossipMessage *message;
383 GossipContact *own_contact;
385 priv = GET_PRIV (chat);
387 if (G_STR_EMPTY (msg)) {
391 chat_sent_message_add (chat, msg);
393 if (g_str_has_prefix (msg, "/clear")) {
394 gossip_chat_view_clear (chat->view);
398 /* FIXME: add here something to let group/privrate chat handle
399 * some special messages */
401 /* FIXME: gossip_app_set_not_away ();*/
403 own_contact = empathy_contact_manager_get_user (priv->manager, chat->account);
404 message = gossip_message_new (msg);
405 gossip_message_set_sender (message, own_contact);
407 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
408 //gossip_log_message_for_contact (log_manager, message, FALSE);
410 empathy_tp_chat_send (priv->tp_chat, message);
412 g_object_unref (message);
416 chat_input_text_view_send (GossipChat *chat)
418 GossipChatPriv *priv;
419 GtkTextBuffer *buffer;
420 GtkTextIter start, end;
423 priv = GET_PRIV (chat);
425 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
427 gtk_text_buffer_get_bounds (buffer, &start, &end);
428 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
430 /* clear the input field */
431 gtk_text_buffer_set_text (buffer, "", -1);
433 chat_send (chat, msg);
437 chat->is_first_char = TRUE;
441 chat_message_received_cb (EmpathyTpChat *tp_chat,
442 GossipMessage *message,
445 GossipChatPriv *priv;
446 //GossipLogManager *log_manager;
447 GossipContact *sender;
449 priv = GET_PRIV (chat);
451 sender = gossip_message_get_sender (message);
452 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
453 gossip_contact_get_name (sender));
456 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
457 gossip_log_message_for_contact (log_manager, message, TRUE);
459 gossip_chat_view_append_message (chat->view, message);
461 if (gossip_chat_should_play_sound (chat)) {
462 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
465 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
469 chat_sent_message_add (GossipChat *chat,
472 GossipChatPriv *priv;
476 priv = GET_PRIV (chat);
478 /* Save the sent message in our repeat buffer */
479 list = priv->sent_messages;
481 /* Remove any other occurances of this msg */
482 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
483 list = g_slist_remove_link (list, item);
485 g_slist_free1 (item);
488 /* Trim the list to the last 10 items */
489 while (g_slist_length (list) > 10) {
490 item = g_slist_last (list);
492 list = g_slist_remove_link (list, item);
494 g_slist_free1 (item);
498 /* Add new message */
499 list = g_slist_prepend (list, g_strdup (str));
501 /* Set list and reset the index */
502 priv->sent_messages = list;
503 priv->sent_messages_index = -1;
507 chat_sent_message_get_next (GossipChat *chat)
509 GossipChatPriv *priv;
512 priv = GET_PRIV (chat);
514 if (!priv->sent_messages) {
515 gossip_debug (DEBUG_DOMAIN,
516 "No sent messages, next message is NULL");
520 max = g_slist_length (priv->sent_messages) - 1;
522 if (priv->sent_messages_index < max) {
523 priv->sent_messages_index++;
526 gossip_debug (DEBUG_DOMAIN,
527 "Returning next message index:%d",
528 priv->sent_messages_index);
530 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
534 chat_sent_message_get_last (GossipChat *chat)
536 GossipChatPriv *priv;
538 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
540 priv = GET_PRIV (chat);
542 if (!priv->sent_messages) {
543 gossip_debug (DEBUG_DOMAIN,
544 "No sent messages, last message is NULL");
548 if (priv->sent_messages_index >= 0) {
549 priv->sent_messages_index--;
552 gossip_debug (DEBUG_DOMAIN,
553 "Returning last message index:%d",
554 priv->sent_messages_index);
556 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
560 chat_input_key_press_event_cb (GtkWidget *widget,
564 GossipChatPriv *priv;
567 GtkWidget *text_view_sw;
569 priv = GET_PRIV (chat);
571 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
575 /* Catch ctrl+up/down so we can traverse messages we sent */
576 if ((event->state & GDK_CONTROL_MASK) &&
577 (event->keyval == GDK_Up ||
578 event->keyval == GDK_Down)) {
579 GtkTextBuffer *buffer;
582 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
584 if (event->keyval == GDK_Up) {
585 str = chat_sent_message_get_next (chat);
587 str = chat_sent_message_get_last (chat);
590 g_signal_handlers_block_by_func (buffer,
591 chat_input_text_buffer_changed_cb,
593 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
594 g_signal_handlers_unblock_by_func (buffer,
595 chat_input_text_buffer_changed_cb,
601 /* Catch enter but not ctrl/shift-enter */
602 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
605 /* This is to make sure that kinput2 gets the enter. And if
606 * it's handled there we shouldn't send on it. This is because
607 * kinput2 uses Enter to commit letters. See:
608 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
611 view = GTK_TEXT_VIEW (chat->input_text_view);
612 if (gtk_im_context_filter_keypress (view->im_context, event)) {
613 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
617 chat_input_text_view_send (chat);
621 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
623 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
624 /* Newline for shift-enter. */
627 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
628 event->keyval == GDK_Page_Up) {
629 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
630 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
634 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
635 event->keyval == GDK_Page_Down) {
636 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
637 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
638 gtk_adjustment_set_value (adj, val);
647 chat_text_view_focus_in_event_cb (GtkWidget *widget,
651 gtk_widget_grab_focus (chat->input_text_view);
657 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
660 GossipChatPriv *priv;
661 GtkTextIter start, end;
663 gboolean spell_checker = FALSE;
665 priv = GET_PRIV (chat);
667 if (gtk_text_buffer_get_char_count (buffer) == 0) {
668 chat_composing_stop (chat);
670 chat_composing_start (chat);
673 gossip_conf_get_bool (gossip_conf_get (),
674 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
677 if (chat->is_first_char) {
681 GtkAllocation *allocation;
683 /* Save the window's size */
684 dialog = gossip_chat_window_get_dialog (priv->window);
685 gtk_window_get_size (GTK_WINDOW (dialog),
686 NULL, &window_height);
688 gtk_widget_size_request (chat->input_text_view, &req);
690 allocation = >K_WIDGET (chat->view)->allocation;
692 priv->default_window_height = window_height;
693 priv->last_input_height = req.height;
694 priv->padding_height = window_height - req.height - allocation->height;
696 chat->is_first_char = FALSE;
699 gtk_text_buffer_get_start_iter (buffer, &start);
701 if (!spell_checker) {
702 gtk_text_buffer_get_end_iter (buffer, &end);
703 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
707 if (!gossip_spell_supported ()) {
711 /* NOTE: this is really inefficient, we shouldn't have to
712 reiterate the whole buffer each time and check each work
715 gboolean correct = FALSE;
718 if (gtk_text_iter_is_start (&start)) {
721 if (!gtk_text_iter_forward_word_end (&end)) {
722 /* no whole word yet */
726 if (!gtk_text_iter_forward_word_end (&end)) {
727 /* must be the end of the buffer */
732 gtk_text_iter_backward_word_start (&start);
735 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
737 /* spell check string */
738 if (!gossip_chat_get_is_command (str)) {
739 correct = gossip_spell_check (str);
745 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
747 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
752 /* set start iter to the end iters position */
764 chat_change_size_in_idle_cb (ChangeSizeData *data)
766 gtk_window_resize (GTK_WINDOW (data->window),
767 data->width, data->height);
773 chat_text_view_scroll_hide_cb (GtkWidget *widget,
776 GossipChatPriv *priv;
779 priv = GET_PRIV (chat);
781 priv->vscroll_visible = FALSE;
782 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
784 sw = gtk_widget_get_parent (chat->input_text_view);
785 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
788 g_object_set (sw, "height-request", -1, NULL);
792 chat_text_view_size_allocate_cb (GtkWidget *widget,
793 GtkAllocation *allocation,
796 GossipChatPriv *priv;
799 ChangeSizeData *data;
802 GtkAllocation *view_allocation;
807 priv = GET_PRIV (chat);
809 if (priv->default_window_height <= 0) {
813 sw = gtk_widget_get_parent (widget);
814 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
817 priv->vscroll_visible = TRUE;
818 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
819 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
821 GTK_POLICY_AUTOMATIC);
822 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
823 g_signal_connect (vscroll, "hide",
824 G_CALLBACK (chat_text_view_scroll_hide_cb),
828 if (priv->last_input_height <= allocation->height) {
829 priv->last_input_height = allocation->height;
833 diff = priv->last_input_height - allocation->height;
834 priv->last_input_height = allocation->height;
836 view_allocation = >K_WIDGET (chat->view)->allocation;
838 dialog = gossip_chat_window_get_dialog (priv->window);
839 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
841 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
843 if (new_height <= priv->default_window_height) {
844 window_height = priv->default_window_height;
846 window_height = new_height;
849 if (current_height <= window_height) {
853 /* Restore the window's size */
854 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
856 data = g_new0 (ChangeSizeData, 1);
857 data->window = dialog;
859 data->height = window_height;
861 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
862 (GSourceFunc) chat_change_size_in_idle_cb,
867 chat_text_view_realize_cb (GtkWidget *widget,
870 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
871 gtk_widget_grab_focus (widget);
875 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
878 GtkTextBuffer *buffer;
882 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
884 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
886 gtk_text_buffer_get_end_iter (buffer, &iter);
887 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
889 gtk_text_buffer_get_end_iter (buffer, &iter);
890 gtk_text_buffer_insert (buffer, &iter, " ", -1);
894 chat_text_populate_popup_cb (GtkTextView *view,
898 GossipChatPriv *priv;
899 GtkTextBuffer *buffer;
900 GtkTextTagTable *table;
903 GtkTextIter iter, start, end;
906 GossipChatSpell *chat_spell;
907 GtkWidget *smiley_menu;
909 priv = GET_PRIV (chat);
911 /* Add the emoticon menu. */
912 item = gtk_separator_menu_item_new ();
913 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
914 gtk_widget_show (item);
916 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
917 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
918 gtk_widget_show (item);
920 smiley_menu = gossip_chat_view_get_smiley_menu (
921 G_CALLBACK (chat_insert_smiley_activate_cb),
924 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
926 /* Add the spell check menu item. */
927 buffer = gtk_text_view_get_buffer (view);
928 table = gtk_text_buffer_get_tag_table (buffer);
930 tag = gtk_text_tag_table_lookup (table, "misspelled");
932 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
934 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
935 GTK_TEXT_WINDOW_WIDGET,
939 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
943 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
944 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
946 str = gtk_text_buffer_get_text (buffer,
947 &start, &end, FALSE);
950 if (G_STR_EMPTY (str)) {
954 chat_spell = chat_spell_new (chat, str, start, end);
956 g_object_set_data_full (G_OBJECT (menu),
957 "chat_spell", chat_spell,
958 (GDestroyNotify) chat_spell_free);
960 item = gtk_separator_menu_item_new ();
961 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
962 gtk_widget_show (item);
964 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
965 g_signal_connect (item,
967 G_CALLBACK (chat_text_check_word_spelling_cb),
969 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
970 gtk_widget_show (item);
974 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
975 GossipChatSpell *chat_spell)
977 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
983 static GossipChatSpell *
984 chat_spell_new (GossipChat *chat,
989 GossipChatSpell *chat_spell;
991 chat_spell = g_new0 (GossipChatSpell, 1);
993 chat_spell->chat = g_object_ref (chat);
994 chat_spell->word = g_strdup (word);
995 chat_spell->start = start;
996 chat_spell->end = end;
1002 chat_spell_free (GossipChatSpell *chat_spell)
1004 g_object_unref (chat_spell->chat);
1005 g_free (chat_spell->word);
1006 g_free (chat_spell);
1010 chat_composing_start (GossipChat *chat)
1012 GossipChatPriv *priv;
1014 priv = GET_PRIV (chat);
1016 if (priv->composing_stop_timeout_id) {
1017 /* Just restart the timeout */
1018 chat_composing_remove_timeout (chat);
1020 empathy_tp_chat_set_state (priv->tp_chat,
1021 TP_CHANNEL_CHAT_STATE_COMPOSING);
1024 priv->composing_stop_timeout_id = g_timeout_add (
1025 1000 * COMPOSING_STOP_TIMEOUT,
1026 (GSourceFunc) chat_composing_stop_timeout_cb,
1031 chat_composing_stop (GossipChat *chat)
1033 GossipChatPriv *priv;
1035 priv = GET_PRIV (chat);
1037 chat_composing_remove_timeout (chat);
1038 empathy_tp_chat_set_state (priv->tp_chat,
1039 TP_CHANNEL_CHAT_STATE_ACTIVE);
1043 chat_composing_remove_timeout (GossipChat *chat)
1045 GossipChatPriv *priv;
1047 priv = GET_PRIV (chat);
1049 if (priv->composing_stop_timeout_id) {
1050 g_source_remove (priv->composing_stop_timeout_id);
1051 priv->composing_stop_timeout_id = 0;
1056 chat_composing_stop_timeout_cb (GossipChat *chat)
1058 GossipChatPriv *priv;
1060 priv = GET_PRIV (chat);
1062 priv->composing_stop_timeout_id = 0;
1063 empathy_tp_chat_set_state (priv->tp_chat,
1064 TP_CHANNEL_CHAT_STATE_PAUSED);
1070 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1071 GossipContact *contact,
1072 TelepathyChannelChatState state,
1075 GossipChatPriv *priv;
1076 GossipContact *own_contact;
1078 gboolean was_composing;
1080 priv = GET_PRIV (chat);
1082 own_contact = gossip_contact_get_user (contact);
1083 if (gossip_contact_equal (own_contact, contact)) {
1084 /* We don't care about our own chat state */
1088 was_composing = (priv->compositors != NULL);
1090 /* Find the contact in the list. After that l is the list elem or NULL */
1091 for (l = priv->compositors; l; l = l->next) {
1092 if (gossip_contact_equal (contact, l->data)) {
1098 case TP_CHANNEL_CHAT_STATE_GONE:
1099 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1100 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1101 /* Contact is not composing */
1103 priv->compositors = g_list_remove_link (priv->compositors, l);
1104 g_object_unref (l->data);
1108 case TP_CHANNEL_CHAT_STATE_PAUSED:
1109 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1110 /* Contact is composing */
1112 priv->compositors = g_list_prepend (priv->compositors,
1113 g_object_ref (contact));
1117 g_assert_not_reached ();
1120 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1121 was_composing ? "yes" : "no",
1122 priv->compositors ? "yes" : "no");
1124 if ((was_composing && !priv->compositors) ||
1125 (!was_composing && priv->compositors)) {
1126 /* Composing state changed */
1127 g_signal_emit (chat, signals[COMPOSING], 0,
1128 (gboolean) priv->compositors);
1132 /* Scroll down after the back-log has been received. */
1134 chat_scroll_down_idle_func (GossipChat *chat)
1136 GossipChatPriv *priv;
1138 priv = GET_PRIV (chat);
1140 gossip_chat_scroll_down (chat);
1141 g_object_unref (chat);
1143 priv->scroll_idle_id = 0;
1149 gossip_chat_get_is_command (const gchar *str)
1151 g_return_val_if_fail (str != NULL, FALSE);
1153 if (str[0] != '/') {
1157 if (g_str_has_prefix (str, "/me")) {
1160 else if (g_str_has_prefix (str, "/nick")) {
1163 else if (g_str_has_prefix (str, "/topic")) {
1171 gossip_chat_correct_word (GossipChat *chat,
1174 const gchar *new_word)
1176 GtkTextBuffer *buffer;
1178 g_return_if_fail (chat != NULL);
1179 g_return_if_fail (new_word != NULL);
1181 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1183 gtk_text_buffer_delete (buffer, &start, &end);
1184 gtk_text_buffer_insert (buffer, &start,
1190 gossip_chat_get_name (GossipChat *chat)
1192 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1194 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1195 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1202 gossip_chat_get_tooltip (GossipChat *chat)
1204 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1206 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1207 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1214 gossip_chat_get_status_icon_name (GossipChat *chat)
1216 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1218 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1219 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1226 gossip_chat_get_widget (GossipChat *chat)
1228 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1230 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1231 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1238 gossip_chat_is_group_chat (GossipChat *chat)
1240 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1242 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1243 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1250 gossip_chat_is_connected (GossipChat *chat)
1252 GossipChatPriv *priv;
1254 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1256 priv = GET_PRIV (chat);
1258 return (priv->tp_chat != NULL);
1262 gossip_chat_save_geometry (GossipChat *chat,
1268 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1272 gossip_chat_load_geometry (GossipChat *chat,
1278 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1282 gossip_chat_set_tp_chat (GossipChat *chat,
1283 EmpathyTpChat *tp_chat)
1285 GossipChatPriv *priv;
1288 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1289 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1291 priv = GET_PRIV (chat);
1293 if (tp_chat == priv->tp_chat) {
1297 if (priv->tp_chat) {
1298 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1299 chat_message_received_cb,
1301 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1304 g_object_unref (priv->tp_chat);
1308 priv->tp_chat = g_object_ref (tp_chat);
1309 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1311 g_signal_connect (tp_chat, "message-received",
1312 G_CALLBACK (chat_message_received_cb),
1314 g_signal_connect (tp_chat, "chat-state-changed",
1315 G_CALLBACK (chat_state_changed_cb),
1317 g_signal_connect (tp_chat, "destroy",
1318 G_CALLBACK (chat_destroy_cb),
1321 empathy_tp_chat_request_pending (tp_chat);
1323 if (!priv->sensitive) {
1324 widget = gossip_chat_get_widget (chat);
1325 gtk_widget_set_sensitive (widget, TRUE);
1326 gossip_chat_view_append_event (chat->view, _("Connected"));
1327 priv->sensitive = TRUE;
1332 gossip_chat_get_id (GossipChat *chat)
1334 GossipChatPriv *priv;
1336 priv = GET_PRIV (chat);
1342 gossip_chat_clear (GossipChat *chat)
1344 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1346 gossip_chat_view_clear (chat->view);
1350 gossip_chat_set_window (GossipChat *chat,
1351 GossipChatWindow *window)
1353 GossipChatPriv *priv;
1355 priv = GET_PRIV (chat);
1356 priv->window = window;
1360 gossip_chat_get_window (GossipChat *chat)
1362 GossipChatPriv *priv;
1364 priv = GET_PRIV (chat);
1366 return priv->window;
1370 gossip_chat_scroll_down (GossipChat *chat)
1372 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1374 gossip_chat_view_scroll_down (chat->view);
1378 gossip_chat_cut (GossipChat *chat)
1380 GtkTextBuffer *buffer;
1382 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1384 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1385 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1386 GtkClipboard *clipboard;
1388 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1390 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1395 gossip_chat_copy (GossipChat *chat)
1397 GtkTextBuffer *buffer;
1399 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1401 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1402 gossip_chat_view_copy_clipboard (chat->view);
1406 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1407 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1408 GtkClipboard *clipboard;
1410 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1412 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1417 gossip_chat_paste (GossipChat *chat)
1419 GtkTextBuffer *buffer;
1420 GtkClipboard *clipboard;
1422 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1424 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1425 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1427 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1431 gossip_chat_present (GossipChat *chat)
1433 GossipChatPriv *priv;
1435 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1437 priv = GET_PRIV (chat);
1439 if (priv->window == NULL) {
1440 GossipChatWindow *window;
1442 window = gossip_chat_window_get_default ();
1444 window = gossip_chat_window_new ();
1447 gossip_chat_window_add_chat (window, chat);
1450 gossip_chat_window_switch_to_chat (priv->window, chat);
1451 gossip_window_present (
1452 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1455 gtk_widget_grab_focus (chat->input_text_view);
1459 gossip_chat_should_play_sound (GossipChat *chat)
1461 GossipChatWindow *window;
1462 gboolean play = TRUE;
1464 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1466 window = gossip_chat_get_window (chat);
1471 play = !gossip_chat_window_has_focus (window);
1477 gossip_chat_should_highlight_nick (GossipMessage *message)
1479 GossipContact *my_contact;
1480 const gchar *msg, *to;
1481 gchar *cf_msg, *cf_to;
1485 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1487 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1491 msg = gossip_message_get_body (message);
1496 my_contact = gossip_contact_get_user (gossip_message_get_sender (message));
1497 to = gossip_contact_get_name (my_contact);
1502 cf_msg = g_utf8_casefold (msg, -1);
1503 cf_to = g_utf8_casefold (to, -1);
1505 ch = strstr (cf_msg, cf_to);
1511 /* Not first in the message */
1512 if ((*(ch - 1) != ' ') &&
1513 (*(ch - 1) != ',') &&
1514 (*(ch - 1) != '.')) {
1519 ch = ch + strlen (cf_to);
1520 if (ch >= cf_msg + strlen (cf_msg)) {