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-geometry.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-spell-dialog.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-gtk-marshal.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
54 #define DEBUG_DOMAIN "Chat"
56 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
59 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
63 #define COMPOSING_STOP_TIMEOUT 5
65 struct _EmpathyChatPriv {
66 EmpathyLogManager *log_manager;
67 EmpathyTpChat *tp_chat;
71 guint composing_stop_timeout_id;
74 GSList *sent_messages;
75 gint sent_messages_index;
78 gboolean first_tp_chat;
79 time_t last_log_timestamp;
80 gboolean is_first_char;
81 guint block_events_timeout_id;
82 /* Used to automatically shrink a window that has temporarily
83 * grown due to long input.
86 gint default_window_height;
87 gint last_input_height;
88 gboolean vscroll_visible;
99 static void empathy_chat_class_init (EmpathyChatClass *klass);
100 static void empathy_chat_init (EmpathyChat *chat);
101 static void chat_finalize (GObject *object);
102 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
104 static void chat_send (EmpathyChat *chat,
106 static void chat_input_text_view_send (EmpathyChat *chat);
107 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
108 EmpathyMessage *message,
110 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
111 EmpathyMessage *message,
112 TpChannelTextSendError error_code,
114 static void chat_sent_message_add (EmpathyChat *chat,
116 static const gchar * chat_sent_message_get_next (EmpathyChat *chat);
117 static const gchar * chat_sent_message_get_last (EmpathyChat *chat);
118 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
121 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
123 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
126 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
128 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
129 GtkAllocation *allocation,
131 static void chat_text_view_realize_cb (GtkWidget *widget,
133 static void chat_text_populate_popup_cb (GtkTextView *view,
136 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
137 EmpathyChatSpell *chat_spell);
138 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
142 static void chat_spell_free (EmpathyChatSpell *chat_spell);
143 static void chat_composing_start (EmpathyChat *chat);
144 static void chat_composing_stop (EmpathyChat *chat);
145 static void chat_composing_remove_timeout (EmpathyChat *chat);
146 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
147 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
148 EmpathyContact *contact,
149 TpChannelChatState state,
151 static void chat_add_logs (EmpathyChat *chat);
152 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
167 static guint signals[LAST_SIGNAL] = { 0 };
169 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
172 chat_get_property (GObject *object,
177 EmpathyChatPriv *priv = GET_PRIV (object);
181 g_value_set_object (value, priv->tp_chat);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
190 chat_set_property (GObject *object,
195 EmpathyChat *chat = EMPATHY_CHAT (object);
199 empathy_chat_set_tp_chat (chat,
200 EMPATHY_TP_CHAT (g_value_get_object (value)));
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
209 chat_status_changed_cb (MissionControl *mc,
210 TpConnectionStatus status,
212 TpConnectionStatusReason reason,
213 const gchar *unique_name,
216 EmpathyChatPriv *priv = GET_PRIV (chat);
219 account = mc_account_lookup (unique_name);
221 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
222 empathy_account_equal (account, priv->account)) {
223 TpHandleType handle_type;
225 empathy_debug (DEBUG_DOMAIN,
226 "Account reconnected, request a new Text channel");
228 if (empathy_chat_is_group_chat (chat)) {
229 handle_type = TP_HANDLE_TYPE_ROOM;
231 handle_type = TP_HANDLE_TYPE_CONTACT;
234 mission_control_request_channel_with_string_handle (mc,
236 TP_IFACE_CHANNEL_TYPE_TEXT,
242 g_object_unref (account);
246 empathy_chat_class_init (EmpathyChatClass *klass)
248 GObjectClass *object_class;
250 object_class = G_OBJECT_CLASS (klass);
252 object_class->finalize = chat_finalize;
253 object_class->get_property = chat_get_property;
254 object_class->set_property = chat_set_property;
256 g_object_class_install_property (object_class,
258 g_param_spec_object ("tp-chat",
260 "The tp chat object",
261 EMPATHY_TYPE_TP_CHAT,
266 g_signal_new ("composing",
267 G_OBJECT_CLASS_TYPE (object_class),
271 g_cclosure_marshal_VOID__BOOLEAN,
275 signals[NEW_MESSAGE] =
276 g_signal_new ("new-message",
277 G_OBJECT_CLASS_TYPE (object_class),
281 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
283 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
285 signals[NAME_CHANGED] =
286 g_signal_new ("name-changed",
287 G_OBJECT_CLASS_TYPE (object_class),
291 g_cclosure_marshal_VOID__POINTER,
295 signals[STATUS_CHANGED] =
296 g_signal_new ("status-changed",
297 G_OBJECT_CLASS_TYPE (object_class),
301 g_cclosure_marshal_VOID__VOID,
305 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
309 empathy_chat_init (EmpathyChat *chat)
311 EmpathyChatPriv *priv = GET_PRIV (chat);
312 GtkTextBuffer *buffer;
314 chat->view = empathy_chat_view_new ();
315 chat->input_text_view = gtk_text_view_new ();
317 priv->is_first_char = TRUE;
319 g_object_set (chat->input_text_view,
320 "pixels-above-lines", 2,
321 "pixels-below-lines", 2,
322 "pixels-inside-wrap", 1,
325 "wrap-mode", GTK_WRAP_WORD_CHAR,
328 priv->log_manager = empathy_log_manager_new ();
329 priv->default_window_height = -1;
330 priv->vscroll_visible = FALSE;
331 priv->sensitive = TRUE;
332 priv->sent_messages = NULL;
333 priv->sent_messages_index = -1;
334 priv->first_tp_chat = TRUE;
335 priv->mc = empathy_mission_control_new ();
337 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
338 G_CALLBACK (chat_status_changed_cb),
341 g_signal_connect (chat->input_text_view,
343 G_CALLBACK (chat_input_key_press_event_cb),
346 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
347 g_signal_connect (buffer,
349 G_CALLBACK (chat_input_text_buffer_changed_cb),
351 g_signal_connect (chat->view,
353 G_CALLBACK (chat_text_view_focus_in_event_cb),
356 g_signal_connect (chat->input_text_view,
358 G_CALLBACK (chat_text_view_size_allocate_cb),
361 g_signal_connect (chat->input_text_view,
363 G_CALLBACK (chat_text_view_realize_cb),
366 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
368 G_CALLBACK (chat_text_populate_popup_cb),
371 /* create misspelt words identification tag */
372 gtk_text_buffer_create_tag (buffer,
374 "underline", PANGO_UNDERLINE_ERROR,
379 chat_finalize (GObject *object)
382 EmpathyChatPriv *priv;
384 chat = EMPATHY_CHAT (object);
385 priv = GET_PRIV (chat);
387 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
389 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
390 g_slist_free (priv->sent_messages);
392 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
393 g_list_free (priv->compositors);
395 chat_composing_remove_timeout (chat);
396 g_object_unref (priv->log_manager);
398 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
399 G_CALLBACK (chat_status_changed_cb),
401 g_object_unref (priv->mc);
405 g_object_unref (priv->tp_chat);
409 g_object_unref (priv->account);
412 if (priv->scroll_idle_id) {
413 g_source_remove (priv->scroll_idle_id);
416 if (priv->block_events_timeout_id) {
417 g_source_remove (priv->block_events_timeout_id);
422 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
426 chat_destroy_cb (EmpathyTpChat *tp_chat,
429 EmpathyChatPriv *priv;
431 priv = GET_PRIV (chat);
434 g_object_unref (priv->tp_chat);
435 priv->tp_chat = NULL;
437 priv->sensitive = FALSE;
439 empathy_chat_view_append_event (chat->view, _("Disconnected"));
440 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
442 if (priv->block_events_timeout_id != 0) {
443 g_source_remove (priv->block_events_timeout_id);
446 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
447 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
452 chat_send (EmpathyChat *chat,
455 EmpathyChatPriv *priv;
456 EmpathyMessage *message;
458 priv = GET_PRIV (chat);
460 if (G_STR_EMPTY (msg)) {
464 chat_sent_message_add (chat, msg);
466 if (g_str_has_prefix (msg, "/clear")) {
467 empathy_chat_view_clear (chat->view);
471 /* FIXME: add here something to let group/privrate chat handle
472 * some special messages */
474 message = empathy_message_new (msg);
476 empathy_tp_chat_send (priv->tp_chat, message);
478 g_object_unref (message);
482 chat_input_text_view_send (EmpathyChat *chat)
484 EmpathyChatPriv *priv;
485 GtkTextBuffer *buffer;
486 GtkTextIter start, end;
489 priv = GET_PRIV (chat);
491 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
493 gtk_text_buffer_get_bounds (buffer, &start, &end);
494 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
496 /* clear the input field */
497 gtk_text_buffer_set_text (buffer, "", -1);
499 chat_send (chat, msg);
503 priv->is_first_char = TRUE;
507 chat_message_received_cb (EmpathyTpChat *tp_chat,
508 EmpathyMessage *message,
511 EmpathyChatPriv *priv;
512 EmpathyContact *sender;
515 priv = GET_PRIV (chat);
517 timestamp = empathy_message_get_timestamp (message);
518 if (timestamp <= priv->last_log_timestamp) {
519 /* Do not take care of messages anterior of the last
520 * logged message. Some Jabber chatroom sends messages
521 * received before we joined the room, this avoid
522 * displaying those messages if we already logged them
523 * last time we joined that room. */
524 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
525 "anterior of last logged message.");
529 sender = empathy_message_get_sender (message);
530 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
531 empathy_contact_get_name (sender));
533 empathy_log_manager_add_message (priv->log_manager,
534 empathy_chat_get_id (chat),
535 empathy_chat_is_group_chat (chat),
538 empathy_chat_view_append_message (chat->view, message);
540 if (empathy_chat_should_play_sound (chat)) {
541 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
544 /* We received a message so the contact is no more composing */
545 chat_state_changed_cb (tp_chat, sender,
546 TP_CHANNEL_CHAT_STATE_ACTIVE,
549 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
553 chat_send_error_cb (EmpathyTpChat *tp_chat,
554 EmpathyMessage *message,
555 TpChannelTextSendError error_code,
561 switch (error_code) {
562 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
563 error = _("offline");
565 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
566 error = _("invalid contact");
568 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
569 error = _("permission denied");
571 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
572 error = _("too long message");
574 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
575 error = _("not implemented");
578 error = _("unknown");
582 str = g_strdup_printf (_("Error sending message '%s': %s"),
583 empathy_message_get_body (message),
585 empathy_chat_view_append_event (chat->view, str);
590 chat_sent_message_add (EmpathyChat *chat,
593 EmpathyChatPriv *priv;
597 priv = GET_PRIV (chat);
599 /* Save the sent message in our repeat buffer */
600 list = priv->sent_messages;
602 /* Remove any other occurances of this msg */
603 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
604 list = g_slist_remove_link (list, item);
606 g_slist_free1 (item);
609 /* Trim the list to the last 10 items */
610 while (g_slist_length (list) > 10) {
611 item = g_slist_last (list);
613 list = g_slist_remove_link (list, item);
615 g_slist_free1 (item);
619 /* Add new message */
620 list = g_slist_prepend (list, g_strdup (str));
622 /* Set list and reset the index */
623 priv->sent_messages = list;
624 priv->sent_messages_index = -1;
628 chat_sent_message_get_next (EmpathyChat *chat)
630 EmpathyChatPriv *priv;
633 priv = GET_PRIV (chat);
635 if (!priv->sent_messages) {
636 empathy_debug (DEBUG_DOMAIN,
637 "No sent messages, next message is NULL");
641 max = g_slist_length (priv->sent_messages) - 1;
643 if (priv->sent_messages_index < max) {
644 priv->sent_messages_index++;
647 empathy_debug (DEBUG_DOMAIN,
648 "Returning next message index:%d",
649 priv->sent_messages_index);
651 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
655 chat_sent_message_get_last (EmpathyChat *chat)
657 EmpathyChatPriv *priv;
659 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
661 priv = GET_PRIV (chat);
663 if (!priv->sent_messages) {
664 empathy_debug (DEBUG_DOMAIN,
665 "No sent messages, last message is NULL");
669 if (priv->sent_messages_index >= 0) {
670 priv->sent_messages_index--;
673 empathy_debug (DEBUG_DOMAIN,
674 "Returning last message index:%d",
675 priv->sent_messages_index);
677 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
681 chat_input_key_press_event_cb (GtkWidget *widget,
685 EmpathyChatPriv *priv;
688 GtkWidget *text_view_sw;
690 priv = GET_PRIV (chat);
692 /* Catch ctrl+up/down so we can traverse messages we sent */
693 if ((event->state & GDK_CONTROL_MASK) &&
694 (event->keyval == GDK_Up ||
695 event->keyval == GDK_Down)) {
696 GtkTextBuffer *buffer;
699 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
701 if (event->keyval == GDK_Up) {
702 str = chat_sent_message_get_next (chat);
704 str = chat_sent_message_get_last (chat);
707 g_signal_handlers_block_by_func (buffer,
708 chat_input_text_buffer_changed_cb,
710 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
711 g_signal_handlers_unblock_by_func (buffer,
712 chat_input_text_buffer_changed_cb,
718 /* Catch enter but not ctrl/shift-enter */
719 if (IS_ENTER (event->keyval) &&
720 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
723 /* This is to make sure that kinput2 gets the enter. And if
724 * it's handled there we shouldn't send on it. This is because
725 * kinput2 uses Enter to commit letters. See:
726 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
729 view = GTK_TEXT_VIEW (chat->input_text_view);
730 if (gtk_im_context_filter_keypress (view->im_context, event)) {
731 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
735 chat_input_text_view_send (chat);
739 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
741 if (IS_ENTER (event->keyval) &&
742 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
743 /* Newline for shift/control-enter. */
746 else if (!(event->state & GDK_CONTROL_MASK) &&
747 event->keyval == GDK_Page_Up) {
748 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
749 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
753 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
754 event->keyval == GDK_Page_Down) {
755 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
756 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
757 gtk_adjustment_set_value (adj, val);
762 if (EMPATHY_CHAT_GET_CLASS (chat)->key_press_event) {
763 return EMPATHY_CHAT_GET_CLASS (chat)->key_press_event (chat, event);
770 chat_text_view_focus_in_event_cb (GtkWidget *widget,
774 gtk_widget_grab_focus (chat->input_text_view);
780 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
783 EmpathyChatPriv *priv;
784 GtkTextIter start, end;
786 gboolean spell_checker = FALSE;
788 priv = GET_PRIV (chat);
790 if (gtk_text_buffer_get_char_count (buffer) == 0) {
791 chat_composing_stop (chat);
793 chat_composing_start (chat);
796 empathy_conf_get_bool (empathy_conf_get (),
797 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
800 if (priv->is_first_char) {
804 GtkAllocation *allocation;
806 /* Save the window's size */
807 dialog = empathy_get_toplevel_window (priv->widget);
809 gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
810 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;
818 priv->is_first_char = FALSE;
821 gtk_text_buffer_get_start_iter (buffer, &start);
823 if (!spell_checker) {
824 gtk_text_buffer_get_end_iter (buffer, &end);
825 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
829 if (!empathy_spell_supported ()) {
833 /* NOTE: this is really inefficient, we shouldn't have to
834 reiterate the whole buffer each time and check each work
837 gboolean correct = FALSE;
840 if (gtk_text_iter_is_start (&start)) {
843 if (!gtk_text_iter_forward_word_end (&end)) {
844 /* no whole word yet */
848 if (!gtk_text_iter_forward_word_end (&end)) {
849 /* must be the end of the buffer */
854 gtk_text_iter_backward_word_start (&start);
857 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
859 /* spell check string */
860 if (!empathy_chat_get_is_command (str)) {
861 correct = empathy_spell_check (str);
867 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
869 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
874 /* set start iter to the end iters position */
886 chat_change_size_in_idle_cb (ChangeSizeData *data)
888 gtk_window_resize (data->window, 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_get_toplevel_window (priv->widget);
960 gtk_window_get_size (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 (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 (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 EmpathyChatPriv *priv = GET_PRIV (chat);
1396 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1398 if (!priv->widget && EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1399 priv->widget = EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1402 return priv->widget;
1406 empathy_chat_is_group_chat (EmpathyChat *chat)
1408 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1410 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1411 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1418 empathy_chat_is_connected (EmpathyChat *chat)
1420 EmpathyChatPriv *priv;
1422 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1424 priv = GET_PRIV (chat);
1426 return (priv->tp_chat != NULL);
1429 static const gchar *
1430 chat_get_window_id_for_geometry (EmpathyChat *chat)
1432 gboolean separate_windows;
1434 empathy_conf_get_bool (empathy_conf_get (),
1435 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1438 if (separate_windows) {
1439 return empathy_chat_get_id (chat);
1441 return "chat-window";
1446 empathy_chat_save_geometry (EmpathyChat *chat,
1452 empathy_geometry_save (chat_get_window_id_for_geometry (chat), x, y, w, h);
1456 empathy_chat_load_geometry (EmpathyChat *chat,
1462 empathy_geometry_load (chat_get_window_id_for_geometry (chat), x, y, w, h);
1466 chat_block_events_timeout_cb (gpointer data)
1468 EmpathyChat *chat = EMPATHY_CHAT (data);
1469 EmpathyChatPriv *priv = GET_PRIV (chat);
1471 chat->block_events = FALSE;
1472 priv->block_events_timeout_id = 0;
1478 empathy_chat_set_tp_chat (EmpathyChat *chat,
1479 EmpathyTpChat *tp_chat)
1481 EmpathyChatPriv *priv;
1483 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1484 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1486 priv = GET_PRIV (chat);
1488 if (tp_chat == priv->tp_chat) {
1492 /* Block events for some time to avoid having "has come online" or
1493 * "joined" messages. */
1494 chat->block_events = TRUE;
1495 if (priv->block_events_timeout_id != 0) {
1496 g_source_remove (priv->block_events_timeout_id);
1498 priv->block_events_timeout_id =
1499 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1501 if (priv->tp_chat) {
1502 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1503 chat_message_received_cb,
1505 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1508 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1511 g_object_unref (priv->tp_chat);
1513 if (priv->account) {
1514 g_object_unref (priv->account);
1518 priv->tp_chat = g_object_ref (tp_chat);
1519 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1520 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1521 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1523 if (priv->first_tp_chat) {
1524 chat_add_logs (chat);
1525 priv->first_tp_chat = FALSE;
1528 g_signal_connect (tp_chat, "message-received",
1529 G_CALLBACK (chat_message_received_cb),
1531 g_signal_connect (tp_chat, "send-error",
1532 G_CALLBACK (chat_send_error_cb),
1534 g_signal_connect (tp_chat, "chat-state-changed",
1535 G_CALLBACK (chat_state_changed_cb),
1537 g_signal_connect (tp_chat, "destroy",
1538 G_CALLBACK (chat_destroy_cb),
1541 if (!priv->sensitive) {
1542 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1543 empathy_chat_view_append_event (chat->view, _("Connected"));
1544 priv->sensitive = TRUE;
1547 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1548 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1551 g_object_notify (G_OBJECT (chat), "tp-chat");
1555 empathy_chat_get_id (EmpathyChat *chat)
1557 EmpathyChatPriv *priv;
1559 priv = GET_PRIV (chat);
1565 empathy_chat_get_account (EmpathyChat *chat)
1567 EmpathyChatPriv *priv = GET_PRIV (chat);
1569 return priv->account;
1573 empathy_chat_clear (EmpathyChat *chat)
1575 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1577 empathy_chat_view_clear (chat->view);
1581 empathy_chat_scroll_down (EmpathyChat *chat)
1583 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1585 empathy_chat_view_scroll_down (chat->view);
1589 empathy_chat_cut (EmpathyChat *chat)
1591 GtkTextBuffer *buffer;
1593 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1595 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1596 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1597 GtkClipboard *clipboard;
1599 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1601 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1606 empathy_chat_copy (EmpathyChat *chat)
1608 GtkTextBuffer *buffer;
1610 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1612 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1613 empathy_chat_view_copy_clipboard (chat->view);
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_copy_clipboard (buffer, clipboard);
1628 empathy_chat_paste (EmpathyChat *chat)
1630 GtkTextBuffer *buffer;
1631 GtkClipboard *clipboard;
1633 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1635 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1636 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1638 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1642 empathy_chat_should_play_sound (EmpathyChat *chat)
1644 EmpathyChatPriv *priv = GET_PRIV (chat);
1646 gboolean has_focus = FALSE;
1648 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1650 window = empathy_get_toplevel_window (priv->widget);
1652 g_object_get (window, "has-toplevel-focus", &has_focus, NULL);
1659 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1661 EmpathyContact *contact;
1662 const gchar *msg, *to;
1663 gchar *cf_msg, *cf_to;
1667 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1669 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1673 msg = empathy_message_get_body (message);
1678 contact = empathy_message_get_receiver (message);
1679 if (!contact || !empathy_contact_is_user (contact)) {
1683 to = empathy_contact_get_name (contact);
1688 cf_msg = g_utf8_casefold (msg, -1);
1689 cf_to = g_utf8_casefold (to, -1);
1691 ch = strstr (cf_msg, cf_to);
1697 /* Not first in the message */
1698 if ((*(ch - 1) != ' ') &&
1699 (*(ch - 1) != ',') &&
1700 (*(ch - 1) != '.')) {
1705 ch = ch + strlen (cf_to);
1706 if (ch >= cf_msg + strlen (cf_msg)) {