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>
38 #include <telepathy-glib/util.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-debug.h>
43 #include <libempathy/empathy-utils.h>
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-spell-dialog.h"
49 #include "empathy-ui-utils.h"
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
53 #define DEBUG_DOMAIN "Chat"
55 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
56 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
57 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
58 #define MAX_INPUT_HEIGHT 150
59 #define COMPOSING_STOP_TIMEOUT 5
61 struct _EmpathyChatPriv {
62 EmpathyTpChat *tp_chat;
67 EmpathyContact *selected_contact;
70 EmpathyLogManager *log_manager;
72 GSList *sent_messages;
73 gint sent_messages_index;
75 GList *backlog_messages;
76 GCompletion *completion;
77 guint composing_stop_timeout_id;
78 guint block_events_timeout_id;
79 TpHandleType handle_type;
84 GtkWidget *scrolled_window_chat;
85 GtkWidget *scrolled_window_input;
86 GtkWidget *scrolled_window_contacts;
87 GtkWidget *hbox_topic;
88 GtkWidget *label_topic;
90 /* Used to automatically shrink a window that has temporarily
91 * grown due to long input.
94 gint default_window_height;
95 gint last_input_height;
96 gboolean vscroll_visible;
97 gboolean is_first_char;
100 static void empathy_chat_class_init (EmpathyChatClass *klass);
101 static void empathy_chat_init (EmpathyChat *chat);
116 PROP_SELECTED_CONTACT,
120 static guint signals[LAST_SIGNAL] = { 0 };
122 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
125 chat_get_property (GObject *object,
130 EmpathyChatPriv *priv = GET_PRIV (object);
134 g_value_set_object (value, priv->tp_chat);
137 g_value_set_object (value, priv->account);
140 g_value_set_string (value, priv->name);
143 g_value_set_string (value, priv->subject);
146 g_value_set_string (value, priv->tooltip);
148 case PROP_SELECTED_CONTACT:
149 g_value_set_object (value, priv->selected_contact);
152 g_value_set_string (value, priv->id);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
161 chat_set_property (GObject *object,
166 EmpathyChat *chat = EMPATHY_CHAT (object);
170 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
179 chat_status_changed_cb (MissionControl *mc,
180 TpConnectionStatus status,
182 TpConnectionStatusReason reason,
183 const gchar *unique_name,
186 EmpathyChatPriv *priv = GET_PRIV (chat);
189 account = mc_account_lookup (unique_name);
191 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
192 empathy_account_equal (account, priv->account) &&
193 priv->handle_type != 0) {
194 empathy_debug (DEBUG_DOMAIN,
195 "Account reconnected, request a new Text channel");
196 mission_control_request_channel_with_string_handle (mc,
198 TP_IFACE_CHANNEL_TYPE_TEXT,
204 g_object_unref (account);
208 chat_composing_remove_timeout (EmpathyChat *chat)
210 EmpathyChatPriv *priv;
212 priv = GET_PRIV (chat);
214 if (priv->composing_stop_timeout_id) {
215 g_source_remove (priv->composing_stop_timeout_id);
216 priv->composing_stop_timeout_id = 0;
221 chat_composing_stop_timeout_cb (EmpathyChat *chat)
223 EmpathyChatPriv *priv;
225 priv = GET_PRIV (chat);
227 priv->composing_stop_timeout_id = 0;
228 empathy_tp_chat_set_state (priv->tp_chat,
229 TP_CHANNEL_CHAT_STATE_PAUSED);
235 chat_composing_start (EmpathyChat *chat)
237 EmpathyChatPriv *priv;
239 priv = GET_PRIV (chat);
241 if (priv->composing_stop_timeout_id) {
242 /* Just restart the timeout */
243 chat_composing_remove_timeout (chat);
245 empathy_tp_chat_set_state (priv->tp_chat,
246 TP_CHANNEL_CHAT_STATE_COMPOSING);
249 priv->composing_stop_timeout_id = g_timeout_add_seconds (
250 COMPOSING_STOP_TIMEOUT,
251 (GSourceFunc) chat_composing_stop_timeout_cb,
256 chat_composing_stop (EmpathyChat *chat)
258 EmpathyChatPriv *priv;
260 priv = GET_PRIV (chat);
262 chat_composing_remove_timeout (chat);
263 empathy_tp_chat_set_state (priv->tp_chat,
264 TP_CHANNEL_CHAT_STATE_ACTIVE);
268 chat_destroy_cb (EmpathyTpChat *tp_chat,
271 EmpathyChatPriv *priv;
273 priv = GET_PRIV (chat);
276 g_object_unref (priv->tp_chat);
277 priv->tp_chat = NULL;
280 empathy_chat_view_append_event (chat->view, _("Disconnected"));
281 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
285 chat_sent_message_add (EmpathyChat *chat,
288 EmpathyChatPriv *priv;
292 priv = GET_PRIV (chat);
294 /* Save the sent message in our repeat buffer */
295 list = priv->sent_messages;
297 /* Remove any other occurances of this msg */
298 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
299 list = g_slist_remove_link (list, item);
301 g_slist_free1 (item);
304 /* Trim the list to the last 10 items */
305 while (g_slist_length (list) > 10) {
306 item = g_slist_last (list);
308 list = g_slist_remove_link (list, item);
310 g_slist_free1 (item);
314 /* Add new message */
315 list = g_slist_prepend (list, g_strdup (str));
317 /* Set list and reset the index */
318 priv->sent_messages = list;
319 priv->sent_messages_index = -1;
323 chat_sent_message_get_next (EmpathyChat *chat)
325 EmpathyChatPriv *priv;
328 priv = GET_PRIV (chat);
330 if (!priv->sent_messages) {
331 empathy_debug (DEBUG_DOMAIN,
332 "No sent messages, next message is NULL");
336 max = g_slist_length (priv->sent_messages) - 1;
338 if (priv->sent_messages_index < max) {
339 priv->sent_messages_index++;
342 empathy_debug (DEBUG_DOMAIN,
343 "Returning next message index:%d",
344 priv->sent_messages_index);
346 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
350 chat_sent_message_get_last (EmpathyChat *chat)
352 EmpathyChatPriv *priv;
354 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
356 priv = GET_PRIV (chat);
358 if (!priv->sent_messages) {
359 empathy_debug (DEBUG_DOMAIN,
360 "No sent messages, last message is NULL");
364 if (priv->sent_messages_index >= 0) {
365 priv->sent_messages_index--;
368 empathy_debug (DEBUG_DOMAIN,
369 "Returning last message index:%d",
370 priv->sent_messages_index);
372 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
376 chat_send (EmpathyChat *chat,
379 EmpathyChatPriv *priv;
380 EmpathyMessage *message;
382 priv = GET_PRIV (chat);
384 if (G_STR_EMPTY (msg)) {
388 chat_sent_message_add (chat, msg);
390 if (g_str_has_prefix (msg, "/clear")) {
391 empathy_chat_view_clear (chat->view);
395 message = empathy_message_new (msg);
397 empathy_tp_chat_send (priv->tp_chat, message);
399 g_object_unref (message);
403 chat_input_text_view_send (EmpathyChat *chat)
405 EmpathyChatPriv *priv;
406 GtkTextBuffer *buffer;
407 GtkTextIter start, end;
410 priv = GET_PRIV (chat);
412 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
414 gtk_text_buffer_get_bounds (buffer, &start, &end);
415 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
417 /* clear the input field */
418 gtk_text_buffer_set_text (buffer, "", -1);
420 chat_send (chat, msg);
423 priv->is_first_char = TRUE;
427 chat_state_changed_cb (EmpathyTpChat *tp_chat,
428 EmpathyContact *contact,
429 TpChannelChatState state,
432 EmpathyChatPriv *priv;
434 gboolean was_composing;
436 priv = GET_PRIV (chat);
438 if (empathy_contact_is_user (contact)) {
439 /* We don't care about our own chat state */
443 was_composing = (priv->compositors != NULL);
445 /* Find the contact in the list. After that l is the list elem or NULL */
446 for (l = priv->compositors; l; l = l->next) {
447 if (contact == l->data) {
453 case TP_CHANNEL_CHAT_STATE_GONE:
454 case TP_CHANNEL_CHAT_STATE_INACTIVE:
455 case TP_CHANNEL_CHAT_STATE_PAUSED:
456 case TP_CHANNEL_CHAT_STATE_ACTIVE:
457 /* Contact is not composing */
459 priv->compositors = g_list_remove_link (priv->compositors, l);
460 g_object_unref (l->data);
464 case TP_CHANNEL_CHAT_STATE_COMPOSING:
465 /* Contact is composing */
467 priv->compositors = g_list_prepend (priv->compositors,
468 g_object_ref (contact));
472 g_assert_not_reached ();
475 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
476 was_composing ? "yes" : "no",
477 priv->compositors ? "yes" : "no");
479 if ((was_composing && !priv->compositors) ||
480 (!was_composing && priv->compositors)) {
481 /* Composing state changed */
482 g_signal_emit (chat, signals[COMPOSING], 0,
483 priv->compositors != NULL);
488 chat_message_received_cb (EmpathyTpChat *tp_chat,
489 EmpathyMessage *message,
492 EmpathyChatPriv *priv;
493 EmpathyContact *sender;
496 priv = GET_PRIV (chat);
498 sender = empathy_message_get_sender (message);
499 body = empathy_message_get_body (message);
500 while (priv->backlog_messages) {
501 EmpathyMessage *log_message;
502 EmpathyContact *log_sender;
503 const gchar *log_body;
505 log_message = priv->backlog_messages->data;
506 log_sender = empathy_message_get_sender (log_message);
507 log_body = empathy_message_get_body (log_message);
509 priv->backlog_messages = g_list_remove (priv->backlog_messages,
512 if (empathy_contact_equal (sender, log_sender) &&
513 !tp_strdiff (body, log_body)) {
514 /* The message we received is already displayed because
515 * some jabber chatrooms sends us back logs and we
516 * already displayed it from localy logged messages. */
517 empathy_debug (DEBUG_DOMAIN, "Skipping message because "
518 "it is already displayed from logged "
520 g_object_unref (log_message);
523 g_object_unref (log_message);
526 empathy_debug (DEBUG_DOMAIN, "Appending new message from %s (%d)",
527 empathy_contact_get_name (sender),
528 empathy_contact_get_handle (sender));
530 empathy_log_manager_add_message (priv->log_manager,
531 empathy_chat_get_id (chat),
535 empathy_chat_view_append_message (chat->view, message);
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);
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_property_changed_cb (EmpathyTpChat *tp_chat,
588 EmpathyChatPriv *priv = GET_PRIV (chat);
590 if (!tp_strdiff (name, "subject")) {
591 g_free (priv->subject);
592 priv->subject = g_value_dup_string (value);
593 g_object_notify (G_OBJECT (chat), "subject");
595 else if (!tp_strdiff (name, "name")) {
597 priv->name = g_value_dup_string (value);
598 g_object_notify (G_OBJECT (chat), "name");
603 g_free (priv->tooltip);
604 priv->tooltip = g_strconcat (priv->name, "\n",
605 _("Topic: "), priv->subject, NULL);
606 g_object_notify (G_OBJECT (chat), "tooltip");
610 chat_selected_contact_notify_cb (EmpathyChat *chat)
612 EmpathyChatPriv *priv = GET_PRIV (chat);
615 priv->name = g_strdup (empathy_contact_get_name (priv->selected_contact));
616 g_object_notify (G_OBJECT (chat), "name");
618 g_free (priv->tooltip);
619 priv->tooltip = g_strconcat (empathy_contact_get_id (priv->selected_contact),
621 empathy_contact_get_status (priv->selected_contact),
623 g_object_notify (G_OBJECT (chat), "tooltip");
627 chat_remote_contact_notify_cb (EmpathyChat *chat)
629 EmpathyChatPriv *priv = GET_PRIV (chat);
630 EmpathyContact *contact;
632 contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
633 if (contact == priv->selected_contact) {
637 if (priv->selected_contact) {
638 g_signal_handlers_disconnect_by_func (contact,
639 chat_selected_contact_notify_cb,
641 g_object_unref (priv->selected_contact);
642 priv->selected_contact = NULL;
646 priv->selected_contact = g_object_ref (contact);
647 g_signal_connect_swapped (contact, "notify",
648 G_CALLBACK (chat_selected_contact_notify_cb),
650 chat_selected_contact_notify_cb (chat);
653 g_object_notify (G_OBJECT (chat), "selected-contact");
657 chat_get_is_command (const gchar *str)
659 g_return_val_if_fail (str != NULL, FALSE);
665 if (g_str_has_prefix (str, "/me")) {
668 else if (g_str_has_prefix (str, "/nick")) {
671 else if (g_str_has_prefix (str, "/topic")) {
679 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
682 EmpathyChatPriv *priv;
683 GtkTextIter start, end;
685 gboolean spell_checker = FALSE;
687 priv = GET_PRIV (chat);
689 if (gtk_text_buffer_get_char_count (buffer) == 0) {
690 chat_composing_stop (chat);
692 chat_composing_start (chat);
695 empathy_conf_get_bool (empathy_conf_get (),
696 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
699 if (priv->is_first_char) {
703 GtkAllocation *allocation;
705 /* Save the window's size */
706 dialog = empathy_get_toplevel_window (GTK_WIDGET (chat));
708 gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
709 gtk_widget_size_request (chat->input_text_view, &req);
710 allocation = >K_WIDGET (chat->view)->allocation;
712 priv->default_window_height = window_height;
713 priv->last_input_height = req.height;
714 priv->padding_height = window_height - req.height - allocation->height;
717 priv->is_first_char = FALSE;
720 gtk_text_buffer_get_start_iter (buffer, &start);
722 if (!spell_checker) {
723 gtk_text_buffer_get_end_iter (buffer, &end);
724 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
728 if (!empathy_spell_supported ()) {
732 /* NOTE: this is really inefficient, we shouldn't have to
733 reiterate the whole buffer each time and check each work
736 gboolean correct = FALSE;
739 if (gtk_text_iter_is_start (&start)) {
742 if (!gtk_text_iter_forward_word_end (&end)) {
743 /* no whole word yet */
747 if (!gtk_text_iter_forward_word_end (&end)) {
748 /* must be the end of the buffer */
753 gtk_text_iter_backward_word_start (&start);
756 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
758 /* spell check string */
759 if (!chat_get_is_command (str)) {
760 correct = empathy_spell_check (str);
766 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
768 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
773 /* set start iter to the end iters position */
779 chat_input_key_press_event_cb (GtkWidget *widget,
783 EmpathyChatPriv *priv;
786 GtkWidget *text_view_sw;
788 priv = GET_PRIV (chat);
790 /* Catch ctrl+up/down so we can traverse messages we sent */
791 if ((event->state & GDK_CONTROL_MASK) &&
792 (event->keyval == GDK_Up ||
793 event->keyval == GDK_Down)) {
794 GtkTextBuffer *buffer;
797 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
799 if (event->keyval == GDK_Up) {
800 str = chat_sent_message_get_next (chat);
802 str = chat_sent_message_get_last (chat);
805 g_signal_handlers_block_by_func (buffer,
806 chat_input_text_buffer_changed_cb,
808 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
809 g_signal_handlers_unblock_by_func (buffer,
810 chat_input_text_buffer_changed_cb,
816 /* Catch enter but not ctrl/shift-enter */
817 if (IS_ENTER (event->keyval) &&
818 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
821 /* This is to make sure that kinput2 gets the enter. And if
822 * it's handled there we shouldn't send on it. This is because
823 * kinput2 uses Enter to commit letters. See:
824 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
827 view = GTK_TEXT_VIEW (chat->input_text_view);
828 if (gtk_im_context_filter_keypress (view->im_context, event)) {
829 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
833 chat_input_text_view_send (chat);
837 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
839 if (IS_ENTER (event->keyval) &&
840 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
841 /* Newline for shift/control-enter. */
844 if (!(event->state & GDK_CONTROL_MASK) &&
845 event->keyval == GDK_Page_Up) {
846 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
847 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
850 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
851 event->keyval == GDK_Page_Down) {
852 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
853 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
854 gtk_adjustment_set_value (adj, val);
857 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
858 event->keyval == GDK_Tab) {
859 GtkTextBuffer *buffer;
860 GtkTextIter start, current;
861 gchar *nick, *completed;
862 GList *list, *completed_list;
863 gboolean is_start_of_buffer;
865 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
866 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
868 /* Get the start of the nick to complete. */
869 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
870 gtk_text_iter_backward_word_start (&start);
871 is_start_of_buffer = gtk_text_iter_is_start (&start);
873 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
874 g_completion_add_items (priv->completion, list);
876 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
877 completed_list = g_completion_complete (priv->completion,
886 gchar *complete_char = NULL;
888 gtk_text_buffer_delete (buffer, &start, ¤t);
890 len = g_list_length (completed_list);
893 /* If we only have one hit, use that text
894 * instead of the text in completed since the
895 * completed text will use the typed string
896 * which might be cased all wrong.
899 text = empathy_contact_get_name (completed_list->data);
904 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
906 if (len == 1 && is_start_of_buffer &&
907 empathy_conf_get_string (empathy_conf_get (),
908 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
910 complete_char != NULL) {
911 gtk_text_buffer_insert_at_cursor (buffer,
913 strlen (complete_char));
914 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
915 g_free (complete_char);
921 g_completion_clear_items (priv->completion);
923 g_list_foreach (list, (GFunc) g_object_unref, NULL);
933 chat_text_view_focus_in_event_cb (GtkWidget *widget,
937 gtk_widget_grab_focus (chat->input_text_view);
949 chat_change_size_in_idle_cb (ChangeSizeData *data)
951 gtk_window_resize (data->window, data->width, data->height);
957 chat_text_view_scroll_hide_cb (GtkWidget *widget,
960 EmpathyChatPriv *priv;
963 priv = GET_PRIV (chat);
965 priv->vscroll_visible = FALSE;
966 g_signal_handlers_disconnect_by_func (widget,
967 chat_text_view_scroll_hide_cb,
970 sw = gtk_widget_get_parent (chat->input_text_view);
971 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
974 g_object_set (sw, "height-request", -1, NULL);
978 chat_text_view_size_allocate_cb (GtkWidget *widget,
979 GtkAllocation *allocation,
982 EmpathyChatPriv *priv;
985 ChangeSizeData *data;
988 GtkAllocation *view_allocation;
993 priv = GET_PRIV (chat);
995 if (priv->default_window_height <= 0) {
999 sw = gtk_widget_get_parent (widget);
1000 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
1003 priv->vscroll_visible = TRUE;
1004 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
1005 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1007 GTK_POLICY_AUTOMATIC);
1008 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
1009 g_signal_connect (vscroll, "hide",
1010 G_CALLBACK (chat_text_view_scroll_hide_cb),
1014 if (priv->last_input_height <= allocation->height) {
1015 priv->last_input_height = allocation->height;
1019 diff = priv->last_input_height - allocation->height;
1020 priv->last_input_height = allocation->height;
1022 view_allocation = >K_WIDGET (chat->view)->allocation;
1024 dialog = empathy_get_toplevel_window (GTK_WIDGET (widget));
1025 gtk_window_get_size (dialog, NULL, ¤t_height);
1027 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
1029 if (new_height <= priv->default_window_height) {
1030 window_height = priv->default_window_height;
1032 window_height = new_height;
1035 if (current_height <= window_height) {
1039 /* Restore the window's size */
1040 gtk_window_get_size (dialog, &width, NULL);
1042 data = g_new0 (ChangeSizeData, 1);
1043 data->window = dialog;
1044 data->width = width;
1045 data->height = window_height;
1047 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1048 (GSourceFunc) chat_change_size_in_idle_cb,
1053 chat_text_view_realize_cb (GtkWidget *widget,
1056 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
1057 gtk_widget_grab_focus (widget);
1061 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1064 GtkTextBuffer *buffer;
1066 const gchar *smiley;
1068 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1070 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1072 gtk_text_buffer_get_end_iter (buffer, &iter);
1073 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1075 gtk_text_buffer_get_end_iter (buffer, &iter);
1076 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1087 static EmpathyChatSpell *
1088 chat_spell_new (EmpathyChat *chat,
1093 EmpathyChatSpell *chat_spell;
1095 chat_spell = g_slice_new0 (EmpathyChatSpell);
1097 chat_spell->chat = g_object_ref (chat);
1098 chat_spell->word = g_strdup (word);
1099 chat_spell->start = start;
1100 chat_spell->end = end;
1106 chat_spell_free (EmpathyChatSpell *chat_spell)
1108 g_object_unref (chat_spell->chat);
1109 g_free (chat_spell->word);
1110 g_slice_free (EmpathyChatSpell, chat_spell);
1114 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1115 EmpathyChatSpell *chat_spell)
1117 empathy_spell_dialog_show (chat_spell->chat,
1124 chat_text_populate_popup_cb (GtkTextView *view,
1128 EmpathyChatPriv *priv;
1129 GtkTextBuffer *buffer;
1130 GtkTextTagTable *table;
1133 GtkTextIter iter, start, end;
1136 EmpathyChatSpell *chat_spell;
1137 GtkWidget *smiley_menu;
1139 priv = GET_PRIV (chat);
1141 /* Add the emoticon menu. */
1142 item = gtk_separator_menu_item_new ();
1143 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1144 gtk_widget_show (item);
1146 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1147 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1148 gtk_widget_show (item);
1150 smiley_menu = empathy_chat_view_get_smiley_menu (
1151 G_CALLBACK (chat_insert_smiley_activate_cb),
1153 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1155 /* Add the spell check menu item. */
1156 buffer = gtk_text_view_get_buffer (view);
1157 table = gtk_text_buffer_get_tag_table (buffer);
1159 tag = gtk_text_tag_table_lookup (table, "misspelled");
1161 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1163 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1164 GTK_TEXT_WINDOW_WIDGET,
1168 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1172 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1173 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1175 str = gtk_text_buffer_get_text (buffer,
1176 &start, &end, FALSE);
1179 if (G_STR_EMPTY (str)) {
1183 chat_spell = chat_spell_new (chat, str, start, end);
1185 g_object_set_data_full (G_OBJECT (menu),
1186 "chat_spell", chat_spell,
1187 (GDestroyNotify) chat_spell_free);
1189 item = gtk_separator_menu_item_new ();
1190 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1191 gtk_widget_show (item);
1193 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1194 g_signal_connect (item,
1196 G_CALLBACK (chat_text_check_word_spelling_cb),
1198 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1199 gtk_widget_show (item);
1203 chat_add_logs (EmpathyChat *chat)
1205 EmpathyChatPriv *priv;
1206 GList *messages, *l;
1210 priv = GET_PRIV (chat);
1212 /* Turn off scrolling temporarily */
1213 empathy_chat_view_scroll (chat->view, FALSE);
1215 /* Add messages from last conversation */
1216 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1218 empathy_chat_get_id (chat),
1220 num_messages = g_list_length (messages);
1222 /* Only keep the 10 last messages */
1223 for (i = 0; num_messages - i > 10; i++) {
1224 EmpathyMessage *message;
1226 message = messages->data;
1227 messages = g_list_remove (messages, message);
1228 g_object_unref (message);
1231 for (l = messages; l; l = l->next) {
1232 empathy_chat_view_append_message (chat->view, l->data);
1234 priv->backlog_messages = messages;
1236 /* Turn back on scrolling */
1237 empathy_chat_view_scroll (chat->view, TRUE);
1241 chat_contacts_completion_func (const gchar *s1,
1245 gchar *tmp, *nick1, *nick2;
1248 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1249 nick1 = g_utf8_casefold (tmp, -1);
1252 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1253 nick2 = g_utf8_casefold (tmp, -1);
1256 ret = strncmp (nick1, nick2, n);
1265 chat_create_ui (EmpathyChat *chat)
1267 EmpathyChatPriv *priv = GET_PRIV (chat);
1272 filename = empathy_file_lookup ("empathy-chat.glade",
1274 glade = empathy_glade_get_file (filename,
1277 "chat_widget", &priv->widget,
1278 "hpaned", &priv->hpaned,
1279 "vbox_left", &priv->vbox_left,
1280 "scrolled_window_chat", &priv->scrolled_window_chat,
1281 "scrolled_window_input", &priv->scrolled_window_input,
1282 "hbox_topic", &priv->hbox_topic,
1283 "label_topic", &priv->label_topic,
1284 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1287 g_object_unref (glade);
1289 /* Add message GtkTextView. */
1290 chat->view = empathy_chat_view_new ();
1291 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1292 GTK_WIDGET (chat->view));
1293 gtk_widget_show (GTK_WIDGET (chat->view));
1295 /* Add input GtkTextView */
1296 chat->input_text_view = gtk_text_view_new ();
1297 g_object_set (chat->input_text_view,
1298 "pixels-above-lines", 2,
1299 "pixels-below-lines", 2,
1300 "pixels-inside-wrap", 1,
1303 "wrap-mode", GTK_WRAP_WORD_CHAR,
1305 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1306 chat->input_text_view);
1307 gtk_widget_show (chat->input_text_view);
1309 /* Add nick name completion */
1310 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1311 g_completion_set_compare (priv->completion,
1312 chat_contacts_completion_func);
1314 /* Set widget focus order */
1315 list = g_list_append (NULL, priv->scrolled_window_input);
1316 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1319 list = g_list_append (NULL, priv->vbox_left);
1320 list = g_list_append (list, priv->scrolled_window_contacts);
1321 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1324 list = g_list_append (NULL, priv->hpaned);
1325 list = g_list_append (list, priv->hbox_topic);
1326 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1329 /* Add the main widget in the chat widget */
1330 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1334 chat_finalize (GObject *object)
1337 EmpathyChatPriv *priv;
1339 chat = EMPATHY_CHAT (object);
1340 priv = GET_PRIV (chat);
1342 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1344 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1345 g_slist_free (priv->sent_messages);
1347 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1348 g_list_free (priv->compositors);
1350 g_list_foreach (priv->backlog_messages, (GFunc) g_object_unref, NULL);
1351 g_list_free (priv->backlog_messages);
1353 chat_composing_remove_timeout (chat);
1355 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1356 G_CALLBACK (chat_status_changed_cb),
1358 g_object_unref (priv->mc);
1359 g_object_unref (priv->log_manager);
1362 if (priv->tp_chat) {
1363 g_object_unref (priv->tp_chat);
1366 if (priv->account) {
1367 g_object_unref (priv->account);
1370 if (priv->selected_contact) {
1371 g_signal_handlers_disconnect_by_func (priv->selected_contact,
1372 chat_selected_contact_notify_cb,
1374 g_object_unref (priv->selected_contact);
1377 if (priv->block_events_timeout_id) {
1378 g_source_remove (priv->block_events_timeout_id);
1382 g_free (priv->name);
1383 g_free (priv->subject);
1384 g_free (priv->tooltip);
1386 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1390 chat_constructed (GObject *object)
1392 chat_add_logs (EMPATHY_CHAT (object));
1396 empathy_chat_class_init (EmpathyChatClass *klass)
1398 GObjectClass *object_class;
1400 object_class = G_OBJECT_CLASS (klass);
1402 object_class->finalize = chat_finalize;
1403 object_class->get_property = chat_get_property;
1404 object_class->set_property = chat_set_property;
1405 object_class->constructed = chat_constructed;
1407 g_object_class_install_property (object_class,
1409 g_param_spec_object ("tp-chat",
1411 "The tp chat object",
1412 EMPATHY_TYPE_TP_CHAT,
1414 G_PARAM_READWRITE));
1415 g_object_class_install_property (object_class,
1417 g_param_spec_object ("account",
1418 "Account of the chat",
1419 "The account of the chat",
1422 g_object_class_install_property (object_class,
1424 g_param_spec_string ("name",
1426 "The name of the chat",
1429 g_object_class_install_property (object_class,
1431 g_param_spec_string ("subject",
1433 "The subject or topic of the chat",
1436 g_object_class_install_property (object_class,
1438 g_param_spec_string ("tooltip",
1440 "The tooltip of the chat",
1443 g_object_class_install_property (object_class,
1444 PROP_SELECTED_CONTACT,
1445 g_param_spec_object ("selected-contact",
1446 "The selected contact",
1447 "The selected contact, "
1448 "either the remote contact or "
1449 "the one selected on the contact list",
1450 EMPATHY_TYPE_CONTACT,
1452 g_object_class_install_property (object_class,
1454 g_param_spec_string ("id",
1456 "The id of the chat",
1460 signals[COMPOSING] =
1461 g_signal_new ("composing",
1462 G_OBJECT_CLASS_TYPE (object_class),
1466 g_cclosure_marshal_VOID__BOOLEAN,
1470 signals[NEW_MESSAGE] =
1471 g_signal_new ("new-message",
1472 G_OBJECT_CLASS_TYPE (object_class),
1476 g_cclosure_marshal_VOID__OBJECT,
1478 1, EMPATHY_TYPE_MESSAGE);
1480 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1484 empathy_chat_init (EmpathyChat *chat)
1486 EmpathyChatPriv *priv = GET_PRIV (chat);
1487 GtkTextBuffer *buffer;
1489 chat_create_ui (chat);
1491 priv->is_first_char = TRUE;
1492 priv->log_manager = empathy_log_manager_new ();
1493 priv->default_window_height = -1;
1494 priv->vscroll_visible = FALSE;
1495 priv->sent_messages = NULL;
1496 priv->sent_messages_index = -1;
1497 priv->mc = empathy_mission_control_new ();
1499 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1500 G_CALLBACK (chat_status_changed_cb),
1503 g_signal_connect (chat->input_text_view,
1505 G_CALLBACK (chat_input_key_press_event_cb),
1508 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1509 g_signal_connect (buffer,
1511 G_CALLBACK (chat_input_text_buffer_changed_cb),
1513 g_signal_connect (chat->view,
1515 G_CALLBACK (chat_text_view_focus_in_event_cb),
1518 g_signal_connect (chat->input_text_view,
1520 G_CALLBACK (chat_text_view_size_allocate_cb),
1523 g_signal_connect (chat->input_text_view,
1525 G_CALLBACK (chat_text_view_realize_cb),
1528 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
1530 G_CALLBACK (chat_text_populate_popup_cb),
1533 /* create misspelt words identification tag */
1534 gtk_text_buffer_create_tag (buffer,
1536 "underline", PANGO_UNDERLINE_ERROR,
1541 empathy_chat_new (EmpathyTpChat *tp_chat)
1543 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1547 empathy_chat_get_tp_chat (EmpathyChat *chat)
1549 EmpathyChatPriv *priv = GET_PRIV (chat);
1551 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1553 return priv->tp_chat;
1557 chat_block_events_timeout_cb (gpointer data)
1559 EmpathyChatPriv *priv = GET_PRIV (data);
1561 priv->block_events_timeout_id = 0;
1567 empathy_chat_set_tp_chat (EmpathyChat *chat,
1568 EmpathyTpChat *tp_chat)
1570 EmpathyChatPriv *priv;
1573 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1574 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1576 priv = GET_PRIV (chat);
1578 if (tp_chat == priv->tp_chat) {
1582 if (priv->tp_chat) {
1583 g_object_unref (priv->tp_chat);
1585 if (priv->account) {
1586 g_object_unref (priv->account);
1590 priv->tp_chat = g_object_ref (tp_chat);
1591 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1592 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1593 tp_chan = empathy_tp_chat_get_channel (tp_chat);
1594 priv->handle_type = tp_chan->handle_type;
1595 chat_remote_contact_notify_cb (chat);
1597 g_signal_connect (tp_chat, "message-received",
1598 G_CALLBACK (chat_message_received_cb),
1600 g_signal_connect (tp_chat, "send-error",
1601 G_CALLBACK (chat_send_error_cb),
1603 g_signal_connect (tp_chat, "chat-state-changed",
1604 G_CALLBACK (chat_state_changed_cb),
1606 g_signal_connect (tp_chat, "property-changed",
1607 G_CALLBACK (chat_property_changed_cb),
1609 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1610 G_CALLBACK (chat_remote_contact_notify_cb),
1612 g_signal_connect (tp_chat, "destroy",
1613 G_CALLBACK (chat_destroy_cb),
1616 /* Block events for some time to avoid having "has come online" or
1617 * "joined" messages. */
1618 if (priv->block_events_timeout_id == 0) {
1619 priv->block_events_timeout_id =
1620 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1623 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1624 empathy_chat_view_append_event (chat->view, _("Connected"));
1626 g_object_notify (G_OBJECT (chat), "tp-chat");
1627 g_object_notify (G_OBJECT (chat), "id");
1628 g_object_notify (G_OBJECT (chat), "account");
1632 empathy_chat_get_account (EmpathyChat *chat)
1634 EmpathyChatPriv *priv = GET_PRIV (chat);
1636 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1638 return priv->account;
1642 empathy_chat_get_name (EmpathyChat *chat)
1644 EmpathyChatPriv *priv = GET_PRIV (chat);
1646 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1652 empathy_chat_get_subject (EmpathyChat *chat)
1654 EmpathyChatPriv *priv = GET_PRIV (chat);
1656 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1658 return priv->subject;
1662 empathy_chat_get_tooltip (EmpathyChat *chat)
1664 EmpathyChatPriv *priv = GET_PRIV (chat);
1666 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1668 return priv->tooltip;
1672 empathy_chat_get_selected_contact (EmpathyChat *chat)
1674 EmpathyChatPriv *priv = GET_PRIV (chat);
1676 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1678 return priv->selected_contact;
1682 empathy_chat_get_id (EmpathyChat *chat)
1684 EmpathyChatPriv *priv = GET_PRIV (chat);
1686 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1692 empathy_chat_clear (EmpathyChat *chat)
1694 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1696 empathy_chat_view_clear (chat->view);
1700 empathy_chat_scroll_down (EmpathyChat *chat)
1702 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1704 empathy_chat_view_scroll_down (chat->view);
1708 empathy_chat_cut (EmpathyChat *chat)
1710 GtkTextBuffer *buffer;
1712 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1714 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1715 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1716 GtkClipboard *clipboard;
1718 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1720 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1725 empathy_chat_copy (EmpathyChat *chat)
1727 GtkTextBuffer *buffer;
1729 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1731 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1732 empathy_chat_view_copy_clipboard (chat->view);
1736 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1737 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1738 GtkClipboard *clipboard;
1740 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1742 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1747 empathy_chat_paste (EmpathyChat *chat)
1749 GtkTextBuffer *buffer;
1750 GtkClipboard *clipboard;
1752 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1754 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1755 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1757 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1761 empathy_chat_correct_word (EmpathyChat *chat,
1764 const gchar *new_word)
1766 GtkTextBuffer *buffer;
1768 g_return_if_fail (chat != NULL);
1769 g_return_if_fail (new_word != NULL);
1771 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1773 gtk_text_buffer_delete (buffer, &start, &end);
1774 gtk_text_buffer_insert (buffer, &start,