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;
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);
206 account = empathy_account_manager_get_account (manager, connection);
207 if (!priv->tp_chat && empathy_account_equal (account, priv->account) &&
208 priv->handle_type != TP_HANDLE_TYPE_NONE &&
209 !EMP_STR_EMPTY (priv->id)) {
211 DEBUG ("Account reconnected, request a new Text channel");
213 switch (priv->handle_type) {
214 case TP_HANDLE_TYPE_CONTACT:
215 empathy_dispatcher_chat_with_contact_id (
216 connection, priv->id,
217 chat_connect_channel_reconnected,
220 case TP_HANDLE_TYPE_ROOM:
221 empathy_dispatcher_join_muc (connection,
223 chat_connect_channel_reconnected,
227 g_assert_not_reached ();
234 chat_composing_remove_timeout (EmpathyChat *chat)
236 EmpathyChatPriv *priv;
238 priv = GET_PRIV (chat);
240 if (priv->composing_stop_timeout_id) {
241 g_source_remove (priv->composing_stop_timeout_id);
242 priv->composing_stop_timeout_id = 0;
247 chat_composing_stop_timeout_cb (EmpathyChat *chat)
249 EmpathyChatPriv *priv;
251 priv = GET_PRIV (chat);
253 priv->composing_stop_timeout_id = 0;
254 empathy_tp_chat_set_state (priv->tp_chat,
255 TP_CHANNEL_CHAT_STATE_PAUSED);
261 chat_composing_start (EmpathyChat *chat)
263 EmpathyChatPriv *priv;
265 priv = GET_PRIV (chat);
267 if (priv->composing_stop_timeout_id) {
268 /* Just restart the timeout */
269 chat_composing_remove_timeout (chat);
271 empathy_tp_chat_set_state (priv->tp_chat,
272 TP_CHANNEL_CHAT_STATE_COMPOSING);
275 priv->composing_stop_timeout_id = g_timeout_add_seconds (
276 COMPOSING_STOP_TIMEOUT,
277 (GSourceFunc) chat_composing_stop_timeout_cb,
282 chat_composing_stop (EmpathyChat *chat)
284 EmpathyChatPriv *priv;
286 priv = GET_PRIV (chat);
288 chat_composing_remove_timeout (chat);
289 empathy_tp_chat_set_state (priv->tp_chat,
290 TP_CHANNEL_CHAT_STATE_ACTIVE);
294 chat_sent_message_add (EmpathyChat *chat,
297 EmpathyChatPriv *priv;
301 priv = GET_PRIV (chat);
303 /* Save the sent message in our repeat buffer */
304 list = priv->sent_messages;
306 /* Remove any other occurances of this msg */
307 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
308 list = g_slist_remove_link (list, item);
310 g_slist_free1 (item);
313 /* Trim the list to the last 10 items */
314 while (g_slist_length (list) > 10) {
315 item = g_slist_last (list);
317 list = g_slist_remove_link (list, item);
319 g_slist_free1 (item);
323 /* Add new message */
324 list = g_slist_prepend (list, g_strdup (str));
326 /* Set list and reset the index */
327 priv->sent_messages = list;
328 priv->sent_messages_index = -1;
332 chat_sent_message_get_next (EmpathyChat *chat)
334 EmpathyChatPriv *priv;
337 priv = GET_PRIV (chat);
339 if (!priv->sent_messages) {
340 DEBUG ("No sent messages, next message is NULL");
344 max = g_slist_length (priv->sent_messages) - 1;
346 if (priv->sent_messages_index < max) {
347 priv->sent_messages_index++;
350 DEBUG ("Returning next message index:%d", priv->sent_messages_index);
352 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
356 chat_sent_message_get_last (EmpathyChat *chat)
358 EmpathyChatPriv *priv;
360 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
362 priv = GET_PRIV (chat);
364 if (!priv->sent_messages) {
365 DEBUG ("No sent messages, last message is NULL");
369 if (priv->sent_messages_index >= 0) {
370 priv->sent_messages_index--;
373 DEBUG ("Returning last message index:%d", priv->sent_messages_index);
375 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
379 chat_send (EmpathyChat *chat,
382 EmpathyChatPriv *priv;
383 EmpathyMessage *message;
385 if (EMP_STR_EMPTY (msg)) {
389 priv = GET_PRIV (chat);
391 chat_sent_message_add (chat, msg);
393 if (g_str_has_prefix (msg, "/clear")) {
394 empathy_chat_view_clear (chat->view);
398 /* Blacklist messages begining by '/', except for "/me" and "/say"
399 * because they are handled in EmpathyMessage */
401 !g_str_has_prefix (msg, "/me") &&
402 !g_str_has_prefix (msg, "/say")) {
403 empathy_chat_view_append_event (chat->view,
404 _("Unsupported command"));
408 /* We can send the message */
409 message = empathy_message_new (msg);
410 empathy_tp_chat_send (priv->tp_chat, message);
411 g_object_unref (message);
415 chat_input_text_view_send (EmpathyChat *chat)
417 EmpathyChatPriv *priv;
418 GtkTextBuffer *buffer;
419 GtkTextIter start, end;
422 priv = GET_PRIV (chat);
424 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
426 gtk_text_buffer_get_bounds (buffer, &start, &end);
427 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
429 /* clear the input field */
430 gtk_text_buffer_set_text (buffer, "", -1);
432 chat_send (chat, msg);
437 chat_state_changed_cb (EmpathyTpChat *tp_chat,
438 EmpathyContact *contact,
439 TpChannelChatState state,
442 EmpathyChatPriv *priv;
444 gboolean was_composing;
446 priv = GET_PRIV (chat);
448 if (empathy_contact_is_user (contact)) {
449 /* We don't care about our own chat state */
453 was_composing = (priv->compositors != NULL);
455 /* Find the contact in the list. After that l is the list elem or NULL */
456 for (l = priv->compositors; l; l = l->next) {
457 if (contact == l->data) {
463 case TP_CHANNEL_CHAT_STATE_GONE:
464 case TP_CHANNEL_CHAT_STATE_INACTIVE:
465 case TP_CHANNEL_CHAT_STATE_PAUSED:
466 case TP_CHANNEL_CHAT_STATE_ACTIVE:
467 /* Contact is not composing */
469 priv->compositors = g_list_remove_link (priv->compositors, l);
470 g_object_unref (l->data);
474 case TP_CHANNEL_CHAT_STATE_COMPOSING:
475 /* Contact is composing */
477 priv->compositors = g_list_prepend (priv->compositors,
478 g_object_ref (contact));
482 g_assert_not_reached ();
485 DEBUG ("Was composing: %s now composing: %s",
486 was_composing ? "yes" : "no",
487 priv->compositors ? "yes" : "no");
489 if ((was_composing && !priv->compositors) ||
490 (!was_composing && priv->compositors)) {
491 /* Composing state changed */
492 g_signal_emit (chat, signals[COMPOSING], 0,
493 priv->compositors != NULL);
498 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
500 EmpathyChatPriv *priv = GET_PRIV (chat);
501 EmpathyContact *sender;
503 sender = empathy_message_get_sender (message);
505 DEBUG ("Appending new message from %s (%d)",
506 empathy_contact_get_name (sender),
507 empathy_contact_get_handle (sender));
509 empathy_chat_view_append_message (chat->view, message);
511 /* We received a message so the contact is no longer composing */
512 chat_state_changed_cb (priv->tp_chat, sender,
513 TP_CHANNEL_CHAT_STATE_ACTIVE,
516 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
520 chat_message_received_cb (EmpathyTpChat *tp_chat,
521 EmpathyMessage *message,
524 chat_message_received (chat, message);
525 empathy_tp_chat_acknowledge_message (tp_chat, message);
529 chat_send_error_cb (EmpathyTpChat *tp_chat,
530 EmpathyMessage *message,
531 TpChannelTextSendError error_code,
537 switch (error_code) {
538 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
539 error = _("offline");
541 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
542 error = _("invalid contact");
544 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
545 error = _("permission denied");
547 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
548 error = _("too long message");
550 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
551 error = _("not implemented");
554 error = _("unknown");
558 str = g_strdup_printf (_("Error sending message '%s': %s"),
559 empathy_message_get_body (message),
561 empathy_chat_view_append_event (chat->view, str);
566 chat_property_changed_cb (EmpathyTpChat *tp_chat,
571 EmpathyChatPriv *priv = GET_PRIV (chat);
573 if (!tp_strdiff (name, "subject")) {
574 g_free (priv->subject);
575 priv->subject = g_value_dup_string (value);
576 g_object_notify (G_OBJECT (chat), "subject");
578 if (EMP_STR_EMPTY (priv->subject)) {
579 gtk_widget_hide (priv->hbox_topic);
581 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
582 gtk_widget_show (priv->hbox_topic);
584 if (priv->block_events_timeout_id == 0) {
587 if (!EMP_STR_EMPTY (priv->subject)) {
588 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
590 str = g_strdup (_("No topic defined"));
592 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
596 else if (!tp_strdiff (name, "name")) {
598 priv->name = g_value_dup_string (value);
599 g_object_notify (G_OBJECT (chat), "name");
604 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
607 EmpathyChatPriv *priv;
608 GtkTextIter start, end;
610 gboolean spell_checker = FALSE;
612 priv = GET_PRIV (chat);
614 if (gtk_text_buffer_get_char_count (buffer) == 0) {
615 chat_composing_stop (chat);
617 chat_composing_start (chat);
620 empathy_conf_get_bool (empathy_conf_get (),
621 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
624 gtk_text_buffer_get_start_iter (buffer, &start);
626 if (!spell_checker) {
627 gtk_text_buffer_get_end_iter (buffer, &end);
628 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
632 if (!empathy_spell_supported ()) {
636 /* NOTE: this is really inefficient, we shouldn't have to
637 reiterate the whole buffer each time and check each work
640 gboolean correct = FALSE;
643 if (gtk_text_iter_is_start (&start)) {
646 if (!gtk_text_iter_forward_word_end (&end)) {
647 /* no whole word yet */
651 if (!gtk_text_iter_forward_word_end (&end)) {
652 /* must be the end of the buffer */
657 gtk_text_iter_backward_word_start (&start);
660 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
662 /* spell check string if not a command */
664 correct = empathy_spell_check (str);
670 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
672 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
677 /* set start iter to the end iters position */
683 chat_input_key_press_event_cb (GtkWidget *widget,
687 EmpathyChatPriv *priv;
690 GtkWidget *text_view_sw;
692 priv = GET_PRIV (chat);
694 /* Catch ctrl+up/down so we can traverse messages we sent */
695 if ((event->state & GDK_CONTROL_MASK) &&
696 (event->keyval == GDK_Up ||
697 event->keyval == GDK_Down)) {
698 GtkTextBuffer *buffer;
701 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
703 if (event->keyval == GDK_Up) {
704 str = chat_sent_message_get_next (chat);
706 str = chat_sent_message_get_last (chat);
709 g_signal_handlers_block_by_func (buffer,
710 chat_input_text_buffer_changed_cb,
712 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
713 g_signal_handlers_unblock_by_func (buffer,
714 chat_input_text_buffer_changed_cb,
720 /* Catch enter but not ctrl/shift-enter */
721 if (IS_ENTER (event->keyval) &&
722 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
725 /* This is to make sure that kinput2 gets the enter. And if
726 * it's handled there we shouldn't send on it. This is because
727 * kinput2 uses Enter to commit letters. See:
728 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
731 view = GTK_TEXT_VIEW (chat->input_text_view);
732 if (gtk_im_context_filter_keypress (view->im_context, event)) {
733 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
737 chat_input_text_view_send (chat);
741 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
743 if (IS_ENTER (event->keyval) &&
744 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
745 /* Newline for shift/control-enter. */
748 if (!(event->state & GDK_CONTROL_MASK) &&
749 event->keyval == GDK_Page_Up) {
750 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
751 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
754 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
755 event->keyval == GDK_Page_Down) {
756 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
757 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
758 gtk_adjustment_set_value (adj, val);
761 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
762 event->keyval == GDK_Tab) {
763 GtkTextBuffer *buffer;
764 GtkTextIter start, current;
765 gchar *nick, *completed;
766 GList *list, *completed_list;
767 gboolean is_start_of_buffer;
769 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
770 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
772 /* Get the start of the nick to complete. */
773 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
774 gtk_text_iter_backward_word_start (&start);
775 is_start_of_buffer = gtk_text_iter_is_start (&start);
777 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
778 g_completion_add_items (priv->completion, list);
780 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
781 completed_list = g_completion_complete (priv->completion,
790 gchar *complete_char = NULL;
792 gtk_text_buffer_delete (buffer, &start, ¤t);
794 len = g_list_length (completed_list);
797 /* If we only have one hit, use that text
798 * instead of the text in completed since the
799 * completed text will use the typed string
800 * which might be cased all wrong.
803 text = empathy_contact_get_name (completed_list->data);
808 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
810 if (len == 1 && is_start_of_buffer &&
811 empathy_conf_get_string (empathy_conf_get (),
812 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
814 complete_char != NULL) {
815 gtk_text_buffer_insert_at_cursor (buffer,
817 strlen (complete_char));
818 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
819 g_free (complete_char);
825 g_completion_clear_items (priv->completion);
827 g_list_foreach (list, (GFunc) g_object_unref, NULL);
837 chat_text_view_focus_in_event_cb (GtkWidget *widget,
841 gtk_widget_grab_focus (chat->input_text_view);
847 chat_input_set_size_request_idle (gpointer sw)
849 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
855 chat_input_size_request_cb (GtkWidget *widget,
856 GtkRequisition *requisition,
859 EmpathyChatPriv *priv = GET_PRIV (chat);
862 sw = gtk_widget_get_parent (widget);
863 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
864 g_idle_add (chat_input_set_size_request_idle, sw);
865 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
868 priv->has_input_vscroll = TRUE;
871 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
872 gtk_widget_set_size_request (sw, -1, -1);
873 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
876 priv->has_input_vscroll = FALSE;
881 chat_input_realize_cb (GtkWidget *widget,
884 DEBUG ("Setting focus to the input text view");
885 gtk_widget_grab_focus (widget);
889 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
890 EmpathySmiley *smiley,
893 EmpathyChat *chat = EMPATHY_CHAT (user_data);
894 GtkTextBuffer *buffer;
897 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
899 gtk_text_buffer_get_end_iter (buffer, &iter);
900 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
902 gtk_text_buffer_get_end_iter (buffer, &iter);
903 gtk_text_buffer_insert (buffer, &iter, " ", -1);
914 static EmpathyChatSpell *
915 chat_spell_new (EmpathyChat *chat,
920 EmpathyChatSpell *chat_spell;
922 chat_spell = g_slice_new0 (EmpathyChatSpell);
924 chat_spell->chat = g_object_ref (chat);
925 chat_spell->word = g_strdup (word);
926 chat_spell->start = start;
927 chat_spell->end = end;
933 chat_spell_free (EmpathyChatSpell *chat_spell)
935 g_object_unref (chat_spell->chat);
936 g_free (chat_spell->word);
937 g_slice_free (EmpathyChatSpell, chat_spell);
941 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
942 EmpathyChatSpell *chat_spell)
944 empathy_chat_correct_word (chat_spell->chat,
945 &(chat_spell->start),
947 gtk_menu_item_get_label (menu_item));
951 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
953 GtkWidget *menu, *menu_item;
954 GList *suggestions, *l;
956 menu = gtk_menu_new ();
957 suggestions = empathy_spell_get_suggestions (chat_spell->word);
958 if (suggestions == NULL) {
959 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
960 gtk_widget_set_sensitive (menu_item, FALSE);
961 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
963 for (l = suggestions; l; l = l->next) {
964 menu_item = gtk_menu_item_new_with_label (l->data);
965 g_signal_connect (G_OBJECT (menu_item),
967 G_CALLBACK (chat_spelling_menu_activate_cb),
969 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
972 empathy_spell_free_suggestions (suggestions);
974 gtk_widget_show_all (menu);
980 chat_text_send_cb (GtkMenuItem *menuitem,
983 chat_input_text_view_send (chat);
987 chat_input_populate_popup_cb (GtkTextView *view,
991 EmpathyChatPriv *priv;
992 GtkTextBuffer *buffer;
993 GtkTextTagTable *table;
996 GtkTextIter iter, start, end;
999 EmpathyChatSpell *chat_spell;
1000 GtkWidget *spell_menu;
1001 EmpathySmileyManager *smiley_manager;
1002 GtkWidget *smiley_menu;
1005 priv = GET_PRIV (chat);
1006 buffer = gtk_text_view_get_buffer (view);
1008 /* Add the emoticon menu. */
1009 item = gtk_separator_menu_item_new ();
1010 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1011 gtk_widget_show (item);
1013 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1014 image = gtk_image_new_from_icon_name ("face-smile",
1015 GTK_ICON_SIZE_MENU);
1016 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1017 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1018 gtk_widget_show (item);
1020 smiley_manager = empathy_smiley_manager_dup_singleton ();
1021 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1022 chat_insert_smiley_activate_cb,
1024 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1025 g_object_unref (smiley_manager);
1027 /* Add the Send menu item. */
1028 gtk_text_buffer_get_bounds (buffer, &start, &end);
1029 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1030 if (!EMP_STR_EMPTY (str)) {
1031 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1032 g_signal_connect (G_OBJECT (item), "activate",
1033 G_CALLBACK (chat_text_send_cb), chat);
1034 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1035 gtk_widget_show (item);
1039 /* Add the spell check menu item. */
1040 table = gtk_text_buffer_get_tag_table (buffer);
1041 tag = gtk_text_tag_table_lookup (table, "misspelled");
1042 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1043 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1044 GTK_TEXT_WINDOW_WIDGET,
1047 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1049 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1050 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1052 str = gtk_text_buffer_get_text (buffer,
1053 &start, &end, FALSE);
1055 if (!EMP_STR_EMPTY (str)) {
1056 chat_spell = chat_spell_new (chat, str, start, end);
1057 g_object_set_data_full (G_OBJECT (menu),
1058 "chat_spell", chat_spell,
1059 (GDestroyNotify) chat_spell_free);
1061 item = gtk_separator_menu_item_new ();
1062 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1063 gtk_widget_show (item);
1065 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1066 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1067 GTK_ICON_SIZE_MENU);
1068 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1070 spell_menu = chat_spelling_build_menu (chat_spell);
1071 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1073 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1074 gtk_widget_show (item);
1079 chat_log_filter (EmpathyMessage *message,
1082 EmpathyChat *chat = (EmpathyChat *) user_data;
1083 EmpathyChatPriv *priv = GET_PRIV (chat);
1084 const GList *pending;
1086 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1088 for (; pending; pending = g_list_next (pending)) {
1089 if (empathy_message_equal (message, pending->data)) {
1098 chat_add_logs (EmpathyChat *chat)
1100 EmpathyChatPriv *priv = GET_PRIV (chat);
1101 gboolean is_chatroom;
1102 GList *messages, *l;
1108 /* Turn off scrolling temporarily */
1109 empathy_chat_view_scroll (chat->view, FALSE);
1111 /* Add messages from last conversation */
1112 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1114 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1122 for (l = messages; l; l = g_list_next (l)) {
1123 empathy_chat_view_append_message (chat->view, l->data);
1124 g_object_unref (l->data);
1127 g_list_free (messages);
1129 /* Turn back on scrolling */
1130 empathy_chat_view_scroll (chat->view, TRUE);
1134 chat_contacts_completion_func (const gchar *s1,
1138 gchar *tmp, *nick1, *nick2;
1145 return s1 ? -1 : +1;
1148 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1149 nick1 = g_utf8_casefold (tmp, -1);
1152 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1153 nick2 = g_utf8_casefold (tmp, -1);
1156 ret = strncmp (nick1, nick2, n);
1165 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1166 EmpathyContact *contact,
1167 EmpathyContact *actor,
1173 EmpathyChatPriv *priv = GET_PRIV (chat);
1175 if (priv->block_events_timeout_id == 0) {
1179 str = g_strdup_printf (_("%s has joined the room"),
1180 empathy_contact_get_name (contact));
1182 str = g_strdup_printf (_("%s has left the room"),
1183 empathy_contact_get_name (contact));
1185 empathy_chat_view_append_event (chat->view, str);
1191 chat_reset_size_request (gpointer widget)
1193 gtk_widget_set_size_request (widget, -1, -1);
1199 chat_update_contacts_visibility (EmpathyChat *chat)
1201 EmpathyChatPriv *priv = GET_PRIV (chat);
1204 show = priv->remote_contact == NULL && priv->show_contacts;
1206 if (!priv->scrolled_window_contacts) {
1210 if (show && priv->contact_list_view == NULL) {
1211 EmpathyContactListStore *store;
1214 /* We are adding the contact list to the chat, we don't want the
1215 * chat view to become too small. If the chat view is already
1216 * smaller than 250 make sure that size won't change. If the
1217 * chat view is bigger the contact list will take some space on
1218 * it but we make sure the chat view don't become smaller than
1219 * 250. Relax the size request once the resize is done */
1220 min_width = MIN (priv->vbox_left->allocation.width, 250);
1221 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1222 g_idle_add (chat_reset_size_request, priv->vbox_left);
1224 if (priv->contacts_width > 0) {
1225 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1226 priv->contacts_width);
1229 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1230 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1231 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1232 EMPATHY_CONTACT_FEATURE_CHAT |
1233 EMPATHY_CONTACT_FEATURE_CALL |
1234 EMPATHY_CONTACT_FEATURE_LOG |
1235 EMPATHY_CONTACT_FEATURE_INFO));
1236 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1237 priv->contact_list_view);
1238 gtk_widget_show (priv->contact_list_view);
1239 gtk_widget_show (priv->scrolled_window_contacts);
1240 g_object_unref (store);
1242 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1243 gtk_widget_hide (priv->scrolled_window_contacts);
1244 if (priv->contact_list_view != NULL) {
1245 gtk_widget_destroy (priv->contact_list_view);
1246 priv->contact_list_view = NULL;
1252 empathy_chat_set_show_contacts (EmpathyChat *chat,
1255 EmpathyChatPriv *priv = GET_PRIV (chat);
1257 priv->show_contacts = show;
1259 chat_update_contacts_visibility (chat);
1261 g_object_notify (G_OBJECT (chat), "show-contacts");
1265 chat_remote_contact_changed_cb (EmpathyChat *chat)
1267 EmpathyChatPriv *priv = GET_PRIV (chat);
1269 if (priv->remote_contact != NULL) {
1270 g_object_unref (priv->remote_contact);
1271 priv->remote_contact = NULL;
1274 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1275 if (priv->remote_contact != NULL) {
1276 g_object_ref (priv->remote_contact);
1277 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1279 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1281 else if (priv->tp_chat != NULL) {
1284 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1285 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1287 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1290 chat_update_contacts_visibility (chat);
1292 g_object_notify (G_OBJECT (chat), "remote-contact");
1293 g_object_notify (G_OBJECT (chat), "id");
1297 chat_destroy_cb (EmpathyTpChat *tp_chat,
1300 EmpathyChatPriv *priv;
1302 priv = GET_PRIV (chat);
1304 if (!priv->tp_chat) {
1308 chat_composing_remove_timeout (chat);
1309 g_object_unref (priv->tp_chat);
1310 priv->tp_chat = NULL;
1311 g_object_notify (G_OBJECT (chat), "tp-chat");
1313 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1314 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1315 empathy_chat_set_show_contacts (chat, FALSE);
1319 show_pending_messages (EmpathyChat *chat) {
1320 EmpathyChatPriv *priv = GET_PRIV (chat);
1321 const GList *messages, *l;
1323 if (chat->view == NULL || priv->tp_chat == NULL)
1326 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1328 for (l = messages; l != NULL ; l = g_list_next (l)) {
1329 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1330 chat_message_received (chat, message);
1332 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1336 chat_create_ui (EmpathyChat *chat)
1338 EmpathyChatPriv *priv = GET_PRIV (chat);
1342 GtkTextBuffer *buffer;
1344 filename = empathy_file_lookup ("empathy-chat.ui",
1346 gui = empathy_builder_get_file (filename,
1347 "chat_widget", &priv->widget,
1348 "hpaned", &priv->hpaned,
1349 "vbox_left", &priv->vbox_left,
1350 "scrolled_window_chat", &priv->scrolled_window_chat,
1351 "scrolled_window_input", &priv->scrolled_window_input,
1352 "hbox_topic", &priv->hbox_topic,
1353 "label_topic", &priv->label_topic,
1354 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1358 /* Add message view. */
1359 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1360 g_signal_connect (chat->view, "focus_in_event",
1361 G_CALLBACK (chat_text_view_focus_in_event_cb),
1363 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1364 GTK_WIDGET (chat->view));
1365 gtk_widget_show (GTK_WIDGET (chat->view));
1367 /* Add input GtkTextView */
1368 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1369 "pixels-above-lines", 2,
1370 "pixels-below-lines", 2,
1371 "pixels-inside-wrap", 1,
1374 "wrap-mode", GTK_WRAP_WORD_CHAR,
1376 g_signal_connect (chat->input_text_view, "key-press-event",
1377 G_CALLBACK (chat_input_key_press_event_cb),
1379 g_signal_connect (chat->input_text_view, "size-request",
1380 G_CALLBACK (chat_input_size_request_cb),
1382 g_signal_connect (chat->input_text_view, "realize",
1383 G_CALLBACK (chat_input_realize_cb),
1385 g_signal_connect (chat->input_text_view, "populate-popup",
1386 G_CALLBACK (chat_input_populate_popup_cb),
1388 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1389 g_signal_connect (buffer, "changed",
1390 G_CALLBACK (chat_input_text_buffer_changed_cb),
1392 gtk_text_buffer_create_tag (buffer, "misspelled",
1393 "underline", PANGO_UNDERLINE_ERROR,
1395 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1396 chat->input_text_view);
1397 gtk_widget_show (chat->input_text_view);
1399 /* Create contact list */
1400 chat_update_contacts_visibility (chat);
1402 /* Initialy hide the topic, will be shown if not empty */
1403 gtk_widget_hide (priv->hbox_topic);
1405 /* Set widget focus order */
1406 list = g_list_append (NULL, priv->scrolled_window_input);
1407 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1410 list = g_list_append (NULL, priv->vbox_left);
1411 list = g_list_append (list, priv->scrolled_window_contacts);
1412 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1415 list = g_list_append (NULL, priv->hpaned);
1416 list = g_list_append (list, priv->hbox_topic);
1417 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1420 /* Add the main widget in the chat widget */
1421 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1422 g_object_unref (gui);
1426 chat_size_request (GtkWidget *widget,
1427 GtkRequisition *requisition)
1429 GtkBin *bin = GTK_BIN (widget);
1431 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1432 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1434 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1436 GtkRequisition child_requisition;
1438 gtk_widget_size_request (bin->child, &child_requisition);
1440 requisition->width += child_requisition.width;
1441 requisition->height += child_requisition.height;
1446 chat_size_allocate (GtkWidget *widget,
1447 GtkAllocation *allocation)
1449 GtkBin *bin = GTK_BIN (widget);
1450 GtkAllocation child_allocation;
1452 widget->allocation = *allocation;
1454 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1456 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1457 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1458 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1459 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1461 gtk_widget_size_allocate (bin->child, &child_allocation);
1466 chat_finalize (GObject *object)
1469 EmpathyChatPriv *priv;
1471 chat = EMPATHY_CHAT (object);
1472 priv = GET_PRIV (chat);
1474 DEBUG ("Finalized: %p", object);
1476 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1477 g_slist_free (priv->sent_messages);
1479 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1480 g_list_free (priv->compositors);
1482 chat_composing_remove_timeout (chat);
1484 g_signal_handlers_disconnect_by_func (priv->account_manager,
1485 chat_new_connection_cb, object);
1487 g_object_unref (priv->account_manager);
1488 g_object_unref (priv->log_manager);
1490 if (priv->tp_chat) {
1491 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1492 chat_destroy_cb, chat);
1493 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1494 chat_message_received_cb, chat);
1495 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1496 chat_send_error_cb, chat);
1497 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1498 chat_state_changed_cb, chat);
1499 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1500 chat_property_changed_cb, chat);
1501 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1502 chat_members_changed_cb, chat);
1503 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1504 chat_remote_contact_changed_cb, chat);
1505 empathy_tp_chat_close (priv->tp_chat);
1506 g_object_unref (priv->tp_chat);
1508 if (priv->account) {
1509 g_object_unref (priv->account);
1511 if (priv->remote_contact) {
1512 g_object_unref (priv->remote_contact);
1515 if (priv->block_events_timeout_id) {
1516 g_source_remove (priv->block_events_timeout_id);
1520 g_free (priv->name);
1521 g_free (priv->subject);
1522 g_completion_free (priv->completion);
1524 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1528 chat_constructed (GObject *object)
1530 EmpathyChat *chat = EMPATHY_CHAT (object);
1532 chat_create_ui (chat);
1533 chat_add_logs (chat);
1534 show_pending_messages (chat);
1538 empathy_chat_class_init (EmpathyChatClass *klass)
1540 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1541 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1543 object_class->finalize = chat_finalize;
1544 object_class->get_property = chat_get_property;
1545 object_class->set_property = chat_set_property;
1546 object_class->constructed = chat_constructed;
1548 widget_class->size_request = chat_size_request;
1549 widget_class->size_allocate = chat_size_allocate;
1551 g_object_class_install_property (object_class,
1553 g_param_spec_object ("tp-chat",
1555 "The tp chat object",
1556 EMPATHY_TYPE_TP_CHAT,
1559 G_PARAM_STATIC_STRINGS));
1560 g_object_class_install_property (object_class,
1562 g_param_spec_object ("account",
1563 "Account of the chat",
1564 "The account of the chat",
1567 G_PARAM_STATIC_STRINGS));
1568 g_object_class_install_property (object_class,
1570 g_param_spec_string ("id",
1572 "The id of the chat",
1575 G_PARAM_STATIC_STRINGS));
1576 g_object_class_install_property (object_class,
1578 g_param_spec_string ("name",
1580 "The name of the chat",
1583 G_PARAM_STATIC_STRINGS));
1584 g_object_class_install_property (object_class,
1586 g_param_spec_string ("subject",
1588 "The subject or topic of the chat",
1591 G_PARAM_STATIC_STRINGS));
1592 g_object_class_install_property (object_class,
1593 PROP_REMOTE_CONTACT,
1594 g_param_spec_object ("remote-contact",
1595 "The remote contact",
1596 "The remote contact is any",
1597 EMPATHY_TYPE_CONTACT,
1599 G_PARAM_STATIC_STRINGS));
1600 g_object_class_install_property (object_class,
1602 g_param_spec_boolean ("show-contacts",
1603 "Contacts' visibility",
1604 "The visibility of the contacts' list",
1607 G_PARAM_STATIC_STRINGS));
1609 signals[COMPOSING] =
1610 g_signal_new ("composing",
1611 G_OBJECT_CLASS_TYPE (object_class),
1615 g_cclosure_marshal_VOID__BOOLEAN,
1619 signals[NEW_MESSAGE] =
1620 g_signal_new ("new-message",
1621 G_OBJECT_CLASS_TYPE (object_class),
1625 g_cclosure_marshal_VOID__OBJECT,
1627 1, EMPATHY_TYPE_MESSAGE);
1629 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1633 chat_block_events_timeout_cb (gpointer data)
1635 EmpathyChatPriv *priv = GET_PRIV (data);
1637 priv->block_events_timeout_id = 0;
1643 empathy_chat_init (EmpathyChat *chat)
1645 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1646 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1649 priv->log_manager = empathy_log_manager_dup_singleton ();
1650 priv->contacts_width = -1;
1651 priv->sent_messages = NULL;
1652 priv->sent_messages_index = -1;
1653 priv->account_manager = empathy_account_manager_dup_singleton ();
1655 g_signal_connect (priv->account_manager,
1657 G_CALLBACK (chat_new_connection_cb),
1660 empathy_conf_get_bool (empathy_conf_get (),
1661 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1662 &priv->show_contacts);
1664 /* Block events for some time to avoid having "has come online" or
1665 * "joined" messages. */
1666 priv->block_events_timeout_id =
1667 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1669 /* Add nick name completion */
1670 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1671 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1675 empathy_chat_new (EmpathyTpChat *tp_chat)
1677 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1681 empathy_chat_get_tp_chat (EmpathyChat *chat)
1683 EmpathyChatPriv *priv = GET_PRIV (chat);
1685 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1687 return priv->tp_chat;
1691 empathy_chat_set_tp_chat (EmpathyChat *chat,
1692 EmpathyTpChat *tp_chat)
1694 EmpathyChatPriv *priv = GET_PRIV (chat);
1695 TpConnection *connection;
1697 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1698 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1699 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1701 if (priv->tp_chat) {
1705 if (priv->account) {
1706 g_object_unref (priv->account);
1709 priv->tp_chat = g_object_ref (tp_chat);
1710 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1711 priv->account = empathy_account_manager_get_account (priv->account_manager,
1713 g_object_ref (priv->account);
1715 g_signal_connect (tp_chat, "destroy",
1716 G_CALLBACK (chat_destroy_cb),
1718 g_signal_connect (tp_chat, "message-received",
1719 G_CALLBACK (chat_message_received_cb),
1721 g_signal_connect (tp_chat, "send-error",
1722 G_CALLBACK (chat_send_error_cb),
1724 g_signal_connect (tp_chat, "chat-state-changed",
1725 G_CALLBACK (chat_state_changed_cb),
1727 g_signal_connect (tp_chat, "property-changed",
1728 G_CALLBACK (chat_property_changed_cb),
1730 g_signal_connect (tp_chat, "members-changed",
1731 G_CALLBACK (chat_members_changed_cb),
1733 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1734 G_CALLBACK (chat_remote_contact_changed_cb),
1737 chat_remote_contact_changed_cb (chat);
1739 if (chat->input_text_view) {
1740 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1741 if (priv->block_events_timeout_id == 0) {
1742 empathy_chat_view_append_event (chat->view, _("Connected"));
1746 g_object_notify (G_OBJECT (chat), "tp-chat");
1747 g_object_notify (G_OBJECT (chat), "id");
1748 g_object_notify (G_OBJECT (chat), "account");
1750 /* This is a noop when tp-chat is set at object construction time and causes
1751 * the pending messages to be show when it's set on the object after it has
1753 show_pending_messages (chat);
1757 empathy_chat_get_account (EmpathyChat *chat)
1759 EmpathyChatPriv *priv = GET_PRIV (chat);
1761 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1763 return priv->account;
1767 empathy_chat_get_id (EmpathyChat *chat)
1769 EmpathyChatPriv *priv = GET_PRIV (chat);
1771 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1777 empathy_chat_get_name (EmpathyChat *chat)
1779 EmpathyChatPriv *priv = GET_PRIV (chat);
1782 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1785 if (!ret && priv->remote_contact) {
1786 ret = empathy_contact_get_name (priv->remote_contact);
1792 return ret ? ret : _("Conversation");
1796 empathy_chat_get_subject (EmpathyChat *chat)
1798 EmpathyChatPriv *priv = GET_PRIV (chat);
1800 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1802 return priv->subject;
1806 empathy_chat_get_remote_contact (EmpathyChat *chat)
1808 EmpathyChatPriv *priv = GET_PRIV (chat);
1810 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1812 return priv->remote_contact;
1816 empathy_chat_get_contact_menu (EmpathyChat *chat)
1818 EmpathyChatPriv *priv = GET_PRIV (chat);
1819 GtkWidget *menu = NULL;
1821 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1823 if (priv->remote_contact) {
1824 menu = empathy_contact_menu_new (priv->remote_contact,
1825 EMPATHY_CONTACT_FEATURE_CALL |
1826 EMPATHY_CONTACT_FEATURE_LOG |
1827 EMPATHY_CONTACT_FEATURE_INFO);
1829 else if (priv->contact_list_view) {
1830 EmpathyContactListView *view;
1832 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1833 menu = empathy_contact_list_view_get_contact_menu (view);
1840 empathy_chat_clear (EmpathyChat *chat)
1842 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1844 empathy_chat_view_clear (chat->view);
1848 empathy_chat_scroll_down (EmpathyChat *chat)
1850 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1852 empathy_chat_view_scroll_down (chat->view);
1856 empathy_chat_cut (EmpathyChat *chat)
1858 GtkTextBuffer *buffer;
1860 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1862 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1863 if (gtk_text_buffer_get_has_selection (buffer)) {
1864 GtkClipboard *clipboard;
1866 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1868 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1873 empathy_chat_copy (EmpathyChat *chat)
1875 GtkTextBuffer *buffer;
1877 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1879 if (empathy_chat_view_get_has_selection (chat->view)) {
1880 empathy_chat_view_copy_clipboard (chat->view);
1884 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1885 if (gtk_text_buffer_get_has_selection (buffer)) {
1886 GtkClipboard *clipboard;
1888 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1890 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1895 empathy_chat_paste (EmpathyChat *chat)
1897 GtkTextBuffer *buffer;
1898 GtkClipboard *clipboard;
1900 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1902 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1903 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1905 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1909 empathy_chat_correct_word (EmpathyChat *chat,
1912 const gchar *new_word)
1914 GtkTextBuffer *buffer;
1916 g_return_if_fail (chat != NULL);
1917 g_return_if_fail (new_word != NULL);
1919 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1921 gtk_text_buffer_delete (buffer, start, end);
1922 gtk_text_buffer_insert (buffer, start,
1928 empathy_chat_is_room (EmpathyChat *chat)
1930 EmpathyChatPriv *priv = GET_PRIV (chat);
1932 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1934 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);