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;
76 /* Used to automatically shrink a window that has temporarily
77 * grown due to long input.
80 gint default_window_height;
81 gint last_input_height;
82 gboolean vscroll_visible;
93 static void gossip_chat_class_init (GossipChatClass *klass);
94 static void gossip_chat_init (GossipChat *chat);
95 static void chat_finalize (GObject *object);
96 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
98 static void chat_send (GossipChat *chat,
100 static void chat_input_text_view_send (GossipChat *chat);
101 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
102 GossipMessage *message,
104 void chat_sent_message_add (GossipChat *chat,
106 const gchar * chat_sent_message_get_next (GossipChat *chat);
107 const gchar * chat_sent_message_get_last (GossipChat *chat);
108 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
111 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
113 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
116 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
118 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
119 GtkAllocation *allocation,
121 static void chat_text_view_realize_cb (GtkWidget *widget,
123 static void chat_text_populate_popup_cb (GtkTextView *view,
126 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
127 GossipChatSpell *chat_spell);
128 static GossipChatSpell *chat_spell_new (GossipChat *chat,
132 static void chat_spell_free (GossipChatSpell *chat_spell);
133 static void chat_composing_start (GossipChat *chat);
134 static void chat_composing_stop (GossipChat *chat);
135 static void chat_composing_remove_timeout (GossipChat *chat);
136 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
137 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
138 GossipContact *contact,
139 TelepathyChannelChatState state,
150 static guint signals[LAST_SIGNAL] = { 0 };
152 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
155 gossip_chat_class_init (GossipChatClass *klass)
157 GObjectClass *object_class;
159 object_class = G_OBJECT_CLASS (klass);
161 object_class->finalize = chat_finalize;
164 g_signal_new ("composing",
165 G_OBJECT_CLASS_TYPE (object_class),
169 g_cclosure_marshal_VOID__BOOLEAN,
173 signals[NEW_MESSAGE] =
174 g_signal_new ("new-message",
175 G_OBJECT_CLASS_TYPE (object_class),
179 empathy_marshal_VOID__OBJECT_BOOLEAN,
181 2, GOSSIP_TYPE_MESSAGE, G_TYPE_BOOLEAN);
183 signals[NAME_CHANGED] =
184 g_signal_new ("name-changed",
185 G_OBJECT_CLASS_TYPE (object_class),
189 g_cclosure_marshal_VOID__POINTER,
193 signals[STATUS_CHANGED] =
194 g_signal_new ("status-changed",
195 G_OBJECT_CLASS_TYPE (object_class),
199 g_cclosure_marshal_VOID__VOID,
203 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
207 gossip_chat_init (GossipChat *chat)
209 GossipChatPriv *priv;
210 GtkTextBuffer *buffer;
212 chat->view = gossip_chat_view_new ();
213 chat->input_text_view = gtk_text_view_new ();
215 chat->is_first_char = TRUE;
217 g_object_set (chat->input_text_view,
218 "pixels-above-lines", 2,
219 "pixels-below-lines", 2,
220 "pixels-inside-wrap", 1,
223 "wrap-mode", GTK_WRAP_WORD_CHAR,
226 priv = GET_PRIV (chat);
228 priv->manager = empathy_contact_manager_new ();
229 priv->tooltips = gtk_tooltips_new ();
230 priv->default_window_height = -1;
231 priv->vscroll_visible = FALSE;
232 priv->sensitive = TRUE;
233 priv->sent_messages = NULL;
234 priv->sent_messages_index = -1;
236 g_signal_connect (chat->input_text_view,
238 G_CALLBACK (chat_input_key_press_event_cb),
241 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
242 g_signal_connect (buffer,
244 G_CALLBACK (chat_input_text_buffer_changed_cb),
246 g_signal_connect (chat->view,
248 G_CALLBACK (chat_text_view_focus_in_event_cb),
251 g_signal_connect (chat->input_text_view,
253 G_CALLBACK (chat_text_view_size_allocate_cb),
256 g_signal_connect (chat->input_text_view,
258 G_CALLBACK (chat_text_view_realize_cb),
261 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
263 G_CALLBACK (chat_text_populate_popup_cb),
266 /* create misspelt words identification tag */
267 gtk_text_buffer_create_tag (buffer,
269 "underline", PANGO_UNDERLINE_ERROR,
274 chat_finalize (GObject *object)
277 GossipChatPriv *priv;
279 chat = GOSSIP_CHAT (object);
280 priv = GET_PRIV (chat);
282 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
284 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
285 g_slist_free (priv->sent_messages);
287 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
288 g_list_free (priv->compositors);
290 chat_composing_remove_timeout (chat);
291 g_object_unref (chat->account);
292 g_object_unref (priv->manager);
295 g_object_unref (priv->tp_chat);
300 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
304 chat_destroy_cb (EmpathyTpChat *tp_chat,
307 GossipChatPriv *priv;
310 priv = GET_PRIV (chat);
313 g_object_unref (priv->tp_chat);
314 priv->tp_chat = NULL;
317 gossip_chat_view_append_event (chat->view, _("Disconnected"));
319 widget = gossip_chat_get_widget (chat);
320 gtk_widget_set_sensitive (widget, FALSE);
321 priv->sensitive = FALSE;
325 chat_send (GossipChat *chat,
328 GossipChatPriv *priv;
329 //GossipLogManager *log_manager;
330 GossipMessage *message;
331 GossipContact *own_contact;
333 priv = GET_PRIV (chat);
335 if (G_STR_EMPTY (msg)) {
339 chat_sent_message_add (chat, msg);
341 if (g_str_has_prefix (msg, "/clear")) {
342 gossip_chat_view_clear (chat->view);
346 /* FIXME: gossip_app_set_not_away ();*/
348 own_contact = gossip_chat_get_own_contact (chat);
349 message = gossip_message_new (msg);
350 gossip_message_set_sender (message, own_contact);
352 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
353 //gossip_log_message_for_contact (log_manager, message, FALSE);
355 empathy_tp_chat_send (priv->tp_chat, message);
357 g_object_unref (message);
361 chat_input_text_view_send (GossipChat *chat)
363 GossipChatPriv *priv;
364 GtkTextBuffer *buffer;
365 GtkTextIter start, end;
368 priv = GET_PRIV (chat);
370 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
372 gtk_text_buffer_get_bounds (buffer, &start, &end);
373 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
375 /* clear the input field */
376 gtk_text_buffer_set_text (buffer, "", -1);
378 chat_send (chat, msg);
382 chat->is_first_char = TRUE;
386 chat_message_received_cb (EmpathyTpChat *tp_chat,
387 GossipMessage *message,
390 GossipChatPriv *priv;
391 //GossipLogManager *log_manager;
392 GossipContact *sender;
394 priv = GET_PRIV (chat);
396 sender = gossip_message_get_sender (message);
397 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
398 gossip_contact_get_name (sender));
401 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
402 gossip_log_message_for_contact (log_manager, message, TRUE);
404 gossip_chat_view_append_message (chat->view, message);
406 if (gossip_chat_should_play_sound (chat)) {
407 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
410 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
414 chat_sent_message_add (GossipChat *chat,
417 GossipChatPriv *priv;
421 priv = GET_PRIV (chat);
423 /* Save the sent message in our repeat buffer */
424 list = priv->sent_messages;
426 /* Remove any other occurances of this msg */
427 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
428 list = g_slist_remove_link (list, item);
430 g_slist_free1 (item);
433 /* Trim the list to the last 10 items */
434 while (g_slist_length (list) > 10) {
435 item = g_slist_last (list);
437 list = g_slist_remove_link (list, item);
439 g_slist_free1 (item);
443 /* Add new message */
444 list = g_slist_prepend (list, g_strdup (str));
446 /* Set list and reset the index */
447 priv->sent_messages = list;
448 priv->sent_messages_index = -1;
452 chat_sent_message_get_next (GossipChat *chat)
454 GossipChatPriv *priv;
457 priv = GET_PRIV (chat);
459 if (!priv->sent_messages) {
460 gossip_debug (DEBUG_DOMAIN,
461 "No sent messages, next message is NULL");
465 max = g_slist_length (priv->sent_messages) - 1;
467 if (priv->sent_messages_index < max) {
468 priv->sent_messages_index++;
471 gossip_debug (DEBUG_DOMAIN,
472 "Returning next message index:%d",
473 priv->sent_messages_index);
475 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
479 chat_sent_message_get_last (GossipChat *chat)
481 GossipChatPriv *priv;
483 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
485 priv = GET_PRIV (chat);
487 if (!priv->sent_messages) {
488 gossip_debug (DEBUG_DOMAIN,
489 "No sent messages, last message is NULL");
493 if (priv->sent_messages_index >= 0) {
494 priv->sent_messages_index--;
497 gossip_debug (DEBUG_DOMAIN,
498 "Returning last message index:%d",
499 priv->sent_messages_index);
501 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
505 chat_input_key_press_event_cb (GtkWidget *widget,
509 GossipChatPriv *priv;
512 GtkWidget *text_view_sw;
514 priv = GET_PRIV (chat);
516 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
520 /* Catch ctrl+up/down so we can traverse messages we sent */
521 if ((event->state & GDK_CONTROL_MASK) &&
522 (event->keyval == GDK_Up ||
523 event->keyval == GDK_Down)) {
524 GtkTextBuffer *buffer;
527 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
529 if (event->keyval == GDK_Up) {
530 str = chat_sent_message_get_next (chat);
532 str = chat_sent_message_get_last (chat);
535 g_signal_handlers_block_by_func (buffer,
536 chat_input_text_buffer_changed_cb,
538 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
539 g_signal_handlers_unblock_by_func (buffer,
540 chat_input_text_buffer_changed_cb,
546 /* Catch enter but not ctrl/shift-enter */
547 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
550 /* This is to make sure that kinput2 gets the enter. And if
551 * it's handled there we shouldn't send on it. This is because
552 * kinput2 uses Enter to commit letters. See:
553 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
556 view = GTK_TEXT_VIEW (chat->input_text_view);
557 if (gtk_im_context_filter_keypress (view->im_context, event)) {
558 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
562 chat_input_text_view_send (chat);
566 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
568 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
569 /* Newline for shift-enter. */
572 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
573 event->keyval == GDK_Page_Up) {
574 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
575 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
579 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
580 event->keyval == GDK_Page_Down) {
581 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
582 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
583 gtk_adjustment_set_value (adj, val);
592 chat_text_view_focus_in_event_cb (GtkWidget *widget,
596 gtk_widget_grab_focus (chat->input_text_view);
602 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
605 GossipChatPriv *priv;
606 GtkTextIter start, end;
608 gboolean spell_checker = FALSE;
610 priv = GET_PRIV (chat);
612 if (gtk_text_buffer_get_char_count (buffer) == 0) {
613 chat_composing_stop (chat);
615 chat_composing_start (chat);
618 gossip_conf_get_bool (gossip_conf_get (),
619 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
622 if (chat->is_first_char) {
626 GtkAllocation *allocation;
628 /* Save the window's size */
629 dialog = gossip_chat_window_get_dialog (priv->window);
630 gtk_window_get_size (GTK_WINDOW (dialog),
631 NULL, &window_height);
633 gtk_widget_size_request (chat->input_text_view, &req);
635 allocation = >K_WIDGET (chat->view)->allocation;
637 priv->default_window_height = window_height;
638 priv->last_input_height = req.height;
639 priv->padding_height = window_height - req.height - allocation->height;
641 chat->is_first_char = FALSE;
644 gtk_text_buffer_get_start_iter (buffer, &start);
646 if (!spell_checker) {
647 gtk_text_buffer_get_end_iter (buffer, &end);
648 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
652 if (!gossip_spell_supported ()) {
656 /* NOTE: this is really inefficient, we shouldn't have to
657 reiterate the whole buffer each time and check each work
660 gboolean correct = FALSE;
663 if (gtk_text_iter_is_start (&start)) {
666 if (!gtk_text_iter_forward_word_end (&end)) {
667 /* no whole word yet */
671 if (!gtk_text_iter_forward_word_end (&end)) {
672 /* must be the end of the buffer */
677 gtk_text_iter_backward_word_start (&start);
680 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
682 /* spell check string */
683 if (!gossip_chat_get_is_command (str)) {
684 correct = gossip_spell_check (str);
690 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
692 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
697 /* set start iter to the end iters position */
709 chat_change_size_in_idle_cb (ChangeSizeData *data)
711 gtk_window_resize (GTK_WINDOW (data->window),
712 data->width, data->height);
718 chat_text_view_scroll_hide_cb (GtkWidget *widget,
721 GossipChatPriv *priv;
724 priv = GET_PRIV (chat);
726 priv->vscroll_visible = FALSE;
727 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
729 sw = gtk_widget_get_parent (chat->input_text_view);
730 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
733 g_object_set (sw, "height-request", -1, NULL);
737 chat_text_view_size_allocate_cb (GtkWidget *widget,
738 GtkAllocation *allocation,
741 GossipChatPriv *priv;
744 ChangeSizeData *data;
747 GtkAllocation *view_allocation;
752 priv = GET_PRIV (chat);
754 if (priv->default_window_height <= 0) {
758 sw = gtk_widget_get_parent (widget);
759 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
762 priv->vscroll_visible = TRUE;
763 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
764 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
766 GTK_POLICY_AUTOMATIC);
767 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
768 g_signal_connect (vscroll, "hide",
769 G_CALLBACK (chat_text_view_scroll_hide_cb),
773 if (priv->last_input_height <= allocation->height) {
774 priv->last_input_height = allocation->height;
778 diff = priv->last_input_height - allocation->height;
779 priv->last_input_height = allocation->height;
781 view_allocation = >K_WIDGET (chat->view)->allocation;
783 dialog = gossip_chat_window_get_dialog (priv->window);
784 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
786 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
788 if (new_height <= priv->default_window_height) {
789 window_height = priv->default_window_height;
791 window_height = new_height;
794 if (current_height <= window_height) {
798 /* Restore the window's size */
799 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
801 data = g_new0 (ChangeSizeData, 1);
802 data->window = dialog;
804 data->height = window_height;
806 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
807 (GSourceFunc) chat_change_size_in_idle_cb,
812 chat_text_view_realize_cb (GtkWidget *widget,
815 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
816 gtk_widget_grab_focus (widget);
820 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
823 GtkTextBuffer *buffer;
827 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
829 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
831 gtk_text_buffer_get_end_iter (buffer, &iter);
832 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
834 gtk_text_buffer_get_end_iter (buffer, &iter);
835 gtk_text_buffer_insert (buffer, &iter, " ", -1);
839 chat_text_populate_popup_cb (GtkTextView *view,
843 GossipChatPriv *priv;
844 GtkTextBuffer *buffer;
845 GtkTextTagTable *table;
848 GtkTextIter iter, start, end;
851 GossipChatSpell *chat_spell;
852 GtkWidget *smiley_menu;
854 priv = GET_PRIV (chat);
856 /* Add the emoticon menu. */
857 item = gtk_separator_menu_item_new ();
858 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
859 gtk_widget_show (item);
861 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
862 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
863 gtk_widget_show (item);
865 smiley_menu = gossip_chat_view_get_smiley_menu (
866 G_CALLBACK (chat_insert_smiley_activate_cb),
869 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
871 /* Add the spell check menu item. */
872 buffer = gtk_text_view_get_buffer (view);
873 table = gtk_text_buffer_get_tag_table (buffer);
875 tag = gtk_text_tag_table_lookup (table, "misspelled");
877 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
879 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
880 GTK_TEXT_WINDOW_WIDGET,
884 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
888 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
889 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
891 str = gtk_text_buffer_get_text (buffer,
892 &start, &end, FALSE);
895 if (G_STR_EMPTY (str)) {
899 chat_spell = chat_spell_new (chat, str, start, end);
901 g_object_set_data_full (G_OBJECT (menu),
902 "chat_spell", chat_spell,
903 (GDestroyNotify) chat_spell_free);
905 item = gtk_separator_menu_item_new ();
906 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
907 gtk_widget_show (item);
909 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
910 g_signal_connect (item,
912 G_CALLBACK (chat_text_check_word_spelling_cb),
914 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
915 gtk_widget_show (item);
919 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
920 GossipChatSpell *chat_spell)
922 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
928 static GossipChatSpell *
929 chat_spell_new (GossipChat *chat,
934 GossipChatSpell *chat_spell;
936 chat_spell = g_new0 (GossipChatSpell, 1);
938 chat_spell->chat = g_object_ref (chat);
939 chat_spell->word = g_strdup (word);
940 chat_spell->start = start;
941 chat_spell->end = end;
947 chat_spell_free (GossipChatSpell *chat_spell)
949 g_object_unref (chat_spell->chat);
950 g_free (chat_spell->word);
955 chat_composing_start (GossipChat *chat)
957 GossipChatPriv *priv;
959 priv = GET_PRIV (chat);
961 if (priv->composing_stop_timeout_id) {
962 /* Just restart the timeout */
963 chat_composing_remove_timeout (chat);
965 empathy_tp_chat_set_state (priv->tp_chat,
966 TP_CHANNEL_CHAT_STATE_COMPOSING);
969 priv->composing_stop_timeout_id = g_timeout_add (
970 1000 * COMPOSING_STOP_TIMEOUT,
971 (GSourceFunc) chat_composing_stop_timeout_cb,
976 chat_composing_stop (GossipChat *chat)
978 GossipChatPriv *priv;
980 priv = GET_PRIV (chat);
982 chat_composing_remove_timeout (chat);
983 empathy_tp_chat_set_state (priv->tp_chat,
984 TP_CHANNEL_CHAT_STATE_ACTIVE);
988 chat_composing_remove_timeout (GossipChat *chat)
990 GossipChatPriv *priv;
992 priv = GET_PRIV (chat);
994 if (priv->composing_stop_timeout_id) {
995 g_source_remove (priv->composing_stop_timeout_id);
996 priv->composing_stop_timeout_id = 0;
1001 chat_composing_stop_timeout_cb (GossipChat *chat)
1003 GossipChatPriv *priv;
1005 priv = GET_PRIV (chat);
1007 priv->composing_stop_timeout_id = 0;
1008 empathy_tp_chat_set_state (priv->tp_chat,
1009 TP_CHANNEL_CHAT_STATE_PAUSED);
1015 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1016 GossipContact *contact,
1017 TelepathyChannelChatState state,
1020 GossipChatPriv *priv;
1022 gboolean was_composing;
1024 priv = GET_PRIV (chat);
1026 was_composing = (priv->compositors != NULL);
1028 /* Find the contact in the list. After that l is the list elem or NULL */
1029 for (l = priv->compositors; l; l = l->next) {
1030 if (gossip_contact_equal (contact, l->data)) {
1036 case TP_CHANNEL_CHAT_STATE_GONE:
1037 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1038 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1039 /* Contact is not composing */
1041 priv->compositors = g_list_remove_link (priv->compositors, l);
1042 g_object_unref (l->data);
1046 case TP_CHANNEL_CHAT_STATE_PAUSED:
1047 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1048 /* Contact is composing */
1050 priv->compositors = g_list_prepend (priv->compositors,
1051 g_object_ref (contact));
1055 g_assert_not_reached ();
1058 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1059 was_composing ? "yes" : "no",
1060 priv->compositors ? "yes" : "no");
1062 if ((was_composing && !priv->compositors) ||
1063 (!was_composing && priv->compositors)) {
1064 /* Composing state changed */
1065 g_signal_emit (chat, signals[COMPOSING], 0,
1066 (gboolean) priv->compositors);
1071 gossip_chat_get_is_command (const gchar *str)
1073 g_return_val_if_fail (str != NULL, FALSE);
1075 if (str[0] != '/') {
1079 if (g_str_has_prefix (str, "/me")) {
1082 else if (g_str_has_prefix (str, "/nick")) {
1085 else if (g_str_has_prefix (str, "/topic")) {
1093 gossip_chat_correct_word (GossipChat *chat,
1096 const gchar *new_word)
1098 GtkTextBuffer *buffer;
1100 g_return_if_fail (chat != NULL);
1101 g_return_if_fail (new_word != NULL);
1103 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1105 gtk_text_buffer_delete (buffer, &start, &end);
1106 gtk_text_buffer_insert (buffer, &start,
1112 gossip_chat_get_name (GossipChat *chat)
1114 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1116 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1117 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1124 gossip_chat_get_tooltip (GossipChat *chat)
1126 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1128 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1129 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1136 gossip_chat_get_status_icon_name (GossipChat *chat)
1138 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1140 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1141 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1148 gossip_chat_get_contact (GossipChat *chat)
1150 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1152 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1153 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1159 gossip_chat_get_own_contact (GossipChat *chat)
1161 GossipChatPriv *priv;
1163 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1165 priv = GET_PRIV (chat);
1167 return empathy_contact_manager_get_own (priv->manager, chat->account);
1171 gossip_chat_get_widget (GossipChat *chat)
1173 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1175 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1176 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1183 gossip_chat_is_group_chat (GossipChat *chat)
1185 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1187 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1188 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1195 gossip_chat_is_connected (GossipChat *chat)
1197 GossipChatPriv *priv;
1199 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1201 priv = GET_PRIV (chat);
1203 return (priv->tp_chat != NULL);
1207 gossip_chat_get_show_contacts (GossipChat *chat)
1209 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1211 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1212 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1219 gossip_chat_set_show_contacts (GossipChat *chat,
1222 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1224 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1225 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1230 gossip_chat_save_geometry (GossipChat *chat,
1236 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1240 gossip_chat_load_geometry (GossipChat *chat,
1246 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1250 gossip_chat_set_tp_chat (GossipChat *chat,
1251 EmpathyTpChat *tp_chat)
1253 GossipChatPriv *priv;
1256 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1257 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1259 priv = GET_PRIV (chat);
1261 if (tp_chat == priv->tp_chat) {
1265 if (priv->tp_chat) {
1266 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1267 chat_message_received_cb,
1269 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1272 g_object_unref (priv->tp_chat);
1276 priv->tp_chat = g_object_ref (tp_chat);
1277 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1279 g_signal_connect (tp_chat, "message-received",
1280 G_CALLBACK (chat_message_received_cb),
1282 g_signal_connect (tp_chat, "chat-state-changed",
1283 G_CALLBACK (chat_state_changed_cb),
1285 g_signal_connect (tp_chat, "destroy",
1286 G_CALLBACK (chat_destroy_cb),
1289 empathy_tp_chat_request_pending (tp_chat);
1291 if (!priv->sensitive) {
1292 widget = gossip_chat_get_widget (chat);
1293 gtk_widget_set_sensitive (widget, TRUE);
1294 gossip_chat_view_append_event (chat->view, _("Connected"));
1295 priv->sensitive = TRUE;
1300 gossip_chat_get_id (GossipChat *chat)
1302 GossipChatPriv *priv;
1304 priv = GET_PRIV (chat);
1310 gossip_chat_clear (GossipChat *chat)
1312 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1314 gossip_chat_view_clear (chat->view);
1318 gossip_chat_set_window (GossipChat *chat,
1319 GossipChatWindow *window)
1321 GossipChatPriv *priv;
1323 priv = GET_PRIV (chat);
1324 priv->window = window;
1328 gossip_chat_get_window (GossipChat *chat)
1330 GossipChatPriv *priv;
1332 priv = GET_PRIV (chat);
1334 return priv->window;
1338 gossip_chat_scroll_down (GossipChat *chat)
1340 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1342 gossip_chat_view_scroll_down (chat->view);
1346 gossip_chat_cut (GossipChat *chat)
1348 GtkTextBuffer *buffer;
1350 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1352 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1353 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1354 GtkClipboard *clipboard;
1356 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1358 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1363 gossip_chat_copy (GossipChat *chat)
1365 GtkTextBuffer *buffer;
1367 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1369 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1370 gossip_chat_view_copy_clipboard (chat->view);
1374 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1375 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1376 GtkClipboard *clipboard;
1378 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1380 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1385 gossip_chat_paste (GossipChat *chat)
1387 GtkTextBuffer *buffer;
1388 GtkClipboard *clipboard;
1390 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1392 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1393 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1395 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1399 gossip_chat_present (GossipChat *chat)
1401 GossipChatPriv *priv;
1403 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1405 priv = GET_PRIV (chat);
1407 if (priv->window == NULL) {
1408 GossipChatWindow *window;
1410 window = gossip_chat_window_get_default ();
1412 window = gossip_chat_window_new ();
1415 gossip_chat_window_add_chat (window, chat);
1418 gossip_chat_window_switch_to_chat (priv->window, chat);
1419 gossip_window_present (
1420 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1423 gtk_widget_grab_focus (chat->input_text_view);
1427 gossip_chat_should_play_sound (GossipChat *chat)
1429 GossipChatWindow *window;
1430 gboolean play = TRUE;
1432 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1434 window = gossip_chat_get_window (chat);
1439 play = !gossip_chat_window_has_focus (window);
1445 gossip_chat_should_highlight_nick (GossipMessage *message)
1447 GossipContact *my_contact;
1448 const gchar *msg, *to;
1449 gchar *cf_msg, *cf_to;
1453 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1455 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1459 msg = gossip_message_get_body (message);
1464 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1465 to = gossip_contact_get_name (my_contact);
1470 cf_msg = g_utf8_casefold (msg, -1);
1471 cf_to = g_utf8_casefold (to, -1);
1473 ch = strstr (cf_msg, cf_to);
1479 /* Not first in the message */
1480 if ((*(ch - 1) != ' ') &&
1481 (*(ch - 1) != ',') &&
1482 (*(ch - 1) != '.')) {
1487 ch = ch + strlen (cf_to);
1488 if (ch >= cf_msg + strlen (cf_msg)) {