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;
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);
118 static guint signals[LAST_SIGNAL] = { 0 };
120 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
123 chat_get_property (GObject *object,
128 EmpathyChatPriv *priv = GET_PRIV (object);
132 g_value_set_object (value, priv->tp_chat);
135 g_value_set_object (value, priv->account);
138 g_value_set_string (value, priv->name);
141 g_value_set_string (value, priv->id);
144 g_value_set_string (value, priv->subject);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153 chat_set_property (GObject *object,
158 EmpathyChat *chat = EMPATHY_CHAT (object);
162 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
171 chat_status_changed_cb (MissionControl *mc,
172 TpConnectionStatus status,
174 TpConnectionStatusReason reason,
175 const gchar *unique_name,
178 EmpathyChatPriv *priv = GET_PRIV (chat);
181 account = mc_account_lookup (unique_name);
183 if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
184 empathy_account_equal (account, priv->account) &&
185 priv->handle_type != 0) {
186 empathy_debug (DEBUG_DOMAIN,
187 "Account reconnected, request a new Text channel");
188 mission_control_request_channel_with_string_handle (mc,
190 TP_IFACE_CHANNEL_TYPE_TEXT,
196 g_object_unref (account);
200 chat_composing_remove_timeout (EmpathyChat *chat)
202 EmpathyChatPriv *priv;
204 priv = GET_PRIV (chat);
206 if (priv->composing_stop_timeout_id) {
207 g_source_remove (priv->composing_stop_timeout_id);
208 priv->composing_stop_timeout_id = 0;
213 chat_composing_stop_timeout_cb (EmpathyChat *chat)
215 EmpathyChatPriv *priv;
217 priv = GET_PRIV (chat);
219 priv->composing_stop_timeout_id = 0;
220 empathy_tp_chat_set_state (priv->tp_chat,
221 TP_CHANNEL_CHAT_STATE_PAUSED);
227 chat_composing_start (EmpathyChat *chat)
229 EmpathyChatPriv *priv;
231 priv = GET_PRIV (chat);
233 if (priv->composing_stop_timeout_id) {
234 /* Just restart the timeout */
235 chat_composing_remove_timeout (chat);
237 empathy_tp_chat_set_state (priv->tp_chat,
238 TP_CHANNEL_CHAT_STATE_COMPOSING);
241 priv->composing_stop_timeout_id = g_timeout_add_seconds (
242 COMPOSING_STOP_TIMEOUT,
243 (GSourceFunc) chat_composing_stop_timeout_cb,
248 chat_composing_stop (EmpathyChat *chat)
250 EmpathyChatPriv *priv;
252 priv = GET_PRIV (chat);
254 chat_composing_remove_timeout (chat);
255 empathy_tp_chat_set_state (priv->tp_chat,
256 TP_CHANNEL_CHAT_STATE_ACTIVE);
260 chat_destroy_cb (EmpathyTpChat *tp_chat,
263 EmpathyChatPriv *priv;
265 priv = GET_PRIV (chat);
268 g_object_unref (priv->tp_chat);
269 priv->tp_chat = NULL;
272 empathy_chat_view_append_event (chat->view, _("Disconnected"));
273 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
277 chat_sent_message_add (EmpathyChat *chat,
280 EmpathyChatPriv *priv;
284 priv = GET_PRIV (chat);
286 /* Save the sent message in our repeat buffer */
287 list = priv->sent_messages;
289 /* Remove any other occurances of this msg */
290 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
291 list = g_slist_remove_link (list, item);
293 g_slist_free1 (item);
296 /* Trim the list to the last 10 items */
297 while (g_slist_length (list) > 10) {
298 item = g_slist_last (list);
300 list = g_slist_remove_link (list, item);
302 g_slist_free1 (item);
306 /* Add new message */
307 list = g_slist_prepend (list, g_strdup (str));
309 /* Set list and reset the index */
310 priv->sent_messages = list;
311 priv->sent_messages_index = -1;
315 chat_sent_message_get_next (EmpathyChat *chat)
317 EmpathyChatPriv *priv;
320 priv = GET_PRIV (chat);
322 if (!priv->sent_messages) {
323 empathy_debug (DEBUG_DOMAIN,
324 "No sent messages, next message is NULL");
328 max = g_slist_length (priv->sent_messages) - 1;
330 if (priv->sent_messages_index < max) {
331 priv->sent_messages_index++;
334 empathy_debug (DEBUG_DOMAIN,
335 "Returning next message index:%d",
336 priv->sent_messages_index);
338 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
342 chat_sent_message_get_last (EmpathyChat *chat)
344 EmpathyChatPriv *priv;
346 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
348 priv = GET_PRIV (chat);
350 if (!priv->sent_messages) {
351 empathy_debug (DEBUG_DOMAIN,
352 "No sent messages, last message is NULL");
356 if (priv->sent_messages_index >= 0) {
357 priv->sent_messages_index--;
360 empathy_debug (DEBUG_DOMAIN,
361 "Returning last message index:%d",
362 priv->sent_messages_index);
364 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
368 chat_send (EmpathyChat *chat,
371 EmpathyChatPriv *priv;
372 EmpathyMessage *message;
374 priv = GET_PRIV (chat);
376 if (G_STR_EMPTY (msg)) {
380 chat_sent_message_add (chat, msg);
382 if (g_str_has_prefix (msg, "/clear")) {
383 empathy_chat_view_clear (chat->view);
387 message = empathy_message_new (msg);
389 empathy_tp_chat_send (priv->tp_chat, message);
391 g_object_unref (message);
395 chat_input_text_view_send (EmpathyChat *chat)
397 EmpathyChatPriv *priv;
398 GtkTextBuffer *buffer;
399 GtkTextIter start, end;
402 priv = GET_PRIV (chat);
404 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
406 gtk_text_buffer_get_bounds (buffer, &start, &end);
407 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
409 /* clear the input field */
410 gtk_text_buffer_set_text (buffer, "", -1);
412 chat_send (chat, msg);
415 priv->is_first_char = TRUE;
419 chat_state_changed_cb (EmpathyTpChat *tp_chat,
420 EmpathyContact *contact,
421 TpChannelChatState state,
424 EmpathyChatPriv *priv;
426 gboolean was_composing;
428 priv = GET_PRIV (chat);
430 if (empathy_contact_is_user (contact)) {
431 /* We don't care about our own chat state */
435 was_composing = (priv->compositors != NULL);
437 /* Find the contact in the list. After that l is the list elem or NULL */
438 for (l = priv->compositors; l; l = l->next) {
439 if (contact == l->data) {
445 case TP_CHANNEL_CHAT_STATE_GONE:
446 case TP_CHANNEL_CHAT_STATE_INACTIVE:
447 case TP_CHANNEL_CHAT_STATE_PAUSED:
448 case TP_CHANNEL_CHAT_STATE_ACTIVE:
449 /* Contact is not composing */
451 priv->compositors = g_list_remove_link (priv->compositors, l);
452 g_object_unref (l->data);
456 case TP_CHANNEL_CHAT_STATE_COMPOSING:
457 /* Contact is composing */
459 priv->compositors = g_list_prepend (priv->compositors,
460 g_object_ref (contact));
464 g_assert_not_reached ();
467 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
468 was_composing ? "yes" : "no",
469 priv->compositors ? "yes" : "no");
471 if ((was_composing && !priv->compositors) ||
472 (!was_composing && priv->compositors)) {
473 /* Composing state changed */
474 g_signal_emit (chat, signals[COMPOSING], 0,
475 priv->compositors != NULL);
480 chat_message_received_cb (EmpathyTpChat *tp_chat,
481 EmpathyMessage *message,
484 EmpathyChatPriv *priv;
485 EmpathyContact *sender;
488 priv = GET_PRIV (chat);
490 sender = empathy_message_get_sender (message);
491 body = empathy_message_get_body (message);
492 while (priv->backlog_messages) {
493 EmpathyMessage *log_message;
494 EmpathyContact *log_sender;
495 const gchar *log_body;
497 log_message = priv->backlog_messages->data;
498 log_sender = empathy_message_get_sender (log_message);
499 log_body = empathy_message_get_body (log_message);
501 priv->backlog_messages = g_list_remove (priv->backlog_messages,
504 if (empathy_contact_equal (sender, log_sender) &&
505 !tp_strdiff (body, log_body)) {
506 /* The message we received is already displayed because
507 * some jabber chatrooms sends us back logs and we
508 * already displayed it from localy logged messages. */
509 empathy_debug (DEBUG_DOMAIN, "Skipping message because "
510 "it is already displayed from logged "
512 g_object_unref (log_message);
515 g_object_unref (log_message);
518 empathy_debug (DEBUG_DOMAIN, "Appending new message from %s (%d)",
519 empathy_contact_get_name (sender),
520 empathy_contact_get_handle (sender));
522 empathy_log_manager_add_message (priv->log_manager,
523 empathy_chat_get_id (chat),
527 empathy_chat_view_append_message (chat->view, message);
529 /* We received a message so the contact is no more composing */
530 chat_state_changed_cb (tp_chat, sender,
531 TP_CHANNEL_CHAT_STATE_ACTIVE,
534 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
538 chat_send_error_cb (EmpathyTpChat *tp_chat,
539 EmpathyMessage *message,
540 TpChannelTextSendError error_code,
546 switch (error_code) {
547 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
548 error = _("offline");
550 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
551 error = _("invalid contact");
553 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
554 error = _("permission denied");
556 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
557 error = _("too long message");
559 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
560 error = _("not implemented");
563 error = _("unknown");
567 str = g_strdup_printf (_("Error sending message '%s': %s"),
568 empathy_message_get_body (message),
570 empathy_chat_view_append_event (chat->view, str);
575 chat_property_changed_cb (EmpathyTpChat *tp_chat,
580 EmpathyChatPriv *priv = GET_PRIV (chat);
582 if (!tp_strdiff (name, "subject")) {
583 g_free (priv->subject);
584 priv->subject = g_value_dup_string (value);
585 g_object_notify (G_OBJECT (chat), "subject");
587 else if (!tp_strdiff (name, "name")) {
589 priv->name = g_value_dup_string (value);
590 g_object_notify (G_OBJECT (chat), "name");
595 chat_get_is_command (const gchar *str)
597 g_return_val_if_fail (str != NULL, FALSE);
603 if (g_str_has_prefix (str, "/me")) {
606 else if (g_str_has_prefix (str, "/nick")) {
609 else if (g_str_has_prefix (str, "/topic")) {
617 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
620 EmpathyChatPriv *priv;
621 GtkTextIter start, end;
623 gboolean spell_checker = FALSE;
625 priv = GET_PRIV (chat);
627 if (gtk_text_buffer_get_char_count (buffer) == 0) {
628 chat_composing_stop (chat);
630 chat_composing_start (chat);
633 empathy_conf_get_bool (empathy_conf_get (),
634 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
637 if (priv->is_first_char) {
641 GtkAllocation *allocation;
643 /* Save the window's size */
644 dialog = empathy_get_toplevel_window (GTK_WIDGET (chat));
646 gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
647 gtk_widget_size_request (chat->input_text_view, &req);
648 allocation = >K_WIDGET (chat->view)->allocation;
650 priv->default_window_height = window_height;
651 priv->last_input_height = req.height;
652 priv->padding_height = window_height - req.height - allocation->height;
655 priv->is_first_char = FALSE;
658 gtk_text_buffer_get_start_iter (buffer, &start);
660 if (!spell_checker) {
661 gtk_text_buffer_get_end_iter (buffer, &end);
662 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
666 if (!empathy_spell_supported ()) {
670 /* NOTE: this is really inefficient, we shouldn't have to
671 reiterate the whole buffer each time and check each work
674 gboolean correct = FALSE;
677 if (gtk_text_iter_is_start (&start)) {
680 if (!gtk_text_iter_forward_word_end (&end)) {
681 /* no whole word yet */
685 if (!gtk_text_iter_forward_word_end (&end)) {
686 /* must be the end of the buffer */
691 gtk_text_iter_backward_word_start (&start);
694 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
696 /* spell check string */
697 if (!chat_get_is_command (str)) {
698 correct = empathy_spell_check (str);
704 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
706 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
711 /* set start iter to the end iters position */
717 chat_input_key_press_event_cb (GtkWidget *widget,
721 EmpathyChatPriv *priv;
724 GtkWidget *text_view_sw;
726 priv = GET_PRIV (chat);
728 /* Catch ctrl+up/down so we can traverse messages we sent */
729 if ((event->state & GDK_CONTROL_MASK) &&
730 (event->keyval == GDK_Up ||
731 event->keyval == GDK_Down)) {
732 GtkTextBuffer *buffer;
735 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
737 if (event->keyval == GDK_Up) {
738 str = chat_sent_message_get_next (chat);
740 str = chat_sent_message_get_last (chat);
743 g_signal_handlers_block_by_func (buffer,
744 chat_input_text_buffer_changed_cb,
746 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
747 g_signal_handlers_unblock_by_func (buffer,
748 chat_input_text_buffer_changed_cb,
754 /* Catch enter but not ctrl/shift-enter */
755 if (IS_ENTER (event->keyval) &&
756 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
759 /* This is to make sure that kinput2 gets the enter. And if
760 * it's handled there we shouldn't send on it. This is because
761 * kinput2 uses Enter to commit letters. See:
762 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
765 view = GTK_TEXT_VIEW (chat->input_text_view);
766 if (gtk_im_context_filter_keypress (view->im_context, event)) {
767 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
771 chat_input_text_view_send (chat);
775 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
777 if (IS_ENTER (event->keyval) &&
778 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
779 /* Newline for shift/control-enter. */
782 if (!(event->state & GDK_CONTROL_MASK) &&
783 event->keyval == GDK_Page_Up) {
784 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
785 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
788 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
789 event->keyval == GDK_Page_Down) {
790 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
791 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
792 gtk_adjustment_set_value (adj, val);
795 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
796 event->keyval == GDK_Tab) {
797 GtkTextBuffer *buffer;
798 GtkTextIter start, current;
799 gchar *nick, *completed;
800 GList *list, *completed_list;
801 gboolean is_start_of_buffer;
803 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
804 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
806 /* Get the start of the nick to complete. */
807 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
808 gtk_text_iter_backward_word_start (&start);
809 is_start_of_buffer = gtk_text_iter_is_start (&start);
811 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
812 g_completion_add_items (priv->completion, list);
814 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
815 completed_list = g_completion_complete (priv->completion,
824 gchar *complete_char = NULL;
826 gtk_text_buffer_delete (buffer, &start, ¤t);
828 len = g_list_length (completed_list);
831 /* If we only have one hit, use that text
832 * instead of the text in completed since the
833 * completed text will use the typed string
834 * which might be cased all wrong.
837 text = empathy_contact_get_name (completed_list->data);
842 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
844 if (len == 1 && is_start_of_buffer &&
845 empathy_conf_get_string (empathy_conf_get (),
846 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
848 complete_char != NULL) {
849 gtk_text_buffer_insert_at_cursor (buffer,
851 strlen (complete_char));
852 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
853 g_free (complete_char);
859 g_completion_clear_items (priv->completion);
861 g_list_foreach (list, (GFunc) g_object_unref, NULL);
871 chat_text_view_focus_in_event_cb (GtkWidget *widget,
875 gtk_widget_grab_focus (chat->input_text_view);
887 chat_change_size_in_idle_cb (ChangeSizeData *data)
889 gtk_window_resize (data->window, data->width, data->height);
895 chat_text_view_scroll_hide_cb (GtkWidget *widget,
898 EmpathyChatPriv *priv;
901 priv = GET_PRIV (chat);
903 priv->vscroll_visible = FALSE;
904 g_signal_handlers_disconnect_by_func (widget,
905 chat_text_view_scroll_hide_cb,
908 sw = gtk_widget_get_parent (chat->input_text_view);
909 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
912 g_object_set (sw, "height-request", -1, NULL);
916 chat_text_view_size_allocate_cb (GtkWidget *widget,
917 GtkAllocation *allocation,
920 EmpathyChatPriv *priv;
923 ChangeSizeData *data;
926 GtkAllocation *view_allocation;
931 priv = GET_PRIV (chat);
933 if (priv->default_window_height <= 0) {
937 sw = gtk_widget_get_parent (widget);
938 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
941 priv->vscroll_visible = TRUE;
942 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
943 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
945 GTK_POLICY_AUTOMATIC);
946 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
947 g_signal_connect (vscroll, "hide",
948 G_CALLBACK (chat_text_view_scroll_hide_cb),
952 if (priv->last_input_height <= allocation->height) {
953 priv->last_input_height = allocation->height;
957 diff = priv->last_input_height - allocation->height;
958 priv->last_input_height = allocation->height;
960 view_allocation = >K_WIDGET (chat->view)->allocation;
962 dialog = empathy_get_toplevel_window (GTK_WIDGET (widget));
963 gtk_window_get_size (dialog, NULL, ¤t_height);
965 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
967 if (new_height <= priv->default_window_height) {
968 window_height = priv->default_window_height;
970 window_height = new_height;
973 if (current_height <= window_height) {
977 /* Restore the window's size */
978 gtk_window_get_size (dialog, &width, NULL);
980 data = g_new0 (ChangeSizeData, 1);
981 data->window = dialog;
983 data->height = window_height;
985 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
986 (GSourceFunc) chat_change_size_in_idle_cb,
991 chat_text_view_realize_cb (GtkWidget *widget,
994 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
995 gtk_widget_grab_focus (widget);
999 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
1002 GtkTextBuffer *buffer;
1004 const gchar *smiley;
1006 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
1008 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1010 gtk_text_buffer_get_end_iter (buffer, &iter);
1011 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
1013 gtk_text_buffer_get_end_iter (buffer, &iter);
1014 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1025 static EmpathyChatSpell *
1026 chat_spell_new (EmpathyChat *chat,
1031 EmpathyChatSpell *chat_spell;
1033 chat_spell = g_slice_new0 (EmpathyChatSpell);
1035 chat_spell->chat = g_object_ref (chat);
1036 chat_spell->word = g_strdup (word);
1037 chat_spell->start = start;
1038 chat_spell->end = end;
1044 chat_spell_free (EmpathyChatSpell *chat_spell)
1046 g_object_unref (chat_spell->chat);
1047 g_free (chat_spell->word);
1048 g_slice_free (EmpathyChatSpell, chat_spell);
1052 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
1053 EmpathyChatSpell *chat_spell)
1055 empathy_spell_dialog_show (chat_spell->chat,
1062 chat_text_populate_popup_cb (GtkTextView *view,
1066 EmpathyChatPriv *priv;
1067 GtkTextBuffer *buffer;
1068 GtkTextTagTable *table;
1071 GtkTextIter iter, start, end;
1074 EmpathyChatSpell *chat_spell;
1075 GtkWidget *smiley_menu;
1077 priv = GET_PRIV (chat);
1079 /* Add the emoticon menu. */
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 (_("Insert Smiley"));
1085 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1086 gtk_widget_show (item);
1088 smiley_menu = empathy_chat_view_get_smiley_menu (
1089 G_CALLBACK (chat_insert_smiley_activate_cb),
1091 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1093 /* Add the spell check menu item. */
1094 buffer = gtk_text_view_get_buffer (view);
1095 table = gtk_text_buffer_get_tag_table (buffer);
1097 tag = gtk_text_tag_table_lookup (table, "misspelled");
1099 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1101 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1102 GTK_TEXT_WINDOW_WIDGET,
1106 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1110 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1111 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1113 str = gtk_text_buffer_get_text (buffer,
1114 &start, &end, FALSE);
1117 if (G_STR_EMPTY (str)) {
1121 chat_spell = chat_spell_new (chat, str, start, end);
1123 g_object_set_data_full (G_OBJECT (menu),
1124 "chat_spell", chat_spell,
1125 (GDestroyNotify) chat_spell_free);
1127 item = gtk_separator_menu_item_new ();
1128 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1129 gtk_widget_show (item);
1131 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1132 g_signal_connect (item,
1134 G_CALLBACK (chat_text_check_word_spelling_cb),
1136 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1137 gtk_widget_show (item);
1141 chat_add_logs (EmpathyChat *chat)
1143 EmpathyChatPriv *priv;
1144 GList *messages, *l;
1148 priv = GET_PRIV (chat);
1150 /* Turn off scrolling temporarily */
1151 empathy_chat_view_scroll (chat->view, FALSE);
1153 /* Add messages from last conversation */
1154 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1156 empathy_chat_get_id (chat),
1158 num_messages = g_list_length (messages);
1160 /* Only keep the 10 last messages */
1161 for (i = 0; num_messages - i > 10; i++) {
1162 EmpathyMessage *message;
1164 message = messages->data;
1165 messages = g_list_remove (messages, message);
1166 g_object_unref (message);
1169 for (l = messages; l; l = l->next) {
1170 empathy_chat_view_append_message (chat->view, l->data);
1172 priv->backlog_messages = messages;
1174 /* Turn back on scrolling */
1175 empathy_chat_view_scroll (chat->view, TRUE);
1179 chat_contacts_completion_func (const gchar *s1,
1183 gchar *tmp, *nick1, *nick2;
1186 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1187 nick1 = g_utf8_casefold (tmp, -1);
1190 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1191 nick2 = g_utf8_casefold (tmp, -1);
1194 ret = strncmp (nick1, nick2, n);
1203 chat_create_ui (EmpathyChat *chat)
1205 EmpathyChatPriv *priv = GET_PRIV (chat);
1210 filename = empathy_file_lookup ("empathy-chat.glade",
1212 glade = empathy_glade_get_file (filename,
1215 "chat_widget", &priv->widget,
1216 "hpaned", &priv->hpaned,
1217 "vbox_left", &priv->vbox_left,
1218 "scrolled_window_chat", &priv->scrolled_window_chat,
1219 "scrolled_window_input", &priv->scrolled_window_input,
1220 "hbox_topic", &priv->hbox_topic,
1221 "label_topic", &priv->label_topic,
1222 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1225 g_object_unref (glade);
1227 /* Add message GtkTextView. */
1228 chat->view = empathy_chat_view_new ();
1229 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1230 GTK_WIDGET (chat->view));
1231 gtk_widget_show (GTK_WIDGET (chat->view));
1233 /* Add input GtkTextView */
1234 chat->input_text_view = gtk_text_view_new ();
1235 g_object_set (chat->input_text_view,
1236 "pixels-above-lines", 2,
1237 "pixels-below-lines", 2,
1238 "pixels-inside-wrap", 1,
1241 "wrap-mode", GTK_WRAP_WORD_CHAR,
1243 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1244 chat->input_text_view);
1245 gtk_widget_show (chat->input_text_view);
1247 /* Add contact list */
1249 /* Add nick name completion */
1250 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1251 g_completion_set_compare (priv->completion,
1252 chat_contacts_completion_func);
1254 /* Set widget focus order */
1255 list = g_list_append (NULL, priv->scrolled_window_input);
1256 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1259 list = g_list_append (NULL, priv->vbox_left);
1260 list = g_list_append (list, priv->scrolled_window_contacts);
1261 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1264 list = g_list_append (NULL, priv->hpaned);
1265 list = g_list_append (list, priv->hbox_topic);
1266 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1269 /* Add the main widget in the chat widget */
1270 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1274 chat_size_request (GtkWidget *widget,
1275 GtkRequisition *requisition)
1277 GtkBin *bin = GTK_BIN (widget);
1279 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1280 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1282 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1284 GtkRequisition child_requisition;
1286 gtk_widget_size_request (bin->child, &child_requisition);
1288 requisition->width += child_requisition.width;
1289 requisition->height += child_requisition.height;
1294 chat_size_allocate (GtkWidget *widget,
1295 GtkAllocation *allocation)
1297 GtkBin *bin = GTK_BIN (widget);
1298 GtkAllocation child_allocation;
1300 widget->allocation = *allocation;
1302 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1304 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1305 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1306 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1307 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1309 gtk_widget_size_allocate (bin->child, &child_allocation);
1314 chat_finalize (GObject *object)
1317 EmpathyChatPriv *priv;
1319 chat = EMPATHY_CHAT (object);
1320 priv = GET_PRIV (chat);
1322 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1324 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1325 g_slist_free (priv->sent_messages);
1327 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1328 g_list_free (priv->compositors);
1330 g_list_foreach (priv->backlog_messages, (GFunc) g_object_unref, NULL);
1331 g_list_free (priv->backlog_messages);
1333 chat_composing_remove_timeout (chat);
1335 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1336 G_CALLBACK (chat_status_changed_cb),
1338 g_object_unref (priv->mc);
1339 g_object_unref (priv->log_manager);
1341 if (priv->tp_chat) {
1342 g_object_unref (priv->tp_chat);
1345 if (priv->account) {
1346 g_object_unref (priv->account);
1349 if (priv->block_events_timeout_id) {
1350 g_source_remove (priv->block_events_timeout_id);
1354 g_free (priv->name);
1355 g_free (priv->subject);
1357 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1361 chat_constructed (GObject *object)
1363 EmpathyChat *chat = EMPATHY_CHAT (object);
1364 GtkTextBuffer *buffer;
1366 chat_create_ui (chat);
1368 g_signal_connect (chat->input_text_view,
1370 G_CALLBACK (chat_input_key_press_event_cb),
1373 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1374 g_signal_connect (buffer,
1376 G_CALLBACK (chat_input_text_buffer_changed_cb),
1378 g_signal_connect (chat->view,
1380 G_CALLBACK (chat_text_view_focus_in_event_cb),
1383 g_signal_connect (chat->input_text_view,
1385 G_CALLBACK (chat_text_view_size_allocate_cb),
1388 g_signal_connect (chat->input_text_view,
1390 G_CALLBACK (chat_text_view_realize_cb),
1393 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
1395 G_CALLBACK (chat_text_populate_popup_cb),
1398 /* create misspelt words identification tag */
1399 gtk_text_buffer_create_tag (buffer,
1401 "underline", PANGO_UNDERLINE_ERROR,
1404 chat_add_logs (EMPATHY_CHAT (object));
1408 empathy_chat_class_init (EmpathyChatClass *klass)
1410 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1411 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1413 object_class->finalize = chat_finalize;
1414 object_class->get_property = chat_get_property;
1415 object_class->set_property = chat_set_property;
1416 object_class->constructed = chat_constructed;
1418 widget_class->size_request = chat_size_request;
1419 widget_class->size_allocate = chat_size_allocate;
1421 g_object_class_install_property (object_class,
1423 g_param_spec_object ("tp-chat",
1425 "The tp chat object",
1426 EMPATHY_TYPE_TP_CHAT,
1428 G_PARAM_READWRITE));
1429 g_object_class_install_property (object_class,
1431 g_param_spec_object ("account",
1432 "Account of the chat",
1433 "The account of the chat",
1436 g_object_class_install_property (object_class,
1438 g_param_spec_string ("id",
1440 "The id of the chat",
1443 g_object_class_install_property (object_class,
1445 g_param_spec_string ("name",
1447 "The name of the chat",
1450 g_object_class_install_property (object_class,
1452 g_param_spec_string ("subject",
1454 "The subject or topic of the chat",
1458 signals[COMPOSING] =
1459 g_signal_new ("composing",
1460 G_OBJECT_CLASS_TYPE (object_class),
1464 g_cclosure_marshal_VOID__BOOLEAN,
1468 signals[NEW_MESSAGE] =
1469 g_signal_new ("new-message",
1470 G_OBJECT_CLASS_TYPE (object_class),
1474 g_cclosure_marshal_VOID__OBJECT,
1476 1, EMPATHY_TYPE_MESSAGE);
1478 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1482 chat_block_events_timeout_cb (gpointer data)
1484 EmpathyChatPriv *priv = GET_PRIV (data);
1486 priv->block_events_timeout_id = 0;
1492 empathy_chat_init (EmpathyChat *chat)
1494 EmpathyChatPriv *priv = GET_PRIV (chat);
1496 chat_create_ui (chat);
1498 priv->is_first_char = TRUE;
1499 priv->log_manager = empathy_log_manager_new ();
1500 priv->default_window_height = -1;
1501 priv->vscroll_visible = FALSE;
1502 priv->sent_messages = NULL;
1503 priv->sent_messages_index = -1;
1504 priv->mc = empathy_mission_control_new ();
1506 /* Block events for some time to avoid having "has come online" or
1507 * "joined" messages. */
1508 priv->block_events_timeout_id =
1509 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1511 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
1512 G_CALLBACK (chat_status_changed_cb),
1517 empathy_chat_new (EmpathyTpChat *tp_chat)
1519 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1523 empathy_chat_get_tp_chat (EmpathyChat *chat)
1525 EmpathyChatPriv *priv = GET_PRIV (chat);
1527 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1529 return priv->tp_chat;
1533 empathy_chat_set_tp_chat (EmpathyChat *chat,
1534 EmpathyTpChat *tp_chat)
1536 EmpathyChatPriv *priv;
1539 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1540 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1542 priv = GET_PRIV (chat);
1544 if (tp_chat == priv->tp_chat) {
1548 if (priv->tp_chat) {
1549 g_object_unref (priv->tp_chat);
1551 if (priv->account) {
1552 g_object_unref (priv->account);
1556 priv->tp_chat = g_object_ref (tp_chat);
1557 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1558 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1559 tp_chan = empathy_tp_chat_get_channel (tp_chat);
1560 priv->handle_type = tp_chan->handle_type;
1562 g_signal_connect (tp_chat, "message-received",
1563 G_CALLBACK (chat_message_received_cb),
1565 g_signal_connect (tp_chat, "send-error",
1566 G_CALLBACK (chat_send_error_cb),
1568 g_signal_connect (tp_chat, "chat-state-changed",
1569 G_CALLBACK (chat_state_changed_cb),
1571 g_signal_connect (tp_chat, "property-changed",
1572 G_CALLBACK (chat_property_changed_cb),
1574 g_signal_connect (tp_chat, "destroy",
1575 G_CALLBACK (chat_destroy_cb),
1578 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1579 if (priv->block_events_timeout_id == 0) {
1580 empathy_chat_view_append_event (chat->view, _("Connected"));
1583 g_object_notify (G_OBJECT (chat), "tp-chat");
1584 g_object_notify (G_OBJECT (chat), "id");
1585 g_object_notify (G_OBJECT (chat), "account");
1589 empathy_chat_get_account (EmpathyChat *chat)
1591 EmpathyChatPriv *priv = GET_PRIV (chat);
1593 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1595 return priv->account;
1599 empathy_chat_get_id (EmpathyChat *chat)
1601 EmpathyChatPriv *priv = GET_PRIV (chat);
1603 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1609 empathy_chat_get_name (EmpathyChat *chat)
1611 EmpathyChatPriv *priv = GET_PRIV (chat);
1613 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1619 empathy_chat_get_subject (EmpathyChat *chat)
1621 EmpathyChatPriv *priv = GET_PRIV (chat);
1623 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1625 return priv->subject;
1629 empathy_chat_clear (EmpathyChat *chat)
1631 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1633 empathy_chat_view_clear (chat->view);
1637 empathy_chat_scroll_down (EmpathyChat *chat)
1639 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1641 empathy_chat_view_scroll_down (chat->view);
1645 empathy_chat_cut (EmpathyChat *chat)
1647 GtkTextBuffer *buffer;
1649 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1651 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1652 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1653 GtkClipboard *clipboard;
1655 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1657 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1662 empathy_chat_copy (EmpathyChat *chat)
1664 GtkTextBuffer *buffer;
1666 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1668 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1669 empathy_chat_view_copy_clipboard (chat->view);
1673 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1674 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1675 GtkClipboard *clipboard;
1677 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1679 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1684 empathy_chat_paste (EmpathyChat *chat)
1686 GtkTextBuffer *buffer;
1687 GtkClipboard *clipboard;
1689 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1691 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1692 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1694 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1698 empathy_chat_correct_word (EmpathyChat *chat,
1701 const gchar *new_word)
1703 GtkTextBuffer *buffer;
1705 g_return_if_fail (chat != NULL);
1706 g_return_if_fail (new_word != NULL);
1708 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1710 gtk_text_buffer_delete (buffer, &start, &end);
1711 gtk_text_buffer_insert (buffer, &start,