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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 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-lib.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-account-manager.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-dispatcher.h>
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-smiley-manager.h"
53 #include "empathy-ui-utils.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
58 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
66 EmpathyTpChat *tp_chat;
67 EmpathyAccount *account;
71 EmpathyContact *remote_contact;
72 gboolean show_contacts;
74 EmpathyLogManager *log_manager;
75 EmpathyAccountManager *account_manager;
76 GSList *sent_messages;
77 gint sent_messages_index;
79 GCompletion *completion;
80 guint composing_stop_timeout_id;
81 guint block_events_timeout_id;
82 TpHandleType handle_type;
84 gboolean has_input_vscroll;
89 GtkWidget *scrolled_window_chat;
90 GtkWidget *scrolled_window_input;
91 GtkWidget *scrolled_window_contacts;
92 GtkWidget *hbox_topic;
93 GtkWidget *label_topic;
94 GtkWidget *contact_list_view;
114 static guint signals[LAST_SIGNAL] = { 0 };
116 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
119 chat_get_property (GObject *object,
124 EmpathyChat *chat = EMPATHY_CHAT (object);
125 EmpathyChatPriv *priv = GET_PRIV (object);
129 g_value_set_object (value, priv->tp_chat);
132 g_value_set_object (value, priv->account);
135 g_value_set_string (value, empathy_chat_get_name (chat));
138 g_value_set_string (value, priv->id);
141 g_value_set_string (value, priv->subject);
143 case PROP_REMOTE_CONTACT:
144 g_value_set_object (value, priv->remote_contact);
146 case PROP_SHOW_CONTACTS:
147 g_value_set_boolean (value, priv->show_contacts);
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
156 chat_set_property (GObject *object,
161 EmpathyChat *chat = EMPATHY_CHAT (object);
165 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
167 case PROP_SHOW_CONTACTS:
168 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
177 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
181 EmpathyChat *chat = EMPATHY_CHAT (user_data);
182 EmpathyTpChat *tpchat;
185 empathy_chat_view_append_event (chat->view,
186 _("Failed to reconnect this chat"));
190 tpchat = EMPATHY_TP_CHAT (
191 empathy_dispatch_operation_get_channel_wrapper (dispatch));
193 if (empathy_dispatch_operation_claim (dispatch)) {
194 empathy_chat_set_tp_chat (chat, tpchat);
199 chat_new_connection_cb (EmpathyAccountManager *manager,
200 TpConnection *connection,
203 EmpathyChatPriv *priv = GET_PRIV (chat);
204 EmpathyAccount *account;
206 account = empathy_account_manager_get_account_for_connection (manager,
208 if (!priv->tp_chat && account == priv->account &&
209 priv->handle_type != TP_HANDLE_TYPE_NONE &&
210 !EMP_STR_EMPTY (priv->id)) {
212 DEBUG ("Account reconnected, request a new Text channel");
214 switch (priv->handle_type) {
215 case TP_HANDLE_TYPE_CONTACT:
216 empathy_dispatcher_chat_with_contact_id (
217 connection, priv->id,
218 chat_connect_channel_reconnected,
221 case TP_HANDLE_TYPE_ROOM:
222 empathy_dispatcher_join_muc (connection,
224 chat_connect_channel_reconnected,
228 g_assert_not_reached ();
235 chat_composing_remove_timeout (EmpathyChat *chat)
237 EmpathyChatPriv *priv;
239 priv = GET_PRIV (chat);
241 if (priv->composing_stop_timeout_id) {
242 g_source_remove (priv->composing_stop_timeout_id);
243 priv->composing_stop_timeout_id = 0;
248 chat_composing_stop_timeout_cb (EmpathyChat *chat)
250 EmpathyChatPriv *priv;
252 priv = GET_PRIV (chat);
254 priv->composing_stop_timeout_id = 0;
255 empathy_tp_chat_set_state (priv->tp_chat,
256 TP_CHANNEL_CHAT_STATE_PAUSED);
262 chat_composing_start (EmpathyChat *chat)
264 EmpathyChatPriv *priv;
266 priv = GET_PRIV (chat);
268 if (priv->composing_stop_timeout_id) {
269 /* Just restart the timeout */
270 chat_composing_remove_timeout (chat);
272 empathy_tp_chat_set_state (priv->tp_chat,
273 TP_CHANNEL_CHAT_STATE_COMPOSING);
276 priv->composing_stop_timeout_id = g_timeout_add_seconds (
277 COMPOSING_STOP_TIMEOUT,
278 (GSourceFunc) chat_composing_stop_timeout_cb,
283 chat_composing_stop (EmpathyChat *chat)
285 EmpathyChatPriv *priv;
287 priv = GET_PRIV (chat);
289 chat_composing_remove_timeout (chat);
290 empathy_tp_chat_set_state (priv->tp_chat,
291 TP_CHANNEL_CHAT_STATE_ACTIVE);
295 chat_sent_message_add (EmpathyChat *chat,
298 EmpathyChatPriv *priv;
302 priv = GET_PRIV (chat);
304 /* Save the sent message in our repeat buffer */
305 list = priv->sent_messages;
307 /* Remove any other occurances of this msg */
308 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
309 list = g_slist_remove_link (list, item);
311 g_slist_free1 (item);
314 /* Trim the list to the last 10 items */
315 while (g_slist_length (list) > 10) {
316 item = g_slist_last (list);
318 list = g_slist_remove_link (list, item);
320 g_slist_free1 (item);
324 /* Add new message */
325 list = g_slist_prepend (list, g_strdup (str));
327 /* Set list and reset the index */
328 priv->sent_messages = list;
329 priv->sent_messages_index = -1;
333 chat_sent_message_get_next (EmpathyChat *chat)
335 EmpathyChatPriv *priv;
338 priv = GET_PRIV (chat);
340 if (!priv->sent_messages) {
341 DEBUG ("No sent messages, next message is NULL");
345 max = g_slist_length (priv->sent_messages) - 1;
347 if (priv->sent_messages_index < max) {
348 priv->sent_messages_index++;
351 DEBUG ("Returning next message index:%d", priv->sent_messages_index);
353 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
357 chat_sent_message_get_last (EmpathyChat *chat)
359 EmpathyChatPriv *priv;
361 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
363 priv = GET_PRIV (chat);
365 if (!priv->sent_messages) {
366 DEBUG ("No sent messages, last message is NULL");
370 if (priv->sent_messages_index >= 0) {
371 priv->sent_messages_index--;
374 DEBUG ("Returning last message index:%d", priv->sent_messages_index);
376 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
380 chat_send (EmpathyChat *chat,
383 EmpathyChatPriv *priv;
384 EmpathyMessage *message;
386 if (EMP_STR_EMPTY (msg)) {
390 priv = GET_PRIV (chat);
392 chat_sent_message_add (chat, msg);
394 if (g_str_has_prefix (msg, "/clear")) {
395 empathy_chat_view_clear (chat->view);
399 /* Blacklist messages begining by '/', except for "/me" and "/say"
400 * because they are handled in EmpathyMessage */
402 !g_str_has_prefix (msg, "/me") &&
403 !g_str_has_prefix (msg, "/say")) {
404 empathy_chat_view_append_event (chat->view,
405 _("Unsupported command"));
409 /* We can send the message */
410 message = empathy_message_new (msg);
411 empathy_tp_chat_send (priv->tp_chat, message);
412 g_object_unref (message);
416 chat_input_text_view_send (EmpathyChat *chat)
418 EmpathyChatPriv *priv;
419 GtkTextBuffer *buffer;
420 GtkTextIter start, end;
423 priv = GET_PRIV (chat);
425 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
427 gtk_text_buffer_get_bounds (buffer, &start, &end);
428 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
430 /* clear the input field */
431 gtk_text_buffer_set_text (buffer, "", -1);
433 chat_send (chat, msg);
438 chat_state_changed_cb (EmpathyTpChat *tp_chat,
439 EmpathyContact *contact,
440 TpChannelChatState state,
443 EmpathyChatPriv *priv;
445 gboolean was_composing;
447 priv = GET_PRIV (chat);
449 if (empathy_contact_is_user (contact)) {
450 /* We don't care about our own chat state */
454 was_composing = (priv->compositors != NULL);
456 /* Find the contact in the list. After that l is the list elem or NULL */
457 for (l = priv->compositors; l; l = l->next) {
458 if (contact == l->data) {
464 case TP_CHANNEL_CHAT_STATE_GONE:
465 case TP_CHANNEL_CHAT_STATE_INACTIVE:
466 case TP_CHANNEL_CHAT_STATE_PAUSED:
467 case TP_CHANNEL_CHAT_STATE_ACTIVE:
468 /* Contact is not composing */
470 priv->compositors = g_list_remove_link (priv->compositors, l);
471 g_object_unref (l->data);
475 case TP_CHANNEL_CHAT_STATE_COMPOSING:
476 /* Contact is composing */
478 priv->compositors = g_list_prepend (priv->compositors,
479 g_object_ref (contact));
483 g_assert_not_reached ();
486 DEBUG ("Was composing: %s now composing: %s",
487 was_composing ? "yes" : "no",
488 priv->compositors ? "yes" : "no");
490 if ((was_composing && !priv->compositors) ||
491 (!was_composing && priv->compositors)) {
492 /* Composing state changed */
493 g_signal_emit (chat, signals[COMPOSING], 0,
494 priv->compositors != NULL);
499 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
501 EmpathyChatPriv *priv = GET_PRIV (chat);
502 EmpathyContact *sender;
504 sender = empathy_message_get_sender (message);
506 DEBUG ("Appending new message from %s (%d)",
507 empathy_contact_get_name (sender),
508 empathy_contact_get_handle (sender));
510 empathy_chat_view_append_message (chat->view, message);
512 /* We received a message so the contact is no longer composing */
513 chat_state_changed_cb (priv->tp_chat, sender,
514 TP_CHANNEL_CHAT_STATE_ACTIVE,
517 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
521 chat_message_received_cb (EmpathyTpChat *tp_chat,
522 EmpathyMessage *message,
525 chat_message_received (chat, message);
526 empathy_tp_chat_acknowledge_message (tp_chat, message);
530 chat_send_error_cb (EmpathyTpChat *tp_chat,
531 EmpathyMessage *message,
532 TpChannelTextSendError error_code,
538 switch (error_code) {
539 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
540 error = _("offline");
542 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
543 error = _("invalid contact");
545 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
546 error = _("permission denied");
548 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
549 error = _("too long message");
551 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
552 error = _("not implemented");
555 error = _("unknown");
559 str = g_strdup_printf (_("Error sending message '%s': %s"),
560 empathy_message_get_body (message),
562 empathy_chat_view_append_event (chat->view, str);
567 chat_property_changed_cb (EmpathyTpChat *tp_chat,
572 EmpathyChatPriv *priv = GET_PRIV (chat);
574 if (!tp_strdiff (name, "subject")) {
575 g_free (priv->subject);
576 priv->subject = g_value_dup_string (value);
577 g_object_notify (G_OBJECT (chat), "subject");
579 if (EMP_STR_EMPTY (priv->subject)) {
580 gtk_widget_hide (priv->hbox_topic);
582 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
583 gtk_widget_show (priv->hbox_topic);
585 if (priv->block_events_timeout_id == 0) {
588 if (!EMP_STR_EMPTY (priv->subject)) {
589 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
591 str = g_strdup (_("No topic defined"));
593 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
597 else if (!tp_strdiff (name, "name")) {
599 priv->name = g_value_dup_string (value);
600 g_object_notify (G_OBJECT (chat), "name");
605 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
608 EmpathyChatPriv *priv;
609 GtkTextIter start, end;
611 gboolean spell_checker = FALSE;
613 priv = GET_PRIV (chat);
615 if (gtk_text_buffer_get_char_count (buffer) == 0) {
616 chat_composing_stop (chat);
618 chat_composing_start (chat);
621 empathy_conf_get_bool (empathy_conf_get (),
622 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
625 gtk_text_buffer_get_start_iter (buffer, &start);
627 if (!spell_checker) {
628 gtk_text_buffer_get_end_iter (buffer, &end);
629 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
633 if (!empathy_spell_supported ()) {
637 /* NOTE: this is really inefficient, we shouldn't have to
638 reiterate the whole buffer each time and check each work
641 gboolean correct = FALSE;
644 if (gtk_text_iter_is_start (&start)) {
647 if (!gtk_text_iter_forward_word_end (&end)) {
648 /* no whole word yet */
652 if (!gtk_text_iter_forward_word_end (&end)) {
653 /* must be the end of the buffer */
658 gtk_text_iter_backward_word_start (&start);
661 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
663 /* spell check string if not a command */
665 correct = empathy_spell_check (str);
671 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
673 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
678 /* set start iter to the end iters position */
684 chat_input_key_press_event_cb (GtkWidget *widget,
688 EmpathyChatPriv *priv;
691 GtkWidget *text_view_sw;
693 priv = GET_PRIV (chat);
695 /* Catch ctrl+up/down so we can traverse messages we sent */
696 if ((event->state & GDK_CONTROL_MASK) &&
697 (event->keyval == GDK_Up ||
698 event->keyval == GDK_Down)) {
699 GtkTextBuffer *buffer;
702 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
704 if (event->keyval == GDK_Up) {
705 str = chat_sent_message_get_next (chat);
707 str = chat_sent_message_get_last (chat);
710 g_signal_handlers_block_by_func (buffer,
711 chat_input_text_buffer_changed_cb,
713 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
714 g_signal_handlers_unblock_by_func (buffer,
715 chat_input_text_buffer_changed_cb,
721 /* Catch enter but not ctrl/shift-enter */
722 if (IS_ENTER (event->keyval) &&
723 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
726 /* This is to make sure that kinput2 gets the enter. And if
727 * it's handled there we shouldn't send on it. This is because
728 * kinput2 uses Enter to commit letters. See:
729 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
732 view = GTK_TEXT_VIEW (chat->input_text_view);
733 if (gtk_im_context_filter_keypress (view->im_context, event)) {
734 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
738 chat_input_text_view_send (chat);
742 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
744 if (IS_ENTER (event->keyval) &&
745 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
746 /* Newline for shift/control-enter. */
749 if (!(event->state & GDK_CONTROL_MASK) &&
750 event->keyval == GDK_Page_Up) {
751 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
752 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
755 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
756 event->keyval == GDK_Page_Down) {
757 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
758 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
759 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
760 gtk_adjustment_set_value (adj, val);
763 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
764 event->keyval == GDK_Tab) {
765 GtkTextBuffer *buffer;
766 GtkTextIter start, current;
767 gchar *nick, *completed;
768 GList *list, *completed_list;
769 gboolean is_start_of_buffer;
771 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
772 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
774 /* Get the start of the nick to complete. */
775 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
776 gtk_text_iter_backward_word_start (&start);
777 is_start_of_buffer = gtk_text_iter_is_start (&start);
779 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
780 g_completion_add_items (priv->completion, list);
782 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
783 completed_list = g_completion_complete (priv->completion,
792 gchar *complete_char = NULL;
794 gtk_text_buffer_delete (buffer, &start, ¤t);
796 len = g_list_length (completed_list);
799 /* If we only have one hit, use that text
800 * instead of the text in completed since the
801 * completed text will use the typed string
802 * which might be cased all wrong.
805 text = empathy_contact_get_name (completed_list->data);
810 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
812 if (len == 1 && is_start_of_buffer &&
813 empathy_conf_get_string (empathy_conf_get (),
814 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
816 complete_char != NULL) {
817 gtk_text_buffer_insert_at_cursor (buffer,
819 strlen (complete_char));
820 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
821 g_free (complete_char);
827 g_completion_clear_items (priv->completion);
829 g_list_foreach (list, (GFunc) g_object_unref, NULL);
839 chat_text_view_focus_in_event_cb (GtkWidget *widget,
843 gtk_widget_grab_focus (chat->input_text_view);
849 chat_input_set_size_request_idle (gpointer sw)
851 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
857 chat_input_size_request_cb (GtkWidget *widget,
858 GtkRequisition *requisition,
861 EmpathyChatPriv *priv = GET_PRIV (chat);
864 sw = gtk_widget_get_parent (widget);
865 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
866 g_idle_add (chat_input_set_size_request_idle, sw);
867 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
870 priv->has_input_vscroll = TRUE;
873 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
874 gtk_widget_set_size_request (sw, -1, -1);
875 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
878 priv->has_input_vscroll = FALSE;
883 chat_input_realize_cb (GtkWidget *widget,
886 DEBUG ("Setting focus to the input text view");
887 gtk_widget_grab_focus (widget);
891 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
892 EmpathySmiley *smiley,
895 EmpathyChat *chat = EMPATHY_CHAT (user_data);
896 GtkTextBuffer *buffer;
899 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
901 gtk_text_buffer_get_end_iter (buffer, &iter);
902 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
904 gtk_text_buffer_get_end_iter (buffer, &iter);
905 gtk_text_buffer_insert (buffer, &iter, " ", -1);
916 static EmpathyChatSpell *
917 chat_spell_new (EmpathyChat *chat,
922 EmpathyChatSpell *chat_spell;
924 chat_spell = g_slice_new0 (EmpathyChatSpell);
926 chat_spell->chat = g_object_ref (chat);
927 chat_spell->word = g_strdup (word);
928 chat_spell->start = start;
929 chat_spell->end = end;
935 chat_spell_free (EmpathyChatSpell *chat_spell)
937 g_object_unref (chat_spell->chat);
938 g_free (chat_spell->word);
939 g_slice_free (EmpathyChatSpell, chat_spell);
943 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
944 EmpathyChatSpell *chat_spell)
946 empathy_chat_correct_word (chat_spell->chat,
947 &(chat_spell->start),
949 gtk_menu_item_get_label (menu_item));
953 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
955 GtkWidget *menu, *menu_item;
956 GList *suggestions, *l;
958 menu = gtk_menu_new ();
959 suggestions = empathy_spell_get_suggestions (chat_spell->word);
960 if (suggestions == NULL) {
961 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
962 gtk_widget_set_sensitive (menu_item, FALSE);
963 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
965 for (l = suggestions; l; l = l->next) {
966 menu_item = gtk_menu_item_new_with_label (l->data);
967 g_signal_connect (G_OBJECT (menu_item),
969 G_CALLBACK (chat_spelling_menu_activate_cb),
971 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
974 empathy_spell_free_suggestions (suggestions);
976 gtk_widget_show_all (menu);
982 chat_text_send_cb (GtkMenuItem *menuitem,
985 chat_input_text_view_send (chat);
989 chat_input_populate_popup_cb (GtkTextView *view,
993 EmpathyChatPriv *priv;
994 GtkTextBuffer *buffer;
995 GtkTextTagTable *table;
998 GtkTextIter iter, start, end;
1001 EmpathyChatSpell *chat_spell;
1002 GtkWidget *spell_menu;
1003 EmpathySmileyManager *smiley_manager;
1004 GtkWidget *smiley_menu;
1007 priv = GET_PRIV (chat);
1008 buffer = gtk_text_view_get_buffer (view);
1010 /* Add the emoticon menu. */
1011 item = gtk_separator_menu_item_new ();
1012 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1013 gtk_widget_show (item);
1015 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1016 image = gtk_image_new_from_icon_name ("face-smile",
1017 GTK_ICON_SIZE_MENU);
1018 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1019 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1020 gtk_widget_show (item);
1022 smiley_manager = empathy_smiley_manager_dup_singleton ();
1023 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1024 chat_insert_smiley_activate_cb,
1026 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1027 g_object_unref (smiley_manager);
1029 /* Add the Send menu item. */
1030 gtk_text_buffer_get_bounds (buffer, &start, &end);
1031 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1032 if (!EMP_STR_EMPTY (str)) {
1033 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1034 g_signal_connect (G_OBJECT (item), "activate",
1035 G_CALLBACK (chat_text_send_cb), chat);
1036 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1037 gtk_widget_show (item);
1041 /* Add the spell check menu item. */
1042 table = gtk_text_buffer_get_tag_table (buffer);
1043 tag = gtk_text_tag_table_lookup (table, "misspelled");
1044 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1045 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1046 GTK_TEXT_WINDOW_WIDGET,
1049 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1051 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1052 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1054 str = gtk_text_buffer_get_text (buffer,
1055 &start, &end, FALSE);
1057 if (!EMP_STR_EMPTY (str)) {
1058 chat_spell = chat_spell_new (chat, str, start, end);
1059 g_object_set_data_full (G_OBJECT (menu),
1060 "chat_spell", chat_spell,
1061 (GDestroyNotify) chat_spell_free);
1063 item = gtk_separator_menu_item_new ();
1064 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1065 gtk_widget_show (item);
1067 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1068 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1069 GTK_ICON_SIZE_MENU);
1070 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1072 spell_menu = chat_spelling_build_menu (chat_spell);
1073 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1075 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1076 gtk_widget_show (item);
1081 chat_log_filter (EmpathyMessage *message,
1084 EmpathyChat *chat = (EmpathyChat *) user_data;
1085 EmpathyChatPriv *priv = GET_PRIV (chat);
1086 const GList *pending;
1088 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1090 for (; pending; pending = g_list_next (pending)) {
1091 if (empathy_message_equal (message, pending->data)) {
1100 chat_add_logs (EmpathyChat *chat)
1102 EmpathyChatPriv *priv = GET_PRIV (chat);
1103 gboolean is_chatroom;
1104 GList *messages, *l;
1110 /* Turn off scrolling temporarily */
1111 empathy_chat_view_scroll (chat->view, FALSE);
1113 /* Add messages from last conversation */
1114 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1116 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1124 for (l = messages; l; l = g_list_next (l)) {
1125 empathy_chat_view_append_message (chat->view, l->data);
1126 g_object_unref (l->data);
1129 g_list_free (messages);
1131 /* Turn back on scrolling */
1132 empathy_chat_view_scroll (chat->view, TRUE);
1136 chat_contacts_completion_func (const gchar *s1,
1140 gchar *tmp, *nick1, *nick2;
1147 return s1 ? -1 : +1;
1150 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1151 nick1 = g_utf8_casefold (tmp, -1);
1154 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1155 nick2 = g_utf8_casefold (tmp, -1);
1158 ret = strncmp (nick1, nick2, n);
1167 build_part_message (guint reason,
1169 EmpathyContact *actor,
1170 const gchar *message)
1172 GString *s = g_string_new ("");
1173 const gchar *actor_name = NULL;
1175 if (actor != NULL) {
1176 actor_name = empathy_contact_get_name (actor);
1179 /* Having an actor only really makes sense for a few actions... */
1181 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1182 g_string_append_printf (s, _("%s has disconnected"), name);
1184 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1185 if (actor_name != NULL) {
1186 g_string_append_printf (s, _("%s was kicked by %s"),
1189 g_string_append_printf (s, _("%s was kicked"), name);
1192 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1193 if (actor_name != NULL) {
1194 g_string_append_printf (s, _("%s was banned by %s"),
1197 g_string_append_printf (s, _("%s was banned"), name);
1201 g_string_append_printf (s, _("%s has left the room"), name);
1204 if (!EMP_STR_EMPTY (message)) {
1205 /* Note to translators: this string is appended to
1206 * notifications like "foo has left the room", with the message
1207 * given by the user living the room. If this poses a problem,
1208 * please let us know. :-)
1210 g_string_append_printf (s, _(" (%s)"), message);
1213 return g_string_free (s, FALSE);
1217 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1218 EmpathyContact *contact,
1219 EmpathyContact *actor,
1225 EmpathyChatPriv *priv = GET_PRIV (chat);
1226 const gchar *name = empathy_contact_get_name (contact);
1229 if (priv->block_events_timeout_id != 0)
1233 str = g_strdup_printf (_("%s has joined the room"),
1236 str = build_part_message (reason, name, actor, message);
1239 empathy_chat_view_append_event (chat->view, str);
1244 chat_reset_size_request (gpointer widget)
1246 gtk_widget_set_size_request (widget, -1, -1);
1252 chat_update_contacts_visibility (EmpathyChat *chat)
1254 EmpathyChatPriv *priv = GET_PRIV (chat);
1257 show = priv->remote_contact == NULL && priv->show_contacts;
1259 if (!priv->scrolled_window_contacts) {
1263 if (show && priv->contact_list_view == NULL) {
1264 EmpathyContactListStore *store;
1267 /* We are adding the contact list to the chat, we don't want the
1268 * chat view to become too small. If the chat view is already
1269 * smaller than 250 make sure that size won't change. If the
1270 * chat view is bigger the contact list will take some space on
1271 * it but we make sure the chat view don't become smaller than
1272 * 250. Relax the size request once the resize is done */
1273 min_width = MIN (priv->vbox_left->allocation.width, 250);
1274 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1275 g_idle_add (chat_reset_size_request, priv->vbox_left);
1277 if (priv->contacts_width > 0) {
1278 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1279 priv->contacts_width);
1282 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1283 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1284 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1285 EMPATHY_CONTACT_FEATURE_CHAT |
1286 EMPATHY_CONTACT_FEATURE_CALL |
1287 EMPATHY_CONTACT_FEATURE_LOG |
1288 EMPATHY_CONTACT_FEATURE_INFO));
1289 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1290 priv->contact_list_view);
1291 gtk_widget_show (priv->contact_list_view);
1292 gtk_widget_show (priv->scrolled_window_contacts);
1293 g_object_unref (store);
1295 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1296 gtk_widget_hide (priv->scrolled_window_contacts);
1297 if (priv->contact_list_view != NULL) {
1298 gtk_widget_destroy (priv->contact_list_view);
1299 priv->contact_list_view = NULL;
1305 empathy_chat_set_show_contacts (EmpathyChat *chat,
1308 EmpathyChatPriv *priv = GET_PRIV (chat);
1310 priv->show_contacts = show;
1312 chat_update_contacts_visibility (chat);
1314 g_object_notify (G_OBJECT (chat), "show-contacts");
1318 chat_remote_contact_changed_cb (EmpathyChat *chat)
1320 EmpathyChatPriv *priv = GET_PRIV (chat);
1322 if (priv->remote_contact != NULL) {
1323 g_object_unref (priv->remote_contact);
1324 priv->remote_contact = NULL;
1327 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1328 if (priv->remote_contact != NULL) {
1329 g_object_ref (priv->remote_contact);
1330 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1332 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1334 else if (priv->tp_chat != NULL) {
1337 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1338 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1340 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1343 chat_update_contacts_visibility (chat);
1345 g_object_notify (G_OBJECT (chat), "remote-contact");
1346 g_object_notify (G_OBJECT (chat), "id");
1350 chat_destroy_cb (EmpathyTpChat *tp_chat,
1353 EmpathyChatPriv *priv;
1355 priv = GET_PRIV (chat);
1357 if (!priv->tp_chat) {
1361 chat_composing_remove_timeout (chat);
1362 g_object_unref (priv->tp_chat);
1363 priv->tp_chat = NULL;
1364 g_object_notify (G_OBJECT (chat), "tp-chat");
1366 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1367 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1368 empathy_chat_set_show_contacts (chat, FALSE);
1372 show_pending_messages (EmpathyChat *chat) {
1373 EmpathyChatPriv *priv = GET_PRIV (chat);
1374 const GList *messages, *l;
1376 if (chat->view == NULL || priv->tp_chat == NULL)
1379 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1381 for (l = messages; l != NULL ; l = g_list_next (l)) {
1382 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1383 chat_message_received (chat, message);
1385 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1389 chat_create_ui (EmpathyChat *chat)
1391 EmpathyChatPriv *priv = GET_PRIV (chat);
1395 GtkTextBuffer *buffer;
1397 filename = empathy_file_lookup ("empathy-chat.ui",
1399 gui = empathy_builder_get_file (filename,
1400 "chat_widget", &priv->widget,
1401 "hpaned", &priv->hpaned,
1402 "vbox_left", &priv->vbox_left,
1403 "scrolled_window_chat", &priv->scrolled_window_chat,
1404 "scrolled_window_input", &priv->scrolled_window_input,
1405 "hbox_topic", &priv->hbox_topic,
1406 "label_topic", &priv->label_topic,
1407 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1411 /* Add message view. */
1412 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1413 g_signal_connect (chat->view, "focus_in_event",
1414 G_CALLBACK (chat_text_view_focus_in_event_cb),
1416 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1417 GTK_WIDGET (chat->view));
1418 gtk_widget_show (GTK_WIDGET (chat->view));
1420 /* Add input GtkTextView */
1421 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1422 "pixels-above-lines", 2,
1423 "pixels-below-lines", 2,
1424 "pixels-inside-wrap", 1,
1427 "wrap-mode", GTK_WRAP_WORD_CHAR,
1429 g_signal_connect (chat->input_text_view, "key-press-event",
1430 G_CALLBACK (chat_input_key_press_event_cb),
1432 g_signal_connect (chat->input_text_view, "size-request",
1433 G_CALLBACK (chat_input_size_request_cb),
1435 g_signal_connect (chat->input_text_view, "realize",
1436 G_CALLBACK (chat_input_realize_cb),
1438 g_signal_connect (chat->input_text_view, "populate-popup",
1439 G_CALLBACK (chat_input_populate_popup_cb),
1441 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1442 g_signal_connect (buffer, "changed",
1443 G_CALLBACK (chat_input_text_buffer_changed_cb),
1445 gtk_text_buffer_create_tag (buffer, "misspelled",
1446 "underline", PANGO_UNDERLINE_ERROR,
1448 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1449 chat->input_text_view);
1450 gtk_widget_show (chat->input_text_view);
1452 /* Create contact list */
1453 chat_update_contacts_visibility (chat);
1455 /* Initialy hide the topic, will be shown if not empty */
1456 gtk_widget_hide (priv->hbox_topic);
1458 /* Set widget focus order */
1459 list = g_list_append (NULL, priv->scrolled_window_input);
1460 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1463 list = g_list_append (NULL, priv->vbox_left);
1464 list = g_list_append (list, priv->scrolled_window_contacts);
1465 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1468 list = g_list_append (NULL, priv->hpaned);
1469 list = g_list_append (list, priv->hbox_topic);
1470 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1473 /* Add the main widget in the chat widget */
1474 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1475 g_object_unref (gui);
1479 chat_size_request (GtkWidget *widget,
1480 GtkRequisition *requisition)
1482 GtkBin *bin = GTK_BIN (widget);
1485 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1486 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1488 child = gtk_bin_get_child (bin);
1490 if (child && GTK_WIDGET_VISIBLE (child))
1492 GtkRequisition child_requisition;
1494 gtk_widget_size_request (child, &child_requisition);
1496 requisition->width += child_requisition.width;
1497 requisition->height += child_requisition.height;
1502 chat_size_allocate (GtkWidget *widget,
1503 GtkAllocation *allocation)
1505 GtkBin *bin = GTK_BIN (widget);
1506 GtkAllocation child_allocation;
1509 widget->allocation = *allocation;
1511 child = gtk_bin_get_child (bin);
1513 if (child && GTK_WIDGET_VISIBLE (child))
1515 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
1516 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
1517 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1518 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1520 gtk_widget_size_allocate (child, &child_allocation);
1525 chat_finalize (GObject *object)
1528 EmpathyChatPriv *priv;
1530 chat = EMPATHY_CHAT (object);
1531 priv = GET_PRIV (chat);
1533 DEBUG ("Finalized: %p", object);
1535 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1536 g_slist_free (priv->sent_messages);
1538 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1539 g_list_free (priv->compositors);
1541 chat_composing_remove_timeout (chat);
1543 g_signal_handlers_disconnect_by_func (priv->account_manager,
1544 chat_new_connection_cb, object);
1546 g_object_unref (priv->account_manager);
1547 g_object_unref (priv->log_manager);
1549 if (priv->tp_chat) {
1550 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1551 chat_destroy_cb, chat);
1552 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1553 chat_message_received_cb, chat);
1554 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1555 chat_send_error_cb, chat);
1556 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1557 chat_state_changed_cb, chat);
1558 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1559 chat_property_changed_cb, chat);
1560 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1561 chat_members_changed_cb, chat);
1562 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1563 chat_remote_contact_changed_cb, chat);
1564 empathy_tp_chat_close (priv->tp_chat);
1565 g_object_unref (priv->tp_chat);
1567 if (priv->account) {
1568 g_object_unref (priv->account);
1570 if (priv->remote_contact) {
1571 g_object_unref (priv->remote_contact);
1574 if (priv->block_events_timeout_id) {
1575 g_source_remove (priv->block_events_timeout_id);
1579 g_free (priv->name);
1580 g_free (priv->subject);
1581 g_completion_free (priv->completion);
1583 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1587 chat_constructed (GObject *object)
1589 EmpathyChat *chat = EMPATHY_CHAT (object);
1591 chat_create_ui (chat);
1592 chat_add_logs (chat);
1593 show_pending_messages (chat);
1597 empathy_chat_class_init (EmpathyChatClass *klass)
1599 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1600 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1602 object_class->finalize = chat_finalize;
1603 object_class->get_property = chat_get_property;
1604 object_class->set_property = chat_set_property;
1605 object_class->constructed = chat_constructed;
1607 widget_class->size_request = chat_size_request;
1608 widget_class->size_allocate = chat_size_allocate;
1610 g_object_class_install_property (object_class,
1612 g_param_spec_object ("tp-chat",
1614 "The tp chat object",
1615 EMPATHY_TYPE_TP_CHAT,
1618 G_PARAM_STATIC_STRINGS));
1619 g_object_class_install_property (object_class,
1621 g_param_spec_object ("account",
1622 "Account of the chat",
1623 "The account of the chat",
1624 EMPATHY_TYPE_ACCOUNT,
1626 G_PARAM_STATIC_STRINGS));
1627 g_object_class_install_property (object_class,
1629 g_param_spec_string ("id",
1631 "The id of the chat",
1634 G_PARAM_STATIC_STRINGS));
1635 g_object_class_install_property (object_class,
1637 g_param_spec_string ("name",
1639 "The name of the chat",
1642 G_PARAM_STATIC_STRINGS));
1643 g_object_class_install_property (object_class,
1645 g_param_spec_string ("subject",
1647 "The subject or topic of the chat",
1650 G_PARAM_STATIC_STRINGS));
1651 g_object_class_install_property (object_class,
1652 PROP_REMOTE_CONTACT,
1653 g_param_spec_object ("remote-contact",
1654 "The remote contact",
1655 "The remote contact is any",
1656 EMPATHY_TYPE_CONTACT,
1658 G_PARAM_STATIC_STRINGS));
1659 g_object_class_install_property (object_class,
1661 g_param_spec_boolean ("show-contacts",
1662 "Contacts' visibility",
1663 "The visibility of the contacts' list",
1666 G_PARAM_STATIC_STRINGS));
1668 signals[COMPOSING] =
1669 g_signal_new ("composing",
1670 G_OBJECT_CLASS_TYPE (object_class),
1674 g_cclosure_marshal_VOID__BOOLEAN,
1678 signals[NEW_MESSAGE] =
1679 g_signal_new ("new-message",
1680 G_OBJECT_CLASS_TYPE (object_class),
1684 g_cclosure_marshal_VOID__OBJECT,
1686 1, EMPATHY_TYPE_MESSAGE);
1688 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1692 chat_block_events_timeout_cb (gpointer data)
1694 EmpathyChatPriv *priv = GET_PRIV (data);
1696 priv->block_events_timeout_id = 0;
1702 empathy_chat_init (EmpathyChat *chat)
1704 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1705 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1708 priv->log_manager = empathy_log_manager_dup_singleton ();
1709 priv->contacts_width = -1;
1710 priv->sent_messages = NULL;
1711 priv->sent_messages_index = -1;
1712 priv->account_manager = empathy_account_manager_dup_singleton ();
1714 g_signal_connect (priv->account_manager,
1716 G_CALLBACK (chat_new_connection_cb),
1719 empathy_conf_get_bool (empathy_conf_get (),
1720 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1721 &priv->show_contacts);
1723 /* Block events for some time to avoid having "has come online" or
1724 * "joined" messages. */
1725 priv->block_events_timeout_id =
1726 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1728 /* Add nick name completion */
1729 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1730 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1734 empathy_chat_new (EmpathyTpChat *tp_chat)
1736 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1740 empathy_chat_get_tp_chat (EmpathyChat *chat)
1742 EmpathyChatPriv *priv = GET_PRIV (chat);
1744 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1746 return priv->tp_chat;
1750 empathy_chat_set_tp_chat (EmpathyChat *chat,
1751 EmpathyTpChat *tp_chat)
1753 EmpathyChatPriv *priv = GET_PRIV (chat);
1754 TpConnection *connection;
1756 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1757 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1758 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1760 if (priv->tp_chat) {
1764 if (priv->account) {
1765 g_object_unref (priv->account);
1768 priv->tp_chat = g_object_ref (tp_chat);
1769 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1770 priv->account = empathy_account_manager_get_account_for_connection (
1771 priv->account_manager,
1773 g_object_ref (priv->account);
1775 g_signal_connect (tp_chat, "destroy",
1776 G_CALLBACK (chat_destroy_cb),
1778 g_signal_connect (tp_chat, "message-received",
1779 G_CALLBACK (chat_message_received_cb),
1781 g_signal_connect (tp_chat, "send-error",
1782 G_CALLBACK (chat_send_error_cb),
1784 g_signal_connect (tp_chat, "chat-state-changed",
1785 G_CALLBACK (chat_state_changed_cb),
1787 g_signal_connect (tp_chat, "property-changed",
1788 G_CALLBACK (chat_property_changed_cb),
1790 g_signal_connect (tp_chat, "members-changed",
1791 G_CALLBACK (chat_members_changed_cb),
1793 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1794 G_CALLBACK (chat_remote_contact_changed_cb),
1797 chat_remote_contact_changed_cb (chat);
1799 if (chat->input_text_view) {
1800 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1801 if (priv->block_events_timeout_id == 0) {
1802 empathy_chat_view_append_event (chat->view, _("Connected"));
1806 g_object_notify (G_OBJECT (chat), "tp-chat");
1807 g_object_notify (G_OBJECT (chat), "id");
1808 g_object_notify (G_OBJECT (chat), "account");
1810 /* This is a noop when tp-chat is set at object construction time and causes
1811 * the pending messages to be show when it's set on the object after it has
1813 show_pending_messages (chat);
1817 empathy_chat_get_account (EmpathyChat *chat)
1819 EmpathyChatPriv *priv = GET_PRIV (chat);
1821 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1823 return priv->account;
1827 empathy_chat_get_id (EmpathyChat *chat)
1829 EmpathyChatPriv *priv = GET_PRIV (chat);
1831 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1837 empathy_chat_get_name (EmpathyChat *chat)
1839 EmpathyChatPriv *priv = GET_PRIV (chat);
1842 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1845 if (!ret && priv->remote_contact) {
1846 ret = empathy_contact_get_name (priv->remote_contact);
1852 return ret ? ret : _("Conversation");
1856 empathy_chat_get_subject (EmpathyChat *chat)
1858 EmpathyChatPriv *priv = GET_PRIV (chat);
1860 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1862 return priv->subject;
1866 empathy_chat_get_remote_contact (EmpathyChat *chat)
1868 EmpathyChatPriv *priv = GET_PRIV (chat);
1870 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1872 return priv->remote_contact;
1876 empathy_chat_get_contact_menu (EmpathyChat *chat)
1878 EmpathyChatPriv *priv = GET_PRIV (chat);
1879 GtkWidget *menu = NULL;
1881 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1883 if (priv->remote_contact) {
1884 menu = empathy_contact_menu_new (priv->remote_contact,
1885 EMPATHY_CONTACT_FEATURE_CALL |
1886 EMPATHY_CONTACT_FEATURE_LOG |
1887 EMPATHY_CONTACT_FEATURE_INFO);
1889 else if (priv->contact_list_view) {
1890 EmpathyContactListView *view;
1892 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1893 menu = empathy_contact_list_view_get_contact_menu (view);
1900 empathy_chat_clear (EmpathyChat *chat)
1902 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1904 empathy_chat_view_clear (chat->view);
1908 empathy_chat_scroll_down (EmpathyChat *chat)
1910 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1912 empathy_chat_view_scroll_down (chat->view);
1916 empathy_chat_cut (EmpathyChat *chat)
1918 GtkTextBuffer *buffer;
1920 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1922 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1923 if (gtk_text_buffer_get_has_selection (buffer)) {
1924 GtkClipboard *clipboard;
1926 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1928 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1933 empathy_chat_copy (EmpathyChat *chat)
1935 GtkTextBuffer *buffer;
1937 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1939 if (empathy_chat_view_get_has_selection (chat->view)) {
1940 empathy_chat_view_copy_clipboard (chat->view);
1944 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1945 if (gtk_text_buffer_get_has_selection (buffer)) {
1946 GtkClipboard *clipboard;
1948 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1950 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1955 empathy_chat_paste (EmpathyChat *chat)
1957 GtkTextBuffer *buffer;
1958 GtkClipboard *clipboard;
1960 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1962 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1963 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1965 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1969 empathy_chat_correct_word (EmpathyChat *chat,
1972 const gchar *new_word)
1974 GtkTextBuffer *buffer;
1976 g_return_if_fail (chat != NULL);
1977 g_return_if_fail (new_word != NULL);
1979 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1981 gtk_text_buffer_delete (buffer, start, end);
1982 gtk_text_buffer_insert (buffer, start,
1988 empathy_chat_is_room (EmpathyChat *chat)
1990 EmpathyChatPriv *priv = GET_PRIV (chat);
1992 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1994 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);