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 GossipTime time_joined;
81 /* Used to automatically shrink a window that has temporarily
82 * grown due to long input.
85 gint default_window_height;
86 gint last_input_height;
87 gboolean vscroll_visible;
98 static void gossip_chat_class_init (GossipChatClass *klass);
99 static void gossip_chat_init (GossipChat *chat);
100 static void chat_finalize (GObject *object);
101 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
103 static void chat_send (GossipChat *chat,
105 static void chat_input_text_view_send (GossipChat *chat);
106 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
107 GossipMessage *message,
109 void chat_sent_message_add (GossipChat *chat,
111 const gchar * chat_sent_message_get_next (GossipChat *chat);
112 const gchar * chat_sent_message_get_last (GossipChat *chat);
113 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
116 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
118 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
121 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
123 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
124 GtkAllocation *allocation,
126 static void chat_text_view_realize_cb (GtkWidget *widget,
128 static void chat_text_populate_popup_cb (GtkTextView *view,
131 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
132 GossipChatSpell *chat_spell);
133 static GossipChatSpell *chat_spell_new (GossipChat *chat,
137 static void chat_spell_free (GossipChatSpell *chat_spell);
138 static void chat_composing_start (GossipChat *chat);
139 static void chat_composing_stop (GossipChat *chat);
140 static void chat_composing_remove_timeout (GossipChat *chat);
141 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
142 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
143 GossipContact *contact,
144 TelepathyChannelChatState state,
146 static void chat_add_logs (GossipChat *chat);
147 static gboolean chat_scroll_down_idle_func (GossipChat *chat);
157 static guint signals[LAST_SIGNAL] = { 0 };
159 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
162 gossip_chat_class_init (GossipChatClass *klass)
164 GObjectClass *object_class;
166 object_class = G_OBJECT_CLASS (klass);
168 object_class->finalize = chat_finalize;
171 g_signal_new ("composing",
172 G_OBJECT_CLASS_TYPE (object_class),
176 g_cclosure_marshal_VOID__BOOLEAN,
180 signals[NEW_MESSAGE] =
181 g_signal_new ("new-message",
182 G_OBJECT_CLASS_TYPE (object_class),
186 empathy_marshal_VOID__OBJECT_BOOLEAN,
188 2, GOSSIP_TYPE_MESSAGE, G_TYPE_BOOLEAN);
190 signals[NAME_CHANGED] =
191 g_signal_new ("name-changed",
192 G_OBJECT_CLASS_TYPE (object_class),
196 g_cclosure_marshal_VOID__POINTER,
200 signals[STATUS_CHANGED] =
201 g_signal_new ("status-changed",
202 G_OBJECT_CLASS_TYPE (object_class),
206 g_cclosure_marshal_VOID__VOID,
210 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
214 gossip_chat_init (GossipChat *chat)
216 GossipChatPriv *priv;
217 GtkTextBuffer *buffer;
219 chat->view = gossip_chat_view_new ();
220 chat->input_text_view = gtk_text_view_new ();
222 chat->is_first_char = TRUE;
224 g_object_set (chat->input_text_view,
225 "pixels-above-lines", 2,
226 "pixels-below-lines", 2,
227 "pixels-inside-wrap", 1,
230 "wrap-mode", GTK_WRAP_WORD_CHAR,
233 priv = GET_PRIV (chat);
235 priv->manager = empathy_contact_manager_new ();
236 priv->log_manager = empathy_log_manager_new ();
237 priv->tooltips = g_object_ref_sink (gtk_tooltips_new ());
238 priv->default_window_height = -1;
239 priv->vscroll_visible = FALSE;
240 priv->sensitive = TRUE;
241 priv->sent_messages = NULL;
242 priv->sent_messages_index = -1;
243 priv->first_tp_chat = TRUE;
245 g_signal_connect (chat->input_text_view,
247 G_CALLBACK (chat_input_key_press_event_cb),
250 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
251 g_signal_connect (buffer,
253 G_CALLBACK (chat_input_text_buffer_changed_cb),
255 g_signal_connect (chat->view,
257 G_CALLBACK (chat_text_view_focus_in_event_cb),
260 g_signal_connect (chat->input_text_view,
262 G_CALLBACK (chat_text_view_size_allocate_cb),
265 g_signal_connect (chat->input_text_view,
267 G_CALLBACK (chat_text_view_realize_cb),
270 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
272 G_CALLBACK (chat_text_populate_popup_cb),
275 /* create misspelt words identification tag */
276 gtk_text_buffer_create_tag (buffer,
278 "underline", PANGO_UNDERLINE_ERROR,
283 chat_finalize (GObject *object)
286 GossipChatPriv *priv;
288 chat = GOSSIP_CHAT (object);
289 priv = GET_PRIV (chat);
291 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
293 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
294 g_slist_free (priv->sent_messages);
296 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
297 g_list_free (priv->compositors);
299 chat_composing_remove_timeout (chat);
300 g_object_unref (chat->account);
301 g_object_unref (priv->manager);
302 g_object_unref (priv->log_manager);
303 g_object_unref (priv->tooltips);
306 g_object_unref (priv->tp_chat);
309 if (priv->scroll_idle_id) {
310 g_source_remove (priv->scroll_idle_id);
315 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
319 chat_destroy_cb (EmpathyTpChat *tp_chat,
322 GossipChatPriv *priv;
324 priv = GET_PRIV (chat);
327 g_object_unref (priv->tp_chat);
328 priv->tp_chat = NULL;
330 priv->sensitive = FALSE;
332 gossip_chat_view_append_event (chat->view, _("Disconnected"));
333 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
335 if (GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat) {
336 GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
341 chat_send (GossipChat *chat,
344 GossipChatPriv *priv;
345 GossipMessage *message;
347 priv = GET_PRIV (chat);
349 if (G_STR_EMPTY (msg)) {
353 chat_sent_message_add (chat, msg);
355 if (g_str_has_prefix (msg, "/clear")) {
356 gossip_chat_view_clear (chat->view);
360 /* FIXME: add here something to let group/privrate chat handle
361 * some special messages */
363 message = gossip_message_new (msg);
365 empathy_tp_chat_send (priv->tp_chat, message);
367 g_object_unref (message);
371 chat_input_text_view_send (GossipChat *chat)
373 GossipChatPriv *priv;
374 GtkTextBuffer *buffer;
375 GtkTextIter start, end;
378 priv = GET_PRIV (chat);
380 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
382 gtk_text_buffer_get_bounds (buffer, &start, &end);
383 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
385 /* clear the input field */
386 gtk_text_buffer_set_text (buffer, "", -1);
388 chat_send (chat, msg);
392 chat->is_first_char = TRUE;
396 chat_message_received_cb (EmpathyTpChat *tp_chat,
397 GossipMessage *message,
400 GossipChatPriv *priv;
401 GossipContact *sender;
402 GossipTime timestamp;
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 /* Log the message only if it's not backlog */
411 timestamp = gossip_message_get_timestamp (message);
412 if (timestamp >= priv->time_joined) {
413 empathy_log_manager_add_message (priv->log_manager,
414 gossip_chat_get_id (chat),
415 gossip_chat_is_group_chat (chat),
419 gossip_chat_view_append_message (chat->view, message);
421 if (gossip_chat_should_play_sound (chat)) {
422 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
425 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
429 chat_sent_message_add (GossipChat *chat,
432 GossipChatPriv *priv;
436 priv = GET_PRIV (chat);
438 /* Save the sent message in our repeat buffer */
439 list = priv->sent_messages;
441 /* Remove any other occurances of this msg */
442 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
443 list = g_slist_remove_link (list, item);
445 g_slist_free1 (item);
448 /* Trim the list to the last 10 items */
449 while (g_slist_length (list) > 10) {
450 item = g_slist_last (list);
452 list = g_slist_remove_link (list, item);
454 g_slist_free1 (item);
458 /* Add new message */
459 list = g_slist_prepend (list, g_strdup (str));
461 /* Set list and reset the index */
462 priv->sent_messages = list;
463 priv->sent_messages_index = -1;
467 chat_sent_message_get_next (GossipChat *chat)
469 GossipChatPriv *priv;
472 priv = GET_PRIV (chat);
474 if (!priv->sent_messages) {
475 gossip_debug (DEBUG_DOMAIN,
476 "No sent messages, next message is NULL");
480 max = g_slist_length (priv->sent_messages) - 1;
482 if (priv->sent_messages_index < max) {
483 priv->sent_messages_index++;
486 gossip_debug (DEBUG_DOMAIN,
487 "Returning next message index:%d",
488 priv->sent_messages_index);
490 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
494 chat_sent_message_get_last (GossipChat *chat)
496 GossipChatPriv *priv;
498 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
500 priv = GET_PRIV (chat);
502 if (!priv->sent_messages) {
503 gossip_debug (DEBUG_DOMAIN,
504 "No sent messages, last message is NULL");
508 if (priv->sent_messages_index >= 0) {
509 priv->sent_messages_index--;
512 gossip_debug (DEBUG_DOMAIN,
513 "Returning last message index:%d",
514 priv->sent_messages_index);
516 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
520 chat_input_key_press_event_cb (GtkWidget *widget,
524 GossipChatPriv *priv;
527 GtkWidget *text_view_sw;
529 priv = GET_PRIV (chat);
531 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
535 /* Catch ctrl+up/down so we can traverse messages we sent */
536 if ((event->state & GDK_CONTROL_MASK) &&
537 (event->keyval == GDK_Up ||
538 event->keyval == GDK_Down)) {
539 GtkTextBuffer *buffer;
542 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
544 if (event->keyval == GDK_Up) {
545 str = chat_sent_message_get_next (chat);
547 str = chat_sent_message_get_last (chat);
550 g_signal_handlers_block_by_func (buffer,
551 chat_input_text_buffer_changed_cb,
553 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
554 g_signal_handlers_unblock_by_func (buffer,
555 chat_input_text_buffer_changed_cb,
561 /* Catch enter but not ctrl/shift-enter */
562 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
565 /* This is to make sure that kinput2 gets the enter. And if
566 * it's handled there we shouldn't send on it. This is because
567 * kinput2 uses Enter to commit letters. See:
568 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
571 view = GTK_TEXT_VIEW (chat->input_text_view);
572 if (gtk_im_context_filter_keypress (view->im_context, event)) {
573 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
577 chat_input_text_view_send (chat);
581 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
583 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
584 /* Newline for shift-enter. */
587 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
588 event->keyval == GDK_Page_Up) {
589 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
590 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
594 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
595 event->keyval == GDK_Page_Down) {
596 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
597 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
598 gtk_adjustment_set_value (adj, val);
607 chat_text_view_focus_in_event_cb (GtkWidget *widget,
611 gtk_widget_grab_focus (chat->input_text_view);
617 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
620 GossipChatPriv *priv;
621 GtkTextIter start, end;
623 gboolean spell_checker = FALSE;
625 priv = GET_PRIV (chat);
627 if (gtk_text_buffer_get_char_count (buffer) == 0) {
628 chat_composing_stop (chat);
630 chat_composing_start (chat);
633 gossip_conf_get_bool (gossip_conf_get (),
634 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
637 if (chat->is_first_char) {
641 GtkAllocation *allocation;
643 /* Save the window's size */
644 dialog = gossip_chat_window_get_dialog (priv->window);
645 gtk_window_get_size (GTK_WINDOW (dialog),
646 NULL, &window_height);
648 gtk_widget_size_request (chat->input_text_view, &req);
650 allocation = >K_WIDGET (chat->view)->allocation;
652 priv->default_window_height = window_height;
653 priv->last_input_height = req.height;
654 priv->padding_height = window_height - req.height - allocation->height;
656 chat->is_first_char = FALSE;
659 gtk_text_buffer_get_start_iter (buffer, &start);
661 if (!spell_checker) {
662 gtk_text_buffer_get_end_iter (buffer, &end);
663 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
667 if (!gossip_spell_supported ()) {
671 /* NOTE: this is really inefficient, we shouldn't have to
672 reiterate the whole buffer each time and check each work
675 gboolean correct = FALSE;
678 if (gtk_text_iter_is_start (&start)) {
681 if (!gtk_text_iter_forward_word_end (&end)) {
682 /* no whole word yet */
686 if (!gtk_text_iter_forward_word_end (&end)) {
687 /* must be the end of the buffer */
692 gtk_text_iter_backward_word_start (&start);
695 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
697 /* spell check string */
698 if (!gossip_chat_get_is_command (str)) {
699 correct = gossip_spell_check (str);
705 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
707 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
712 /* set start iter to the end iters position */
724 chat_change_size_in_idle_cb (ChangeSizeData *data)
726 gtk_window_resize (GTK_WINDOW (data->window),
727 data->width, data->height);
733 chat_text_view_scroll_hide_cb (GtkWidget *widget,
736 GossipChatPriv *priv;
739 priv = GET_PRIV (chat);
741 priv->vscroll_visible = FALSE;
742 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
744 sw = gtk_widget_get_parent (chat->input_text_view);
745 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
748 g_object_set (sw, "height-request", -1, NULL);
752 chat_text_view_size_allocate_cb (GtkWidget *widget,
753 GtkAllocation *allocation,
756 GossipChatPriv *priv;
759 ChangeSizeData *data;
762 GtkAllocation *view_allocation;
767 priv = GET_PRIV (chat);
769 if (priv->default_window_height <= 0) {
773 sw = gtk_widget_get_parent (widget);
774 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
777 priv->vscroll_visible = TRUE;
778 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
779 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
781 GTK_POLICY_AUTOMATIC);
782 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
783 g_signal_connect (vscroll, "hide",
784 G_CALLBACK (chat_text_view_scroll_hide_cb),
788 if (priv->last_input_height <= allocation->height) {
789 priv->last_input_height = allocation->height;
793 diff = priv->last_input_height - allocation->height;
794 priv->last_input_height = allocation->height;
796 view_allocation = >K_WIDGET (chat->view)->allocation;
798 dialog = gossip_chat_window_get_dialog (priv->window);
799 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
801 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
803 if (new_height <= priv->default_window_height) {
804 window_height = priv->default_window_height;
806 window_height = new_height;
809 if (current_height <= window_height) {
813 /* Restore the window's size */
814 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
816 data = g_new0 (ChangeSizeData, 1);
817 data->window = dialog;
819 data->height = window_height;
821 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
822 (GSourceFunc) chat_change_size_in_idle_cb,
827 chat_text_view_realize_cb (GtkWidget *widget,
830 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
831 gtk_widget_grab_focus (widget);
835 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
838 GtkTextBuffer *buffer;
842 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
844 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
846 gtk_text_buffer_get_end_iter (buffer, &iter);
847 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
849 gtk_text_buffer_get_end_iter (buffer, &iter);
850 gtk_text_buffer_insert (buffer, &iter, " ", -1);
854 chat_text_populate_popup_cb (GtkTextView *view,
858 GossipChatPriv *priv;
859 GtkTextBuffer *buffer;
860 GtkTextTagTable *table;
863 GtkTextIter iter, start, end;
866 GossipChatSpell *chat_spell;
867 GtkWidget *smiley_menu;
869 priv = GET_PRIV (chat);
871 /* Add the emoticon menu. */
872 item = gtk_separator_menu_item_new ();
873 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
874 gtk_widget_show (item);
876 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
877 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
878 gtk_widget_show (item);
880 smiley_menu = gossip_chat_view_get_smiley_menu (
881 G_CALLBACK (chat_insert_smiley_activate_cb),
884 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
886 /* Add the spell check menu item. */
887 buffer = gtk_text_view_get_buffer (view);
888 table = gtk_text_buffer_get_tag_table (buffer);
890 tag = gtk_text_tag_table_lookup (table, "misspelled");
892 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
894 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
895 GTK_TEXT_WINDOW_WIDGET,
899 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
903 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
904 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
906 str = gtk_text_buffer_get_text (buffer,
907 &start, &end, FALSE);
910 if (G_STR_EMPTY (str)) {
914 chat_spell = chat_spell_new (chat, str, start, end);
916 g_object_set_data_full (G_OBJECT (menu),
917 "chat_spell", chat_spell,
918 (GDestroyNotify) chat_spell_free);
920 item = gtk_separator_menu_item_new ();
921 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
922 gtk_widget_show (item);
924 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
925 g_signal_connect (item,
927 G_CALLBACK (chat_text_check_word_spelling_cb),
929 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
930 gtk_widget_show (item);
934 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
935 GossipChatSpell *chat_spell)
937 gossip_spell_dialog_show (chat_spell->chat,
943 static GossipChatSpell *
944 chat_spell_new (GossipChat *chat,
949 GossipChatSpell *chat_spell;
951 chat_spell = g_new0 (GossipChatSpell, 1);
953 chat_spell->chat = g_object_ref (chat);
954 chat_spell->word = g_strdup (word);
955 chat_spell->start = start;
956 chat_spell->end = end;
962 chat_spell_free (GossipChatSpell *chat_spell)
964 g_object_unref (chat_spell->chat);
965 g_free (chat_spell->word);
970 chat_composing_start (GossipChat *chat)
972 GossipChatPriv *priv;
974 priv = GET_PRIV (chat);
976 if (priv->composing_stop_timeout_id) {
977 /* Just restart the timeout */
978 chat_composing_remove_timeout (chat);
980 empathy_tp_chat_set_state (priv->tp_chat,
981 TP_CHANNEL_CHAT_STATE_COMPOSING);
984 priv->composing_stop_timeout_id = g_timeout_add (
985 1000 * COMPOSING_STOP_TIMEOUT,
986 (GSourceFunc) chat_composing_stop_timeout_cb,
991 chat_composing_stop (GossipChat *chat)
993 GossipChatPriv *priv;
995 priv = GET_PRIV (chat);
997 chat_composing_remove_timeout (chat);
998 empathy_tp_chat_set_state (priv->tp_chat,
999 TP_CHANNEL_CHAT_STATE_ACTIVE);
1003 chat_composing_remove_timeout (GossipChat *chat)
1005 GossipChatPriv *priv;
1007 priv = GET_PRIV (chat);
1009 if (priv->composing_stop_timeout_id) {
1010 g_source_remove (priv->composing_stop_timeout_id);
1011 priv->composing_stop_timeout_id = 0;
1016 chat_composing_stop_timeout_cb (GossipChat *chat)
1018 GossipChatPriv *priv;
1020 priv = GET_PRIV (chat);
1022 priv->composing_stop_timeout_id = 0;
1023 empathy_tp_chat_set_state (priv->tp_chat,
1024 TP_CHANNEL_CHAT_STATE_PAUSED);
1030 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1031 GossipContact *contact,
1032 TelepathyChannelChatState state,
1035 GossipChatPriv *priv;
1037 gboolean was_composing;
1039 priv = GET_PRIV (chat);
1041 if (gossip_contact_is_user (contact)) {
1042 /* We don't care about our own chat state */
1046 was_composing = (priv->compositors != NULL);
1048 /* Find the contact in the list. After that l is the list elem or NULL */
1049 for (l = priv->compositors; l; l = l->next) {
1050 if (gossip_contact_equal (contact, l->data)) {
1056 case TP_CHANNEL_CHAT_STATE_GONE:
1057 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1058 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1059 /* Contact is not composing */
1061 priv->compositors = g_list_remove_link (priv->compositors, l);
1062 g_object_unref (l->data);
1066 case TP_CHANNEL_CHAT_STATE_PAUSED:
1067 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1068 /* Contact is composing */
1070 priv->compositors = g_list_prepend (priv->compositors,
1071 g_object_ref (contact));
1075 g_assert_not_reached ();
1078 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1079 was_composing ? "yes" : "no",
1080 priv->compositors ? "yes" : "no");
1082 if ((was_composing && !priv->compositors) ||
1083 (!was_composing && priv->compositors)) {
1084 /* Composing state changed */
1085 g_signal_emit (chat, signals[COMPOSING], 0,
1086 priv->compositors != NULL);
1091 chat_add_logs (GossipChat *chat)
1093 GossipChatPriv *priv;
1094 GList *messages, *l;
1098 priv = GET_PRIV (chat);
1100 /* Do not display backlog for chatrooms */
1101 if (gossip_chat_is_group_chat (chat)) {
1105 /* Turn off scrolling temporarily */
1106 gossip_chat_view_scroll (chat->view, FALSE);
1108 /* Add messages from last conversation */
1109 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1111 gossip_chat_get_id (chat),
1112 gossip_chat_is_group_chat (chat));
1113 num_messages = g_list_length (messages);
1115 for (l = messages, i = 0; l; l = l->next, i++) {
1116 GossipMessage *message;
1120 /* Only add 10 last messages */
1121 if (num_messages - i > 10) {
1122 g_object_unref (message);
1127 gossip_chat_view_append_message (chat->view, message);
1128 g_object_unref (message);
1130 g_list_free (messages);
1132 /* Turn back on scrolling */
1133 gossip_chat_view_scroll (chat->view, TRUE);
1135 /* Scroll to the most recent messages, we reference the chat
1136 * for the duration of the scroll func.
1138 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1139 g_object_ref (chat));
1142 /* Scroll down after the back-log has been received. */
1144 chat_scroll_down_idle_func (GossipChat *chat)
1146 GossipChatPriv *priv;
1148 priv = GET_PRIV (chat);
1150 gossip_chat_scroll_down (chat);
1151 g_object_unref (chat);
1153 priv->scroll_idle_id = 0;
1159 gossip_chat_get_is_command (const gchar *str)
1161 g_return_val_if_fail (str != NULL, FALSE);
1163 if (str[0] != '/') {
1167 if (g_str_has_prefix (str, "/me")) {
1170 else if (g_str_has_prefix (str, "/nick")) {
1173 else if (g_str_has_prefix (str, "/topic")) {
1181 gossip_chat_correct_word (GossipChat *chat,
1184 const gchar *new_word)
1186 GtkTextBuffer *buffer;
1188 g_return_if_fail (chat != NULL);
1189 g_return_if_fail (new_word != NULL);
1191 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1193 gtk_text_buffer_delete (buffer, &start, &end);
1194 gtk_text_buffer_insert (buffer, &start,
1200 gossip_chat_get_name (GossipChat *chat)
1202 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1204 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1205 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1212 gossip_chat_get_tooltip (GossipChat *chat)
1214 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1216 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1217 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1224 gossip_chat_get_status_icon_name (GossipChat *chat)
1226 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1228 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1229 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1236 gossip_chat_get_widget (GossipChat *chat)
1238 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1240 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1241 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1248 gossip_chat_is_group_chat (GossipChat *chat)
1250 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1252 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1253 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1260 gossip_chat_is_connected (GossipChat *chat)
1262 GossipChatPriv *priv;
1264 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1266 priv = GET_PRIV (chat);
1268 return (priv->tp_chat != NULL);
1272 gossip_chat_save_geometry (GossipChat *chat,
1278 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1282 gossip_chat_load_geometry (GossipChat *chat,
1288 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1292 gossip_chat_set_tp_chat (GossipChat *chat,
1293 EmpathyTpChat *tp_chat)
1295 GossipChatPriv *priv;
1297 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1298 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1300 priv = GET_PRIV (chat);
1302 if (tp_chat == priv->tp_chat) {
1306 if (priv->tp_chat) {
1307 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1308 chat_message_received_cb,
1310 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1313 g_object_unref (priv->tp_chat);
1317 priv->tp_chat = g_object_ref (tp_chat);
1318 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1319 priv->time_joined = gossip_time_get_current ();
1321 if (priv->first_tp_chat) {
1322 chat_add_logs (chat);
1323 priv->first_tp_chat = FALSE;
1326 g_signal_connect (tp_chat, "message-received",
1327 G_CALLBACK (chat_message_received_cb),
1329 g_signal_connect (tp_chat, "chat-state-changed",
1330 G_CALLBACK (chat_state_changed_cb),
1332 g_signal_connect (tp_chat, "destroy",
1333 G_CALLBACK (chat_destroy_cb),
1336 empathy_tp_chat_request_pending (tp_chat);
1338 if (!priv->sensitive) {
1339 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1340 gossip_chat_view_append_event (chat->view, _("Connected"));
1341 priv->sensitive = TRUE;
1344 if (GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat) {
1345 GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1351 gossip_chat_get_id (GossipChat *chat)
1353 GossipChatPriv *priv;
1355 priv = GET_PRIV (chat);
1361 gossip_chat_clear (GossipChat *chat)
1363 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1365 gossip_chat_view_clear (chat->view);
1369 gossip_chat_set_window (GossipChat *chat,
1370 GossipChatWindow *window)
1372 GossipChatPriv *priv;
1374 priv = GET_PRIV (chat);
1375 priv->window = window;
1379 gossip_chat_get_window (GossipChat *chat)
1381 GossipChatPriv *priv;
1383 priv = GET_PRIV (chat);
1385 return priv->window;
1389 gossip_chat_scroll_down (GossipChat *chat)
1391 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1393 gossip_chat_view_scroll_down (chat->view);
1397 gossip_chat_cut (GossipChat *chat)
1399 GtkTextBuffer *buffer;
1401 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1403 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1404 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1405 GtkClipboard *clipboard;
1407 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1409 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1414 gossip_chat_copy (GossipChat *chat)
1416 GtkTextBuffer *buffer;
1418 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1420 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1421 gossip_chat_view_copy_clipboard (chat->view);
1425 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1426 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1427 GtkClipboard *clipboard;
1429 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1431 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1436 gossip_chat_paste (GossipChat *chat)
1438 GtkTextBuffer *buffer;
1439 GtkClipboard *clipboard;
1441 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1443 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1444 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1446 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1450 gossip_chat_present (GossipChat *chat)
1452 GossipChatPriv *priv;
1454 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1456 priv = GET_PRIV (chat);
1458 if (priv->window == NULL) {
1459 GossipChatWindow *window;
1461 window = gossip_chat_window_get_default ();
1463 window = gossip_chat_window_new ();
1466 gossip_chat_window_add_chat (window, chat);
1469 gossip_chat_window_switch_to_chat (priv->window, chat);
1470 gossip_window_present (
1471 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1474 gtk_widget_grab_focus (chat->input_text_view);
1478 gossip_chat_should_play_sound (GossipChat *chat)
1480 GossipChatWindow *window;
1481 gboolean play = TRUE;
1483 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1485 window = gossip_chat_get_window (chat);
1490 play = !gossip_chat_window_has_focus (window);
1496 gossip_chat_should_highlight_nick (GossipMessage *message)
1498 GossipContact *contact;
1499 const gchar *msg, *to;
1500 gchar *cf_msg, *cf_to;
1504 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1506 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1510 msg = gossip_message_get_body (message);
1515 contact = gossip_message_get_receiver (message);
1516 if (!contact || !gossip_contact_is_user (contact)) {
1520 to = gossip_contact_get_name (contact);
1525 cf_msg = g_utf8_casefold (msg, -1);
1526 cf_to = g_utf8_casefold (to, -1);
1528 ch = strstr (cf_msg, cf_to);
1534 /* Not first in the message */
1535 if ((*(ch - 1) != ' ') &&
1536 (*(ch - 1) != ',') &&
1537 (*(ch - 1) != '.')) {
1542 ch = ch + strlen (cf_to);
1543 if (ch >= cf_msg + strlen (cf_msg)) {