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 /* Also allow messages with two slashes before the first space,
404 * so it is possible to send an /unix/path */
405 int slash_count = 0, i;
406 for (i = 0; msg[i] && msg[i] != ' ' && slash_count < 2; i++) {
410 if (slash_count == 1) {
411 empathy_chat_view_append_event (chat->view,
412 _("Unsupported command"));
417 /* We can send the message */
418 message = empathy_message_new (msg);
419 empathy_tp_chat_send (priv->tp_chat, message);
420 g_object_unref (message);
424 chat_input_text_view_send (EmpathyChat *chat)
426 EmpathyChatPriv *priv;
427 GtkTextBuffer *buffer;
428 GtkTextIter start, end;
431 priv = GET_PRIV (chat);
433 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
435 gtk_text_buffer_get_bounds (buffer, &start, &end);
436 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
438 /* clear the input field */
439 gtk_text_buffer_set_text (buffer, "", -1);
441 chat_send (chat, msg);
446 chat_state_changed_cb (EmpathyTpChat *tp_chat,
447 EmpathyContact *contact,
448 TpChannelChatState state,
451 EmpathyChatPriv *priv;
453 gboolean was_composing;
455 priv = GET_PRIV (chat);
457 if (empathy_contact_is_user (contact)) {
458 /* We don't care about our own chat state */
462 was_composing = (priv->compositors != NULL);
464 /* Find the contact in the list. After that l is the list elem or NULL */
465 for (l = priv->compositors; l; l = l->next) {
466 if (contact == l->data) {
472 case TP_CHANNEL_CHAT_STATE_GONE:
473 case TP_CHANNEL_CHAT_STATE_INACTIVE:
474 case TP_CHANNEL_CHAT_STATE_PAUSED:
475 case TP_CHANNEL_CHAT_STATE_ACTIVE:
476 /* Contact is not composing */
478 priv->compositors = g_list_remove_link (priv->compositors, l);
479 g_object_unref (l->data);
483 case TP_CHANNEL_CHAT_STATE_COMPOSING:
484 /* Contact is composing */
486 priv->compositors = g_list_prepend (priv->compositors,
487 g_object_ref (contact));
491 g_assert_not_reached ();
494 DEBUG ("Was composing: %s now composing: %s",
495 was_composing ? "yes" : "no",
496 priv->compositors ? "yes" : "no");
498 if ((was_composing && !priv->compositors) ||
499 (!was_composing && priv->compositors)) {
500 /* Composing state changed */
501 g_signal_emit (chat, signals[COMPOSING], 0,
502 priv->compositors != NULL);
507 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
509 EmpathyChatPriv *priv = GET_PRIV (chat);
510 EmpathyContact *sender;
512 sender = empathy_message_get_sender (message);
514 DEBUG ("Appending new message from %s (%d)",
515 empathy_contact_get_name (sender),
516 empathy_contact_get_handle (sender));
518 empathy_chat_view_append_message (chat->view, message);
520 /* We received a message so the contact is no longer composing */
521 chat_state_changed_cb (priv->tp_chat, sender,
522 TP_CHANNEL_CHAT_STATE_ACTIVE,
525 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
529 chat_message_received_cb (EmpathyTpChat *tp_chat,
530 EmpathyMessage *message,
533 chat_message_received (chat, message);
534 empathy_tp_chat_acknowledge_message (tp_chat, message);
538 chat_send_error_cb (EmpathyTpChat *tp_chat,
539 EmpathyMessage *message,
540 TpChannelTextSendError error_code,
546 switch (error_code) {
547 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
548 error = _("offline");
550 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
551 error = _("invalid contact");
553 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
554 error = _("permission denied");
556 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
557 error = _("too long message");
559 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
560 error = _("not implemented");
563 error = _("unknown");
567 str = g_strdup_printf (_("Error sending message '%s': %s"),
568 empathy_message_get_body (message),
570 empathy_chat_view_append_event (chat->view, str);
575 chat_property_changed_cb (EmpathyTpChat *tp_chat,
580 EmpathyChatPriv *priv = GET_PRIV (chat);
582 if (!tp_strdiff (name, "subject")) {
583 g_free (priv->subject);
584 priv->subject = g_value_dup_string (value);
585 g_object_notify (G_OBJECT (chat), "subject");
587 if (EMP_STR_EMPTY (priv->subject)) {
588 gtk_widget_hide (priv->hbox_topic);
590 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
591 gtk_widget_show (priv->hbox_topic);
593 if (priv->block_events_timeout_id == 0) {
596 if (!EMP_STR_EMPTY (priv->subject)) {
597 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
599 str = g_strdup (_("No topic defined"));
601 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
605 else if (!tp_strdiff (name, "name")) {
607 priv->name = g_value_dup_string (value);
608 g_object_notify (G_OBJECT (chat), "name");
613 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
616 EmpathyChatPriv *priv;
617 GtkTextIter start, end;
619 gboolean spell_checker = FALSE;
621 priv = GET_PRIV (chat);
623 if (gtk_text_buffer_get_char_count (buffer) == 0) {
624 chat_composing_stop (chat);
626 chat_composing_start (chat);
629 empathy_conf_get_bool (empathy_conf_get (),
630 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
633 gtk_text_buffer_get_start_iter (buffer, &start);
635 if (!spell_checker) {
636 gtk_text_buffer_get_end_iter (buffer, &end);
637 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
641 if (!empathy_spell_supported ()) {
645 /* NOTE: this is really inefficient, we shouldn't have to
646 reiterate the whole buffer each time and check each work
649 gboolean correct = FALSE;
652 if (gtk_text_iter_is_start (&start)) {
655 if (!gtk_text_iter_forward_word_end (&end)) {
656 /* no whole word yet */
660 if (!gtk_text_iter_forward_word_end (&end)) {
661 /* must be the end of the buffer */
666 gtk_text_iter_backward_word_start (&start);
669 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
671 /* spell check string if not a command */
673 correct = empathy_spell_check (str);
679 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
681 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
686 /* set start iter to the end iters position */
692 chat_input_key_press_event_cb (GtkWidget *widget,
696 EmpathyChatPriv *priv;
699 GtkWidget *text_view_sw;
701 priv = GET_PRIV (chat);
703 /* Catch ctrl+up/down so we can traverse messages we sent */
704 if ((event->state & GDK_CONTROL_MASK) &&
705 (event->keyval == GDK_Up ||
706 event->keyval == GDK_Down)) {
707 GtkTextBuffer *buffer;
710 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
712 if (event->keyval == GDK_Up) {
713 str = chat_sent_message_get_next (chat);
715 str = chat_sent_message_get_last (chat);
718 g_signal_handlers_block_by_func (buffer,
719 chat_input_text_buffer_changed_cb,
721 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
722 g_signal_handlers_unblock_by_func (buffer,
723 chat_input_text_buffer_changed_cb,
729 /* Catch enter but not ctrl/shift-enter */
730 if (IS_ENTER (event->keyval) &&
731 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
734 /* This is to make sure that kinput2 gets the enter. And if
735 * it's handled there we shouldn't send on it. This is because
736 * kinput2 uses Enter to commit letters. See:
737 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
740 view = GTK_TEXT_VIEW (chat->input_text_view);
741 if (gtk_im_context_filter_keypress (view->im_context, event)) {
742 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
746 chat_input_text_view_send (chat);
750 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
752 if (IS_ENTER (event->keyval) &&
753 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
754 /* Newline for shift/control-enter. */
757 if (!(event->state & GDK_CONTROL_MASK) &&
758 event->keyval == GDK_Page_Up) {
759 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
760 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
763 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
764 event->keyval == GDK_Page_Down) {
765 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
766 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
767 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
768 gtk_adjustment_set_value (adj, val);
771 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
772 event->keyval == GDK_Tab) {
773 GtkTextBuffer *buffer;
774 GtkTextIter start, current;
775 gchar *nick, *completed;
776 GList *list, *completed_list;
777 gboolean is_start_of_buffer;
779 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
780 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
782 /* Get the start of the nick to complete. */
783 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
784 gtk_text_iter_backward_word_start (&start);
785 is_start_of_buffer = gtk_text_iter_is_start (&start);
787 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
788 g_completion_add_items (priv->completion, list);
790 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
791 completed_list = g_completion_complete (priv->completion,
800 gchar *complete_char = NULL;
802 gtk_text_buffer_delete (buffer, &start, ¤t);
804 len = g_list_length (completed_list);
807 /* If we only have one hit, use that text
808 * instead of the text in completed since the
809 * completed text will use the typed string
810 * which might be cased all wrong.
813 text = empathy_contact_get_name (completed_list->data);
818 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
820 if (len == 1 && is_start_of_buffer &&
821 empathy_conf_get_string (empathy_conf_get (),
822 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
824 complete_char != NULL) {
825 gtk_text_buffer_insert_at_cursor (buffer,
827 strlen (complete_char));
828 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
829 g_free (complete_char);
835 g_completion_clear_items (priv->completion);
837 g_list_foreach (list, (GFunc) g_object_unref, NULL);
847 chat_text_view_focus_in_event_cb (GtkWidget *widget,
851 gtk_widget_grab_focus (chat->input_text_view);
857 chat_input_set_size_request_idle (gpointer sw)
859 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
865 chat_input_size_request_cb (GtkWidget *widget,
866 GtkRequisition *requisition,
869 EmpathyChatPriv *priv = GET_PRIV (chat);
872 sw = gtk_widget_get_parent (widget);
873 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
874 g_idle_add (chat_input_set_size_request_idle, sw);
875 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
878 priv->has_input_vscroll = TRUE;
881 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
882 gtk_widget_set_size_request (sw, -1, -1);
883 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
886 priv->has_input_vscroll = FALSE;
891 chat_input_realize_cb (GtkWidget *widget,
894 DEBUG ("Setting focus to the input text view");
895 gtk_widget_grab_focus (widget);
899 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
900 EmpathySmiley *smiley,
903 EmpathyChat *chat = EMPATHY_CHAT (user_data);
904 GtkTextBuffer *buffer;
907 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
909 gtk_text_buffer_get_end_iter (buffer, &iter);
910 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
912 gtk_text_buffer_get_end_iter (buffer, &iter);
913 gtk_text_buffer_insert (buffer, &iter, " ", -1);
924 static EmpathyChatSpell *
925 chat_spell_new (EmpathyChat *chat,
930 EmpathyChatSpell *chat_spell;
932 chat_spell = g_slice_new0 (EmpathyChatSpell);
934 chat_spell->chat = g_object_ref (chat);
935 chat_spell->word = g_strdup (word);
936 chat_spell->start = start;
937 chat_spell->end = end;
943 chat_spell_free (EmpathyChatSpell *chat_spell)
945 g_object_unref (chat_spell->chat);
946 g_free (chat_spell->word);
947 g_slice_free (EmpathyChatSpell, chat_spell);
951 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
952 EmpathyChatSpell *chat_spell)
954 empathy_chat_correct_word (chat_spell->chat,
955 &(chat_spell->start),
957 gtk_menu_item_get_label (menu_item));
961 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
963 GtkWidget *menu, *menu_item;
964 GList *suggestions, *l;
966 menu = gtk_menu_new ();
967 suggestions = empathy_spell_get_suggestions (chat_spell->word);
968 if (suggestions == NULL) {
969 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
970 gtk_widget_set_sensitive (menu_item, FALSE);
971 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
973 for (l = suggestions; l; l = l->next) {
974 menu_item = gtk_menu_item_new_with_label (l->data);
975 g_signal_connect (G_OBJECT (menu_item),
977 G_CALLBACK (chat_spelling_menu_activate_cb),
979 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
982 empathy_spell_free_suggestions (suggestions);
984 gtk_widget_show_all (menu);
990 chat_text_send_cb (GtkMenuItem *menuitem,
993 chat_input_text_view_send (chat);
997 chat_input_populate_popup_cb (GtkTextView *view,
1001 EmpathyChatPriv *priv;
1002 GtkTextBuffer *buffer;
1003 GtkTextTagTable *table;
1006 GtkTextIter iter, start, end;
1009 EmpathyChatSpell *chat_spell;
1010 GtkWidget *spell_menu;
1011 EmpathySmileyManager *smiley_manager;
1012 GtkWidget *smiley_menu;
1015 priv = GET_PRIV (chat);
1016 buffer = gtk_text_view_get_buffer (view);
1018 /* Add the emoticon menu. */
1019 item = gtk_separator_menu_item_new ();
1020 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1021 gtk_widget_show (item);
1023 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1024 image = gtk_image_new_from_icon_name ("face-smile",
1025 GTK_ICON_SIZE_MENU);
1026 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1027 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1028 gtk_widget_show (item);
1030 smiley_manager = empathy_smiley_manager_dup_singleton ();
1031 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1032 chat_insert_smiley_activate_cb,
1034 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1035 g_object_unref (smiley_manager);
1037 /* Add the Send menu item. */
1038 gtk_text_buffer_get_bounds (buffer, &start, &end);
1039 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1040 if (!EMP_STR_EMPTY (str)) {
1041 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1042 g_signal_connect (G_OBJECT (item), "activate",
1043 G_CALLBACK (chat_text_send_cb), chat);
1044 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1045 gtk_widget_show (item);
1049 /* Add the spell check menu item. */
1050 table = gtk_text_buffer_get_tag_table (buffer);
1051 tag = gtk_text_tag_table_lookup (table, "misspelled");
1052 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1053 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1054 GTK_TEXT_WINDOW_WIDGET,
1057 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1059 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1060 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1062 str = gtk_text_buffer_get_text (buffer,
1063 &start, &end, FALSE);
1065 if (!EMP_STR_EMPTY (str)) {
1066 chat_spell = chat_spell_new (chat, str, start, end);
1067 g_object_set_data_full (G_OBJECT (menu),
1068 "chat_spell", chat_spell,
1069 (GDestroyNotify) chat_spell_free);
1071 item = gtk_separator_menu_item_new ();
1072 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1073 gtk_widget_show (item);
1075 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1076 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1077 GTK_ICON_SIZE_MENU);
1078 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1080 spell_menu = chat_spelling_build_menu (chat_spell);
1081 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1083 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1084 gtk_widget_show (item);
1089 chat_log_filter (EmpathyMessage *message,
1092 EmpathyChat *chat = (EmpathyChat *) user_data;
1093 EmpathyChatPriv *priv = GET_PRIV (chat);
1094 const GList *pending;
1096 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1098 for (; pending; pending = g_list_next (pending)) {
1099 if (empathy_message_equal (message, pending->data)) {
1108 chat_add_logs (EmpathyChat *chat)
1110 EmpathyChatPriv *priv = GET_PRIV (chat);
1111 gboolean is_chatroom;
1112 GList *messages, *l;
1118 /* Turn off scrolling temporarily */
1119 empathy_chat_view_scroll (chat->view, FALSE);
1121 /* Add messages from last conversation */
1122 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1124 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1132 for (l = messages; l; l = g_list_next (l)) {
1133 empathy_chat_view_append_message (chat->view, l->data);
1134 g_object_unref (l->data);
1137 g_list_free (messages);
1139 /* Turn back on scrolling */
1140 empathy_chat_view_scroll (chat->view, TRUE);
1144 chat_contacts_completion_func (const gchar *s1,
1148 gchar *tmp, *nick1, *nick2;
1155 return s1 ? -1 : +1;
1158 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1159 nick1 = g_utf8_casefold (tmp, -1);
1162 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1163 nick2 = g_utf8_casefold (tmp, -1);
1166 ret = strncmp (nick1, nick2, n);
1175 build_part_message (guint reason,
1177 EmpathyContact *actor,
1178 const gchar *message)
1180 GString *s = g_string_new ("");
1181 const gchar *actor_name = NULL;
1183 if (actor != NULL) {
1184 actor_name = empathy_contact_get_name (actor);
1187 /* Having an actor only really makes sense for a few actions... */
1189 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1190 g_string_append_printf (s, _("%s has disconnected"), name);
1192 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1193 if (actor_name != NULL) {
1194 g_string_append_printf (s, _("%s was kicked by %s"),
1197 g_string_append_printf (s, _("%s was kicked"), name);
1200 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1201 if (actor_name != NULL) {
1202 g_string_append_printf (s, _("%s was banned by %s"),
1205 g_string_append_printf (s, _("%s was banned"), name);
1209 g_string_append_printf (s, _("%s has left the room"), name);
1212 if (!EMP_STR_EMPTY (message)) {
1213 /* Note to translators: this string is appended to
1214 * notifications like "foo has left the room", with the message
1215 * given by the user living the room. If this poses a problem,
1216 * please let us know. :-)
1218 g_string_append_printf (s, _(" (%s)"), message);
1221 return g_string_free (s, FALSE);
1225 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1226 EmpathyContact *contact,
1227 EmpathyContact *actor,
1233 EmpathyChatPriv *priv = GET_PRIV (chat);
1234 const gchar *name = empathy_contact_get_name (contact);
1237 if (priv->block_events_timeout_id != 0)
1241 str = g_strdup_printf (_("%s has joined the room"),
1244 str = build_part_message (reason, name, actor, message);
1247 empathy_chat_view_append_event (chat->view, str);
1252 chat_reset_size_request (gpointer widget)
1254 gtk_widget_set_size_request (widget, -1, -1);
1260 chat_update_contacts_visibility (EmpathyChat *chat)
1262 EmpathyChatPriv *priv = GET_PRIV (chat);
1265 show = priv->remote_contact == NULL && priv->show_contacts;
1267 if (!priv->scrolled_window_contacts) {
1271 if (show && priv->contact_list_view == NULL) {
1272 EmpathyContactListStore *store;
1275 /* We are adding the contact list to the chat, we don't want the
1276 * chat view to become too small. If the chat view is already
1277 * smaller than 250 make sure that size won't change. If the
1278 * chat view is bigger the contact list will take some space on
1279 * it but we make sure the chat view don't become smaller than
1280 * 250. Relax the size request once the resize is done */
1281 min_width = MIN (priv->vbox_left->allocation.width, 250);
1282 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1283 g_idle_add (chat_reset_size_request, priv->vbox_left);
1285 if (priv->contacts_width > 0) {
1286 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1287 priv->contacts_width);
1290 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1291 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1292 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1293 EMPATHY_CONTACT_FEATURE_CHAT |
1294 EMPATHY_CONTACT_FEATURE_CALL |
1295 EMPATHY_CONTACT_FEATURE_LOG |
1296 EMPATHY_CONTACT_FEATURE_INFO));
1297 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1298 priv->contact_list_view);
1299 gtk_widget_show (priv->contact_list_view);
1300 gtk_widget_show (priv->scrolled_window_contacts);
1301 g_object_unref (store);
1303 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1304 gtk_widget_hide (priv->scrolled_window_contacts);
1305 if (priv->contact_list_view != NULL) {
1306 gtk_widget_destroy (priv->contact_list_view);
1307 priv->contact_list_view = NULL;
1313 empathy_chat_set_show_contacts (EmpathyChat *chat,
1316 EmpathyChatPriv *priv = GET_PRIV (chat);
1318 priv->show_contacts = show;
1320 chat_update_contacts_visibility (chat);
1322 g_object_notify (G_OBJECT (chat), "show-contacts");
1326 chat_remote_contact_changed_cb (EmpathyChat *chat)
1328 EmpathyChatPriv *priv = GET_PRIV (chat);
1330 if (priv->remote_contact != NULL) {
1331 g_object_unref (priv->remote_contact);
1332 priv->remote_contact = NULL;
1335 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1336 if (priv->remote_contact != NULL) {
1337 g_object_ref (priv->remote_contact);
1338 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1340 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1342 else if (priv->tp_chat != NULL) {
1345 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1346 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1348 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1351 chat_update_contacts_visibility (chat);
1353 g_object_notify (G_OBJECT (chat), "remote-contact");
1354 g_object_notify (G_OBJECT (chat), "id");
1358 chat_destroy_cb (EmpathyTpChat *tp_chat,
1361 EmpathyChatPriv *priv;
1363 priv = GET_PRIV (chat);
1365 if (!priv->tp_chat) {
1369 chat_composing_remove_timeout (chat);
1370 g_object_unref (priv->tp_chat);
1371 priv->tp_chat = NULL;
1372 g_object_notify (G_OBJECT (chat), "tp-chat");
1374 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1375 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1376 empathy_chat_set_show_contacts (chat, FALSE);
1380 show_pending_messages (EmpathyChat *chat) {
1381 EmpathyChatPriv *priv = GET_PRIV (chat);
1382 const GList *messages, *l;
1384 if (chat->view == NULL || priv->tp_chat == NULL)
1387 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1389 for (l = messages; l != NULL ; l = g_list_next (l)) {
1390 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1391 chat_message_received (chat, message);
1393 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1397 chat_create_ui (EmpathyChat *chat)
1399 EmpathyChatPriv *priv = GET_PRIV (chat);
1403 GtkTextBuffer *buffer;
1405 filename = empathy_file_lookup ("empathy-chat.ui",
1407 gui = empathy_builder_get_file (filename,
1408 "chat_widget", &priv->widget,
1409 "hpaned", &priv->hpaned,
1410 "vbox_left", &priv->vbox_left,
1411 "scrolled_window_chat", &priv->scrolled_window_chat,
1412 "scrolled_window_input", &priv->scrolled_window_input,
1413 "hbox_topic", &priv->hbox_topic,
1414 "label_topic", &priv->label_topic,
1415 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1419 /* Add message view. */
1420 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1421 g_signal_connect (chat->view, "focus_in_event",
1422 G_CALLBACK (chat_text_view_focus_in_event_cb),
1424 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1425 GTK_WIDGET (chat->view));
1426 gtk_widget_show (GTK_WIDGET (chat->view));
1428 /* Add input GtkTextView */
1429 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1430 "pixels-above-lines", 2,
1431 "pixels-below-lines", 2,
1432 "pixels-inside-wrap", 1,
1435 "wrap-mode", GTK_WRAP_WORD_CHAR,
1437 g_signal_connect (chat->input_text_view, "key-press-event",
1438 G_CALLBACK (chat_input_key_press_event_cb),
1440 g_signal_connect (chat->input_text_view, "size-request",
1441 G_CALLBACK (chat_input_size_request_cb),
1443 g_signal_connect (chat->input_text_view, "realize",
1444 G_CALLBACK (chat_input_realize_cb),
1446 g_signal_connect (chat->input_text_view, "populate-popup",
1447 G_CALLBACK (chat_input_populate_popup_cb),
1449 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1450 g_signal_connect (buffer, "changed",
1451 G_CALLBACK (chat_input_text_buffer_changed_cb),
1453 gtk_text_buffer_create_tag (buffer, "misspelled",
1454 "underline", PANGO_UNDERLINE_ERROR,
1456 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1457 chat->input_text_view);
1458 gtk_widget_show (chat->input_text_view);
1460 /* Create contact list */
1461 chat_update_contacts_visibility (chat);
1463 /* Initialy hide the topic, will be shown if not empty */
1464 gtk_widget_hide (priv->hbox_topic);
1466 /* Set widget focus order */
1467 list = g_list_append (NULL, priv->scrolled_window_input);
1468 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1471 list = g_list_append (NULL, priv->vbox_left);
1472 list = g_list_append (list, priv->scrolled_window_contacts);
1473 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1476 list = g_list_append (NULL, priv->hpaned);
1477 list = g_list_append (list, priv->hbox_topic);
1478 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1481 /* Add the main widget in the chat widget */
1482 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1483 g_object_unref (gui);
1487 chat_size_request (GtkWidget *widget,
1488 GtkRequisition *requisition)
1490 GtkBin *bin = GTK_BIN (widget);
1493 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1494 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1496 child = gtk_bin_get_child (bin);
1498 if (child && GTK_WIDGET_VISIBLE (child))
1500 GtkRequisition child_requisition;
1502 gtk_widget_size_request (child, &child_requisition);
1504 requisition->width += child_requisition.width;
1505 requisition->height += child_requisition.height;
1510 chat_size_allocate (GtkWidget *widget,
1511 GtkAllocation *allocation)
1513 GtkBin *bin = GTK_BIN (widget);
1514 GtkAllocation child_allocation;
1517 widget->allocation = *allocation;
1519 child = gtk_bin_get_child (bin);
1521 if (child && GTK_WIDGET_VISIBLE (child))
1523 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
1524 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
1525 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1526 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1528 gtk_widget_size_allocate (child, &child_allocation);
1533 chat_finalize (GObject *object)
1536 EmpathyChatPriv *priv;
1538 chat = EMPATHY_CHAT (object);
1539 priv = GET_PRIV (chat);
1541 DEBUG ("Finalized: %p", object);
1543 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1544 g_slist_free (priv->sent_messages);
1546 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1547 g_list_free (priv->compositors);
1549 chat_composing_remove_timeout (chat);
1551 g_signal_handlers_disconnect_by_func (priv->account_manager,
1552 chat_new_connection_cb, object);
1554 g_object_unref (priv->account_manager);
1555 g_object_unref (priv->log_manager);
1557 if (priv->tp_chat) {
1558 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1559 chat_destroy_cb, chat);
1560 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1561 chat_message_received_cb, chat);
1562 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1563 chat_send_error_cb, chat);
1564 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1565 chat_state_changed_cb, chat);
1566 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1567 chat_property_changed_cb, chat);
1568 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1569 chat_members_changed_cb, chat);
1570 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1571 chat_remote_contact_changed_cb, chat);
1572 empathy_tp_chat_close (priv->tp_chat);
1573 g_object_unref (priv->tp_chat);
1575 if (priv->account) {
1576 g_object_unref (priv->account);
1578 if (priv->remote_contact) {
1579 g_object_unref (priv->remote_contact);
1582 if (priv->block_events_timeout_id) {
1583 g_source_remove (priv->block_events_timeout_id);
1587 g_free (priv->name);
1588 g_free (priv->subject);
1589 g_completion_free (priv->completion);
1591 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1595 chat_constructed (GObject *object)
1597 EmpathyChat *chat = EMPATHY_CHAT (object);
1599 chat_create_ui (chat);
1600 chat_add_logs (chat);
1601 show_pending_messages (chat);
1605 empathy_chat_class_init (EmpathyChatClass *klass)
1607 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1608 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1610 object_class->finalize = chat_finalize;
1611 object_class->get_property = chat_get_property;
1612 object_class->set_property = chat_set_property;
1613 object_class->constructed = chat_constructed;
1615 widget_class->size_request = chat_size_request;
1616 widget_class->size_allocate = chat_size_allocate;
1618 g_object_class_install_property (object_class,
1620 g_param_spec_object ("tp-chat",
1622 "The tp chat object",
1623 EMPATHY_TYPE_TP_CHAT,
1626 G_PARAM_STATIC_STRINGS));
1627 g_object_class_install_property (object_class,
1629 g_param_spec_object ("account",
1630 "Account of the chat",
1631 "The account of the chat",
1632 EMPATHY_TYPE_ACCOUNT,
1634 G_PARAM_STATIC_STRINGS));
1635 g_object_class_install_property (object_class,
1637 g_param_spec_string ("id",
1639 "The id of the chat",
1642 G_PARAM_STATIC_STRINGS));
1643 g_object_class_install_property (object_class,
1645 g_param_spec_string ("name",
1647 "The name of the chat",
1650 G_PARAM_STATIC_STRINGS));
1651 g_object_class_install_property (object_class,
1653 g_param_spec_string ("subject",
1655 "The subject or topic of the chat",
1658 G_PARAM_STATIC_STRINGS));
1659 g_object_class_install_property (object_class,
1660 PROP_REMOTE_CONTACT,
1661 g_param_spec_object ("remote-contact",
1662 "The remote contact",
1663 "The remote contact is any",
1664 EMPATHY_TYPE_CONTACT,
1666 G_PARAM_STATIC_STRINGS));
1667 g_object_class_install_property (object_class,
1669 g_param_spec_boolean ("show-contacts",
1670 "Contacts' visibility",
1671 "The visibility of the contacts' list",
1674 G_PARAM_STATIC_STRINGS));
1676 signals[COMPOSING] =
1677 g_signal_new ("composing",
1678 G_OBJECT_CLASS_TYPE (object_class),
1682 g_cclosure_marshal_VOID__BOOLEAN,
1686 signals[NEW_MESSAGE] =
1687 g_signal_new ("new-message",
1688 G_OBJECT_CLASS_TYPE (object_class),
1692 g_cclosure_marshal_VOID__OBJECT,
1694 1, EMPATHY_TYPE_MESSAGE);
1696 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1700 chat_block_events_timeout_cb (gpointer data)
1702 EmpathyChatPriv *priv = GET_PRIV (data);
1704 priv->block_events_timeout_id = 0;
1710 empathy_chat_init (EmpathyChat *chat)
1712 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1713 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1716 priv->log_manager = empathy_log_manager_dup_singleton ();
1717 priv->contacts_width = -1;
1718 priv->sent_messages = NULL;
1719 priv->sent_messages_index = -1;
1720 priv->account_manager = empathy_account_manager_dup_singleton ();
1722 g_signal_connect (priv->account_manager,
1724 G_CALLBACK (chat_new_connection_cb),
1727 empathy_conf_get_bool (empathy_conf_get (),
1728 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1729 &priv->show_contacts);
1731 /* Block events for some time to avoid having "has come online" or
1732 * "joined" messages. */
1733 priv->block_events_timeout_id =
1734 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1736 /* Add nick name completion */
1737 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1738 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1742 empathy_chat_new (EmpathyTpChat *tp_chat)
1744 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1748 empathy_chat_get_tp_chat (EmpathyChat *chat)
1750 EmpathyChatPriv *priv = GET_PRIV (chat);
1752 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1754 return priv->tp_chat;
1758 empathy_chat_set_tp_chat (EmpathyChat *chat,
1759 EmpathyTpChat *tp_chat)
1761 EmpathyChatPriv *priv = GET_PRIV (chat);
1762 TpConnection *connection;
1764 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1765 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1766 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1768 if (priv->tp_chat) {
1772 if (priv->account) {
1773 g_object_unref (priv->account);
1776 priv->tp_chat = g_object_ref (tp_chat);
1777 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1778 priv->account = empathy_account_manager_get_account (priv->account_manager,
1780 g_object_ref (priv->account);
1782 g_signal_connect (tp_chat, "destroy",
1783 G_CALLBACK (chat_destroy_cb),
1785 g_signal_connect (tp_chat, "message-received",
1786 G_CALLBACK (chat_message_received_cb),
1788 g_signal_connect (tp_chat, "send-error",
1789 G_CALLBACK (chat_send_error_cb),
1791 g_signal_connect (tp_chat, "chat-state-changed",
1792 G_CALLBACK (chat_state_changed_cb),
1794 g_signal_connect (tp_chat, "property-changed",
1795 G_CALLBACK (chat_property_changed_cb),
1797 g_signal_connect (tp_chat, "members-changed",
1798 G_CALLBACK (chat_members_changed_cb),
1800 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1801 G_CALLBACK (chat_remote_contact_changed_cb),
1804 chat_remote_contact_changed_cb (chat);
1806 if (chat->input_text_view) {
1807 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1808 if (priv->block_events_timeout_id == 0) {
1809 empathy_chat_view_append_event (chat->view, _("Connected"));
1813 g_object_notify (G_OBJECT (chat), "tp-chat");
1814 g_object_notify (G_OBJECT (chat), "id");
1815 g_object_notify (G_OBJECT (chat), "account");
1817 /* This is a noop when tp-chat is set at object construction time and causes
1818 * the pending messages to be show when it's set on the object after it has
1820 show_pending_messages (chat);
1824 empathy_chat_get_account (EmpathyChat *chat)
1826 EmpathyChatPriv *priv = GET_PRIV (chat);
1828 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1830 return priv->account;
1834 empathy_chat_get_id (EmpathyChat *chat)
1836 EmpathyChatPriv *priv = GET_PRIV (chat);
1838 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1844 empathy_chat_get_name (EmpathyChat *chat)
1846 EmpathyChatPriv *priv = GET_PRIV (chat);
1849 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1852 if (!ret && priv->remote_contact) {
1853 ret = empathy_contact_get_name (priv->remote_contact);
1859 return ret ? ret : _("Conversation");
1863 empathy_chat_get_subject (EmpathyChat *chat)
1865 EmpathyChatPriv *priv = GET_PRIV (chat);
1867 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1869 return priv->subject;
1873 empathy_chat_get_remote_contact (EmpathyChat *chat)
1875 EmpathyChatPriv *priv = GET_PRIV (chat);
1877 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1879 return priv->remote_contact;
1883 empathy_chat_get_contact_menu (EmpathyChat *chat)
1885 EmpathyChatPriv *priv = GET_PRIV (chat);
1886 GtkWidget *menu = NULL;
1888 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1890 if (priv->remote_contact) {
1891 menu = empathy_contact_menu_new (priv->remote_contact,
1892 EMPATHY_CONTACT_FEATURE_CALL |
1893 EMPATHY_CONTACT_FEATURE_LOG |
1894 EMPATHY_CONTACT_FEATURE_INFO);
1896 else if (priv->contact_list_view) {
1897 EmpathyContactListView *view;
1899 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1900 menu = empathy_contact_list_view_get_contact_menu (view);
1907 empathy_chat_clear (EmpathyChat *chat)
1909 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1911 empathy_chat_view_clear (chat->view);
1915 empathy_chat_scroll_down (EmpathyChat *chat)
1917 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1919 empathy_chat_view_scroll_down (chat->view);
1923 empathy_chat_cut (EmpathyChat *chat)
1925 GtkTextBuffer *buffer;
1927 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1929 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1930 if (gtk_text_buffer_get_has_selection (buffer)) {
1931 GtkClipboard *clipboard;
1933 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1935 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1940 empathy_chat_copy (EmpathyChat *chat)
1942 GtkTextBuffer *buffer;
1944 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1946 if (empathy_chat_view_get_has_selection (chat->view)) {
1947 empathy_chat_view_copy_clipboard (chat->view);
1951 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1952 if (gtk_text_buffer_get_has_selection (buffer)) {
1953 GtkClipboard *clipboard;
1955 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1957 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1962 empathy_chat_paste (EmpathyChat *chat)
1964 GtkTextBuffer *buffer;
1965 GtkClipboard *clipboard;
1967 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1969 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1970 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1972 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1976 empathy_chat_correct_word (EmpathyChat *chat,
1979 const gchar *new_word)
1981 GtkTextBuffer *buffer;
1983 g_return_if_fail (chat != NULL);
1984 g_return_if_fail (new_word != NULL);
1986 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1988 gtk_text_buffer_delete (buffer, start, end);
1989 gtk_text_buffer_insert (buffer, start,
1995 empathy_chat_is_room (EmpathyChat *chat)
1997 EmpathyChatPriv *priv = GET_PRIV (chat);
1999 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2001 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);