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 <libmissioncontrol/mission-control.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
44 #include "empathy-chat.h"
45 #include "empathy-chat-window.h"
46 #include "empathy-geometry.h"
47 #include "empathy-conf.h"
48 #include "empathy-preferences.h"
49 #include "empathy-spell.h"
50 #include "empathy-spell-dialog.h"
51 #include "empathy-ui-utils.h"
52 #include "empathy-gtk-marshal.h"
54 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
56 #define DEBUG_DOMAIN "Chat"
58 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
61 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
63 #define MAX_INPUT_HEIGHT 150
65 #define COMPOSING_STOP_TIMEOUT 5
67 struct _EmpathyChatPriv {
68 EmpathyLogManager *log_manager;
69 EmpathyTpChat *tp_chat;
70 EmpathyChatWindow *window;
73 guint composing_stop_timeout_id;
76 GSList *sent_messages;
77 gint sent_messages_index;
80 gboolean first_tp_chat;
81 time_t last_log_timestamp;
82 gboolean is_first_char;
83 guint block_events_timeout_id;
84 GList *pending_messages;
85 /* Used to automatically shrink a window that has temporarily
86 * grown due to long input.
89 gint default_window_height;
90 gint last_input_height;
91 gboolean vscroll_visible;
102 static void empathy_chat_class_init (EmpathyChatClass *klass);
103 static void empathy_chat_init (EmpathyChat *chat);
104 static void chat_finalize (GObject *object);
105 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
107 static void chat_send (EmpathyChat *chat,
109 static void chat_input_text_view_send (EmpathyChat *chat);
110 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
111 EmpathyMessage *message,
113 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
114 EmpathyMessage *message,
115 TpChannelTextSendError error_code,
117 static void chat_sent_message_add (EmpathyChat *chat,
119 static const gchar * chat_sent_message_get_next (EmpathyChat *chat);
120 static const gchar * chat_sent_message_get_last (EmpathyChat *chat);
121 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
124 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
126 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
129 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
131 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
132 GtkAllocation *allocation,
134 static void chat_text_view_realize_cb (GtkWidget *widget,
136 static void chat_text_populate_popup_cb (GtkTextView *view,
139 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
140 EmpathyChatSpell *chat_spell);
141 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
145 static void chat_spell_free (EmpathyChatSpell *chat_spell);
146 static void chat_composing_start (EmpathyChat *chat);
147 static void chat_composing_stop (EmpathyChat *chat);
148 static void chat_composing_remove_timeout (EmpathyChat *chat);
149 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
150 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
151 EmpathyContact *contact,
152 TpChannelChatState state,
154 static void chat_add_logs (EmpathyChat *chat);
155 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
170 static guint signals[LAST_SIGNAL] = { 0 };
172 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
175 chat_get_property (GObject *object,
180 EmpathyChatPriv *priv = GET_PRIV (object);
184 g_value_set_object (value, priv->tp_chat);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
193 chat_set_property (GObject *object,
198 EmpathyChat *chat = EMPATHY_CHAT (object);
202 empathy_chat_set_tp_chat (chat,
203 EMPATHY_TP_CHAT (g_value_get_object (value)));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
212 chat_status_changed_cb (MissionControl *mc,
213 TpConnectionStatus status,
215 TpConnectionStatusReason reason,
216 const gchar *unique_name,
219 EmpathyChatPriv *priv = GET_PRIV (chat);
222 account = mc_account_lookup (unique_name);
224 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
225 empathy_account_equal (account, priv->account)) {
226 TpHandleType handle_type;
228 empathy_debug (DEBUG_DOMAIN,
229 "Account reconnected, request a new Text channel");
231 if (empathy_chat_is_group_chat (chat)) {
232 handle_type = TP_HANDLE_TYPE_ROOM;
234 handle_type = TP_HANDLE_TYPE_CONTACT;
237 mission_control_request_channel_with_string_handle (mc,
239 TP_IFACE_CHANNEL_TYPE_TEXT,
245 g_object_unref (account);
249 empathy_chat_class_init (EmpathyChatClass *klass)
251 GObjectClass *object_class;
253 object_class = G_OBJECT_CLASS (klass);
255 object_class->finalize = chat_finalize;
256 object_class->get_property = chat_get_property;
257 object_class->set_property = chat_set_property;
259 g_object_class_install_property (object_class,
261 g_param_spec_object ("tp-chat",
263 "The tp chat object",
264 EMPATHY_TYPE_TP_CHAT,
269 g_signal_new ("composing",
270 G_OBJECT_CLASS_TYPE (object_class),
274 g_cclosure_marshal_VOID__BOOLEAN,
278 signals[NEW_MESSAGE] =
279 g_signal_new ("new-message",
280 G_OBJECT_CLASS_TYPE (object_class),
284 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
286 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
288 signals[NAME_CHANGED] =
289 g_signal_new ("name-changed",
290 G_OBJECT_CLASS_TYPE (object_class),
294 g_cclosure_marshal_VOID__POINTER,
298 signals[STATUS_CHANGED] =
299 g_signal_new ("status-changed",
300 G_OBJECT_CLASS_TYPE (object_class),
304 g_cclosure_marshal_VOID__VOID,
308 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
312 empathy_chat_init (EmpathyChat *chat)
314 EmpathyChatPriv *priv = GET_PRIV (chat);
315 GtkTextBuffer *buffer;
317 chat->view = empathy_chat_view_new ();
318 chat->input_text_view = gtk_text_view_new ();
320 priv->is_first_char = TRUE;
322 g_object_set (chat->input_text_view,
323 "pixels-above-lines", 2,
324 "pixels-below-lines", 2,
325 "pixels-inside-wrap", 1,
328 "wrap-mode", GTK_WRAP_WORD_CHAR,
331 priv->log_manager = empathy_log_manager_new ();
332 priv->default_window_height = -1;
333 priv->vscroll_visible = FALSE;
334 priv->sensitive = TRUE;
335 priv->sent_messages = NULL;
336 priv->sent_messages_index = -1;
337 priv->first_tp_chat = TRUE;
338 priv->mc = empathy_mission_control_new ();
340 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
341 G_CALLBACK (chat_status_changed_cb),
344 g_signal_connect (chat->input_text_view,
346 G_CALLBACK (chat_input_key_press_event_cb),
349 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
350 g_signal_connect (buffer,
352 G_CALLBACK (chat_input_text_buffer_changed_cb),
354 g_signal_connect (chat->view,
356 G_CALLBACK (chat_text_view_focus_in_event_cb),
359 g_signal_connect (chat->input_text_view,
361 G_CALLBACK (chat_text_view_size_allocate_cb),
364 g_signal_connect (chat->input_text_view,
366 G_CALLBACK (chat_text_view_realize_cb),
369 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
371 G_CALLBACK (chat_text_populate_popup_cb),
374 /* create misspelt words identification tag */
375 gtk_text_buffer_create_tag (buffer,
377 "underline", PANGO_UNDERLINE_ERROR,
382 chat_finalize (GObject *object)
385 EmpathyChatPriv *priv;
387 chat = EMPATHY_CHAT (object);
388 priv = GET_PRIV (chat);
390 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
392 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
393 g_slist_free (priv->sent_messages);
395 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
396 g_list_free (priv->compositors);
398 chat_composing_remove_timeout (chat);
399 g_object_unref (priv->log_manager);
401 g_list_foreach (priv->pending_messages, (GFunc) g_object_unref, NULL);
402 g_list_free (priv->pending_messages);
404 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
405 G_CALLBACK (chat_status_changed_cb),
407 g_object_unref (priv->mc);
411 g_object_unref (priv->tp_chat);
415 g_object_unref (priv->account);
418 if (priv->scroll_idle_id) {
419 g_source_remove (priv->scroll_idle_id);
422 if (priv->block_events_timeout_id) {
423 g_source_remove (priv->block_events_timeout_id);
428 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
432 chat_destroy_cb (EmpathyTpChat *tp_chat,
435 EmpathyChatPriv *priv;
437 priv = GET_PRIV (chat);
440 g_object_unref (priv->tp_chat);
441 priv->tp_chat = NULL;
443 priv->sensitive = FALSE;
445 empathy_chat_view_append_event (chat->view, _("Disconnected"));
446 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
448 if (priv->block_events_timeout_id != 0) {
449 g_source_remove (priv->block_events_timeout_id);
452 g_list_foreach (priv->pending_messages, (GFunc) g_object_unref, NULL);
453 g_list_free (priv->pending_messages);
455 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
456 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
461 chat_send (EmpathyChat *chat,
464 EmpathyChatPriv *priv;
465 EmpathyMessage *message;
467 priv = GET_PRIV (chat);
469 if (G_STR_EMPTY (msg)) {
473 chat_sent_message_add (chat, msg);
475 if (g_str_has_prefix (msg, "/clear")) {
476 empathy_chat_view_clear (chat->view);
480 /* FIXME: add here something to let group/privrate chat handle
481 * some special messages */
483 message = empathy_message_new (msg);
485 empathy_tp_chat_send (priv->tp_chat, message);
487 g_object_unref (message);
491 chat_input_text_view_send (EmpathyChat *chat)
493 EmpathyChatPriv *priv;
494 GtkTextBuffer *buffer;
495 GtkTextIter start, end;
498 priv = GET_PRIV (chat);
500 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
502 gtk_text_buffer_get_bounds (buffer, &start, &end);
503 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
505 /* clear the input field */
506 gtk_text_buffer_set_text (buffer, "", -1);
508 chat_send (chat, msg);
512 priv->is_first_char = TRUE;
516 chat_message_received_cb (EmpathyTpChat *tp_chat,
517 EmpathyMessage *message,
520 EmpathyChatPriv *priv;
521 EmpathyContact *sender;
524 priv = GET_PRIV (chat);
526 timestamp = empathy_message_get_timestamp (message);
527 if (timestamp <= priv->last_log_timestamp) {
528 /* Do not take care of messages anterior of the last
529 * logged message. Some Jabber chatroom sends messages
530 * received before we joined the room, this avoid
531 * displaying those messages if we already logged them
532 * last time we joined that room. */
533 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
534 "anterior of last logged message.");
538 if (chat->block_events) {
539 /* Wait until block_events cb before displaying
540 * them to have to chance to get alias/avatar of sender. */
541 priv->pending_messages = g_list_append (priv->pending_messages,
542 g_object_ref (message));
546 sender = empathy_message_get_sender (message);
547 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
548 empathy_contact_get_name (sender));
550 empathy_log_manager_add_message (priv->log_manager,
551 empathy_chat_get_id (chat),
552 empathy_chat_is_group_chat (chat),
555 empathy_chat_view_append_message (chat->view, message);
557 if (empathy_chat_should_play_sound (chat)) {
558 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
561 /* We received a message so the contact is no more composing */
562 chat_state_changed_cb (tp_chat, sender,
563 TP_CHANNEL_CHAT_STATE_ACTIVE,
566 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
570 chat_send_error_cb (EmpathyTpChat *tp_chat,
571 EmpathyMessage *message,
572 TpChannelTextSendError error_code,
578 switch (error_code) {
579 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
580 error = _("offline");
582 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
583 error = _("invalid contact");
585 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
586 error = _("permission denied");
588 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
589 error = _("too long message");
591 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
592 error = _("not implemented");
595 error = _("unknown");
599 str = g_strdup_printf (_("Error sending message '%s': %s"),
600 empathy_message_get_body (message),
602 empathy_chat_view_append_event (chat->view, str);
607 chat_sent_message_add (EmpathyChat *chat,
610 EmpathyChatPriv *priv;
614 priv = GET_PRIV (chat);
616 /* Save the sent message in our repeat buffer */
617 list = priv->sent_messages;
619 /* Remove any other occurances of this msg */
620 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
621 list = g_slist_remove_link (list, item);
623 g_slist_free1 (item);
626 /* Trim the list to the last 10 items */
627 while (g_slist_length (list) > 10) {
628 item = g_slist_last (list);
630 list = g_slist_remove_link (list, item);
632 g_slist_free1 (item);
636 /* Add new message */
637 list = g_slist_prepend (list, g_strdup (str));
639 /* Set list and reset the index */
640 priv->sent_messages = list;
641 priv->sent_messages_index = -1;
645 chat_sent_message_get_next (EmpathyChat *chat)
647 EmpathyChatPriv *priv;
650 priv = GET_PRIV (chat);
652 if (!priv->sent_messages) {
653 empathy_debug (DEBUG_DOMAIN,
654 "No sent messages, next message is NULL");
658 max = g_slist_length (priv->sent_messages) - 1;
660 if (priv->sent_messages_index < max) {
661 priv->sent_messages_index++;
664 empathy_debug (DEBUG_DOMAIN,
665 "Returning next message index:%d",
666 priv->sent_messages_index);
668 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
672 chat_sent_message_get_last (EmpathyChat *chat)
674 EmpathyChatPriv *priv;
676 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
678 priv = GET_PRIV (chat);
680 if (!priv->sent_messages) {
681 empathy_debug (DEBUG_DOMAIN,
682 "No sent messages, last message is NULL");
686 if (priv->sent_messages_index >= 0) {
687 priv->sent_messages_index--;
690 empathy_debug (DEBUG_DOMAIN,
691 "Returning last message index:%d",
692 priv->sent_messages_index);
694 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
698 chat_input_key_press_event_cb (GtkWidget *widget,
702 EmpathyChatPriv *priv;
705 GtkWidget *text_view_sw;
707 priv = GET_PRIV (chat);
709 /* Catch ctrl+up/down so we can traverse messages we sent */
710 if ((event->state & GDK_CONTROL_MASK) &&
711 (event->keyval == GDK_Up ||
712 event->keyval == GDK_Down)) {
713 GtkTextBuffer *buffer;
716 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
718 if (event->keyval == GDK_Up) {
719 str = chat_sent_message_get_next (chat);
721 str = chat_sent_message_get_last (chat);
724 g_signal_handlers_block_by_func (buffer,
725 chat_input_text_buffer_changed_cb,
727 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
728 g_signal_handlers_unblock_by_func (buffer,
729 chat_input_text_buffer_changed_cb,
735 /* Catch enter but not ctrl/shift-enter */
736 if (IS_ENTER (event->keyval) &&
737 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
740 /* This is to make sure that kinput2 gets the enter. And if
741 * it's handled there we shouldn't send on it. This is because
742 * kinput2 uses Enter to commit letters. See:
743 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
746 view = GTK_TEXT_VIEW (chat->input_text_view);
747 if (gtk_im_context_filter_keypress (view->im_context, event)) {
748 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
752 chat_input_text_view_send (chat);
756 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
758 if (IS_ENTER (event->keyval) &&
759 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
760 /* Newline for shift/control-enter. */
763 else if (!(event->state & GDK_CONTROL_MASK) &&
764 event->keyval == GDK_Page_Up) {
765 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
766 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
770 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
771 event->keyval == GDK_Page_Down) {
772 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
773 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
774 gtk_adjustment_set_value (adj, val);
779 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
780 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
787 chat_text_view_focus_in_event_cb (GtkWidget *widget,
791 gtk_widget_grab_focus (chat->input_text_view);
797 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
800 EmpathyChatPriv *priv;
801 GtkTextIter start, end;
803 gboolean spell_checker = FALSE;
805 priv = GET_PRIV (chat);
807 if (gtk_text_buffer_get_char_count (buffer) == 0) {
808 chat_composing_stop (chat);
810 chat_composing_start (chat);
813 empathy_conf_get_bool (empathy_conf_get (),
814 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
817 if (priv->is_first_char) {
821 GtkAllocation *allocation;
823 /* Save the window's size */
824 dialog = empathy_chat_window_get_dialog (priv->window);
825 gtk_window_get_size (GTK_WINDOW (dialog),
826 NULL, &window_height);
828 gtk_widget_size_request (chat->input_text_view, &req);
830 allocation = >K_WIDGET (chat->view)->allocation;
832 priv->default_window_height = window_height;
833 priv->last_input_height = req.height;
834 priv->padding_height = window_height - req.height - allocation->height;
836 priv->is_first_char = FALSE;
839 gtk_text_buffer_get_start_iter (buffer, &start);
841 if (!spell_checker) {
842 gtk_text_buffer_get_end_iter (buffer, &end);
843 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
847 if (!empathy_spell_supported ()) {
851 /* NOTE: this is really inefficient, we shouldn't have to
852 reiterate the whole buffer each time and check each work
855 gboolean correct = FALSE;
858 if (gtk_text_iter_is_start (&start)) {
861 if (!gtk_text_iter_forward_word_end (&end)) {
862 /* no whole word yet */
866 if (!gtk_text_iter_forward_word_end (&end)) {
867 /* must be the end of the buffer */
872 gtk_text_iter_backward_word_start (&start);
875 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
877 /* spell check string */
878 if (!empathy_chat_get_is_command (str)) {
879 correct = empathy_spell_check (str);
885 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
887 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
892 /* set start iter to the end iters position */
904 chat_change_size_in_idle_cb (ChangeSizeData *data)
906 gtk_window_resize (GTK_WINDOW (data->window),
907 data->width, data->height);
913 chat_text_view_scroll_hide_cb (GtkWidget *widget,
916 EmpathyChatPriv *priv;
919 priv = GET_PRIV (chat);
921 priv->vscroll_visible = FALSE;
922 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
924 sw = gtk_widget_get_parent (chat->input_text_view);
925 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
928 g_object_set (sw, "height-request", -1, NULL);
932 chat_text_view_size_allocate_cb (GtkWidget *widget,
933 GtkAllocation *allocation,
936 EmpathyChatPriv *priv;
939 ChangeSizeData *data;
942 GtkAllocation *view_allocation;
947 priv = GET_PRIV (chat);
949 if (priv->default_window_height <= 0) {
953 sw = gtk_widget_get_parent (widget);
954 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
957 priv->vscroll_visible = TRUE;
958 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
959 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
961 GTK_POLICY_AUTOMATIC);
962 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
963 g_signal_connect (vscroll, "hide",
964 G_CALLBACK (chat_text_view_scroll_hide_cb),
968 if (priv->last_input_height <= allocation->height) {
969 priv->last_input_height = allocation->height;
973 diff = priv->last_input_height - allocation->height;
974 priv->last_input_height = allocation->height;
976 view_allocation = >K_WIDGET (chat->view)->allocation;
978 dialog = empathy_chat_window_get_dialog (priv->window);
979 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
981 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
983 if (new_height <= priv->default_window_height) {
984 window_height = priv->default_window_height;
986 window_height = new_height;
989 if (current_height <= window_height) {
993 /* Restore the window's size */
994 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
996 data = g_new0 (ChangeSizeData, 1);
997 data->window = dialog;
999 data->height = window_height;
1001 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1002 (GSourceFunc) chat_change_size_in_idle_cb,
1007 chat_text_view_realize_cb (GtkWidget *widget,
1010 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
1011 gtk_widget_grab_focus (widget);
1015 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1018 GtkTextBuffer *buffer;
1020 const gchar *smiley;
1022 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1024 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1026 gtk_text_buffer_get_end_iter (buffer, &iter);
1027 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1029 gtk_text_buffer_get_end_iter (buffer, &iter);
1030 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1034 chat_text_populate_popup_cb (GtkTextView *view,
1038 EmpathyChatPriv *priv;
1039 GtkTextBuffer *buffer;
1040 GtkTextTagTable *table;
1043 GtkTextIter iter, start, end;
1046 EmpathyChatSpell *chat_spell;
1047 GtkWidget *smiley_menu;
1049 priv = GET_PRIV (chat);
1051 /* Add the emoticon menu. */
1052 item = gtk_separator_menu_item_new ();
1053 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1054 gtk_widget_show (item);
1056 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1057 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1058 gtk_widget_show (item);
1060 smiley_menu = empathy_chat_view_get_smiley_menu (
1061 G_CALLBACK (chat_insert_smiley_activate_cb),
1063 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1065 /* Add the spell check menu item. */
1066 buffer = gtk_text_view_get_buffer (view);
1067 table = gtk_text_buffer_get_tag_table (buffer);
1069 tag = gtk_text_tag_table_lookup (table, "misspelled");
1071 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1073 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1074 GTK_TEXT_WINDOW_WIDGET,
1078 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1082 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1083 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1085 str = gtk_text_buffer_get_text (buffer,
1086 &start, &end, FALSE);
1089 if (G_STR_EMPTY (str)) {
1093 chat_spell = chat_spell_new (chat, str, start, end);
1095 g_object_set_data_full (G_OBJECT (menu),
1096 "chat_spell", chat_spell,
1097 (GDestroyNotify) chat_spell_free);
1099 item = gtk_separator_menu_item_new ();
1100 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1101 gtk_widget_show (item);
1103 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1104 g_signal_connect (item,
1106 G_CALLBACK (chat_text_check_word_spelling_cb),
1108 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1109 gtk_widget_show (item);
1113 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1114 EmpathyChatSpell *chat_spell)
1116 empathy_spell_dialog_show (chat_spell->chat,
1122 static EmpathyChatSpell *
1123 chat_spell_new (EmpathyChat *chat,
1128 EmpathyChatSpell *chat_spell;
1130 chat_spell = g_new0 (EmpathyChatSpell, 1);
1132 chat_spell->chat = g_object_ref (chat);
1133 chat_spell->word = g_strdup (word);
1134 chat_spell->start = start;
1135 chat_spell->end = end;
1141 chat_spell_free (EmpathyChatSpell *chat_spell)
1143 g_object_unref (chat_spell->chat);
1144 g_free (chat_spell->word);
1145 g_free (chat_spell);
1149 chat_composing_start (EmpathyChat *chat)
1151 EmpathyChatPriv *priv;
1153 priv = GET_PRIV (chat);
1155 if (priv->composing_stop_timeout_id) {
1156 /* Just restart the timeout */
1157 chat_composing_remove_timeout (chat);
1159 empathy_tp_chat_set_state (priv->tp_chat,
1160 TP_CHANNEL_CHAT_STATE_COMPOSING);
1163 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1164 COMPOSING_STOP_TIMEOUT,
1165 (GSourceFunc) chat_composing_stop_timeout_cb,
1170 chat_composing_stop (EmpathyChat *chat)
1172 EmpathyChatPriv *priv;
1174 priv = GET_PRIV (chat);
1176 chat_composing_remove_timeout (chat);
1177 empathy_tp_chat_set_state (priv->tp_chat,
1178 TP_CHANNEL_CHAT_STATE_ACTIVE);
1182 chat_composing_remove_timeout (EmpathyChat *chat)
1184 EmpathyChatPriv *priv;
1186 priv = GET_PRIV (chat);
1188 if (priv->composing_stop_timeout_id) {
1189 g_source_remove (priv->composing_stop_timeout_id);
1190 priv->composing_stop_timeout_id = 0;
1195 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1197 EmpathyChatPriv *priv;
1199 priv = GET_PRIV (chat);
1201 priv->composing_stop_timeout_id = 0;
1202 empathy_tp_chat_set_state (priv->tp_chat,
1203 TP_CHANNEL_CHAT_STATE_PAUSED);
1209 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1210 EmpathyContact *contact,
1211 TpChannelChatState state,
1214 EmpathyChatPriv *priv;
1216 gboolean was_composing;
1218 priv = GET_PRIV (chat);
1220 if (empathy_contact_is_user (contact)) {
1221 /* We don't care about our own chat state */
1225 was_composing = (priv->compositors != NULL);
1227 /* Find the contact in the list. After that l is the list elem or NULL */
1228 for (l = priv->compositors; l; l = l->next) {
1229 if (empathy_contact_equal (contact, l->data)) {
1235 case TP_CHANNEL_CHAT_STATE_GONE:
1236 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1237 case TP_CHANNEL_CHAT_STATE_PAUSED:
1238 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1239 /* Contact is not composing */
1241 priv->compositors = g_list_remove_link (priv->compositors, l);
1242 g_object_unref (l->data);
1246 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1247 /* Contact is composing */
1249 priv->compositors = g_list_prepend (priv->compositors,
1250 g_object_ref (contact));
1254 g_assert_not_reached ();
1257 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1258 was_composing ? "yes" : "no",
1259 priv->compositors ? "yes" : "no");
1261 if ((was_composing && !priv->compositors) ||
1262 (!was_composing && priv->compositors)) {
1263 /* Composing state changed */
1264 g_signal_emit (chat, signals[COMPOSING], 0,
1265 priv->compositors != NULL);
1270 chat_add_logs (EmpathyChat *chat)
1272 EmpathyChatPriv *priv;
1273 GList *messages, *l;
1277 priv = GET_PRIV (chat);
1279 /* Turn off scrolling temporarily */
1280 empathy_chat_view_scroll (chat->view, FALSE);
1282 /* Add messages from last conversation */
1283 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1285 empathy_chat_get_id (chat),
1286 empathy_chat_is_group_chat (chat));
1287 num_messages = g_list_length (messages);
1289 for (l = messages, i = 0; l; l = l->next, i++) {
1290 EmpathyMessage *message;
1294 /* Only add 10 last messages */
1295 if (num_messages - i > 10) {
1296 g_object_unref (message);
1300 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1301 empathy_chat_view_append_message (chat->view, message);
1303 g_object_unref (message);
1305 g_list_free (messages);
1307 /* Turn back on scrolling */
1308 empathy_chat_view_scroll (chat->view, TRUE);
1310 /* Scroll to the most recent messages, we reference the chat
1311 * for the duration of the scroll func.
1313 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1314 g_object_ref (chat));
1317 /* Scroll down after the back-log has been received. */
1319 chat_scroll_down_idle_func (EmpathyChat *chat)
1321 EmpathyChatPriv *priv;
1323 priv = GET_PRIV (chat);
1325 empathy_chat_scroll_down (chat);
1326 g_object_unref (chat);
1328 priv->scroll_idle_id = 0;
1334 empathy_chat_get_is_command (const gchar *str)
1336 g_return_val_if_fail (str != NULL, FALSE);
1338 if (str[0] != '/') {
1342 if (g_str_has_prefix (str, "/me")) {
1345 else if (g_str_has_prefix (str, "/nick")) {
1348 else if (g_str_has_prefix (str, "/topic")) {
1356 empathy_chat_correct_word (EmpathyChat *chat,
1359 const gchar *new_word)
1361 GtkTextBuffer *buffer;
1363 g_return_if_fail (chat != NULL);
1364 g_return_if_fail (new_word != NULL);
1366 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1368 gtk_text_buffer_delete (buffer, &start, &end);
1369 gtk_text_buffer_insert (buffer, &start,
1375 empathy_chat_get_name (EmpathyChat *chat)
1377 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1379 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1380 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1387 empathy_chat_get_tooltip (EmpathyChat *chat)
1389 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1391 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1392 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1399 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1401 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1403 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1404 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1411 empathy_chat_get_widget (EmpathyChat *chat)
1413 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1415 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1416 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1423 empathy_chat_is_group_chat (EmpathyChat *chat)
1425 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1427 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1428 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1435 empathy_chat_is_connected (EmpathyChat *chat)
1437 EmpathyChatPriv *priv;
1439 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1441 priv = GET_PRIV (chat);
1443 return (priv->tp_chat != NULL);
1447 empathy_chat_save_geometry (EmpathyChat *chat,
1453 empathy_geometry_save (empathy_chat_get_id (chat), x, y, w, h);
1457 empathy_chat_load_geometry (EmpathyChat *chat,
1463 empathy_geometry_load (empathy_chat_get_id (chat), x, y, w, h);
1467 chat_block_events_timeout_cb (gpointer data)
1469 EmpathyChat *chat = EMPATHY_CHAT (data);
1470 EmpathyChatPriv *priv = GET_PRIV (chat);
1473 chat->block_events = FALSE;
1474 priv->block_events_timeout_id = 0;
1476 for (l = priv->pending_messages; l; l = l->next) {
1477 chat_message_received_cb (priv->tp_chat, l->data, chat);
1478 g_object_unref (l->data);
1480 g_list_free (priv->pending_messages);
1481 priv->pending_messages = NULL;
1487 empathy_chat_set_tp_chat (EmpathyChat *chat,
1488 EmpathyTpChat *tp_chat)
1490 EmpathyChatPriv *priv;
1493 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1494 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1496 priv = GET_PRIV (chat);
1498 if (tp_chat == priv->tp_chat) {
1502 /* Block events for some time to avoid having "has come online" or
1503 * "joined" messages. */
1504 chat->block_events = TRUE;
1505 if (priv->block_events_timeout_id != 0) {
1506 g_source_remove (priv->block_events_timeout_id);
1508 priv->block_events_timeout_id =
1509 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1511 if (priv->tp_chat) {
1512 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1513 chat_message_received_cb,
1515 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1518 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1521 g_object_unref (priv->tp_chat);
1523 if (priv->account) {
1524 g_object_unref (priv->account);
1528 priv->tp_chat = g_object_ref (tp_chat);
1529 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1530 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1532 if (priv->first_tp_chat) {
1533 chat_add_logs (chat);
1534 priv->first_tp_chat = FALSE;
1537 g_signal_connect (tp_chat, "message-received",
1538 G_CALLBACK (chat_message_received_cb),
1540 g_signal_connect (tp_chat, "send-error",
1541 G_CALLBACK (chat_send_error_cb),
1543 g_signal_connect (tp_chat, "chat-state-changed",
1544 G_CALLBACK (chat_state_changed_cb),
1546 g_signal_connect (tp_chat, "destroy",
1547 G_CALLBACK (chat_destroy_cb),
1550 /* Get pending messages, wait until block_events cb before displaying
1551 * them to have to chance to get alias/avatar of sender. */
1552 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1553 messages = empathy_tp_chat_get_pendings (tp_chat);
1554 priv->pending_messages = g_list_concat (priv->pending_messages, messages);
1556 if (!priv->sensitive) {
1557 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1558 empathy_chat_view_append_event (chat->view, _("Connected"));
1559 priv->sensitive = TRUE;
1562 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1563 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1566 g_object_notify (G_OBJECT (chat), "tp-chat");
1570 empathy_chat_get_id (EmpathyChat *chat)
1572 EmpathyChatPriv *priv;
1574 priv = GET_PRIV (chat);
1580 empathy_chat_get_account (EmpathyChat *chat)
1582 EmpathyChatPriv *priv = GET_PRIV (chat);
1584 return priv->account;
1588 empathy_chat_clear (EmpathyChat *chat)
1590 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1592 empathy_chat_view_clear (chat->view);
1596 empathy_chat_set_window (EmpathyChat *chat,
1597 EmpathyChatWindow *window)
1599 EmpathyChatPriv *priv;
1601 priv = GET_PRIV (chat);
1602 priv->window = window;
1606 empathy_chat_get_window (EmpathyChat *chat)
1608 EmpathyChatPriv *priv;
1610 priv = GET_PRIV (chat);
1612 return priv->window;
1616 empathy_chat_scroll_down (EmpathyChat *chat)
1618 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1620 empathy_chat_view_scroll_down (chat->view);
1624 empathy_chat_cut (EmpathyChat *chat)
1626 GtkTextBuffer *buffer;
1628 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1630 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1631 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1632 GtkClipboard *clipboard;
1634 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1636 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1641 empathy_chat_copy (EmpathyChat *chat)
1643 GtkTextBuffer *buffer;
1645 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1647 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1648 empathy_chat_view_copy_clipboard (chat->view);
1652 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1653 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1654 GtkClipboard *clipboard;
1656 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1658 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1663 empathy_chat_paste (EmpathyChat *chat)
1665 GtkTextBuffer *buffer;
1666 GtkClipboard *clipboard;
1668 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1670 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1671 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1673 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1677 empathy_chat_present (EmpathyChat *chat)
1679 EmpathyChatPriv *priv;
1681 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1683 priv = GET_PRIV (chat);
1685 if (priv->window == NULL) {
1686 EmpathyChatWindow *window;
1688 window = empathy_chat_window_get_default ();
1690 window = empathy_chat_window_new ();
1693 empathy_chat_window_add_chat (window, chat);
1696 empathy_chat_window_switch_to_chat (priv->window, chat);
1697 empathy_window_present (
1698 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1701 gtk_widget_grab_focus (chat->input_text_view);
1705 empathy_chat_should_play_sound (EmpathyChat *chat)
1707 EmpathyChatWindow *window;
1708 gboolean play = TRUE;
1710 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1712 window = empathy_chat_get_window (chat);
1717 play = !empathy_chat_window_has_focus (window);
1723 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1725 EmpathyContact *contact;
1726 const gchar *msg, *to;
1727 gchar *cf_msg, *cf_to;
1731 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1733 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1737 msg = empathy_message_get_body (message);
1742 contact = empathy_message_get_receiver (message);
1743 if (!contact || !empathy_contact_is_user (contact)) {
1747 to = empathy_contact_get_name (contact);
1752 cf_msg = g_utf8_casefold (msg, -1);
1753 cf_to = g_utf8_casefold (to, -1);
1755 ch = strstr (cf_msg, cf_to);
1761 /* Not first in the message */
1762 if ((*(ch - 1) != ' ') &&
1763 (*(ch - 1) != ',') &&
1764 (*(ch - 1) != '.')) {
1769 ch = ch + strlen (cf_to);
1770 if (ch >= cf_msg + strlen (cf_msg)) {