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 <telepathy-logger/log-manager.h>
43 #include <libempathy/empathy-log-manager.h>
44 #endif /* ENABLE_TPL */
45 #include <libempathy/empathy-contact-list.h>
46 #include <libempathy/empathy-utils.h>
47 #include <libempathy/empathy-dispatcher.h>
49 #include "empathy-chat.h"
50 #include "empathy-conf.h"
51 #include "empathy-spell.h"
52 #include "empathy-contact-list-store.h"
53 #include "empathy-contact-list-view.h"
54 #include "empathy-contact-menu.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-smiley-manager.h"
58 #include "empathy-ui-utils.h"
59 #include "empathy-string-parser.h"
61 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
62 #include <libempathy/empathy-debug.h>
64 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
65 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
66 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
67 #define MAX_INPUT_HEIGHT 150
68 #define COMPOSING_STOP_TIMEOUT 5
70 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
72 EmpathyTpChat *tp_chat;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
81 TplLogManager *log_manager;
83 EmpathyLogManager *log_manager;
84 #endif /* ENABLE_TPL */
85 TpAccountManager *account_manager;
87 GList *input_history_current;
89 GCompletion *completion;
90 guint composing_stop_timeout_id;
91 guint block_events_timeout_id;
92 TpHandleType handle_type;
94 gboolean has_input_vscroll;
100 GtkWidget *scrolled_window_chat;
101 GtkWidget *scrolled_window_input;
102 GtkWidget *scrolled_window_contacts;
103 GtkWidget *hbox_topic;
104 GtkWidget *expander_topic;
105 GtkWidget *label_topic;
106 GtkWidget *contact_list_view;
107 GtkWidget *info_bar_vbox;
108 GtkWidget *search_bar;
110 guint unread_messages;
111 /* TRUE if the pending messages can be displayed. This is to avoid to show
112 * pending messages *before* messages from logs. (#603980) */
113 gboolean can_show_pending;
115 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
116 * be differently handled since it introduces another race condition, which
117 * is really hard to occur, but still possible.
119 * With the current workaround (which has the race above), we need to be
120 * sure to ACK any pending messages only when the retrieval of backlogs is
121 * finished, that's why using retrieving_backlogs flag.
122 * empathy_chat_messages_read () will check this variable and not ACK
123 * anything when TRUE. It will be set TRUE at chat_constructed () and set
124 * back to FALSE when the backlog has been retrieved and the pending
125 * messages actually showed to the user.
127 * Race condition introduced with this workaround:
128 * Scenario: a message is pending, the user is notified and selects the tab.
129 * the tab with a pending message is focused before the messages are properly
130 * shown (since the preparation of the window is slower AND async WRT the
131 * tab showing), which means the user won't see any new messages (rare but
132 * possible), if he/she will change tab focus before the messages are
133 * properly shown, the tab will be set as 'seen' and the user won't be
134 * notified again about the already notified pending messages when the
135 * messages in tab will be properly shown */
136 gboolean retrieving_backlogs;
140 gchar *text; /* Original message that was specified
141 * upon entry creation. */
142 gchar *modified_text; /* Message that was modified by user.
143 * When no modifications were made, it is NULL */
163 static guint signals[LAST_SIGNAL] = { 0 };
165 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
168 chat_get_property (GObject *object,
173 EmpathyChat *chat = EMPATHY_CHAT (object);
174 EmpathyChatPriv *priv = GET_PRIV (object);
178 g_value_set_object (value, priv->tp_chat);
181 g_value_set_object (value, priv->account);
184 g_value_set_string (value, empathy_chat_get_name (chat));
187 g_value_set_string (value, priv->id);
190 g_value_set_string (value, priv->subject);
192 case PROP_REMOTE_CONTACT:
193 g_value_set_object (value, priv->remote_contact);
195 case PROP_SHOW_CONTACTS:
196 g_value_set_boolean (value, priv->show_contacts);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
205 chat_set_property (GObject *object,
210 EmpathyChat *chat = EMPATHY_CHAT (object);
214 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
216 case PROP_SHOW_CONTACTS:
217 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
226 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
230 EmpathyChat *chat = EMPATHY_CHAT (user_data);
231 EmpathyTpChat *tpchat;
234 empathy_chat_view_append_event (chat->view,
235 _("Failed to reconnect this chat"));
239 tpchat = EMPATHY_TP_CHAT (
240 empathy_dispatch_operation_get_channel_wrapper (dispatch));
242 if (empathy_dispatch_operation_claim (dispatch)) {
243 empathy_chat_set_tp_chat (chat, tpchat);
248 reconnected_connection_ready_cb (TpConnection *connection,
252 EmpathyChat *chat = user_data;
253 EmpathyChatPriv *priv = GET_PRIV (chat);
256 DEBUG ("connection is not ready: %s", error->message);
260 DEBUG ("Account reconnected, request a new Text channel");
262 switch (priv->handle_type) {
263 case TP_HANDLE_TYPE_CONTACT:
264 empathy_dispatcher_chat_with_contact_id (
265 connection, priv->id,
266 chat_connect_channel_reconnected,
269 case TP_HANDLE_TYPE_ROOM:
270 empathy_dispatcher_join_muc (connection,
272 chat_connect_channel_reconnected,
276 g_assert_not_reached ();
281 g_object_unref (chat);
285 chat_new_connection_cb (TpAccount *account,
289 gchar *dbus_error_name,
293 EmpathyChatPriv *priv = GET_PRIV (chat);
294 TpConnection *connection;
296 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
299 connection = tp_account_get_connection (account);
301 if (priv->tp_chat != NULL || account != priv->account ||
302 priv->handle_type == TP_HANDLE_TYPE_NONE ||
303 EMP_STR_EMPTY (priv->id))
307 tp_connection_call_when_ready (connection, reconnected_connection_ready_cb,
312 chat_composing_remove_timeout (EmpathyChat *chat)
314 EmpathyChatPriv *priv;
316 priv = GET_PRIV (chat);
318 if (priv->composing_stop_timeout_id) {
319 g_source_remove (priv->composing_stop_timeout_id);
320 priv->composing_stop_timeout_id = 0;
325 chat_composing_stop_timeout_cb (EmpathyChat *chat)
327 EmpathyChatPriv *priv;
329 priv = GET_PRIV (chat);
331 priv->composing_stop_timeout_id = 0;
332 empathy_tp_chat_set_state (priv->tp_chat,
333 TP_CHANNEL_CHAT_STATE_PAUSED);
339 chat_composing_start (EmpathyChat *chat)
341 EmpathyChatPriv *priv;
343 priv = GET_PRIV (chat);
345 if (priv->composing_stop_timeout_id) {
346 /* Just restart the timeout */
347 chat_composing_remove_timeout (chat);
349 empathy_tp_chat_set_state (priv->tp_chat,
350 TP_CHANNEL_CHAT_STATE_COMPOSING);
353 priv->composing_stop_timeout_id = g_timeout_add_seconds (
354 COMPOSING_STOP_TIMEOUT,
355 (GSourceFunc) chat_composing_stop_timeout_cb,
360 chat_composing_stop (EmpathyChat *chat)
362 EmpathyChatPriv *priv;
364 priv = GET_PRIV (chat);
366 chat_composing_remove_timeout (chat);
367 empathy_tp_chat_set_state (priv->tp_chat,
368 TP_CHANNEL_CHAT_STATE_ACTIVE);
372 chat_input_history_entry_cmp (InputHistoryEntry *entry,
375 if (!tp_strdiff (entry->text, text)) {
376 if (entry->modified_text != NULL) {
377 /* Modified entry and single string cannot be equal. */
385 static InputHistoryEntry *
386 chat_input_history_entry_new_with_text (const gchar *text)
388 InputHistoryEntry *entry;
389 entry = g_slice_new0 (InputHistoryEntry);
390 entry->text = g_strdup (text);
396 chat_input_history_entry_free (InputHistoryEntry *entry)
398 g_free (entry->text);
399 g_free (entry->modified_text);
400 g_slice_free (InputHistoryEntry, entry);
404 chat_input_history_entry_revert (InputHistoryEntry *entry)
406 g_free (entry->modified_text);
407 entry->modified_text = NULL;
411 chat_input_history_entry_update_text (InputHistoryEntry *entry,
416 if (!tp_strdiff (text, entry->text)) {
417 g_free (entry->modified_text);
418 entry->modified_text = NULL;
422 old = entry->modified_text;
423 entry->modified_text = g_strdup (text);
428 chat_input_history_entry_get_text (InputHistoryEntry *entry)
434 if (entry->modified_text != NULL) {
435 return entry->modified_text;
441 chat_input_history_remove_item (GList *list,
444 list = g_list_remove_link (list, item);
445 chat_input_history_entry_free (item->data);
446 g_list_free_1 (item);
451 chat_input_history_revert (EmpathyChat *chat)
453 EmpathyChatPriv *priv;
457 InputHistoryEntry *entry;
459 priv = GET_PRIV (chat);
460 list = priv->input_history;
463 DEBUG ("No input history");
467 /* Delete temporary entry */
468 if (priv->input_history_current != NULL) {
470 list = chat_input_history_remove_item (list, item1);
471 if (priv->input_history_current == item1) {
472 /* Removed temporary entry was current entry */
473 priv->input_history = list;
474 priv->input_history_current = NULL;
479 /* There is no entry to revert */
483 /* Restore the current history entry to original value */
484 item1 = priv->input_history_current;
486 chat_input_history_entry_revert (entry);
488 /* Remove restored entry if there is other occurance before this entry */
489 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
490 (GCompareFunc) chat_input_history_entry_cmp);
491 if (item2 != item1) {
492 list = chat_input_history_remove_item (list, item1);
495 /* Remove other occurance of the restored entry */
496 item2 = g_list_find_custom (item1->next,
497 chat_input_history_entry_get_text (entry),
498 (GCompareFunc) chat_input_history_entry_cmp);
500 list = chat_input_history_remove_item (list, item2);
504 priv->input_history_current = NULL;
505 priv->input_history = list;
509 chat_input_history_add (EmpathyChat *chat,
513 EmpathyChatPriv *priv;
516 InputHistoryEntry *entry;
518 priv = GET_PRIV (chat);
520 list = priv->input_history;
522 /* Remove any other occurances of this entry, if not temporary */
524 while ((item = g_list_find_custom (list, str,
525 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
526 list = chat_input_history_remove_item (list, item);
529 /* Trim the list to the last 10 items */
530 while (g_list_length (list) > 10) {
531 item = g_list_last (list);
533 list = chat_input_history_remove_item (list, item);
541 entry = chat_input_history_entry_new_with_text (str);
542 list = g_list_prepend (list, entry);
544 /* Set the list and the current item pointer */
545 priv->input_history = list;
547 priv->input_history_current = list;
550 priv->input_history_current = NULL;
555 chat_input_history_get_next (EmpathyChat *chat)
557 EmpathyChatPriv *priv;
561 priv = GET_PRIV (chat);
563 if (priv->input_history == NULL) {
564 DEBUG ("No input history, next entry is NULL");
567 g_assert (priv->input_history_current != NULL);
569 if ((item = g_list_next (priv->input_history_current)) == NULL)
571 item = priv->input_history_current;
574 msg = chat_input_history_entry_get_text (item->data);
576 DEBUG ("Returning next entry: '%s'", msg);
578 priv->input_history_current = item;
584 chat_input_history_get_prev (EmpathyChat *chat)
586 EmpathyChatPriv *priv;
590 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
592 priv = GET_PRIV (chat);
594 if (priv->input_history == NULL) {
595 DEBUG ("No input history, previous entry is NULL");
599 if (priv->input_history_current == NULL)
603 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
605 item = priv->input_history_current;
608 msg = chat_input_history_entry_get_text (item->data);
610 DEBUG ("Returning previous entry: '%s'", msg);
612 priv->input_history_current = item;
618 chat_input_history_update (EmpathyChat *chat,
619 GtkTextBuffer *buffer)
621 EmpathyChatPriv *priv;
622 GtkTextIter start, end;
624 InputHistoryEntry *entry;
626 priv = GET_PRIV (chat);
628 gtk_text_buffer_get_bounds (buffer, &start, &end);
629 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
631 if (priv->input_history_current == NULL) {
632 /* Add the current text temporarily to the history */
633 chat_input_history_add (chat, text, TRUE);
638 /* Save the changes in the history */
639 entry = priv->input_history_current->data;
640 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
641 chat_input_history_entry_update_text (entry, text);
648 chat_command_join_cb (EmpathyDispatchOperation *dispatch,
652 EmpathyChat *chat = user_data;
655 DEBUG ("Error: %s", error->message);
656 empathy_chat_view_append_event (chat->view,
657 _("Failed to join chat room"));
664 } ChatCommandMsgData;
667 chat_command_msg_cb (EmpathyDispatchOperation *dispatch,
671 ChatCommandMsgData *data = user_data;
674 empathy_chat_view_append_event (data->chat->view,
675 _("Failed to open private chat"));
679 if (!EMP_STR_EMPTY (data->message)) {
680 EmpathyTpChat *tpchat;
681 EmpathyMessage *message;
683 tpchat = EMPATHY_TP_CHAT (
684 empathy_dispatch_operation_get_channel_wrapper (dispatch));
686 message = empathy_message_new (data->message);
687 empathy_tp_chat_send (tpchat, message);
688 g_object_unref (message);
692 g_free (data->message);
693 g_slice_free (ChatCommandMsgData, data);
697 chat_command_clear (EmpathyChat *chat,
700 empathy_chat_view_clear (chat->view);
704 chat_command_topic (EmpathyChat *chat,
707 EmpathyChatPriv *priv = GET_PRIV (chat);
708 EmpathyTpChatProperty *property;
709 GValue value = {0, };
711 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
712 if (property == NULL) {
713 empathy_chat_view_append_event (chat->view,
714 _("Topic not supported on this conversation"));
718 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
719 empathy_chat_view_append_event (chat->view,
720 _("You are not allowed to change the topic"));
724 g_value_init (&value, G_TYPE_STRING);
725 g_value_set_string (&value, strv[1]);
726 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
727 g_value_unset (&value);
731 chat_command_join (EmpathyChat *chat,
735 EmpathyChatPriv *priv = GET_PRIV (chat);
737 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
739 while (rooms[i] != NULL) {
740 /* ignore empty strings */
741 if (!EMP_STR_EMPTY (rooms[i])) {
742 TpConnection *connection;
744 connection = empathy_tp_chat_get_connection (priv->tp_chat);
745 empathy_dispatcher_join_muc (connection, rooms[i],
746 chat_command_join_cb,
755 chat_command_msg_internal (EmpathyChat *chat,
756 const gchar *contact_id,
757 const gchar *message)
759 EmpathyChatPriv *priv = GET_PRIV (chat);
760 TpConnection *connection;
761 ChatCommandMsgData *data;
763 /* FIXME: We should probably search in members alias. But this
764 * is enough for IRC */
765 data = g_slice_new (ChatCommandMsgData);
767 data->message = g_strdup (message);
768 connection = empathy_tp_chat_get_connection (priv->tp_chat);
769 empathy_dispatcher_chat_with_contact_id (connection, contact_id,
775 chat_command_query (EmpathyChat *chat,
778 /* If <message> part is not defined,
779 * strv[2] will be the terminal NULL */
780 chat_command_msg_internal (chat, strv[1], strv[2]);
784 chat_command_msg (EmpathyChat *chat,
787 chat_command_msg_internal (chat, strv[1], strv[2]);
791 chat_command_nick (EmpathyChat *chat,
794 EmpathyChatPriv *priv = GET_PRIV (chat);
795 TpConnection *connection;
796 GHashTable *new_alias;
799 connection = tp_account_get_connection (priv->account);
800 handle = tp_connection_get_self_handle (connection);
801 new_alias = g_hash_table_new (g_direct_hash, g_direct_equal);
802 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), strv[1]);
804 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
805 new_alias, NULL, NULL, NULL, NULL);
807 g_hash_table_destroy (new_alias);
811 chat_command_me (EmpathyChat *chat,
814 EmpathyChatPriv *priv = GET_PRIV (chat);
815 EmpathyMessage *message;
817 message = empathy_message_new (strv[1]);
818 empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
819 empathy_tp_chat_send (priv->tp_chat, message);
820 g_object_unref (message);
824 chat_command_say (EmpathyChat *chat,
827 EmpathyChatPriv *priv = GET_PRIV (chat);
828 EmpathyMessage *message;
830 message = empathy_message_new (strv[1]);
831 empathy_tp_chat_send (priv->tp_chat, message);
832 g_object_unref (message);
835 static void chat_command_help (EmpathyChat *chat, GStrv strv);
837 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
843 ChatCommandFunc func;
847 static ChatCommandItem commands[] = {
848 {"clear", 1, 1, chat_command_clear,
849 N_("/clear: clear all messages from the current conversation")},
851 {"topic", 2, 2, chat_command_topic,
852 N_("/topic <topic>: set the topic of the current conversation")},
854 {"join", 2, 2, chat_command_join,
855 N_("/join <chat room ID>: join a new chat room")},
857 {"j", 2, 2, chat_command_join,
858 N_("/j <chat room ID>: join a new chat room")},
860 {"query", 2, 3, chat_command_query,
861 N_("/query <contact ID> [<message>]: open a private chat")},
863 {"msg", 3, 3, chat_command_msg,
864 N_("/msg <contact ID> <message>: open a private chat")},
866 {"nick", 2, 2, chat_command_nick,
867 N_("/nick <nickname>: change your nickname on the current server")},
869 {"me", 2, 2, chat_command_me,
870 N_("/me <message>: send an ACTION message to the current conversation")},
872 {"say", 2, 2, chat_command_say,
873 N_("/say <message>: send <message> to the current conversation. "
874 "This is used to send a message starting with a '/'. For example: "
875 "\"/say /join is used to join a new chat room\"")},
877 {"help", 1, 2, chat_command_help,
878 N_("/help [<command>]: show all supported commands. "
879 "If <command> is defined, show its usage.")},
883 chat_command_show_help (EmpathyChat *chat,
884 ChatCommandItem *item)
888 str = g_strdup_printf (_("Usage: %s"), _(item->help));
889 empathy_chat_view_append_event (chat->view, str);
894 chat_command_help (EmpathyChat *chat,
899 /* If <command> part is not defined,
900 * strv[1] will be the terminal NULL */
901 if (strv[1] == NULL) {
902 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
903 empathy_chat_view_append_event (chat->view,
904 _(commands[i].help));
909 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
910 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
911 chat_command_show_help (chat, &commands[i]);
916 empathy_chat_view_append_event (chat->view,
917 _("Unknown command"));
921 chat_command_parse (const gchar *text, guint max_parts)
926 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
928 array = g_ptr_array_sized_new (max_parts + 1);
929 while (max_parts > 1) {
932 /* Skip white spaces */
933 while (g_ascii_isspace (*text)) {
937 /* Search the end of this part, until first space. */
938 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
944 item = g_strndup (text, end - text);
945 g_ptr_array_add (array, item);
946 DEBUG ("\tITEM: \"%s\"", item);
952 /* Append last part if not empty */
953 item = g_strstrip (g_strdup (text));
954 if (!EMP_STR_EMPTY (item)) {
955 g_ptr_array_add (array, item);
956 DEBUG ("\tITEM: \"%s\"", item);
961 /* Make the array NULL-terminated */
962 g_ptr_array_add (array, NULL);
964 return (GStrv) g_ptr_array_free (array, FALSE);
968 has_prefix_case (const gchar *s,
971 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
975 chat_send (EmpathyChat *chat,
978 EmpathyChatPriv *priv;
979 EmpathyMessage *message;
982 if (EMP_STR_EMPTY (msg)) {
986 priv = GET_PRIV (chat);
988 chat_input_history_add (chat, msg, FALSE);
991 gboolean second_slash = FALSE;
992 const gchar *iter = msg + 1;
994 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
999 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1002 c = *(msg + 1 + strlen (commands[i].prefix));
1003 if (c != '\0' && !g_ascii_isspace (c)) {
1007 /* We can't use g_strsplit here because it does
1008 * not deal correctly if we have more than one space
1010 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1012 strv_len = g_strv_length (strv);
1013 if (strv_len < commands[i].min_parts ||
1014 strv_len > commands[i].max_parts) {
1015 chat_command_show_help (chat, &commands[i]);
1020 commands[i].func (chat, strv);
1025 /* Also allow messages with two slashes before the
1026 * first space, so it is possible to send a /unix/path.
1027 * This heuristic is kind of crap. */
1028 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1030 second_slash = TRUE;
1036 if (!second_slash) {
1037 empathy_chat_view_append_event (chat->view,
1038 _("Unknown command; see /help for the available"
1044 message = empathy_message_new (msg);
1045 empathy_tp_chat_send (priv->tp_chat, message);
1046 g_object_unref (message);
1050 chat_input_text_view_send (EmpathyChat *chat)
1052 EmpathyChatPriv *priv;
1053 GtkTextBuffer *buffer;
1054 GtkTextIter start, end;
1057 priv = GET_PRIV (chat);
1059 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1061 gtk_text_buffer_get_bounds (buffer, &start, &end);
1062 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1064 /* clear the input field */
1065 gtk_text_buffer_set_text (buffer, "", -1);
1066 /* delete input history modifications */
1067 chat_input_history_revert (chat);
1069 chat_send (chat, msg);
1074 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1075 EmpathyContact *contact,
1076 TpChannelChatState state,
1079 EmpathyChatPriv *priv;
1081 gboolean was_composing;
1083 priv = GET_PRIV (chat);
1085 if (empathy_contact_is_user (contact)) {
1086 /* We don't care about our own chat state */
1090 was_composing = (priv->compositors != NULL);
1092 /* Find the contact in the list. After that l is the list elem or NULL */
1093 for (l = priv->compositors; l; l = l->next) {
1094 if (contact == l->data) {
1100 case TP_CHANNEL_CHAT_STATE_GONE:
1101 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1102 case TP_CHANNEL_CHAT_STATE_PAUSED:
1103 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1104 /* Contact is not composing */
1106 priv->compositors = g_list_remove_link (priv->compositors, l);
1107 g_object_unref (l->data);
1111 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1112 /* Contact is composing */
1114 priv->compositors = g_list_prepend (priv->compositors,
1115 g_object_ref (contact));
1119 g_assert_not_reached ();
1122 DEBUG ("Was composing: %s now composing: %s",
1123 was_composing ? "yes" : "no",
1124 priv->compositors ? "yes" : "no");
1126 if ((was_composing && !priv->compositors) ||
1127 (!was_composing && priv->compositors)) {
1128 /* Composing state changed */
1129 g_signal_emit (chat, signals[COMPOSING], 0,
1130 priv->compositors != NULL);
1135 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
1137 EmpathyChatPriv *priv = GET_PRIV (chat);
1138 EmpathyContact *sender;
1140 sender = empathy_message_get_sender (message);
1142 DEBUG ("Appending new message from %s (%d)",
1143 empathy_contact_get_name (sender),
1144 empathy_contact_get_handle (sender));
1146 empathy_chat_view_append_message (chat->view, message);
1148 /* We received a message so the contact is no longer composing */
1149 chat_state_changed_cb (priv->tp_chat, sender,
1150 TP_CHANNEL_CHAT_STATE_ACTIVE,
1153 priv->unread_messages++;
1154 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
1158 chat_message_received_cb (EmpathyTpChat *tp_chat,
1159 EmpathyMessage *message,
1162 chat_message_received (chat, message);
1166 chat_send_error_cb (EmpathyTpChat *tp_chat,
1167 const gchar *message_body,
1168 TpChannelTextSendError error_code,
1174 switch (error_code) {
1175 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1176 error = _("offline");
1178 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1179 error = _("invalid contact");
1181 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1182 error = _("permission denied");
1184 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1185 error = _("too long message");
1187 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1188 error = _("not implemented");
1191 error = _("unknown");
1195 str = g_strdup_printf (_("Error sending message '%s': %s"),
1198 empathy_chat_view_append_event (chat->view, str);
1202 /* WARNING: EXPLICIT CONTENT, keep away childrens!
1204 * When a GtkLabel is set to wrap, it assume and hardcoded width. To change
1205 * that width we have to set a size request on the label... but that's not
1206 * possible because we want the window to be able to shrink, so we MUST request
1207 * width of 1. Note that the height of a wrapping label depends on its width.
1209 * To work around that, here is what happens:
1210 * 1) size-request is first called, an hardcoded small width is requested by
1211 * GtkLabel, which means also a too big height. We do nothing.
1212 * 2) size-allocate is called with the full width available, that's the width
1213 * we really want to make wrap the label. We save that width and restart a
1214 * size-request/size-allocate round.
1215 * 3) size-request is called a 2nd time, now we can tell the pango layout its
1216 * width (we can't do that in step 2 because GtkLabel::size-request recreate
1217 * the layout each time). When the layout has its width, we can know the
1218 * height of the label and set its requisition. The width request is set to
1219 * 1px to make sure the window can shrink, the layout will fill all the
1220 * available width anyway.
1223 chat_topic_label_size_request_cb (GtkLabel *label,
1224 GtkRequisition *requisition,
1227 EmpathyChatPriv *priv = GET_PRIV (chat);
1229 if (gtk_label_get_line_wrap (label) && priv->topic_width > 0) {
1230 PangoLayout *layout;
1231 PangoRectangle rect;
1234 layout = gtk_label_get_layout (label);
1235 pango_layout_set_width (layout, priv->topic_width * PANGO_SCALE);
1236 pango_layout_get_extents (layout, NULL, &rect);
1237 gtk_misc_get_padding (GTK_MISC (label), NULL, &ypad);
1239 requisition->width = 1;
1240 requisition->height = PANGO_PIXELS (rect.height) + ypad * 2;
1245 chat_topic_label_size_allocate_cb (GtkLabel *label,
1246 GtkAllocation *allocation,
1249 EmpathyChatPriv *priv = GET_PRIV (chat);
1251 if (!gtk_label_get_line_wrap (label)) {
1252 priv->topic_width = -1;
1254 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1255 gtk_widget_show (priv->expander_topic);
1257 gtk_widget_hide (priv->expander_topic);
1262 if (priv->topic_width != allocation->width) {
1263 priv->topic_width = allocation->width;
1264 gtk_widget_queue_resize (GTK_WIDGET (label));
1269 chat_topic_expander_activate_cb (GtkExpander *expander,
1270 GParamSpec *param_spec,
1273 EmpathyChatPriv *priv = GET_PRIV (chat);
1275 if (gtk_expander_get_expanded (expander)) {
1276 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1277 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1279 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1280 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1285 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1290 EmpathyChatPriv *priv = GET_PRIV (chat);
1292 if (!tp_strdiff (name, "subject")) {
1293 g_free (priv->subject);
1294 priv->subject = g_value_dup_string (value);
1295 g_object_notify (G_OBJECT (chat), "subject");
1297 if (EMP_STR_EMPTY (priv->subject)) {
1298 gtk_widget_hide (priv->hbox_topic);
1300 gchar *markup_topic;
1303 markup_topic = empathy_add_link_markup (priv->subject);
1304 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1305 _("Topic:"), markup_topic);
1307 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1308 g_free (markup_text);
1309 g_free (markup_topic);
1311 gtk_widget_show (priv->hbox_topic);
1313 if (priv->block_events_timeout_id == 0) {
1316 if (!EMP_STR_EMPTY (priv->subject)) {
1317 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1319 str = g_strdup (_("No topic defined"));
1321 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1325 else if (!tp_strdiff (name, "name")) {
1326 g_free (priv->name);
1327 priv->name = g_value_dup_string (value);
1328 g_object_notify (G_OBJECT (chat), "name");
1333 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1336 GtkTextIter start, end;
1338 gboolean spell_checker = FALSE;
1340 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1341 chat_composing_stop (chat);
1343 chat_composing_start (chat);
1346 empathy_conf_get_bool (empathy_conf_get (),
1347 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
1350 gtk_text_buffer_get_start_iter (buffer, &start);
1352 if (!spell_checker) {
1353 gtk_text_buffer_get_end_iter (buffer, &end);
1354 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1358 if (!empathy_spell_supported ()) {
1362 /* NOTE: this is really inefficient, we shouldn't have to
1363 reiterate the whole buffer each time and check each work
1366 gboolean correct = FALSE;
1369 if (gtk_text_iter_is_start (&start)) {
1372 if (!gtk_text_iter_forward_word_end (&end)) {
1373 /* no whole word yet */
1377 if (!gtk_text_iter_forward_word_end (&end)) {
1378 /* must be the end of the buffer */
1383 gtk_text_iter_backward_word_start (&start);
1386 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1388 /* spell check string if not a command */
1389 if (str[0] != '/') {
1390 correct = empathy_spell_check (str);
1396 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1398 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1403 /* set start iter to the end iters position */
1409 empathy_isspace_cb (gunichar c,
1412 return g_unichar_isspace (c);
1416 chat_input_key_press_event_cb (GtkWidget *widget,
1420 EmpathyChatPriv *priv;
1423 GtkWidget *text_view_sw;
1425 priv = GET_PRIV (chat);
1427 /* Catch ctrl+up/down so we can traverse messages we sent */
1428 if ((event->state & GDK_CONTROL_MASK) &&
1429 (event->keyval == GDK_Up ||
1430 event->keyval == GDK_Down)) {
1431 GtkTextBuffer *buffer;
1434 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1435 chat_input_history_update (chat, buffer);
1437 if (event->keyval == GDK_Up) {
1438 str = chat_input_history_get_next (chat);
1440 str = chat_input_history_get_prev (chat);
1443 g_signal_handlers_block_by_func (buffer,
1444 chat_input_text_buffer_changed_cb,
1446 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1447 g_signal_handlers_unblock_by_func (buffer,
1448 chat_input_text_buffer_changed_cb,
1454 /* Catch enter but not ctrl/shift-enter */
1455 if (IS_ENTER (event->keyval) &&
1456 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1459 /* This is to make sure that kinput2 gets the enter. And if
1460 * it's handled there we shouldn't send on it. This is because
1461 * kinput2 uses Enter to commit letters. See:
1462 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1465 view = GTK_TEXT_VIEW (chat->input_text_view);
1466 if (gtk_im_context_filter_keypress (view->im_context, event)) {
1467 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
1471 chat_input_text_view_send (chat);
1475 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1477 if (IS_ENTER (event->keyval) &&
1478 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1479 /* Newline for shift/control-enter. */
1482 if (!(event->state & GDK_CONTROL_MASK) &&
1483 event->keyval == GDK_Page_Up) {
1484 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1485 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1488 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1489 event->keyval == GDK_Page_Down) {
1490 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1491 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1492 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1493 gtk_adjustment_set_value (adj, val);
1496 if (event->keyval == GDK_Escape) {
1497 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1499 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1500 event->keyval == GDK_Tab) {
1501 GtkTextBuffer *buffer;
1502 GtkTextIter start, current;
1503 gchar *nick, *completed;
1504 GList *list, *completed_list;
1505 gboolean is_start_of_buffer;
1507 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1508 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1510 /* Get the start of the nick to complete. */
1511 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1512 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1513 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1515 is_start_of_buffer = gtk_text_iter_is_start (&start);
1517 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1518 g_completion_add_items (priv->completion, list);
1520 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1521 completed_list = g_completion_complete (priv->completion,
1530 gchar *complete_char = NULL;
1531 GString *message = NULL;
1534 gtk_text_buffer_delete (buffer, &start, ¤t);
1536 len = g_list_length (completed_list);
1539 /* If we only have one hit, use that text
1540 * instead of the text in completed since the
1541 * completed text will use the typed string
1542 * which might be cased all wrong.
1545 text = empathy_contact_get_name (completed_list->data);
1549 /* Print all hits to the scrollback view, so the
1550 * user knows what possibilities he has.
1553 message = g_string_new ("");
1554 for (l = completed_list; l != NULL; l = l->next) {
1555 g_string_append (message, empathy_contact_get_name (l->data));
1556 g_string_append (message, " - ");
1558 empathy_chat_view_append_event (chat->view, message->str);
1559 g_string_free (message, TRUE);
1562 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1564 if (len == 1 && is_start_of_buffer &&
1565 empathy_conf_get_string (empathy_conf_get (),
1566 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1568 complete_char != NULL) {
1569 gtk_text_buffer_insert_at_cursor (buffer,
1571 strlen (complete_char));
1572 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1573 g_free (complete_char);
1579 g_completion_clear_items (priv->completion);
1581 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1591 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1595 gtk_widget_grab_focus (chat->input_text_view);
1601 chat_input_set_size_request_idle (gpointer sw)
1603 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1609 chat_input_size_request_cb (GtkWidget *widget,
1610 GtkRequisition *requisition,
1613 EmpathyChatPriv *priv = GET_PRIV (chat);
1616 sw = gtk_widget_get_parent (widget);
1617 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1618 g_idle_add (chat_input_set_size_request_idle, sw);
1619 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1622 priv->has_input_vscroll = TRUE;
1625 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1626 gtk_widget_set_size_request (sw, -1, -1);
1627 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1630 priv->has_input_vscroll = FALSE;
1635 chat_input_realize_cb (GtkWidget *widget,
1638 DEBUG ("Setting focus to the input text view");
1639 if (gtk_widget_is_sensitive (widget)) {
1640 gtk_widget_grab_focus (widget);
1645 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1646 EmpathySmiley *smiley,
1649 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1650 GtkTextBuffer *buffer;
1653 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1655 gtk_text_buffer_get_end_iter (buffer, &iter);
1656 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1658 gtk_text_buffer_get_end_iter (buffer, &iter);
1659 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1670 static EmpathyChatSpell *
1671 chat_spell_new (EmpathyChat *chat,
1676 EmpathyChatSpell *chat_spell;
1678 chat_spell = g_slice_new0 (EmpathyChatSpell);
1680 chat_spell->chat = g_object_ref (chat);
1681 chat_spell->word = g_strdup (word);
1682 chat_spell->start = start;
1683 chat_spell->end = end;
1689 chat_spell_free (EmpathyChatSpell *chat_spell)
1691 g_object_unref (chat_spell->chat);
1692 g_free (chat_spell->word);
1693 g_slice_free (EmpathyChatSpell, chat_spell);
1697 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1698 EmpathyChatSpell *chat_spell)
1700 empathy_chat_correct_word (chat_spell->chat,
1701 &(chat_spell->start),
1703 gtk_menu_item_get_label (menu_item));
1707 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1709 GtkWidget *menu, *menu_item;
1710 GList *suggestions, *l;
1712 menu = gtk_menu_new ();
1713 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1714 if (suggestions == NULL) {
1715 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1716 gtk_widget_set_sensitive (menu_item, FALSE);
1717 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1719 for (l = suggestions; l; l = l->next) {
1720 menu_item = gtk_menu_item_new_with_label (l->data);
1721 g_signal_connect (G_OBJECT (menu_item),
1723 G_CALLBACK (chat_spelling_menu_activate_cb),
1725 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1728 empathy_spell_free_suggestions (suggestions);
1730 gtk_widget_show_all (menu);
1736 chat_text_send_cb (GtkMenuItem *menuitem,
1739 chat_input_text_view_send (chat);
1743 chat_input_populate_popup_cb (GtkTextView *view,
1747 EmpathyChatPriv *priv;
1748 GtkTextBuffer *buffer;
1749 GtkTextTagTable *table;
1752 GtkTextIter iter, start, end;
1755 EmpathyChatSpell *chat_spell;
1756 GtkWidget *spell_menu;
1757 EmpathySmileyManager *smiley_manager;
1758 GtkWidget *smiley_menu;
1761 priv = GET_PRIV (chat);
1762 buffer = gtk_text_view_get_buffer (view);
1764 /* Add the emoticon menu. */
1765 item = gtk_separator_menu_item_new ();
1766 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1767 gtk_widget_show (item);
1769 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1770 image = gtk_image_new_from_icon_name ("face-smile",
1771 GTK_ICON_SIZE_MENU);
1772 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1773 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1774 gtk_widget_show (item);
1776 smiley_manager = empathy_smiley_manager_dup_singleton ();
1777 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1778 chat_insert_smiley_activate_cb,
1780 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1781 g_object_unref (smiley_manager);
1783 /* Add the Send menu item. */
1784 gtk_text_buffer_get_bounds (buffer, &start, &end);
1785 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1786 if (!EMP_STR_EMPTY (str)) {
1787 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1788 g_signal_connect (G_OBJECT (item), "activate",
1789 G_CALLBACK (chat_text_send_cb), chat);
1790 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1791 gtk_widget_show (item);
1795 /* Add the spell check menu item. */
1796 table = gtk_text_buffer_get_tag_table (buffer);
1797 tag = gtk_text_tag_table_lookup (table, "misspelled");
1798 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1799 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1800 GTK_TEXT_WINDOW_WIDGET,
1803 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1805 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1806 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1808 str = gtk_text_buffer_get_text (buffer,
1809 &start, &end, FALSE);
1811 if (!EMP_STR_EMPTY (str)) {
1812 chat_spell = chat_spell_new (chat, str, start, end);
1813 g_object_set_data_full (G_OBJECT (menu),
1814 "chat_spell", chat_spell,
1815 (GDestroyNotify) chat_spell_free);
1817 item = gtk_separator_menu_item_new ();
1818 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1819 gtk_widget_show (item);
1821 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1822 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1823 GTK_ICON_SIZE_MENU);
1824 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1826 spell_menu = chat_spelling_build_menu (chat_spell);
1827 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1829 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1830 gtk_widget_show (item);
1837 chat_log_filter (TplLogEntry *log,
1841 chat_log_filter (EmpathyMessage *message,
1843 #endif /* ENABLE_TPL */
1845 EmpathyChat *chat = user_data;
1847 EmpathyMessage *message;
1848 #endif /* ENABLE_TPL */
1849 EmpathyChatPriv *priv = GET_PRIV (chat);
1850 const GList *pending;
1853 g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
1855 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1856 #endif /* ENABLE_TPL */
1857 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1859 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1861 message = empathy_message_from_tpl_log_entry (log);
1862 #endif /* ENABLE_TPL */
1864 for (; pending; pending = g_list_next (pending)) {
1865 if (empathy_message_equal (message, pending->data)) {
1870 g_object_unref (message);
1877 show_pending_messages (EmpathyChat *chat) {
1878 EmpathyChatPriv *priv = GET_PRIV (chat);
1879 const GList *messages, *l;
1881 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1883 if (chat->view == NULL || priv->tp_chat == NULL)
1886 if (!priv->can_show_pending)
1889 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1891 for (l = messages; l != NULL ; l = g_list_next (l)) {
1892 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1893 chat_message_received (chat, message);
1900 got_filtered_messages_cb (GObject *manager,
1901 GAsyncResult *result,
1906 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1907 EmpathyChatPriv *priv = GET_PRIV (chat);
1908 GError *error = NULL;
1910 messages = tpl_log_manager_get_filtered_messages_async_finish (result, &error);
1912 if (error != NULL) {
1913 DEBUG ("%s. Aborting.", error->message);
1914 empathy_chat_view_append_event (chat->view,
1915 _("Failed to retrieve recent logs"));
1916 g_error_free (error);
1920 for (l = messages; l; l = g_list_next (l)) {
1921 EmpathyMessage *message;
1922 g_assert (TPL_IS_LOG_ENTRY (l->data));
1924 message = empathy_message_from_tpl_log_entry (l->data);
1925 g_object_unref (l->data);
1927 empathy_chat_view_append_message (chat->view, message);
1928 g_object_unref (message);
1930 g_list_free (messages);
1933 /* in case of TPL error, skip backlog and show pending messages */
1934 priv->can_show_pending = TRUE;
1935 show_pending_messages (chat);
1937 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
1938 * about it in EmpathyChatPriv definition */
1939 priv->retrieving_backlogs = FALSE;
1940 empathy_chat_messages_read (chat);
1942 /* Turn back on scrolling */
1943 empathy_chat_view_scroll (chat->view, TRUE);
1945 #endif /* ENABLE_TPL */
1949 chat_add_logs (EmpathyChat *chat)
1951 EmpathyChatPriv *priv = GET_PRIV (chat);
1952 gboolean is_chatroom;
1954 GList *messages, *l;
1955 #endif /* ENABLE_TPL */
1961 /* Turn off scrolling temporarily */
1962 empathy_chat_view_scroll (chat->view, FALSE);
1964 /* Add messages from last conversation */
1965 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1968 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1976 for (l = messages; l; l = g_list_next (l)) {
1977 empathy_chat_view_append_message (chat->view, l->data);
1978 g_object_unref (l->data);
1981 g_list_free (messages);
1983 /* Turn back on scrolling */
1984 empathy_chat_view_scroll (chat->view, TRUE);
1986 priv->retrieving_backlogs = TRUE;
1987 tpl_log_manager_get_filtered_messages_async (priv->log_manager,
1994 got_filtered_messages_cb,
1996 #endif /* ENABLE_TPL */
2000 chat_contacts_completion_func (const gchar *s1,
2004 gchar *tmp, *nick1, *nick2;
2011 return s1 ? -1 : +1;
2014 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2015 nick1 = g_utf8_casefold (tmp, -1);
2018 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2019 nick2 = g_utf8_casefold (tmp, -1);
2022 ret = strncmp (nick1, nick2, n);
2031 build_part_message (guint reason,
2033 EmpathyContact *actor,
2034 const gchar *message)
2036 GString *s = g_string_new ("");
2037 const gchar *actor_name = NULL;
2039 if (actor != NULL) {
2040 actor_name = empathy_contact_get_name (actor);
2043 /* Having an actor only really makes sense for a few actions... */
2045 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2046 g_string_append_printf (s, _("%s has disconnected"), name);
2048 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2049 if (actor_name != NULL) {
2050 /* translators: reverse the order of these arguments
2051 * if the kicked should come before the kicker in your locale.
2053 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2056 g_string_append_printf (s, _("%s was kicked"), name);
2059 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2060 if (actor_name != NULL) {
2061 /* translators: reverse the order of these arguments
2062 * if the banned should come before the banner in your locale.
2064 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2067 g_string_append_printf (s, _("%s was banned"), name);
2071 g_string_append_printf (s, _("%s has left the room"), name);
2074 if (!EMP_STR_EMPTY (message)) {
2075 /* Note to translators: this string is appended to
2076 * notifications like "foo has left the room", with the message
2077 * given by the user living the room. If this poses a problem,
2078 * please let us know. :-)
2080 g_string_append_printf (s, _(" (%s)"), message);
2083 return g_string_free (s, FALSE);
2087 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2088 EmpathyContact *contact,
2089 EmpathyContact *actor,
2095 EmpathyChatPriv *priv = GET_PRIV (chat);
2096 const gchar *name = empathy_contact_get_name (contact);
2099 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2101 if (priv->block_events_timeout_id != 0)
2105 str = g_strdup_printf (_("%s has joined the room"),
2108 str = build_part_message (reason, name, actor, message);
2111 empathy_chat_view_append_event (chat->view, str);
2116 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2117 EmpathyContact *old_contact,
2118 EmpathyContact *new_contact,
2123 EmpathyChatPriv *priv = GET_PRIV (chat);
2125 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2127 if (priv->block_events_timeout_id == 0) {
2130 str = g_strdup_printf (_("%s is now known as %s"),
2131 empathy_contact_get_name (old_contact),
2132 empathy_contact_get_name (new_contact));
2133 empathy_chat_view_append_event (chat->view, str);
2140 chat_reset_size_request (gpointer widget)
2142 gtk_widget_set_size_request (widget, -1, -1);
2148 chat_update_contacts_visibility (EmpathyChat *chat,
2151 EmpathyChatPriv *priv = GET_PRIV (chat);
2152 GtkAllocation allocation;
2154 if (!priv->scrolled_window_contacts) {
2158 if (priv->remote_contact != NULL) {
2162 if (show && priv->contact_list_view == NULL) {
2163 EmpathyContactListStore *store;
2166 /* We are adding the contact list to the chat, we don't want the
2167 * chat view to become too small. If the chat view is already
2168 * smaller than 250 make sure that size won't change. If the
2169 * chat view is bigger the contact list will take some space on
2170 * it but we make sure the chat view don't become smaller than
2171 * 250. Relax the size request once the resize is done */
2172 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2173 min_width = MIN (allocation.width, 250);
2174 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2175 g_idle_add (chat_reset_size_request, priv->vbox_left);
2177 if (priv->contacts_width > 0) {
2178 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2179 priv->contacts_width);
2182 store = empathy_contact_list_store_new (
2183 EMPATHY_CONTACT_LIST (priv->tp_chat));
2184 empathy_contact_list_store_set_show_groups (
2185 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2187 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2188 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2189 EMPATHY_CONTACT_FEATURE_CHAT |
2190 EMPATHY_CONTACT_FEATURE_CALL |
2191 EMPATHY_CONTACT_FEATURE_LOG |
2192 EMPATHY_CONTACT_FEATURE_INFO));
2193 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2194 priv->contact_list_view);
2195 gtk_widget_show (priv->contact_list_view);
2196 gtk_widget_show (priv->scrolled_window_contacts);
2197 g_object_unref (store);
2199 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2200 gtk_widget_hide (priv->scrolled_window_contacts);
2201 if (priv->contact_list_view != NULL) {
2202 gtk_widget_destroy (priv->contact_list_view);
2203 priv->contact_list_view = NULL;
2209 empathy_chat_set_show_contacts (EmpathyChat *chat,
2212 EmpathyChatPriv *priv = GET_PRIV (chat);
2214 priv->show_contacts = show;
2216 chat_update_contacts_visibility (chat, show);
2218 g_object_notify (G_OBJECT (chat), "show-contacts");
2222 chat_remote_contact_changed_cb (EmpathyChat *chat)
2224 EmpathyChatPriv *priv = GET_PRIV (chat);
2226 if (priv->remote_contact != NULL) {
2227 g_object_unref (priv->remote_contact);
2228 priv->remote_contact = NULL;
2233 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2234 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2235 if (priv->remote_contact != NULL) {
2236 g_object_ref (priv->remote_contact);
2237 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2239 else if (priv->tp_chat != NULL) {
2242 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2243 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2246 chat_update_contacts_visibility (chat, priv->show_contacts);
2248 g_object_notify (G_OBJECT (chat), "remote-contact");
2249 g_object_notify (G_OBJECT (chat), "id");
2253 chat_destroy_cb (EmpathyTpChat *tp_chat,
2256 EmpathyChatPriv *priv;
2258 priv = GET_PRIV (chat);
2260 if (!priv->tp_chat) {
2264 chat_composing_remove_timeout (chat);
2265 g_object_unref (priv->tp_chat);
2266 priv->tp_chat = NULL;
2267 g_object_notify (G_OBJECT (chat), "tp-chat");
2269 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2270 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2272 chat_update_contacts_visibility (chat, FALSE);
2276 chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
2279 hpaned_pos = gtk_paned_get_position (GTK_PANED(hpaned));
2280 empathy_conf_set_int (empathy_conf_get (),
2281 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2287 chat_create_ui (EmpathyChat *chat)
2289 EmpathyChatPriv *priv = GET_PRIV (chat);
2293 GtkTextBuffer *buffer;
2296 filename = empathy_file_lookup ("empathy-chat.ui",
2298 gui = empathy_builder_get_file (filename,
2299 "chat_widget", &priv->widget,
2300 "hpaned", &priv->hpaned,
2301 "vbox_left", &priv->vbox_left,
2302 "scrolled_window_chat", &priv->scrolled_window_chat,
2303 "scrolled_window_input", &priv->scrolled_window_input,
2304 "hbox_topic", &priv->hbox_topic,
2305 "expander_topic", &priv->expander_topic,
2306 "label_topic", &priv->label_topic,
2307 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2308 "info_bar_vbox", &priv->info_bar_vbox,
2311 empathy_builder_connect (gui, chat,
2312 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2313 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2314 "label_topic", "size-request", chat_topic_label_size_request_cb,
2319 /* Add message view. */
2320 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
2321 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2322 and other types, even though it's non-editable and doesn't accept any
2323 drags. This steals drag motion for anything inside the scrollbars,
2324 making drag destinations on chat windows far less useful.
2326 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2327 g_signal_connect (chat->view, "focus_in_event",
2328 G_CALLBACK (chat_text_view_focus_in_event_cb),
2330 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2331 GTK_WIDGET (chat->view));
2332 gtk_widget_show (GTK_WIDGET (chat->view));
2334 /* Add input GtkTextView */
2335 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
2336 "pixels-above-lines", 2,
2337 "pixels-below-lines", 2,
2338 "pixels-inside-wrap", 1,
2341 "wrap-mode", GTK_WRAP_WORD_CHAR,
2343 g_signal_connect (chat->input_text_view, "key-press-event",
2344 G_CALLBACK (chat_input_key_press_event_cb),
2346 g_signal_connect (chat->input_text_view, "size-request",
2347 G_CALLBACK (chat_input_size_request_cb),
2349 g_signal_connect (chat->input_text_view, "realize",
2350 G_CALLBACK (chat_input_realize_cb),
2352 g_signal_connect (chat->input_text_view, "populate-popup",
2353 G_CALLBACK (chat_input_populate_popup_cb),
2355 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2356 empathy_signal_connect_weak (buffer, "changed",
2357 G_CALLBACK (chat_input_text_buffer_changed_cb),
2359 gtk_text_buffer_create_tag (buffer, "misspelled",
2360 "underline", PANGO_UNDERLINE_ERROR,
2362 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2363 chat->input_text_view);
2364 gtk_widget_show (chat->input_text_view);
2366 /* Add the (invisible) search bar */
2367 priv->search_bar = empathy_search_bar_new (chat->view);
2368 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2371 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2373 /* Initialy hide the topic, will be shown if not empty */
2374 gtk_widget_hide (priv->hbox_topic);
2376 g_signal_connect (priv->hpaned, "notify::position",
2377 G_CALLBACK (chat_hpaned_pos_changed_cb),
2380 /* Load the paned position */
2381 if (empathy_conf_get_int (empathy_conf_get (),
2382 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2385 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2387 /* Set widget focus order */
2388 list = g_list_append (NULL, priv->search_bar);
2389 list = g_list_append (list, priv->scrolled_window_input);
2390 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2393 list = g_list_append (NULL, priv->vbox_left);
2394 list = g_list_append (list, priv->scrolled_window_contacts);
2395 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2398 list = g_list_append (NULL, priv->hpaned);
2399 list = g_list_append (list, priv->hbox_topic);
2400 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2403 /* Add the main widget in the chat widget */
2404 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2405 g_object_unref (gui);
2409 chat_size_request (GtkWidget *widget,
2410 GtkRequisition *requisition)
2412 GtkBin *bin = GTK_BIN (widget);
2415 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2416 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2418 child = gtk_bin_get_child (bin);
2420 if (child && gtk_widget_get_visible (child))
2422 GtkRequisition child_requisition;
2424 gtk_widget_size_request (child, &child_requisition);
2426 requisition->width += child_requisition.width;
2427 requisition->height += child_requisition.height;
2432 chat_size_allocate (GtkWidget *widget,
2433 GtkAllocation *allocation)
2435 GtkBin *bin = GTK_BIN (widget);
2436 GtkAllocation child_allocation;
2439 gtk_widget_set_allocation (widget, allocation);
2441 child = gtk_bin_get_child (bin);
2443 if (child && gtk_widget_get_visible (child))
2445 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2446 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2447 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2448 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2450 gtk_widget_size_allocate (child, &child_allocation);
2455 chat_finalize (GObject *object)
2458 EmpathyChatPriv *priv;
2460 chat = EMPATHY_CHAT (object);
2461 priv = GET_PRIV (chat);
2463 DEBUG ("Finalized: %p", object);
2465 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2466 g_list_free (priv->input_history);
2468 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2469 g_list_free (priv->compositors);
2471 chat_composing_remove_timeout (chat);
2473 g_object_unref (priv->account_manager);
2474 g_object_unref (priv->log_manager);
2476 if (priv->tp_chat) {
2477 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2478 chat_destroy_cb, chat);
2479 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2480 chat_message_received_cb, chat);
2481 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2482 chat_send_error_cb, chat);
2483 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2484 chat_state_changed_cb, chat);
2485 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2486 chat_property_changed_cb, chat);
2487 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2488 chat_members_changed_cb, chat);
2489 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2490 chat_remote_contact_changed_cb, chat);
2491 empathy_tp_chat_leave (priv->tp_chat);
2492 g_object_unref (priv->tp_chat);
2494 if (priv->account) {
2495 g_object_unref (priv->account);
2497 if (priv->remote_contact) {
2498 g_object_unref (priv->remote_contact);
2501 if (priv->block_events_timeout_id) {
2502 g_source_remove (priv->block_events_timeout_id);
2506 g_free (priv->name);
2507 g_free (priv->subject);
2508 g_completion_free (priv->completion);
2510 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2514 chat_constructed (GObject *object)
2516 EmpathyChat *chat = EMPATHY_CHAT (object);
2517 EmpathyChatPriv *priv = GET_PRIV (chat);
2519 if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
2520 chat_add_logs (chat);
2522 /* When async API are involved, pending message are shown at the end of the
2523 * callbacks' chain fired by chat_add_logs */
2524 priv->can_show_pending = TRUE;
2525 show_pending_messages (chat);
2526 #endif /* ENABLE_TPL */
2530 empathy_chat_class_init (EmpathyChatClass *klass)
2532 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2533 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2535 object_class->finalize = chat_finalize;
2536 object_class->get_property = chat_get_property;
2537 object_class->set_property = chat_set_property;
2538 object_class->constructed = chat_constructed;
2540 widget_class->size_request = chat_size_request;
2541 widget_class->size_allocate = chat_size_allocate;
2543 g_object_class_install_property (object_class,
2545 g_param_spec_object ("tp-chat",
2547 "The tp chat object",
2548 EMPATHY_TYPE_TP_CHAT,
2551 G_PARAM_STATIC_STRINGS));
2552 g_object_class_install_property (object_class,
2554 g_param_spec_object ("account",
2555 "Account of the chat",
2556 "The account of the chat",
2559 G_PARAM_STATIC_STRINGS));
2560 g_object_class_install_property (object_class,
2562 g_param_spec_string ("id",
2564 "The id of the chat",
2567 G_PARAM_STATIC_STRINGS));
2568 g_object_class_install_property (object_class,
2570 g_param_spec_string ("name",
2572 "The name of the chat",
2575 G_PARAM_STATIC_STRINGS));
2576 g_object_class_install_property (object_class,
2578 g_param_spec_string ("subject",
2580 "The subject or topic of the chat",
2583 G_PARAM_STATIC_STRINGS));
2584 g_object_class_install_property (object_class,
2585 PROP_REMOTE_CONTACT,
2586 g_param_spec_object ("remote-contact",
2587 "The remote contact",
2588 "The remote contact is any",
2589 EMPATHY_TYPE_CONTACT,
2591 G_PARAM_STATIC_STRINGS));
2592 g_object_class_install_property (object_class,
2594 g_param_spec_boolean ("show-contacts",
2595 "Contacts' visibility",
2596 "The visibility of the contacts' list",
2599 G_PARAM_STATIC_STRINGS));
2601 signals[COMPOSING] =
2602 g_signal_new ("composing",
2603 G_OBJECT_CLASS_TYPE (object_class),
2607 g_cclosure_marshal_VOID__BOOLEAN,
2611 signals[NEW_MESSAGE] =
2612 g_signal_new ("new-message",
2613 G_OBJECT_CLASS_TYPE (object_class),
2617 g_cclosure_marshal_VOID__OBJECT,
2619 1, EMPATHY_TYPE_MESSAGE);
2621 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2625 chat_block_events_timeout_cb (gpointer data)
2627 EmpathyChatPriv *priv = GET_PRIV (data);
2629 priv->block_events_timeout_id = 0;
2635 account_manager_prepared_cb (GObject *source_object,
2636 GAsyncResult *result,
2639 GList *accounts, *l;
2640 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2641 EmpathyChat *chat = user_data;
2642 GError *error = NULL;
2644 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2645 DEBUG ("Failed to prepare the account manager: %s", error->message);
2646 g_error_free (error);
2650 accounts = tp_account_manager_get_valid_accounts (account_manager);
2652 for (l = accounts; l != NULL; l = l->next) {
2653 TpAccount *account = l->data;
2654 empathy_signal_connect_weak (account, "status-changed",
2655 G_CALLBACK (chat_new_connection_cb),
2659 g_list_free (accounts);
2663 empathy_chat_init (EmpathyChat *chat)
2665 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2666 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2670 priv->log_manager = empathy_log_manager_dup_singleton ();
2672 priv->log_manager = tpl_log_manager_dup_singleton ();
2673 #endif /* ENABLE_TPL */
2674 priv->contacts_width = -1;
2675 priv->input_history = NULL;
2676 priv->input_history_current = NULL;
2677 priv->account_manager = tp_account_manager_dup ();
2679 tp_account_manager_prepare_async (priv->account_manager, NULL,
2680 account_manager_prepared_cb, chat);
2682 empathy_conf_get_bool (empathy_conf_get (),
2683 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
2684 &priv->show_contacts);
2686 /* Block events for some time to avoid having "has come online" or
2687 * "joined" messages. */
2688 priv->block_events_timeout_id =
2689 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2691 /* Add nick name completion */
2692 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
2693 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2695 chat_create_ui (chat);
2699 empathy_chat_new (EmpathyTpChat *tp_chat)
2701 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2705 empathy_chat_get_tp_chat (EmpathyChat *chat)
2707 EmpathyChatPriv *priv = GET_PRIV (chat);
2709 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2711 return priv->tp_chat;
2714 static void display_password_info_bar (EmpathyChat *self,
2718 provide_password_cb (GObject *tp_chat,
2722 EmpathyChat *self = EMPATHY_CHAT (user_data);
2723 EmpathyChatPriv *priv = GET_PRIV (self);
2724 GError *error = NULL;
2726 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
2728 DEBUG ("error: %s", error->message);
2729 /* FIXME: what should we do if that's another error? Close the channel?
2730 * Display the raw D-Bus error to the user isn't very useful */
2731 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
2732 display_password_info_bar (self, TRUE);
2733 g_error_free (error);
2738 gtk_widget_set_sensitive (priv->hpaned, TRUE);
2739 gtk_widget_grab_focus (self->input_text_view);
2743 password_infobar_response_cb (GtkWidget *info_bar,
2747 EmpathyChatPriv *priv = GET_PRIV (self);
2749 const gchar *password;
2751 if (response_id != GTK_RESPONSE_OK)
2754 entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
2755 g_assert (entry != NULL);
2757 password = gtk_entry_get_text (GTK_ENTRY (entry));
2759 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
2760 provide_password_cb, self);
2763 gtk_widget_destroy (info_bar);
2767 password_entry_activate_cb (GtkWidget *entry,
2768 GtkWidget *info_bar)
2770 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2774 passwd_join_button_cb (GtkButton *button,
2775 GtkWidget *info_bar)
2777 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2781 display_password_info_bar (EmpathyChat *self,
2784 EmpathyChatPriv *priv = GET_PRIV (self);
2785 GtkWidget *info_bar;
2786 GtkWidget *content_area;
2793 GtkMessageType type;
2794 const gchar *msg, *button_label;
2797 /* Previous password was wrong */
2798 type = GTK_MESSAGE_ERROR;
2799 msg = _("Wrong password; please try again:");
2800 button_label = _("Retry");
2803 /* First time we're trying to join */
2804 type = GTK_MESSAGE_QUESTION;
2805 msg = _("This room is protected by a password:");
2806 button_label = _("Join");
2809 info_bar = gtk_info_bar_new ();
2810 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
2812 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2814 hbox = gtk_hbox_new (FALSE, 3);
2815 gtk_container_add (GTK_CONTAINER (content_area), hbox);
2818 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
2819 GTK_ICON_SIZE_DIALOG);
2820 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2823 label = gtk_label_new (msg);
2824 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
2826 /* Add password entry */
2827 entry = gtk_entry_new ();
2828 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
2829 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
2831 g_signal_connect (entry, "activate",
2832 G_CALLBACK (password_entry_activate_cb), info_bar);
2834 /* Focus the password entry once it's realized */
2835 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
2837 /* Add 'Join' button */
2838 alig = gtk_alignment_new (0, 0.5, 0, 0);
2840 button = gtk_button_new_with_label (button_label);
2841 gtk_container_add (GTK_CONTAINER (alig), button);
2842 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
2844 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
2847 g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
2849 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
2851 gtk_widget_show_all (hbox);
2853 g_signal_connect (info_bar, "response",
2854 G_CALLBACK (password_infobar_response_cb), self);
2856 gtk_widget_show_all (info_bar);
2860 chat_password_needed_changed_cb (EmpathyChat *self)
2862 EmpathyChatPriv *priv = GET_PRIV (self);
2864 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
2865 display_password_info_bar (self, FALSE);
2866 gtk_widget_set_sensitive (priv->hpaned, FALSE);
2871 empathy_chat_set_tp_chat (EmpathyChat *chat,
2872 EmpathyTpChat *tp_chat)
2874 EmpathyChatPriv *priv = GET_PRIV (chat);
2875 TpConnection *connection;
2876 GPtrArray *properties;
2878 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2879 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
2880 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
2882 if (priv->tp_chat) {
2886 if (priv->account) {
2887 g_object_unref (priv->account);
2890 priv->tp_chat = g_object_ref (tp_chat);
2891 connection = empathy_tp_chat_get_connection (priv->tp_chat);
2892 priv->account = g_object_ref (empathy_get_account_for_connection (connection));
2894 g_signal_connect (tp_chat, "destroy",
2895 G_CALLBACK (chat_destroy_cb),
2897 g_signal_connect (tp_chat, "message-received",
2898 G_CALLBACK (chat_message_received_cb),
2900 g_signal_connect (tp_chat, "send-error",
2901 G_CALLBACK (chat_send_error_cb),
2903 g_signal_connect (tp_chat, "chat-state-changed",
2904 G_CALLBACK (chat_state_changed_cb),
2906 g_signal_connect (tp_chat, "property-changed",
2907 G_CALLBACK (chat_property_changed_cb),
2909 g_signal_connect (tp_chat, "members-changed",
2910 G_CALLBACK (chat_members_changed_cb),
2912 g_signal_connect (tp_chat, "member-renamed",
2913 G_CALLBACK (chat_member_renamed_cb),
2915 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
2916 G_CALLBACK (chat_remote_contact_changed_cb),
2918 g_signal_connect_swapped (tp_chat, "notify::password-needed",
2919 G_CALLBACK (chat_password_needed_changed_cb),
2922 /* Get initial value of properties */
2923 properties = empathy_tp_chat_get_properties (priv->tp_chat);
2924 if (properties != NULL) {
2927 for (i = 0; i < properties->len; i++) {
2928 EmpathyTpChatProperty *property;
2930 property = g_ptr_array_index (properties, i);
2931 if (property->value == NULL)
2934 chat_property_changed_cb (priv->tp_chat,
2941 chat_remote_contact_changed_cb (chat);
2943 if (chat->input_text_view) {
2944 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2945 if (priv->block_events_timeout_id == 0) {
2946 empathy_chat_view_append_event (chat->view, _("Connected"));
2950 g_object_notify (G_OBJECT (chat), "tp-chat");
2951 g_object_notify (G_OBJECT (chat), "id");
2952 g_object_notify (G_OBJECT (chat), "account");
2954 /* This is a noop when tp-chat is set at object construction time and causes
2955 * the pending messages to be show when it's set on the object after it has
2957 show_pending_messages (chat);
2959 /* check if a password is needed */
2960 chat_password_needed_changed_cb (chat);
2964 empathy_chat_get_account (EmpathyChat *chat)
2966 EmpathyChatPriv *priv = GET_PRIV (chat);
2968 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2970 return priv->account;
2974 empathy_chat_get_id (EmpathyChat *chat)
2976 EmpathyChatPriv *priv = GET_PRIV (chat);
2978 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2984 empathy_chat_get_name (EmpathyChat *chat)
2986 EmpathyChatPriv *priv = GET_PRIV (chat);
2989 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2992 if (!ret && priv->remote_contact) {
2993 ret = empathy_contact_get_name (priv->remote_contact);
2999 return ret ? ret : _("Conversation");
3003 empathy_chat_get_subject (EmpathyChat *chat)
3005 EmpathyChatPriv *priv = GET_PRIV (chat);
3007 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3009 return priv->subject;
3013 empathy_chat_get_remote_contact (EmpathyChat *chat)
3015 EmpathyChatPriv *priv = GET_PRIV (chat);
3017 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3019 return priv->remote_contact;
3023 empathy_chat_get_contact_menu (EmpathyChat *chat)
3025 EmpathyChatPriv *priv = GET_PRIV (chat);
3026 GtkWidget *menu = NULL;
3028 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3030 if (priv->remote_contact) {
3031 menu = empathy_contact_menu_new (priv->remote_contact,
3032 EMPATHY_CONTACT_FEATURE_CALL |
3033 EMPATHY_CONTACT_FEATURE_LOG |
3034 EMPATHY_CONTACT_FEATURE_INFO);
3036 else if (priv->contact_list_view) {
3037 EmpathyContactListView *view;
3039 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
3040 menu = empathy_contact_list_view_get_contact_menu (view);
3047 empathy_chat_clear (EmpathyChat *chat)
3049 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3051 empathy_chat_view_clear (chat->view);
3055 empathy_chat_scroll_down (EmpathyChat *chat)
3057 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3059 empathy_chat_view_scroll_down (chat->view);
3063 empathy_chat_cut (EmpathyChat *chat)
3065 GtkTextBuffer *buffer;
3067 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3069 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3070 if (gtk_text_buffer_get_has_selection (buffer)) {
3071 GtkClipboard *clipboard;
3073 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3075 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3080 empathy_chat_copy (EmpathyChat *chat)
3082 GtkTextBuffer *buffer;
3084 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3086 if (empathy_chat_view_get_has_selection (chat->view)) {
3087 empathy_chat_view_copy_clipboard (chat->view);
3091 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3092 if (gtk_text_buffer_get_has_selection (buffer)) {
3093 GtkClipboard *clipboard;
3095 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3097 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3102 empathy_chat_paste (EmpathyChat *chat)
3104 GtkTextBuffer *buffer;
3105 GtkClipboard *clipboard;
3106 EmpathyChatPriv *priv;
3108 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3110 priv = GET_PRIV (chat);
3112 if (priv->tp_chat == NULL ||
3113 !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
3116 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3117 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3119 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3123 empathy_chat_find (EmpathyChat *chat)
3125 EmpathyChatPriv *priv;
3127 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3129 priv = GET_PRIV (chat);
3131 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3135 empathy_chat_correct_word (EmpathyChat *chat,
3138 const gchar *new_word)
3140 GtkTextBuffer *buffer;
3142 g_return_if_fail (chat != NULL);
3143 g_return_if_fail (new_word != NULL);
3145 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3147 gtk_text_buffer_delete (buffer, start, end);
3148 gtk_text_buffer_insert (buffer, start,
3154 empathy_chat_is_room (EmpathyChat *chat)
3156 EmpathyChatPriv *priv = GET_PRIV (chat);
3158 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3160 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3164 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3166 EmpathyChatPriv *priv = GET_PRIV (self);
3168 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3170 return priv->unread_messages;
3173 /* called when the messages have been read by user */
3175 empathy_chat_messages_read (EmpathyChat *self)
3177 EmpathyChatPriv *priv = GET_PRIV (self);
3179 g_return_if_fail (EMPATHY_IS_CHAT (self));
3181 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3182 * definition. If we are still retrieving the backlogs, do not ACK */
3183 if (priv->retrieving_backlogs)
3186 if (priv->tp_chat != NULL ) {
3187 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3189 priv->unread_messages = 0;