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-2008 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 <telepathy-glib/util.h>
41 #include <libempathy/empathy-contact-manager.h>
42 #include <libempathy/empathy-log-manager.h>
43 #include <libempathy/empathy-debug.h>
44 #include <libempathy/empathy-utils.h>
46 #include "empathy-chat.h"
47 #include "empathy-geometry.h"
48 #include "empathy-conf.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;
73 guint composing_stop_timeout_id;
76 GSList *sent_messages;
77 gint sent_messages_index;
80 gboolean first_tp_chat;
81 GList *backlog_messages;
82 gboolean is_first_char;
83 guint block_events_timeout_id;
84 /* Used to automatically shrink a window that has temporarily
85 * grown due to long input.
88 gint default_window_height;
89 gint last_input_height;
90 gboolean vscroll_visible;
101 static void empathy_chat_class_init (EmpathyChatClass *klass);
102 static void empathy_chat_init (EmpathyChat *chat);
103 static void chat_finalize (GObject *object);
104 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
106 static void chat_send (EmpathyChat *chat,
108 static void chat_input_text_view_send (EmpathyChat *chat);
109 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
110 EmpathyMessage *message,
112 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
113 EmpathyMessage *message,
114 TpChannelTextSendError error_code,
116 static void chat_sent_message_add (EmpathyChat *chat,
118 static const gchar * chat_sent_message_get_next (EmpathyChat *chat);
119 static const gchar * chat_sent_message_get_last (EmpathyChat *chat);
120 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
123 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
125 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
128 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
130 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
131 GtkAllocation *allocation,
133 static void chat_text_view_realize_cb (GtkWidget *widget,
135 static void chat_text_populate_popup_cb (GtkTextView *view,
138 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
139 EmpathyChatSpell *chat_spell);
140 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
144 static void chat_spell_free (EmpathyChatSpell *chat_spell);
145 static void chat_composing_start (EmpathyChat *chat);
146 static void chat_composing_stop (EmpathyChat *chat);
147 static void chat_composing_remove_timeout (EmpathyChat *chat);
148 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
149 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
150 EmpathyContact *contact,
151 TpChannelChatState state,
153 static void chat_add_logs (EmpathyChat *chat);
154 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
169 static guint signals[LAST_SIGNAL] = { 0 };
171 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
174 chat_get_property (GObject *object,
179 EmpathyChatPriv *priv = GET_PRIV (object);
183 g_value_set_object (value, priv->tp_chat);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
192 chat_set_property (GObject *object,
197 EmpathyChat *chat = EMPATHY_CHAT (object);
201 empathy_chat_set_tp_chat (chat,
202 EMPATHY_TP_CHAT (g_value_get_object (value)));
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
211 chat_status_changed_cb (MissionControl *mc,
212 TpConnectionStatus status,
214 TpConnectionStatusReason reason,
215 const gchar *unique_name,
218 EmpathyChatPriv *priv = GET_PRIV (chat);
221 account = mc_account_lookup (unique_name);
223 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
224 empathy_account_equal (account, priv->account)) {
225 TpHandleType handle_type;
227 empathy_debug (DEBUG_DOMAIN,
228 "Account reconnected, request a new Text channel");
230 if (empathy_chat_is_group_chat (chat)) {
231 handle_type = TP_HANDLE_TYPE_ROOM;
233 handle_type = TP_HANDLE_TYPE_CONTACT;
236 mission_control_request_channel_with_string_handle (mc,
238 TP_IFACE_CHANNEL_TYPE_TEXT,
244 g_object_unref (account);
248 empathy_chat_class_init (EmpathyChatClass *klass)
250 GObjectClass *object_class;
252 object_class = G_OBJECT_CLASS (klass);
254 object_class->finalize = chat_finalize;
255 object_class->get_property = chat_get_property;
256 object_class->set_property = chat_set_property;
258 g_object_class_install_property (object_class,
260 g_param_spec_object ("tp-chat",
262 "The tp chat object",
263 EMPATHY_TYPE_TP_CHAT,
268 g_signal_new ("composing",
269 G_OBJECT_CLASS_TYPE (object_class),
273 g_cclosure_marshal_VOID__BOOLEAN,
277 signals[NEW_MESSAGE] =
278 g_signal_new ("new-message",
279 G_OBJECT_CLASS_TYPE (object_class),
283 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
285 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
287 signals[NAME_CHANGED] =
288 g_signal_new ("name-changed",
289 G_OBJECT_CLASS_TYPE (object_class),
293 g_cclosure_marshal_VOID__POINTER,
297 signals[STATUS_CHANGED] =
298 g_signal_new ("status-changed",
299 G_OBJECT_CLASS_TYPE (object_class),
303 g_cclosure_marshal_VOID__VOID,
307 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
311 empathy_chat_init (EmpathyChat *chat)
313 EmpathyChatPriv *priv = GET_PRIV (chat);
314 GtkTextBuffer *buffer;
316 chat->view = empathy_chat_view_new ();
317 chat->input_text_view = gtk_text_view_new ();
319 priv->is_first_char = TRUE;
321 g_object_set (chat->input_text_view,
322 "pixels-above-lines", 2,
323 "pixels-below-lines", 2,
324 "pixels-inside-wrap", 1,
327 "wrap-mode", GTK_WRAP_WORD_CHAR,
330 priv->log_manager = empathy_log_manager_new ();
331 priv->default_window_height = -1;
332 priv->vscroll_visible = FALSE;
333 priv->sensitive = TRUE;
334 priv->sent_messages = NULL;
335 priv->sent_messages_index = -1;
336 priv->first_tp_chat = TRUE;
337 priv->mc = empathy_mission_control_new ();
339 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
340 G_CALLBACK (chat_status_changed_cb),
343 g_signal_connect (chat->input_text_view,
345 G_CALLBACK (chat_input_key_press_event_cb),
348 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
349 g_signal_connect (buffer,
351 G_CALLBACK (chat_input_text_buffer_changed_cb),
353 g_signal_connect (chat->view,
355 G_CALLBACK (chat_text_view_focus_in_event_cb),
358 g_signal_connect (chat->input_text_view,
360 G_CALLBACK (chat_text_view_size_allocate_cb),
363 g_signal_connect (chat->input_text_view,
365 G_CALLBACK (chat_text_view_realize_cb),
368 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
370 G_CALLBACK (chat_text_populate_popup_cb),
373 /* create misspelt words identification tag */
374 gtk_text_buffer_create_tag (buffer,
376 "underline", PANGO_UNDERLINE_ERROR,
381 chat_finalize (GObject *object)
384 EmpathyChatPriv *priv;
386 chat = EMPATHY_CHAT (object);
387 priv = GET_PRIV (chat);
389 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
391 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
392 g_slist_free (priv->sent_messages);
394 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
395 g_list_free (priv->compositors);
397 chat_composing_remove_timeout (chat);
398 g_object_unref (priv->log_manager);
400 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
401 G_CALLBACK (chat_status_changed_cb),
403 g_object_unref (priv->mc);
407 g_object_unref (priv->tp_chat);
411 g_object_unref (priv->account);
414 if (priv->scroll_idle_id) {
415 g_source_remove (priv->scroll_idle_id);
418 if (priv->block_events_timeout_id) {
419 g_source_remove (priv->block_events_timeout_id);
424 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
428 chat_destroy_cb (EmpathyTpChat *tp_chat,
431 EmpathyChatPriv *priv;
433 priv = GET_PRIV (chat);
436 g_object_unref (priv->tp_chat);
437 priv->tp_chat = NULL;
439 priv->sensitive = FALSE;
441 empathy_chat_view_append_event (chat->view, _("Disconnected"));
442 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
444 if (priv->block_events_timeout_id != 0) {
445 g_source_remove (priv->block_events_timeout_id);
448 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
449 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
454 chat_send (EmpathyChat *chat,
457 EmpathyChatPriv *priv;
458 EmpathyMessage *message;
460 priv = GET_PRIV (chat);
462 if (G_STR_EMPTY (msg)) {
466 chat_sent_message_add (chat, msg);
468 if (g_str_has_prefix (msg, "/clear")) {
469 empathy_chat_view_clear (chat->view);
473 /* FIXME: add here something to let group/privrate chat handle
474 * some special messages */
476 message = empathy_message_new (msg);
478 empathy_tp_chat_send (priv->tp_chat, message);
480 g_object_unref (message);
484 chat_input_text_view_send (EmpathyChat *chat)
486 EmpathyChatPriv *priv;
487 GtkTextBuffer *buffer;
488 GtkTextIter start, end;
491 priv = GET_PRIV (chat);
493 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
495 gtk_text_buffer_get_bounds (buffer, &start, &end);
496 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
498 /* clear the input field */
499 gtk_text_buffer_set_text (buffer, "", -1);
501 chat_send (chat, msg);
505 priv->is_first_char = TRUE;
509 chat_message_received_cb (EmpathyTpChat *tp_chat,
510 EmpathyMessage *message,
513 EmpathyChatPriv *priv;
514 EmpathyContact *sender;
517 priv = GET_PRIV (chat);
519 sender = empathy_message_get_sender (message);
520 body = empathy_message_get_body (message);
521 while (priv->backlog_messages) {
522 EmpathyMessage *log_message;
523 EmpathyContact *log_sender;
524 const gchar *log_body;
526 log_message = priv->backlog_messages->data;
527 log_sender = empathy_message_get_sender (log_message);
528 log_body = empathy_message_get_body (log_message);
530 priv->backlog_messages = g_list_remove (priv->backlog_messages,
533 if (empathy_contact_equal (sender, log_sender) &&
534 !tp_strdiff (body, log_body)) {
535 /* The message we received is already displayed because
536 * some jabber chatrooms sends us back logs and we
537 * already displayed it from localy logged messages. */
538 empathy_debug (DEBUG_DOMAIN, "Skipping message because "
539 "it is already displayed from logged "
541 g_object_unref (log_message);
544 g_object_unref (log_message);
547 empathy_debug (DEBUG_DOMAIN, "Appending new message from %s (%d)",
548 empathy_contact_get_name (sender),
549 empathy_contact_get_handle (sender));
551 empathy_log_manager_add_message (priv->log_manager,
552 empathy_chat_get_id (chat),
553 empathy_chat_is_group_chat (chat),
556 empathy_chat_view_append_message (chat->view, message);
558 if (empathy_chat_should_play_sound (chat)) {
559 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
562 /* We received a message so the contact is no more composing */
563 chat_state_changed_cb (tp_chat, sender,
564 TP_CHANNEL_CHAT_STATE_ACTIVE,
567 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
571 chat_send_error_cb (EmpathyTpChat *tp_chat,
572 EmpathyMessage *message,
573 TpChannelTextSendError error_code,
579 switch (error_code) {
580 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
581 error = _("offline");
583 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
584 error = _("invalid contact");
586 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
587 error = _("permission denied");
589 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
590 error = _("too long message");
592 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
593 error = _("not implemented");
596 error = _("unknown");
600 str = g_strdup_printf (_("Error sending message '%s': %s"),
601 empathy_message_get_body (message),
603 empathy_chat_view_append_event (chat->view, str);
608 chat_sent_message_add (EmpathyChat *chat,
611 EmpathyChatPriv *priv;
615 priv = GET_PRIV (chat);
617 /* Save the sent message in our repeat buffer */
618 list = priv->sent_messages;
620 /* Remove any other occurances of this msg */
621 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
622 list = g_slist_remove_link (list, item);
624 g_slist_free1 (item);
627 /* Trim the list to the last 10 items */
628 while (g_slist_length (list) > 10) {
629 item = g_slist_last (list);
631 list = g_slist_remove_link (list, item);
633 g_slist_free1 (item);
637 /* Add new message */
638 list = g_slist_prepend (list, g_strdup (str));
640 /* Set list and reset the index */
641 priv->sent_messages = list;
642 priv->sent_messages_index = -1;
646 chat_sent_message_get_next (EmpathyChat *chat)
648 EmpathyChatPriv *priv;
651 priv = GET_PRIV (chat);
653 if (!priv->sent_messages) {
654 empathy_debug (DEBUG_DOMAIN,
655 "No sent messages, next message is NULL");
659 max = g_slist_length (priv->sent_messages) - 1;
661 if (priv->sent_messages_index < max) {
662 priv->sent_messages_index++;
665 empathy_debug (DEBUG_DOMAIN,
666 "Returning next message index:%d",
667 priv->sent_messages_index);
669 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
673 chat_sent_message_get_last (EmpathyChat *chat)
675 EmpathyChatPriv *priv;
677 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
679 priv = GET_PRIV (chat);
681 if (!priv->sent_messages) {
682 empathy_debug (DEBUG_DOMAIN,
683 "No sent messages, last message is NULL");
687 if (priv->sent_messages_index >= 0) {
688 priv->sent_messages_index--;
691 empathy_debug (DEBUG_DOMAIN,
692 "Returning last message index:%d",
693 priv->sent_messages_index);
695 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
699 chat_input_key_press_event_cb (GtkWidget *widget,
703 EmpathyChatPriv *priv;
706 GtkWidget *text_view_sw;
708 priv = GET_PRIV (chat);
710 /* Catch ctrl+up/down so we can traverse messages we sent */
711 if ((event->state & GDK_CONTROL_MASK) &&
712 (event->keyval == GDK_Up ||
713 event->keyval == GDK_Down)) {
714 GtkTextBuffer *buffer;
717 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
719 if (event->keyval == GDK_Up) {
720 str = chat_sent_message_get_next (chat);
722 str = chat_sent_message_get_last (chat);
725 g_signal_handlers_block_by_func (buffer,
726 chat_input_text_buffer_changed_cb,
728 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
729 g_signal_handlers_unblock_by_func (buffer,
730 chat_input_text_buffer_changed_cb,
736 /* Catch enter but not ctrl/shift-enter */
737 if (IS_ENTER (event->keyval) &&
738 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
741 /* This is to make sure that kinput2 gets the enter. And if
742 * it's handled there we shouldn't send on it. This is because
743 * kinput2 uses Enter to commit letters. See:
744 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
747 view = GTK_TEXT_VIEW (chat->input_text_view);
748 if (gtk_im_context_filter_keypress (view->im_context, event)) {
749 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
753 chat_input_text_view_send (chat);
757 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
759 if (IS_ENTER (event->keyval) &&
760 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
761 /* Newline for shift/control-enter. */
764 else if (!(event->state & GDK_CONTROL_MASK) &&
765 event->keyval == GDK_Page_Up) {
766 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
767 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
771 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
772 event->keyval == GDK_Page_Down) {
773 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
774 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
775 gtk_adjustment_set_value (adj, val);
780 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
781 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
788 chat_text_view_focus_in_event_cb (GtkWidget *widget,
792 gtk_widget_grab_focus (chat->input_text_view);
798 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
801 EmpathyChatPriv *priv;
802 GtkTextIter start, end;
804 gboolean spell_checker = FALSE;
806 priv = GET_PRIV (chat);
808 if (gtk_text_buffer_get_char_count (buffer) == 0) {
809 chat_composing_stop (chat);
811 chat_composing_start (chat);
814 empathy_conf_get_bool (empathy_conf_get (),
815 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
818 if (priv->is_first_char) {
822 GtkAllocation *allocation;
824 /* Save the window's size */
825 dialog = empathy_get_toplevel_window (priv->widget);
827 gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
828 gtk_widget_size_request (chat->input_text_view, &req);
829 allocation = >K_WIDGET (chat->view)->allocation;
831 priv->default_window_height = window_height;
832 priv->last_input_height = req.height;
833 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 (data->window, data->width, data->height);
912 chat_text_view_scroll_hide_cb (GtkWidget *widget,
915 EmpathyChatPriv *priv;
918 priv = GET_PRIV (chat);
920 priv->vscroll_visible = FALSE;
921 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
923 sw = gtk_widget_get_parent (chat->input_text_view);
924 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
927 g_object_set (sw, "height-request", -1, NULL);
931 chat_text_view_size_allocate_cb (GtkWidget *widget,
932 GtkAllocation *allocation,
935 EmpathyChatPriv *priv;
938 ChangeSizeData *data;
941 GtkAllocation *view_allocation;
946 priv = GET_PRIV (chat);
948 if (priv->default_window_height <= 0) {
952 sw = gtk_widget_get_parent (widget);
953 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
956 priv->vscroll_visible = TRUE;
957 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
958 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
960 GTK_POLICY_AUTOMATIC);
961 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
962 g_signal_connect (vscroll, "hide",
963 G_CALLBACK (chat_text_view_scroll_hide_cb),
967 if (priv->last_input_height <= allocation->height) {
968 priv->last_input_height = allocation->height;
972 diff = priv->last_input_height - allocation->height;
973 priv->last_input_height = allocation->height;
975 view_allocation = >K_WIDGET (chat->view)->allocation;
977 dialog = empathy_get_toplevel_window (priv->widget);
978 gtk_window_get_size (dialog, NULL, ¤t_height);
980 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
982 if (new_height <= priv->default_window_height) {
983 window_height = priv->default_window_height;
985 window_height = new_height;
988 if (current_height <= window_height) {
992 /* Restore the window's size */
993 gtk_window_get_size (dialog, &width, NULL);
995 data = g_new0 (ChangeSizeData, 1);
996 data->window = dialog;
998 data->height = window_height;
1000 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1001 (GSourceFunc) chat_change_size_in_idle_cb,
1006 chat_text_view_realize_cb (GtkWidget *widget,
1009 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
1010 gtk_widget_grab_focus (widget);
1014 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1017 GtkTextBuffer *buffer;
1019 const gchar *smiley;
1021 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1023 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1025 gtk_text_buffer_get_end_iter (buffer, &iter);
1026 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1028 gtk_text_buffer_get_end_iter (buffer, &iter);
1029 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1033 chat_text_populate_popup_cb (GtkTextView *view,
1037 EmpathyChatPriv *priv;
1038 GtkTextBuffer *buffer;
1039 GtkTextTagTable *table;
1042 GtkTextIter iter, start, end;
1045 EmpathyChatSpell *chat_spell;
1046 GtkWidget *smiley_menu;
1048 priv = GET_PRIV (chat);
1050 /* Add the emoticon menu. */
1051 item = gtk_separator_menu_item_new ();
1052 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1053 gtk_widget_show (item);
1055 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1056 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1057 gtk_widget_show (item);
1059 smiley_menu = empathy_chat_view_get_smiley_menu (
1060 G_CALLBACK (chat_insert_smiley_activate_cb),
1062 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1064 /* Add the spell check menu item. */
1065 buffer = gtk_text_view_get_buffer (view);
1066 table = gtk_text_buffer_get_tag_table (buffer);
1068 tag = gtk_text_tag_table_lookup (table, "misspelled");
1070 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1072 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1073 GTK_TEXT_WINDOW_WIDGET,
1077 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1081 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1082 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1084 str = gtk_text_buffer_get_text (buffer,
1085 &start, &end, FALSE);
1088 if (G_STR_EMPTY (str)) {
1092 chat_spell = chat_spell_new (chat, str, start, end);
1094 g_object_set_data_full (G_OBJECT (menu),
1095 "chat_spell", chat_spell,
1096 (GDestroyNotify) chat_spell_free);
1098 item = gtk_separator_menu_item_new ();
1099 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1100 gtk_widget_show (item);
1102 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1103 g_signal_connect (item,
1105 G_CALLBACK (chat_text_check_word_spelling_cb),
1107 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1108 gtk_widget_show (item);
1112 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1113 EmpathyChatSpell *chat_spell)
1115 empathy_spell_dialog_show (chat_spell->chat,
1121 static EmpathyChatSpell *
1122 chat_spell_new (EmpathyChat *chat,
1127 EmpathyChatSpell *chat_spell;
1129 chat_spell = g_new0 (EmpathyChatSpell, 1);
1131 chat_spell->chat = g_object_ref (chat);
1132 chat_spell->word = g_strdup (word);
1133 chat_spell->start = start;
1134 chat_spell->end = end;
1140 chat_spell_free (EmpathyChatSpell *chat_spell)
1142 g_object_unref (chat_spell->chat);
1143 g_free (chat_spell->word);
1144 g_free (chat_spell);
1148 chat_composing_start (EmpathyChat *chat)
1150 EmpathyChatPriv *priv;
1152 priv = GET_PRIV (chat);
1154 if (priv->composing_stop_timeout_id) {
1155 /* Just restart the timeout */
1156 chat_composing_remove_timeout (chat);
1158 empathy_tp_chat_set_state (priv->tp_chat,
1159 TP_CHANNEL_CHAT_STATE_COMPOSING);
1162 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1163 COMPOSING_STOP_TIMEOUT,
1164 (GSourceFunc) chat_composing_stop_timeout_cb,
1169 chat_composing_stop (EmpathyChat *chat)
1171 EmpathyChatPriv *priv;
1173 priv = GET_PRIV (chat);
1175 chat_composing_remove_timeout (chat);
1176 empathy_tp_chat_set_state (priv->tp_chat,
1177 TP_CHANNEL_CHAT_STATE_ACTIVE);
1181 chat_composing_remove_timeout (EmpathyChat *chat)
1183 EmpathyChatPriv *priv;
1185 priv = GET_PRIV (chat);
1187 if (priv->composing_stop_timeout_id) {
1188 g_source_remove (priv->composing_stop_timeout_id);
1189 priv->composing_stop_timeout_id = 0;
1194 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1196 EmpathyChatPriv *priv;
1198 priv = GET_PRIV (chat);
1200 priv->composing_stop_timeout_id = 0;
1201 empathy_tp_chat_set_state (priv->tp_chat,
1202 TP_CHANNEL_CHAT_STATE_PAUSED);
1208 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1209 EmpathyContact *contact,
1210 TpChannelChatState state,
1213 EmpathyChatPriv *priv;
1215 gboolean was_composing;
1217 priv = GET_PRIV (chat);
1219 if (empathy_contact_is_user (contact)) {
1220 /* We don't care about our own chat state */
1224 was_composing = (priv->compositors != NULL);
1226 /* Find the contact in the list. After that l is the list elem or NULL */
1227 for (l = priv->compositors; l; l = l->next) {
1228 if (contact == l->data) {
1234 case TP_CHANNEL_CHAT_STATE_GONE:
1235 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1236 case TP_CHANNEL_CHAT_STATE_PAUSED:
1237 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1238 /* Contact is not composing */
1240 priv->compositors = g_list_remove_link (priv->compositors, l);
1241 g_object_unref (l->data);
1245 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1246 /* Contact is composing */
1248 priv->compositors = g_list_prepend (priv->compositors,
1249 g_object_ref (contact));
1253 g_assert_not_reached ();
1256 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1257 was_composing ? "yes" : "no",
1258 priv->compositors ? "yes" : "no");
1260 if ((was_composing && !priv->compositors) ||
1261 (!was_composing && priv->compositors)) {
1262 /* Composing state changed */
1263 g_signal_emit (chat, signals[COMPOSING], 0,
1264 priv->compositors != NULL);
1269 chat_add_logs (EmpathyChat *chat)
1271 EmpathyChatPriv *priv;
1272 GList *messages, *l;
1276 priv = GET_PRIV (chat);
1278 /* Turn off scrolling temporarily */
1279 empathy_chat_view_scroll (chat->view, FALSE);
1281 /* Add messages from last conversation */
1282 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1284 empathy_chat_get_id (chat),
1285 empathy_chat_is_group_chat (chat));
1286 num_messages = g_list_length (messages);
1288 /* Only keep the 10 last messages */
1289 for (i = 0; num_messages - i > 10; i++) {
1290 EmpathyMessage *message;
1292 message = messages->data;
1293 messages = g_list_remove (messages, message);
1294 g_object_unref (message);
1297 for (l = messages; l; l = l->next) {
1298 empathy_chat_view_append_message (chat->view, l->data);
1300 priv->backlog_messages = messages;
1302 /* Turn back on scrolling */
1303 empathy_chat_view_scroll (chat->view, TRUE);
1305 /* Scroll to the most recent messages, we reference the chat
1306 * for the duration of the scroll func.
1308 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1309 g_object_ref (chat));
1312 /* Scroll down after the back-log has been received. */
1314 chat_scroll_down_idle_func (EmpathyChat *chat)
1316 EmpathyChatPriv *priv;
1318 priv = GET_PRIV (chat);
1320 empathy_chat_scroll_down (chat);
1321 g_object_unref (chat);
1323 priv->scroll_idle_id = 0;
1329 empathy_chat_get_is_command (const gchar *str)
1331 g_return_val_if_fail (str != NULL, FALSE);
1333 if (str[0] != '/') {
1337 if (g_str_has_prefix (str, "/me")) {
1340 else if (g_str_has_prefix (str, "/nick")) {
1343 else if (g_str_has_prefix (str, "/topic")) {
1351 empathy_chat_correct_word (EmpathyChat *chat,
1354 const gchar *new_word)
1356 GtkTextBuffer *buffer;
1358 g_return_if_fail (chat != NULL);
1359 g_return_if_fail (new_word != NULL);
1361 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1363 gtk_text_buffer_delete (buffer, &start, &end);
1364 gtk_text_buffer_insert (buffer, &start,
1370 empathy_chat_get_name (EmpathyChat *chat)
1372 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1374 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1375 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1382 empathy_chat_get_tooltip (EmpathyChat *chat)
1384 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1386 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1387 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1394 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1396 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1398 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1399 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1406 empathy_chat_get_widget (EmpathyChat *chat)
1408 EmpathyChatPriv *priv = GET_PRIV (chat);
1410 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1412 if (!priv->widget && EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1413 priv->widget = EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1416 return priv->widget;
1420 empathy_chat_is_group_chat (EmpathyChat *chat)
1422 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1424 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1425 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1432 empathy_chat_is_connected (EmpathyChat *chat)
1434 EmpathyChatPriv *priv;
1436 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1438 priv = GET_PRIV (chat);
1440 return (priv->tp_chat != NULL);
1443 static const gchar *
1444 chat_get_window_id_for_geometry (EmpathyChat *chat)
1446 gboolean separate_windows;
1448 empathy_conf_get_bool (empathy_conf_get (),
1449 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1452 if (separate_windows) {
1453 return empathy_chat_get_id (chat);
1455 return "chat-window";
1460 empathy_chat_save_geometry (EmpathyChat *chat,
1466 empathy_geometry_save (chat_get_window_id_for_geometry (chat), x, y, w, h);
1470 empathy_chat_load_geometry (EmpathyChat *chat,
1476 empathy_geometry_load (chat_get_window_id_for_geometry (chat), x, y, w, h);
1480 chat_block_events_timeout_cb (gpointer data)
1482 EmpathyChat *chat = EMPATHY_CHAT (data);
1483 EmpathyChatPriv *priv = GET_PRIV (chat);
1485 chat->block_events = FALSE;
1486 priv->block_events_timeout_id = 0;
1492 empathy_chat_set_tp_chat (EmpathyChat *chat,
1493 EmpathyTpChat *tp_chat)
1495 EmpathyChatPriv *priv;
1497 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1498 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1500 priv = GET_PRIV (chat);
1502 if (tp_chat == priv->tp_chat) {
1506 /* Block events for some time to avoid having "has come online" or
1507 * "joined" messages. */
1508 chat->block_events = TRUE;
1509 if (priv->block_events_timeout_id != 0) {
1510 g_source_remove (priv->block_events_timeout_id);
1512 priv->block_events_timeout_id =
1513 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1515 if (priv->tp_chat) {
1516 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1517 chat_message_received_cb,
1519 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1522 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1525 g_object_unref (priv->tp_chat);
1527 if (priv->account) {
1528 g_object_unref (priv->account);
1532 priv->tp_chat = g_object_ref (tp_chat);
1533 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1534 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1535 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1537 if (priv->first_tp_chat) {
1538 chat_add_logs (chat);
1539 priv->first_tp_chat = FALSE;
1542 g_signal_connect (tp_chat, "message-received",
1543 G_CALLBACK (chat_message_received_cb),
1545 g_signal_connect (tp_chat, "send-error",
1546 G_CALLBACK (chat_send_error_cb),
1548 g_signal_connect (tp_chat, "chat-state-changed",
1549 G_CALLBACK (chat_state_changed_cb),
1551 g_signal_connect (tp_chat, "destroy",
1552 G_CALLBACK (chat_destroy_cb),
1555 if (!priv->sensitive) {
1556 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1557 empathy_chat_view_append_event (chat->view, _("Connected"));
1558 priv->sensitive = TRUE;
1561 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1562 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1565 g_object_notify (G_OBJECT (chat), "tp-chat");
1569 empathy_chat_get_id (EmpathyChat *chat)
1571 EmpathyChatPriv *priv;
1573 priv = GET_PRIV (chat);
1579 empathy_chat_get_account (EmpathyChat *chat)
1581 EmpathyChatPriv *priv = GET_PRIV (chat);
1583 return priv->account;
1587 empathy_chat_clear (EmpathyChat *chat)
1589 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1591 empathy_chat_view_clear (chat->view);
1595 empathy_chat_scroll_down (EmpathyChat *chat)
1597 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1599 empathy_chat_view_scroll_down (chat->view);
1603 empathy_chat_cut (EmpathyChat *chat)
1605 GtkTextBuffer *buffer;
1607 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1609 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1610 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1611 GtkClipboard *clipboard;
1613 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1615 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1620 empathy_chat_copy (EmpathyChat *chat)
1622 GtkTextBuffer *buffer;
1624 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1626 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1627 empathy_chat_view_copy_clipboard (chat->view);
1631 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1632 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1633 GtkClipboard *clipboard;
1635 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1637 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1642 empathy_chat_paste (EmpathyChat *chat)
1644 GtkTextBuffer *buffer;
1645 GtkClipboard *clipboard;
1647 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1649 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1650 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1652 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1656 empathy_chat_should_play_sound (EmpathyChat *chat)
1658 EmpathyChatPriv *priv = GET_PRIV (chat);
1660 gboolean has_focus = FALSE;
1662 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1664 window = empathy_get_toplevel_window (priv->widget);
1666 g_object_get (window, "has-toplevel-focus", &has_focus, NULL);
1673 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1675 EmpathyContact *contact;
1676 const gchar *msg, *to;
1677 gchar *cf_msg, *cf_to;
1681 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1683 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1687 msg = empathy_message_get_body (message);
1692 contact = empathy_message_get_receiver (message);
1693 if (!contact || !empathy_contact_is_user (contact)) {
1697 to = empathy_contact_get_name (contact);
1702 cf_msg = g_utf8_casefold (msg, -1);
1703 cf_to = g_utf8_casefold (to, -1);
1705 ch = strstr (cf_msg, cf_to);
1711 /* Not first in the message */
1712 if ((*(ch - 1) != ' ') &&
1713 (*(ch - 1) != ',') &&
1714 (*(ch - 1) != '.')) {
1719 ch = ch + strlen (cf_to);
1720 if (ch >= cf_msg + strlen (cf_msg)) {