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 (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, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
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 (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
758 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
759 gtk_adjustment_set_value (adj, val);
762 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
763 event->keyval == GDK_Tab) {
764 GtkTextBuffer *buffer;
765 GtkTextIter start, current;
766 gchar *nick, *completed;
767 GList *list, *completed_list;
768 gboolean is_start_of_buffer;
770 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
771 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
773 /* Get the start of the nick to complete. */
774 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
775 gtk_text_iter_backward_word_start (&start);
776 is_start_of_buffer = gtk_text_iter_is_start (&start);
778 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
779 g_completion_add_items (priv->completion, list);
781 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
782 completed_list = g_completion_complete (priv->completion,
791 gchar *complete_char = NULL;
793 gtk_text_buffer_delete (buffer, &start, ¤t);
795 len = g_list_length (completed_list);
798 /* If we only have one hit, use that text
799 * instead of the text in completed since the
800 * completed text will use the typed string
801 * which might be cased all wrong.
804 text = empathy_contact_get_name (completed_list->data);
809 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
811 if (len == 1 && is_start_of_buffer &&
812 empathy_conf_get_string (empathy_conf_get (),
813 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
815 complete_char != NULL) {
816 gtk_text_buffer_insert_at_cursor (buffer,
818 strlen (complete_char));
819 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
820 g_free (complete_char);
826 g_completion_clear_items (priv->completion);
828 g_list_foreach (list, (GFunc) g_object_unref, NULL);
838 chat_text_view_focus_in_event_cb (GtkWidget *widget,
842 gtk_widget_grab_focus (chat->input_text_view);
848 chat_input_set_size_request_idle (gpointer sw)
850 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
856 chat_input_size_request_cb (GtkWidget *widget,
857 GtkRequisition *requisition,
860 EmpathyChatPriv *priv = GET_PRIV (chat);
863 sw = gtk_widget_get_parent (widget);
864 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
865 g_idle_add (chat_input_set_size_request_idle, sw);
866 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
869 priv->has_input_vscroll = TRUE;
872 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
873 gtk_widget_set_size_request (sw, -1, -1);
874 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
877 priv->has_input_vscroll = FALSE;
882 chat_input_realize_cb (GtkWidget *widget,
885 DEBUG ("Setting focus to the input text view");
886 gtk_widget_grab_focus (widget);
890 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
891 EmpathySmiley *smiley,
894 EmpathyChat *chat = EMPATHY_CHAT (user_data);
895 GtkTextBuffer *buffer;
898 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
900 gtk_text_buffer_get_end_iter (buffer, &iter);
901 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
903 gtk_text_buffer_get_end_iter (buffer, &iter);
904 gtk_text_buffer_insert (buffer, &iter, " ", -1);
915 static EmpathyChatSpell *
916 chat_spell_new (EmpathyChat *chat,
921 EmpathyChatSpell *chat_spell;
923 chat_spell = g_slice_new0 (EmpathyChatSpell);
925 chat_spell->chat = g_object_ref (chat);
926 chat_spell->word = g_strdup (word);
927 chat_spell->start = start;
928 chat_spell->end = end;
934 chat_spell_free (EmpathyChatSpell *chat_spell)
936 g_object_unref (chat_spell->chat);
937 g_free (chat_spell->word);
938 g_slice_free (EmpathyChatSpell, chat_spell);
942 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
943 EmpathyChatSpell *chat_spell)
945 empathy_chat_correct_word (chat_spell->chat,
946 &(chat_spell->start),
948 gtk_menu_item_get_label (menu_item));
952 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
954 GtkWidget *menu, *menu_item;
955 GList *suggestions, *l;
957 menu = gtk_menu_new ();
958 suggestions = empathy_spell_get_suggestions (chat_spell->word);
959 if (suggestions == NULL) {
960 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
961 gtk_widget_set_sensitive (menu_item, FALSE);
962 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
964 for (l = suggestions; l; l = l->next) {
965 menu_item = gtk_menu_item_new_with_label (l->data);
966 g_signal_connect (G_OBJECT (menu_item),
968 G_CALLBACK (chat_spelling_menu_activate_cb),
970 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
973 empathy_spell_free_suggestions (suggestions);
975 gtk_widget_show_all (menu);
981 chat_text_send_cb (GtkMenuItem *menuitem,
984 chat_input_text_view_send (chat);
988 chat_input_populate_popup_cb (GtkTextView *view,
992 EmpathyChatPriv *priv;
993 GtkTextBuffer *buffer;
994 GtkTextTagTable *table;
997 GtkTextIter iter, start, end;
1000 EmpathyChatSpell *chat_spell;
1001 GtkWidget *spell_menu;
1002 EmpathySmileyManager *smiley_manager;
1003 GtkWidget *smiley_menu;
1006 priv = GET_PRIV (chat);
1007 buffer = gtk_text_view_get_buffer (view);
1009 /* Add the emoticon menu. */
1010 item = gtk_separator_menu_item_new ();
1011 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1012 gtk_widget_show (item);
1014 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1015 image = gtk_image_new_from_icon_name ("face-smile",
1016 GTK_ICON_SIZE_MENU);
1017 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1018 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1019 gtk_widget_show (item);
1021 smiley_manager = empathy_smiley_manager_dup_singleton ();
1022 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1023 chat_insert_smiley_activate_cb,
1025 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1026 g_object_unref (smiley_manager);
1028 /* Add the Send menu item. */
1029 gtk_text_buffer_get_bounds (buffer, &start, &end);
1030 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1031 if (!EMP_STR_EMPTY (str)) {
1032 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1033 g_signal_connect (G_OBJECT (item), "activate",
1034 G_CALLBACK (chat_text_send_cb), chat);
1035 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1036 gtk_widget_show (item);
1040 /* Add the spell check menu item. */
1041 table = gtk_text_buffer_get_tag_table (buffer);
1042 tag = gtk_text_tag_table_lookup (table, "misspelled");
1043 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1044 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1045 GTK_TEXT_WINDOW_WIDGET,
1048 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1050 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1051 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1053 str = gtk_text_buffer_get_text (buffer,
1054 &start, &end, FALSE);
1056 if (!EMP_STR_EMPTY (str)) {
1057 chat_spell = chat_spell_new (chat, str, start, end);
1058 g_object_set_data_full (G_OBJECT (menu),
1059 "chat_spell", chat_spell,
1060 (GDestroyNotify) chat_spell_free);
1062 item = gtk_separator_menu_item_new ();
1063 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1064 gtk_widget_show (item);
1066 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1067 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1068 GTK_ICON_SIZE_MENU);
1069 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1071 spell_menu = chat_spelling_build_menu (chat_spell);
1072 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1074 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1075 gtk_widget_show (item);
1080 chat_log_filter (EmpathyMessage *message,
1083 EmpathyChat *chat = (EmpathyChat *) user_data;
1084 EmpathyChatPriv *priv = GET_PRIV (chat);
1085 const GList *pending;
1087 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1089 for (; pending; pending = g_list_next (pending)) {
1090 if (empathy_message_equal (message, pending->data)) {
1099 chat_add_logs (EmpathyChat *chat)
1101 EmpathyChatPriv *priv = GET_PRIV (chat);
1102 gboolean is_chatroom;
1103 GList *messages, *l;
1109 /* Turn off scrolling temporarily */
1110 empathy_chat_view_scroll (chat->view, FALSE);
1112 /* Add messages from last conversation */
1113 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1115 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1123 for (l = messages; l; l = g_list_next (l)) {
1124 empathy_chat_view_append_message (chat->view, l->data);
1125 g_object_unref (l->data);
1128 g_list_free (messages);
1130 /* Turn back on scrolling */
1131 empathy_chat_view_scroll (chat->view, TRUE);
1135 chat_contacts_completion_func (const gchar *s1,
1139 gchar *tmp, *nick1, *nick2;
1146 return s1 ? -1 : +1;
1149 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1150 nick1 = g_utf8_casefold (tmp, -1);
1153 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1154 nick2 = g_utf8_casefold (tmp, -1);
1157 ret = strncmp (nick1, nick2, n);
1166 build_part_message (guint reason,
1168 EmpathyContact *actor,
1169 const gchar *message)
1171 GString *s = g_string_new ("");
1172 const gchar *actor_name = NULL;
1174 if (actor != NULL) {
1175 actor_name = empathy_contact_get_name (actor);
1178 /* Having an actor only really makes sense for a few actions... */
1180 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1181 g_string_append_printf (s, _("%s has disconnected"), name);
1183 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1184 if (actor_name != NULL) {
1185 g_string_append_printf (s, _("%s was kicked by %s"),
1188 g_string_append_printf (s, _("%s was kicked"), name);
1191 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1192 if (actor_name != NULL) {
1193 g_string_append_printf (s, _("%s was banned by %s"),
1196 g_string_append_printf (s, _("%s was banned"), name);
1200 g_string_append_printf (s, _("%s has left the room"), name);
1203 if (!EMP_STR_EMPTY (message)) {
1204 /* Note to translators: this string is appended to
1205 * notifications like "foo has left the room", with the message
1206 * given by the user living the room. If this poses a problem,
1207 * please let us know. :-)
1209 g_string_append_printf (s, _(" (%s)"), message);
1212 return g_string_free (s, FALSE);
1216 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1217 EmpathyContact *contact,
1218 EmpathyContact *actor,
1224 EmpathyChatPriv *priv = GET_PRIV (chat);
1225 const gchar *name = empathy_contact_get_name (contact);
1228 if (priv->block_events_timeout_id != 0)
1232 str = g_strdup_printf (_("%s has joined the room"),
1235 str = build_part_message (reason, name, actor, message);
1238 empathy_chat_view_append_event (chat->view, str);
1243 chat_reset_size_request (gpointer widget)
1245 gtk_widget_set_size_request (widget, -1, -1);
1251 chat_update_contacts_visibility (EmpathyChat *chat)
1253 EmpathyChatPriv *priv = GET_PRIV (chat);
1256 show = priv->remote_contact == NULL && priv->show_contacts;
1258 if (!priv->scrolled_window_contacts) {
1262 if (show && priv->contact_list_view == NULL) {
1263 EmpathyContactListStore *store;
1266 /* We are adding the contact list to the chat, we don't want the
1267 * chat view to become too small. If the chat view is already
1268 * smaller than 250 make sure that size won't change. If the
1269 * chat view is bigger the contact list will take some space on
1270 * it but we make sure the chat view don't become smaller than
1271 * 250. Relax the size request once the resize is done */
1272 min_width = MIN (priv->vbox_left->allocation.width, 250);
1273 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1274 g_idle_add (chat_reset_size_request, priv->vbox_left);
1276 if (priv->contacts_width > 0) {
1277 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1278 priv->contacts_width);
1281 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1282 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1283 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1284 EMPATHY_CONTACT_FEATURE_CHAT |
1285 EMPATHY_CONTACT_FEATURE_CALL |
1286 EMPATHY_CONTACT_FEATURE_LOG |
1287 EMPATHY_CONTACT_FEATURE_INFO));
1288 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1289 priv->contact_list_view);
1290 gtk_widget_show (priv->contact_list_view);
1291 gtk_widget_show (priv->scrolled_window_contacts);
1292 g_object_unref (store);
1294 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1295 gtk_widget_hide (priv->scrolled_window_contacts);
1296 if (priv->contact_list_view != NULL) {
1297 gtk_widget_destroy (priv->contact_list_view);
1298 priv->contact_list_view = NULL;
1304 empathy_chat_set_show_contacts (EmpathyChat *chat,
1307 EmpathyChatPriv *priv = GET_PRIV (chat);
1309 priv->show_contacts = show;
1311 chat_update_contacts_visibility (chat);
1313 g_object_notify (G_OBJECT (chat), "show-contacts");
1317 chat_remote_contact_changed_cb (EmpathyChat *chat)
1319 EmpathyChatPriv *priv = GET_PRIV (chat);
1321 if (priv->remote_contact != NULL) {
1322 g_object_unref (priv->remote_contact);
1323 priv->remote_contact = NULL;
1326 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1327 if (priv->remote_contact != NULL) {
1328 g_object_ref (priv->remote_contact);
1329 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1331 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1333 else if (priv->tp_chat != NULL) {
1336 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1337 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1339 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1342 chat_update_contacts_visibility (chat);
1344 g_object_notify (G_OBJECT (chat), "remote-contact");
1345 g_object_notify (G_OBJECT (chat), "id");
1349 chat_destroy_cb (EmpathyTpChat *tp_chat,
1352 EmpathyChatPriv *priv;
1354 priv = GET_PRIV (chat);
1356 if (!priv->tp_chat) {
1360 chat_composing_remove_timeout (chat);
1361 g_object_unref (priv->tp_chat);
1362 priv->tp_chat = NULL;
1363 g_object_notify (G_OBJECT (chat), "tp-chat");
1365 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1366 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1367 empathy_chat_set_show_contacts (chat, FALSE);
1371 show_pending_messages (EmpathyChat *chat) {
1372 EmpathyChatPriv *priv = GET_PRIV (chat);
1373 const GList *messages, *l;
1375 if (chat->view == NULL || priv->tp_chat == NULL)
1378 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1380 for (l = messages; l != NULL ; l = g_list_next (l)) {
1381 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1382 chat_message_received (chat, message);
1384 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1388 chat_create_ui (EmpathyChat *chat)
1390 EmpathyChatPriv *priv = GET_PRIV (chat);
1394 GtkTextBuffer *buffer;
1396 filename = empathy_file_lookup ("empathy-chat.ui",
1398 gui = empathy_builder_get_file (filename,
1399 "chat_widget", &priv->widget,
1400 "hpaned", &priv->hpaned,
1401 "vbox_left", &priv->vbox_left,
1402 "scrolled_window_chat", &priv->scrolled_window_chat,
1403 "scrolled_window_input", &priv->scrolled_window_input,
1404 "hbox_topic", &priv->hbox_topic,
1405 "label_topic", &priv->label_topic,
1406 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1410 /* Add message view. */
1411 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1412 g_signal_connect (chat->view, "focus_in_event",
1413 G_CALLBACK (chat_text_view_focus_in_event_cb),
1415 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1416 GTK_WIDGET (chat->view));
1417 gtk_widget_show (GTK_WIDGET (chat->view));
1419 /* Add input GtkTextView */
1420 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1421 "pixels-above-lines", 2,
1422 "pixels-below-lines", 2,
1423 "pixels-inside-wrap", 1,
1426 "wrap-mode", GTK_WRAP_WORD_CHAR,
1428 g_signal_connect (chat->input_text_view, "key-press-event",
1429 G_CALLBACK (chat_input_key_press_event_cb),
1431 g_signal_connect (chat->input_text_view, "size-request",
1432 G_CALLBACK (chat_input_size_request_cb),
1434 g_signal_connect (chat->input_text_view, "realize",
1435 G_CALLBACK (chat_input_realize_cb),
1437 g_signal_connect (chat->input_text_view, "populate-popup",
1438 G_CALLBACK (chat_input_populate_popup_cb),
1440 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1441 g_signal_connect (buffer, "changed",
1442 G_CALLBACK (chat_input_text_buffer_changed_cb),
1444 gtk_text_buffer_create_tag (buffer, "misspelled",
1445 "underline", PANGO_UNDERLINE_ERROR,
1447 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1448 chat->input_text_view);
1449 gtk_widget_show (chat->input_text_view);
1451 /* Create contact list */
1452 chat_update_contacts_visibility (chat);
1454 /* Initialy hide the topic, will be shown if not empty */
1455 gtk_widget_hide (priv->hbox_topic);
1457 /* Set widget focus order */
1458 list = g_list_append (NULL, priv->scrolled_window_input);
1459 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1462 list = g_list_append (NULL, priv->vbox_left);
1463 list = g_list_append (list, priv->scrolled_window_contacts);
1464 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1467 list = g_list_append (NULL, priv->hpaned);
1468 list = g_list_append (list, priv->hbox_topic);
1469 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1472 /* Add the main widget in the chat widget */
1473 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1474 g_object_unref (gui);
1478 chat_size_request (GtkWidget *widget,
1479 GtkRequisition *requisition)
1481 GtkBin *bin = GTK_BIN (widget);
1484 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1485 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1487 child = gtk_bin_get_child (bin);
1489 if (child && GTK_WIDGET_VISIBLE (child))
1491 GtkRequisition child_requisition;
1493 gtk_widget_size_request (child, &child_requisition);
1495 requisition->width += child_requisition.width;
1496 requisition->height += child_requisition.height;
1501 chat_size_allocate (GtkWidget *widget,
1502 GtkAllocation *allocation)
1504 GtkBin *bin = GTK_BIN (widget);
1505 GtkAllocation child_allocation;
1508 widget->allocation = *allocation;
1510 child = gtk_bin_get_child (bin);
1512 if (child && GTK_WIDGET_VISIBLE (child))
1514 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
1515 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
1516 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1517 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1519 gtk_widget_size_allocate (child, &child_allocation);
1524 chat_finalize (GObject *object)
1527 EmpathyChatPriv *priv;
1529 chat = EMPATHY_CHAT (object);
1530 priv = GET_PRIV (chat);
1532 DEBUG ("Finalized: %p", object);
1534 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1535 g_slist_free (priv->sent_messages);
1537 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1538 g_list_free (priv->compositors);
1540 chat_composing_remove_timeout (chat);
1542 g_signal_handlers_disconnect_by_func (priv->account_manager,
1543 chat_new_connection_cb, object);
1545 g_object_unref (priv->account_manager);
1546 g_object_unref (priv->log_manager);
1548 if (priv->tp_chat) {
1549 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1550 chat_destroy_cb, chat);
1551 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1552 chat_message_received_cb, chat);
1553 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1554 chat_send_error_cb, chat);
1555 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1556 chat_state_changed_cb, chat);
1557 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1558 chat_property_changed_cb, chat);
1559 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1560 chat_members_changed_cb, chat);
1561 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1562 chat_remote_contact_changed_cb, chat);
1563 empathy_tp_chat_close (priv->tp_chat);
1564 g_object_unref (priv->tp_chat);
1566 if (priv->account) {
1567 g_object_unref (priv->account);
1569 if (priv->remote_contact) {
1570 g_object_unref (priv->remote_contact);
1573 if (priv->block_events_timeout_id) {
1574 g_source_remove (priv->block_events_timeout_id);
1578 g_free (priv->name);
1579 g_free (priv->subject);
1580 g_completion_free (priv->completion);
1582 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1586 chat_constructed (GObject *object)
1588 EmpathyChat *chat = EMPATHY_CHAT (object);
1590 chat_create_ui (chat);
1591 chat_add_logs (chat);
1592 show_pending_messages (chat);
1596 empathy_chat_class_init (EmpathyChatClass *klass)
1598 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1599 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1601 object_class->finalize = chat_finalize;
1602 object_class->get_property = chat_get_property;
1603 object_class->set_property = chat_set_property;
1604 object_class->constructed = chat_constructed;
1606 widget_class->size_request = chat_size_request;
1607 widget_class->size_allocate = chat_size_allocate;
1609 g_object_class_install_property (object_class,
1611 g_param_spec_object ("tp-chat",
1613 "The tp chat object",
1614 EMPATHY_TYPE_TP_CHAT,
1617 G_PARAM_STATIC_STRINGS));
1618 g_object_class_install_property (object_class,
1620 g_param_spec_object ("account",
1621 "Account of the chat",
1622 "The account of the chat",
1623 EMPATHY_TYPE_ACCOUNT,
1625 G_PARAM_STATIC_STRINGS));
1626 g_object_class_install_property (object_class,
1628 g_param_spec_string ("id",
1630 "The id of the chat",
1633 G_PARAM_STATIC_STRINGS));
1634 g_object_class_install_property (object_class,
1636 g_param_spec_string ("name",
1638 "The name of the chat",
1641 G_PARAM_STATIC_STRINGS));
1642 g_object_class_install_property (object_class,
1644 g_param_spec_string ("subject",
1646 "The subject or topic of the chat",
1649 G_PARAM_STATIC_STRINGS));
1650 g_object_class_install_property (object_class,
1651 PROP_REMOTE_CONTACT,
1652 g_param_spec_object ("remote-contact",
1653 "The remote contact",
1654 "The remote contact is any",
1655 EMPATHY_TYPE_CONTACT,
1657 G_PARAM_STATIC_STRINGS));
1658 g_object_class_install_property (object_class,
1660 g_param_spec_boolean ("show-contacts",
1661 "Contacts' visibility",
1662 "The visibility of the contacts' list",
1665 G_PARAM_STATIC_STRINGS));
1667 signals[COMPOSING] =
1668 g_signal_new ("composing",
1669 G_OBJECT_CLASS_TYPE (object_class),
1673 g_cclosure_marshal_VOID__BOOLEAN,
1677 signals[NEW_MESSAGE] =
1678 g_signal_new ("new-message",
1679 G_OBJECT_CLASS_TYPE (object_class),
1683 g_cclosure_marshal_VOID__OBJECT,
1685 1, EMPATHY_TYPE_MESSAGE);
1687 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1691 chat_block_events_timeout_cb (gpointer data)
1693 EmpathyChatPriv *priv = GET_PRIV (data);
1695 priv->block_events_timeout_id = 0;
1701 empathy_chat_init (EmpathyChat *chat)
1703 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1704 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1707 priv->log_manager = empathy_log_manager_dup_singleton ();
1708 priv->contacts_width = -1;
1709 priv->sent_messages = NULL;
1710 priv->sent_messages_index = -1;
1711 priv->account_manager = empathy_account_manager_dup_singleton ();
1713 g_signal_connect (priv->account_manager,
1715 G_CALLBACK (chat_new_connection_cb),
1718 empathy_conf_get_bool (empathy_conf_get (),
1719 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1720 &priv->show_contacts);
1722 /* Block events for some time to avoid having "has come online" or
1723 * "joined" messages. */
1724 priv->block_events_timeout_id =
1725 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1727 /* Add nick name completion */
1728 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1729 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1733 empathy_chat_new (EmpathyTpChat *tp_chat)
1735 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1739 empathy_chat_get_tp_chat (EmpathyChat *chat)
1741 EmpathyChatPriv *priv = GET_PRIV (chat);
1743 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1745 return priv->tp_chat;
1749 empathy_chat_set_tp_chat (EmpathyChat *chat,
1750 EmpathyTpChat *tp_chat)
1752 EmpathyChatPriv *priv = GET_PRIV (chat);
1753 TpConnection *connection;
1755 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1756 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1757 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1759 if (priv->tp_chat) {
1763 if (priv->account) {
1764 g_object_unref (priv->account);
1767 priv->tp_chat = g_object_ref (tp_chat);
1768 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1769 priv->account = empathy_account_manager_get_account (priv->account_manager,
1771 g_object_ref (priv->account);
1773 g_signal_connect (tp_chat, "destroy",
1774 G_CALLBACK (chat_destroy_cb),
1776 g_signal_connect (tp_chat, "message-received",
1777 G_CALLBACK (chat_message_received_cb),
1779 g_signal_connect (tp_chat, "send-error",
1780 G_CALLBACK (chat_send_error_cb),
1782 g_signal_connect (tp_chat, "chat-state-changed",
1783 G_CALLBACK (chat_state_changed_cb),
1785 g_signal_connect (tp_chat, "property-changed",
1786 G_CALLBACK (chat_property_changed_cb),
1788 g_signal_connect (tp_chat, "members-changed",
1789 G_CALLBACK (chat_members_changed_cb),
1791 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1792 G_CALLBACK (chat_remote_contact_changed_cb),
1795 chat_remote_contact_changed_cb (chat);
1797 if (chat->input_text_view) {
1798 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1799 if (priv->block_events_timeout_id == 0) {
1800 empathy_chat_view_append_event (chat->view, _("Connected"));
1804 g_object_notify (G_OBJECT (chat), "tp-chat");
1805 g_object_notify (G_OBJECT (chat), "id");
1806 g_object_notify (G_OBJECT (chat), "account");
1808 /* This is a noop when tp-chat is set at object construction time and causes
1809 * the pending messages to be show when it's set on the object after it has
1811 show_pending_messages (chat);
1815 empathy_chat_get_account (EmpathyChat *chat)
1817 EmpathyChatPriv *priv = GET_PRIV (chat);
1819 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1821 return priv->account;
1825 empathy_chat_get_id (EmpathyChat *chat)
1827 EmpathyChatPriv *priv = GET_PRIV (chat);
1829 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1835 empathy_chat_get_name (EmpathyChat *chat)
1837 EmpathyChatPriv *priv = GET_PRIV (chat);
1840 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1843 if (!ret && priv->remote_contact) {
1844 ret = empathy_contact_get_name (priv->remote_contact);
1850 return ret ? ret : _("Conversation");
1854 empathy_chat_get_subject (EmpathyChat *chat)
1856 EmpathyChatPriv *priv = GET_PRIV (chat);
1858 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1860 return priv->subject;
1864 empathy_chat_get_remote_contact (EmpathyChat *chat)
1866 EmpathyChatPriv *priv = GET_PRIV (chat);
1868 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1870 return priv->remote_contact;
1874 empathy_chat_get_contact_menu (EmpathyChat *chat)
1876 EmpathyChatPriv *priv = GET_PRIV (chat);
1877 GtkWidget *menu = NULL;
1879 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1881 if (priv->remote_contact) {
1882 menu = empathy_contact_menu_new (priv->remote_contact,
1883 EMPATHY_CONTACT_FEATURE_CALL |
1884 EMPATHY_CONTACT_FEATURE_LOG |
1885 EMPATHY_CONTACT_FEATURE_INFO);
1887 else if (priv->contact_list_view) {
1888 EmpathyContactListView *view;
1890 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1891 menu = empathy_contact_list_view_get_contact_menu (view);
1898 empathy_chat_clear (EmpathyChat *chat)
1900 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1902 empathy_chat_view_clear (chat->view);
1906 empathy_chat_scroll_down (EmpathyChat *chat)
1908 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1910 empathy_chat_view_scroll_down (chat->view);
1914 empathy_chat_cut (EmpathyChat *chat)
1916 GtkTextBuffer *buffer;
1918 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1920 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1921 if (gtk_text_buffer_get_has_selection (buffer)) {
1922 GtkClipboard *clipboard;
1924 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1926 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1931 empathy_chat_copy (EmpathyChat *chat)
1933 GtkTextBuffer *buffer;
1935 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1937 if (empathy_chat_view_get_has_selection (chat->view)) {
1938 empathy_chat_view_copy_clipboard (chat->view);
1942 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1943 if (gtk_text_buffer_get_has_selection (buffer)) {
1944 GtkClipboard *clipboard;
1946 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1948 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1953 empathy_chat_paste (EmpathyChat *chat)
1955 GtkTextBuffer *buffer;
1956 GtkClipboard *clipboard;
1958 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1960 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1961 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1963 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1967 empathy_chat_correct_word (EmpathyChat *chat,
1970 const gchar *new_word)
1972 GtkTextBuffer *buffer;
1974 g_return_if_fail (chat != NULL);
1975 g_return_if_fail (new_word != NULL);
1977 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1979 gtk_text_buffer_delete (buffer, start, end);
1980 gtk_text_buffer_insert (buffer, start,
1986 empathy_chat_is_room (EmpathyChat *chat)
1988 EmpathyChatPriv *priv = GET_PRIV (chat);
1990 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1992 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);