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 <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 (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 timestamp = empathy_message_get_timestamp (message);
520 if (timestamp <= priv->last_log_timestamp) {
521 /* Do not take care of messages anterior of the last
522 * logged message. Some Jabber chatroom sends messages
523 * received before we joined the room, this avoid
524 * displaying those messages if we already logged them
525 * last time we joined that room. */
526 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
527 "anterior of last logged message.");
531 sender = empathy_message_get_sender (message);
532 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
533 empathy_contact_get_name (sender));
535 empathy_log_manager_add_message (priv->log_manager,
536 empathy_chat_get_id (chat),
537 empathy_chat_is_group_chat (chat),
540 empathy_chat_view_append_message (chat->view, message);
542 if (empathy_chat_should_play_sound (chat)) {
543 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
546 /* We received a message so the contact is no more composing */
547 chat_state_changed_cb (tp_chat, sender,
548 TP_CHANNEL_CHAT_STATE_ACTIVE,
551 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
555 chat_send_error_cb (EmpathyTpChat *tp_chat,
556 EmpathyMessage *message,
557 TpChannelTextSendError error_code,
563 switch (error_code) {
564 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
565 error = _("offline");
567 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
568 error = _("invalid contact");
570 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
571 error = _("permission denied");
573 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
574 error = _("too long message");
576 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
577 error = _("not implemented");
580 error = _("unknown");
584 str = g_strdup_printf (_("Error sending message '%s': %s"),
585 empathy_message_get_body (message),
587 empathy_chat_view_append_event (chat->view, str);
592 chat_sent_message_add (EmpathyChat *chat,
595 EmpathyChatPriv *priv;
599 priv = GET_PRIV (chat);
601 /* Save the sent message in our repeat buffer */
602 list = priv->sent_messages;
604 /* Remove any other occurances of this msg */
605 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
606 list = g_slist_remove_link (list, item);
608 g_slist_free1 (item);
611 /* Trim the list to the last 10 items */
612 while (g_slist_length (list) > 10) {
613 item = g_slist_last (list);
615 list = g_slist_remove_link (list, item);
617 g_slist_free1 (item);
621 /* Add new message */
622 list = g_slist_prepend (list, g_strdup (str));
624 /* Set list and reset the index */
625 priv->sent_messages = list;
626 priv->sent_messages_index = -1;
630 chat_sent_message_get_next (EmpathyChat *chat)
632 EmpathyChatPriv *priv;
635 priv = GET_PRIV (chat);
637 if (!priv->sent_messages) {
638 empathy_debug (DEBUG_DOMAIN,
639 "No sent messages, next message is NULL");
643 max = g_slist_length (priv->sent_messages) - 1;
645 if (priv->sent_messages_index < max) {
646 priv->sent_messages_index++;
649 empathy_debug (DEBUG_DOMAIN,
650 "Returning next message index:%d",
651 priv->sent_messages_index);
653 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
657 chat_sent_message_get_last (EmpathyChat *chat)
659 EmpathyChatPriv *priv;
661 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
663 priv = GET_PRIV (chat);
665 if (!priv->sent_messages) {
666 empathy_debug (DEBUG_DOMAIN,
667 "No sent messages, last message is NULL");
671 if (priv->sent_messages_index >= 0) {
672 priv->sent_messages_index--;
675 empathy_debug (DEBUG_DOMAIN,
676 "Returning last message index:%d",
677 priv->sent_messages_index);
679 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
683 chat_input_key_press_event_cb (GtkWidget *widget,
687 EmpathyChatPriv *priv;
690 GtkWidget *text_view_sw;
692 priv = GET_PRIV (chat);
694 /* Catch ctrl+up/down so we can traverse messages we sent */
695 if ((event->state & GDK_CONTROL_MASK) &&
696 (event->keyval == GDK_Up ||
697 event->keyval == GDK_Down)) {
698 GtkTextBuffer *buffer;
701 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
703 if (event->keyval == GDK_Up) {
704 str = chat_sent_message_get_next (chat);
706 str = chat_sent_message_get_last (chat);
709 g_signal_handlers_block_by_func (buffer,
710 chat_input_text_buffer_changed_cb,
712 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
713 g_signal_handlers_unblock_by_func (buffer,
714 chat_input_text_buffer_changed_cb,
720 /* Catch enter but not ctrl/shift-enter */
721 if (IS_ENTER (event->keyval) &&
722 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
725 /* This is to make sure that kinput2 gets the enter. And if
726 * it's handled there we shouldn't send on it. This is because
727 * kinput2 uses Enter to commit letters. See:
728 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
731 view = GTK_TEXT_VIEW (chat->input_text_view);
732 if (gtk_im_context_filter_keypress (view->im_context, event)) {
733 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
737 chat_input_text_view_send (chat);
741 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
743 if (IS_ENTER (event->keyval) &&
744 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
745 /* Newline for shift/control-enter. */
748 else if (!(event->state & GDK_CONTROL_MASK) &&
749 event->keyval == GDK_Page_Up) {
750 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
751 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
755 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
756 event->keyval == GDK_Page_Down) {
757 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
758 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
759 gtk_adjustment_set_value (adj, val);
764 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
765 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
772 chat_text_view_focus_in_event_cb (GtkWidget *widget,
776 gtk_widget_grab_focus (chat->input_text_view);
782 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
785 EmpathyChatPriv *priv;
786 GtkTextIter start, end;
788 gboolean spell_checker = FALSE;
790 priv = GET_PRIV (chat);
792 if (gtk_text_buffer_get_char_count (buffer) == 0) {
793 chat_composing_stop (chat);
795 chat_composing_start (chat);
798 empathy_conf_get_bool (empathy_conf_get (),
799 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
802 if (priv->is_first_char) {
806 GtkAllocation *allocation;
808 /* Save the window's size */
809 dialog = empathy_chat_window_get_dialog (priv->window);
810 gtk_window_get_size (GTK_WINDOW (dialog),
811 NULL, &window_height);
813 gtk_widget_size_request (chat->input_text_view, &req);
815 allocation = >K_WIDGET (chat->view)->allocation;
817 priv->default_window_height = window_height;
818 priv->last_input_height = req.height;
819 priv->padding_height = window_height - req.height - allocation->height;
821 priv->is_first_char = FALSE;
824 gtk_text_buffer_get_start_iter (buffer, &start);
826 if (!spell_checker) {
827 gtk_text_buffer_get_end_iter (buffer, &end);
828 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
832 if (!empathy_spell_supported ()) {
836 /* NOTE: this is really inefficient, we shouldn't have to
837 reiterate the whole buffer each time and check each work
840 gboolean correct = FALSE;
843 if (gtk_text_iter_is_start (&start)) {
846 if (!gtk_text_iter_forward_word_end (&end)) {
847 /* no whole word yet */
851 if (!gtk_text_iter_forward_word_end (&end)) {
852 /* must be the end of the buffer */
857 gtk_text_iter_backward_word_start (&start);
860 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
862 /* spell check string */
863 if (!empathy_chat_get_is_command (str)) {
864 correct = empathy_spell_check (str);
870 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
872 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
877 /* set start iter to the end iters position */
889 chat_change_size_in_idle_cb (ChangeSizeData *data)
891 gtk_window_resize (GTK_WINDOW (data->window),
892 data->width, data->height);
898 chat_text_view_scroll_hide_cb (GtkWidget *widget,
901 EmpathyChatPriv *priv;
904 priv = GET_PRIV (chat);
906 priv->vscroll_visible = FALSE;
907 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
909 sw = gtk_widget_get_parent (chat->input_text_view);
910 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
913 g_object_set (sw, "height-request", -1, NULL);
917 chat_text_view_size_allocate_cb (GtkWidget *widget,
918 GtkAllocation *allocation,
921 EmpathyChatPriv *priv;
924 ChangeSizeData *data;
927 GtkAllocation *view_allocation;
932 priv = GET_PRIV (chat);
934 if (priv->default_window_height <= 0) {
938 sw = gtk_widget_get_parent (widget);
939 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
942 priv->vscroll_visible = TRUE;
943 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
944 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
946 GTK_POLICY_AUTOMATIC);
947 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
948 g_signal_connect (vscroll, "hide",
949 G_CALLBACK (chat_text_view_scroll_hide_cb),
953 if (priv->last_input_height <= allocation->height) {
954 priv->last_input_height = allocation->height;
958 diff = priv->last_input_height - allocation->height;
959 priv->last_input_height = allocation->height;
961 view_allocation = >K_WIDGET (chat->view)->allocation;
963 dialog = empathy_chat_window_get_dialog (priv->window);
964 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
966 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
968 if (new_height <= priv->default_window_height) {
969 window_height = priv->default_window_height;
971 window_height = new_height;
974 if (current_height <= window_height) {
978 /* Restore the window's size */
979 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
981 data = g_new0 (ChangeSizeData, 1);
982 data->window = dialog;
984 data->height = window_height;
986 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
987 (GSourceFunc) chat_change_size_in_idle_cb,
992 chat_text_view_realize_cb (GtkWidget *widget,
995 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
996 gtk_widget_grab_focus (widget);
1000 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1003 GtkTextBuffer *buffer;
1005 const gchar *smiley;
1007 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1009 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1011 gtk_text_buffer_get_end_iter (buffer, &iter);
1012 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1014 gtk_text_buffer_get_end_iter (buffer, &iter);
1015 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1019 chat_text_populate_popup_cb (GtkTextView *view,
1023 EmpathyChatPriv *priv;
1024 GtkTextBuffer *buffer;
1025 GtkTextTagTable *table;
1028 GtkTextIter iter, start, end;
1031 EmpathyChatSpell *chat_spell;
1032 GtkWidget *smiley_menu;
1034 priv = GET_PRIV (chat);
1036 /* Add the emoticon menu. */
1037 item = gtk_separator_menu_item_new ();
1038 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1039 gtk_widget_show (item);
1041 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1042 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1043 gtk_widget_show (item);
1045 smiley_menu = empathy_chat_view_get_smiley_menu (
1046 G_CALLBACK (chat_insert_smiley_activate_cb),
1048 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1050 /* Add the spell check menu item. */
1051 buffer = gtk_text_view_get_buffer (view);
1052 table = gtk_text_buffer_get_tag_table (buffer);
1054 tag = gtk_text_tag_table_lookup (table, "misspelled");
1056 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1058 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1059 GTK_TEXT_WINDOW_WIDGET,
1063 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1067 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1068 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1070 str = gtk_text_buffer_get_text (buffer,
1071 &start, &end, FALSE);
1074 if (G_STR_EMPTY (str)) {
1078 chat_spell = chat_spell_new (chat, str, start, end);
1080 g_object_set_data_full (G_OBJECT (menu),
1081 "chat_spell", chat_spell,
1082 (GDestroyNotify) chat_spell_free);
1084 item = gtk_separator_menu_item_new ();
1085 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1086 gtk_widget_show (item);
1088 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1089 g_signal_connect (item,
1091 G_CALLBACK (chat_text_check_word_spelling_cb),
1093 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1094 gtk_widget_show (item);
1098 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1099 EmpathyChatSpell *chat_spell)
1101 empathy_spell_dialog_show (chat_spell->chat,
1107 static EmpathyChatSpell *
1108 chat_spell_new (EmpathyChat *chat,
1113 EmpathyChatSpell *chat_spell;
1115 chat_spell = g_new0 (EmpathyChatSpell, 1);
1117 chat_spell->chat = g_object_ref (chat);
1118 chat_spell->word = g_strdup (word);
1119 chat_spell->start = start;
1120 chat_spell->end = end;
1126 chat_spell_free (EmpathyChatSpell *chat_spell)
1128 g_object_unref (chat_spell->chat);
1129 g_free (chat_spell->word);
1130 g_free (chat_spell);
1134 chat_composing_start (EmpathyChat *chat)
1136 EmpathyChatPriv *priv;
1138 priv = GET_PRIV (chat);
1140 if (priv->composing_stop_timeout_id) {
1141 /* Just restart the timeout */
1142 chat_composing_remove_timeout (chat);
1144 empathy_tp_chat_set_state (priv->tp_chat,
1145 TP_CHANNEL_CHAT_STATE_COMPOSING);
1148 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1149 COMPOSING_STOP_TIMEOUT,
1150 (GSourceFunc) chat_composing_stop_timeout_cb,
1155 chat_composing_stop (EmpathyChat *chat)
1157 EmpathyChatPriv *priv;
1159 priv = GET_PRIV (chat);
1161 chat_composing_remove_timeout (chat);
1162 empathy_tp_chat_set_state (priv->tp_chat,
1163 TP_CHANNEL_CHAT_STATE_ACTIVE);
1167 chat_composing_remove_timeout (EmpathyChat *chat)
1169 EmpathyChatPriv *priv;
1171 priv = GET_PRIV (chat);
1173 if (priv->composing_stop_timeout_id) {
1174 g_source_remove (priv->composing_stop_timeout_id);
1175 priv->composing_stop_timeout_id = 0;
1180 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1182 EmpathyChatPriv *priv;
1184 priv = GET_PRIV (chat);
1186 priv->composing_stop_timeout_id = 0;
1187 empathy_tp_chat_set_state (priv->tp_chat,
1188 TP_CHANNEL_CHAT_STATE_PAUSED);
1194 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1195 EmpathyContact *contact,
1196 TpChannelChatState state,
1199 EmpathyChatPriv *priv;
1201 gboolean was_composing;
1203 priv = GET_PRIV (chat);
1205 if (empathy_contact_is_user (contact)) {
1206 /* We don't care about our own chat state */
1210 was_composing = (priv->compositors != NULL);
1212 /* Find the contact in the list. After that l is the list elem or NULL */
1213 for (l = priv->compositors; l; l = l->next) {
1214 if (empathy_contact_equal (contact, l->data)) {
1220 case TP_CHANNEL_CHAT_STATE_GONE:
1221 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1222 case TP_CHANNEL_CHAT_STATE_PAUSED:
1223 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1224 /* Contact is not composing */
1226 priv->compositors = g_list_remove_link (priv->compositors, l);
1227 g_object_unref (l->data);
1231 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1232 /* Contact is composing */
1234 priv->compositors = g_list_prepend (priv->compositors,
1235 g_object_ref (contact));
1239 g_assert_not_reached ();
1242 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1243 was_composing ? "yes" : "no",
1244 priv->compositors ? "yes" : "no");
1246 if ((was_composing && !priv->compositors) ||
1247 (!was_composing && priv->compositors)) {
1248 /* Composing state changed */
1249 g_signal_emit (chat, signals[COMPOSING], 0,
1250 priv->compositors != NULL);
1255 chat_add_logs (EmpathyChat *chat)
1257 EmpathyChatPriv *priv;
1258 GList *messages, *l;
1262 priv = GET_PRIV (chat);
1264 /* Turn off scrolling temporarily */
1265 empathy_chat_view_scroll (chat->view, FALSE);
1267 /* Add messages from last conversation */
1268 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1270 empathy_chat_get_id (chat),
1271 empathy_chat_is_group_chat (chat));
1272 num_messages = g_list_length (messages);
1274 for (l = messages, i = 0; l; l = l->next, i++) {
1275 EmpathyMessage *message;
1279 /* Only add 10 last messages */
1280 if (num_messages - i > 10) {
1281 g_object_unref (message);
1285 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1286 empathy_chat_view_append_message (chat->view, message);
1288 g_object_unref (message);
1290 g_list_free (messages);
1292 /* Turn back on scrolling */
1293 empathy_chat_view_scroll (chat->view, TRUE);
1295 /* Scroll to the most recent messages, we reference the chat
1296 * for the duration of the scroll func.
1298 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1299 g_object_ref (chat));
1302 /* Scroll down after the back-log has been received. */
1304 chat_scroll_down_idle_func (EmpathyChat *chat)
1306 EmpathyChatPriv *priv;
1308 priv = GET_PRIV (chat);
1310 empathy_chat_scroll_down (chat);
1311 g_object_unref (chat);
1313 priv->scroll_idle_id = 0;
1319 empathy_chat_get_is_command (const gchar *str)
1321 g_return_val_if_fail (str != NULL, FALSE);
1323 if (str[0] != '/') {
1327 if (g_str_has_prefix (str, "/me")) {
1330 else if (g_str_has_prefix (str, "/nick")) {
1333 else if (g_str_has_prefix (str, "/topic")) {
1341 empathy_chat_correct_word (EmpathyChat *chat,
1344 const gchar *new_word)
1346 GtkTextBuffer *buffer;
1348 g_return_if_fail (chat != NULL);
1349 g_return_if_fail (new_word != NULL);
1351 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1353 gtk_text_buffer_delete (buffer, &start, &end);
1354 gtk_text_buffer_insert (buffer, &start,
1360 empathy_chat_get_name (EmpathyChat *chat)
1362 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1364 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1365 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1372 empathy_chat_get_tooltip (EmpathyChat *chat)
1374 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1376 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1377 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1384 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1386 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1388 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1389 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1396 empathy_chat_get_widget (EmpathyChat *chat)
1398 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1400 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1401 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1408 empathy_chat_is_group_chat (EmpathyChat *chat)
1410 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1412 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1413 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1420 empathy_chat_is_connected (EmpathyChat *chat)
1422 EmpathyChatPriv *priv;
1424 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1426 priv = GET_PRIV (chat);
1428 return (priv->tp_chat != NULL);
1431 static const gchar *
1432 chat_get_window_id_for_geometry (EmpathyChat *chat)
1434 gboolean separate_windows;
1436 empathy_conf_get_bool (empathy_conf_get (),
1437 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1440 if (separate_windows) {
1441 return empathy_chat_get_id (chat);
1443 return "chat-window";
1448 empathy_chat_save_geometry (EmpathyChat *chat,
1454 empathy_geometry_save (chat_get_window_id_for_geometry (chat), x, y, w, h);
1458 empathy_chat_load_geometry (EmpathyChat *chat,
1464 empathy_geometry_load (chat_get_window_id_for_geometry (chat), x, y, w, h);
1468 chat_block_events_timeout_cb (gpointer data)
1470 EmpathyChat *chat = EMPATHY_CHAT (data);
1471 EmpathyChatPriv *priv = GET_PRIV (chat);
1473 chat->block_events = FALSE;
1474 priv->block_events_timeout_id = 0;
1480 empathy_chat_set_tp_chat (EmpathyChat *chat,
1481 EmpathyTpChat *tp_chat)
1483 EmpathyChatPriv *priv;
1485 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1486 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1488 priv = GET_PRIV (chat);
1490 if (tp_chat == priv->tp_chat) {
1494 /* Block events for some time to avoid having "has come online" or
1495 * "joined" messages. */
1496 chat->block_events = TRUE;
1497 if (priv->block_events_timeout_id != 0) {
1498 g_source_remove (priv->block_events_timeout_id);
1500 priv->block_events_timeout_id =
1501 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1503 if (priv->tp_chat) {
1504 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1505 chat_message_received_cb,
1507 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1510 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1513 g_object_unref (priv->tp_chat);
1515 if (priv->account) {
1516 g_object_unref (priv->account);
1520 priv->tp_chat = g_object_ref (tp_chat);
1521 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1522 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1523 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1525 if (priv->first_tp_chat) {
1526 chat_add_logs (chat);
1527 priv->first_tp_chat = FALSE;
1530 g_signal_connect (tp_chat, "message-received",
1531 G_CALLBACK (chat_message_received_cb),
1533 g_signal_connect (tp_chat, "send-error",
1534 G_CALLBACK (chat_send_error_cb),
1536 g_signal_connect (tp_chat, "chat-state-changed",
1537 G_CALLBACK (chat_state_changed_cb),
1539 g_signal_connect (tp_chat, "destroy",
1540 G_CALLBACK (chat_destroy_cb),
1543 if (!priv->sensitive) {
1544 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1545 empathy_chat_view_append_event (chat->view, _("Connected"));
1546 priv->sensitive = TRUE;
1549 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1550 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1553 g_object_notify (G_OBJECT (chat), "tp-chat");
1557 empathy_chat_get_id (EmpathyChat *chat)
1559 EmpathyChatPriv *priv;
1561 priv = GET_PRIV (chat);
1567 empathy_chat_get_account (EmpathyChat *chat)
1569 EmpathyChatPriv *priv = GET_PRIV (chat);
1571 return priv->account;
1575 empathy_chat_clear (EmpathyChat *chat)
1577 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1579 empathy_chat_view_clear (chat->view);
1583 empathy_chat_set_window (EmpathyChat *chat,
1584 EmpathyChatWindow *window)
1586 EmpathyChatPriv *priv;
1588 priv = GET_PRIV (chat);
1589 priv->window = window;
1593 empathy_chat_get_window (EmpathyChat *chat)
1595 EmpathyChatPriv *priv;
1597 priv = GET_PRIV (chat);
1599 return priv->window;
1603 empathy_chat_scroll_down (EmpathyChat *chat)
1605 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1607 empathy_chat_view_scroll_down (chat->view);
1611 empathy_chat_cut (EmpathyChat *chat)
1613 GtkTextBuffer *buffer;
1615 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1617 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1618 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1619 GtkClipboard *clipboard;
1621 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1623 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1628 empathy_chat_copy (EmpathyChat *chat)
1630 GtkTextBuffer *buffer;
1632 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1634 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1635 empathy_chat_view_copy_clipboard (chat->view);
1639 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1640 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1641 GtkClipboard *clipboard;
1643 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1645 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1650 empathy_chat_paste (EmpathyChat *chat)
1652 GtkTextBuffer *buffer;
1653 GtkClipboard *clipboard;
1655 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1657 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1658 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1660 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1664 empathy_chat_present (EmpathyChat *chat)
1666 EmpathyChatPriv *priv;
1668 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1670 priv = GET_PRIV (chat);
1672 if (priv->window == NULL) {
1673 EmpathyChatWindow *window;
1675 window = empathy_chat_window_get_default ();
1677 window = empathy_chat_window_new ();
1680 empathy_chat_window_add_chat (window, chat);
1683 empathy_chat_window_switch_to_chat (priv->window, chat);
1684 empathy_window_present (
1685 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1688 gtk_widget_grab_focus (chat->input_text_view);
1692 empathy_chat_should_play_sound (EmpathyChat *chat)
1694 EmpathyChatWindow *window;
1695 gboolean play = TRUE;
1697 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1699 window = empathy_chat_get_window (chat);
1704 play = !empathy_chat_window_has_focus (window);
1710 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1712 EmpathyContact *contact;
1713 const gchar *msg, *to;
1714 gchar *cf_msg, *cf_to;
1718 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1720 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1724 msg = empathy_message_get_body (message);
1729 contact = empathy_message_get_receiver (message);
1730 if (!contact || !empathy_contact_is_user (contact)) {
1734 to = empathy_contact_get_name (contact);
1739 cf_msg = g_utf8_casefold (msg, -1);
1740 cf_to = g_utf8_casefold (to, -1);
1742 ch = strstr (cf_msg, cf_to);
1748 /* Not first in the message */
1749 if ((*(ch - 1) != ' ') &&
1750 (*(ch - 1) != ',') &&
1751 (*(ch - 1) != '.')) {
1756 ch = ch + strlen (cf_to);
1757 if (ch >= cf_msg + strlen (cf_msg)) {