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/empathy-log-manager.h>
40 #include <libempathy/gossip-debug.h>
41 #include <libempathy/gossip-utils.h>
42 #include <libempathy/gossip-conf.h>
43 #include <libempathy/empathy-marshal.h>
45 #include "gossip-chat.h"
46 #include "gossip-chat-window.h"
47 #include "gossip-geometry.h"
48 #include "gossip-preferences.h"
49 #include "gossip-spell.h"
50 //#include "gossip-spell-dialog.h"
51 #include "gossip-ui-utils.h"
53 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
55 #define DEBUG_DOMAIN "Chat"
57 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
58 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
62 #define MAX_INPUT_HEIGHT 150
64 #define COMPOSING_STOP_TIMEOUT 5
66 struct _GossipChatPriv {
67 EmpathyContactManager *manager;
68 EmpathyLogManager *log_manager;
69 EmpathyTpChat *tp_chat;
70 GossipChatWindow *window;
71 GtkTooltips *tooltips;
72 guint composing_stop_timeout_id;
75 GSList *sent_messages;
76 gint sent_messages_index;
79 gboolean first_tp_chat;
80 /* Used to automatically shrink a window that has temporarily
81 * grown due to long input.
84 gint default_window_height;
85 gint last_input_height;
86 gboolean vscroll_visible;
97 static void gossip_chat_class_init (GossipChatClass *klass);
98 static void gossip_chat_init (GossipChat *chat);
99 static void chat_finalize (GObject *object);
100 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
102 static void chat_send (GossipChat *chat,
104 static void chat_input_text_view_send (GossipChat *chat);
105 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
106 GossipMessage *message,
108 void chat_sent_message_add (GossipChat *chat,
110 const gchar * chat_sent_message_get_next (GossipChat *chat);
111 const gchar * chat_sent_message_get_last (GossipChat *chat);
112 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
115 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
117 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
120 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
122 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
123 GtkAllocation *allocation,
125 static void chat_text_view_realize_cb (GtkWidget *widget,
127 static void chat_text_populate_popup_cb (GtkTextView *view,
130 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
131 GossipChatSpell *chat_spell);
132 static GossipChatSpell *chat_spell_new (GossipChat *chat,
136 static void chat_spell_free (GossipChatSpell *chat_spell);
137 static void chat_composing_start (GossipChat *chat);
138 static void chat_composing_stop (GossipChat *chat);
139 static void chat_composing_remove_timeout (GossipChat *chat);
140 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
141 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
142 GossipContact *contact,
143 TelepathyChannelChatState state,
145 static void chat_add_logs (GossipChat *chat);
146 static gboolean chat_scroll_down_idle_func (GossipChat *chat);
156 static guint signals[LAST_SIGNAL] = { 0 };
158 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
161 gossip_chat_class_init (GossipChatClass *klass)
163 GObjectClass *object_class;
165 object_class = G_OBJECT_CLASS (klass);
167 object_class->finalize = chat_finalize;
170 g_signal_new ("composing",
171 G_OBJECT_CLASS_TYPE (object_class),
175 g_cclosure_marshal_VOID__BOOLEAN,
179 signals[NEW_MESSAGE] =
180 g_signal_new ("new-message",
181 G_OBJECT_CLASS_TYPE (object_class),
185 empathy_marshal_VOID__OBJECT_BOOLEAN,
187 2, GOSSIP_TYPE_MESSAGE, G_TYPE_BOOLEAN);
189 signals[NAME_CHANGED] =
190 g_signal_new ("name-changed",
191 G_OBJECT_CLASS_TYPE (object_class),
195 g_cclosure_marshal_VOID__POINTER,
199 signals[STATUS_CHANGED] =
200 g_signal_new ("status-changed",
201 G_OBJECT_CLASS_TYPE (object_class),
205 g_cclosure_marshal_VOID__VOID,
209 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
213 gossip_chat_init (GossipChat *chat)
215 GossipChatPriv *priv;
216 GtkTextBuffer *buffer;
218 chat->view = gossip_chat_view_new ();
219 chat->input_text_view = gtk_text_view_new ();
221 chat->is_first_char = TRUE;
223 g_object_set (chat->input_text_view,
224 "pixels-above-lines", 2,
225 "pixels-below-lines", 2,
226 "pixels-inside-wrap", 1,
229 "wrap-mode", GTK_WRAP_WORD_CHAR,
232 priv = GET_PRIV (chat);
234 priv->manager = empathy_contact_manager_new ();
235 priv->log_manager = empathy_log_manager_new ();
236 priv->tooltips = g_object_ref_sink (gtk_tooltips_new ());
237 priv->default_window_height = -1;
238 priv->vscroll_visible = FALSE;
239 priv->sensitive = TRUE;
240 priv->sent_messages = NULL;
241 priv->sent_messages_index = -1;
242 priv->first_tp_chat = TRUE;
244 g_signal_connect (chat->input_text_view,
246 G_CALLBACK (chat_input_key_press_event_cb),
249 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
250 g_signal_connect (buffer,
252 G_CALLBACK (chat_input_text_buffer_changed_cb),
254 g_signal_connect (chat->view,
256 G_CALLBACK (chat_text_view_focus_in_event_cb),
259 g_signal_connect (chat->input_text_view,
261 G_CALLBACK (chat_text_view_size_allocate_cb),
264 g_signal_connect (chat->input_text_view,
266 G_CALLBACK (chat_text_view_realize_cb),
269 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
271 G_CALLBACK (chat_text_populate_popup_cb),
274 /* create misspelt words identification tag */
275 gtk_text_buffer_create_tag (buffer,
277 "underline", PANGO_UNDERLINE_ERROR,
282 chat_finalize (GObject *object)
285 GossipChatPriv *priv;
287 chat = GOSSIP_CHAT (object);
288 priv = GET_PRIV (chat);
290 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
292 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
293 g_slist_free (priv->sent_messages);
295 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
296 g_list_free (priv->compositors);
298 chat_composing_remove_timeout (chat);
299 g_object_unref (chat->account);
300 g_object_unref (priv->manager);
301 g_object_unref (priv->log_manager);
302 g_object_unref (priv->tooltips);
305 g_object_unref (priv->tp_chat);
308 if (priv->scroll_idle_id) {
309 g_source_remove (priv->scroll_idle_id);
314 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
318 chat_destroy_cb (EmpathyTpChat *tp_chat,
321 GossipChatPriv *priv;
324 priv = GET_PRIV (chat);
327 g_object_unref (priv->tp_chat);
328 priv->tp_chat = NULL;
331 gossip_chat_view_append_event (chat->view, _("Disconnected"));
333 widget = gossip_chat_get_widget (chat);
334 gtk_widget_set_sensitive (widget, FALSE);
335 priv->sensitive = FALSE;
339 chat_send (GossipChat *chat,
342 GossipChatPriv *priv;
343 GossipMessage *message;
344 GossipContact *own_contact;
346 priv = GET_PRIV (chat);
348 if (G_STR_EMPTY (msg)) {
352 chat_sent_message_add (chat, msg);
354 if (g_str_has_prefix (msg, "/clear")) {
355 gossip_chat_view_clear (chat->view);
359 /* FIXME: add here something to let group/privrate chat handle
360 * some special messages */
362 own_contact = empathy_contact_manager_get_user (priv->manager, chat->account);
363 message = gossip_message_new (msg);
364 gossip_message_set_sender (message, own_contact);
366 empathy_tp_chat_send (priv->tp_chat, message);
368 g_object_unref (message);
372 chat_input_text_view_send (GossipChat *chat)
374 GossipChatPriv *priv;
375 GtkTextBuffer *buffer;
376 GtkTextIter start, end;
379 priv = GET_PRIV (chat);
381 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
383 gtk_text_buffer_get_bounds (buffer, &start, &end);
384 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
386 /* clear the input field */
387 gtk_text_buffer_set_text (buffer, "", -1);
389 chat_send (chat, msg);
393 chat->is_first_char = TRUE;
397 chat_message_received_cb (EmpathyTpChat *tp_chat,
398 GossipMessage *message,
401 GossipChatPriv *priv;
402 GossipContact *sender;
404 priv = GET_PRIV (chat);
406 sender = gossip_message_get_sender (message);
407 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
408 gossip_contact_get_name (sender));
410 empathy_log_manager_add_message (priv->log_manager,
411 gossip_chat_get_id (chat),
414 gossip_chat_view_append_message (chat->view, message);
416 if (gossip_chat_should_play_sound (chat)) {
417 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
420 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
424 chat_sent_message_add (GossipChat *chat,
427 GossipChatPriv *priv;
431 priv = GET_PRIV (chat);
433 /* Save the sent message in our repeat buffer */
434 list = priv->sent_messages;
436 /* Remove any other occurances of this msg */
437 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
438 list = g_slist_remove_link (list, item);
440 g_slist_free1 (item);
443 /* Trim the list to the last 10 items */
444 while (g_slist_length (list) > 10) {
445 item = g_slist_last (list);
447 list = g_slist_remove_link (list, item);
449 g_slist_free1 (item);
453 /* Add new message */
454 list = g_slist_prepend (list, g_strdup (str));
456 /* Set list and reset the index */
457 priv->sent_messages = list;
458 priv->sent_messages_index = -1;
462 chat_sent_message_get_next (GossipChat *chat)
464 GossipChatPriv *priv;
467 priv = GET_PRIV (chat);
469 if (!priv->sent_messages) {
470 gossip_debug (DEBUG_DOMAIN,
471 "No sent messages, next message is NULL");
475 max = g_slist_length (priv->sent_messages) - 1;
477 if (priv->sent_messages_index < max) {
478 priv->sent_messages_index++;
481 gossip_debug (DEBUG_DOMAIN,
482 "Returning next message index:%d",
483 priv->sent_messages_index);
485 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
489 chat_sent_message_get_last (GossipChat *chat)
491 GossipChatPriv *priv;
493 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
495 priv = GET_PRIV (chat);
497 if (!priv->sent_messages) {
498 gossip_debug (DEBUG_DOMAIN,
499 "No sent messages, last message is NULL");
503 if (priv->sent_messages_index >= 0) {
504 priv->sent_messages_index--;
507 gossip_debug (DEBUG_DOMAIN,
508 "Returning last message index:%d",
509 priv->sent_messages_index);
511 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
515 chat_input_key_press_event_cb (GtkWidget *widget,
519 GossipChatPriv *priv;
522 GtkWidget *text_view_sw;
524 priv = GET_PRIV (chat);
526 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
530 /* Catch ctrl+up/down so we can traverse messages we sent */
531 if ((event->state & GDK_CONTROL_MASK) &&
532 (event->keyval == GDK_Up ||
533 event->keyval == GDK_Down)) {
534 GtkTextBuffer *buffer;
537 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
539 if (event->keyval == GDK_Up) {
540 str = chat_sent_message_get_next (chat);
542 str = chat_sent_message_get_last (chat);
545 g_signal_handlers_block_by_func (buffer,
546 chat_input_text_buffer_changed_cb,
548 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
549 g_signal_handlers_unblock_by_func (buffer,
550 chat_input_text_buffer_changed_cb,
556 /* Catch enter but not ctrl/shift-enter */
557 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
560 /* This is to make sure that kinput2 gets the enter. And if
561 * it's handled there we shouldn't send on it. This is because
562 * kinput2 uses Enter to commit letters. See:
563 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
566 view = GTK_TEXT_VIEW (chat->input_text_view);
567 if (gtk_im_context_filter_keypress (view->im_context, event)) {
568 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
572 chat_input_text_view_send (chat);
576 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
578 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
579 /* Newline for shift-enter. */
582 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
583 event->keyval == GDK_Page_Up) {
584 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
585 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
589 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
590 event->keyval == GDK_Page_Down) {
591 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
592 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
593 gtk_adjustment_set_value (adj, val);
602 chat_text_view_focus_in_event_cb (GtkWidget *widget,
606 gtk_widget_grab_focus (chat->input_text_view);
612 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
615 GossipChatPriv *priv;
616 GtkTextIter start, end;
618 gboolean spell_checker = FALSE;
620 priv = GET_PRIV (chat);
622 if (gtk_text_buffer_get_char_count (buffer) == 0) {
623 chat_composing_stop (chat);
625 chat_composing_start (chat);
628 gossip_conf_get_bool (gossip_conf_get (),
629 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
632 if (chat->is_first_char) {
636 GtkAllocation *allocation;
638 /* Save the window's size */
639 dialog = gossip_chat_window_get_dialog (priv->window);
640 gtk_window_get_size (GTK_WINDOW (dialog),
641 NULL, &window_height);
643 gtk_widget_size_request (chat->input_text_view, &req);
645 allocation = >K_WIDGET (chat->view)->allocation;
647 priv->default_window_height = window_height;
648 priv->last_input_height = req.height;
649 priv->padding_height = window_height - req.height - allocation->height;
651 chat->is_first_char = FALSE;
654 gtk_text_buffer_get_start_iter (buffer, &start);
656 if (!spell_checker) {
657 gtk_text_buffer_get_end_iter (buffer, &end);
658 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
662 if (!gossip_spell_supported ()) {
666 /* NOTE: this is really inefficient, we shouldn't have to
667 reiterate the whole buffer each time and check each work
670 gboolean correct = FALSE;
673 if (gtk_text_iter_is_start (&start)) {
676 if (!gtk_text_iter_forward_word_end (&end)) {
677 /* no whole word yet */
681 if (!gtk_text_iter_forward_word_end (&end)) {
682 /* must be the end of the buffer */
687 gtk_text_iter_backward_word_start (&start);
690 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
692 /* spell check string */
693 if (!gossip_chat_get_is_command (str)) {
694 correct = gossip_spell_check (str);
700 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
702 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
707 /* set start iter to the end iters position */
719 chat_change_size_in_idle_cb (ChangeSizeData *data)
721 gtk_window_resize (GTK_WINDOW (data->window),
722 data->width, data->height);
728 chat_text_view_scroll_hide_cb (GtkWidget *widget,
731 GossipChatPriv *priv;
734 priv = GET_PRIV (chat);
736 priv->vscroll_visible = FALSE;
737 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
739 sw = gtk_widget_get_parent (chat->input_text_view);
740 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
743 g_object_set (sw, "height-request", -1, NULL);
747 chat_text_view_size_allocate_cb (GtkWidget *widget,
748 GtkAllocation *allocation,
751 GossipChatPriv *priv;
754 ChangeSizeData *data;
757 GtkAllocation *view_allocation;
762 priv = GET_PRIV (chat);
764 if (priv->default_window_height <= 0) {
768 sw = gtk_widget_get_parent (widget);
769 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
772 priv->vscroll_visible = TRUE;
773 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
774 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
776 GTK_POLICY_AUTOMATIC);
777 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
778 g_signal_connect (vscroll, "hide",
779 G_CALLBACK (chat_text_view_scroll_hide_cb),
783 if (priv->last_input_height <= allocation->height) {
784 priv->last_input_height = allocation->height;
788 diff = priv->last_input_height - allocation->height;
789 priv->last_input_height = allocation->height;
791 view_allocation = >K_WIDGET (chat->view)->allocation;
793 dialog = gossip_chat_window_get_dialog (priv->window);
794 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
796 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
798 if (new_height <= priv->default_window_height) {
799 window_height = priv->default_window_height;
801 window_height = new_height;
804 if (current_height <= window_height) {
808 /* Restore the window's size */
809 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
811 data = g_new0 (ChangeSizeData, 1);
812 data->window = dialog;
814 data->height = window_height;
816 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
817 (GSourceFunc) chat_change_size_in_idle_cb,
822 chat_text_view_realize_cb (GtkWidget *widget,
825 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
826 gtk_widget_grab_focus (widget);
830 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
833 GtkTextBuffer *buffer;
837 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
839 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
841 gtk_text_buffer_get_end_iter (buffer, &iter);
842 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
844 gtk_text_buffer_get_end_iter (buffer, &iter);
845 gtk_text_buffer_insert (buffer, &iter, " ", -1);
849 chat_text_populate_popup_cb (GtkTextView *view,
853 GossipChatPriv *priv;
854 GtkTextBuffer *buffer;
855 GtkTextTagTable *table;
858 GtkTextIter iter, start, end;
861 GossipChatSpell *chat_spell;
862 GtkWidget *smiley_menu;
864 priv = GET_PRIV (chat);
866 /* Add the emoticon menu. */
867 item = gtk_separator_menu_item_new ();
868 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
869 gtk_widget_show (item);
871 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
872 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
873 gtk_widget_show (item);
875 smiley_menu = gossip_chat_view_get_smiley_menu (
876 G_CALLBACK (chat_insert_smiley_activate_cb),
879 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
881 /* Add the spell check menu item. */
882 buffer = gtk_text_view_get_buffer (view);
883 table = gtk_text_buffer_get_tag_table (buffer);
885 tag = gtk_text_tag_table_lookup (table, "misspelled");
887 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
889 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
890 GTK_TEXT_WINDOW_WIDGET,
894 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
898 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
899 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
901 str = gtk_text_buffer_get_text (buffer,
902 &start, &end, FALSE);
905 if (G_STR_EMPTY (str)) {
909 chat_spell = chat_spell_new (chat, str, start, end);
911 g_object_set_data_full (G_OBJECT (menu),
912 "chat_spell", chat_spell,
913 (GDestroyNotify) chat_spell_free);
915 item = gtk_separator_menu_item_new ();
916 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
917 gtk_widget_show (item);
919 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
920 g_signal_connect (item,
922 G_CALLBACK (chat_text_check_word_spelling_cb),
924 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
925 gtk_widget_show (item);
929 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
930 GossipChatSpell *chat_spell)
932 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
938 static GossipChatSpell *
939 chat_spell_new (GossipChat *chat,
944 GossipChatSpell *chat_spell;
946 chat_spell = g_new0 (GossipChatSpell, 1);
948 chat_spell->chat = g_object_ref (chat);
949 chat_spell->word = g_strdup (word);
950 chat_spell->start = start;
951 chat_spell->end = end;
957 chat_spell_free (GossipChatSpell *chat_spell)
959 g_object_unref (chat_spell->chat);
960 g_free (chat_spell->word);
965 chat_composing_start (GossipChat *chat)
967 GossipChatPriv *priv;
969 priv = GET_PRIV (chat);
971 if (priv->composing_stop_timeout_id) {
972 /* Just restart the timeout */
973 chat_composing_remove_timeout (chat);
975 empathy_tp_chat_set_state (priv->tp_chat,
976 TP_CHANNEL_CHAT_STATE_COMPOSING);
979 priv->composing_stop_timeout_id = g_timeout_add (
980 1000 * COMPOSING_STOP_TIMEOUT,
981 (GSourceFunc) chat_composing_stop_timeout_cb,
986 chat_composing_stop (GossipChat *chat)
988 GossipChatPriv *priv;
990 priv = GET_PRIV (chat);
992 chat_composing_remove_timeout (chat);
993 empathy_tp_chat_set_state (priv->tp_chat,
994 TP_CHANNEL_CHAT_STATE_ACTIVE);
998 chat_composing_remove_timeout (GossipChat *chat)
1000 GossipChatPriv *priv;
1002 priv = GET_PRIV (chat);
1004 if (priv->composing_stop_timeout_id) {
1005 g_source_remove (priv->composing_stop_timeout_id);
1006 priv->composing_stop_timeout_id = 0;
1011 chat_composing_stop_timeout_cb (GossipChat *chat)
1013 GossipChatPriv *priv;
1015 priv = GET_PRIV (chat);
1017 priv->composing_stop_timeout_id = 0;
1018 empathy_tp_chat_set_state (priv->tp_chat,
1019 TP_CHANNEL_CHAT_STATE_PAUSED);
1025 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1026 GossipContact *contact,
1027 TelepathyChannelChatState state,
1030 GossipChatPriv *priv;
1031 GossipContact *own_contact;
1033 gboolean was_composing;
1035 priv = GET_PRIV (chat);
1037 own_contact = gossip_contact_get_user (contact);
1038 if (gossip_contact_equal (own_contact, contact)) {
1039 /* We don't care about our own chat state */
1043 was_composing = (priv->compositors != NULL);
1045 /* Find the contact in the list. After that l is the list elem or NULL */
1046 for (l = priv->compositors; l; l = l->next) {
1047 if (gossip_contact_equal (contact, l->data)) {
1053 case TP_CHANNEL_CHAT_STATE_GONE:
1054 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1055 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1056 /* Contact is not composing */
1058 priv->compositors = g_list_remove_link (priv->compositors, l);
1059 g_object_unref (l->data);
1063 case TP_CHANNEL_CHAT_STATE_PAUSED:
1064 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1065 /* Contact is composing */
1067 priv->compositors = g_list_prepend (priv->compositors,
1068 g_object_ref (contact));
1072 g_assert_not_reached ();
1075 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1076 was_composing ? "yes" : "no",
1077 priv->compositors ? "yes" : "no");
1079 if ((was_composing && !priv->compositors) ||
1080 (!was_composing && priv->compositors)) {
1081 /* Composing state changed */
1082 g_signal_emit (chat, signals[COMPOSING], 0,
1083 (gboolean) priv->compositors);
1088 chat_add_logs (GossipChat *chat)
1090 GossipChatPriv *priv;
1091 GList *messages, *l;
1095 priv = GET_PRIV (chat);
1097 /* Turn off scrolling temporarily */
1098 gossip_chat_view_scroll (chat->view, FALSE);
1100 /* Add messages from last conversation */
1101 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1103 gossip_chat_get_id (chat));
1104 num_messages = g_list_length (messages);
1106 for (l = messages, i = 0; l; l = l->next, i++) {
1107 GossipMessage *message;
1111 /* Only add 10 last messages */
1112 if (num_messages - i > 10) {
1113 g_object_unref (message);
1118 gossip_chat_view_append_message (chat->view, message);
1119 g_object_unref (message);
1121 g_list_free (messages);
1123 /* Turn back on scrolling */
1124 gossip_chat_view_scroll (chat->view, TRUE);
1126 /* Scroll to the most recent messages, we reference the chat
1127 * for the duration of the scroll func.
1129 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1130 g_object_ref (chat));
1133 /* Scroll down after the back-log has been received. */
1135 chat_scroll_down_idle_func (GossipChat *chat)
1137 GossipChatPriv *priv;
1139 priv = GET_PRIV (chat);
1141 gossip_chat_scroll_down (chat);
1142 g_object_unref (chat);
1144 priv->scroll_idle_id = 0;
1150 gossip_chat_get_is_command (const gchar *str)
1152 g_return_val_if_fail (str != NULL, FALSE);
1154 if (str[0] != '/') {
1158 if (g_str_has_prefix (str, "/me")) {
1161 else if (g_str_has_prefix (str, "/nick")) {
1164 else if (g_str_has_prefix (str, "/topic")) {
1172 gossip_chat_correct_word (GossipChat *chat,
1175 const gchar *new_word)
1177 GtkTextBuffer *buffer;
1179 g_return_if_fail (chat != NULL);
1180 g_return_if_fail (new_word != NULL);
1182 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1184 gtk_text_buffer_delete (buffer, &start, &end);
1185 gtk_text_buffer_insert (buffer, &start,
1191 gossip_chat_get_name (GossipChat *chat)
1193 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1195 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1196 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1203 gossip_chat_get_tooltip (GossipChat *chat)
1205 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1207 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1208 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1215 gossip_chat_get_status_icon_name (GossipChat *chat)
1217 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1219 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1220 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1227 gossip_chat_get_widget (GossipChat *chat)
1229 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1231 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1232 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1239 gossip_chat_is_group_chat (GossipChat *chat)
1241 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1243 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1244 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1251 gossip_chat_is_connected (GossipChat *chat)
1253 GossipChatPriv *priv;
1255 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1257 priv = GET_PRIV (chat);
1259 return (priv->tp_chat != NULL);
1263 gossip_chat_save_geometry (GossipChat *chat,
1269 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1273 gossip_chat_load_geometry (GossipChat *chat,
1279 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1283 gossip_chat_set_tp_chat (GossipChat *chat,
1284 EmpathyTpChat *tp_chat)
1286 GossipChatPriv *priv;
1289 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1290 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1292 priv = GET_PRIV (chat);
1294 if (tp_chat == priv->tp_chat) {
1298 if (priv->tp_chat) {
1299 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1300 chat_message_received_cb,
1302 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1305 g_object_unref (priv->tp_chat);
1309 priv->tp_chat = g_object_ref (tp_chat);
1310 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1312 if (priv->first_tp_chat) {
1313 chat_add_logs (chat);
1314 priv->first_tp_chat = FALSE;
1317 g_signal_connect (tp_chat, "message-received",
1318 G_CALLBACK (chat_message_received_cb),
1320 g_signal_connect (tp_chat, "chat-state-changed",
1321 G_CALLBACK (chat_state_changed_cb),
1323 g_signal_connect (tp_chat, "destroy",
1324 G_CALLBACK (chat_destroy_cb),
1327 empathy_tp_chat_request_pending (tp_chat);
1329 if (!priv->sensitive) {
1330 widget = gossip_chat_get_widget (chat);
1331 gtk_widget_set_sensitive (widget, TRUE);
1332 gossip_chat_view_append_event (chat->view, _("Connected"));
1333 priv->sensitive = TRUE;
1338 gossip_chat_get_id (GossipChat *chat)
1340 GossipChatPriv *priv;
1342 priv = GET_PRIV (chat);
1348 gossip_chat_clear (GossipChat *chat)
1350 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1352 gossip_chat_view_clear (chat->view);
1356 gossip_chat_set_window (GossipChat *chat,
1357 GossipChatWindow *window)
1359 GossipChatPriv *priv;
1361 priv = GET_PRIV (chat);
1362 priv->window = window;
1366 gossip_chat_get_window (GossipChat *chat)
1368 GossipChatPriv *priv;
1370 priv = GET_PRIV (chat);
1372 return priv->window;
1376 gossip_chat_scroll_down (GossipChat *chat)
1378 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1380 gossip_chat_view_scroll_down (chat->view);
1384 gossip_chat_cut (GossipChat *chat)
1386 GtkTextBuffer *buffer;
1388 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1390 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1391 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1392 GtkClipboard *clipboard;
1394 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1396 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1401 gossip_chat_copy (GossipChat *chat)
1403 GtkTextBuffer *buffer;
1405 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1407 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1408 gossip_chat_view_copy_clipboard (chat->view);
1412 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1413 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1414 GtkClipboard *clipboard;
1416 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1418 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1423 gossip_chat_paste (GossipChat *chat)
1425 GtkTextBuffer *buffer;
1426 GtkClipboard *clipboard;
1428 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1430 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1431 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1433 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1437 gossip_chat_present (GossipChat *chat)
1439 GossipChatPriv *priv;
1441 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1443 priv = GET_PRIV (chat);
1445 if (priv->window == NULL) {
1446 GossipChatWindow *window;
1448 window = gossip_chat_window_get_default ();
1450 window = gossip_chat_window_new ();
1453 gossip_chat_window_add_chat (window, chat);
1456 gossip_chat_window_switch_to_chat (priv->window, chat);
1457 gossip_window_present (
1458 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1461 gtk_widget_grab_focus (chat->input_text_view);
1465 gossip_chat_should_play_sound (GossipChat *chat)
1467 GossipChatWindow *window;
1468 gboolean play = TRUE;
1470 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1472 window = gossip_chat_get_window (chat);
1477 play = !gossip_chat_window_has_focus (window);
1483 gossip_chat_should_highlight_nick (GossipMessage *message)
1485 GossipContact *my_contact;
1486 const gchar *msg, *to;
1487 gchar *cf_msg, *cf_to;
1491 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1493 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1497 msg = gossip_message_get_body (message);
1502 my_contact = gossip_contact_get_user (gossip_message_get_sender (message));
1503 to = gossip_contact_get_name (my_contact);
1508 cf_msg = g_utf8_casefold (msg, -1);
1509 cf_to = g_utf8_casefold (to, -1);
1511 ch = strstr (cf_msg, cf_to);
1517 /* Not first in the message */
1518 if ((*(ch - 1) != ' ') &&
1519 (*(ch - 1) != ',') &&
1520 (*(ch - 1) != '.')) {
1525 ch = ch + strlen (cf_to);
1526 if (ch >= cf_msg + strlen (cf_msg)) {