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>
43 #include <libempathy/empathy-marshal.h>
45 #include "empathy-chat.h"
46 #include "empathy-chat-window.h"
47 #include "empathy-geometry.h"
48 #include "empathy-conf.h"
49 #include "empathy-preferences.h"
50 #include "empathy-spell.h"
51 #include "empathy-spell-dialog.h"
52 #include "empathy-ui-utils.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 /* Used to automatically shrink a window that has temporarily
84 * grown due to long input.
87 gint default_window_height;
88 gint last_input_height;
89 gboolean vscroll_visible;
100 static void empathy_chat_class_init (EmpathyChatClass *klass);
101 static void empathy_chat_init (EmpathyChat *chat);
102 static void chat_finalize (GObject *object);
103 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
105 static void chat_send (EmpathyChat *chat,
107 static void chat_input_text_view_send (EmpathyChat *chat);
108 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
109 EmpathyMessage *message,
111 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
112 EmpathyMessage *message,
113 TpChannelTextSendError error_code,
115 void chat_sent_message_add (EmpathyChat *chat,
117 const gchar * chat_sent_message_get_next (EmpathyChat *chat);
118 const gchar * chat_sent_message_get_last (EmpathyChat *chat);
119 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
122 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
124 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
127 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
129 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
130 GtkAllocation *allocation,
132 static void chat_text_view_realize_cb (GtkWidget *widget,
134 static void chat_text_populate_popup_cb (GtkTextView *view,
137 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
138 EmpathyChatSpell *chat_spell);
139 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
143 static void chat_spell_free (EmpathyChatSpell *chat_spell);
144 static void chat_composing_start (EmpathyChat *chat);
145 static void chat_composing_stop (EmpathyChat *chat);
146 static void chat_composing_remove_timeout (EmpathyChat *chat);
147 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
148 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
149 EmpathyContact *contact,
150 TpChannelChatState state,
152 static void chat_add_logs (EmpathyChat *chat);
153 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
168 static guint signals[LAST_SIGNAL] = { 0 };
170 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
173 chat_get_property (GObject *object,
178 EmpathyChatPriv *priv = GET_PRIV (object);
182 g_value_set_object (value, priv->tp_chat);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
191 chat_set_property (GObject *object,
196 EmpathyChat *chat = EMPATHY_CHAT (object);
200 empathy_chat_set_tp_chat (chat,
201 EMPATHY_TP_CHAT (g_value_get_object (value)));
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
210 chat_status_changed_cb (MissionControl *mc,
211 TpConnectionStatus status,
213 TpConnectionStatusReason reason,
214 const gchar *unique_name,
217 EmpathyChatPriv *priv = GET_PRIV (chat);
220 account = mc_account_lookup (unique_name);
222 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
223 empathy_account_equal (account, priv->account)) {
224 TpHandleType handle_type;
226 empathy_debug (DEBUG_DOMAIN,
227 "Account reconnected, request a new Text channel");
229 if (empathy_chat_is_group_chat (chat)) {
230 handle_type = TP_HANDLE_TYPE_ROOM;
232 handle_type = TP_HANDLE_TYPE_CONTACT;
235 mission_control_request_channel_with_string_handle (mc,
237 TP_IFACE_CHANNEL_TYPE_TEXT,
243 g_object_unref (account);
247 empathy_chat_class_init (EmpathyChatClass *klass)
249 GObjectClass *object_class;
251 object_class = G_OBJECT_CLASS (klass);
253 object_class->finalize = chat_finalize;
254 object_class->get_property = chat_get_property;
255 object_class->set_property = chat_set_property;
257 g_object_class_install_property (object_class,
259 g_param_spec_object ("tp-chat",
261 "The tp chat object",
262 EMPATHY_TYPE_TP_CHAT,
267 g_signal_new ("composing",
268 G_OBJECT_CLASS_TYPE (object_class),
272 g_cclosure_marshal_VOID__BOOLEAN,
276 signals[NEW_MESSAGE] =
277 g_signal_new ("new-message",
278 G_OBJECT_CLASS_TYPE (object_class),
282 empathy_marshal_VOID__OBJECT_BOOLEAN,
284 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
286 signals[NAME_CHANGED] =
287 g_signal_new ("name-changed",
288 G_OBJECT_CLASS_TYPE (object_class),
292 g_cclosure_marshal_VOID__POINTER,
296 signals[STATUS_CHANGED] =
297 g_signal_new ("status-changed",
298 G_OBJECT_CLASS_TYPE (object_class),
302 g_cclosure_marshal_VOID__VOID,
306 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
310 empathy_chat_init (EmpathyChat *chat)
312 EmpathyChatPriv *priv = GET_PRIV (chat);
313 GtkTextBuffer *buffer;
315 chat->view = empathy_chat_view_new ();
316 chat->input_text_view = gtk_text_view_new ();
318 priv->is_first_char = TRUE;
320 g_object_set (chat->input_text_view,
321 "pixels-above-lines", 2,
322 "pixels-below-lines", 2,
323 "pixels-inside-wrap", 1,
326 "wrap-mode", GTK_WRAP_WORD_CHAR,
329 priv->log_manager = empathy_log_manager_new ();
330 priv->default_window_height = -1;
331 priv->vscroll_visible = FALSE;
332 priv->sensitive = TRUE;
333 priv->sent_messages = NULL;
334 priv->sent_messages_index = -1;
335 priv->first_tp_chat = TRUE;
336 priv->mc = empathy_mission_control_new ();
338 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
339 G_CALLBACK (chat_status_changed_cb),
342 g_signal_connect (chat->input_text_view,
344 G_CALLBACK (chat_input_key_press_event_cb),
347 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
348 g_signal_connect (buffer,
350 G_CALLBACK (chat_input_text_buffer_changed_cb),
352 g_signal_connect (chat->view,
354 G_CALLBACK (chat_text_view_focus_in_event_cb),
357 g_signal_connect (chat->input_text_view,
359 G_CALLBACK (chat_text_view_size_allocate_cb),
362 g_signal_connect (chat->input_text_view,
364 G_CALLBACK (chat_text_view_realize_cb),
367 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
369 G_CALLBACK (chat_text_populate_popup_cb),
372 /* create misspelt words identification tag */
373 gtk_text_buffer_create_tag (buffer,
375 "underline", PANGO_UNDERLINE_ERROR,
380 chat_finalize (GObject *object)
383 EmpathyChatPriv *priv;
385 chat = EMPATHY_CHAT (object);
386 priv = GET_PRIV (chat);
388 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
390 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
391 g_slist_free (priv->sent_messages);
393 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
394 g_list_free (priv->compositors);
396 chat_composing_remove_timeout (chat);
397 g_object_unref (priv->log_manager);
399 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
400 G_CALLBACK (chat_status_changed_cb),
402 g_object_unref (priv->mc);
406 g_object_unref (priv->tp_chat);
410 g_object_unref (priv->account);
413 if (priv->scroll_idle_id) {
414 g_source_remove (priv->scroll_idle_id);
419 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
423 chat_destroy_cb (EmpathyTpChat *tp_chat,
426 EmpathyChatPriv *priv;
428 priv = GET_PRIV (chat);
431 g_object_unref (priv->tp_chat);
432 priv->tp_chat = NULL;
434 priv->sensitive = FALSE;
436 empathy_chat_view_append_event (chat->view, _("Disconnected"));
437 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
439 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
440 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
445 chat_send (EmpathyChat *chat,
448 EmpathyChatPriv *priv;
449 EmpathyMessage *message;
451 priv = GET_PRIV (chat);
453 if (G_STR_EMPTY (msg)) {
457 chat_sent_message_add (chat, msg);
459 if (g_str_has_prefix (msg, "/clear")) {
460 empathy_chat_view_clear (chat->view);
464 /* FIXME: add here something to let group/privrate chat handle
465 * some special messages */
467 message = empathy_message_new (msg);
469 empathy_tp_chat_send (priv->tp_chat, message);
471 g_object_unref (message);
475 chat_input_text_view_send (EmpathyChat *chat)
477 EmpathyChatPriv *priv;
478 GtkTextBuffer *buffer;
479 GtkTextIter start, end;
482 priv = GET_PRIV (chat);
484 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
486 gtk_text_buffer_get_bounds (buffer, &start, &end);
487 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
489 /* clear the input field */
490 gtk_text_buffer_set_text (buffer, "", -1);
492 chat_send (chat, msg);
496 priv->is_first_char = TRUE;
500 chat_message_received_cb (EmpathyTpChat *tp_chat,
501 EmpathyMessage *message,
504 EmpathyChatPriv *priv;
505 EmpathyContact *sender;
508 priv = GET_PRIV (chat);
510 timestamp = empathy_message_get_timestamp (message);
511 if (timestamp <= priv->last_log_timestamp) {
512 /* Do not take care of messages anterior of the last
513 * logged message. Some Jabber chatroom sends messages
514 * received before we joined the room, this avoid
515 * displaying those messages if we already logged them
516 * last time we joined that room. */
517 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
518 "anterior of last logged message.");
522 sender = empathy_message_get_sender (message);
523 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
524 empathy_contact_get_name (sender));
526 empathy_log_manager_add_message (priv->log_manager,
527 empathy_chat_get_id (chat),
528 empathy_chat_is_group_chat (chat),
531 empathy_chat_view_append_message (chat->view, message);
533 if (empathy_chat_should_play_sound (chat)) {
534 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
537 /* We received a message so the contact is no more composing */
538 chat_state_changed_cb (tp_chat, sender,
539 TP_CHANNEL_CHAT_STATE_ACTIVE,
542 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
546 chat_send_error_cb (EmpathyTpChat *tp_chat,
547 EmpathyMessage *message,
548 TpChannelTextSendError error_code,
554 switch (error_code) {
555 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
556 error = _("offline");
558 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
559 error = _("invalid contact");
561 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
562 error = _("permission denied");
564 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
565 error = _("too long message");
567 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
568 error = _("not implemented");
571 error = _("unknown");
575 str = g_strdup_printf (_("Error sending message '%s': %s"),
576 empathy_message_get_body (message),
578 empathy_chat_view_append_event (chat->view, str);
583 chat_sent_message_add (EmpathyChat *chat,
586 EmpathyChatPriv *priv;
590 priv = GET_PRIV (chat);
592 /* Save the sent message in our repeat buffer */
593 list = priv->sent_messages;
595 /* Remove any other occurances of this msg */
596 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
597 list = g_slist_remove_link (list, item);
599 g_slist_free1 (item);
602 /* Trim the list to the last 10 items */
603 while (g_slist_length (list) > 10) {
604 item = g_slist_last (list);
606 list = g_slist_remove_link (list, item);
608 g_slist_free1 (item);
612 /* Add new message */
613 list = g_slist_prepend (list, g_strdup (str));
615 /* Set list and reset the index */
616 priv->sent_messages = list;
617 priv->sent_messages_index = -1;
621 chat_sent_message_get_next (EmpathyChat *chat)
623 EmpathyChatPriv *priv;
626 priv = GET_PRIV (chat);
628 if (!priv->sent_messages) {
629 empathy_debug (DEBUG_DOMAIN,
630 "No sent messages, next message is NULL");
634 max = g_slist_length (priv->sent_messages) - 1;
636 if (priv->sent_messages_index < max) {
637 priv->sent_messages_index++;
640 empathy_debug (DEBUG_DOMAIN,
641 "Returning next message index:%d",
642 priv->sent_messages_index);
644 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
648 chat_sent_message_get_last (EmpathyChat *chat)
650 EmpathyChatPriv *priv;
652 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
654 priv = GET_PRIV (chat);
656 if (!priv->sent_messages) {
657 empathy_debug (DEBUG_DOMAIN,
658 "No sent messages, last message is NULL");
662 if (priv->sent_messages_index >= 0) {
663 priv->sent_messages_index--;
666 empathy_debug (DEBUG_DOMAIN,
667 "Returning last message index:%d",
668 priv->sent_messages_index);
670 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
674 chat_input_key_press_event_cb (GtkWidget *widget,
678 EmpathyChatPriv *priv;
681 GtkWidget *text_view_sw;
683 priv = GET_PRIV (chat);
685 /* Catch ctrl+up/down so we can traverse messages we sent */
686 if ((event->state & GDK_CONTROL_MASK) &&
687 (event->keyval == GDK_Up ||
688 event->keyval == GDK_Down)) {
689 GtkTextBuffer *buffer;
692 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
694 if (event->keyval == GDK_Up) {
695 str = chat_sent_message_get_next (chat);
697 str = chat_sent_message_get_last (chat);
700 g_signal_handlers_block_by_func (buffer,
701 chat_input_text_buffer_changed_cb,
703 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
704 g_signal_handlers_unblock_by_func (buffer,
705 chat_input_text_buffer_changed_cb,
711 /* Catch enter but not ctrl/shift-enter */
712 if (IS_ENTER (event->keyval) &&
713 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
716 /* This is to make sure that kinput2 gets the enter. And if
717 * it's handled there we shouldn't send on it. This is because
718 * kinput2 uses Enter to commit letters. See:
719 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
722 view = GTK_TEXT_VIEW (chat->input_text_view);
723 if (gtk_im_context_filter_keypress (view->im_context, event)) {
724 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
728 chat_input_text_view_send (chat);
732 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
734 if (IS_ENTER (event->keyval) &&
735 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
736 /* Newline for shift/control-enter. */
739 else if (!(event->state & GDK_CONTROL_MASK) &&
740 event->keyval == GDK_Page_Up) {
741 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
742 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
746 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
747 event->keyval == GDK_Page_Down) {
748 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
749 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
750 gtk_adjustment_set_value (adj, val);
755 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
756 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
763 chat_text_view_focus_in_event_cb (GtkWidget *widget,
767 gtk_widget_grab_focus (chat->input_text_view);
773 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
776 EmpathyChatPriv *priv;
777 GtkTextIter start, end;
779 gboolean spell_checker = FALSE;
781 priv = GET_PRIV (chat);
783 if (gtk_text_buffer_get_char_count (buffer) == 0) {
784 chat_composing_stop (chat);
786 chat_composing_start (chat);
789 empathy_conf_get_bool (empathy_conf_get (),
790 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
793 if (priv->is_first_char) {
797 GtkAllocation *allocation;
799 /* Save the window's size */
800 dialog = empathy_chat_window_get_dialog (priv->window);
801 gtk_window_get_size (GTK_WINDOW (dialog),
802 NULL, &window_height);
804 gtk_widget_size_request (chat->input_text_view, &req);
806 allocation = >K_WIDGET (chat->view)->allocation;
808 priv->default_window_height = window_height;
809 priv->last_input_height = req.height;
810 priv->padding_height = window_height - req.height - allocation->height;
812 priv->is_first_char = FALSE;
815 gtk_text_buffer_get_start_iter (buffer, &start);
817 if (!spell_checker) {
818 gtk_text_buffer_get_end_iter (buffer, &end);
819 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
823 if (!empathy_spell_supported ()) {
827 /* NOTE: this is really inefficient, we shouldn't have to
828 reiterate the whole buffer each time and check each work
831 gboolean correct = FALSE;
834 if (gtk_text_iter_is_start (&start)) {
837 if (!gtk_text_iter_forward_word_end (&end)) {
838 /* no whole word yet */
842 if (!gtk_text_iter_forward_word_end (&end)) {
843 /* must be the end of the buffer */
848 gtk_text_iter_backward_word_start (&start);
851 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
853 /* spell check string */
854 if (!empathy_chat_get_is_command (str)) {
855 correct = empathy_spell_check (str);
861 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
863 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
868 /* set start iter to the end iters position */
880 chat_change_size_in_idle_cb (ChangeSizeData *data)
882 gtk_window_resize (GTK_WINDOW (data->window),
883 data->width, data->height);
889 chat_text_view_scroll_hide_cb (GtkWidget *widget,
892 EmpathyChatPriv *priv;
895 priv = GET_PRIV (chat);
897 priv->vscroll_visible = FALSE;
898 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
900 sw = gtk_widget_get_parent (chat->input_text_view);
901 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
904 g_object_set (sw, "height-request", -1, NULL);
908 chat_text_view_size_allocate_cb (GtkWidget *widget,
909 GtkAllocation *allocation,
912 EmpathyChatPriv *priv;
915 ChangeSizeData *data;
918 GtkAllocation *view_allocation;
923 priv = GET_PRIV (chat);
925 if (priv->default_window_height <= 0) {
929 sw = gtk_widget_get_parent (widget);
930 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
933 priv->vscroll_visible = TRUE;
934 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
935 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
937 GTK_POLICY_AUTOMATIC);
938 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
939 g_signal_connect (vscroll, "hide",
940 G_CALLBACK (chat_text_view_scroll_hide_cb),
944 if (priv->last_input_height <= allocation->height) {
945 priv->last_input_height = allocation->height;
949 diff = priv->last_input_height - allocation->height;
950 priv->last_input_height = allocation->height;
952 view_allocation = >K_WIDGET (chat->view)->allocation;
954 dialog = empathy_chat_window_get_dialog (priv->window);
955 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
957 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
959 if (new_height <= priv->default_window_height) {
960 window_height = priv->default_window_height;
962 window_height = new_height;
965 if (current_height <= window_height) {
969 /* Restore the window's size */
970 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
972 data = g_new0 (ChangeSizeData, 1);
973 data->window = dialog;
975 data->height = window_height;
977 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
978 (GSourceFunc) chat_change_size_in_idle_cb,
983 chat_text_view_realize_cb (GtkWidget *widget,
986 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
987 gtk_widget_grab_focus (widget);
991 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
994 GtkTextBuffer *buffer;
998 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1000 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1002 gtk_text_buffer_get_end_iter (buffer, &iter);
1003 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1005 gtk_text_buffer_get_end_iter (buffer, &iter);
1006 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1010 chat_text_populate_popup_cb (GtkTextView *view,
1014 EmpathyChatPriv *priv;
1015 GtkTextBuffer *buffer;
1016 GtkTextTagTable *table;
1019 GtkTextIter iter, start, end;
1022 EmpathyChatSpell *chat_spell;
1023 GtkWidget *smiley_menu;
1025 priv = GET_PRIV (chat);
1027 /* Add the emoticon menu. */
1028 item = gtk_separator_menu_item_new ();
1029 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1030 gtk_widget_show (item);
1032 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1033 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1034 gtk_widget_show (item);
1036 smiley_menu = empathy_chat_view_get_smiley_menu (
1037 G_CALLBACK (chat_insert_smiley_activate_cb),
1039 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1041 /* Add the spell check menu item. */
1042 buffer = gtk_text_view_get_buffer (view);
1043 table = gtk_text_buffer_get_tag_table (buffer);
1045 tag = gtk_text_tag_table_lookup (table, "misspelled");
1047 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1049 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1050 GTK_TEXT_WINDOW_WIDGET,
1054 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1058 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1059 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1061 str = gtk_text_buffer_get_text (buffer,
1062 &start, &end, FALSE);
1065 if (G_STR_EMPTY (str)) {
1069 chat_spell = chat_spell_new (chat, str, start, end);
1071 g_object_set_data_full (G_OBJECT (menu),
1072 "chat_spell", chat_spell,
1073 (GDestroyNotify) chat_spell_free);
1075 item = gtk_separator_menu_item_new ();
1076 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1077 gtk_widget_show (item);
1079 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1080 g_signal_connect (item,
1082 G_CALLBACK (chat_text_check_word_spelling_cb),
1084 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1085 gtk_widget_show (item);
1089 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1090 EmpathyChatSpell *chat_spell)
1092 empathy_spell_dialog_show (chat_spell->chat,
1098 static EmpathyChatSpell *
1099 chat_spell_new (EmpathyChat *chat,
1104 EmpathyChatSpell *chat_spell;
1106 chat_spell = g_new0 (EmpathyChatSpell, 1);
1108 chat_spell->chat = g_object_ref (chat);
1109 chat_spell->word = g_strdup (word);
1110 chat_spell->start = start;
1111 chat_spell->end = end;
1117 chat_spell_free (EmpathyChatSpell *chat_spell)
1119 g_object_unref (chat_spell->chat);
1120 g_free (chat_spell->word);
1121 g_free (chat_spell);
1125 chat_composing_start (EmpathyChat *chat)
1127 EmpathyChatPriv *priv;
1129 priv = GET_PRIV (chat);
1131 if (priv->composing_stop_timeout_id) {
1132 /* Just restart the timeout */
1133 chat_composing_remove_timeout (chat);
1135 empathy_tp_chat_set_state (priv->tp_chat,
1136 TP_CHANNEL_CHAT_STATE_COMPOSING);
1139 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1140 COMPOSING_STOP_TIMEOUT,
1141 (GSourceFunc) chat_composing_stop_timeout_cb,
1146 chat_composing_stop (EmpathyChat *chat)
1148 EmpathyChatPriv *priv;
1150 priv = GET_PRIV (chat);
1152 chat_composing_remove_timeout (chat);
1153 empathy_tp_chat_set_state (priv->tp_chat,
1154 TP_CHANNEL_CHAT_STATE_ACTIVE);
1158 chat_composing_remove_timeout (EmpathyChat *chat)
1160 EmpathyChatPriv *priv;
1162 priv = GET_PRIV (chat);
1164 if (priv->composing_stop_timeout_id) {
1165 g_source_remove (priv->composing_stop_timeout_id);
1166 priv->composing_stop_timeout_id = 0;
1171 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1173 EmpathyChatPriv *priv;
1175 priv = GET_PRIV (chat);
1177 priv->composing_stop_timeout_id = 0;
1178 empathy_tp_chat_set_state (priv->tp_chat,
1179 TP_CHANNEL_CHAT_STATE_PAUSED);
1185 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1186 EmpathyContact *contact,
1187 TpChannelChatState state,
1190 EmpathyChatPriv *priv;
1192 gboolean was_composing;
1194 priv = GET_PRIV (chat);
1196 if (empathy_contact_is_user (contact)) {
1197 /* We don't care about our own chat state */
1201 was_composing = (priv->compositors != NULL);
1203 /* Find the contact in the list. After that l is the list elem or NULL */
1204 for (l = priv->compositors; l; l = l->next) {
1205 if (empathy_contact_equal (contact, l->data)) {
1211 case TP_CHANNEL_CHAT_STATE_GONE:
1212 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1213 case TP_CHANNEL_CHAT_STATE_PAUSED:
1214 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1215 /* Contact is not composing */
1217 priv->compositors = g_list_remove_link (priv->compositors, l);
1218 g_object_unref (l->data);
1222 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1223 /* Contact is composing */
1225 priv->compositors = g_list_prepend (priv->compositors,
1226 g_object_ref (contact));
1230 g_assert_not_reached ();
1233 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1234 was_composing ? "yes" : "no",
1235 priv->compositors ? "yes" : "no");
1237 if ((was_composing && !priv->compositors) ||
1238 (!was_composing && priv->compositors)) {
1239 /* Composing state changed */
1240 g_signal_emit (chat, signals[COMPOSING], 0,
1241 priv->compositors != NULL);
1246 chat_add_logs (EmpathyChat *chat)
1248 EmpathyChatPriv *priv;
1249 GList *messages, *l;
1253 priv = GET_PRIV (chat);
1255 /* Turn off scrolling temporarily */
1256 empathy_chat_view_scroll (chat->view, FALSE);
1258 /* Add messages from last conversation */
1259 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1261 empathy_chat_get_id (chat),
1262 empathy_chat_is_group_chat (chat));
1263 num_messages = g_list_length (messages);
1265 for (l = messages, i = 0; l; l = l->next, i++) {
1266 EmpathyMessage *message;
1270 /* Only add 10 last messages */
1271 if (num_messages - i > 10) {
1272 g_object_unref (message);
1276 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1277 empathy_chat_view_append_message (chat->view, message);
1279 g_object_unref (message);
1281 g_list_free (messages);
1283 /* Turn back on scrolling */
1284 empathy_chat_view_scroll (chat->view, TRUE);
1286 /* Scroll to the most recent messages, we reference the chat
1287 * for the duration of the scroll func.
1289 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1290 g_object_ref (chat));
1293 /* Scroll down after the back-log has been received. */
1295 chat_scroll_down_idle_func (EmpathyChat *chat)
1297 EmpathyChatPriv *priv;
1299 priv = GET_PRIV (chat);
1301 empathy_chat_scroll_down (chat);
1302 g_object_unref (chat);
1304 priv->scroll_idle_id = 0;
1310 empathy_chat_get_is_command (const gchar *str)
1312 g_return_val_if_fail (str != NULL, FALSE);
1314 if (str[0] != '/') {
1318 if (g_str_has_prefix (str, "/me")) {
1321 else if (g_str_has_prefix (str, "/nick")) {
1324 else if (g_str_has_prefix (str, "/topic")) {
1332 empathy_chat_correct_word (EmpathyChat *chat,
1335 const gchar *new_word)
1337 GtkTextBuffer *buffer;
1339 g_return_if_fail (chat != NULL);
1340 g_return_if_fail (new_word != NULL);
1342 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1344 gtk_text_buffer_delete (buffer, &start, &end);
1345 gtk_text_buffer_insert (buffer, &start,
1351 empathy_chat_get_name (EmpathyChat *chat)
1353 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1355 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1356 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1363 empathy_chat_get_tooltip (EmpathyChat *chat)
1365 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1367 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1368 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1375 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1377 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1379 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1380 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1387 empathy_chat_get_widget (EmpathyChat *chat)
1389 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1391 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1392 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1399 empathy_chat_is_group_chat (EmpathyChat *chat)
1401 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1403 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1404 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1411 empathy_chat_is_connected (EmpathyChat *chat)
1413 EmpathyChatPriv *priv;
1415 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1417 priv = GET_PRIV (chat);
1419 return (priv->tp_chat != NULL);
1423 empathy_chat_save_geometry (EmpathyChat *chat,
1429 empathy_geometry_save (empathy_chat_get_id (chat), x, y, w, h);
1433 empathy_chat_load_geometry (EmpathyChat *chat,
1439 empathy_geometry_load (empathy_chat_get_id (chat), x, y, w, h);
1443 empathy_chat_set_tp_chat (EmpathyChat *chat,
1444 EmpathyTpChat *tp_chat)
1446 EmpathyChatPriv *priv;
1447 GList *messages, *l;
1449 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1450 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1452 priv = GET_PRIV (chat);
1454 if (tp_chat == priv->tp_chat) {
1458 if (priv->tp_chat) {
1459 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1460 chat_message_received_cb,
1462 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1465 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1468 g_object_unref (priv->tp_chat);
1470 if (priv->account) {
1471 g_object_unref (priv->account);
1475 priv->tp_chat = g_object_ref (tp_chat);
1476 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1477 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1479 if (priv->first_tp_chat) {
1480 chat_add_logs (chat);
1481 priv->first_tp_chat = FALSE;
1484 g_signal_connect (tp_chat, "message-received",
1485 G_CALLBACK (chat_message_received_cb),
1487 g_signal_connect (tp_chat, "send-error",
1488 G_CALLBACK (chat_send_error_cb),
1490 g_signal_connect (tp_chat, "chat-state-changed",
1491 G_CALLBACK (chat_state_changed_cb),
1493 g_signal_connect (tp_chat, "destroy",
1494 G_CALLBACK (chat_destroy_cb),
1497 /* Get pending messages */
1498 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1499 messages = empathy_tp_chat_get_pendings (tp_chat);
1500 for (l = messages; l; l = l->next) {
1501 chat_message_received_cb (tp_chat, l->data, chat);
1502 g_object_unref (l->data);
1504 g_list_free (messages);
1506 if (!priv->sensitive) {
1507 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1508 empathy_chat_view_append_event (chat->view, _("Connected"));
1509 priv->sensitive = TRUE;
1512 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1513 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1516 g_object_notify (G_OBJECT (chat), "tp-chat");
1520 empathy_chat_get_id (EmpathyChat *chat)
1522 EmpathyChatPriv *priv;
1524 priv = GET_PRIV (chat);
1530 empathy_chat_get_account (EmpathyChat *chat)
1532 EmpathyChatPriv *priv = GET_PRIV (chat);
1534 return priv->account;
1538 empathy_chat_clear (EmpathyChat *chat)
1540 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1542 empathy_chat_view_clear (chat->view);
1546 empathy_chat_set_window (EmpathyChat *chat,
1547 EmpathyChatWindow *window)
1549 EmpathyChatPriv *priv;
1551 priv = GET_PRIV (chat);
1552 priv->window = window;
1556 empathy_chat_get_window (EmpathyChat *chat)
1558 EmpathyChatPriv *priv;
1560 priv = GET_PRIV (chat);
1562 return priv->window;
1566 empathy_chat_scroll_down (EmpathyChat *chat)
1568 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1570 empathy_chat_view_scroll_down (chat->view);
1574 empathy_chat_cut (EmpathyChat *chat)
1576 GtkTextBuffer *buffer;
1578 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1580 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1581 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1582 GtkClipboard *clipboard;
1584 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1586 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1591 empathy_chat_copy (EmpathyChat *chat)
1593 GtkTextBuffer *buffer;
1595 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1597 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1598 empathy_chat_view_copy_clipboard (chat->view);
1602 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1603 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1604 GtkClipboard *clipboard;
1606 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1608 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1613 empathy_chat_paste (EmpathyChat *chat)
1615 GtkTextBuffer *buffer;
1616 GtkClipboard *clipboard;
1618 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1620 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1621 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1623 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1627 empathy_chat_present (EmpathyChat *chat)
1629 EmpathyChatPriv *priv;
1631 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1633 priv = GET_PRIV (chat);
1635 if (priv->window == NULL) {
1636 EmpathyChatWindow *window;
1638 window = empathy_chat_window_get_default ();
1640 window = empathy_chat_window_new ();
1643 empathy_chat_window_add_chat (window, chat);
1646 empathy_chat_window_switch_to_chat (priv->window, chat);
1647 empathy_window_present (
1648 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1651 gtk_widget_grab_focus (chat->input_text_view);
1655 empathy_chat_should_play_sound (EmpathyChat *chat)
1657 EmpathyChatWindow *window;
1658 gboolean play = TRUE;
1660 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1662 window = empathy_chat_get_window (chat);
1667 play = !empathy_chat_window_has_focus (window);
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)) {