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;
346 GossipContact *own_contact;
348 priv = GET_PRIV (chat);
350 if (G_STR_EMPTY (msg)) {
354 chat_sent_message_add (chat, msg);
356 if (g_str_has_prefix (msg, "/clear")) {
357 gossip_chat_view_clear (chat->view);
361 /* FIXME: add here something to let group/privrate chat handle
362 * some special messages */
364 own_contact = empathy_contact_manager_get_user (priv->manager, chat->account);
365 message = gossip_message_new (msg);
366 gossip_message_set_sender (message, own_contact);
368 empathy_tp_chat_send (priv->tp_chat, message);
370 g_object_unref (message);
374 chat_input_text_view_send (GossipChat *chat)
376 GossipChatPriv *priv;
377 GtkTextBuffer *buffer;
378 GtkTextIter start, end;
381 priv = GET_PRIV (chat);
383 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
385 gtk_text_buffer_get_bounds (buffer, &start, &end);
386 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
388 /* clear the input field */
389 gtk_text_buffer_set_text (buffer, "", -1);
391 chat_send (chat, msg);
395 chat->is_first_char = TRUE;
399 chat_message_received_cb (EmpathyTpChat *tp_chat,
400 GossipMessage *message,
403 GossipChatPriv *priv;
404 GossipContact *sender;
405 GossipTime timestamp;
407 priv = GET_PRIV (chat);
409 sender = gossip_message_get_sender (message);
410 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
411 gossip_contact_get_name (sender));
413 /* Log the message only if it's not backlog */
414 timestamp = gossip_message_get_timestamp (message);
415 if (timestamp >= priv->time_joined) {
416 empathy_log_manager_add_message (priv->log_manager,
417 gossip_chat_get_id (chat),
421 gossip_chat_view_append_message (chat->view, message);
423 if (gossip_chat_should_play_sound (chat)) {
424 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
427 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
431 chat_sent_message_add (GossipChat *chat,
434 GossipChatPriv *priv;
438 priv = GET_PRIV (chat);
440 /* Save the sent message in our repeat buffer */
441 list = priv->sent_messages;
443 /* Remove any other occurances of this msg */
444 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
445 list = g_slist_remove_link (list, item);
447 g_slist_free1 (item);
450 /* Trim the list to the last 10 items */
451 while (g_slist_length (list) > 10) {
452 item = g_slist_last (list);
454 list = g_slist_remove_link (list, item);
456 g_slist_free1 (item);
460 /* Add new message */
461 list = g_slist_prepend (list, g_strdup (str));
463 /* Set list and reset the index */
464 priv->sent_messages = list;
465 priv->sent_messages_index = -1;
469 chat_sent_message_get_next (GossipChat *chat)
471 GossipChatPriv *priv;
474 priv = GET_PRIV (chat);
476 if (!priv->sent_messages) {
477 gossip_debug (DEBUG_DOMAIN,
478 "No sent messages, next message is NULL");
482 max = g_slist_length (priv->sent_messages) - 1;
484 if (priv->sent_messages_index < max) {
485 priv->sent_messages_index++;
488 gossip_debug (DEBUG_DOMAIN,
489 "Returning next message index:%d",
490 priv->sent_messages_index);
492 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
496 chat_sent_message_get_last (GossipChat *chat)
498 GossipChatPriv *priv;
500 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
502 priv = GET_PRIV (chat);
504 if (!priv->sent_messages) {
505 gossip_debug (DEBUG_DOMAIN,
506 "No sent messages, last message is NULL");
510 if (priv->sent_messages_index >= 0) {
511 priv->sent_messages_index--;
514 gossip_debug (DEBUG_DOMAIN,
515 "Returning last message index:%d",
516 priv->sent_messages_index);
518 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
522 chat_input_key_press_event_cb (GtkWidget *widget,
526 GossipChatPriv *priv;
529 GtkWidget *text_view_sw;
531 priv = GET_PRIV (chat);
533 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
537 /* Catch ctrl+up/down so we can traverse messages we sent */
538 if ((event->state & GDK_CONTROL_MASK) &&
539 (event->keyval == GDK_Up ||
540 event->keyval == GDK_Down)) {
541 GtkTextBuffer *buffer;
544 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
546 if (event->keyval == GDK_Up) {
547 str = chat_sent_message_get_next (chat);
549 str = chat_sent_message_get_last (chat);
552 g_signal_handlers_block_by_func (buffer,
553 chat_input_text_buffer_changed_cb,
555 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
556 g_signal_handlers_unblock_by_func (buffer,
557 chat_input_text_buffer_changed_cb,
563 /* Catch enter but not ctrl/shift-enter */
564 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
567 /* This is to make sure that kinput2 gets the enter. And if
568 * it's handled there we shouldn't send on it. This is because
569 * kinput2 uses Enter to commit letters. See:
570 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
573 view = GTK_TEXT_VIEW (chat->input_text_view);
574 if (gtk_im_context_filter_keypress (view->im_context, event)) {
575 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
579 chat_input_text_view_send (chat);
583 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
585 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
586 /* Newline for shift-enter. */
589 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
590 event->keyval == GDK_Page_Up) {
591 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
592 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
596 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
597 event->keyval == GDK_Page_Down) {
598 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
599 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
600 gtk_adjustment_set_value (adj, val);
609 chat_text_view_focus_in_event_cb (GtkWidget *widget,
613 gtk_widget_grab_focus (chat->input_text_view);
619 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
622 GossipChatPriv *priv;
623 GtkTextIter start, end;
625 gboolean spell_checker = FALSE;
627 priv = GET_PRIV (chat);
629 if (gtk_text_buffer_get_char_count (buffer) == 0) {
630 chat_composing_stop (chat);
632 chat_composing_start (chat);
635 gossip_conf_get_bool (gossip_conf_get (),
636 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
639 if (chat->is_first_char) {
643 GtkAllocation *allocation;
645 /* Save the window's size */
646 dialog = gossip_chat_window_get_dialog (priv->window);
647 gtk_window_get_size (GTK_WINDOW (dialog),
648 NULL, &window_height);
650 gtk_widget_size_request (chat->input_text_view, &req);
652 allocation = >K_WIDGET (chat->view)->allocation;
654 priv->default_window_height = window_height;
655 priv->last_input_height = req.height;
656 priv->padding_height = window_height - req.height - allocation->height;
658 chat->is_first_char = FALSE;
661 gtk_text_buffer_get_start_iter (buffer, &start);
663 if (!spell_checker) {
664 gtk_text_buffer_get_end_iter (buffer, &end);
665 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
669 if (!gossip_spell_supported ()) {
673 /* NOTE: this is really inefficient, we shouldn't have to
674 reiterate the whole buffer each time and check each work
677 gboolean correct = FALSE;
680 if (gtk_text_iter_is_start (&start)) {
683 if (!gtk_text_iter_forward_word_end (&end)) {
684 /* no whole word yet */
688 if (!gtk_text_iter_forward_word_end (&end)) {
689 /* must be the end of the buffer */
694 gtk_text_iter_backward_word_start (&start);
697 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
699 /* spell check string */
700 if (!gossip_chat_get_is_command (str)) {
701 correct = gossip_spell_check (str);
707 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
709 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
714 /* set start iter to the end iters position */
726 chat_change_size_in_idle_cb (ChangeSizeData *data)
728 gtk_window_resize (GTK_WINDOW (data->window),
729 data->width, data->height);
735 chat_text_view_scroll_hide_cb (GtkWidget *widget,
738 GossipChatPriv *priv;
741 priv = GET_PRIV (chat);
743 priv->vscroll_visible = FALSE;
744 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
746 sw = gtk_widget_get_parent (chat->input_text_view);
747 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
750 g_object_set (sw, "height-request", -1, NULL);
754 chat_text_view_size_allocate_cb (GtkWidget *widget,
755 GtkAllocation *allocation,
758 GossipChatPriv *priv;
761 ChangeSizeData *data;
764 GtkAllocation *view_allocation;
769 priv = GET_PRIV (chat);
771 if (priv->default_window_height <= 0) {
775 sw = gtk_widget_get_parent (widget);
776 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
779 priv->vscroll_visible = TRUE;
780 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
781 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
783 GTK_POLICY_AUTOMATIC);
784 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
785 g_signal_connect (vscroll, "hide",
786 G_CALLBACK (chat_text_view_scroll_hide_cb),
790 if (priv->last_input_height <= allocation->height) {
791 priv->last_input_height = allocation->height;
795 diff = priv->last_input_height - allocation->height;
796 priv->last_input_height = allocation->height;
798 view_allocation = >K_WIDGET (chat->view)->allocation;
800 dialog = gossip_chat_window_get_dialog (priv->window);
801 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
803 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
805 if (new_height <= priv->default_window_height) {
806 window_height = priv->default_window_height;
808 window_height = new_height;
811 if (current_height <= window_height) {
815 /* Restore the window's size */
816 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
818 data = g_new0 (ChangeSizeData, 1);
819 data->window = dialog;
821 data->height = window_height;
823 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
824 (GSourceFunc) chat_change_size_in_idle_cb,
829 chat_text_view_realize_cb (GtkWidget *widget,
832 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
833 gtk_widget_grab_focus (widget);
837 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
840 GtkTextBuffer *buffer;
844 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
846 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
848 gtk_text_buffer_get_end_iter (buffer, &iter);
849 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
851 gtk_text_buffer_get_end_iter (buffer, &iter);
852 gtk_text_buffer_insert (buffer, &iter, " ", -1);
856 chat_text_populate_popup_cb (GtkTextView *view,
860 GossipChatPriv *priv;
861 GtkTextBuffer *buffer;
862 GtkTextTagTable *table;
865 GtkTextIter iter, start, end;
868 GossipChatSpell *chat_spell;
869 GtkWidget *smiley_menu;
871 priv = GET_PRIV (chat);
873 /* Add the emoticon menu. */
874 item = gtk_separator_menu_item_new ();
875 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
876 gtk_widget_show (item);
878 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
879 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
880 gtk_widget_show (item);
882 smiley_menu = gossip_chat_view_get_smiley_menu (
883 G_CALLBACK (chat_insert_smiley_activate_cb),
886 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
888 /* Add the spell check menu item. */
889 buffer = gtk_text_view_get_buffer (view);
890 table = gtk_text_buffer_get_tag_table (buffer);
892 tag = gtk_text_tag_table_lookup (table, "misspelled");
894 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
896 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
897 GTK_TEXT_WINDOW_WIDGET,
901 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
905 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
906 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
908 str = gtk_text_buffer_get_text (buffer,
909 &start, &end, FALSE);
912 if (G_STR_EMPTY (str)) {
916 chat_spell = chat_spell_new (chat, str, start, end);
918 g_object_set_data_full (G_OBJECT (menu),
919 "chat_spell", chat_spell,
920 (GDestroyNotify) chat_spell_free);
922 item = gtk_separator_menu_item_new ();
923 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
924 gtk_widget_show (item);
926 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
927 g_signal_connect (item,
929 G_CALLBACK (chat_text_check_word_spelling_cb),
931 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
932 gtk_widget_show (item);
936 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
937 GossipChatSpell *chat_spell)
939 gossip_spell_dialog_show (chat_spell->chat,
945 static GossipChatSpell *
946 chat_spell_new (GossipChat *chat,
951 GossipChatSpell *chat_spell;
953 chat_spell = g_new0 (GossipChatSpell, 1);
955 chat_spell->chat = g_object_ref (chat);
956 chat_spell->word = g_strdup (word);
957 chat_spell->start = start;
958 chat_spell->end = end;
964 chat_spell_free (GossipChatSpell *chat_spell)
966 g_object_unref (chat_spell->chat);
967 g_free (chat_spell->word);
972 chat_composing_start (GossipChat *chat)
974 GossipChatPriv *priv;
976 priv = GET_PRIV (chat);
978 if (priv->composing_stop_timeout_id) {
979 /* Just restart the timeout */
980 chat_composing_remove_timeout (chat);
982 empathy_tp_chat_set_state (priv->tp_chat,
983 TP_CHANNEL_CHAT_STATE_COMPOSING);
986 priv->composing_stop_timeout_id = g_timeout_add (
987 1000 * COMPOSING_STOP_TIMEOUT,
988 (GSourceFunc) chat_composing_stop_timeout_cb,
993 chat_composing_stop (GossipChat *chat)
995 GossipChatPriv *priv;
997 priv = GET_PRIV (chat);
999 chat_composing_remove_timeout (chat);
1000 empathy_tp_chat_set_state (priv->tp_chat,
1001 TP_CHANNEL_CHAT_STATE_ACTIVE);
1005 chat_composing_remove_timeout (GossipChat *chat)
1007 GossipChatPriv *priv;
1009 priv = GET_PRIV (chat);
1011 if (priv->composing_stop_timeout_id) {
1012 g_source_remove (priv->composing_stop_timeout_id);
1013 priv->composing_stop_timeout_id = 0;
1018 chat_composing_stop_timeout_cb (GossipChat *chat)
1020 GossipChatPriv *priv;
1022 priv = GET_PRIV (chat);
1024 priv->composing_stop_timeout_id = 0;
1025 empathy_tp_chat_set_state (priv->tp_chat,
1026 TP_CHANNEL_CHAT_STATE_PAUSED);
1032 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1033 GossipContact *contact,
1034 TelepathyChannelChatState state,
1037 GossipChatPriv *priv;
1038 GossipContact *own_contact;
1040 gboolean was_composing;
1042 priv = GET_PRIV (chat);
1044 own_contact = gossip_contact_get_user (contact);
1045 if (gossip_contact_equal (own_contact, contact)) {
1046 /* We don't care about our own chat state */
1050 was_composing = (priv->compositors != NULL);
1052 /* Find the contact in the list. After that l is the list elem or NULL */
1053 for (l = priv->compositors; l; l = l->next) {
1054 if (gossip_contact_equal (contact, l->data)) {
1060 case TP_CHANNEL_CHAT_STATE_GONE:
1061 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1062 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1063 /* Contact is not composing */
1065 priv->compositors = g_list_remove_link (priv->compositors, l);
1066 g_object_unref (l->data);
1070 case TP_CHANNEL_CHAT_STATE_PAUSED:
1071 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1072 /* Contact is composing */
1074 priv->compositors = g_list_prepend (priv->compositors,
1075 g_object_ref (contact));
1079 g_assert_not_reached ();
1082 gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1083 was_composing ? "yes" : "no",
1084 priv->compositors ? "yes" : "no");
1086 if ((was_composing && !priv->compositors) ||
1087 (!was_composing && priv->compositors)) {
1088 /* Composing state changed */
1089 g_signal_emit (chat, signals[COMPOSING], 0,
1090 priv->compositors != NULL);
1095 chat_add_logs (GossipChat *chat)
1097 GossipChatPriv *priv;
1098 GList *messages, *l;
1102 priv = GET_PRIV (chat);
1104 /* Do not display backlog for chatrooms */
1105 if (gossip_chat_is_group_chat (chat)) {
1109 /* Turn off scrolling temporarily */
1110 gossip_chat_view_scroll (chat->view, FALSE);
1112 /* Add messages from last conversation */
1113 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1115 gossip_chat_get_id (chat));
1116 num_messages = g_list_length (messages);
1118 for (l = messages, i = 0; l; l = l->next, i++) {
1119 GossipMessage *message;
1123 /* Only add 10 last messages */
1124 if (num_messages - i > 10) {
1125 g_object_unref (message);
1130 gossip_chat_view_append_message (chat->view, message);
1131 g_object_unref (message);
1133 g_list_free (messages);
1135 /* Turn back on scrolling */
1136 gossip_chat_view_scroll (chat->view, TRUE);
1138 /* Scroll to the most recent messages, we reference the chat
1139 * for the duration of the scroll func.
1141 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1142 g_object_ref (chat));
1145 /* Scroll down after the back-log has been received. */
1147 chat_scroll_down_idle_func (GossipChat *chat)
1149 GossipChatPriv *priv;
1151 priv = GET_PRIV (chat);
1153 gossip_chat_scroll_down (chat);
1154 g_object_unref (chat);
1156 priv->scroll_idle_id = 0;
1162 gossip_chat_get_is_command (const gchar *str)
1164 g_return_val_if_fail (str != NULL, FALSE);
1166 if (str[0] != '/') {
1170 if (g_str_has_prefix (str, "/me")) {
1173 else if (g_str_has_prefix (str, "/nick")) {
1176 else if (g_str_has_prefix (str, "/topic")) {
1184 gossip_chat_correct_word (GossipChat *chat,
1187 const gchar *new_word)
1189 GtkTextBuffer *buffer;
1191 g_return_if_fail (chat != NULL);
1192 g_return_if_fail (new_word != NULL);
1194 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1196 gtk_text_buffer_delete (buffer, &start, &end);
1197 gtk_text_buffer_insert (buffer, &start,
1203 gossip_chat_get_name (GossipChat *chat)
1205 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1207 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1208 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1215 gossip_chat_get_tooltip (GossipChat *chat)
1217 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1219 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1220 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1227 gossip_chat_get_status_icon_name (GossipChat *chat)
1229 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1231 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1232 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1239 gossip_chat_get_widget (GossipChat *chat)
1241 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1243 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1244 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1251 gossip_chat_is_group_chat (GossipChat *chat)
1253 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1255 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1256 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1263 gossip_chat_is_connected (GossipChat *chat)
1265 GossipChatPriv *priv;
1267 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1269 priv = GET_PRIV (chat);
1271 return (priv->tp_chat != NULL);
1275 gossip_chat_save_geometry (GossipChat *chat,
1281 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1285 gossip_chat_load_geometry (GossipChat *chat,
1291 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1295 gossip_chat_set_tp_chat (GossipChat *chat,
1296 EmpathyTpChat *tp_chat)
1298 GossipChatPriv *priv;
1300 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1301 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1303 priv = GET_PRIV (chat);
1305 if (tp_chat == priv->tp_chat) {
1309 if (priv->tp_chat) {
1310 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1311 chat_message_received_cb,
1313 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1316 g_object_unref (priv->tp_chat);
1320 priv->tp_chat = g_object_ref (tp_chat);
1321 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1322 priv->time_joined = gossip_time_get_current ();
1324 if (priv->first_tp_chat) {
1325 chat_add_logs (chat);
1326 priv->first_tp_chat = FALSE;
1329 g_signal_connect (tp_chat, "message-received",
1330 G_CALLBACK (chat_message_received_cb),
1332 g_signal_connect (tp_chat, "chat-state-changed",
1333 G_CALLBACK (chat_state_changed_cb),
1335 g_signal_connect (tp_chat, "destroy",
1336 G_CALLBACK (chat_destroy_cb),
1339 empathy_tp_chat_request_pending (tp_chat);
1341 if (!priv->sensitive) {
1342 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1343 gossip_chat_view_append_event (chat->view, _("Connected"));
1344 priv->sensitive = TRUE;
1347 if (GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat) {
1348 GOSSIP_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1354 gossip_chat_get_id (GossipChat *chat)
1356 GossipChatPriv *priv;
1358 priv = GET_PRIV (chat);
1364 gossip_chat_clear (GossipChat *chat)
1366 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1368 gossip_chat_view_clear (chat->view);
1372 gossip_chat_set_window (GossipChat *chat,
1373 GossipChatWindow *window)
1375 GossipChatPriv *priv;
1377 priv = GET_PRIV (chat);
1378 priv->window = window;
1382 gossip_chat_get_window (GossipChat *chat)
1384 GossipChatPriv *priv;
1386 priv = GET_PRIV (chat);
1388 return priv->window;
1392 gossip_chat_scroll_down (GossipChat *chat)
1394 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1396 gossip_chat_view_scroll_down (chat->view);
1400 gossip_chat_cut (GossipChat *chat)
1402 GtkTextBuffer *buffer;
1404 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1406 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1407 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1408 GtkClipboard *clipboard;
1410 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1412 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1417 gossip_chat_copy (GossipChat *chat)
1419 GtkTextBuffer *buffer;
1421 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1423 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1424 gossip_chat_view_copy_clipboard (chat->view);
1428 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1429 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1430 GtkClipboard *clipboard;
1432 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1434 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1439 gossip_chat_paste (GossipChat *chat)
1441 GtkTextBuffer *buffer;
1442 GtkClipboard *clipboard;
1444 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1446 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1447 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1449 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1453 gossip_chat_present (GossipChat *chat)
1455 GossipChatPriv *priv;
1457 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1459 priv = GET_PRIV (chat);
1461 if (priv->window == NULL) {
1462 GossipChatWindow *window;
1464 window = gossip_chat_window_get_default ();
1466 window = gossip_chat_window_new ();
1469 gossip_chat_window_add_chat (window, chat);
1472 gossip_chat_window_switch_to_chat (priv->window, chat);
1473 gossip_window_present (
1474 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1477 gtk_widget_grab_focus (chat->input_text_view);
1481 gossip_chat_should_play_sound (GossipChat *chat)
1483 GossipChatWindow *window;
1484 gboolean play = TRUE;
1486 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1488 window = gossip_chat_get_window (chat);
1493 play = !gossip_chat_window_has_focus (window);
1499 gossip_chat_should_highlight_nick (GossipMessage *message)
1501 GossipContact *my_contact;
1502 const gchar *msg, *to;
1503 gchar *cf_msg, *cf_to;
1507 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1509 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1513 msg = gossip_message_get_body (message);
1518 my_contact = gossip_contact_get_user (gossip_message_get_sender (message));
1519 to = gossip_contact_get_name (my_contact);
1524 cf_msg = g_utf8_casefold (msg, -1);
1525 cf_to = g_utf8_casefold (to, -1);
1527 ch = strstr (cf_msg, cf_to);
1533 /* Not first in the message */
1534 if ((*(ch - 1) != ' ') &&
1535 (*(ch - 1) != ',') &&
1536 (*(ch - 1) != '.')) {
1541 ch = ch + strlen (cf_to);
1542 if (ch >= cf_msg + strlen (cf_msg)) {