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>
43 #include "gossip-chat.h"
44 #include "gossip-chat-window.h"
45 #include "gossip-geometry.h"
46 #include "gossip-preferences.h"
47 #include "gossip-spell.h"
48 //#include "gossip-spell-dialog.h"
49 #include "gossip-ui-utils.h"
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
53 #define DEBUG_DOMAIN "Chat"
55 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
56 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
58 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
60 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
64 struct _GossipChatPriv {
65 EmpathyContactManager *manager;
66 EmpathyTpChat *tp_chat;
67 GossipChatWindow *window;
69 GtkTooltips *tooltips;
70 guint composing_stop_timeout_id;
73 GSList *sent_messages;
74 gint sent_messages_index;
75 /* Used to automatically shrink a window that has temporarily
76 * grown due to long input.
79 gint default_window_height;
80 gint last_input_height;
81 gboolean vscroll_visible;
92 static void gossip_chat_class_init (GossipChatClass *klass);
93 static void gossip_chat_init (GossipChat *chat);
94 static void chat_finalize (GObject *object);
95 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
97 static void chat_send (GossipChat *chat,
99 static void chat_input_text_view_send (GossipChat *chat);
100 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
101 GossipMessage *message,
103 void chat_sent_message_add (GossipChat *chat,
105 const gchar * chat_sent_message_get_next (GossipChat *chat);
106 const gchar * chat_sent_message_get_last (GossipChat *chat);
107 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
110 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
112 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
115 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
117 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
118 GtkAllocation *allocation,
120 static void chat_text_view_realize_cb (GtkWidget *widget,
122 static void chat_text_populate_popup_cb (GtkTextView *view,
125 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
126 GossipChatSpell *chat_spell);
127 static GossipChatSpell *chat_spell_new (GossipChat *chat,
131 static void chat_spell_free (GossipChatSpell *chat_spell);
132 static void chat_composing_start (GossipChat *chat);
133 static void chat_composing_stop (GossipChat *chat);
134 static void chat_composing_remove_timeout (GossipChat *chat);
135 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
136 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
137 GossipContact *contact,
138 TelepathyChannelChatState state,
149 static guint chat_signals[LAST_SIGNAL] = { 0 };
151 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
154 gossip_chat_class_init (GossipChatClass *klass)
156 GObjectClass *object_class;
158 object_class = G_OBJECT_CLASS (klass);
160 object_class->finalize = chat_finalize;
162 chat_signals[COMPOSING] =
163 g_signal_new ("composing",
164 G_OBJECT_CLASS_TYPE (object_class),
168 g_cclosure_marshal_VOID__BOOLEAN,
172 chat_signals[NEW_MESSAGE] =
173 g_signal_new ("new-message",
174 G_OBJECT_CLASS_TYPE (object_class),
178 g_cclosure_marshal_VOID__OBJECT,
180 1, GOSSIP_TYPE_MESSAGE);
182 chat_signals[NAME_CHANGED] =
183 g_signal_new ("name-changed",
184 G_OBJECT_CLASS_TYPE (object_class),
188 g_cclosure_marshal_VOID__POINTER,
192 chat_signals[STATUS_CHANGED] =
193 g_signal_new ("status-changed",
194 G_OBJECT_CLASS_TYPE (object_class),
198 g_cclosure_marshal_VOID__VOID,
202 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
206 gossip_chat_init (GossipChat *chat)
208 GossipChatPriv *priv;
209 GtkTextBuffer *buffer;
211 chat->view = gossip_chat_view_new ();
212 chat->input_text_view = gtk_text_view_new ();
214 chat->is_first_char = TRUE;
216 g_object_set (chat->input_text_view,
217 "pixels-above-lines", 2,
218 "pixels-below-lines", 2,
219 "pixels-inside-wrap", 1,
222 "wrap-mode", GTK_WRAP_WORD_CHAR,
225 priv = GET_PRIV (chat);
227 priv->manager = empathy_contact_manager_new ();
228 priv->tooltips = gtk_tooltips_new ();
229 priv->default_window_height = -1;
230 priv->vscroll_visible = FALSE;
231 priv->sensitive = TRUE;
232 priv->sent_messages = NULL;
233 priv->sent_messages_index = -1;
235 g_signal_connect (chat->input_text_view,
237 G_CALLBACK (chat_input_key_press_event_cb),
240 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
241 g_signal_connect (buffer,
243 G_CALLBACK (chat_input_text_buffer_changed_cb),
245 g_signal_connect (chat->view,
247 G_CALLBACK (chat_text_view_focus_in_event_cb),
250 g_signal_connect (chat->input_text_view,
252 G_CALLBACK (chat_text_view_size_allocate_cb),
255 g_signal_connect (chat->input_text_view,
257 G_CALLBACK (chat_text_view_realize_cb),
260 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
262 G_CALLBACK (chat_text_populate_popup_cb),
265 /* create misspelt words identification tag */
266 gtk_text_buffer_create_tag (buffer,
268 "underline", PANGO_UNDERLINE_ERROR,
273 chat_finalize (GObject *object)
276 GossipChatPriv *priv;
278 chat = GOSSIP_CHAT (object);
279 priv = GET_PRIV (chat);
281 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
283 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
284 g_slist_free (priv->sent_messages);
286 chat_composing_remove_timeout (chat);
287 g_object_unref (chat->account);
288 g_object_unref (priv->manager);
291 g_object_unref (priv->tp_chat);
296 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
300 chat_destroy_cb (EmpathyTpChat *tp_chat,
303 GossipChatPriv *priv;
306 priv = GET_PRIV (chat);
309 g_object_unref (priv->tp_chat);
310 priv->tp_chat = NULL;
313 gossip_chat_view_append_event (chat->view, _("Disconnected"));
315 widget = gossip_chat_get_widget (chat);
316 gtk_widget_set_sensitive (widget, FALSE);
317 priv->sensitive = FALSE;
321 chat_send (GossipChat *chat,
324 GossipChatPriv *priv;
325 //GossipLogManager *log_manager;
326 GossipMessage *message;
327 GossipContact *own_contact;
329 priv = GET_PRIV (chat);
331 if (G_STR_EMPTY (msg)) {
335 chat_sent_message_add (chat, msg);
337 if (g_str_has_prefix (msg, "/clear")) {
338 gossip_chat_view_clear (chat->view);
342 /* FIXME: gossip_app_set_not_away ();*/
344 own_contact = gossip_chat_get_own_contact (chat);
345 message = gossip_message_new (msg);
346 gossip_message_set_sender (message, own_contact);
348 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
349 //gossip_log_message_for_contact (log_manager, message, FALSE);
351 empathy_tp_chat_send (priv->tp_chat, message);
353 g_object_unref (message);
357 chat_input_text_view_send (GossipChat *chat)
359 GossipChatPriv *priv;
360 GtkTextBuffer *buffer;
361 GtkTextIter start, end;
364 priv = GET_PRIV (chat);
366 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
368 gtk_text_buffer_get_bounds (buffer, &start, &end);
369 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
371 /* clear the input field */
372 gtk_text_buffer_set_text (buffer, "", -1);
374 chat_send (chat, msg);
378 chat->is_first_char = TRUE;
382 chat_message_received_cb (EmpathyTpChat *tp_chat,
383 GossipMessage *message,
386 GossipChatPriv *priv;
387 //GossipLogManager *log_manager;
388 GossipContact *sender;
390 priv = GET_PRIV (chat);
392 sender = gossip_message_get_sender (message);
393 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
394 gossip_contact_get_name (sender));
397 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
398 gossip_log_message_for_contact (log_manager, message, TRUE);
400 gossip_chat_view_append_message (chat->view, message);
402 if (gossip_chat_should_play_sound (chat)) {
403 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
406 g_signal_emit_by_name (chat, "new-message", message);
410 chat_sent_message_add (GossipChat *chat,
413 GossipChatPriv *priv;
417 priv = GET_PRIV (chat);
419 /* Save the sent message in our repeat buffer */
420 list = priv->sent_messages;
422 /* Remove any other occurances of this msg */
423 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
424 list = g_slist_remove_link (list, item);
426 g_slist_free1 (item);
429 /* Trim the list to the last 10 items */
430 while (g_slist_length (list) > 10) {
431 item = g_slist_last (list);
433 list = g_slist_remove_link (list, item);
435 g_slist_free1 (item);
439 /* Add new message */
440 list = g_slist_prepend (list, g_strdup (str));
442 /* Set list and reset the index */
443 priv->sent_messages = list;
444 priv->sent_messages_index = -1;
448 chat_sent_message_get_next (GossipChat *chat)
450 GossipChatPriv *priv;
453 priv = GET_PRIV (chat);
455 if (!priv->sent_messages) {
456 gossip_debug (DEBUG_DOMAIN,
457 "No sent messages, next message is NULL");
461 max = g_slist_length (priv->sent_messages) - 1;
463 if (priv->sent_messages_index < max) {
464 priv->sent_messages_index++;
467 gossip_debug (DEBUG_DOMAIN,
468 "Returning next message index:%d",
469 priv->sent_messages_index);
471 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
475 chat_sent_message_get_last (GossipChat *chat)
477 GossipChatPriv *priv;
479 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
481 priv = GET_PRIV (chat);
483 if (!priv->sent_messages) {
484 gossip_debug (DEBUG_DOMAIN,
485 "No sent messages, last message is NULL");
489 if (priv->sent_messages_index >= 0) {
490 priv->sent_messages_index--;
493 gossip_debug (DEBUG_DOMAIN,
494 "Returning last message index:%d",
495 priv->sent_messages_index);
497 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
501 chat_input_key_press_event_cb (GtkWidget *widget,
505 GossipChatPriv *priv;
508 GtkWidget *text_view_sw;
510 priv = GET_PRIV (chat);
512 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
516 /* Catch ctrl+up/down so we can traverse messages we sent */
517 if ((event->state & GDK_CONTROL_MASK) &&
518 (event->keyval == GDK_Up ||
519 event->keyval == GDK_Down)) {
520 GtkTextBuffer *buffer;
523 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
525 if (event->keyval == GDK_Up) {
526 str = chat_sent_message_get_next (chat);
528 str = chat_sent_message_get_last (chat);
531 g_signal_handlers_block_by_func (buffer,
532 chat_input_text_buffer_changed_cb,
534 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
535 g_signal_handlers_unblock_by_func (buffer,
536 chat_input_text_buffer_changed_cb,
542 /* Catch enter but not ctrl/shift-enter */
543 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
546 /* This is to make sure that kinput2 gets the enter. And if
547 * it's handled there we shouldn't send on it. This is because
548 * kinput2 uses Enter to commit letters. See:
549 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
552 view = GTK_TEXT_VIEW (chat->input_text_view);
553 if (gtk_im_context_filter_keypress (view->im_context, event)) {
554 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
558 chat_input_text_view_send (chat);
562 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
564 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
565 /* Newline for shift-enter. */
568 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
569 event->keyval == GDK_Page_Up) {
570 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
571 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
575 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
576 event->keyval == GDK_Page_Down) {
577 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
578 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
579 gtk_adjustment_set_value (adj, val);
588 chat_text_view_focus_in_event_cb (GtkWidget *widget,
592 gtk_widget_grab_focus (chat->input_text_view);
598 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
601 GossipChatPriv *priv;
602 GtkTextIter start, end;
604 gboolean spell_checker = FALSE;
606 priv = GET_PRIV (chat);
608 if (gtk_text_buffer_get_char_count (buffer) == 0) {
609 chat_composing_stop (chat);
611 chat_composing_start (chat);
614 gossip_conf_get_bool (gossip_conf_get (),
615 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
618 if (chat->is_first_char) {
622 GtkAllocation *allocation;
624 /* Save the window's size */
625 dialog = gossip_chat_window_get_dialog (priv->window);
626 gtk_window_get_size (GTK_WINDOW (dialog),
627 NULL, &window_height);
629 gtk_widget_size_request (chat->input_text_view, &req);
631 allocation = >K_WIDGET (chat->view)->allocation;
633 priv->default_window_height = window_height;
634 priv->last_input_height = req.height;
635 priv->padding_height = window_height - req.height - allocation->height;
637 chat->is_first_char = FALSE;
640 gtk_text_buffer_get_start_iter (buffer, &start);
642 if (!spell_checker) {
643 gtk_text_buffer_get_end_iter (buffer, &end);
644 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
648 if (!gossip_spell_supported ()) {
652 /* NOTE: this is really inefficient, we shouldn't have to
653 reiterate the whole buffer each time and check each work
656 gboolean correct = FALSE;
659 if (gtk_text_iter_is_start (&start)) {
662 if (!gtk_text_iter_forward_word_end (&end)) {
663 /* no whole word yet */
667 if (!gtk_text_iter_forward_word_end (&end)) {
668 /* must be the end of the buffer */
673 gtk_text_iter_backward_word_start (&start);
676 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
678 /* spell check string */
679 if (!gossip_chat_get_is_command (str)) {
680 correct = gossip_spell_check (str);
686 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
688 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
693 /* set start iter to the end iters position */
705 chat_change_size_in_idle_cb (ChangeSizeData *data)
707 gtk_window_resize (GTK_WINDOW (data->window),
708 data->width, data->height);
714 chat_text_view_scroll_hide_cb (GtkWidget *widget,
717 GossipChatPriv *priv;
720 priv = GET_PRIV (chat);
722 priv->vscroll_visible = FALSE;
723 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
725 sw = gtk_widget_get_parent (chat->input_text_view);
726 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
729 g_object_set (sw, "height-request", -1, NULL);
733 chat_text_view_size_allocate_cb (GtkWidget *widget,
734 GtkAllocation *allocation,
737 GossipChatPriv *priv;
740 ChangeSizeData *data;
743 GtkAllocation *view_allocation;
748 priv = GET_PRIV (chat);
750 if (priv->default_window_height <= 0) {
754 sw = gtk_widget_get_parent (widget);
755 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
758 priv->vscroll_visible = TRUE;
759 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
760 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
762 GTK_POLICY_AUTOMATIC);
763 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
764 g_signal_connect (vscroll, "hide",
765 G_CALLBACK (chat_text_view_scroll_hide_cb),
769 if (priv->last_input_height <= allocation->height) {
770 priv->last_input_height = allocation->height;
774 diff = priv->last_input_height - allocation->height;
775 priv->last_input_height = allocation->height;
777 view_allocation = >K_WIDGET (chat->view)->allocation;
779 dialog = gossip_chat_window_get_dialog (priv->window);
780 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
782 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
784 if (new_height <= priv->default_window_height) {
785 window_height = priv->default_window_height;
787 window_height = new_height;
790 if (current_height <= window_height) {
794 /* Restore the window's size */
795 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
797 data = g_new0 (ChangeSizeData, 1);
798 data->window = dialog;
800 data->height = window_height;
802 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
803 (GSourceFunc) chat_change_size_in_idle_cb,
808 chat_text_view_realize_cb (GtkWidget *widget,
811 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
812 gtk_widget_grab_focus (widget);
816 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
819 GtkTextBuffer *buffer;
823 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
825 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
827 gtk_text_buffer_get_end_iter (buffer, &iter);
828 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
830 gtk_text_buffer_get_end_iter (buffer, &iter);
831 gtk_text_buffer_insert (buffer, &iter, " ", -1);
835 chat_text_populate_popup_cb (GtkTextView *view,
839 GossipChatPriv *priv;
840 GtkTextBuffer *buffer;
841 GtkTextTagTable *table;
844 GtkTextIter iter, start, end;
847 GossipChatSpell *chat_spell;
848 GtkWidget *smiley_menu;
850 priv = GET_PRIV (chat);
852 /* Add the emoticon menu. */
853 item = gtk_separator_menu_item_new ();
854 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
855 gtk_widget_show (item);
857 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
858 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
859 gtk_widget_show (item);
861 smiley_menu = gossip_chat_view_get_smiley_menu (
862 G_CALLBACK (chat_insert_smiley_activate_cb),
865 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
867 /* Add the spell check menu item. */
868 buffer = gtk_text_view_get_buffer (view);
869 table = gtk_text_buffer_get_tag_table (buffer);
871 tag = gtk_text_tag_table_lookup (table, "misspelled");
873 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
875 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
876 GTK_TEXT_WINDOW_WIDGET,
880 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
884 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
885 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
887 str = gtk_text_buffer_get_text (buffer,
888 &start, &end, FALSE);
891 if (G_STR_EMPTY (str)) {
895 chat_spell = chat_spell_new (chat, str, start, end);
897 g_object_set_data_full (G_OBJECT (menu),
898 "chat_spell", chat_spell,
899 (GDestroyNotify) chat_spell_free);
901 item = gtk_separator_menu_item_new ();
902 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
903 gtk_widget_show (item);
905 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
906 g_signal_connect (item,
908 G_CALLBACK (chat_text_check_word_spelling_cb),
910 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
911 gtk_widget_show (item);
915 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
916 GossipChatSpell *chat_spell)
918 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
924 static GossipChatSpell *
925 chat_spell_new (GossipChat *chat,
930 GossipChatSpell *chat_spell;
932 chat_spell = g_new0 (GossipChatSpell, 1);
934 chat_spell->chat = g_object_ref (chat);
935 chat_spell->word = g_strdup (word);
936 chat_spell->start = start;
937 chat_spell->end = end;
943 chat_spell_free (GossipChatSpell *chat_spell)
945 g_object_unref (chat_spell->chat);
946 g_free (chat_spell->word);
951 chat_composing_start (GossipChat *chat)
953 GossipChatPriv *priv;
955 priv = GET_PRIV (chat);
957 if (priv->composing_stop_timeout_id) {
958 /* Just restart the timeout */
959 chat_composing_remove_timeout (chat);
961 empathy_tp_chat_set_state (priv->tp_chat,
962 TP_CHANNEL_CHAT_STATE_COMPOSING);
965 priv->composing_stop_timeout_id = g_timeout_add (
966 1000 * COMPOSING_STOP_TIMEOUT,
967 (GSourceFunc) chat_composing_stop_timeout_cb,
972 chat_composing_stop (GossipChat *chat)
974 GossipChatPriv *priv;
976 priv = GET_PRIV (chat);
978 chat_composing_remove_timeout (chat);
979 empathy_tp_chat_set_state (priv->tp_chat,
980 TP_CHANNEL_CHAT_STATE_ACTIVE);
984 chat_composing_remove_timeout (GossipChat *chat)
986 GossipChatPriv *priv;
988 priv = GET_PRIV (chat);
990 if (priv->composing_stop_timeout_id) {
991 g_source_remove (priv->composing_stop_timeout_id);
992 priv->composing_stop_timeout_id = 0;
997 chat_composing_stop_timeout_cb (GossipChat *chat)
999 GossipChatPriv *priv;
1001 priv = GET_PRIV (chat);
1003 priv->composing_stop_timeout_id = 0;
1004 empathy_tp_chat_set_state (priv->tp_chat,
1005 TP_CHANNEL_CHAT_STATE_PAUSED);
1011 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1012 GossipContact *contact,
1013 TelepathyChannelChatState state,
1016 /* FIXME: not yet implemented */
1020 gossip_chat_get_is_command (const gchar *str)
1022 g_return_val_if_fail (str != NULL, FALSE);
1024 if (str[0] != '/') {
1028 if (g_str_has_prefix (str, "/me")) {
1031 else if (g_str_has_prefix (str, "/nick")) {
1034 else if (g_str_has_prefix (str, "/topic")) {
1042 gossip_chat_correct_word (GossipChat *chat,
1045 const gchar *new_word)
1047 GtkTextBuffer *buffer;
1049 g_return_if_fail (chat != NULL);
1050 g_return_if_fail (new_word != NULL);
1052 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1054 gtk_text_buffer_delete (buffer, &start, &end);
1055 gtk_text_buffer_insert (buffer, &start,
1061 gossip_chat_get_name (GossipChat *chat)
1063 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1065 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1066 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1073 gossip_chat_get_tooltip (GossipChat *chat)
1075 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1077 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1078 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1085 gossip_chat_get_status_icon_name (GossipChat *chat)
1087 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1089 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1090 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1097 gossip_chat_get_contact (GossipChat *chat)
1099 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1101 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1102 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1108 gossip_chat_get_own_contact (GossipChat *chat)
1110 GossipChatPriv *priv;
1112 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1114 priv = GET_PRIV (chat);
1116 return empathy_contact_manager_get_own (priv->manager, chat->account);
1120 gossip_chat_get_widget (GossipChat *chat)
1122 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1124 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1125 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1132 gossip_chat_is_group_chat (GossipChat *chat)
1134 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1136 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1137 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1144 gossip_chat_is_connected (GossipChat *chat)
1146 GossipChatPriv *priv;
1148 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1150 priv = GET_PRIV (chat);
1152 return (priv->tp_chat != NULL);
1156 gossip_chat_get_show_contacts (GossipChat *chat)
1158 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1160 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1161 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1168 gossip_chat_set_show_contacts (GossipChat *chat,
1171 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1173 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1174 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1179 gossip_chat_save_geometry (GossipChat *chat,
1185 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1189 gossip_chat_load_geometry (GossipChat *chat,
1195 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1199 gossip_chat_set_tp_chat (GossipChat *chat,
1200 EmpathyTpChat *tp_chat)
1202 GossipChatPriv *priv;
1205 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1206 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1208 priv = GET_PRIV (chat);
1210 if (tp_chat == priv->tp_chat) {
1214 if (priv->tp_chat) {
1215 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1216 chat_message_received_cb,
1218 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1221 g_object_unref (priv->tp_chat);
1225 priv->tp_chat = g_object_ref (tp_chat);
1226 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1228 g_signal_connect (tp_chat, "message-received",
1229 G_CALLBACK (chat_message_received_cb),
1231 g_signal_connect (tp_chat, "chat-state-changed",
1232 G_CALLBACK (chat_state_changed_cb),
1234 g_signal_connect (tp_chat, "destroy",
1235 G_CALLBACK (chat_destroy_cb),
1238 empathy_tp_chat_request_pending (tp_chat);
1240 if (!priv->sensitive) {
1241 widget = gossip_chat_get_widget (chat);
1242 gtk_widget_set_sensitive (widget, TRUE);
1243 gossip_chat_view_append_event (chat->view, _("Connected"));
1244 priv->sensitive = TRUE;
1249 gossip_chat_get_id (GossipChat *chat)
1251 GossipChatPriv *priv;
1253 priv = GET_PRIV (chat);
1259 gossip_chat_clear (GossipChat *chat)
1261 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1263 gossip_chat_view_clear (chat->view);
1267 gossip_chat_set_window (GossipChat *chat,
1268 GossipChatWindow *window)
1270 GossipChatPriv *priv;
1272 priv = GET_PRIV (chat);
1273 priv->window = window;
1277 gossip_chat_get_window (GossipChat *chat)
1279 GossipChatPriv *priv;
1281 priv = GET_PRIV (chat);
1283 return priv->window;
1287 gossip_chat_scroll_down (GossipChat *chat)
1289 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1291 gossip_chat_view_scroll_down (chat->view);
1295 gossip_chat_cut (GossipChat *chat)
1297 GtkTextBuffer *buffer;
1299 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1301 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1302 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1303 GtkClipboard *clipboard;
1305 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1307 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1312 gossip_chat_copy (GossipChat *chat)
1314 GtkTextBuffer *buffer;
1316 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1318 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1319 gossip_chat_view_copy_clipboard (chat->view);
1323 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1324 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1325 GtkClipboard *clipboard;
1327 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1329 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1334 gossip_chat_paste (GossipChat *chat)
1336 GtkTextBuffer *buffer;
1337 GtkClipboard *clipboard;
1339 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1341 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1342 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1344 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1348 gossip_chat_present (GossipChat *chat)
1350 GossipChatPriv *priv;
1352 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1354 priv = GET_PRIV (chat);
1356 if (priv->window == NULL) {
1357 GossipChatWindow *window;
1359 window = gossip_chat_window_get_default ();
1361 window = gossip_chat_window_new ();
1364 gossip_chat_window_add_chat (window, chat);
1367 gossip_chat_window_switch_to_chat (priv->window, chat);
1368 gossip_window_present (
1369 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1372 gtk_widget_grab_focus (chat->input_text_view);
1376 gossip_chat_should_play_sound (GossipChat *chat)
1378 GossipChatWindow *window;
1379 gboolean play = TRUE;
1381 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1383 window = gossip_chat_get_window (chat);
1388 play = !gossip_chat_window_has_focus (window);
1394 gossip_chat_should_highlight_nick (GossipMessage *message)
1396 GossipContact *my_contact;
1397 const gchar *msg, *to;
1398 gchar *cf_msg, *cf_to;
1402 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1404 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1408 msg = gossip_message_get_body (message);
1413 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1414 to = gossip_contact_get_name (my_contact);
1419 cf_msg = g_utf8_casefold (msg, -1);
1420 cf_to = g_utf8_casefold (to, -1);
1422 ch = strstr (cf_msg, cf_to);
1428 /* Not first in the message */
1429 if ((*(ch - 1) != ' ') &&
1430 (*(ch - 1) != ',') &&
1431 (*(ch - 1) != '.')) {
1436 ch = ch + strlen (cf_to);
1437 if (ch >= cf_msg + strlen (cf_msg)) {