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-contact-list-store.h"
50 #include "empathy-contact-list-view.h"
51 #include "empathy-ui-utils.h"
53 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
55 #define DEBUG_DOMAIN "Chat"
57 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
58 #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)
60 #define MAX_INPUT_HEIGHT 150
61 #define COMPOSING_STOP_TIMEOUT 5
63 struct _EmpathyChatPriv {
64 EmpathyTpChat *tp_chat;
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;
89 EmpathyContactListView *view;
90 EmpathyContactListStore *store;
92 /* Used to automatically shrink a window that has temporarily
93 * grown due to long input.
96 gint default_window_height;
97 gint last_input_height;
98 gboolean vscroll_visible;
99 gboolean is_first_char;
102 static void empathy_chat_class_init (EmpathyChatClass *klass);
103 static void empathy_chat_init (EmpathyChat *chat);
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->id);
146 g_value_set_string (value, priv->subject);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
155 chat_set_property (GObject *object,
160 EmpathyChat *chat = EMPATHY_CHAT (object);
164 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
173 chat_status_changed_cb (MissionControl *mc,
174 TpConnectionStatus status,
176 TpConnectionStatusReason reason,
177 const gchar *unique_name,
180 EmpathyChatPriv *priv = GET_PRIV (chat);
183 account = mc_account_lookup (unique_name);
185 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
186 empathy_account_equal (account, priv->account) &&
187 priv->handle_type != 0) {
188 empathy_debug (DEBUG_DOMAIN,
189 "Account reconnected, request a new Text channel");
190 mission_control_request_channel_with_string_handle (mc,
192 TP_IFACE_CHANNEL_TYPE_TEXT,
198 g_object_unref (account);
202 chat_composing_remove_timeout (EmpathyChat *chat)
204 EmpathyChatPriv *priv;
206 priv = GET_PRIV (chat);
208 if (priv->composing_stop_timeout_id) {
209 g_source_remove (priv->composing_stop_timeout_id);
210 priv->composing_stop_timeout_id = 0;
215 chat_composing_stop_timeout_cb (EmpathyChat *chat)
217 EmpathyChatPriv *priv;
219 priv = GET_PRIV (chat);
221 priv->composing_stop_timeout_id = 0;
222 empathy_tp_chat_set_state (priv->tp_chat,
223 TP_CHANNEL_CHAT_STATE_PAUSED);
229 chat_composing_start (EmpathyChat *chat)
231 EmpathyChatPriv *priv;
233 priv = GET_PRIV (chat);
235 if (priv->composing_stop_timeout_id) {
236 /* Just restart the timeout */
237 chat_composing_remove_timeout (chat);
239 empathy_tp_chat_set_state (priv->tp_chat,
240 TP_CHANNEL_CHAT_STATE_COMPOSING);
243 priv->composing_stop_timeout_id = g_timeout_add_seconds (
244 COMPOSING_STOP_TIMEOUT,
245 (GSourceFunc) chat_composing_stop_timeout_cb,
250 chat_composing_stop (EmpathyChat *chat)
252 EmpathyChatPriv *priv;
254 priv = GET_PRIV (chat);
256 chat_composing_remove_timeout (chat);
257 empathy_tp_chat_set_state (priv->tp_chat,
258 TP_CHANNEL_CHAT_STATE_ACTIVE);
262 chat_destroy_cb (EmpathyTpChat *tp_chat,
265 EmpathyChatPriv *priv;
267 priv = GET_PRIV (chat);
270 g_object_unref (priv->tp_chat);
271 priv->tp_chat = NULL;
274 empathy_chat_view_append_event (chat->view, _("Disconnected"));
275 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
279 chat_sent_message_add (EmpathyChat *chat,
282 EmpathyChatPriv *priv;
286 priv = GET_PRIV (chat);
288 /* Save the sent message in our repeat buffer */
289 list = priv->sent_messages;
291 /* Remove any other occurances of this msg */
292 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
293 list = g_slist_remove_link (list, item);
295 g_slist_free1 (item);
298 /* Trim the list to the last 10 items */
299 while (g_slist_length (list) > 10) {
300 item = g_slist_last (list);
302 list = g_slist_remove_link (list, item);
304 g_slist_free1 (item);
308 /* Add new message */
309 list = g_slist_prepend (list, g_strdup (str));
311 /* Set list and reset the index */
312 priv->sent_messages = list;
313 priv->sent_messages_index = -1;
317 chat_sent_message_get_next (EmpathyChat *chat)
319 EmpathyChatPriv *priv;
322 priv = GET_PRIV (chat);
324 if (!priv->sent_messages) {
325 empathy_debug (DEBUG_DOMAIN,
326 "No sent messages, next message is NULL");
330 max = g_slist_length (priv->sent_messages) - 1;
332 if (priv->sent_messages_index < max) {
333 priv->sent_messages_index++;
336 empathy_debug (DEBUG_DOMAIN,
337 "Returning next message index:%d",
338 priv->sent_messages_index);
340 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
344 chat_sent_message_get_last (EmpathyChat *chat)
346 EmpathyChatPriv *priv;
348 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
350 priv = GET_PRIV (chat);
352 if (!priv->sent_messages) {
353 empathy_debug (DEBUG_DOMAIN,
354 "No sent messages, last message is NULL");
358 if (priv->sent_messages_index >= 0) {
359 priv->sent_messages_index--;
362 empathy_debug (DEBUG_DOMAIN,
363 "Returning last message index:%d",
364 priv->sent_messages_index);
366 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
370 chat_send (EmpathyChat *chat,
373 EmpathyChatPriv *priv;
374 EmpathyMessage *message;
376 priv = GET_PRIV (chat);
378 if (G_STR_EMPTY (msg)) {
382 chat_sent_message_add (chat, msg);
384 if (g_str_has_prefix (msg, "/clear")) {
385 empathy_chat_view_clear (chat->view);
389 message = empathy_message_new (msg);
391 empathy_tp_chat_send (priv->tp_chat, message);
393 g_object_unref (message);
397 chat_input_text_view_send (EmpathyChat *chat)
399 EmpathyChatPriv *priv;
400 GtkTextBuffer *buffer;
401 GtkTextIter start, end;
404 priv = GET_PRIV (chat);
406 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
408 gtk_text_buffer_get_bounds (buffer, &start, &end);
409 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
411 /* clear the input field */
412 gtk_text_buffer_set_text (buffer, "", -1);
414 chat_send (chat, msg);
417 priv->is_first_char = TRUE;
421 chat_state_changed_cb (EmpathyTpChat *tp_chat,
422 EmpathyContact *contact,
423 TpChannelChatState state,
426 EmpathyChatPriv *priv;
428 gboolean was_composing;
430 priv = GET_PRIV (chat);
432 if (empathy_contact_is_user (contact)) {
433 /* We don't care about our own chat state */
437 was_composing = (priv->compositors != NULL);
439 /* Find the contact in the list. After that l is the list elem or NULL */
440 for (l = priv->compositors; l; l = l->next) {
441 if (contact == l->data) {
447 case TP_CHANNEL_CHAT_STATE_GONE:
448 case TP_CHANNEL_CHAT_STATE_INACTIVE:
449 case TP_CHANNEL_CHAT_STATE_PAUSED:
450 case TP_CHANNEL_CHAT_STATE_ACTIVE:
451 /* Contact is not composing */
453 priv->compositors = g_list_remove_link (priv->compositors, l);
454 g_object_unref (l->data);
458 case TP_CHANNEL_CHAT_STATE_COMPOSING:
459 /* Contact is composing */
461 priv->compositors = g_list_prepend (priv->compositors,
462 g_object_ref (contact));
466 g_assert_not_reached ();
469 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
470 was_composing ? "yes" : "no",
471 priv->compositors ? "yes" : "no");
473 if ((was_composing && !priv->compositors) ||
474 (!was_composing && priv->compositors)) {
475 /* Composing state changed */
476 g_signal_emit (chat, signals[COMPOSING], 0,
477 priv->compositors != NULL);
482 chat_message_received_cb (EmpathyTpChat *tp_chat,
483 EmpathyMessage *message,
486 EmpathyChatPriv *priv;
487 EmpathyContact *sender;
490 priv = GET_PRIV (chat);
492 sender = empathy_message_get_sender (message);
493 body = empathy_message_get_body (message);
494 while (priv->backlog_messages) {
495 EmpathyMessage *log_message;
496 EmpathyContact *log_sender;
497 const gchar *log_body;
499 log_message = priv->backlog_messages->data;
500 log_sender = empathy_message_get_sender (log_message);
501 log_body = empathy_message_get_body (log_message);
503 priv->backlog_messages = g_list_remove (priv->backlog_messages,
506 if (empathy_contact_equal (sender, log_sender) &&
507 !tp_strdiff (body, log_body)) {
508 /* The message we received is already displayed because
509 * some jabber chatrooms sends us back logs and we
510 * already displayed it from localy logged messages. */
511 empathy_debug (DEBUG_DOMAIN, "Skipping message because "
512 "it is already displayed from logged "
514 g_object_unref (log_message);
517 g_object_unref (log_message);
520 empathy_debug (DEBUG_DOMAIN, "Appending new message from %s (%d)",
521 empathy_contact_get_name (sender),
522 empathy_contact_get_handle (sender));
524 empathy_log_manager_add_message (priv->log_manager,
525 empathy_chat_get_id (chat),
529 empathy_chat_view_append_message (chat->view, message);
531 /* We received a message so the contact is no more composing */
532 chat_state_changed_cb (tp_chat, sender,
533 TP_CHANNEL_CHAT_STATE_ACTIVE,
536 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
540 chat_send_error_cb (EmpathyTpChat *tp_chat,
541 EmpathyMessage *message,
542 TpChannelTextSendError error_code,
548 switch (error_code) {
549 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
550 error = _("offline");
552 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
553 error = _("invalid contact");
555 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
556 error = _("permission denied");
558 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
559 error = _("too long message");
561 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
562 error = _("not implemented");
565 error = _("unknown");
569 str = g_strdup_printf (_("Error sending message '%s': %s"),
570 empathy_message_get_body (message),
572 empathy_chat_view_append_event (chat->view, str);
577 chat_property_changed_cb (EmpathyTpChat *tp_chat,
582 EmpathyChatPriv *priv = GET_PRIV (chat);
584 if (!tp_strdiff (name, "subject")) {
585 g_free (priv->subject);
586 priv->subject = g_value_dup_string (value);
587 g_object_notify (G_OBJECT (chat), "subject");
589 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
590 if (priv->block_events_timeout_id == 0) {
593 if (!G_STR_EMPTY (priv->subject)) {
594 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
596 str = g_strdup (_("No topic defined"));
598 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
602 else if (!tp_strdiff (name, "name")) {
604 priv->name = g_value_dup_string (value);
605 g_object_notify (G_OBJECT (chat), "name");
610 chat_get_is_command (const gchar *str)
612 g_return_val_if_fail (str != NULL, FALSE);
618 if (g_str_has_prefix (str, "/me")) {
621 else if (g_str_has_prefix (str, "/nick")) {
624 else if (g_str_has_prefix (str, "/topic")) {
632 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
635 EmpathyChatPriv *priv;
636 GtkTextIter start, end;
638 gboolean spell_checker = FALSE;
640 priv = GET_PRIV (chat);
642 if (gtk_text_buffer_get_char_count (buffer) == 0) {
643 chat_composing_stop (chat);
645 chat_composing_start (chat);
648 empathy_conf_get_bool (empathy_conf_get (),
649 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
652 if (priv->is_first_char) {
656 GtkAllocation *allocation;
658 /* Save the window's size */
659 dialog = empathy_get_toplevel_window (GTK_WIDGET (chat));
661 gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
662 gtk_widget_size_request (chat->input_text_view, &req);
663 allocation = >K_WIDGET (chat->view)->allocation;
665 priv->default_window_height = window_height;
666 priv->last_input_height = req.height;
667 priv->padding_height = window_height - req.height - allocation->height;
670 priv->is_first_char = FALSE;
673 gtk_text_buffer_get_start_iter (buffer, &start);
675 if (!spell_checker) {
676 gtk_text_buffer_get_end_iter (buffer, &end);
677 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
681 if (!empathy_spell_supported ()) {
685 /* NOTE: this is really inefficient, we shouldn't have to
686 reiterate the whole buffer each time and check each work
689 gboolean correct = FALSE;
692 if (gtk_text_iter_is_start (&start)) {
695 if (!gtk_text_iter_forward_word_end (&end)) {
696 /* no whole word yet */
700 if (!gtk_text_iter_forward_word_end (&end)) {
701 /* must be the end of the buffer */
706 gtk_text_iter_backward_word_start (&start);
709 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
711 /* spell check string */
712 if (!chat_get_is_command (str)) {
713 correct = empathy_spell_check (str);
719 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
721 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
726 /* set start iter to the end iters position */
732 chat_input_key_press_event_cb (GtkWidget *widget,
736 EmpathyChatPriv *priv;
739 GtkWidget *text_view_sw;
741 priv = GET_PRIV (chat);
743 /* Catch ctrl+up/down so we can traverse messages we sent */
744 if ((event->state & GDK_CONTROL_MASK) &&
745 (event->keyval == GDK_Up ||
746 event->keyval == GDK_Down)) {
747 GtkTextBuffer *buffer;
750 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
752 if (event->keyval == GDK_Up) {
753 str = chat_sent_message_get_next (chat);
755 str = chat_sent_message_get_last (chat);
758 g_signal_handlers_block_by_func (buffer,
759 chat_input_text_buffer_changed_cb,
761 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
762 g_signal_handlers_unblock_by_func (buffer,
763 chat_input_text_buffer_changed_cb,
769 /* Catch enter but not ctrl/shift-enter */
770 if (IS_ENTER (event->keyval) &&
771 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
774 /* This is to make sure that kinput2 gets the enter. And if
775 * it's handled there we shouldn't send on it. This is because
776 * kinput2 uses Enter to commit letters. See:
777 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
780 view = GTK_TEXT_VIEW (chat->input_text_view);
781 if (gtk_im_context_filter_keypress (view->im_context, event)) {
782 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
786 chat_input_text_view_send (chat);
790 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
792 if (IS_ENTER (event->keyval) &&
793 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
794 /* Newline for shift/control-enter. */
797 if (!(event->state & GDK_CONTROL_MASK) &&
798 event->keyval == GDK_Page_Up) {
799 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
800 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
803 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
804 event->keyval == GDK_Page_Down) {
805 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
806 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
807 gtk_adjustment_set_value (adj, val);
810 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
811 event->keyval == GDK_Tab) {
812 GtkTextBuffer *buffer;
813 GtkTextIter start, current;
814 gchar *nick, *completed;
815 GList *list, *completed_list;
816 gboolean is_start_of_buffer;
818 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
819 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
821 /* Get the start of the nick to complete. */
822 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
823 gtk_text_iter_backward_word_start (&start);
824 is_start_of_buffer = gtk_text_iter_is_start (&start);
826 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
827 g_completion_add_items (priv->completion, list);
829 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
830 completed_list = g_completion_complete (priv->completion,
839 gchar *complete_char = NULL;
841 gtk_text_buffer_delete (buffer, &start, ¤t);
843 len = g_list_length (completed_list);
846 /* If we only have one hit, use that text
847 * instead of the text in completed since the
848 * completed text will use the typed string
849 * which might be cased all wrong.
852 text = empathy_contact_get_name (completed_list->data);
857 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
859 if (len == 1 && is_start_of_buffer &&
860 empathy_conf_get_string (empathy_conf_get (),
861 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
863 complete_char != NULL) {
864 gtk_text_buffer_insert_at_cursor (buffer,
866 strlen (complete_char));
867 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
868 g_free (complete_char);
874 g_completion_clear_items (priv->completion);
876 g_list_foreach (list, (GFunc) g_object_unref, NULL);
886 chat_text_view_focus_in_event_cb (GtkWidget *widget,
890 gtk_widget_grab_focus (chat->input_text_view);
902 chat_change_size_in_idle_cb (ChangeSizeData *data)
904 gtk_window_resize (data->window, data->width, data->height);
910 chat_text_view_scroll_hide_cb (GtkWidget *widget,
913 EmpathyChatPriv *priv;
916 priv = GET_PRIV (chat);
918 priv->vscroll_visible = FALSE;
919 g_signal_handlers_disconnect_by_func (widget,
920 chat_text_view_scroll_hide_cb,
923 sw = gtk_widget_get_parent (chat->input_text_view);
924 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
927 g_object_set (sw, "height-request", -1, NULL);
931 chat_text_view_size_allocate_cb (GtkWidget *widget,
932 GtkAllocation *allocation,
935 EmpathyChatPriv *priv;
938 ChangeSizeData *data;
941 GtkAllocation *view_allocation;
946 priv = GET_PRIV (chat);
948 if (priv->default_window_height <= 0) {
952 sw = gtk_widget_get_parent (widget);
953 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
956 priv->vscroll_visible = TRUE;
957 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
958 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
960 GTK_POLICY_AUTOMATIC);
961 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
962 g_signal_connect (vscroll, "hide",
963 G_CALLBACK (chat_text_view_scroll_hide_cb),
967 if (priv->last_input_height <= allocation->height) {
968 priv->last_input_height = allocation->height;
972 diff = priv->last_input_height - allocation->height;
973 priv->last_input_height = allocation->height;
975 view_allocation = >K_WIDGET (chat->view)->allocation;
977 dialog = empathy_get_toplevel_window (GTK_WIDGET (widget));
978 gtk_window_get_size (dialog, NULL, ¤t_height);
980 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
982 if (new_height <= priv->default_window_height) {
983 window_height = priv->default_window_height;
985 window_height = new_height;
988 if (current_height <= window_height) {
992 /* Restore the window's size */
993 gtk_window_get_size (dialog, &width, NULL);
995 data = g_new0 (ChangeSizeData, 1);
996 data->window = dialog;
998 data->height = window_height;
1000 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1001 (GSourceFunc) chat_change_size_in_idle_cb,
1006 chat_text_view_realize_cb (GtkWidget *widget,
1009 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
1010 gtk_widget_grab_focus (widget);
1014 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1017 GtkTextBuffer *buffer;
1019 const gchar *smiley;
1021 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1023 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1025 gtk_text_buffer_get_end_iter (buffer, &iter);
1026 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1028 gtk_text_buffer_get_end_iter (buffer, &iter);
1029 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1040 static EmpathyChatSpell *
1041 chat_spell_new (EmpathyChat *chat,
1046 EmpathyChatSpell *chat_spell;
1048 chat_spell = g_slice_new0 (EmpathyChatSpell);
1050 chat_spell->chat = g_object_ref (chat);
1051 chat_spell->word = g_strdup (word);
1052 chat_spell->start = start;
1053 chat_spell->end = end;
1059 chat_spell_free (EmpathyChatSpell *chat_spell)
1061 g_object_unref (chat_spell->chat);
1062 g_free (chat_spell->word);
1063 g_slice_free (EmpathyChatSpell, chat_spell);
1067 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1068 EmpathyChatSpell *chat_spell)
1070 empathy_spell_dialog_show (chat_spell->chat,
1077 chat_text_populate_popup_cb (GtkTextView *view,
1081 EmpathyChatPriv *priv;
1082 GtkTextBuffer *buffer;
1083 GtkTextTagTable *table;
1086 GtkTextIter iter, start, end;
1089 EmpathyChatSpell *chat_spell;
1090 GtkWidget *smiley_menu;
1092 priv = GET_PRIV (chat);
1094 /* Add the emoticon menu. */
1095 item = gtk_separator_menu_item_new ();
1096 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1097 gtk_widget_show (item);
1099 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
1100 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1101 gtk_widget_show (item);
1103 smiley_menu = empathy_chat_view_get_smiley_menu (
1104 G_CALLBACK (chat_insert_smiley_activate_cb),
1106 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1108 /* Add the spell check menu item. */
1109 buffer = gtk_text_view_get_buffer (view);
1110 table = gtk_text_buffer_get_tag_table (buffer);
1112 tag = gtk_text_tag_table_lookup (table, "misspelled");
1114 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1116 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1117 GTK_TEXT_WINDOW_WIDGET,
1121 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1125 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1126 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1128 str = gtk_text_buffer_get_text (buffer,
1129 &start, &end, FALSE);
1132 if (G_STR_EMPTY (str)) {
1136 chat_spell = chat_spell_new (chat, str, start, end);
1138 g_object_set_data_full (G_OBJECT (menu),
1139 "chat_spell", chat_spell,
1140 (GDestroyNotify) chat_spell_free);
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 (_("_Check Word Spelling..."));
1147 g_signal_connect (item,
1149 G_CALLBACK (chat_text_check_word_spelling_cb),
1151 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1152 gtk_widget_show (item);
1156 chat_add_logs (EmpathyChat *chat)
1158 EmpathyChatPriv *priv;
1159 GList *messages, *l;
1163 priv = GET_PRIV (chat);
1165 /* Turn off scrolling temporarily */
1166 empathy_chat_view_scroll (chat->view, FALSE);
1168 /* Add messages from last conversation */
1169 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1171 empathy_chat_get_id (chat),
1173 num_messages = g_list_length (messages);
1175 /* Only keep the 10 last messages */
1176 for (i = 0; num_messages - i > 10; i++) {
1177 EmpathyMessage *message;
1179 message = messages->data;
1180 messages = g_list_remove (messages, message);
1181 g_object_unref (message);
1184 for (l = messages; l; l = l->next) {
1185 empathy_chat_view_append_message (chat->view, l->data);
1187 priv->backlog_messages = messages;
1189 /* Turn back on scrolling */
1190 empathy_chat_view_scroll (chat->view, TRUE);
1194 chat_contacts_completion_func (const gchar *s1,
1198 gchar *tmp, *nick1, *nick2;
1201 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1202 nick1 = g_utf8_casefold (tmp, -1);
1205 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1206 nick2 = g_utf8_casefold (tmp, -1);
1209 ret = strncmp (nick1, nick2, n);
1218 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1219 EmpathyContact *contact,
1220 EmpathyContact *actor,
1226 EmpathyChatPriv *priv = GET_PRIV (chat);
1228 if (priv->block_events_timeout_id == 0) {
1231 empathy_contact_run_until_ready (contact,
1232 EMPATHY_CONTACT_READY_NAME,
1236 str = g_strdup_printf (_("%s has joined the room"),
1237 empathy_contact_get_name (contact));
1239 str = g_strdup_printf (_("%s has left the room"),
1240 empathy_contact_get_name (contact));
1242 empathy_chat_view_append_event (chat->view, str);
1248 chat_create_ui (EmpathyChat *chat)
1250 EmpathyChatPriv *priv = GET_PRIV (chat);
1254 GtkTextBuffer *buffer;
1256 filename = empathy_file_lookup ("empathy-chat.glade",
1258 glade = empathy_glade_get_file (filename,
1261 "chat_widget", &priv->widget,
1262 "hpaned", &priv->hpaned,
1263 "vbox_left", &priv->vbox_left,
1264 "scrolled_window_chat", &priv->scrolled_window_chat,
1265 "scrolled_window_input", &priv->scrolled_window_input,
1266 "hbox_topic", &priv->hbox_topic,
1267 "label_topic", &priv->label_topic,
1268 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1271 g_object_unref (glade);
1273 /* Add message GtkTextView. */
1274 chat->view = empathy_chat_view_new ();
1275 g_signal_connect (chat->view, "focus_in_event",
1276 G_CALLBACK (chat_text_view_focus_in_event_cb),
1278 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1279 GTK_WIDGET (chat->view));
1280 gtk_widget_show (GTK_WIDGET (chat->view));
1282 /* Add input GtkTextView */
1283 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1284 "pixels-above-lines", 2,
1285 "pixels-below-lines", 2,
1286 "pixels-inside-wrap", 1,
1289 "wrap-mode", GTK_WRAP_WORD_CHAR,
1291 g_signal_connect (chat->input_text_view, "key_press_event",
1292 G_CALLBACK (chat_input_key_press_event_cb),
1294 g_signal_connect (chat->input_text_view, "size_allocate",
1295 G_CALLBACK (chat_text_view_size_allocate_cb),
1297 g_signal_connect (chat->input_text_view, "realize",
1298 G_CALLBACK (chat_text_view_realize_cb),
1300 g_signal_connect (chat->input_text_view, "populate_popup",
1301 G_CALLBACK (chat_text_populate_popup_cb),
1303 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1304 g_signal_connect (buffer, "changed",
1305 G_CALLBACK (chat_input_text_buffer_changed_cb),
1307 gtk_text_buffer_create_tag (buffer, "misspelled",
1308 "underline", PANGO_UNDERLINE_ERROR,
1310 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1311 chat->input_text_view);
1312 gtk_widget_show (chat->input_text_view);
1314 /* Create contact list */
1315 priv->store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1316 priv->view = empathy_contact_list_view_new (priv->store,
1317 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT |
1318 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL |
1319 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG |
1320 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT |
1321 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE |
1322 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO);
1323 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1324 GTK_WIDGET (priv->view));
1325 gtk_widget_show (GTK_WIDGET (priv->view));
1327 /* Set widget focus order */
1328 list = g_list_append (NULL, priv->scrolled_window_input);
1329 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1332 list = g_list_append (NULL, priv->vbox_left);
1333 list = g_list_append (list, priv->scrolled_window_contacts);
1334 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1337 list = g_list_append (NULL, priv->hpaned);
1338 list = g_list_append (list, priv->hbox_topic);
1339 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1342 /* Add the main widget in the chat widget */
1343 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1347 chat_size_request (GtkWidget *widget,
1348 GtkRequisition *requisition)
1350 GtkBin *bin = GTK_BIN (widget);
1352 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1353 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1355 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1357 GtkRequisition child_requisition;
1359 gtk_widget_size_request (bin->child, &child_requisition);
1361 requisition->width += child_requisition.width;
1362 requisition->height += child_requisition.height;
1367 chat_size_allocate (GtkWidget *widget,
1368 GtkAllocation *allocation)
1370 GtkBin *bin = GTK_BIN (widget);
1371 GtkAllocation child_allocation;
1373 widget->allocation = *allocation;
1375 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1377 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1378 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1379 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1380 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1382 gtk_widget_size_allocate (bin->child, &child_allocation);
1387 chat_finalize (GObject *object)
1390 EmpathyChatPriv *priv;
1392 chat = EMPATHY_CHAT (object);
1393 priv = GET_PRIV (chat);
1395 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1397 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1398 g_slist_free (priv->sent_messages);
1400 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1401 g_list_free (priv->compositors);
1403 g_list_foreach (priv->backlog_messages, (GFunc) g_object_unref, NULL);
1404 g_list_free (priv->backlog_messages);
1406 chat_composing_remove_timeout (chat);
1408 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1409 G_CALLBACK (chat_status_changed_cb),
1411 g_object_unref (priv->mc);
1412 g_object_unref (priv->log_manager);
1413 g_object_unref (priv->store);
1415 if (priv->tp_chat) {
1416 g_object_unref (priv->tp_chat);
1419 if (priv->account) {
1420 g_object_unref (priv->account);
1423 if (priv->block_events_timeout_id) {
1424 g_source_remove (priv->block_events_timeout_id);
1428 g_free (priv->name);
1429 g_free (priv->subject);
1431 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1435 chat_constructed (GObject *object)
1437 EmpathyChat *chat = EMPATHY_CHAT (object);
1439 chat_create_ui (chat);
1440 chat_add_logs (chat);
1444 empathy_chat_class_init (EmpathyChatClass *klass)
1446 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1447 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1449 object_class->finalize = chat_finalize;
1450 object_class->get_property = chat_get_property;
1451 object_class->set_property = chat_set_property;
1452 object_class->constructed = chat_constructed;
1454 widget_class->size_request = chat_size_request;
1455 widget_class->size_allocate = chat_size_allocate;
1457 g_object_class_install_property (object_class,
1459 g_param_spec_object ("tp-chat",
1461 "The tp chat object",
1462 EMPATHY_TYPE_TP_CHAT,
1464 G_PARAM_READWRITE));
1465 g_object_class_install_property (object_class,
1467 g_param_spec_object ("account",
1468 "Account of the chat",
1469 "The account of the chat",
1472 g_object_class_install_property (object_class,
1474 g_param_spec_string ("id",
1476 "The id of the chat",
1479 g_object_class_install_property (object_class,
1481 g_param_spec_string ("name",
1483 "The name of the chat",
1486 g_object_class_install_property (object_class,
1488 g_param_spec_string ("subject",
1490 "The subject or topic of the chat",
1494 signals[COMPOSING] =
1495 g_signal_new ("composing",
1496 G_OBJECT_CLASS_TYPE (object_class),
1500 g_cclosure_marshal_VOID__BOOLEAN,
1504 signals[NEW_MESSAGE] =
1505 g_signal_new ("new-message",
1506 G_OBJECT_CLASS_TYPE (object_class),
1510 g_cclosure_marshal_VOID__OBJECT,
1512 1, EMPATHY_TYPE_MESSAGE);
1514 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1518 chat_block_events_timeout_cb (gpointer data)
1520 EmpathyChatPriv *priv = GET_PRIV (data);
1522 priv->block_events_timeout_id = 0;
1528 empathy_chat_init (EmpathyChat *chat)
1530 EmpathyChatPriv *priv = GET_PRIV (chat);
1532 priv->is_first_char = TRUE;
1533 priv->log_manager = empathy_log_manager_new ();
1534 priv->default_window_height = -1;
1535 priv->vscroll_visible = FALSE;
1536 priv->sent_messages = NULL;
1537 priv->sent_messages_index = -1;
1538 priv->mc = empathy_mission_control_new ();
1540 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1541 G_CALLBACK (chat_status_changed_cb),
1544 /* Block events for some time to avoid having "has come online" or
1545 * "joined" messages. */
1546 priv->block_events_timeout_id =
1547 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1549 /* Add nick name completion */
1550 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1551 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1555 empathy_chat_new (EmpathyTpChat *tp_chat)
1557 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1561 empathy_chat_get_tp_chat (EmpathyChat *chat)
1563 EmpathyChatPriv *priv = GET_PRIV (chat);
1565 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1567 return priv->tp_chat;
1571 empathy_chat_set_tp_chat (EmpathyChat *chat,
1572 EmpathyTpChat *tp_chat)
1574 EmpathyChatPriv *priv;
1576 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1577 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1578 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1580 priv = GET_PRIV (chat);
1582 if (tp_chat == priv->tp_chat) {
1586 if (priv->tp_chat) {
1587 g_object_unref (priv->tp_chat);
1589 if (priv->account) {
1590 g_object_unref (priv->account);
1594 priv->tp_chat = g_object_ref (tp_chat);
1595 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1596 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1598 g_signal_connect (tp_chat, "message-received",
1599 G_CALLBACK (chat_message_received_cb),
1601 g_signal_connect (tp_chat, "send-error",
1602 G_CALLBACK (chat_send_error_cb),
1604 g_signal_connect (tp_chat, "chat-state-changed",
1605 G_CALLBACK (chat_state_changed_cb),
1607 g_signal_connect (tp_chat, "property-changed",
1608 G_CALLBACK (chat_property_changed_cb),
1610 g_signal_connect (tp_chat, "members-changed",
1611 G_CALLBACK (chat_members_changed_cb),
1613 g_signal_connect (tp_chat, "destroy",
1614 G_CALLBACK (chat_destroy_cb),
1617 if (chat->input_text_view) {
1618 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1619 if (priv->block_events_timeout_id == 0) {
1620 empathy_chat_view_append_event (chat->view, _("Connected"));
1624 g_object_notify (G_OBJECT (chat), "tp-chat");
1625 g_object_notify (G_OBJECT (chat), "id");
1626 g_object_notify (G_OBJECT (chat), "account");
1630 empathy_chat_get_account (EmpathyChat *chat)
1632 EmpathyChatPriv *priv = GET_PRIV (chat);
1634 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1636 return priv->account;
1640 empathy_chat_get_id (EmpathyChat *chat)
1642 EmpathyChatPriv *priv = GET_PRIV (chat);
1644 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1650 empathy_chat_get_name (EmpathyChat *chat)
1652 EmpathyChatPriv *priv = GET_PRIV (chat);
1654 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1660 empathy_chat_get_subject (EmpathyChat *chat)
1662 EmpathyChatPriv *priv = GET_PRIV (chat);
1664 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1666 return priv->subject;
1670 empathy_chat_clear (EmpathyChat *chat)
1672 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1674 empathy_chat_view_clear (chat->view);
1678 empathy_chat_scroll_down (EmpathyChat *chat)
1680 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1682 empathy_chat_view_scroll_down (chat->view);
1686 empathy_chat_cut (EmpathyChat *chat)
1688 GtkTextBuffer *buffer;
1690 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1692 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1693 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1694 GtkClipboard *clipboard;
1696 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1698 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1703 empathy_chat_copy (EmpathyChat *chat)
1705 GtkTextBuffer *buffer;
1707 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1709 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1710 empathy_chat_view_copy_clipboard (chat->view);
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_copy_clipboard (buffer, clipboard);
1725 empathy_chat_paste (EmpathyChat *chat)
1727 GtkTextBuffer *buffer;
1728 GtkClipboard *clipboard;
1730 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1732 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1733 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1735 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1739 empathy_chat_correct_word (EmpathyChat *chat,
1742 const gchar *new_word)
1744 GtkTextBuffer *buffer;
1746 g_return_if_fail (chat != NULL);
1747 g_return_if_fail (new_word != NULL);
1749 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1751 gtk_text_buffer_delete (buffer, &start, &end);
1752 gtk_text_buffer_insert (buffer, &start,