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>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-log-manager.h>
39 #include <libempathy/empathy-debug.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-conf.h>
42 #include <libempathy/empathy-marshal.h>
44 #include "empathy-chat.h"
45 #include "empathy-chat-window.h"
46 #include "empathy-geometry.h"
47 #include "empathy-preferences.h"
48 #include "empathy-spell.h"
49 #include "empathy-spell-dialog.h"
50 #include "empathy-ui-utils.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
54 #define DEBUG_DOMAIN "Chat"
56 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
59 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
63 #define COMPOSING_STOP_TIMEOUT 5
65 struct _EmpathyChatPriv {
66 EmpathyContactManager *manager;
67 EmpathyLogManager *log_manager;
68 EmpathyTpChat *tp_chat;
69 EmpathyChatWindow *window;
70 GtkTooltips *tooltips;
71 guint composing_stop_timeout_id;
74 GSList *sent_messages;
75 gint sent_messages_index;
78 gboolean first_tp_chat;
79 EmpathyTime last_log_timestamp;
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 empathy_chat_class_init (EmpathyChatClass *klass);
98 static void empathy_chat_init (EmpathyChat *chat);
99 static void chat_finalize (GObject *object);
100 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
102 static void chat_send (EmpathyChat *chat,
104 static void chat_input_text_view_send (EmpathyChat *chat);
105 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
106 EmpathyMessage *message,
108 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
109 EmpathyMessage *message,
110 TelepathyChannelTextSendError error_code,
112 void chat_sent_message_add (EmpathyChat *chat,
114 const gchar * chat_sent_message_get_next (EmpathyChat *chat);
115 const gchar * chat_sent_message_get_last (EmpathyChat *chat);
116 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
119 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
121 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
124 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
126 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
127 GtkAllocation *allocation,
129 static void chat_text_view_realize_cb (GtkWidget *widget,
131 static void chat_text_populate_popup_cb (GtkTextView *view,
134 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
135 EmpathyChatSpell *chat_spell);
136 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
140 static void chat_spell_free (EmpathyChatSpell *chat_spell);
141 static void chat_composing_start (EmpathyChat *chat);
142 static void chat_composing_stop (EmpathyChat *chat);
143 static void chat_composing_remove_timeout (EmpathyChat *chat);
144 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
145 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
146 EmpathyContact *contact,
147 TelepathyChannelChatState state,
149 static void chat_add_logs (EmpathyChat *chat);
150 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
160 static guint signals[LAST_SIGNAL] = { 0 };
162 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
165 empathy_chat_class_init (EmpathyChatClass *klass)
167 GObjectClass *object_class;
169 object_class = G_OBJECT_CLASS (klass);
171 object_class->finalize = chat_finalize;
174 g_signal_new ("composing",
175 G_OBJECT_CLASS_TYPE (object_class),
179 g_cclosure_marshal_VOID__BOOLEAN,
183 signals[NEW_MESSAGE] =
184 g_signal_new ("new-message",
185 G_OBJECT_CLASS_TYPE (object_class),
189 empathy_marshal_VOID__OBJECT_BOOLEAN,
191 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
193 signals[NAME_CHANGED] =
194 g_signal_new ("name-changed",
195 G_OBJECT_CLASS_TYPE (object_class),
199 g_cclosure_marshal_VOID__POINTER,
203 signals[STATUS_CHANGED] =
204 g_signal_new ("status-changed",
205 G_OBJECT_CLASS_TYPE (object_class),
209 g_cclosure_marshal_VOID__VOID,
213 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
217 empathy_chat_init (EmpathyChat *chat)
219 EmpathyChatPriv *priv;
220 GtkTextBuffer *buffer;
222 chat->view = empathy_chat_view_new ();
223 chat->input_text_view = gtk_text_view_new ();
225 chat->is_first_char = TRUE;
227 g_object_set (chat->input_text_view,
228 "pixels-above-lines", 2,
229 "pixels-below-lines", 2,
230 "pixels-inside-wrap", 1,
233 "wrap-mode", GTK_WRAP_WORD_CHAR,
236 priv = GET_PRIV (chat);
238 priv->manager = empathy_contact_manager_new ();
239 priv->log_manager = empathy_log_manager_new ();
240 priv->tooltips = g_object_ref_sink (gtk_tooltips_new ());
241 priv->default_window_height = -1;
242 priv->vscroll_visible = FALSE;
243 priv->sensitive = TRUE;
244 priv->sent_messages = NULL;
245 priv->sent_messages_index = -1;
246 priv->first_tp_chat = TRUE;
248 g_signal_connect (chat->input_text_view,
250 G_CALLBACK (chat_input_key_press_event_cb),
253 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
254 g_signal_connect (buffer,
256 G_CALLBACK (chat_input_text_buffer_changed_cb),
258 g_signal_connect (chat->view,
260 G_CALLBACK (chat_text_view_focus_in_event_cb),
263 g_signal_connect (chat->input_text_view,
265 G_CALLBACK (chat_text_view_size_allocate_cb),
268 g_signal_connect (chat->input_text_view,
270 G_CALLBACK (chat_text_view_realize_cb),
273 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
275 G_CALLBACK (chat_text_populate_popup_cb),
278 /* create misspelt words identification tag */
279 gtk_text_buffer_create_tag (buffer,
281 "underline", PANGO_UNDERLINE_ERROR,
286 chat_finalize (GObject *object)
289 EmpathyChatPriv *priv;
291 chat = EMPATHY_CHAT (object);
292 priv = GET_PRIV (chat);
294 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
296 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
297 g_slist_free (priv->sent_messages);
299 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
300 g_list_free (priv->compositors);
302 chat_composing_remove_timeout (chat);
303 g_object_unref (chat->account);
304 g_object_unref (priv->manager);
305 g_object_unref (priv->log_manager);
306 g_object_unref (priv->tooltips);
309 g_object_unref (priv->tp_chat);
312 if (priv->scroll_idle_id) {
313 g_source_remove (priv->scroll_idle_id);
318 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
322 chat_destroy_cb (EmpathyTpChat *tp_chat,
325 EmpathyChatPriv *priv;
327 priv = GET_PRIV (chat);
330 g_object_unref (priv->tp_chat);
331 priv->tp_chat = NULL;
333 priv->sensitive = FALSE;
335 empathy_chat_view_append_event (chat->view, _("Disconnected"));
336 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
338 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
339 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
344 chat_send (EmpathyChat *chat,
347 EmpathyChatPriv *priv;
348 EmpathyMessage *message;
350 priv = GET_PRIV (chat);
352 if (G_STR_EMPTY (msg)) {
356 chat_sent_message_add (chat, msg);
358 if (g_str_has_prefix (msg, "/clear")) {
359 empathy_chat_view_clear (chat->view);
363 /* FIXME: add here something to let group/privrate chat handle
364 * some special messages */
366 message = empathy_message_new (msg);
368 empathy_tp_chat_send (priv->tp_chat, message);
370 g_object_unref (message);
374 chat_input_text_view_send (EmpathyChat *chat)
376 EmpathyChatPriv *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 EmpathyMessage *message,
403 EmpathyChatPriv *priv;
404 EmpathyContact *sender;
405 EmpathyTime timestamp;
407 priv = GET_PRIV (chat);
409 timestamp = empathy_message_get_timestamp (message);
410 if (timestamp <= priv->last_log_timestamp) {
411 /* Do not take care of messages anterior of the last
412 * logged message. Some Jabber chatroom sends messages
413 * received before we joined the room, this avoid
414 * displaying those messages if we already logged them
415 * last time we joined that room. */
416 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
417 "anterior of last logged message.");
421 sender = empathy_message_get_sender (message);
422 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
423 empathy_contact_get_name (sender));
425 empathy_log_manager_add_message (priv->log_manager,
426 empathy_chat_get_id (chat),
427 empathy_chat_is_group_chat (chat),
430 empathy_chat_view_append_message (chat->view, message);
432 if (empathy_chat_should_play_sound (chat)) {
433 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
436 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
440 chat_send_error_cb (EmpathyTpChat *tp_chat,
441 EmpathyMessage *message,
442 TelepathyChannelTextSendError error_code,
448 switch (error_code) {
449 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
450 error = _("offline");
452 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
453 error = _("invalid contact");
455 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
456 error = _("permission denied");
458 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
459 error = _("too long message");
461 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
462 error = _("not implemented");
465 error = _("unknown");
469 str = g_strdup_printf (_("Error sending message '%s': %s"),
470 empathy_message_get_body (message),
472 empathy_chat_view_append_event (chat->view, str);
477 chat_sent_message_add (EmpathyChat *chat,
480 EmpathyChatPriv *priv;
484 priv = GET_PRIV (chat);
486 /* Save the sent message in our repeat buffer */
487 list = priv->sent_messages;
489 /* Remove any other occurances of this msg */
490 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
491 list = g_slist_remove_link (list, item);
493 g_slist_free1 (item);
496 /* Trim the list to the last 10 items */
497 while (g_slist_length (list) > 10) {
498 item = g_slist_last (list);
500 list = g_slist_remove_link (list, item);
502 g_slist_free1 (item);
506 /* Add new message */
507 list = g_slist_prepend (list, g_strdup (str));
509 /* Set list and reset the index */
510 priv->sent_messages = list;
511 priv->sent_messages_index = -1;
515 chat_sent_message_get_next (EmpathyChat *chat)
517 EmpathyChatPriv *priv;
520 priv = GET_PRIV (chat);
522 if (!priv->sent_messages) {
523 empathy_debug (DEBUG_DOMAIN,
524 "No sent messages, next message is NULL");
528 max = g_slist_length (priv->sent_messages) - 1;
530 if (priv->sent_messages_index < max) {
531 priv->sent_messages_index++;
534 empathy_debug (DEBUG_DOMAIN,
535 "Returning next message index:%d",
536 priv->sent_messages_index);
538 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
542 chat_sent_message_get_last (EmpathyChat *chat)
544 EmpathyChatPriv *priv;
546 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
548 priv = GET_PRIV (chat);
550 if (!priv->sent_messages) {
551 empathy_debug (DEBUG_DOMAIN,
552 "No sent messages, last message is NULL");
556 if (priv->sent_messages_index >= 0) {
557 priv->sent_messages_index--;
560 empathy_debug (DEBUG_DOMAIN,
561 "Returning last message index:%d",
562 priv->sent_messages_index);
564 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
568 chat_input_key_press_event_cb (GtkWidget *widget,
572 EmpathyChatPriv *priv;
575 GtkWidget *text_view_sw;
577 priv = GET_PRIV (chat);
579 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
583 /* Catch ctrl+up/down so we can traverse messages we sent */
584 if ((event->state & GDK_CONTROL_MASK) &&
585 (event->keyval == GDK_Up ||
586 event->keyval == GDK_Down)) {
587 GtkTextBuffer *buffer;
590 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
592 if (event->keyval == GDK_Up) {
593 str = chat_sent_message_get_next (chat);
595 str = chat_sent_message_get_last (chat);
598 g_signal_handlers_block_by_func (buffer,
599 chat_input_text_buffer_changed_cb,
601 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
602 g_signal_handlers_unblock_by_func (buffer,
603 chat_input_text_buffer_changed_cb,
609 /* Catch enter but not ctrl/shift-enter */
610 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
613 /* This is to make sure that kinput2 gets the enter. And if
614 * it's handled there we shouldn't send on it. This is because
615 * kinput2 uses Enter to commit letters. See:
616 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
619 view = GTK_TEXT_VIEW (chat->input_text_view);
620 if (gtk_im_context_filter_keypress (view->im_context, event)) {
621 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
625 chat_input_text_view_send (chat);
629 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
631 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
632 /* Newline for shift-enter. */
635 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
636 event->keyval == GDK_Page_Up) {
637 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
638 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
642 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
643 event->keyval == GDK_Page_Down) {
644 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
645 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
646 gtk_adjustment_set_value (adj, val);
655 chat_text_view_focus_in_event_cb (GtkWidget *widget,
659 gtk_widget_grab_focus (chat->input_text_view);
665 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
668 EmpathyChatPriv *priv;
669 GtkTextIter start, end;
671 gboolean spell_checker = FALSE;
673 priv = GET_PRIV (chat);
675 if (gtk_text_buffer_get_char_count (buffer) == 0) {
676 chat_composing_stop (chat);
678 chat_composing_start (chat);
681 empathy_conf_get_bool (empathy_conf_get (),
682 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
685 if (chat->is_first_char) {
689 GtkAllocation *allocation;
691 /* Save the window's size */
692 dialog = empathy_chat_window_get_dialog (priv->window);
693 gtk_window_get_size (GTK_WINDOW (dialog),
694 NULL, &window_height);
696 gtk_widget_size_request (chat->input_text_view, &req);
698 allocation = >K_WIDGET (chat->view)->allocation;
700 priv->default_window_height = window_height;
701 priv->last_input_height = req.height;
702 priv->padding_height = window_height - req.height - allocation->height;
704 chat->is_first_char = FALSE;
707 gtk_text_buffer_get_start_iter (buffer, &start);
709 if (!spell_checker) {
710 gtk_text_buffer_get_end_iter (buffer, &end);
711 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
715 if (!empathy_spell_supported ()) {
719 /* NOTE: this is really inefficient, we shouldn't have to
720 reiterate the whole buffer each time and check each work
723 gboolean correct = FALSE;
726 if (gtk_text_iter_is_start (&start)) {
729 if (!gtk_text_iter_forward_word_end (&end)) {
730 /* no whole word yet */
734 if (!gtk_text_iter_forward_word_end (&end)) {
735 /* must be the end of the buffer */
740 gtk_text_iter_backward_word_start (&start);
743 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
745 /* spell check string */
746 if (!empathy_chat_get_is_command (str)) {
747 correct = empathy_spell_check (str);
753 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
755 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
760 /* set start iter to the end iters position */
772 chat_change_size_in_idle_cb (ChangeSizeData *data)
774 gtk_window_resize (GTK_WINDOW (data->window),
775 data->width, data->height);
781 chat_text_view_scroll_hide_cb (GtkWidget *widget,
784 EmpathyChatPriv *priv;
787 priv = GET_PRIV (chat);
789 priv->vscroll_visible = FALSE;
790 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
792 sw = gtk_widget_get_parent (chat->input_text_view);
793 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
796 g_object_set (sw, "height-request", -1, NULL);
800 chat_text_view_size_allocate_cb (GtkWidget *widget,
801 GtkAllocation *allocation,
804 EmpathyChatPriv *priv;
807 ChangeSizeData *data;
810 GtkAllocation *view_allocation;
815 priv = GET_PRIV (chat);
817 if (priv->default_window_height <= 0) {
821 sw = gtk_widget_get_parent (widget);
822 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
825 priv->vscroll_visible = TRUE;
826 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
827 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
829 GTK_POLICY_AUTOMATIC);
830 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
831 g_signal_connect (vscroll, "hide",
832 G_CALLBACK (chat_text_view_scroll_hide_cb),
836 if (priv->last_input_height <= allocation->height) {
837 priv->last_input_height = allocation->height;
841 diff = priv->last_input_height - allocation->height;
842 priv->last_input_height = allocation->height;
844 view_allocation = >K_WIDGET (chat->view)->allocation;
846 dialog = empathy_chat_window_get_dialog (priv->window);
847 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
849 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
851 if (new_height <= priv->default_window_height) {
852 window_height = priv->default_window_height;
854 window_height = new_height;
857 if (current_height <= window_height) {
861 /* Restore the window's size */
862 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
864 data = g_new0 (ChangeSizeData, 1);
865 data->window = dialog;
867 data->height = window_height;
869 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
870 (GSourceFunc) chat_change_size_in_idle_cb,
875 chat_text_view_realize_cb (GtkWidget *widget,
878 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
879 gtk_widget_grab_focus (widget);
883 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
886 GtkTextBuffer *buffer;
890 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
892 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
894 gtk_text_buffer_get_end_iter (buffer, &iter);
895 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
897 gtk_text_buffer_get_end_iter (buffer, &iter);
898 gtk_text_buffer_insert (buffer, &iter, " ", -1);
902 chat_text_populate_popup_cb (GtkTextView *view,
906 EmpathyChatPriv *priv;
907 GtkTextBuffer *buffer;
908 GtkTextTagTable *table;
911 GtkTextIter iter, start, end;
914 EmpathyChatSpell *chat_spell;
915 GtkWidget *smiley_menu;
917 priv = GET_PRIV (chat);
919 /* Add the emoticon menu. */
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 (_("Insert Smiley"));
925 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
926 gtk_widget_show (item);
928 smiley_menu = empathy_chat_view_get_smiley_menu (
929 G_CALLBACK (chat_insert_smiley_activate_cb),
932 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
934 /* Add the spell check menu item. */
935 buffer = gtk_text_view_get_buffer (view);
936 table = gtk_text_buffer_get_tag_table (buffer);
938 tag = gtk_text_tag_table_lookup (table, "misspelled");
940 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
942 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
943 GTK_TEXT_WINDOW_WIDGET,
947 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
951 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
952 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
954 str = gtk_text_buffer_get_text (buffer,
955 &start, &end, FALSE);
958 if (G_STR_EMPTY (str)) {
962 chat_spell = chat_spell_new (chat, str, start, end);
964 g_object_set_data_full (G_OBJECT (menu),
965 "chat_spell", chat_spell,
966 (GDestroyNotify) chat_spell_free);
968 item = gtk_separator_menu_item_new ();
969 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
970 gtk_widget_show (item);
972 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
973 g_signal_connect (item,
975 G_CALLBACK (chat_text_check_word_spelling_cb),
977 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
978 gtk_widget_show (item);
982 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
983 EmpathyChatSpell *chat_spell)
985 empathy_spell_dialog_show (chat_spell->chat,
991 static EmpathyChatSpell *
992 chat_spell_new (EmpathyChat *chat,
997 EmpathyChatSpell *chat_spell;
999 chat_spell = g_new0 (EmpathyChatSpell, 1);
1001 chat_spell->chat = g_object_ref (chat);
1002 chat_spell->word = g_strdup (word);
1003 chat_spell->start = start;
1004 chat_spell->end = end;
1010 chat_spell_free (EmpathyChatSpell *chat_spell)
1012 g_object_unref (chat_spell->chat);
1013 g_free (chat_spell->word);
1014 g_free (chat_spell);
1018 chat_composing_start (EmpathyChat *chat)
1020 EmpathyChatPriv *priv;
1022 priv = GET_PRIV (chat);
1024 if (priv->composing_stop_timeout_id) {
1025 /* Just restart the timeout */
1026 chat_composing_remove_timeout (chat);
1028 empathy_tp_chat_set_state (priv->tp_chat,
1029 TP_CHANNEL_CHAT_STATE_COMPOSING);
1032 priv->composing_stop_timeout_id = g_timeout_add (
1033 1000 * COMPOSING_STOP_TIMEOUT,
1034 (GSourceFunc) chat_composing_stop_timeout_cb,
1039 chat_composing_stop (EmpathyChat *chat)
1041 EmpathyChatPriv *priv;
1043 priv = GET_PRIV (chat);
1045 chat_composing_remove_timeout (chat);
1046 empathy_tp_chat_set_state (priv->tp_chat,
1047 TP_CHANNEL_CHAT_STATE_ACTIVE);
1051 chat_composing_remove_timeout (EmpathyChat *chat)
1053 EmpathyChatPriv *priv;
1055 priv = GET_PRIV (chat);
1057 if (priv->composing_stop_timeout_id) {
1058 g_source_remove (priv->composing_stop_timeout_id);
1059 priv->composing_stop_timeout_id = 0;
1064 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1066 EmpathyChatPriv *priv;
1068 priv = GET_PRIV (chat);
1070 priv->composing_stop_timeout_id = 0;
1071 empathy_tp_chat_set_state (priv->tp_chat,
1072 TP_CHANNEL_CHAT_STATE_PAUSED);
1078 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1079 EmpathyContact *contact,
1080 TelepathyChannelChatState state,
1083 EmpathyChatPriv *priv;
1085 gboolean was_composing;
1087 priv = GET_PRIV (chat);
1089 if (empathy_contact_is_user (contact)) {
1090 /* We don't care about our own chat state */
1094 was_composing = (priv->compositors != NULL);
1096 /* Find the contact in the list. After that l is the list elem or NULL */
1097 for (l = priv->compositors; l; l = l->next) {
1098 if (empathy_contact_equal (contact, l->data)) {
1104 case TP_CHANNEL_CHAT_STATE_GONE:
1105 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1106 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1107 /* Contact is not composing */
1109 priv->compositors = g_list_remove_link (priv->compositors, l);
1110 g_object_unref (l->data);
1114 case TP_CHANNEL_CHAT_STATE_PAUSED:
1115 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1116 /* Contact is composing */
1118 priv->compositors = g_list_prepend (priv->compositors,
1119 g_object_ref (contact));
1123 g_assert_not_reached ();
1126 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1127 was_composing ? "yes" : "no",
1128 priv->compositors ? "yes" : "no");
1130 if ((was_composing && !priv->compositors) ||
1131 (!was_composing && priv->compositors)) {
1132 /* Composing state changed */
1133 g_signal_emit (chat, signals[COMPOSING], 0,
1134 priv->compositors != NULL);
1139 chat_add_logs (EmpathyChat *chat)
1141 EmpathyChatPriv *priv;
1142 GList *messages, *l;
1146 priv = GET_PRIV (chat);
1148 /* Turn off scrolling temporarily */
1149 empathy_chat_view_scroll (chat->view, FALSE);
1151 /* Add messages from last conversation */
1152 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1154 empathy_chat_get_id (chat),
1155 empathy_chat_is_group_chat (chat));
1156 num_messages = g_list_length (messages);
1158 for (l = messages, i = 0; l; l = l->next, i++) {
1159 EmpathyMessage *message;
1163 /* Only add 10 last messages */
1164 if (num_messages - i > 10) {
1165 g_object_unref (message);
1169 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1170 empathy_chat_view_append_message (chat->view, message);
1172 g_object_unref (message);
1174 g_list_free (messages);
1176 /* Turn back on scrolling */
1177 empathy_chat_view_scroll (chat->view, TRUE);
1179 /* Scroll to the most recent messages, we reference the chat
1180 * for the duration of the scroll func.
1182 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1183 g_object_ref (chat));
1186 /* Scroll down after the back-log has been received. */
1188 chat_scroll_down_idle_func (EmpathyChat *chat)
1190 EmpathyChatPriv *priv;
1192 priv = GET_PRIV (chat);
1194 empathy_chat_scroll_down (chat);
1195 g_object_unref (chat);
1197 priv->scroll_idle_id = 0;
1203 empathy_chat_get_is_command (const gchar *str)
1205 g_return_val_if_fail (str != NULL, FALSE);
1207 if (str[0] != '/') {
1211 if (g_str_has_prefix (str, "/me")) {
1214 else if (g_str_has_prefix (str, "/nick")) {
1217 else if (g_str_has_prefix (str, "/topic")) {
1225 empathy_chat_correct_word (EmpathyChat *chat,
1228 const gchar *new_word)
1230 GtkTextBuffer *buffer;
1232 g_return_if_fail (chat != NULL);
1233 g_return_if_fail (new_word != NULL);
1235 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1237 gtk_text_buffer_delete (buffer, &start, &end);
1238 gtk_text_buffer_insert (buffer, &start,
1244 empathy_chat_get_name (EmpathyChat *chat)
1246 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1248 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1249 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1256 empathy_chat_get_tooltip (EmpathyChat *chat)
1258 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1260 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1261 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1268 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1270 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1272 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1273 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1280 empathy_chat_get_widget (EmpathyChat *chat)
1282 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1284 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1285 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1292 empathy_chat_is_group_chat (EmpathyChat *chat)
1294 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1296 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1297 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1304 empathy_chat_is_connected (EmpathyChat *chat)
1306 EmpathyChatPriv *priv;
1308 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1310 priv = GET_PRIV (chat);
1312 return (priv->tp_chat != NULL);
1316 empathy_chat_save_geometry (EmpathyChat *chat,
1322 empathy_geometry_save (empathy_chat_get_id (chat), x, y, w, h);
1326 empathy_chat_load_geometry (EmpathyChat *chat,
1332 empathy_geometry_load (empathy_chat_get_id (chat), x, y, w, h);
1336 empathy_chat_set_tp_chat (EmpathyChat *chat,
1337 EmpathyTpChat *tp_chat)
1339 EmpathyChatPriv *priv;
1340 GList *messages, *l;
1342 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1343 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1345 priv = GET_PRIV (chat);
1347 if (tp_chat == priv->tp_chat) {
1351 if (priv->tp_chat) {
1352 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1353 chat_message_received_cb,
1355 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1358 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1361 g_object_unref (priv->tp_chat);
1365 priv->tp_chat = g_object_ref (tp_chat);
1366 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1368 if (priv->first_tp_chat) {
1369 chat_add_logs (chat);
1370 priv->first_tp_chat = FALSE;
1373 g_signal_connect (tp_chat, "message-received",
1374 G_CALLBACK (chat_message_received_cb),
1376 g_signal_connect (tp_chat, "send-error",
1377 G_CALLBACK (chat_send_error_cb),
1379 g_signal_connect (tp_chat, "chat-state-changed",
1380 G_CALLBACK (chat_state_changed_cb),
1382 g_signal_connect (tp_chat, "destroy",
1383 G_CALLBACK (chat_destroy_cb),
1386 /* Get pending messages */
1387 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1388 messages = empathy_tp_chat_get_pendings (tp_chat);
1389 for (l = messages; l; l = l->next) {
1390 chat_message_received_cb (tp_chat, l->data, chat);
1391 g_object_unref (l->data);
1393 g_list_free (messages);
1395 if (!priv->sensitive) {
1396 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1397 empathy_chat_view_append_event (chat->view, _("Connected"));
1398 priv->sensitive = TRUE;
1401 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1402 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1408 empathy_chat_get_id (EmpathyChat *chat)
1410 EmpathyChatPriv *priv;
1412 priv = GET_PRIV (chat);
1418 empathy_chat_clear (EmpathyChat *chat)
1420 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1422 empathy_chat_view_clear (chat->view);
1426 empathy_chat_set_window (EmpathyChat *chat,
1427 EmpathyChatWindow *window)
1429 EmpathyChatPriv *priv;
1431 priv = GET_PRIV (chat);
1432 priv->window = window;
1436 empathy_chat_get_window (EmpathyChat *chat)
1438 EmpathyChatPriv *priv;
1440 priv = GET_PRIV (chat);
1442 return priv->window;
1446 empathy_chat_scroll_down (EmpathyChat *chat)
1448 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1450 empathy_chat_view_scroll_down (chat->view);
1454 empathy_chat_cut (EmpathyChat *chat)
1456 GtkTextBuffer *buffer;
1458 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1460 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1461 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1462 GtkClipboard *clipboard;
1464 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1466 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1471 empathy_chat_copy (EmpathyChat *chat)
1473 GtkTextBuffer *buffer;
1475 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1477 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1478 empathy_chat_view_copy_clipboard (chat->view);
1482 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1483 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1484 GtkClipboard *clipboard;
1486 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1488 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1493 empathy_chat_paste (EmpathyChat *chat)
1495 GtkTextBuffer *buffer;
1496 GtkClipboard *clipboard;
1498 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1500 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1501 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1503 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1507 empathy_chat_present (EmpathyChat *chat)
1509 EmpathyChatPriv *priv;
1511 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1513 priv = GET_PRIV (chat);
1515 if (priv->window == NULL) {
1516 EmpathyChatWindow *window;
1518 window = empathy_chat_window_get_default ();
1520 window = empathy_chat_window_new ();
1523 empathy_chat_window_add_chat (window, chat);
1526 empathy_chat_window_switch_to_chat (priv->window, chat);
1527 empathy_window_present (
1528 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1531 gtk_widget_grab_focus (chat->input_text_view);
1535 empathy_chat_should_play_sound (EmpathyChat *chat)
1537 EmpathyChatWindow *window;
1538 gboolean play = TRUE;
1540 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1542 window = empathy_chat_get_window (chat);
1547 play = !empathy_chat_window_has_focus (window);
1553 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1555 EmpathyContact *contact;
1556 const gchar *msg, *to;
1557 gchar *cf_msg, *cf_to;
1561 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1563 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1567 msg = empathy_message_get_body (message);
1572 contact = empathy_message_get_receiver (message);
1573 if (!contact || !empathy_contact_is_user (contact)) {
1577 to = empathy_contact_get_name (contact);
1582 cf_msg = g_utf8_casefold (msg, -1);
1583 cf_to = g_utf8_casefold (to, -1);
1585 ch = strstr (cf_msg, cf_to);
1591 /* Not first in the message */
1592 if ((*(ch - 1) != ' ') &&
1593 (*(ch - 1) != ',') &&
1594 (*(ch - 1) != '.')) {
1599 ch = ch + strlen (cf_to);
1600 if (ch >= cf_msg + strlen (cf_msg)) {