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/account-manager.h>
38 #include <telepathy-glib/util.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-dispatcher.h>
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-smiley-manager.h"
53 #include "empathy-ui-utils.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
58 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
66 EmpathyTpChat *tp_chat;
71 EmpathyContact *remote_contact;
72 gboolean show_contacts;
74 EmpathyLogManager *log_manager;
75 TpAccountManager *account_manager;
77 GList *input_history_current;
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;
95 GtkWidget *info_bar_vbox;
97 guint unread_messages;
101 gchar *text; /* Original message that was specified
102 * upon entry creation. */
103 gchar *modified_text; /* Message that was modified by user.
104 * When no modifications were made, it is NULL */
124 static guint signals[LAST_SIGNAL] = { 0 };
126 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
129 chat_get_property (GObject *object,
134 EmpathyChat *chat = EMPATHY_CHAT (object);
135 EmpathyChatPriv *priv = GET_PRIV (object);
139 g_value_set_object (value, priv->tp_chat);
142 g_value_set_object (value, priv->account);
145 g_value_set_string (value, empathy_chat_get_name (chat));
148 g_value_set_string (value, priv->id);
151 g_value_set_string (value, priv->subject);
153 case PROP_REMOTE_CONTACT:
154 g_value_set_object (value, priv->remote_contact);
156 case PROP_SHOW_CONTACTS:
157 g_value_set_boolean (value, priv->show_contacts);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
166 chat_set_property (GObject *object,
171 EmpathyChat *chat = EMPATHY_CHAT (object);
175 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
177 case PROP_SHOW_CONTACTS:
178 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
191 EmpathyChat *chat = EMPATHY_CHAT (user_data);
192 EmpathyTpChat *tpchat;
195 empathy_chat_view_append_event (chat->view,
196 _("Failed to reconnect this chat"));
200 tpchat = EMPATHY_TP_CHAT (
201 empathy_dispatch_operation_get_channel_wrapper (dispatch));
203 if (empathy_dispatch_operation_claim (dispatch)) {
204 empathy_chat_set_tp_chat (chat, tpchat);
209 chat_new_connection_cb (TpAccount *account,
213 gchar *dbus_error_name,
217 EmpathyChatPriv *priv = GET_PRIV (chat);
218 TpConnection *connection;
220 connection = tp_account_get_connection (account);
222 if (!priv->tp_chat && account == priv->account &&
223 priv->handle_type != TP_HANDLE_TYPE_NONE &&
224 !EMP_STR_EMPTY (priv->id)) {
226 DEBUG ("Account reconnected, request a new Text channel");
228 switch (priv->handle_type) {
229 case TP_HANDLE_TYPE_CONTACT:
230 empathy_dispatcher_chat_with_contact_id (
231 connection, priv->id,
232 chat_connect_channel_reconnected,
235 case TP_HANDLE_TYPE_ROOM:
236 empathy_dispatcher_join_muc (connection,
238 chat_connect_channel_reconnected,
242 g_assert_not_reached ();
249 chat_composing_remove_timeout (EmpathyChat *chat)
251 EmpathyChatPriv *priv;
253 priv = GET_PRIV (chat);
255 if (priv->composing_stop_timeout_id) {
256 g_source_remove (priv->composing_stop_timeout_id);
257 priv->composing_stop_timeout_id = 0;
262 chat_composing_stop_timeout_cb (EmpathyChat *chat)
264 EmpathyChatPriv *priv;
266 priv = GET_PRIV (chat);
268 priv->composing_stop_timeout_id = 0;
269 empathy_tp_chat_set_state (priv->tp_chat,
270 TP_CHANNEL_CHAT_STATE_PAUSED);
276 chat_composing_start (EmpathyChat *chat)
278 EmpathyChatPriv *priv;
280 priv = GET_PRIV (chat);
282 if (priv->composing_stop_timeout_id) {
283 /* Just restart the timeout */
284 chat_composing_remove_timeout (chat);
286 empathy_tp_chat_set_state (priv->tp_chat,
287 TP_CHANNEL_CHAT_STATE_COMPOSING);
290 priv->composing_stop_timeout_id = g_timeout_add_seconds (
291 COMPOSING_STOP_TIMEOUT,
292 (GSourceFunc) chat_composing_stop_timeout_cb,
297 chat_composing_stop (EmpathyChat *chat)
299 EmpathyChatPriv *priv;
301 priv = GET_PRIV (chat);
303 chat_composing_remove_timeout (chat);
304 empathy_tp_chat_set_state (priv->tp_chat,
305 TP_CHANNEL_CHAT_STATE_ACTIVE);
309 chat_input_history_entry_cmp (InputHistoryEntry *entry,
312 if (!tp_strdiff (entry->text, text)) {
313 if (entry->modified_text != NULL) {
314 /* Modified entry and single string cannot be equal. */
322 static InputHistoryEntry *
323 chat_input_history_entry_new_with_text (const gchar *text)
325 InputHistoryEntry *entry;
326 entry = g_slice_new0 (InputHistoryEntry);
327 entry->text = g_strdup (text);
333 chat_input_history_entry_free (InputHistoryEntry *entry)
335 g_free (entry->text);
336 g_free (entry->modified_text);
337 g_slice_free (InputHistoryEntry, entry);
341 chat_input_history_entry_revert (InputHistoryEntry *entry)
343 g_free (entry->modified_text);
344 entry->modified_text = NULL;
348 chat_input_history_entry_update_text (InputHistoryEntry *entry,
353 if (!tp_strdiff (text, entry->text)) {
354 g_free (entry->modified_text);
355 entry->modified_text = NULL;
359 old = entry->modified_text;
360 entry->modified_text = g_strdup (text);
365 chat_input_history_entry_get_text (InputHistoryEntry *entry)
371 if (entry->modified_text != NULL) {
372 return entry->modified_text;
378 chat_input_history_remove_item (GList *list,
381 list = g_list_remove_link (list, item);
382 chat_input_history_entry_free (item->data);
383 g_list_free_1 (item);
388 chat_input_history_revert (EmpathyChat *chat)
390 EmpathyChatPriv *priv;
394 InputHistoryEntry *entry;
396 priv = GET_PRIV (chat);
397 list = priv->input_history;
400 DEBUG ("No input history");
404 /* Delete temporary entry */
405 if (priv->input_history_current != NULL) {
407 list = chat_input_history_remove_item (list, item1);
408 if (priv->input_history_current == item1) {
409 /* Removed temporary entry was current entry */
410 priv->input_history = list;
411 priv->input_history_current = NULL;
416 /* There is no entry to revert */
420 /* Restore the current history entry to original value */
421 item1 = priv->input_history_current;
423 chat_input_history_entry_revert (entry);
425 /* Remove restored entry if there is other occurance before this entry */
426 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
427 (GCompareFunc) chat_input_history_entry_cmp);
428 if (item2 != item1) {
429 list = chat_input_history_remove_item (list, item1);
432 /* Remove other occurance of the restored entry */
433 item2 = g_list_find_custom (item1->next,
434 chat_input_history_entry_get_text (entry),
435 (GCompareFunc) chat_input_history_entry_cmp);
437 list = chat_input_history_remove_item (list, item2);
441 priv->input_history_current = NULL;
442 priv->input_history = list;
446 chat_input_history_add (EmpathyChat *chat,
450 EmpathyChatPriv *priv;
453 InputHistoryEntry *entry;
455 priv = GET_PRIV (chat);
457 list = priv->input_history;
459 /* Remove any other occurances of this entry, if not temporary */
461 while ((item = g_list_find_custom (list, str,
462 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
463 list = chat_input_history_remove_item (list, item);
466 /* Trim the list to the last 10 items */
467 while (g_list_length (list) > 10) {
468 item = g_list_last (list);
470 list = chat_input_history_remove_item (list, item);
478 entry = chat_input_history_entry_new_with_text (str);
479 list = g_list_prepend (list, entry);
481 /* Set the list and the current item pointer */
482 priv->input_history = list;
484 priv->input_history_current = list;
487 priv->input_history_current = NULL;
492 chat_input_history_get_next (EmpathyChat *chat)
494 EmpathyChatPriv *priv;
498 priv = GET_PRIV (chat);
500 if (priv->input_history == NULL) {
501 DEBUG ("No input history, next entry is NULL");
504 g_assert (priv->input_history_current != NULL);
506 if ((item = g_list_next (priv->input_history_current)) == NULL)
508 item = priv->input_history_current;
511 msg = chat_input_history_entry_get_text (item->data);
513 DEBUG ("Returning next entry: '%s'", msg);
515 priv->input_history_current = item;
521 chat_input_history_get_prev (EmpathyChat *chat)
523 EmpathyChatPriv *priv;
527 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
529 priv = GET_PRIV (chat);
531 if (priv->input_history == NULL) {
532 DEBUG ("No input history, previous entry is NULL");
536 if (priv->input_history_current == NULL)
540 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
542 item = priv->input_history_current;
545 msg = chat_input_history_entry_get_text (item->data);
547 DEBUG ("Returning previous entry: '%s'", msg);
549 priv->input_history_current = item;
555 chat_input_history_update (EmpathyChat *chat,
556 GtkTextBuffer *buffer)
558 EmpathyChatPriv *priv;
559 GtkTextIter start, end;
561 InputHistoryEntry *entry;
563 priv = GET_PRIV (chat);
565 gtk_text_buffer_get_bounds (buffer, &start, &end);
566 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
568 if (priv->input_history_current == NULL) {
569 /* Add the current text temporarily to the history */
570 chat_input_history_add (chat, text, TRUE);
575 /* Save the changes in the history */
576 entry = priv->input_history_current->data;
577 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
578 chat_input_history_entry_update_text (entry, text);
585 chat_command_join_cb (EmpathyDispatchOperation *dispatch,
589 EmpathyChat *chat = user_data;
592 DEBUG ("Error: %s", error->message);
593 empathy_chat_view_append_event (chat->view,
594 _("Failed to join chatroom"));
601 } ChatCommandMsgData;
604 chat_command_msg_cb (EmpathyDispatchOperation *dispatch,
608 ChatCommandMsgData *data = user_data;
611 empathy_chat_view_append_event (data->chat->view,
612 _("Failed to open private chat"));
616 if (!EMP_STR_EMPTY (data->message)) {
617 EmpathyTpChat *tpchat;
618 EmpathyMessage *message;
620 tpchat = EMPATHY_TP_CHAT (
621 empathy_dispatch_operation_get_channel_wrapper (dispatch));
623 message = empathy_message_new (data->message);
624 empathy_tp_chat_send (tpchat, message);
625 g_object_unref (message);
629 g_free (data->message);
630 g_slice_free (ChatCommandMsgData, data);
634 chat_command_clear (EmpathyChat *chat,
637 empathy_chat_view_clear (chat->view);
641 chat_command_topic (EmpathyChat *chat,
644 EmpathyChatPriv *priv = GET_PRIV (chat);
645 EmpathyTpChatProperty *property;
646 GValue value = {0, };
648 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
649 if (property == NULL) {
650 empathy_chat_view_append_event (chat->view,
651 _("Topic not supported on this conversation"));
655 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
656 empathy_chat_view_append_event (chat->view,
657 _("You are not allowed to change the topic"));
661 g_value_init (&value, G_TYPE_STRING);
662 g_value_set_string (&value, strv[1]);
663 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
664 g_value_unset (&value);
668 chat_command_join (EmpathyChat *chat,
671 EmpathyChatPriv *priv = GET_PRIV (chat);
672 TpConnection *connection;
674 connection = empathy_tp_chat_get_connection (priv->tp_chat);
675 empathy_dispatcher_join_muc (connection, strv[1],
676 chat_command_join_cb,
681 chat_command_msg_internal (EmpathyChat *chat,
682 const gchar *contact_id,
683 const gchar *message)
685 EmpathyChatPriv *priv = GET_PRIV (chat);
686 TpConnection *connection;
687 ChatCommandMsgData *data;
689 /* FIXME: We should probably search in members alias. But this
690 * is enough for IRC */
691 data = g_slice_new (ChatCommandMsgData);
693 data->message = g_strdup (message);
694 connection = empathy_tp_chat_get_connection (priv->tp_chat);
695 empathy_dispatcher_chat_with_contact_id (connection, contact_id,
701 chat_command_query (EmpathyChat *chat,
704 /* If <message> part is not defined,
705 * strv[2] will be the terminal NULL */
706 chat_command_msg_internal (chat, strv[1], strv[2]);
710 chat_command_msg (EmpathyChat *chat,
713 chat_command_msg_internal (chat, strv[1], strv[2]);
717 chat_command_nick (EmpathyChat *chat,
720 EmpathyChatPriv *priv = GET_PRIV (chat);
721 TpConnection *connection;
722 GHashTable *new_alias;
725 connection = tp_account_get_connection (priv->account);
726 handle = tp_connection_get_self_handle (connection);
727 new_alias = g_hash_table_new (g_direct_hash, g_direct_equal);
728 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), strv[1]);
730 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
731 new_alias, NULL, NULL, NULL, NULL);
733 g_hash_table_destroy (new_alias);
737 chat_command_me (EmpathyChat *chat,
740 EmpathyChatPriv *priv = GET_PRIV (chat);
741 EmpathyMessage *message;
743 message = empathy_message_new (strv[1]);
744 empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
745 empathy_tp_chat_send (priv->tp_chat, message);
746 g_object_unref (message);
750 chat_command_say (EmpathyChat *chat,
753 EmpathyChatPriv *priv = GET_PRIV (chat);
754 EmpathyMessage *message;
756 message = empathy_message_new (strv[1]);
757 empathy_tp_chat_send (priv->tp_chat, message);
758 g_object_unref (message);
761 static void chat_command_help (EmpathyChat *chat, GStrv strv);
763 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
769 ChatCommandFunc func;
773 static ChatCommandItem commands[] = {
774 {"clear", 1, 1, chat_command_clear,
775 N_("/clear, clear all messages from the current conversation")},
777 {"topic", 2, 2, chat_command_topic,
778 N_("/topic <topic>, set the topic of the current conversation")},
780 {"join", 2, 2, chat_command_join,
781 N_("/join <chatroom id>, join a new chatroom")},
783 {"j", 2, 2, chat_command_join,
784 N_("/j <chatroom id>, join a new chatroom")},
786 {"query", 2, 3, chat_command_query,
787 N_("/query <contact id> [<message>], open a private chat")},
789 {"msg", 3, 3, chat_command_msg,
790 N_("/msg <contact id> <message>, open a private chat")},
792 {"nick", 2, 2, chat_command_nick,
793 N_("/nick <nickname>, change your nickname on current server")},
795 {"me", 2, 2, chat_command_me,
796 N_("/me <message>, send an ACTION message to the current conversation")},
798 {"say", 2, 2, chat_command_say,
799 N_("/say <message>, send <message> to the current conversation. "
800 "This is used to send a message starting with a '/'. For example: "
801 "\"/say /join is used to join a new chatroom\"")},
803 {"help", 1, 2, chat_command_help,
804 N_("/help [<command>], show all supported commands. "
805 "If <command> is defined, show its usage.")},
809 chat_command_show_help (EmpathyChat *chat,
810 ChatCommandItem *item)
814 str = g_strdup_printf (_("Usage: %s"), _(item->help));
815 empathy_chat_view_append_event (chat->view, str);
820 chat_command_help (EmpathyChat *chat,
825 /* If <command> part is not defined,
826 * strv[1] will be the terminal NULL */
827 if (strv[1] == NULL) {
828 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
829 empathy_chat_view_append_event (chat->view,
830 _(commands[i].help));
835 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
836 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
837 chat_command_show_help (chat, &commands[i]);
842 empathy_chat_view_append_event (chat->view,
843 _("Unknown command"));
847 chat_command_parse (const gchar *text, guint max_parts)
852 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
854 array = g_ptr_array_sized_new (max_parts + 1);
855 while (max_parts > 1) {
858 /* Skip white spaces */
859 while (g_ascii_isspace (*text)) {
863 /* Search the end of this part, until first space. */
864 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
870 item = g_strndup (text, end - text);
871 g_ptr_array_add (array, item);
872 DEBUG ("\tITEM: \"%s\"", item);
878 /* Append last part if not empty */
879 item = g_strstrip (g_strdup (text));
880 if (!EMP_STR_EMPTY (item)) {
881 g_ptr_array_add (array, item);
882 DEBUG ("\tITEM: \"%s\"", item);
887 /* Make the array NULL-terminated */
888 g_ptr_array_add (array, NULL);
890 return (GStrv) g_ptr_array_free (array, FALSE);
894 has_prefix_case (const gchar *s,
897 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
901 chat_send (EmpathyChat *chat,
904 EmpathyChatPriv *priv;
905 EmpathyMessage *message;
908 if (EMP_STR_EMPTY (msg)) {
912 priv = GET_PRIV (chat);
914 chat_input_history_add (chat, msg, FALSE);
917 gboolean second_slash = FALSE;
918 const gchar *iter = msg + 1;
920 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
925 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
928 c = *(msg + 1 + strlen (commands[i].prefix));
929 if (c != '\0' && !g_ascii_isspace (c)) {
933 /* We can't use g_strsplit here because it does
934 * not deal correctly if we have more than one space
936 strv = chat_command_parse (msg + 1, commands[i].max_parts);
938 strv_len = g_strv_length (strv);
939 if (strv_len < commands[i].min_parts ||
940 strv_len > commands[i].max_parts) {
941 chat_command_show_help (chat, &commands[i]);
946 commands[i].func (chat, strv);
951 /* Also allow messages with two slashes before the
952 * first space, so it is possible to send a /unix/path.
953 * This heuristic is kind of crap. */
954 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
963 empathy_chat_view_append_event (chat->view,
964 _("Unknown command, see /help for the available"
970 message = empathy_message_new (msg);
971 empathy_tp_chat_send (priv->tp_chat, message);
972 g_object_unref (message);
976 chat_input_text_view_send (EmpathyChat *chat)
978 EmpathyChatPriv *priv;
979 GtkTextBuffer *buffer;
980 GtkTextIter start, end;
983 priv = GET_PRIV (chat);
985 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
987 gtk_text_buffer_get_bounds (buffer, &start, &end);
988 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
990 /* clear the input field */
991 gtk_text_buffer_set_text (buffer, "", -1);
992 /* delete input history modifications */
993 chat_input_history_revert (chat);
995 chat_send (chat, msg);
1000 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1001 EmpathyContact *contact,
1002 TpChannelChatState state,
1005 EmpathyChatPriv *priv;
1007 gboolean was_composing;
1009 priv = GET_PRIV (chat);
1011 if (empathy_contact_is_user (contact)) {
1012 /* We don't care about our own chat state */
1016 was_composing = (priv->compositors != NULL);
1018 /* Find the contact in the list. After that l is the list elem or NULL */
1019 for (l = priv->compositors; l; l = l->next) {
1020 if (contact == l->data) {
1026 case TP_CHANNEL_CHAT_STATE_GONE:
1027 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1028 case TP_CHANNEL_CHAT_STATE_PAUSED:
1029 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1030 /* Contact is not composing */
1032 priv->compositors = g_list_remove_link (priv->compositors, l);
1033 g_object_unref (l->data);
1037 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1038 /* Contact is composing */
1040 priv->compositors = g_list_prepend (priv->compositors,
1041 g_object_ref (contact));
1045 g_assert_not_reached ();
1048 DEBUG ("Was composing: %s now composing: %s",
1049 was_composing ? "yes" : "no",
1050 priv->compositors ? "yes" : "no");
1052 if ((was_composing && !priv->compositors) ||
1053 (!was_composing && priv->compositors)) {
1054 /* Composing state changed */
1055 g_signal_emit (chat, signals[COMPOSING], 0,
1056 priv->compositors != NULL);
1061 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
1063 EmpathyChatPriv *priv = GET_PRIV (chat);
1064 EmpathyContact *sender;
1066 sender = empathy_message_get_sender (message);
1068 DEBUG ("Appending new message from %s (%d)",
1069 empathy_contact_get_name (sender),
1070 empathy_contact_get_handle (sender));
1072 empathy_chat_view_append_message (chat->view, message);
1074 /* We received a message so the contact is no longer composing */
1075 chat_state_changed_cb (priv->tp_chat, sender,
1076 TP_CHANNEL_CHAT_STATE_ACTIVE,
1079 priv->unread_messages++;
1080 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
1084 chat_message_received_cb (EmpathyTpChat *tp_chat,
1085 EmpathyMessage *message,
1088 chat_message_received (chat, message);
1089 empathy_tp_chat_acknowledge_message (tp_chat, message);
1093 chat_send_error_cb (EmpathyTpChat *tp_chat,
1094 const gchar *message_body,
1095 TpChannelTextSendError error_code,
1101 switch (error_code) {
1102 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1103 error = _("offline");
1105 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1106 error = _("invalid contact");
1108 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1109 error = _("permission denied");
1111 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1112 error = _("too long message");
1114 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1115 error = _("not implemented");
1118 error = _("unknown");
1122 str = g_strdup_printf (_("Error sending message '%s': %s"),
1125 empathy_chat_view_append_event (chat->view, str);
1130 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1135 EmpathyChatPriv *priv = GET_PRIV (chat);
1137 if (!tp_strdiff (name, "subject")) {
1138 g_free (priv->subject);
1139 priv->subject = g_value_dup_string (value);
1140 g_object_notify (G_OBJECT (chat), "subject");
1142 if (EMP_STR_EMPTY (priv->subject)) {
1143 gtk_widget_hide (priv->hbox_topic);
1145 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
1146 gtk_widget_show (priv->hbox_topic);
1148 if (priv->block_events_timeout_id == 0) {
1151 if (!EMP_STR_EMPTY (priv->subject)) {
1152 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1154 str = g_strdup (_("No topic defined"));
1156 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1160 else if (!tp_strdiff (name, "name")) {
1161 g_free (priv->name);
1162 priv->name = g_value_dup_string (value);
1163 g_object_notify (G_OBJECT (chat), "name");
1168 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1171 EmpathyChatPriv *priv;
1172 GtkTextIter start, end;
1174 gboolean spell_checker = FALSE;
1176 priv = GET_PRIV (chat);
1178 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1179 chat_composing_stop (chat);
1181 chat_composing_start (chat);
1184 empathy_conf_get_bool (empathy_conf_get (),
1185 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
1188 gtk_text_buffer_get_start_iter (buffer, &start);
1190 if (!spell_checker) {
1191 gtk_text_buffer_get_end_iter (buffer, &end);
1192 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1196 if (!empathy_spell_supported ()) {
1200 /* NOTE: this is really inefficient, we shouldn't have to
1201 reiterate the whole buffer each time and check each work
1204 gboolean correct = FALSE;
1207 if (gtk_text_iter_is_start (&start)) {
1210 if (!gtk_text_iter_forward_word_end (&end)) {
1211 /* no whole word yet */
1215 if (!gtk_text_iter_forward_word_end (&end)) {
1216 /* must be the end of the buffer */
1221 gtk_text_iter_backward_word_start (&start);
1224 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1226 /* spell check string if not a command */
1227 if (str[0] != '/') {
1228 correct = empathy_spell_check (str);
1234 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1236 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1241 /* set start iter to the end iters position */
1247 chat_input_key_press_event_cb (GtkWidget *widget,
1251 EmpathyChatPriv *priv;
1254 GtkWidget *text_view_sw;
1256 priv = GET_PRIV (chat);
1258 /* Catch ctrl+up/down so we can traverse messages we sent */
1259 if ((event->state & GDK_CONTROL_MASK) &&
1260 (event->keyval == GDK_Up ||
1261 event->keyval == GDK_Down)) {
1262 GtkTextBuffer *buffer;
1265 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1266 chat_input_history_update (chat, buffer);
1268 if (event->keyval == GDK_Up) {
1269 str = chat_input_history_get_next (chat);
1271 str = chat_input_history_get_prev (chat);
1274 g_signal_handlers_block_by_func (buffer,
1275 chat_input_text_buffer_changed_cb,
1277 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1278 g_signal_handlers_unblock_by_func (buffer,
1279 chat_input_text_buffer_changed_cb,
1285 /* Catch enter but not ctrl/shift-enter */
1286 if (IS_ENTER (event->keyval) &&
1287 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1290 /* This is to make sure that kinput2 gets the enter. And if
1291 * it's handled there we shouldn't send on it. This is because
1292 * kinput2 uses Enter to commit letters. See:
1293 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1296 view = GTK_TEXT_VIEW (chat->input_text_view);
1297 if (gtk_im_context_filter_keypress (view->im_context, event)) {
1298 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
1302 chat_input_text_view_send (chat);
1306 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1308 if (IS_ENTER (event->keyval) &&
1309 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1310 /* Newline for shift/control-enter. */
1313 if (!(event->state & GDK_CONTROL_MASK) &&
1314 event->keyval == GDK_Page_Up) {
1315 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1316 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1319 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1320 event->keyval == GDK_Page_Down) {
1321 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1322 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1323 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1324 gtk_adjustment_set_value (adj, val);
1327 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1328 event->keyval == GDK_Tab) {
1329 GtkTextBuffer *buffer;
1330 GtkTextIter start, current;
1331 gchar *nick, *completed;
1332 GList *list, *completed_list;
1333 gboolean is_start_of_buffer;
1335 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1336 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1338 /* Get the start of the nick to complete. */
1339 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1340 gtk_text_iter_backward_word_start (&start);
1341 is_start_of_buffer = gtk_text_iter_is_start (&start);
1343 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1344 g_completion_add_items (priv->completion, list);
1346 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1347 completed_list = g_completion_complete (priv->completion,
1356 gchar *complete_char = NULL;
1358 gtk_text_buffer_delete (buffer, &start, ¤t);
1360 len = g_list_length (completed_list);
1363 /* If we only have one hit, use that text
1364 * instead of the text in completed since the
1365 * completed text will use the typed string
1366 * which might be cased all wrong.
1369 text = empathy_contact_get_name (completed_list->data);
1374 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1376 if (len == 1 && is_start_of_buffer &&
1377 empathy_conf_get_string (empathy_conf_get (),
1378 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1380 complete_char != NULL) {
1381 gtk_text_buffer_insert_at_cursor (buffer,
1383 strlen (complete_char));
1384 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1385 g_free (complete_char);
1391 g_completion_clear_items (priv->completion);
1393 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1403 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1407 gtk_widget_grab_focus (chat->input_text_view);
1413 chat_input_set_size_request_idle (gpointer sw)
1415 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1421 chat_input_size_request_cb (GtkWidget *widget,
1422 GtkRequisition *requisition,
1425 EmpathyChatPriv *priv = GET_PRIV (chat);
1428 sw = gtk_widget_get_parent (widget);
1429 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1430 g_idle_add (chat_input_set_size_request_idle, sw);
1431 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1434 priv->has_input_vscroll = TRUE;
1437 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1438 gtk_widget_set_size_request (sw, -1, -1);
1439 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1442 priv->has_input_vscroll = FALSE;
1447 chat_input_realize_cb (GtkWidget *widget,
1450 DEBUG ("Setting focus to the input text view");
1451 if (gtk_widget_is_sensitive (widget)) {
1452 gtk_widget_grab_focus (widget);
1457 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1458 EmpathySmiley *smiley,
1461 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1462 GtkTextBuffer *buffer;
1465 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1467 gtk_text_buffer_get_end_iter (buffer, &iter);
1468 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1470 gtk_text_buffer_get_end_iter (buffer, &iter);
1471 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1482 static EmpathyChatSpell *
1483 chat_spell_new (EmpathyChat *chat,
1488 EmpathyChatSpell *chat_spell;
1490 chat_spell = g_slice_new0 (EmpathyChatSpell);
1492 chat_spell->chat = g_object_ref (chat);
1493 chat_spell->word = g_strdup (word);
1494 chat_spell->start = start;
1495 chat_spell->end = end;
1501 chat_spell_free (EmpathyChatSpell *chat_spell)
1503 g_object_unref (chat_spell->chat);
1504 g_free (chat_spell->word);
1505 g_slice_free (EmpathyChatSpell, chat_spell);
1509 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1510 EmpathyChatSpell *chat_spell)
1512 empathy_chat_correct_word (chat_spell->chat,
1513 &(chat_spell->start),
1515 gtk_menu_item_get_label (menu_item));
1519 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1521 GtkWidget *menu, *menu_item;
1522 GList *suggestions, *l;
1524 menu = gtk_menu_new ();
1525 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1526 if (suggestions == NULL) {
1527 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1528 gtk_widget_set_sensitive (menu_item, FALSE);
1529 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1531 for (l = suggestions; l; l = l->next) {
1532 menu_item = gtk_menu_item_new_with_label (l->data);
1533 g_signal_connect (G_OBJECT (menu_item),
1535 G_CALLBACK (chat_spelling_menu_activate_cb),
1537 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1540 empathy_spell_free_suggestions (suggestions);
1542 gtk_widget_show_all (menu);
1548 chat_text_send_cb (GtkMenuItem *menuitem,
1551 chat_input_text_view_send (chat);
1555 chat_input_populate_popup_cb (GtkTextView *view,
1559 EmpathyChatPriv *priv;
1560 GtkTextBuffer *buffer;
1561 GtkTextTagTable *table;
1564 GtkTextIter iter, start, end;
1567 EmpathyChatSpell *chat_spell;
1568 GtkWidget *spell_menu;
1569 EmpathySmileyManager *smiley_manager;
1570 GtkWidget *smiley_menu;
1573 priv = GET_PRIV (chat);
1574 buffer = gtk_text_view_get_buffer (view);
1576 /* Add the emoticon menu. */
1577 item = gtk_separator_menu_item_new ();
1578 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1579 gtk_widget_show (item);
1581 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1582 image = gtk_image_new_from_icon_name ("face-smile",
1583 GTK_ICON_SIZE_MENU);
1584 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1585 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1586 gtk_widget_show (item);
1588 smiley_manager = empathy_smiley_manager_dup_singleton ();
1589 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1590 chat_insert_smiley_activate_cb,
1592 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1593 g_object_unref (smiley_manager);
1595 /* Add the Send menu item. */
1596 gtk_text_buffer_get_bounds (buffer, &start, &end);
1597 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1598 if (!EMP_STR_EMPTY (str)) {
1599 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1600 g_signal_connect (G_OBJECT (item), "activate",
1601 G_CALLBACK (chat_text_send_cb), chat);
1602 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1603 gtk_widget_show (item);
1607 /* Add the spell check menu item. */
1608 table = gtk_text_buffer_get_tag_table (buffer);
1609 tag = gtk_text_tag_table_lookup (table, "misspelled");
1610 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1611 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1612 GTK_TEXT_WINDOW_WIDGET,
1615 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1617 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1618 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1620 str = gtk_text_buffer_get_text (buffer,
1621 &start, &end, FALSE);
1623 if (!EMP_STR_EMPTY (str)) {
1624 chat_spell = chat_spell_new (chat, str, start, end);
1625 g_object_set_data_full (G_OBJECT (menu),
1626 "chat_spell", chat_spell,
1627 (GDestroyNotify) chat_spell_free);
1629 item = gtk_separator_menu_item_new ();
1630 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1631 gtk_widget_show (item);
1633 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1634 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1635 GTK_ICON_SIZE_MENU);
1636 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1638 spell_menu = chat_spelling_build_menu (chat_spell);
1639 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1641 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1642 gtk_widget_show (item);
1647 chat_log_filter (EmpathyMessage *message,
1650 EmpathyChat *chat = (EmpathyChat *) user_data;
1651 EmpathyChatPriv *priv = GET_PRIV (chat);
1652 const GList *pending;
1654 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1656 for (; pending; pending = g_list_next (pending)) {
1657 if (empathy_message_equal (message, pending->data)) {
1666 chat_add_logs (EmpathyChat *chat)
1668 EmpathyChatPriv *priv = GET_PRIV (chat);
1669 gboolean is_chatroom;
1670 GList *messages, *l;
1676 /* Turn off scrolling temporarily */
1677 empathy_chat_view_scroll (chat->view, FALSE);
1679 /* Add messages from last conversation */
1680 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1682 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1690 for (l = messages; l; l = g_list_next (l)) {
1691 empathy_chat_view_append_message (chat->view, l->data);
1692 g_object_unref (l->data);
1695 g_list_free (messages);
1697 /* Turn back on scrolling */
1698 empathy_chat_view_scroll (chat->view, TRUE);
1702 chat_contacts_completion_func (const gchar *s1,
1706 gchar *tmp, *nick1, *nick2;
1713 return s1 ? -1 : +1;
1716 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1717 nick1 = g_utf8_casefold (tmp, -1);
1720 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1721 nick2 = g_utf8_casefold (tmp, -1);
1724 ret = strncmp (nick1, nick2, n);
1733 build_part_message (guint reason,
1735 EmpathyContact *actor,
1736 const gchar *message)
1738 GString *s = g_string_new ("");
1739 const gchar *actor_name = NULL;
1741 if (actor != NULL) {
1742 actor_name = empathy_contact_get_name (actor);
1745 /* Having an actor only really makes sense for a few actions... */
1747 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1748 g_string_append_printf (s, _("%s has disconnected"), name);
1750 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1751 if (actor_name != NULL) {
1752 /* translators: reverse the order of these arguments
1753 * if the kicked should come before the kicker in your locale.
1755 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
1758 g_string_append_printf (s, _("%s was kicked"), name);
1761 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1762 if (actor_name != NULL) {
1763 /* translators: reverse the order of these arguments
1764 * if the banned should come before the banner in your locale.
1766 g_string_append_printf (s, _("%1$s was banned by %2$s"),
1769 g_string_append_printf (s, _("%s was banned"), name);
1773 g_string_append_printf (s, _("%s has left the room"), name);
1776 if (!EMP_STR_EMPTY (message)) {
1777 /* Note to translators: this string is appended to
1778 * notifications like "foo has left the room", with the message
1779 * given by the user living the room. If this poses a problem,
1780 * please let us know. :-)
1782 g_string_append_printf (s, _(" (%s)"), message);
1785 return g_string_free (s, FALSE);
1789 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1790 EmpathyContact *contact,
1791 EmpathyContact *actor,
1797 EmpathyChatPriv *priv = GET_PRIV (chat);
1798 const gchar *name = empathy_contact_get_name (contact);
1801 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
1803 if (priv->block_events_timeout_id != 0)
1807 str = g_strdup_printf (_("%s has joined the room"),
1810 str = build_part_message (reason, name, actor, message);
1813 empathy_chat_view_append_event (chat->view, str);
1818 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
1819 EmpathyContact *old_contact,
1820 EmpathyContact *new_contact,
1825 EmpathyChatPriv *priv = GET_PRIV (chat);
1827 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
1829 if (priv->block_events_timeout_id == 0) {
1832 str = g_strdup_printf (_("%s is now known as %s"),
1833 empathy_contact_get_name (old_contact),
1834 empathy_contact_get_name (new_contact));
1835 empathy_chat_view_append_event (chat->view, str);
1842 chat_reset_size_request (gpointer widget)
1844 gtk_widget_set_size_request (widget, -1, -1);
1850 chat_update_contacts_visibility (EmpathyChat *chat)
1852 EmpathyChatPriv *priv = GET_PRIV (chat);
1854 GtkAllocation allocation;
1856 show = priv->remote_contact == NULL && priv->show_contacts;
1858 if (!priv->scrolled_window_contacts) {
1862 if (show && priv->contact_list_view == NULL) {
1863 EmpathyContactListStore *store;
1866 /* We are adding the contact list to the chat, we don't want the
1867 * chat view to become too small. If the chat view is already
1868 * smaller than 250 make sure that size won't change. If the
1869 * chat view is bigger the contact list will take some space on
1870 * it but we make sure the chat view don't become smaller than
1871 * 250. Relax the size request once the resize is done */
1872 gtk_widget_get_allocation (priv->vbox_left, &allocation);
1873 min_width = MIN (allocation.width, 250);
1874 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1875 g_idle_add (chat_reset_size_request, priv->vbox_left);
1877 if (priv->contacts_width > 0) {
1878 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1879 priv->contacts_width);
1882 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1883 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1884 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1885 EMPATHY_CONTACT_FEATURE_CHAT |
1886 EMPATHY_CONTACT_FEATURE_CALL |
1887 EMPATHY_CONTACT_FEATURE_LOG |
1888 EMPATHY_CONTACT_FEATURE_INFO));
1889 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1890 priv->contact_list_view);
1891 gtk_widget_show (priv->contact_list_view);
1892 gtk_widget_show (priv->scrolled_window_contacts);
1893 g_object_unref (store);
1895 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1896 gtk_widget_hide (priv->scrolled_window_contacts);
1897 if (priv->contact_list_view != NULL) {
1898 gtk_widget_destroy (priv->contact_list_view);
1899 priv->contact_list_view = NULL;
1905 empathy_chat_set_show_contacts (EmpathyChat *chat,
1908 EmpathyChatPriv *priv = GET_PRIV (chat);
1910 priv->show_contacts = show;
1912 chat_update_contacts_visibility (chat);
1914 g_object_notify (G_OBJECT (chat), "show-contacts");
1918 chat_remote_contact_changed_cb (EmpathyChat *chat)
1920 EmpathyChatPriv *priv = GET_PRIV (chat);
1922 if (priv->remote_contact != NULL) {
1923 g_object_unref (priv->remote_contact);
1924 priv->remote_contact = NULL;
1929 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1930 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1931 if (priv->remote_contact != NULL) {
1932 g_object_ref (priv->remote_contact);
1933 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1935 else if (priv->tp_chat != NULL) {
1938 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1939 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1942 chat_update_contacts_visibility (chat);
1944 g_object_notify (G_OBJECT (chat), "remote-contact");
1945 g_object_notify (G_OBJECT (chat), "id");
1949 chat_destroy_cb (EmpathyTpChat *tp_chat,
1952 EmpathyChatPriv *priv;
1954 priv = GET_PRIV (chat);
1956 if (!priv->tp_chat) {
1960 chat_composing_remove_timeout (chat);
1961 g_object_unref (priv->tp_chat);
1962 priv->tp_chat = NULL;
1963 g_object_notify (G_OBJECT (chat), "tp-chat");
1965 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1966 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1967 empathy_chat_set_show_contacts (chat, FALSE);
1971 show_pending_messages (EmpathyChat *chat) {
1972 EmpathyChatPriv *priv = GET_PRIV (chat);
1973 const GList *messages, *l;
1975 if (chat->view == NULL || priv->tp_chat == NULL)
1978 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1980 for (l = messages; l != NULL ; l = g_list_next (l)) {
1981 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1982 chat_message_received (chat, message);
1984 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1988 chat_create_ui (EmpathyChat *chat)
1990 EmpathyChatPriv *priv = GET_PRIV (chat);
1994 GtkTextBuffer *buffer;
1996 filename = empathy_file_lookup ("empathy-chat.ui",
1998 gui = empathy_builder_get_file (filename,
1999 "chat_widget", &priv->widget,
2000 "hpaned", &priv->hpaned,
2001 "vbox_left", &priv->vbox_left,
2002 "scrolled_window_chat", &priv->scrolled_window_chat,
2003 "scrolled_window_input", &priv->scrolled_window_input,
2004 "hbox_topic", &priv->hbox_topic,
2005 "label_topic", &priv->label_topic,
2006 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2007 "info_bar_vbox", &priv->info_bar_vbox,
2011 /* Add message view. */
2012 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
2013 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2014 and other types, even though it's non-editable and doesn't accept any
2015 drags. This steals drag motion for anything inside the scrollbars,
2016 making drag destinations on chat windows far less useful.
2018 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2019 g_signal_connect (chat->view, "focus_in_event",
2020 G_CALLBACK (chat_text_view_focus_in_event_cb),
2022 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2023 GTK_WIDGET (chat->view));
2024 gtk_widget_show (GTK_WIDGET (chat->view));
2026 /* Add input GtkTextView */
2027 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
2028 "pixels-above-lines", 2,
2029 "pixels-below-lines", 2,
2030 "pixels-inside-wrap", 1,
2033 "wrap-mode", GTK_WRAP_WORD_CHAR,
2035 g_signal_connect (chat->input_text_view, "key-press-event",
2036 G_CALLBACK (chat_input_key_press_event_cb),
2038 g_signal_connect (chat->input_text_view, "size-request",
2039 G_CALLBACK (chat_input_size_request_cb),
2041 g_signal_connect (chat->input_text_view, "realize",
2042 G_CALLBACK (chat_input_realize_cb),
2044 g_signal_connect (chat->input_text_view, "populate-popup",
2045 G_CALLBACK (chat_input_populate_popup_cb),
2047 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2048 g_signal_connect (buffer, "changed",
2049 G_CALLBACK (chat_input_text_buffer_changed_cb),
2051 gtk_text_buffer_create_tag (buffer, "misspelled",
2052 "underline", PANGO_UNDERLINE_ERROR,
2054 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2055 chat->input_text_view);
2056 gtk_widget_show (chat->input_text_view);
2058 /* Initialy hide the topic, will be shown if not empty */
2059 gtk_widget_hide (priv->hbox_topic);
2061 /* Set widget focus order */
2062 list = g_list_append (NULL, priv->scrolled_window_input);
2063 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2066 list = g_list_append (NULL, priv->vbox_left);
2067 list = g_list_append (list, priv->scrolled_window_contacts);
2068 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2071 list = g_list_append (NULL, priv->hpaned);
2072 list = g_list_append (list, priv->hbox_topic);
2073 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2076 /* Add the main widget in the chat widget */
2077 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2078 g_object_unref (gui);
2082 chat_size_request (GtkWidget *widget,
2083 GtkRequisition *requisition)
2085 GtkBin *bin = GTK_BIN (widget);
2088 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2089 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2091 child = gtk_bin_get_child (bin);
2093 if (child && gtk_widget_get_visible (child))
2095 GtkRequisition child_requisition;
2097 gtk_widget_size_request (child, &child_requisition);
2099 requisition->width += child_requisition.width;
2100 requisition->height += child_requisition.height;
2105 chat_size_allocate (GtkWidget *widget,
2106 GtkAllocation *allocation)
2108 GtkBin *bin = GTK_BIN (widget);
2109 GtkAllocation child_allocation;
2112 gtk_widget_set_allocation (widget, allocation);
2114 child = gtk_bin_get_child (bin);
2116 if (child && gtk_widget_get_visible (child))
2118 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2119 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2120 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2121 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2123 gtk_widget_size_allocate (child, &child_allocation);
2128 chat_finalize (GObject *object)
2131 EmpathyChatPriv *priv;
2133 chat = EMPATHY_CHAT (object);
2134 priv = GET_PRIV (chat);
2136 DEBUG ("Finalized: %p", object);
2138 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2139 g_list_free (priv->input_history);
2141 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2142 g_list_free (priv->compositors);
2144 chat_composing_remove_timeout (chat);
2146 g_object_unref (priv->account_manager);
2147 g_object_unref (priv->log_manager);
2149 if (priv->tp_chat) {
2150 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2151 chat_destroy_cb, chat);
2152 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2153 chat_message_received_cb, chat);
2154 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2155 chat_send_error_cb, chat);
2156 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2157 chat_state_changed_cb, chat);
2158 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2159 chat_property_changed_cb, chat);
2160 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2161 chat_members_changed_cb, chat);
2162 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2163 chat_remote_contact_changed_cb, chat);
2164 empathy_tp_chat_close (priv->tp_chat);
2165 g_object_unref (priv->tp_chat);
2167 if (priv->account) {
2168 g_object_unref (priv->account);
2170 if (priv->remote_contact) {
2171 g_object_unref (priv->remote_contact);
2174 if (priv->block_events_timeout_id) {
2175 g_source_remove (priv->block_events_timeout_id);
2179 g_free (priv->name);
2180 g_free (priv->subject);
2181 g_completion_free (priv->completion);
2183 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2187 chat_constructed (GObject *object)
2189 EmpathyChat *chat = EMPATHY_CHAT (object);
2191 chat_add_logs (chat);
2192 show_pending_messages (chat);
2196 empathy_chat_class_init (EmpathyChatClass *klass)
2198 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2199 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2201 object_class->finalize = chat_finalize;
2202 object_class->get_property = chat_get_property;
2203 object_class->set_property = chat_set_property;
2204 object_class->constructed = chat_constructed;
2206 widget_class->size_request = chat_size_request;
2207 widget_class->size_allocate = chat_size_allocate;
2209 g_object_class_install_property (object_class,
2211 g_param_spec_object ("tp-chat",
2213 "The tp chat object",
2214 EMPATHY_TYPE_TP_CHAT,
2217 G_PARAM_STATIC_STRINGS));
2218 g_object_class_install_property (object_class,
2220 g_param_spec_object ("account",
2221 "Account of the chat",
2222 "The account of the chat",
2225 G_PARAM_STATIC_STRINGS));
2226 g_object_class_install_property (object_class,
2228 g_param_spec_string ("id",
2230 "The id of the chat",
2233 G_PARAM_STATIC_STRINGS));
2234 g_object_class_install_property (object_class,
2236 g_param_spec_string ("name",
2238 "The name of the chat",
2241 G_PARAM_STATIC_STRINGS));
2242 g_object_class_install_property (object_class,
2244 g_param_spec_string ("subject",
2246 "The subject or topic of the chat",
2249 G_PARAM_STATIC_STRINGS));
2250 g_object_class_install_property (object_class,
2251 PROP_REMOTE_CONTACT,
2252 g_param_spec_object ("remote-contact",
2253 "The remote contact",
2254 "The remote contact is any",
2255 EMPATHY_TYPE_CONTACT,
2257 G_PARAM_STATIC_STRINGS));
2258 g_object_class_install_property (object_class,
2260 g_param_spec_boolean ("show-contacts",
2261 "Contacts' visibility",
2262 "The visibility of the contacts' list",
2265 G_PARAM_STATIC_STRINGS));
2267 signals[COMPOSING] =
2268 g_signal_new ("composing",
2269 G_OBJECT_CLASS_TYPE (object_class),
2273 g_cclosure_marshal_VOID__BOOLEAN,
2277 signals[NEW_MESSAGE] =
2278 g_signal_new ("new-message",
2279 G_OBJECT_CLASS_TYPE (object_class),
2283 g_cclosure_marshal_VOID__OBJECT,
2285 1, EMPATHY_TYPE_MESSAGE);
2287 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2291 chat_block_events_timeout_cb (gpointer data)
2293 EmpathyChatPriv *priv = GET_PRIV (data);
2295 priv->block_events_timeout_id = 0;
2301 account_manager_prepared_cb (GObject *source_object,
2302 GAsyncResult *result,
2305 GList *accounts, *l;
2306 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2307 EmpathyChat *chat = user_data;
2308 GError *error = NULL;
2310 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2311 DEBUG ("Failed to prepare the account manager: %s", error->message);
2312 g_error_free (error);
2316 accounts = tp_account_manager_get_valid_accounts (account_manager);
2318 for (l = accounts; l != NULL; l = l->next) {
2319 TpAccount *account = l->data;
2320 empathy_signal_connect_weak (account, "status-changed",
2321 G_CALLBACK (chat_new_connection_cb),
2325 g_list_free (accounts);
2329 empathy_chat_init (EmpathyChat *chat)
2331 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2332 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2335 priv->log_manager = empathy_log_manager_dup_singleton ();
2336 priv->contacts_width = -1;
2337 priv->input_history = NULL;
2338 priv->input_history_current = NULL;
2339 priv->account_manager = tp_account_manager_dup ();
2341 tp_account_manager_prepare_async (priv->account_manager, NULL,
2342 account_manager_prepared_cb, chat);
2344 empathy_conf_get_bool (empathy_conf_get (),
2345 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
2346 &priv->show_contacts);
2348 /* Block events for some time to avoid having "has come online" or
2349 * "joined" messages. */
2350 priv->block_events_timeout_id =
2351 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2353 /* Add nick name completion */
2354 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
2355 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2357 chat_create_ui (chat);
2361 empathy_chat_new (EmpathyTpChat *tp_chat)
2363 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2367 empathy_chat_get_tp_chat (EmpathyChat *chat)
2369 EmpathyChatPriv *priv = GET_PRIV (chat);
2371 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2373 return priv->tp_chat;
2376 static void display_password_info_bar (EmpathyChat *self,
2380 provide_password_cb (GObject *tp_chat,
2384 EmpathyChat *self = EMPATHY_CHAT (user_data);
2385 EmpathyChatPriv *priv = GET_PRIV (self);
2386 GError *error = NULL;
2388 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
2390 DEBUG ("error: %s", error->message);
2391 /* FIXME: what should we do if that's another error? Close the channel?
2392 * Display the raw D-Bus error to the user isn't very useful */
2393 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
2394 display_password_info_bar (self, TRUE);
2395 g_error_free (error);
2400 gtk_widget_set_sensitive (priv->hpaned, TRUE);
2401 gtk_widget_grab_focus (self->input_text_view);
2405 password_infobar_response_cb (GtkWidget *info_bar,
2409 EmpathyChatPriv *priv = GET_PRIV (self);
2411 const gchar *password;
2413 if (response_id != GTK_RESPONSE_OK)
2416 entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
2417 g_assert (entry != NULL);
2419 password = gtk_entry_get_text (GTK_ENTRY (entry));
2421 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
2422 provide_password_cb, self);
2425 gtk_widget_destroy (info_bar);
2429 password_entry_activate_cb (GtkWidget *entry,
2430 GtkWidget *info_bar)
2432 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2436 passwd_join_button_cb (GtkButton *button,
2437 GtkWidget *info_bar)
2439 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2443 display_password_info_bar (EmpathyChat *self,
2446 EmpathyChatPriv *priv = GET_PRIV (self);
2447 GtkWidget *info_bar;
2448 GtkWidget *content_area;
2455 GtkMessageType type;
2456 const gchar *msg, *button_label;
2459 /* Previous password was wrong */
2460 type = GTK_MESSAGE_ERROR;
2461 msg = _("Wrong password; please try again:");
2462 button_label = _("Retry");
2465 /* First time we're trying to join */
2466 type = GTK_MESSAGE_QUESTION;
2467 msg = _("This room is protected by a password:");
2468 button_label = _("Join");
2471 info_bar = gtk_info_bar_new ();
2472 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
2474 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2476 hbox = gtk_hbox_new (FALSE, 3);
2477 gtk_container_add (GTK_CONTAINER (content_area), hbox);
2480 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
2481 GTK_ICON_SIZE_DIALOG);
2482 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2485 label = gtk_label_new (msg);
2486 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
2488 /* Add password entry */
2489 entry = gtk_entry_new ();
2490 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
2491 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
2493 g_signal_connect (entry, "activate",
2494 G_CALLBACK (password_entry_activate_cb), info_bar);
2496 /* Focus the password entry once it's realized */
2497 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
2499 /* Add 'Join' button */
2500 alig = gtk_alignment_new (0, 0.5, 0, 0);
2502 button = gtk_button_new_with_label (button_label);
2503 gtk_container_add (GTK_CONTAINER (alig), button);
2504 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
2506 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
2509 g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
2511 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
2513 gtk_widget_show_all (hbox);
2515 g_signal_connect (info_bar, "response",
2516 G_CALLBACK (password_infobar_response_cb), self);
2518 gtk_widget_show_all (info_bar);
2522 chat_password_needed_changed_cb (EmpathyChat *self)
2524 EmpathyChatPriv *priv = GET_PRIV (self);
2526 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
2527 display_password_info_bar (self, FALSE);
2528 gtk_widget_set_sensitive (priv->hpaned, FALSE);
2533 empathy_chat_set_tp_chat (EmpathyChat *chat,
2534 EmpathyTpChat *tp_chat)
2536 EmpathyChatPriv *priv = GET_PRIV (chat);
2537 TpConnection *connection;
2538 GPtrArray *properties;
2540 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2541 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
2542 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
2544 if (priv->tp_chat) {
2548 if (priv->account) {
2549 g_object_unref (priv->account);
2552 priv->tp_chat = g_object_ref (tp_chat);
2553 connection = empathy_tp_chat_get_connection (priv->tp_chat);
2554 priv->account = g_object_ref (empathy_get_account_for_connection (connection));
2556 g_signal_connect (tp_chat, "destroy",
2557 G_CALLBACK (chat_destroy_cb),
2559 g_signal_connect (tp_chat, "message-received",
2560 G_CALLBACK (chat_message_received_cb),
2562 g_signal_connect (tp_chat, "send-error",
2563 G_CALLBACK (chat_send_error_cb),
2565 g_signal_connect (tp_chat, "chat-state-changed",
2566 G_CALLBACK (chat_state_changed_cb),
2568 g_signal_connect (tp_chat, "property-changed",
2569 G_CALLBACK (chat_property_changed_cb),
2571 g_signal_connect (tp_chat, "members-changed",
2572 G_CALLBACK (chat_members_changed_cb),
2574 g_signal_connect (tp_chat, "member-renamed",
2575 G_CALLBACK (chat_member_renamed_cb),
2577 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
2578 G_CALLBACK (chat_remote_contact_changed_cb),
2580 g_signal_connect_swapped (tp_chat, "notify::password-needed",
2581 G_CALLBACK (chat_password_needed_changed_cb),
2584 /* Get initial value of properties */
2585 properties = empathy_tp_chat_get_properties (priv->tp_chat);
2586 if (properties != NULL) {
2589 for (i = 0; i < properties->len; i++) {
2590 EmpathyTpChatProperty *property;
2592 property = g_ptr_array_index (properties, i);
2593 if (property->value == NULL)
2596 chat_property_changed_cb (priv->tp_chat,
2603 chat_remote_contact_changed_cb (chat);
2605 if (chat->input_text_view) {
2606 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2607 if (priv->block_events_timeout_id == 0) {
2608 empathy_chat_view_append_event (chat->view, _("Connected"));
2612 g_object_notify (G_OBJECT (chat), "tp-chat");
2613 g_object_notify (G_OBJECT (chat), "id");
2614 g_object_notify (G_OBJECT (chat), "account");
2616 /* This is a noop when tp-chat is set at object construction time and causes
2617 * the pending messages to be show when it's set on the object after it has
2619 show_pending_messages (chat);
2621 /* check if a password is needed */
2622 chat_password_needed_changed_cb (chat);
2626 empathy_chat_get_account (EmpathyChat *chat)
2628 EmpathyChatPriv *priv = GET_PRIV (chat);
2630 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2632 return priv->account;
2636 empathy_chat_get_id (EmpathyChat *chat)
2638 EmpathyChatPriv *priv = GET_PRIV (chat);
2640 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2646 empathy_chat_get_name (EmpathyChat *chat)
2648 EmpathyChatPriv *priv = GET_PRIV (chat);
2651 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2654 if (!ret && priv->remote_contact) {
2655 ret = empathy_contact_get_name (priv->remote_contact);
2661 return ret ? ret : _("Conversation");
2665 empathy_chat_get_subject (EmpathyChat *chat)
2667 EmpathyChatPriv *priv = GET_PRIV (chat);
2669 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2671 return priv->subject;
2675 empathy_chat_get_remote_contact (EmpathyChat *chat)
2677 EmpathyChatPriv *priv = GET_PRIV (chat);
2679 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2681 return priv->remote_contact;
2685 empathy_chat_get_contact_menu (EmpathyChat *chat)
2687 EmpathyChatPriv *priv = GET_PRIV (chat);
2688 GtkWidget *menu = NULL;
2690 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2692 if (priv->remote_contact) {
2693 menu = empathy_contact_menu_new (priv->remote_contact,
2694 EMPATHY_CONTACT_FEATURE_CALL |
2695 EMPATHY_CONTACT_FEATURE_LOG |
2696 EMPATHY_CONTACT_FEATURE_INFO);
2698 else if (priv->contact_list_view) {
2699 EmpathyContactListView *view;
2701 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
2702 menu = empathy_contact_list_view_get_contact_menu (view);
2709 empathy_chat_clear (EmpathyChat *chat)
2711 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2713 empathy_chat_view_clear (chat->view);
2717 empathy_chat_scroll_down (EmpathyChat *chat)
2719 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2721 empathy_chat_view_scroll_down (chat->view);
2725 empathy_chat_cut (EmpathyChat *chat)
2727 GtkTextBuffer *buffer;
2729 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2731 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2732 if (gtk_text_buffer_get_has_selection (buffer)) {
2733 GtkClipboard *clipboard;
2735 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2737 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2742 empathy_chat_copy (EmpathyChat *chat)
2744 GtkTextBuffer *buffer;
2746 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2748 if (empathy_chat_view_get_has_selection (chat->view)) {
2749 empathy_chat_view_copy_clipboard (chat->view);
2753 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2754 if (gtk_text_buffer_get_has_selection (buffer)) {
2755 GtkClipboard *clipboard;
2757 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2759 gtk_text_buffer_copy_clipboard (buffer, clipboard);
2764 empathy_chat_paste (EmpathyChat *chat)
2766 GtkTextBuffer *buffer;
2767 GtkClipboard *clipboard;
2768 EmpathyChatPriv *priv;
2770 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2772 priv = GET_PRIV (chat);
2774 if (priv->tp_chat == NULL ||
2775 !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
2778 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2779 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2781 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
2785 empathy_chat_correct_word (EmpathyChat *chat,
2788 const gchar *new_word)
2790 GtkTextBuffer *buffer;
2792 g_return_if_fail (chat != NULL);
2793 g_return_if_fail (new_word != NULL);
2795 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2797 gtk_text_buffer_delete (buffer, start, end);
2798 gtk_text_buffer_insert (buffer, start,
2804 empathy_chat_is_room (EmpathyChat *chat)
2806 EmpathyChatPriv *priv = GET_PRIV (chat);
2808 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2810 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
2814 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
2816 EmpathyChatPriv *priv = GET_PRIV (self);
2818 g_return_val_if_fail (EMPATHY_IS_CHAT (self), FALSE);
2820 return priv->unread_messages;
2823 /* called when the messages have been read by user */
2825 empathy_chat_messages_read (EmpathyChat *self)
2827 EmpathyChatPriv *priv = GET_PRIV (self);
2829 g_return_if_fail (EMPATHY_IS_CHAT (self));
2831 priv->unread_messages = 0;