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 /* 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 (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
445 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
450 chat_send (EmpathyChat *chat,
453 EmpathyChatPriv *priv;
454 EmpathyMessage *message;
456 priv = GET_PRIV (chat);
458 if (G_STR_EMPTY (msg)) {
462 chat_sent_message_add (chat, msg);
464 if (g_str_has_prefix (msg, "/clear")) {
465 empathy_chat_view_clear (chat->view);
469 /* FIXME: add here something to let group/privrate chat handle
470 * some special messages */
472 message = empathy_message_new (msg);
474 empathy_tp_chat_send (priv->tp_chat, message);
476 g_object_unref (message);
480 chat_input_text_view_send (EmpathyChat *chat)
482 EmpathyChatPriv *priv;
483 GtkTextBuffer *buffer;
484 GtkTextIter start, end;
487 priv = GET_PRIV (chat);
489 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
491 gtk_text_buffer_get_bounds (buffer, &start, &end);
492 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
494 /* clear the input field */
495 gtk_text_buffer_set_text (buffer, "", -1);
497 chat_send (chat, msg);
501 priv->is_first_char = TRUE;
505 chat_message_received_cb (EmpathyTpChat *tp_chat,
506 EmpathyMessage *message,
509 EmpathyChatPriv *priv;
510 EmpathyContact *sender;
513 priv = GET_PRIV (chat);
515 timestamp = empathy_message_get_timestamp (message);
516 if (timestamp <= priv->last_log_timestamp) {
517 /* Do not take care of messages anterior of the last
518 * logged message. Some Jabber chatroom sends messages
519 * received before we joined the room, this avoid
520 * displaying those messages if we already logged them
521 * last time we joined that room. */
522 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
523 "anterior of last logged message.");
527 sender = empathy_message_get_sender (message);
528 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
529 empathy_contact_get_name (sender));
531 empathy_log_manager_add_message (priv->log_manager,
532 empathy_chat_get_id (chat),
533 empathy_chat_is_group_chat (chat),
536 empathy_chat_view_append_message (chat->view, message);
538 if (empathy_chat_should_play_sound (chat)) {
539 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
542 /* We received a message so the contact is no more composing */
543 chat_state_changed_cb (tp_chat, sender,
544 TP_CHANNEL_CHAT_STATE_ACTIVE,
547 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
551 chat_send_error_cb (EmpathyTpChat *tp_chat,
552 EmpathyMessage *message,
553 TpChannelTextSendError error_code,
559 switch (error_code) {
560 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
561 error = _("offline");
563 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
564 error = _("invalid contact");
566 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
567 error = _("permission denied");
569 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
570 error = _("too long message");
572 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
573 error = _("not implemented");
576 error = _("unknown");
580 str = g_strdup_printf (_("Error sending message '%s': %s"),
581 empathy_message_get_body (message),
583 empathy_chat_view_append_event (chat->view, str);
588 chat_sent_message_add (EmpathyChat *chat,
591 EmpathyChatPriv *priv;
595 priv = GET_PRIV (chat);
597 /* Save the sent message in our repeat buffer */
598 list = priv->sent_messages;
600 /* Remove any other occurances of this msg */
601 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
602 list = g_slist_remove_link (list, item);
604 g_slist_free1 (item);
607 /* Trim the list to the last 10 items */
608 while (g_slist_length (list) > 10) {
609 item = g_slist_last (list);
611 list = g_slist_remove_link (list, item);
613 g_slist_free1 (item);
617 /* Add new message */
618 list = g_slist_prepend (list, g_strdup (str));
620 /* Set list and reset the index */
621 priv->sent_messages = list;
622 priv->sent_messages_index = -1;
626 chat_sent_message_get_next (EmpathyChat *chat)
628 EmpathyChatPriv *priv;
631 priv = GET_PRIV (chat);
633 if (!priv->sent_messages) {
634 empathy_debug (DEBUG_DOMAIN,
635 "No sent messages, next message is NULL");
639 max = g_slist_length (priv->sent_messages) - 1;
641 if (priv->sent_messages_index < max) {
642 priv->sent_messages_index++;
645 empathy_debug (DEBUG_DOMAIN,
646 "Returning next message index:%d",
647 priv->sent_messages_index);
649 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
653 chat_sent_message_get_last (EmpathyChat *chat)
655 EmpathyChatPriv *priv;
657 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
659 priv = GET_PRIV (chat);
661 if (!priv->sent_messages) {
662 empathy_debug (DEBUG_DOMAIN,
663 "No sent messages, last message is NULL");
667 if (priv->sent_messages_index >= 0) {
668 priv->sent_messages_index--;
671 empathy_debug (DEBUG_DOMAIN,
672 "Returning last message index:%d",
673 priv->sent_messages_index);
675 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
679 chat_input_key_press_event_cb (GtkWidget *widget,
683 EmpathyChatPriv *priv;
686 GtkWidget *text_view_sw;
688 priv = GET_PRIV (chat);
690 /* Catch ctrl+up/down so we can traverse messages we sent */
691 if ((event->state & GDK_CONTROL_MASK) &&
692 (event->keyval == GDK_Up ||
693 event->keyval == GDK_Down)) {
694 GtkTextBuffer *buffer;
697 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
699 if (event->keyval == GDK_Up) {
700 str = chat_sent_message_get_next (chat);
702 str = chat_sent_message_get_last (chat);
705 g_signal_handlers_block_by_func (buffer,
706 chat_input_text_buffer_changed_cb,
708 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
709 g_signal_handlers_unblock_by_func (buffer,
710 chat_input_text_buffer_changed_cb,
716 /* Catch enter but not ctrl/shift-enter */
717 if (IS_ENTER (event->keyval) &&
718 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
721 /* This is to make sure that kinput2 gets the enter. And if
722 * it's handled there we shouldn't send on it. This is because
723 * kinput2 uses Enter to commit letters. See:
724 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
727 view = GTK_TEXT_VIEW (chat->input_text_view);
728 if (gtk_im_context_filter_keypress (view->im_context, event)) {
729 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
733 chat_input_text_view_send (chat);
737 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
739 if (IS_ENTER (event->keyval) &&
740 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
741 /* Newline for shift/control-enter. */
744 else if (!(event->state & GDK_CONTROL_MASK) &&
745 event->keyval == GDK_Page_Up) {
746 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
747 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
751 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
752 event->keyval == GDK_Page_Down) {
753 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
754 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
755 gtk_adjustment_set_value (adj, val);
760 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
761 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
768 chat_text_view_focus_in_event_cb (GtkWidget *widget,
772 gtk_widget_grab_focus (chat->input_text_view);
778 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
781 EmpathyChatPriv *priv;
782 GtkTextIter start, end;
784 gboolean spell_checker = FALSE;
786 priv = GET_PRIV (chat);
788 if (gtk_text_buffer_get_char_count (buffer) == 0) {
789 chat_composing_stop (chat);
791 chat_composing_start (chat);
794 empathy_conf_get_bool (empathy_conf_get (),
795 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
798 if (priv->is_first_char) {
802 GtkAllocation *allocation;
804 /* Save the window's size */
805 dialog = empathy_chat_window_get_dialog (priv->window);
806 gtk_window_get_size (GTK_WINDOW (dialog),
807 NULL, &window_height);
809 gtk_widget_size_request (chat->input_text_view, &req);
811 allocation = >K_WIDGET (chat->view)->allocation;
813 priv->default_window_height = window_height;
814 priv->last_input_height = req.height;
815 priv->padding_height = window_height - req.height - allocation->height;
817 priv->is_first_char = FALSE;
820 gtk_text_buffer_get_start_iter (buffer, &start);
822 if (!spell_checker) {
823 gtk_text_buffer_get_end_iter (buffer, &end);
824 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
828 if (!empathy_spell_supported ()) {
832 /* NOTE: this is really inefficient, we shouldn't have to
833 reiterate the whole buffer each time and check each work
836 gboolean correct = FALSE;
839 if (gtk_text_iter_is_start (&start)) {
842 if (!gtk_text_iter_forward_word_end (&end)) {
843 /* no whole word yet */
847 if (!gtk_text_iter_forward_word_end (&end)) {
848 /* must be the end of the buffer */
853 gtk_text_iter_backward_word_start (&start);
856 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
858 /* spell check string */
859 if (!empathy_chat_get_is_command (str)) {
860 correct = empathy_spell_check (str);
866 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
868 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
873 /* set start iter to the end iters position */
885 chat_change_size_in_idle_cb (ChangeSizeData *data)
887 gtk_window_resize (GTK_WINDOW (data->window),
888 data->width, data->height);
894 chat_text_view_scroll_hide_cb (GtkWidget *widget,
897 EmpathyChatPriv *priv;
900 priv = GET_PRIV (chat);
902 priv->vscroll_visible = FALSE;
903 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
905 sw = gtk_widget_get_parent (chat->input_text_view);
906 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
909 g_object_set (sw, "height-request", -1, NULL);
913 chat_text_view_size_allocate_cb (GtkWidget *widget,
914 GtkAllocation *allocation,
917 EmpathyChatPriv *priv;
920 ChangeSizeData *data;
923 GtkAllocation *view_allocation;
928 priv = GET_PRIV (chat);
930 if (priv->default_window_height <= 0) {
934 sw = gtk_widget_get_parent (widget);
935 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
938 priv->vscroll_visible = TRUE;
939 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
940 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
942 GTK_POLICY_AUTOMATIC);
943 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
944 g_signal_connect (vscroll, "hide",
945 G_CALLBACK (chat_text_view_scroll_hide_cb),
949 if (priv->last_input_height <= allocation->height) {
950 priv->last_input_height = allocation->height;
954 diff = priv->last_input_height - allocation->height;
955 priv->last_input_height = allocation->height;
957 view_allocation = >K_WIDGET (chat->view)->allocation;
959 dialog = empathy_chat_window_get_dialog (priv->window);
960 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
962 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
964 if (new_height <= priv->default_window_height) {
965 window_height = priv->default_window_height;
967 window_height = new_height;
970 if (current_height <= window_height) {
974 /* Restore the window's size */
975 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
977 data = g_new0 (ChangeSizeData, 1);
978 data->window = dialog;
980 data->height = window_height;
982 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
983 (GSourceFunc) chat_change_size_in_idle_cb,
988 chat_text_view_realize_cb (GtkWidget *widget,
991 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
992 gtk_widget_grab_focus (widget);
996 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
999 GtkTextBuffer *buffer;
1001 const gchar *smiley;
1003 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1005 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1007 gtk_text_buffer_get_end_iter (buffer, &iter);
1008 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1010 gtk_text_buffer_get_end_iter (buffer, &iter);
1011 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1015 chat_text_populate_popup_cb (GtkTextView *view,
1019 EmpathyChatPriv *priv;
1020 GtkTextBuffer *buffer;
1021 GtkTextTagTable *table;
1024 GtkTextIter iter, start, end;
1027 EmpathyChatSpell *chat_spell;
1028 GtkWidget *smiley_menu;
1030 priv = GET_PRIV (chat);
1032 /* Add the emoticon menu. */
1033 item = gtk_separator_menu_item_new ();
1034 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1035 gtk_widget_show (item);
1037 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1038 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1039 gtk_widget_show (item);
1041 smiley_menu = empathy_chat_view_get_smiley_menu (
1042 G_CALLBACK (chat_insert_smiley_activate_cb),
1044 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1046 /* Add the spell check menu item. */
1047 buffer = gtk_text_view_get_buffer (view);
1048 table = gtk_text_buffer_get_tag_table (buffer);
1050 tag = gtk_text_tag_table_lookup (table, "misspelled");
1052 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1054 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1055 GTK_TEXT_WINDOW_WIDGET,
1059 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1063 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1064 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1066 str = gtk_text_buffer_get_text (buffer,
1067 &start, &end, FALSE);
1070 if (G_STR_EMPTY (str)) {
1074 chat_spell = chat_spell_new (chat, str, start, end);
1076 g_object_set_data_full (G_OBJECT (menu),
1077 "chat_spell", chat_spell,
1078 (GDestroyNotify) chat_spell_free);
1080 item = gtk_separator_menu_item_new ();
1081 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1082 gtk_widget_show (item);
1084 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1085 g_signal_connect (item,
1087 G_CALLBACK (chat_text_check_word_spelling_cb),
1089 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1090 gtk_widget_show (item);
1094 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1095 EmpathyChatSpell *chat_spell)
1097 empathy_spell_dialog_show (chat_spell->chat,
1103 static EmpathyChatSpell *
1104 chat_spell_new (EmpathyChat *chat,
1109 EmpathyChatSpell *chat_spell;
1111 chat_spell = g_new0 (EmpathyChatSpell, 1);
1113 chat_spell->chat = g_object_ref (chat);
1114 chat_spell->word = g_strdup (word);
1115 chat_spell->start = start;
1116 chat_spell->end = end;
1122 chat_spell_free (EmpathyChatSpell *chat_spell)
1124 g_object_unref (chat_spell->chat);
1125 g_free (chat_spell->word);
1126 g_free (chat_spell);
1130 chat_composing_start (EmpathyChat *chat)
1132 EmpathyChatPriv *priv;
1134 priv = GET_PRIV (chat);
1136 if (priv->composing_stop_timeout_id) {
1137 /* Just restart the timeout */
1138 chat_composing_remove_timeout (chat);
1140 empathy_tp_chat_set_state (priv->tp_chat,
1141 TP_CHANNEL_CHAT_STATE_COMPOSING);
1144 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1145 COMPOSING_STOP_TIMEOUT,
1146 (GSourceFunc) chat_composing_stop_timeout_cb,
1151 chat_composing_stop (EmpathyChat *chat)
1153 EmpathyChatPriv *priv;
1155 priv = GET_PRIV (chat);
1157 chat_composing_remove_timeout (chat);
1158 empathy_tp_chat_set_state (priv->tp_chat,
1159 TP_CHANNEL_CHAT_STATE_ACTIVE);
1163 chat_composing_remove_timeout (EmpathyChat *chat)
1165 EmpathyChatPriv *priv;
1167 priv = GET_PRIV (chat);
1169 if (priv->composing_stop_timeout_id) {
1170 g_source_remove (priv->composing_stop_timeout_id);
1171 priv->composing_stop_timeout_id = 0;
1176 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1178 EmpathyChatPriv *priv;
1180 priv = GET_PRIV (chat);
1182 priv->composing_stop_timeout_id = 0;
1183 empathy_tp_chat_set_state (priv->tp_chat,
1184 TP_CHANNEL_CHAT_STATE_PAUSED);
1190 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1191 EmpathyContact *contact,
1192 TpChannelChatState state,
1195 EmpathyChatPriv *priv;
1197 gboolean was_composing;
1199 priv = GET_PRIV (chat);
1201 if (empathy_contact_is_user (contact)) {
1202 /* We don't care about our own chat state */
1206 was_composing = (priv->compositors != NULL);
1208 /* Find the contact in the list. After that l is the list elem or NULL */
1209 for (l = priv->compositors; l; l = l->next) {
1210 if (empathy_contact_equal (contact, l->data)) {
1216 case TP_CHANNEL_CHAT_STATE_GONE:
1217 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1218 case TP_CHANNEL_CHAT_STATE_PAUSED:
1219 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1220 /* Contact is not composing */
1222 priv->compositors = g_list_remove_link (priv->compositors, l);
1223 g_object_unref (l->data);
1227 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1228 /* Contact is composing */
1230 priv->compositors = g_list_prepend (priv->compositors,
1231 g_object_ref (contact));
1235 g_assert_not_reached ();
1238 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1239 was_composing ? "yes" : "no",
1240 priv->compositors ? "yes" : "no");
1242 if ((was_composing && !priv->compositors) ||
1243 (!was_composing && priv->compositors)) {
1244 /* Composing state changed */
1245 g_signal_emit (chat, signals[COMPOSING], 0,
1246 priv->compositors != NULL);
1251 chat_add_logs (EmpathyChat *chat)
1253 EmpathyChatPriv *priv;
1254 GList *messages, *l;
1258 priv = GET_PRIV (chat);
1260 /* Turn off scrolling temporarily */
1261 empathy_chat_view_scroll (chat->view, FALSE);
1263 /* Add messages from last conversation */
1264 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1266 empathy_chat_get_id (chat),
1267 empathy_chat_is_group_chat (chat));
1268 num_messages = g_list_length (messages);
1270 for (l = messages, i = 0; l; l = l->next, i++) {
1271 EmpathyMessage *message;
1275 /* Only add 10 last messages */
1276 if (num_messages - i > 10) {
1277 g_object_unref (message);
1281 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1282 empathy_chat_view_append_message (chat->view, message);
1284 g_object_unref (message);
1286 g_list_free (messages);
1288 /* Turn back on scrolling */
1289 empathy_chat_view_scroll (chat->view, TRUE);
1291 /* Scroll to the most recent messages, we reference the chat
1292 * for the duration of the scroll func.
1294 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1295 g_object_ref (chat));
1298 /* Scroll down after the back-log has been received. */
1300 chat_scroll_down_idle_func (EmpathyChat *chat)
1302 EmpathyChatPriv *priv;
1304 priv = GET_PRIV (chat);
1306 empathy_chat_scroll_down (chat);
1307 g_object_unref (chat);
1309 priv->scroll_idle_id = 0;
1315 empathy_chat_get_is_command (const gchar *str)
1317 g_return_val_if_fail (str != NULL, FALSE);
1319 if (str[0] != '/') {
1323 if (g_str_has_prefix (str, "/me")) {
1326 else if (g_str_has_prefix (str, "/nick")) {
1329 else if (g_str_has_prefix (str, "/topic")) {
1337 empathy_chat_correct_word (EmpathyChat *chat,
1340 const gchar *new_word)
1342 GtkTextBuffer *buffer;
1344 g_return_if_fail (chat != NULL);
1345 g_return_if_fail (new_word != NULL);
1347 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1349 gtk_text_buffer_delete (buffer, &start, &end);
1350 gtk_text_buffer_insert (buffer, &start,
1356 empathy_chat_get_name (EmpathyChat *chat)
1358 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1360 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1361 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1368 empathy_chat_get_tooltip (EmpathyChat *chat)
1370 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1372 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1373 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1380 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1382 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1384 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1385 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1392 empathy_chat_get_widget (EmpathyChat *chat)
1394 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1396 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1397 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1404 empathy_chat_is_group_chat (EmpathyChat *chat)
1406 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1408 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1409 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1416 empathy_chat_is_connected (EmpathyChat *chat)
1418 EmpathyChatPriv *priv;
1420 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1422 priv = GET_PRIV (chat);
1424 return (priv->tp_chat != NULL);
1428 empathy_chat_save_geometry (EmpathyChat *chat,
1434 empathy_geometry_save (empathy_chat_get_id (chat), x, y, w, h);
1438 empathy_chat_load_geometry (EmpathyChat *chat,
1444 empathy_geometry_load (empathy_chat_get_id (chat), x, y, w, h);
1448 chat_block_events_timeout_cb (gpointer data)
1450 EmpathyChat *chat = EMPATHY_CHAT (data);
1451 EmpathyChatPriv *priv = GET_PRIV (chat);
1453 chat->block_events = FALSE;
1454 priv->block_events_timeout_id = 0;
1460 empathy_chat_set_tp_chat (EmpathyChat *chat,
1461 EmpathyTpChat *tp_chat)
1463 EmpathyChatPriv *priv;
1464 GList *messages, *l;
1466 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1467 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1469 priv = GET_PRIV (chat);
1471 if (tp_chat == priv->tp_chat) {
1475 /* Block events for some time to avoid having "has come online" or
1476 * "joined" messages. */
1477 chat->block_events = TRUE;
1478 if (priv->block_events_timeout_id != 0) {
1479 g_source_remove (priv->block_events_timeout_id);
1481 priv->block_events_timeout_id =
1482 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1484 if (priv->tp_chat) {
1485 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1486 chat_message_received_cb,
1488 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1491 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1494 g_object_unref (priv->tp_chat);
1496 if (priv->account) {
1497 g_object_unref (priv->account);
1501 priv->tp_chat = g_object_ref (tp_chat);
1502 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1503 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1505 if (priv->first_tp_chat) {
1506 chat_add_logs (chat);
1507 priv->first_tp_chat = FALSE;
1510 g_signal_connect (tp_chat, "message-received",
1511 G_CALLBACK (chat_message_received_cb),
1513 g_signal_connect (tp_chat, "send-error",
1514 G_CALLBACK (chat_send_error_cb),
1516 g_signal_connect (tp_chat, "chat-state-changed",
1517 G_CALLBACK (chat_state_changed_cb),
1519 g_signal_connect (tp_chat, "destroy",
1520 G_CALLBACK (chat_destroy_cb),
1523 /* Get pending messages */
1524 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1525 messages = empathy_tp_chat_get_pendings (tp_chat);
1526 for (l = messages; l; l = l->next) {
1527 chat_message_received_cb (tp_chat, l->data, chat);
1528 g_object_unref (l->data);
1530 g_list_free (messages);
1532 if (!priv->sensitive) {
1533 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1534 empathy_chat_view_append_event (chat->view, _("Connected"));
1535 priv->sensitive = TRUE;
1538 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1539 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1542 g_object_notify (G_OBJECT (chat), "tp-chat");
1546 empathy_chat_get_id (EmpathyChat *chat)
1548 EmpathyChatPriv *priv;
1550 priv = GET_PRIV (chat);
1556 empathy_chat_get_account (EmpathyChat *chat)
1558 EmpathyChatPriv *priv = GET_PRIV (chat);
1560 return priv->account;
1564 empathy_chat_clear (EmpathyChat *chat)
1566 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1568 empathy_chat_view_clear (chat->view);
1572 empathy_chat_set_window (EmpathyChat *chat,
1573 EmpathyChatWindow *window)
1575 EmpathyChatPriv *priv;
1577 priv = GET_PRIV (chat);
1578 priv->window = window;
1582 empathy_chat_get_window (EmpathyChat *chat)
1584 EmpathyChatPriv *priv;
1586 priv = GET_PRIV (chat);
1588 return priv->window;
1592 empathy_chat_scroll_down (EmpathyChat *chat)
1594 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1596 empathy_chat_view_scroll_down (chat->view);
1600 empathy_chat_cut (EmpathyChat *chat)
1602 GtkTextBuffer *buffer;
1604 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1606 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1607 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1608 GtkClipboard *clipboard;
1610 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1612 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1617 empathy_chat_copy (EmpathyChat *chat)
1619 GtkTextBuffer *buffer;
1621 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1623 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1624 empathy_chat_view_copy_clipboard (chat->view);
1628 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1629 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1630 GtkClipboard *clipboard;
1632 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1634 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1639 empathy_chat_paste (EmpathyChat *chat)
1641 GtkTextBuffer *buffer;
1642 GtkClipboard *clipboard;
1644 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1646 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1647 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1649 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1653 empathy_chat_present (EmpathyChat *chat)
1655 EmpathyChatPriv *priv;
1657 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1659 priv = GET_PRIV (chat);
1661 if (priv->window == NULL) {
1662 EmpathyChatWindow *window;
1664 window = empathy_chat_window_get_default ();
1666 window = empathy_chat_window_new ();
1669 empathy_chat_window_add_chat (window, chat);
1672 empathy_chat_window_switch_to_chat (priv->window, chat);
1673 empathy_window_present (
1674 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1677 gtk_widget_grab_focus (chat->input_text_view);
1681 empathy_chat_should_play_sound (EmpathyChat *chat)
1683 EmpathyChatWindow *window;
1684 gboolean play = TRUE;
1686 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1688 window = empathy_chat_get_window (chat);
1693 play = !empathy_chat_window_has_focus (window);
1699 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1701 EmpathyContact *contact;
1702 const gchar *msg, *to;
1703 gchar *cf_msg, *cf_to;
1707 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1709 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1713 msg = empathy_message_get_body (message);
1718 contact = empathy_message_get_receiver (message);
1719 if (!contact || !empathy_contact_is_user (contact)) {
1723 to = empathy_contact_get_name (contact);
1728 cf_msg = g_utf8_casefold (msg, -1);
1729 cf_to = g_utf8_casefold (to, -1);
1731 ch = strstr (cf_msg, cf_to);
1737 /* Not first in the message */
1738 if ((*(ch - 1) != ' ') &&
1739 (*(ch - 1) != ',') &&
1740 (*(ch - 1) != '.')) {
1745 ch = ch + strlen (cf_to);
1746 if (ch >= cf_msg + strlen (cf_msg)) {