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;
70 GtkTooltips *tooltips;
71 guint composing_stop_timeout_id;
74 GSList *sent_messages;
75 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 chat_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;
163 chat_signals[COMPOSING] =
164 g_signal_new ("composing",
165 G_OBJECT_CLASS_TYPE (object_class),
169 g_cclosure_marshal_VOID__BOOLEAN,
173 chat_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 chat_signals[NAME_CHANGED] =
184 g_signal_new ("name-changed",
185 G_OBJECT_CLASS_TYPE (object_class),
189 g_cclosure_marshal_VOID__POINTER,
193 chat_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 chat_composing_remove_timeout (chat);
288 g_object_unref (chat->account);
289 g_object_unref (priv->manager);
292 g_object_unref (priv->tp_chat);
297 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
301 chat_destroy_cb (EmpathyTpChat *tp_chat,
304 GossipChatPriv *priv;
307 priv = GET_PRIV (chat);
310 g_object_unref (priv->tp_chat);
311 priv->tp_chat = NULL;
314 gossip_chat_view_append_event (chat->view, _("Disconnected"));
316 widget = gossip_chat_get_widget (chat);
317 gtk_widget_set_sensitive (widget, FALSE);
318 priv->sensitive = FALSE;
322 chat_send (GossipChat *chat,
325 GossipChatPriv *priv;
326 //GossipLogManager *log_manager;
327 GossipMessage *message;
328 GossipContact *own_contact;
330 priv = GET_PRIV (chat);
332 if (G_STR_EMPTY (msg)) {
336 chat_sent_message_add (chat, msg);
338 if (g_str_has_prefix (msg, "/clear")) {
339 gossip_chat_view_clear (chat->view);
343 /* FIXME: gossip_app_set_not_away ();*/
345 own_contact = gossip_chat_get_own_contact (chat);
346 message = gossip_message_new (msg);
347 gossip_message_set_sender (message, own_contact);
349 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
350 //gossip_log_message_for_contact (log_manager, message, FALSE);
352 empathy_tp_chat_send (priv->tp_chat, message);
354 g_object_unref (message);
358 chat_input_text_view_send (GossipChat *chat)
360 GossipChatPriv *priv;
361 GtkTextBuffer *buffer;
362 GtkTextIter start, end;
365 priv = GET_PRIV (chat);
367 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
369 gtk_text_buffer_get_bounds (buffer, &start, &end);
370 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
372 /* clear the input field */
373 gtk_text_buffer_set_text (buffer, "", -1);
375 chat_send (chat, msg);
379 chat->is_first_char = TRUE;
383 chat_message_received_cb (EmpathyTpChat *tp_chat,
384 GossipMessage *message,
387 GossipChatPriv *priv;
388 //GossipLogManager *log_manager;
389 GossipContact *sender;
391 priv = GET_PRIV (chat);
393 sender = gossip_message_get_sender (message);
394 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
395 gossip_contact_get_name (sender));
398 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
399 gossip_log_message_for_contact (log_manager, message, TRUE);
401 gossip_chat_view_append_message (chat->view, message);
403 if (gossip_chat_should_play_sound (chat)) {
404 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
407 g_signal_emit_by_name (chat, "new-message", message, FALSE);
411 chat_sent_message_add (GossipChat *chat,
414 GossipChatPriv *priv;
418 priv = GET_PRIV (chat);
420 /* Save the sent message in our repeat buffer */
421 list = priv->sent_messages;
423 /* Remove any other occurances of this msg */
424 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
425 list = g_slist_remove_link (list, item);
427 g_slist_free1 (item);
430 /* Trim the list to the last 10 items */
431 while (g_slist_length (list) > 10) {
432 item = g_slist_last (list);
434 list = g_slist_remove_link (list, item);
436 g_slist_free1 (item);
440 /* Add new message */
441 list = g_slist_prepend (list, g_strdup (str));
443 /* Set list and reset the index */
444 priv->sent_messages = list;
445 priv->sent_messages_index = -1;
449 chat_sent_message_get_next (GossipChat *chat)
451 GossipChatPriv *priv;
454 priv = GET_PRIV (chat);
456 if (!priv->sent_messages) {
457 gossip_debug (DEBUG_DOMAIN,
458 "No sent messages, next message is NULL");
462 max = g_slist_length (priv->sent_messages) - 1;
464 if (priv->sent_messages_index < max) {
465 priv->sent_messages_index++;
468 gossip_debug (DEBUG_DOMAIN,
469 "Returning next message index:%d",
470 priv->sent_messages_index);
472 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
476 chat_sent_message_get_last (GossipChat *chat)
478 GossipChatPriv *priv;
480 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
482 priv = GET_PRIV (chat);
484 if (!priv->sent_messages) {
485 gossip_debug (DEBUG_DOMAIN,
486 "No sent messages, last message is NULL");
490 if (priv->sent_messages_index >= 0) {
491 priv->sent_messages_index--;
494 gossip_debug (DEBUG_DOMAIN,
495 "Returning last message index:%d",
496 priv->sent_messages_index);
498 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
502 chat_input_key_press_event_cb (GtkWidget *widget,
506 GossipChatPriv *priv;
509 GtkWidget *text_view_sw;
511 priv = GET_PRIV (chat);
513 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
517 /* Catch ctrl+up/down so we can traverse messages we sent */
518 if ((event->state & GDK_CONTROL_MASK) &&
519 (event->keyval == GDK_Up ||
520 event->keyval == GDK_Down)) {
521 GtkTextBuffer *buffer;
524 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
526 if (event->keyval == GDK_Up) {
527 str = chat_sent_message_get_next (chat);
529 str = chat_sent_message_get_last (chat);
532 g_signal_handlers_block_by_func (buffer,
533 chat_input_text_buffer_changed_cb,
535 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
536 g_signal_handlers_unblock_by_func (buffer,
537 chat_input_text_buffer_changed_cb,
543 /* Catch enter but not ctrl/shift-enter */
544 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
547 /* This is to make sure that kinput2 gets the enter. And if
548 * it's handled there we shouldn't send on it. This is because
549 * kinput2 uses Enter to commit letters. See:
550 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
553 view = GTK_TEXT_VIEW (chat->input_text_view);
554 if (gtk_im_context_filter_keypress (view->im_context, event)) {
555 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
559 chat_input_text_view_send (chat);
563 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
565 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
566 /* Newline for shift-enter. */
569 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
570 event->keyval == GDK_Page_Up) {
571 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
572 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
576 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
577 event->keyval == GDK_Page_Down) {
578 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
579 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
580 gtk_adjustment_set_value (adj, val);
589 chat_text_view_focus_in_event_cb (GtkWidget *widget,
593 gtk_widget_grab_focus (chat->input_text_view);
599 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
602 GossipChatPriv *priv;
603 GtkTextIter start, end;
605 gboolean spell_checker = FALSE;
607 priv = GET_PRIV (chat);
609 if (gtk_text_buffer_get_char_count (buffer) == 0) {
610 chat_composing_stop (chat);
612 chat_composing_start (chat);
615 gossip_conf_get_bool (gossip_conf_get (),
616 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
619 if (chat->is_first_char) {
623 GtkAllocation *allocation;
625 /* Save the window's size */
626 dialog = gossip_chat_window_get_dialog (priv->window);
627 gtk_window_get_size (GTK_WINDOW (dialog),
628 NULL, &window_height);
630 gtk_widget_size_request (chat->input_text_view, &req);
632 allocation = >K_WIDGET (chat->view)->allocation;
634 priv->default_window_height = window_height;
635 priv->last_input_height = req.height;
636 priv->padding_height = window_height - req.height - allocation->height;
638 chat->is_first_char = FALSE;
641 gtk_text_buffer_get_start_iter (buffer, &start);
643 if (!spell_checker) {
644 gtk_text_buffer_get_end_iter (buffer, &end);
645 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
649 if (!gossip_spell_supported ()) {
653 /* NOTE: this is really inefficient, we shouldn't have to
654 reiterate the whole buffer each time and check each work
657 gboolean correct = FALSE;
660 if (gtk_text_iter_is_start (&start)) {
663 if (!gtk_text_iter_forward_word_end (&end)) {
664 /* no whole word yet */
668 if (!gtk_text_iter_forward_word_end (&end)) {
669 /* must be the end of the buffer */
674 gtk_text_iter_backward_word_start (&start);
677 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
679 /* spell check string */
680 if (!gossip_chat_get_is_command (str)) {
681 correct = gossip_spell_check (str);
687 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
689 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
694 /* set start iter to the end iters position */
706 chat_change_size_in_idle_cb (ChangeSizeData *data)
708 gtk_window_resize (GTK_WINDOW (data->window),
709 data->width, data->height);
715 chat_text_view_scroll_hide_cb (GtkWidget *widget,
718 GossipChatPriv *priv;
721 priv = GET_PRIV (chat);
723 priv->vscroll_visible = FALSE;
724 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
726 sw = gtk_widget_get_parent (chat->input_text_view);
727 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
730 g_object_set (sw, "height-request", -1, NULL);
734 chat_text_view_size_allocate_cb (GtkWidget *widget,
735 GtkAllocation *allocation,
738 GossipChatPriv *priv;
741 ChangeSizeData *data;
744 GtkAllocation *view_allocation;
749 priv = GET_PRIV (chat);
751 if (priv->default_window_height <= 0) {
755 sw = gtk_widget_get_parent (widget);
756 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
759 priv->vscroll_visible = TRUE;
760 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
761 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
763 GTK_POLICY_AUTOMATIC);
764 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
765 g_signal_connect (vscroll, "hide",
766 G_CALLBACK (chat_text_view_scroll_hide_cb),
770 if (priv->last_input_height <= allocation->height) {
771 priv->last_input_height = allocation->height;
775 diff = priv->last_input_height - allocation->height;
776 priv->last_input_height = allocation->height;
778 view_allocation = >K_WIDGET (chat->view)->allocation;
780 dialog = gossip_chat_window_get_dialog (priv->window);
781 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
783 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
785 if (new_height <= priv->default_window_height) {
786 window_height = priv->default_window_height;
788 window_height = new_height;
791 if (current_height <= window_height) {
795 /* Restore the window's size */
796 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
798 data = g_new0 (ChangeSizeData, 1);
799 data->window = dialog;
801 data->height = window_height;
803 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
804 (GSourceFunc) chat_change_size_in_idle_cb,
809 chat_text_view_realize_cb (GtkWidget *widget,
812 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
813 gtk_widget_grab_focus (widget);
817 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
820 GtkTextBuffer *buffer;
824 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
826 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
828 gtk_text_buffer_get_end_iter (buffer, &iter);
829 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
831 gtk_text_buffer_get_end_iter (buffer, &iter);
832 gtk_text_buffer_insert (buffer, &iter, " ", -1);
836 chat_text_populate_popup_cb (GtkTextView *view,
840 GossipChatPriv *priv;
841 GtkTextBuffer *buffer;
842 GtkTextTagTable *table;
845 GtkTextIter iter, start, end;
848 GossipChatSpell *chat_spell;
849 GtkWidget *smiley_menu;
851 priv = GET_PRIV (chat);
853 /* Add the emoticon menu. */
854 item = gtk_separator_menu_item_new ();
855 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
856 gtk_widget_show (item);
858 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
859 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
860 gtk_widget_show (item);
862 smiley_menu = gossip_chat_view_get_smiley_menu (
863 G_CALLBACK (chat_insert_smiley_activate_cb),
866 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
868 /* Add the spell check menu item. */
869 buffer = gtk_text_view_get_buffer (view);
870 table = gtk_text_buffer_get_tag_table (buffer);
872 tag = gtk_text_tag_table_lookup (table, "misspelled");
874 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
876 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
877 GTK_TEXT_WINDOW_WIDGET,
881 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
885 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
886 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
888 str = gtk_text_buffer_get_text (buffer,
889 &start, &end, FALSE);
892 if (G_STR_EMPTY (str)) {
896 chat_spell = chat_spell_new (chat, str, start, end);
898 g_object_set_data_full (G_OBJECT (menu),
899 "chat_spell", chat_spell,
900 (GDestroyNotify) chat_spell_free);
902 item = gtk_separator_menu_item_new ();
903 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
904 gtk_widget_show (item);
906 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
907 g_signal_connect (item,
909 G_CALLBACK (chat_text_check_word_spelling_cb),
911 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
912 gtk_widget_show (item);
916 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
917 GossipChatSpell *chat_spell)
919 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
925 static GossipChatSpell *
926 chat_spell_new (GossipChat *chat,
931 GossipChatSpell *chat_spell;
933 chat_spell = g_new0 (GossipChatSpell, 1);
935 chat_spell->chat = g_object_ref (chat);
936 chat_spell->word = g_strdup (word);
937 chat_spell->start = start;
938 chat_spell->end = end;
944 chat_spell_free (GossipChatSpell *chat_spell)
946 g_object_unref (chat_spell->chat);
947 g_free (chat_spell->word);
952 chat_composing_start (GossipChat *chat)
954 GossipChatPriv *priv;
956 priv = GET_PRIV (chat);
958 if (priv->composing_stop_timeout_id) {
959 /* Just restart the timeout */
960 chat_composing_remove_timeout (chat);
962 empathy_tp_chat_set_state (priv->tp_chat,
963 TP_CHANNEL_CHAT_STATE_COMPOSING);
966 priv->composing_stop_timeout_id = g_timeout_add (
967 1000 * COMPOSING_STOP_TIMEOUT,
968 (GSourceFunc) chat_composing_stop_timeout_cb,
973 chat_composing_stop (GossipChat *chat)
975 GossipChatPriv *priv;
977 priv = GET_PRIV (chat);
979 chat_composing_remove_timeout (chat);
980 empathy_tp_chat_set_state (priv->tp_chat,
981 TP_CHANNEL_CHAT_STATE_ACTIVE);
985 chat_composing_remove_timeout (GossipChat *chat)
987 GossipChatPriv *priv;
989 priv = GET_PRIV (chat);
991 if (priv->composing_stop_timeout_id) {
992 g_source_remove (priv->composing_stop_timeout_id);
993 priv->composing_stop_timeout_id = 0;
998 chat_composing_stop_timeout_cb (GossipChat *chat)
1000 GossipChatPriv *priv;
1002 priv = GET_PRIV (chat);
1004 priv->composing_stop_timeout_id = 0;
1005 empathy_tp_chat_set_state (priv->tp_chat,
1006 TP_CHANNEL_CHAT_STATE_PAUSED);
1012 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1013 GossipContact *contact,
1014 TelepathyChannelChatState state,
1017 /* FIXME: not yet implemented */
1021 gossip_chat_get_is_command (const gchar *str)
1023 g_return_val_if_fail (str != NULL, FALSE);
1025 if (str[0] != '/') {
1029 if (g_str_has_prefix (str, "/me")) {
1032 else if (g_str_has_prefix (str, "/nick")) {
1035 else if (g_str_has_prefix (str, "/topic")) {
1043 gossip_chat_correct_word (GossipChat *chat,
1046 const gchar *new_word)
1048 GtkTextBuffer *buffer;
1050 g_return_if_fail (chat != NULL);
1051 g_return_if_fail (new_word != NULL);
1053 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1055 gtk_text_buffer_delete (buffer, &start, &end);
1056 gtk_text_buffer_insert (buffer, &start,
1062 gossip_chat_get_name (GossipChat *chat)
1064 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1066 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1067 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1074 gossip_chat_get_tooltip (GossipChat *chat)
1076 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1078 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1079 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1086 gossip_chat_get_status_icon_name (GossipChat *chat)
1088 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1090 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1091 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1098 gossip_chat_get_contact (GossipChat *chat)
1100 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1102 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1103 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1109 gossip_chat_get_own_contact (GossipChat *chat)
1111 GossipChatPriv *priv;
1113 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1115 priv = GET_PRIV (chat);
1117 return empathy_contact_manager_get_own (priv->manager, chat->account);
1121 gossip_chat_get_widget (GossipChat *chat)
1123 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1125 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1126 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1133 gossip_chat_is_group_chat (GossipChat *chat)
1135 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1137 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1138 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1145 gossip_chat_is_connected (GossipChat *chat)
1147 GossipChatPriv *priv;
1149 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1151 priv = GET_PRIV (chat);
1153 return (priv->tp_chat != NULL);
1157 gossip_chat_get_show_contacts (GossipChat *chat)
1159 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1161 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1162 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1169 gossip_chat_set_show_contacts (GossipChat *chat,
1172 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1174 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1175 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1180 gossip_chat_save_geometry (GossipChat *chat,
1186 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1190 gossip_chat_load_geometry (GossipChat *chat,
1196 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1200 gossip_chat_set_tp_chat (GossipChat *chat,
1201 EmpathyTpChat *tp_chat)
1203 GossipChatPriv *priv;
1206 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1207 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1209 priv = GET_PRIV (chat);
1211 if (tp_chat == priv->tp_chat) {
1215 if (priv->tp_chat) {
1216 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1217 chat_message_received_cb,
1219 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1222 g_object_unref (priv->tp_chat);
1226 priv->tp_chat = g_object_ref (tp_chat);
1227 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1229 g_signal_connect (tp_chat, "message-received",
1230 G_CALLBACK (chat_message_received_cb),
1232 g_signal_connect (tp_chat, "chat-state-changed",
1233 G_CALLBACK (chat_state_changed_cb),
1235 g_signal_connect (tp_chat, "destroy",
1236 G_CALLBACK (chat_destroy_cb),
1239 empathy_tp_chat_request_pending (tp_chat);
1241 if (!priv->sensitive) {
1242 widget = gossip_chat_get_widget (chat);
1243 gtk_widget_set_sensitive (widget, TRUE);
1244 gossip_chat_view_append_event (chat->view, _("Connected"));
1245 priv->sensitive = TRUE;
1250 gossip_chat_get_id (GossipChat *chat)
1252 GossipChatPriv *priv;
1254 priv = GET_PRIV (chat);
1260 gossip_chat_clear (GossipChat *chat)
1262 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1264 gossip_chat_view_clear (chat->view);
1268 gossip_chat_set_window (GossipChat *chat,
1269 GossipChatWindow *window)
1271 GossipChatPriv *priv;
1273 priv = GET_PRIV (chat);
1274 priv->window = window;
1278 gossip_chat_get_window (GossipChat *chat)
1280 GossipChatPriv *priv;
1282 priv = GET_PRIV (chat);
1284 return priv->window;
1288 gossip_chat_scroll_down (GossipChat *chat)
1290 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1292 gossip_chat_view_scroll_down (chat->view);
1296 gossip_chat_cut (GossipChat *chat)
1298 GtkTextBuffer *buffer;
1300 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1302 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1303 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1304 GtkClipboard *clipboard;
1306 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1308 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1313 gossip_chat_copy (GossipChat *chat)
1315 GtkTextBuffer *buffer;
1317 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1319 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1320 gossip_chat_view_copy_clipboard (chat->view);
1324 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1325 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1326 GtkClipboard *clipboard;
1328 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1330 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1335 gossip_chat_paste (GossipChat *chat)
1337 GtkTextBuffer *buffer;
1338 GtkClipboard *clipboard;
1340 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1342 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1343 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1345 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1349 gossip_chat_present (GossipChat *chat)
1351 GossipChatPriv *priv;
1353 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1355 priv = GET_PRIV (chat);
1357 if (priv->window == NULL) {
1358 GossipChatWindow *window;
1360 window = gossip_chat_window_get_default ();
1362 window = gossip_chat_window_new ();
1365 gossip_chat_window_add_chat (window, chat);
1368 gossip_chat_window_switch_to_chat (priv->window, chat);
1369 gossip_window_present (
1370 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1373 gtk_widget_grab_focus (chat->input_text_view);
1377 gossip_chat_should_play_sound (GossipChat *chat)
1379 GossipChatWindow *window;
1380 gboolean play = TRUE;
1382 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1384 window = gossip_chat_get_window (chat);
1389 play = !gossip_chat_window_has_focus (window);
1395 gossip_chat_should_highlight_nick (GossipMessage *message)
1397 GossipContact *my_contact;
1398 const gchar *msg, *to;
1399 gchar *cf_msg, *cf_to;
1403 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1405 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1409 msg = gossip_message_get_body (message);
1414 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1415 to = gossip_contact_get_name (my_contact);
1420 cf_msg = g_utf8_casefold (msg, -1);
1421 cf_to = g_utf8_casefold (to, -1);
1423 ch = strstr (cf_msg, cf_to);
1429 /* Not first in the message */
1430 if ((*(ch - 1) != ' ') &&
1431 (*(ch - 1) != ',') &&
1432 (*(ch - 1) != '.')) {
1437 ch = ch + strlen (cf_to);
1438 if (ch >= cf_msg + strlen (cf_msg)) {