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;
99 GtkWidget *scrolled_window_chat;
100 GtkWidget *scrolled_window_input;
101 GtkWidget *scrolled_window_contacts;
102 GtkWidget *hbox_topic;
103 GtkWidget *label_topic;
104 GtkWidget *contact_list_view;
105 GtkWidget *info_bar_vbox;
106 GtkWidget *search_bar;
108 guint unread_messages;
109 /* TRUE if the pending messages can be displayed. This is to avoid to show
110 * pending messages *before* messages from logs. (#603980) */
111 gboolean can_show_pending;
113 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
114 * be differently handled since it introduces another race condition, which
115 * is really hard to occur, but still possible.
117 * With the current workaround (which has the race above), we need to be
118 * sure to ACK any pending messages only when the retrieval of backlogs is
119 * finished, that's why using retrieving_backlogs flag.
120 * empathy_chat_messages_read () will check this variable and not ACK
121 * anything when TRUE. It will be set TRUE at chat_constructed () and set
122 * back to FALSE when the backlog has been retrieved and the pending
123 * messages actually showed to the user.
125 * Race condition introduced with this workaround:
126 * Scenario: a message is pending, the user is notified and selects the tab.
127 * the tab with a pending message is focused before the messages are properly
128 * shown (since the preparation of the window is slower AND async WRT the
129 * tab showing), which means the user won't see any new messages (rare but
130 * possible), if he/she will change tab focus before the messages are
131 * properly shown, the tab will be set as 'seen' and the user won't be
132 * notified again about the already notified pending messages when the
133 * messages in tab will be properly shown */
134 gboolean retrieving_backlogs;
138 gchar *text; /* Original message that was specified
139 * upon entry creation. */
140 gchar *modified_text; /* Message that was modified by user.
141 * When no modifications were made, it is NULL */
161 static guint signals[LAST_SIGNAL] = { 0 };
163 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
166 chat_get_property (GObject *object,
171 EmpathyChat *chat = EMPATHY_CHAT (object);
172 EmpathyChatPriv *priv = GET_PRIV (object);
176 g_value_set_object (value, priv->tp_chat);
179 g_value_set_object (value, priv->account);
182 g_value_set_string (value, empathy_chat_get_name (chat));
185 g_value_set_string (value, priv->id);
188 g_value_set_string (value, priv->subject);
190 case PROP_REMOTE_CONTACT:
191 g_value_set_object (value, priv->remote_contact);
193 case PROP_SHOW_CONTACTS:
194 g_value_set_boolean (value, priv->show_contacts);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203 chat_set_property (GObject *object,
208 EmpathyChat *chat = EMPATHY_CHAT (object);
212 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
214 case PROP_SHOW_CONTACTS:
215 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
224 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
228 EmpathyChat *chat = EMPATHY_CHAT (user_data);
229 EmpathyTpChat *tpchat;
232 empathy_chat_view_append_event (chat->view,
233 _("Failed to reconnect this chat"));
237 tpchat = EMPATHY_TP_CHAT (
238 empathy_dispatch_operation_get_channel_wrapper (dispatch));
240 if (empathy_dispatch_operation_claim (dispatch)) {
241 empathy_chat_set_tp_chat (chat, tpchat);
246 reconnected_connection_ready_cb (TpConnection *connection,
250 EmpathyChat *chat = user_data;
251 EmpathyChatPriv *priv = GET_PRIV (chat);
254 DEBUG ("connection is not ready: %s", error->message);
258 DEBUG ("Account reconnected, request a new Text channel");
260 switch (priv->handle_type) {
261 case TP_HANDLE_TYPE_CONTACT:
262 empathy_dispatcher_chat_with_contact_id (
263 connection, priv->id,
264 chat_connect_channel_reconnected,
267 case TP_HANDLE_TYPE_ROOM:
268 empathy_dispatcher_join_muc (connection,
270 chat_connect_channel_reconnected,
274 g_assert_not_reached ();
279 g_object_unref (chat);
283 chat_new_connection_cb (TpAccount *account,
287 gchar *dbus_error_name,
291 EmpathyChatPriv *priv = GET_PRIV (chat);
292 TpConnection *connection;
294 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
297 connection = tp_account_get_connection (account);
299 if (priv->tp_chat != NULL || account != priv->account ||
300 priv->handle_type == TP_HANDLE_TYPE_NONE ||
301 EMP_STR_EMPTY (priv->id))
305 tp_connection_call_when_ready (connection, reconnected_connection_ready_cb,
310 chat_composing_remove_timeout (EmpathyChat *chat)
312 EmpathyChatPriv *priv;
314 priv = GET_PRIV (chat);
316 if (priv->composing_stop_timeout_id) {
317 g_source_remove (priv->composing_stop_timeout_id);
318 priv->composing_stop_timeout_id = 0;
323 chat_composing_stop_timeout_cb (EmpathyChat *chat)
325 EmpathyChatPriv *priv;
327 priv = GET_PRIV (chat);
329 priv->composing_stop_timeout_id = 0;
330 empathy_tp_chat_set_state (priv->tp_chat,
331 TP_CHANNEL_CHAT_STATE_PAUSED);
337 chat_composing_start (EmpathyChat *chat)
339 EmpathyChatPriv *priv;
341 priv = GET_PRIV (chat);
343 if (priv->composing_stop_timeout_id) {
344 /* Just restart the timeout */
345 chat_composing_remove_timeout (chat);
347 empathy_tp_chat_set_state (priv->tp_chat,
348 TP_CHANNEL_CHAT_STATE_COMPOSING);
351 priv->composing_stop_timeout_id = g_timeout_add_seconds (
352 COMPOSING_STOP_TIMEOUT,
353 (GSourceFunc) chat_composing_stop_timeout_cb,
358 chat_composing_stop (EmpathyChat *chat)
360 EmpathyChatPriv *priv;
362 priv = GET_PRIV (chat);
364 chat_composing_remove_timeout (chat);
365 empathy_tp_chat_set_state (priv->tp_chat,
366 TP_CHANNEL_CHAT_STATE_ACTIVE);
370 chat_input_history_entry_cmp (InputHistoryEntry *entry,
373 if (!tp_strdiff (entry->text, text)) {
374 if (entry->modified_text != NULL) {
375 /* Modified entry and single string cannot be equal. */
383 static InputHistoryEntry *
384 chat_input_history_entry_new_with_text (const gchar *text)
386 InputHistoryEntry *entry;
387 entry = g_slice_new0 (InputHistoryEntry);
388 entry->text = g_strdup (text);
394 chat_input_history_entry_free (InputHistoryEntry *entry)
396 g_free (entry->text);
397 g_free (entry->modified_text);
398 g_slice_free (InputHistoryEntry, entry);
402 chat_input_history_entry_revert (InputHistoryEntry *entry)
404 g_free (entry->modified_text);
405 entry->modified_text = NULL;
409 chat_input_history_entry_update_text (InputHistoryEntry *entry,
414 if (!tp_strdiff (text, entry->text)) {
415 g_free (entry->modified_text);
416 entry->modified_text = NULL;
420 old = entry->modified_text;
421 entry->modified_text = g_strdup (text);
426 chat_input_history_entry_get_text (InputHistoryEntry *entry)
432 if (entry->modified_text != NULL) {
433 return entry->modified_text;
439 chat_input_history_remove_item (GList *list,
442 list = g_list_remove_link (list, item);
443 chat_input_history_entry_free (item->data);
444 g_list_free_1 (item);
449 chat_input_history_revert (EmpathyChat *chat)
451 EmpathyChatPriv *priv;
455 InputHistoryEntry *entry;
457 priv = GET_PRIV (chat);
458 list = priv->input_history;
461 DEBUG ("No input history");
465 /* Delete temporary entry */
466 if (priv->input_history_current != NULL) {
468 list = chat_input_history_remove_item (list, item1);
469 if (priv->input_history_current == item1) {
470 /* Removed temporary entry was current entry */
471 priv->input_history = list;
472 priv->input_history_current = NULL;
477 /* There is no entry to revert */
481 /* Restore the current history entry to original value */
482 item1 = priv->input_history_current;
484 chat_input_history_entry_revert (entry);
486 /* Remove restored entry if there is other occurance before this entry */
487 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
488 (GCompareFunc) chat_input_history_entry_cmp);
489 if (item2 != item1) {
490 list = chat_input_history_remove_item (list, item1);
493 /* Remove other occurance of the restored entry */
494 item2 = g_list_find_custom (item1->next,
495 chat_input_history_entry_get_text (entry),
496 (GCompareFunc) chat_input_history_entry_cmp);
498 list = chat_input_history_remove_item (list, item2);
502 priv->input_history_current = NULL;
503 priv->input_history = list;
507 chat_input_history_add (EmpathyChat *chat,
511 EmpathyChatPriv *priv;
514 InputHistoryEntry *entry;
516 priv = GET_PRIV (chat);
518 list = priv->input_history;
520 /* Remove any other occurances of this entry, if not temporary */
522 while ((item = g_list_find_custom (list, str,
523 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
524 list = chat_input_history_remove_item (list, item);
527 /* Trim the list to the last 10 items */
528 while (g_list_length (list) > 10) {
529 item = g_list_last (list);
531 list = chat_input_history_remove_item (list, item);
539 entry = chat_input_history_entry_new_with_text (str);
540 list = g_list_prepend (list, entry);
542 /* Set the list and the current item pointer */
543 priv->input_history = list;
545 priv->input_history_current = list;
548 priv->input_history_current = NULL;
553 chat_input_history_get_next (EmpathyChat *chat)
555 EmpathyChatPriv *priv;
559 priv = GET_PRIV (chat);
561 if (priv->input_history == NULL) {
562 DEBUG ("No input history, next entry is NULL");
565 g_assert (priv->input_history_current != NULL);
567 if ((item = g_list_next (priv->input_history_current)) == NULL)
569 item = priv->input_history_current;
572 msg = chat_input_history_entry_get_text (item->data);
574 DEBUG ("Returning next entry: '%s'", msg);
576 priv->input_history_current = item;
582 chat_input_history_get_prev (EmpathyChat *chat)
584 EmpathyChatPriv *priv;
588 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
590 priv = GET_PRIV (chat);
592 if (priv->input_history == NULL) {
593 DEBUG ("No input history, previous entry is NULL");
597 if (priv->input_history_current == NULL)
601 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
603 item = priv->input_history_current;
606 msg = chat_input_history_entry_get_text (item->data);
608 DEBUG ("Returning previous entry: '%s'", msg);
610 priv->input_history_current = item;
616 chat_input_history_update (EmpathyChat *chat,
617 GtkTextBuffer *buffer)
619 EmpathyChatPriv *priv;
620 GtkTextIter start, end;
622 InputHistoryEntry *entry;
624 priv = GET_PRIV (chat);
626 gtk_text_buffer_get_bounds (buffer, &start, &end);
627 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
629 if (priv->input_history_current == NULL) {
630 /* Add the current text temporarily to the history */
631 chat_input_history_add (chat, text, TRUE);
636 /* Save the changes in the history */
637 entry = priv->input_history_current->data;
638 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
639 chat_input_history_entry_update_text (entry, text);
646 chat_command_join_cb (EmpathyDispatchOperation *dispatch,
650 EmpathyChat *chat = user_data;
653 DEBUG ("Error: %s", error->message);
654 empathy_chat_view_append_event (chat->view,
655 _("Failed to join chat room"));
662 } ChatCommandMsgData;
665 chat_command_msg_cb (EmpathyDispatchOperation *dispatch,
669 ChatCommandMsgData *data = user_data;
672 empathy_chat_view_append_event (data->chat->view,
673 _("Failed to open private chat"));
677 if (!EMP_STR_EMPTY (data->message)) {
678 EmpathyTpChat *tpchat;
679 EmpathyMessage *message;
681 tpchat = EMPATHY_TP_CHAT (
682 empathy_dispatch_operation_get_channel_wrapper (dispatch));
684 message = empathy_message_new (data->message);
685 empathy_tp_chat_send (tpchat, message);
686 g_object_unref (message);
690 g_free (data->message);
691 g_slice_free (ChatCommandMsgData, data);
695 chat_command_clear (EmpathyChat *chat,
698 empathy_chat_view_clear (chat->view);
702 chat_command_topic (EmpathyChat *chat,
705 EmpathyChatPriv *priv = GET_PRIV (chat);
706 EmpathyTpChatProperty *property;
707 GValue value = {0, };
709 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
710 if (property == NULL) {
711 empathy_chat_view_append_event (chat->view,
712 _("Topic not supported on this conversation"));
716 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
717 empathy_chat_view_append_event (chat->view,
718 _("You are not allowed to change the topic"));
722 g_value_init (&value, G_TYPE_STRING);
723 g_value_set_string (&value, strv[1]);
724 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
725 g_value_unset (&value);
729 chat_command_join (EmpathyChat *chat,
733 EmpathyChatPriv *priv = GET_PRIV (chat);
735 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
737 while (rooms[i] != NULL) {
738 /* ignore empty strings */
739 if (!EMP_STR_EMPTY (rooms[i])) {
740 TpConnection *connection;
742 connection = empathy_tp_chat_get_connection (priv->tp_chat);
743 empathy_dispatcher_join_muc (connection, rooms[i],
744 chat_command_join_cb,
753 chat_command_msg_internal (EmpathyChat *chat,
754 const gchar *contact_id,
755 const gchar *message)
757 EmpathyChatPriv *priv = GET_PRIV (chat);
758 TpConnection *connection;
759 ChatCommandMsgData *data;
761 /* FIXME: We should probably search in members alias. But this
762 * is enough for IRC */
763 data = g_slice_new (ChatCommandMsgData);
765 data->message = g_strdup (message);
766 connection = empathy_tp_chat_get_connection (priv->tp_chat);
767 empathy_dispatcher_chat_with_contact_id (connection, contact_id,
773 chat_command_query (EmpathyChat *chat,
776 /* If <message> part is not defined,
777 * strv[2] will be the terminal NULL */
778 chat_command_msg_internal (chat, strv[1], strv[2]);
782 chat_command_msg (EmpathyChat *chat,
785 chat_command_msg_internal (chat, strv[1], strv[2]);
789 chat_command_nick (EmpathyChat *chat,
792 EmpathyChatPriv *priv = GET_PRIV (chat);
793 TpConnection *connection;
794 GHashTable *new_alias;
797 connection = tp_account_get_connection (priv->account);
798 handle = tp_connection_get_self_handle (connection);
799 new_alias = g_hash_table_new (g_direct_hash, g_direct_equal);
800 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), strv[1]);
802 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
803 new_alias, NULL, NULL, NULL, NULL);
805 g_hash_table_destroy (new_alias);
809 chat_command_me (EmpathyChat *chat,
812 EmpathyChatPriv *priv = GET_PRIV (chat);
813 EmpathyMessage *message;
815 message = empathy_message_new (strv[1]);
816 empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
817 empathy_tp_chat_send (priv->tp_chat, message);
818 g_object_unref (message);
822 chat_command_say (EmpathyChat *chat,
825 EmpathyChatPriv *priv = GET_PRIV (chat);
826 EmpathyMessage *message;
828 message = empathy_message_new (strv[1]);
829 empathy_tp_chat_send (priv->tp_chat, message);
830 g_object_unref (message);
833 static void chat_command_help (EmpathyChat *chat, GStrv strv);
835 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
841 ChatCommandFunc func;
845 static ChatCommandItem commands[] = {
846 {"clear", 1, 1, chat_command_clear,
847 N_("/clear: clear all messages from the current conversation")},
849 {"topic", 2, 2, chat_command_topic,
850 N_("/topic <topic>: set the topic of the current conversation")},
852 {"join", 2, 2, chat_command_join,
853 N_("/join <chat room ID>: join a new chat room")},
855 {"j", 2, 2, chat_command_join,
856 N_("/j <chat room ID>: join a new chat room")},
858 {"query", 2, 3, chat_command_query,
859 N_("/query <contact ID> [<message>]: open a private chat")},
861 {"msg", 3, 3, chat_command_msg,
862 N_("/msg <contact ID> <message>: open a private chat")},
864 {"nick", 2, 2, chat_command_nick,
865 N_("/nick <nickname>: change your nickname on the current server")},
867 {"me", 2, 2, chat_command_me,
868 N_("/me <message>: send an ACTION message to the current conversation")},
870 {"say", 2, 2, chat_command_say,
871 N_("/say <message>: send <message> to the current conversation. "
872 "This is used to send a message starting with a '/'. For example: "
873 "\"/say /join is used to join a new chat room\"")},
875 {"help", 1, 2, chat_command_help,
876 N_("/help [<command>]: show all supported commands. "
877 "If <command> is defined, show its usage.")},
881 chat_command_show_help (EmpathyChat *chat,
882 ChatCommandItem *item)
886 str = g_strdup_printf (_("Usage: %s"), _(item->help));
887 empathy_chat_view_append_event (chat->view, str);
892 chat_command_help (EmpathyChat *chat,
897 /* If <command> part is not defined,
898 * strv[1] will be the terminal NULL */
899 if (strv[1] == NULL) {
900 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
901 empathy_chat_view_append_event (chat->view,
902 _(commands[i].help));
907 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
908 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
909 chat_command_show_help (chat, &commands[i]);
914 empathy_chat_view_append_event (chat->view,
915 _("Unknown command"));
919 chat_command_parse (const gchar *text, guint max_parts)
924 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
926 array = g_ptr_array_sized_new (max_parts + 1);
927 while (max_parts > 1) {
930 /* Skip white spaces */
931 while (g_ascii_isspace (*text)) {
935 /* Search the end of this part, until first space. */
936 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
942 item = g_strndup (text, end - text);
943 g_ptr_array_add (array, item);
944 DEBUG ("\tITEM: \"%s\"", item);
950 /* Append last part if not empty */
951 item = g_strstrip (g_strdup (text));
952 if (!EMP_STR_EMPTY (item)) {
953 g_ptr_array_add (array, item);
954 DEBUG ("\tITEM: \"%s\"", item);
959 /* Make the array NULL-terminated */
960 g_ptr_array_add (array, NULL);
962 return (GStrv) g_ptr_array_free (array, FALSE);
966 has_prefix_case (const gchar *s,
969 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
973 chat_send (EmpathyChat *chat,
976 EmpathyChatPriv *priv;
977 EmpathyMessage *message;
980 if (EMP_STR_EMPTY (msg)) {
984 priv = GET_PRIV (chat);
986 chat_input_history_add (chat, msg, FALSE);
989 gboolean second_slash = FALSE;
990 const gchar *iter = msg + 1;
992 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
997 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1000 c = *(msg + 1 + strlen (commands[i].prefix));
1001 if (c != '\0' && !g_ascii_isspace (c)) {
1005 /* We can't use g_strsplit here because it does
1006 * not deal correctly if we have more than one space
1008 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1010 strv_len = g_strv_length (strv);
1011 if (strv_len < commands[i].min_parts ||
1012 strv_len > commands[i].max_parts) {
1013 chat_command_show_help (chat, &commands[i]);
1018 commands[i].func (chat, strv);
1023 /* Also allow messages with two slashes before the
1024 * first space, so it is possible to send a /unix/path.
1025 * This heuristic is kind of crap. */
1026 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1028 second_slash = TRUE;
1034 if (!second_slash) {
1035 empathy_chat_view_append_event (chat->view,
1036 _("Unknown command; see /help for the available"
1042 message = empathy_message_new (msg);
1043 empathy_tp_chat_send (priv->tp_chat, message);
1044 g_object_unref (message);
1048 chat_input_text_view_send (EmpathyChat *chat)
1050 EmpathyChatPriv *priv;
1051 GtkTextBuffer *buffer;
1052 GtkTextIter start, end;
1055 priv = GET_PRIV (chat);
1057 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1059 gtk_text_buffer_get_bounds (buffer, &start, &end);
1060 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1062 /* clear the input field */
1063 gtk_text_buffer_set_text (buffer, "", -1);
1064 /* delete input history modifications */
1065 chat_input_history_revert (chat);
1067 chat_send (chat, msg);
1072 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1073 EmpathyContact *contact,
1074 TpChannelChatState state,
1077 EmpathyChatPriv *priv;
1079 gboolean was_composing;
1081 priv = GET_PRIV (chat);
1083 if (empathy_contact_is_user (contact)) {
1084 /* We don't care about our own chat state */
1088 was_composing = (priv->compositors != NULL);
1090 /* Find the contact in the list. After that l is the list elem or NULL */
1091 for (l = priv->compositors; l; l = l->next) {
1092 if (contact == l->data) {
1098 case TP_CHANNEL_CHAT_STATE_GONE:
1099 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1100 case TP_CHANNEL_CHAT_STATE_PAUSED:
1101 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1102 /* Contact is not composing */
1104 priv->compositors = g_list_remove_link (priv->compositors, l);
1105 g_object_unref (l->data);
1109 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1110 /* Contact is composing */
1112 priv->compositors = g_list_prepend (priv->compositors,
1113 g_object_ref (contact));
1117 g_assert_not_reached ();
1120 DEBUG ("Was composing: %s now composing: %s",
1121 was_composing ? "yes" : "no",
1122 priv->compositors ? "yes" : "no");
1124 if ((was_composing && !priv->compositors) ||
1125 (!was_composing && priv->compositors)) {
1126 /* Composing state changed */
1127 g_signal_emit (chat, signals[COMPOSING], 0,
1128 priv->compositors != NULL);
1133 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
1135 EmpathyChatPriv *priv = GET_PRIV (chat);
1136 EmpathyContact *sender;
1138 sender = empathy_message_get_sender (message);
1140 DEBUG ("Appending new message from %s (%d)",
1141 empathy_contact_get_name (sender),
1142 empathy_contact_get_handle (sender));
1144 empathy_chat_view_append_message (chat->view, message);
1146 /* We received a message so the contact is no longer composing */
1147 chat_state_changed_cb (priv->tp_chat, sender,
1148 TP_CHANNEL_CHAT_STATE_ACTIVE,
1151 priv->unread_messages++;
1152 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
1156 chat_message_received_cb (EmpathyTpChat *tp_chat,
1157 EmpathyMessage *message,
1160 chat_message_received (chat, message);
1164 chat_send_error_cb (EmpathyTpChat *tp_chat,
1165 const gchar *message_body,
1166 TpChannelTextSendError error_code,
1172 switch (error_code) {
1173 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1174 error = _("offline");
1176 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1177 error = _("invalid contact");
1179 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1180 error = _("permission denied");
1182 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1183 error = _("too long message");
1185 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1186 error = _("not implemented");
1189 error = _("unknown");
1193 str = g_strdup_printf (_("Error sending message '%s': %s"),
1196 empathy_chat_view_append_event (chat->view, str);
1201 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1206 EmpathyChatPriv *priv = GET_PRIV (chat);
1208 if (!tp_strdiff (name, "subject")) {
1209 g_free (priv->subject);
1210 priv->subject = g_value_dup_string (value);
1211 g_object_notify (G_OBJECT (chat), "subject");
1213 if (EMP_STR_EMPTY (priv->subject)) {
1214 gtk_widget_hide (priv->hbox_topic);
1218 markup_text = empathy_add_link_markup (priv->subject);
1219 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1220 g_free (markup_text);
1222 gtk_widget_show (priv->hbox_topic);
1224 if (priv->block_events_timeout_id == 0) {
1227 if (!EMP_STR_EMPTY (priv->subject)) {
1228 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1230 str = g_strdup (_("No topic defined"));
1232 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1236 else if (!tp_strdiff (name, "name")) {
1237 g_free (priv->name);
1238 priv->name = g_value_dup_string (value);
1239 g_object_notify (G_OBJECT (chat), "name");
1244 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1247 GtkTextIter start, end;
1249 gboolean spell_checker = FALSE;
1251 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1252 chat_composing_stop (chat);
1254 chat_composing_start (chat);
1257 empathy_conf_get_bool (empathy_conf_get (),
1258 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
1261 gtk_text_buffer_get_start_iter (buffer, &start);
1263 if (!spell_checker) {
1264 gtk_text_buffer_get_end_iter (buffer, &end);
1265 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1269 if (!empathy_spell_supported ()) {
1273 /* NOTE: this is really inefficient, we shouldn't have to
1274 reiterate the whole buffer each time and check each work
1277 gboolean correct = FALSE;
1280 if (gtk_text_iter_is_start (&start)) {
1283 if (!gtk_text_iter_forward_word_end (&end)) {
1284 /* no whole word yet */
1288 if (!gtk_text_iter_forward_word_end (&end)) {
1289 /* must be the end of the buffer */
1294 gtk_text_iter_backward_word_start (&start);
1297 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1299 /* spell check string if not a command */
1300 if (str[0] != '/') {
1301 correct = empathy_spell_check (str);
1307 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1309 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1314 /* set start iter to the end iters position */
1320 empathy_isspace_cb (gunichar c,
1323 return g_unichar_isspace (c);
1327 chat_input_key_press_event_cb (GtkWidget *widget,
1331 EmpathyChatPriv *priv;
1334 GtkWidget *text_view_sw;
1336 priv = GET_PRIV (chat);
1338 /* Catch ctrl+up/down so we can traverse messages we sent */
1339 if ((event->state & GDK_CONTROL_MASK) &&
1340 (event->keyval == GDK_Up ||
1341 event->keyval == GDK_Down)) {
1342 GtkTextBuffer *buffer;
1345 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1346 chat_input_history_update (chat, buffer);
1348 if (event->keyval == GDK_Up) {
1349 str = chat_input_history_get_next (chat);
1351 str = chat_input_history_get_prev (chat);
1354 g_signal_handlers_block_by_func (buffer,
1355 chat_input_text_buffer_changed_cb,
1357 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1358 g_signal_handlers_unblock_by_func (buffer,
1359 chat_input_text_buffer_changed_cb,
1365 /* Catch enter but not ctrl/shift-enter */
1366 if (IS_ENTER (event->keyval) &&
1367 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1370 /* This is to make sure that kinput2 gets the enter. And if
1371 * it's handled there we shouldn't send on it. This is because
1372 * kinput2 uses Enter to commit letters. See:
1373 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1376 view = GTK_TEXT_VIEW (chat->input_text_view);
1377 if (gtk_im_context_filter_keypress (view->im_context, event)) {
1378 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
1382 chat_input_text_view_send (chat);
1386 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1388 if (IS_ENTER (event->keyval) &&
1389 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1390 /* Newline for shift/control-enter. */
1393 if (!(event->state & GDK_CONTROL_MASK) &&
1394 event->keyval == GDK_Page_Up) {
1395 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1396 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1399 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1400 event->keyval == GDK_Page_Down) {
1401 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1402 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1403 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1404 gtk_adjustment_set_value (adj, val);
1407 if (event->keyval == GDK_Escape) {
1408 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1410 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1411 event->keyval == GDK_Tab) {
1412 GtkTextBuffer *buffer;
1413 GtkTextIter start, current;
1414 gchar *nick, *completed;
1415 GList *list, *completed_list;
1416 gboolean is_start_of_buffer;
1418 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1419 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1421 /* Get the start of the nick to complete. */
1422 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1423 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1424 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1426 is_start_of_buffer = gtk_text_iter_is_start (&start);
1428 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1429 g_completion_add_items (priv->completion, list);
1431 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1432 completed_list = g_completion_complete (priv->completion,
1441 gchar *complete_char = NULL;
1442 GString *message = NULL;
1445 gtk_text_buffer_delete (buffer, &start, ¤t);
1447 len = g_list_length (completed_list);
1450 /* If we only have one hit, use that text
1451 * instead of the text in completed since the
1452 * completed text will use the typed string
1453 * which might be cased all wrong.
1456 text = empathy_contact_get_name (completed_list->data);
1460 /* Print all hits to the scrollback view, so the
1461 * user knows what possibilities he has.
1464 message = g_string_new ("");
1465 for (l = completed_list; l != NULL; l = l->next) {
1466 g_string_append (message, empathy_contact_get_name (l->data));
1467 g_string_append (message, " - ");
1469 empathy_chat_view_append_event (chat->view, message->str);
1470 g_string_free (message, TRUE);
1473 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1475 if (len == 1 && is_start_of_buffer &&
1476 empathy_conf_get_string (empathy_conf_get (),
1477 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1479 complete_char != NULL) {
1480 gtk_text_buffer_insert_at_cursor (buffer,
1482 strlen (complete_char));
1483 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1484 g_free (complete_char);
1490 g_completion_clear_items (priv->completion);
1492 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1502 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1506 gtk_widget_grab_focus (chat->input_text_view);
1512 chat_input_set_size_request_idle (gpointer sw)
1514 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1520 chat_input_size_request_cb (GtkWidget *widget,
1521 GtkRequisition *requisition,
1524 EmpathyChatPriv *priv = GET_PRIV (chat);
1527 sw = gtk_widget_get_parent (widget);
1528 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1529 g_idle_add (chat_input_set_size_request_idle, sw);
1530 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1533 priv->has_input_vscroll = TRUE;
1536 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1537 gtk_widget_set_size_request (sw, -1, -1);
1538 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1541 priv->has_input_vscroll = FALSE;
1546 chat_input_realize_cb (GtkWidget *widget,
1549 DEBUG ("Setting focus to the input text view");
1550 if (gtk_widget_is_sensitive (widget)) {
1551 gtk_widget_grab_focus (widget);
1556 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1557 EmpathySmiley *smiley,
1560 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1561 GtkTextBuffer *buffer;
1564 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1566 gtk_text_buffer_get_end_iter (buffer, &iter);
1567 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1569 gtk_text_buffer_get_end_iter (buffer, &iter);
1570 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1581 static EmpathyChatSpell *
1582 chat_spell_new (EmpathyChat *chat,
1587 EmpathyChatSpell *chat_spell;
1589 chat_spell = g_slice_new0 (EmpathyChatSpell);
1591 chat_spell->chat = g_object_ref (chat);
1592 chat_spell->word = g_strdup (word);
1593 chat_spell->start = start;
1594 chat_spell->end = end;
1600 chat_spell_free (EmpathyChatSpell *chat_spell)
1602 g_object_unref (chat_spell->chat);
1603 g_free (chat_spell->word);
1604 g_slice_free (EmpathyChatSpell, chat_spell);
1608 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1609 EmpathyChatSpell *chat_spell)
1611 empathy_chat_correct_word (chat_spell->chat,
1612 &(chat_spell->start),
1614 gtk_menu_item_get_label (menu_item));
1618 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1620 GtkWidget *menu, *menu_item;
1621 GList *suggestions, *l;
1623 menu = gtk_menu_new ();
1624 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1625 if (suggestions == NULL) {
1626 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1627 gtk_widget_set_sensitive (menu_item, FALSE);
1628 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1630 for (l = suggestions; l; l = l->next) {
1631 menu_item = gtk_menu_item_new_with_label (l->data);
1632 g_signal_connect (G_OBJECT (menu_item),
1634 G_CALLBACK (chat_spelling_menu_activate_cb),
1636 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1639 empathy_spell_free_suggestions (suggestions);
1641 gtk_widget_show_all (menu);
1647 chat_text_send_cb (GtkMenuItem *menuitem,
1650 chat_input_text_view_send (chat);
1654 chat_input_populate_popup_cb (GtkTextView *view,
1658 EmpathyChatPriv *priv;
1659 GtkTextBuffer *buffer;
1660 GtkTextTagTable *table;
1663 GtkTextIter iter, start, end;
1666 EmpathyChatSpell *chat_spell;
1667 GtkWidget *spell_menu;
1668 EmpathySmileyManager *smiley_manager;
1669 GtkWidget *smiley_menu;
1672 priv = GET_PRIV (chat);
1673 buffer = gtk_text_view_get_buffer (view);
1675 /* Add the emoticon menu. */
1676 item = gtk_separator_menu_item_new ();
1677 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1678 gtk_widget_show (item);
1680 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1681 image = gtk_image_new_from_icon_name ("face-smile",
1682 GTK_ICON_SIZE_MENU);
1683 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1684 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1685 gtk_widget_show (item);
1687 smiley_manager = empathy_smiley_manager_dup_singleton ();
1688 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1689 chat_insert_smiley_activate_cb,
1691 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1692 g_object_unref (smiley_manager);
1694 /* Add the Send menu item. */
1695 gtk_text_buffer_get_bounds (buffer, &start, &end);
1696 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1697 if (!EMP_STR_EMPTY (str)) {
1698 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1699 g_signal_connect (G_OBJECT (item), "activate",
1700 G_CALLBACK (chat_text_send_cb), chat);
1701 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1702 gtk_widget_show (item);
1706 /* Add the spell check menu item. */
1707 table = gtk_text_buffer_get_tag_table (buffer);
1708 tag = gtk_text_tag_table_lookup (table, "misspelled");
1709 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1710 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1711 GTK_TEXT_WINDOW_WIDGET,
1714 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1716 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1717 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1719 str = gtk_text_buffer_get_text (buffer,
1720 &start, &end, FALSE);
1722 if (!EMP_STR_EMPTY (str)) {
1723 chat_spell = chat_spell_new (chat, str, start, end);
1724 g_object_set_data_full (G_OBJECT (menu),
1725 "chat_spell", chat_spell,
1726 (GDestroyNotify) chat_spell_free);
1728 item = gtk_separator_menu_item_new ();
1729 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1730 gtk_widget_show (item);
1732 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1733 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1734 GTK_ICON_SIZE_MENU);
1735 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1737 spell_menu = chat_spelling_build_menu (chat_spell);
1738 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1740 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1741 gtk_widget_show (item);
1748 chat_log_filter (TplLogEntry *log,
1752 chat_log_filter (EmpathyMessage *message,
1754 #endif /* ENABLE_TPL */
1756 EmpathyChat *chat = user_data;
1758 EmpathyMessage *message;
1759 #endif /* ENABLE_TPL */
1760 EmpathyChatPriv *priv = GET_PRIV (chat);
1761 const GList *pending;
1764 g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
1766 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1767 #endif /* ENABLE_TPL */
1768 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1770 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1772 message = empathy_message_from_tpl_log_entry (log);
1773 #endif /* ENABLE_TPL */
1775 for (; pending; pending = g_list_next (pending)) {
1776 if (empathy_message_equal (message, pending->data)) {
1781 g_object_unref (message);
1788 show_pending_messages (EmpathyChat *chat) {
1789 EmpathyChatPriv *priv = GET_PRIV (chat);
1790 const GList *messages, *l;
1792 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1794 if (chat->view == NULL || priv->tp_chat == NULL)
1797 if (!priv->can_show_pending)
1800 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1802 for (l = messages; l != NULL ; l = g_list_next (l)) {
1803 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1804 chat_message_received (chat, message);
1811 got_filtered_messages_cb (GObject *manager,
1812 GAsyncResult *result,
1817 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1818 EmpathyChatPriv *priv = GET_PRIV (chat);
1819 GError *error = NULL;
1821 messages = tpl_log_manager_get_filtered_messages_async_finish (result, &error);
1823 if (error != NULL) {
1824 DEBUG ("%s. Aborting.", error->message);
1825 empathy_chat_view_append_event (chat->view,
1826 _("Failed to retrieve recent logs"));
1827 g_error_free (error);
1831 for (l = messages; l; l = g_list_next (l)) {
1832 EmpathyMessage *message;
1833 g_assert (TPL_IS_LOG_ENTRY (l->data));
1835 message = empathy_message_from_tpl_log_entry (l->data);
1836 g_object_unref (l->data);
1838 empathy_chat_view_append_message (chat->view, message);
1839 g_object_unref (message);
1841 g_list_free (messages);
1844 /* in case of TPL error, skip backlog and show pending messages */
1845 priv->can_show_pending = TRUE;
1846 show_pending_messages (chat);
1848 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
1849 * about it in EmpathyChatPriv definition */
1850 priv->retrieving_backlogs = FALSE;
1851 empathy_chat_messages_read (chat);
1853 /* Turn back on scrolling */
1854 empathy_chat_view_scroll (chat->view, TRUE);
1856 #endif /* ENABLE_TPL */
1860 chat_add_logs (EmpathyChat *chat)
1862 EmpathyChatPriv *priv = GET_PRIV (chat);
1863 gboolean is_chatroom;
1865 GList *messages, *l;
1866 #endif /* ENABLE_TPL */
1872 /* Turn off scrolling temporarily */
1873 empathy_chat_view_scroll (chat->view, FALSE);
1875 /* Add messages from last conversation */
1876 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1879 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1887 for (l = messages; l; l = g_list_next (l)) {
1888 empathy_chat_view_append_message (chat->view, l->data);
1889 g_object_unref (l->data);
1892 g_list_free (messages);
1894 /* Turn back on scrolling */
1895 empathy_chat_view_scroll (chat->view, TRUE);
1897 priv->retrieving_backlogs = TRUE;
1898 tpl_log_manager_get_filtered_messages_async (priv->log_manager,
1905 got_filtered_messages_cb,
1907 #endif /* ENABLE_TPL */
1911 chat_contacts_completion_func (const gchar *s1,
1915 gchar *tmp, *nick1, *nick2;
1922 return s1 ? -1 : +1;
1925 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1926 nick1 = g_utf8_casefold (tmp, -1);
1929 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1930 nick2 = g_utf8_casefold (tmp, -1);
1933 ret = strncmp (nick1, nick2, n);
1942 build_part_message (guint reason,
1944 EmpathyContact *actor,
1945 const gchar *message)
1947 GString *s = g_string_new ("");
1948 const gchar *actor_name = NULL;
1950 if (actor != NULL) {
1951 actor_name = empathy_contact_get_name (actor);
1954 /* Having an actor only really makes sense for a few actions... */
1956 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1957 g_string_append_printf (s, _("%s has disconnected"), name);
1959 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1960 if (actor_name != NULL) {
1961 /* translators: reverse the order of these arguments
1962 * if the kicked should come before the kicker in your locale.
1964 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
1967 g_string_append_printf (s, _("%s was kicked"), name);
1970 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1971 if (actor_name != NULL) {
1972 /* translators: reverse the order of these arguments
1973 * if the banned should come before the banner in your locale.
1975 g_string_append_printf (s, _("%1$s was banned by %2$s"),
1978 g_string_append_printf (s, _("%s was banned"), name);
1982 g_string_append_printf (s, _("%s has left the room"), name);
1985 if (!EMP_STR_EMPTY (message)) {
1986 /* Note to translators: this string is appended to
1987 * notifications like "foo has left the room", with the message
1988 * given by the user living the room. If this poses a problem,
1989 * please let us know. :-)
1991 g_string_append_printf (s, _(" (%s)"), message);
1994 return g_string_free (s, FALSE);
1998 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1999 EmpathyContact *contact,
2000 EmpathyContact *actor,
2006 EmpathyChatPriv *priv = GET_PRIV (chat);
2007 const gchar *name = empathy_contact_get_name (contact);
2010 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2012 if (priv->block_events_timeout_id != 0)
2016 str = g_strdup_printf (_("%s has joined the room"),
2019 str = build_part_message (reason, name, actor, message);
2022 empathy_chat_view_append_event (chat->view, str);
2027 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2028 EmpathyContact *old_contact,
2029 EmpathyContact *new_contact,
2034 EmpathyChatPriv *priv = GET_PRIV (chat);
2036 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2038 if (priv->block_events_timeout_id == 0) {
2041 str = g_strdup_printf (_("%s is now known as %s"),
2042 empathy_contact_get_name (old_contact),
2043 empathy_contact_get_name (new_contact));
2044 empathy_chat_view_append_event (chat->view, str);
2051 chat_reset_size_request (gpointer widget)
2053 gtk_widget_set_size_request (widget, -1, -1);
2059 chat_update_contacts_visibility (EmpathyChat *chat,
2062 EmpathyChatPriv *priv = GET_PRIV (chat);
2063 GtkAllocation allocation;
2065 if (!priv->scrolled_window_contacts) {
2069 if (priv->remote_contact != NULL) {
2073 if (show && priv->contact_list_view == NULL) {
2074 EmpathyContactListStore *store;
2077 /* We are adding the contact list to the chat, we don't want the
2078 * chat view to become too small. If the chat view is already
2079 * smaller than 250 make sure that size won't change. If the
2080 * chat view is bigger the contact list will take some space on
2081 * it but we make sure the chat view don't become smaller than
2082 * 250. Relax the size request once the resize is done */
2083 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2084 min_width = MIN (allocation.width, 250);
2085 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2086 g_idle_add (chat_reset_size_request, priv->vbox_left);
2088 if (priv->contacts_width > 0) {
2089 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2090 priv->contacts_width);
2093 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
2094 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2095 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2096 EMPATHY_CONTACT_FEATURE_CHAT |
2097 EMPATHY_CONTACT_FEATURE_CALL |
2098 EMPATHY_CONTACT_FEATURE_LOG |
2099 EMPATHY_CONTACT_FEATURE_INFO));
2100 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2101 priv->contact_list_view);
2102 gtk_widget_show (priv->contact_list_view);
2103 gtk_widget_show (priv->scrolled_window_contacts);
2104 g_object_unref (store);
2106 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2107 gtk_widget_hide (priv->scrolled_window_contacts);
2108 if (priv->contact_list_view != NULL) {
2109 gtk_widget_destroy (priv->contact_list_view);
2110 priv->contact_list_view = NULL;
2116 empathy_chat_set_show_contacts (EmpathyChat *chat,
2119 EmpathyChatPriv *priv = GET_PRIV (chat);
2121 priv->show_contacts = show;
2123 chat_update_contacts_visibility (chat, show);
2125 g_object_notify (G_OBJECT (chat), "show-contacts");
2129 chat_remote_contact_changed_cb (EmpathyChat *chat)
2131 EmpathyChatPriv *priv = GET_PRIV (chat);
2133 if (priv->remote_contact != NULL) {
2134 g_object_unref (priv->remote_contact);
2135 priv->remote_contact = NULL;
2140 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2141 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2142 if (priv->remote_contact != NULL) {
2143 g_object_ref (priv->remote_contact);
2144 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2146 else if (priv->tp_chat != NULL) {
2149 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2150 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2153 chat_update_contacts_visibility (chat, priv->show_contacts);
2155 g_object_notify (G_OBJECT (chat), "remote-contact");
2156 g_object_notify (G_OBJECT (chat), "id");
2160 chat_destroy_cb (EmpathyTpChat *tp_chat,
2163 EmpathyChatPriv *priv;
2165 priv = GET_PRIV (chat);
2167 if (!priv->tp_chat) {
2171 chat_composing_remove_timeout (chat);
2172 g_object_unref (priv->tp_chat);
2173 priv->tp_chat = NULL;
2174 g_object_notify (G_OBJECT (chat), "tp-chat");
2176 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2177 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2179 chat_update_contacts_visibility (chat, FALSE);
2183 chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
2186 hpaned_pos = gtk_paned_get_position (GTK_PANED(hpaned));
2187 empathy_conf_set_int (empathy_conf_get (),
2188 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2194 chat_create_ui (EmpathyChat *chat)
2196 EmpathyChatPriv *priv = GET_PRIV (chat);
2200 GtkTextBuffer *buffer;
2203 filename = empathy_file_lookup ("empathy-chat.ui",
2205 gui = empathy_builder_get_file (filename,
2206 "chat_widget", &priv->widget,
2207 "hpaned", &priv->hpaned,
2208 "vbox_left", &priv->vbox_left,
2209 "scrolled_window_chat", &priv->scrolled_window_chat,
2210 "scrolled_window_input", &priv->scrolled_window_input,
2211 "hbox_topic", &priv->hbox_topic,
2212 "label_topic", &priv->label_topic,
2213 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2214 "info_bar_vbox", &priv->info_bar_vbox,
2218 /* Add message view. */
2219 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
2220 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2221 and other types, even though it's non-editable and doesn't accept any
2222 drags. This steals drag motion for anything inside the scrollbars,
2223 making drag destinations on chat windows far less useful.
2225 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2226 g_signal_connect (chat->view, "focus_in_event",
2227 G_CALLBACK (chat_text_view_focus_in_event_cb),
2229 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2230 GTK_WIDGET (chat->view));
2231 gtk_widget_show (GTK_WIDGET (chat->view));
2233 /* Add input GtkTextView */
2234 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
2235 "pixels-above-lines", 2,
2236 "pixels-below-lines", 2,
2237 "pixels-inside-wrap", 1,
2240 "wrap-mode", GTK_WRAP_WORD_CHAR,
2242 g_signal_connect (chat->input_text_view, "key-press-event",
2243 G_CALLBACK (chat_input_key_press_event_cb),
2245 g_signal_connect (chat->input_text_view, "size-request",
2246 G_CALLBACK (chat_input_size_request_cb),
2248 g_signal_connect (chat->input_text_view, "realize",
2249 G_CALLBACK (chat_input_realize_cb),
2251 g_signal_connect (chat->input_text_view, "populate-popup",
2252 G_CALLBACK (chat_input_populate_popup_cb),
2254 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2255 g_signal_connect (buffer, "changed",
2256 G_CALLBACK (chat_input_text_buffer_changed_cb),
2258 gtk_text_buffer_create_tag (buffer, "misspelled",
2259 "underline", PANGO_UNDERLINE_ERROR,
2261 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2262 chat->input_text_view);
2263 gtk_widget_show (chat->input_text_view);
2265 /* Add the (invisible) search bar */
2266 priv->search_bar = empathy_search_bar_new (chat->view);
2267 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2270 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2272 /* Initialy hide the topic, will be shown if not empty */
2273 gtk_widget_hide (priv->hbox_topic);
2275 g_signal_connect (priv->hpaned, "notify::position",
2276 G_CALLBACK (chat_hpaned_pos_changed_cb),
2279 /* Load the paned position */
2280 if (empathy_conf_get_int (empathy_conf_get (),
2281 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2284 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2286 /* Set widget focus order */
2287 list = g_list_append (NULL, priv->search_bar);
2288 list = g_list_append (list, priv->scrolled_window_input);
2289 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2292 list = g_list_append (NULL, priv->vbox_left);
2293 list = g_list_append (list, priv->scrolled_window_contacts);
2294 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2297 list = g_list_append (NULL, priv->hpaned);
2298 list = g_list_append (list, priv->hbox_topic);
2299 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2302 /* Add the main widget in the chat widget */
2303 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2304 g_object_unref (gui);
2308 chat_size_request (GtkWidget *widget,
2309 GtkRequisition *requisition)
2311 GtkBin *bin = GTK_BIN (widget);
2314 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2315 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2317 child = gtk_bin_get_child (bin);
2319 if (child && gtk_widget_get_visible (child))
2321 GtkRequisition child_requisition;
2323 gtk_widget_size_request (child, &child_requisition);
2325 requisition->width += child_requisition.width;
2326 requisition->height += child_requisition.height;
2331 chat_size_allocate (GtkWidget *widget,
2332 GtkAllocation *allocation)
2334 GtkBin *bin = GTK_BIN (widget);
2335 GtkAllocation child_allocation;
2338 gtk_widget_set_allocation (widget, allocation);
2340 child = gtk_bin_get_child (bin);
2342 if (child && gtk_widget_get_visible (child))
2344 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2345 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2346 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2347 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2349 gtk_widget_size_allocate (child, &child_allocation);
2354 chat_finalize (GObject *object)
2357 EmpathyChatPriv *priv;
2359 chat = EMPATHY_CHAT (object);
2360 priv = GET_PRIV (chat);
2362 DEBUG ("Finalized: %p", object);
2364 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2365 g_list_free (priv->input_history);
2367 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2368 g_list_free (priv->compositors);
2370 chat_composing_remove_timeout (chat);
2372 g_object_unref (priv->account_manager);
2373 g_object_unref (priv->log_manager);
2375 if (priv->tp_chat) {
2376 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2377 chat_destroy_cb, chat);
2378 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2379 chat_message_received_cb, chat);
2380 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2381 chat_send_error_cb, chat);
2382 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2383 chat_state_changed_cb, chat);
2384 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2385 chat_property_changed_cb, chat);
2386 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2387 chat_members_changed_cb, chat);
2388 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2389 chat_remote_contact_changed_cb, chat);
2390 empathy_tp_chat_leave (priv->tp_chat);
2391 g_object_unref (priv->tp_chat);
2393 if (priv->account) {
2394 g_object_unref (priv->account);
2396 if (priv->remote_contact) {
2397 g_object_unref (priv->remote_contact);
2400 if (priv->block_events_timeout_id) {
2401 g_source_remove (priv->block_events_timeout_id);
2405 g_free (priv->name);
2406 g_free (priv->subject);
2407 g_completion_free (priv->completion);
2409 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2413 chat_constructed (GObject *object)
2415 EmpathyChat *chat = EMPATHY_CHAT (object);
2416 EmpathyChatPriv *priv = GET_PRIV (chat);
2418 if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
2419 chat_add_logs (chat);
2421 /* When async API are involved, pending message are shown at the end of the
2422 * callbacks' chain fired by chat_add_logs */
2423 priv->can_show_pending = TRUE;
2424 show_pending_messages (chat);
2425 #endif /* ENABLE_TPL */
2429 empathy_chat_class_init (EmpathyChatClass *klass)
2431 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2432 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2434 object_class->finalize = chat_finalize;
2435 object_class->get_property = chat_get_property;
2436 object_class->set_property = chat_set_property;
2437 object_class->constructed = chat_constructed;
2439 widget_class->size_request = chat_size_request;
2440 widget_class->size_allocate = chat_size_allocate;
2442 g_object_class_install_property (object_class,
2444 g_param_spec_object ("tp-chat",
2446 "The tp chat object",
2447 EMPATHY_TYPE_TP_CHAT,
2450 G_PARAM_STATIC_STRINGS));
2451 g_object_class_install_property (object_class,
2453 g_param_spec_object ("account",
2454 "Account of the chat",
2455 "The account of the chat",
2458 G_PARAM_STATIC_STRINGS));
2459 g_object_class_install_property (object_class,
2461 g_param_spec_string ("id",
2463 "The id of the chat",
2466 G_PARAM_STATIC_STRINGS));
2467 g_object_class_install_property (object_class,
2469 g_param_spec_string ("name",
2471 "The name of the chat",
2474 G_PARAM_STATIC_STRINGS));
2475 g_object_class_install_property (object_class,
2477 g_param_spec_string ("subject",
2479 "The subject or topic of the chat",
2482 G_PARAM_STATIC_STRINGS));
2483 g_object_class_install_property (object_class,
2484 PROP_REMOTE_CONTACT,
2485 g_param_spec_object ("remote-contact",
2486 "The remote contact",
2487 "The remote contact is any",
2488 EMPATHY_TYPE_CONTACT,
2490 G_PARAM_STATIC_STRINGS));
2491 g_object_class_install_property (object_class,
2493 g_param_spec_boolean ("show-contacts",
2494 "Contacts' visibility",
2495 "The visibility of the contacts' list",
2498 G_PARAM_STATIC_STRINGS));
2500 signals[COMPOSING] =
2501 g_signal_new ("composing",
2502 G_OBJECT_CLASS_TYPE (object_class),
2506 g_cclosure_marshal_VOID__BOOLEAN,
2510 signals[NEW_MESSAGE] =
2511 g_signal_new ("new-message",
2512 G_OBJECT_CLASS_TYPE (object_class),
2516 g_cclosure_marshal_VOID__OBJECT,
2518 1, EMPATHY_TYPE_MESSAGE);
2520 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2524 chat_block_events_timeout_cb (gpointer data)
2526 EmpathyChatPriv *priv = GET_PRIV (data);
2528 priv->block_events_timeout_id = 0;
2534 account_manager_prepared_cb (GObject *source_object,
2535 GAsyncResult *result,
2538 GList *accounts, *l;
2539 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2540 EmpathyChat *chat = user_data;
2541 GError *error = NULL;
2543 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2544 DEBUG ("Failed to prepare the account manager: %s", error->message);
2545 g_error_free (error);
2549 accounts = tp_account_manager_get_valid_accounts (account_manager);
2551 for (l = accounts; l != NULL; l = l->next) {
2552 TpAccount *account = l->data;
2553 empathy_signal_connect_weak (account, "status-changed",
2554 G_CALLBACK (chat_new_connection_cb),
2558 g_list_free (accounts);
2562 empathy_chat_init (EmpathyChat *chat)
2564 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2565 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2569 priv->log_manager = empathy_log_manager_dup_singleton ();
2571 priv->log_manager = tpl_log_manager_dup_singleton ();
2572 #endif /* ENABLE_TPL */
2573 priv->contacts_width = -1;
2574 priv->input_history = NULL;
2575 priv->input_history_current = NULL;
2576 priv->account_manager = tp_account_manager_dup ();
2578 tp_account_manager_prepare_async (priv->account_manager, NULL,
2579 account_manager_prepared_cb, chat);
2581 empathy_conf_get_bool (empathy_conf_get (),
2582 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
2583 &priv->show_contacts);
2585 /* Block events for some time to avoid having "has come online" or
2586 * "joined" messages. */
2587 priv->block_events_timeout_id =
2588 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2590 /* Add nick name completion */
2591 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
2592 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2594 chat_create_ui (chat);
2598 empathy_chat_new (EmpathyTpChat *tp_chat)
2600 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2604 empathy_chat_get_tp_chat (EmpathyChat *chat)
2606 EmpathyChatPriv *priv = GET_PRIV (chat);
2608 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2610 return priv->tp_chat;
2613 static void display_password_info_bar (EmpathyChat *self,
2617 provide_password_cb (GObject *tp_chat,
2621 EmpathyChat *self = EMPATHY_CHAT (user_data);
2622 EmpathyChatPriv *priv = GET_PRIV (self);
2623 GError *error = NULL;
2625 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
2627 DEBUG ("error: %s", error->message);
2628 /* FIXME: what should we do if that's another error? Close the channel?
2629 * Display the raw D-Bus error to the user isn't very useful */
2630 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
2631 display_password_info_bar (self, TRUE);
2632 g_error_free (error);
2637 gtk_widget_set_sensitive (priv->hpaned, TRUE);
2638 gtk_widget_grab_focus (self->input_text_view);
2642 password_infobar_response_cb (GtkWidget *info_bar,
2646 EmpathyChatPriv *priv = GET_PRIV (self);
2648 const gchar *password;
2650 if (response_id != GTK_RESPONSE_OK)
2653 entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
2654 g_assert (entry != NULL);
2656 password = gtk_entry_get_text (GTK_ENTRY (entry));
2658 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
2659 provide_password_cb, self);
2662 gtk_widget_destroy (info_bar);
2666 password_entry_activate_cb (GtkWidget *entry,
2667 GtkWidget *info_bar)
2669 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2673 passwd_join_button_cb (GtkButton *button,
2674 GtkWidget *info_bar)
2676 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2680 display_password_info_bar (EmpathyChat *self,
2683 EmpathyChatPriv *priv = GET_PRIV (self);
2684 GtkWidget *info_bar;
2685 GtkWidget *content_area;
2692 GtkMessageType type;
2693 const gchar *msg, *button_label;
2696 /* Previous password was wrong */
2697 type = GTK_MESSAGE_ERROR;
2698 msg = _("Wrong password; please try again:");
2699 button_label = _("Retry");
2702 /* First time we're trying to join */
2703 type = GTK_MESSAGE_QUESTION;
2704 msg = _("This room is protected by a password:");
2705 button_label = _("Join");
2708 info_bar = gtk_info_bar_new ();
2709 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
2711 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2713 hbox = gtk_hbox_new (FALSE, 3);
2714 gtk_container_add (GTK_CONTAINER (content_area), hbox);
2717 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
2718 GTK_ICON_SIZE_DIALOG);
2719 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2722 label = gtk_label_new (msg);
2723 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
2725 /* Add password entry */
2726 entry = gtk_entry_new ();
2727 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
2728 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
2730 g_signal_connect (entry, "activate",
2731 G_CALLBACK (password_entry_activate_cb), info_bar);
2733 /* Focus the password entry once it's realized */
2734 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
2736 /* Add 'Join' button */
2737 alig = gtk_alignment_new (0, 0.5, 0, 0);
2739 button = gtk_button_new_with_label (button_label);
2740 gtk_container_add (GTK_CONTAINER (alig), button);
2741 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
2743 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
2746 g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
2748 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
2750 gtk_widget_show_all (hbox);
2752 g_signal_connect (info_bar, "response",
2753 G_CALLBACK (password_infobar_response_cb), self);
2755 gtk_widget_show_all (info_bar);
2759 chat_password_needed_changed_cb (EmpathyChat *self)
2761 EmpathyChatPriv *priv = GET_PRIV (self);
2763 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
2764 display_password_info_bar (self, FALSE);
2765 gtk_widget_set_sensitive (priv->hpaned, FALSE);
2770 empathy_chat_set_tp_chat (EmpathyChat *chat,
2771 EmpathyTpChat *tp_chat)
2773 EmpathyChatPriv *priv = GET_PRIV (chat);
2774 TpConnection *connection;
2775 GPtrArray *properties;
2777 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2778 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
2779 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
2781 if (priv->tp_chat) {
2785 if (priv->account) {
2786 g_object_unref (priv->account);
2789 priv->tp_chat = g_object_ref (tp_chat);
2790 connection = empathy_tp_chat_get_connection (priv->tp_chat);
2791 priv->account = g_object_ref (empathy_get_account_for_connection (connection));
2793 g_signal_connect (tp_chat, "destroy",
2794 G_CALLBACK (chat_destroy_cb),
2796 g_signal_connect (tp_chat, "message-received",
2797 G_CALLBACK (chat_message_received_cb),
2799 g_signal_connect (tp_chat, "send-error",
2800 G_CALLBACK (chat_send_error_cb),
2802 g_signal_connect (tp_chat, "chat-state-changed",
2803 G_CALLBACK (chat_state_changed_cb),
2805 g_signal_connect (tp_chat, "property-changed",
2806 G_CALLBACK (chat_property_changed_cb),
2808 g_signal_connect (tp_chat, "members-changed",
2809 G_CALLBACK (chat_members_changed_cb),
2811 g_signal_connect (tp_chat, "member-renamed",
2812 G_CALLBACK (chat_member_renamed_cb),
2814 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
2815 G_CALLBACK (chat_remote_contact_changed_cb),
2817 g_signal_connect_swapped (tp_chat, "notify::password-needed",
2818 G_CALLBACK (chat_password_needed_changed_cb),
2821 /* Get initial value of properties */
2822 properties = empathy_tp_chat_get_properties (priv->tp_chat);
2823 if (properties != NULL) {
2826 for (i = 0; i < properties->len; i++) {
2827 EmpathyTpChatProperty *property;
2829 property = g_ptr_array_index (properties, i);
2830 if (property->value == NULL)
2833 chat_property_changed_cb (priv->tp_chat,
2840 chat_remote_contact_changed_cb (chat);
2842 if (chat->input_text_view) {
2843 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2844 if (priv->block_events_timeout_id == 0) {
2845 empathy_chat_view_append_event (chat->view, _("Connected"));
2849 g_object_notify (G_OBJECT (chat), "tp-chat");
2850 g_object_notify (G_OBJECT (chat), "id");
2851 g_object_notify (G_OBJECT (chat), "account");
2853 /* This is a noop when tp-chat is set at object construction time and causes
2854 * the pending messages to be show when it's set on the object after it has
2856 show_pending_messages (chat);
2858 /* check if a password is needed */
2859 chat_password_needed_changed_cb (chat);
2863 empathy_chat_get_account (EmpathyChat *chat)
2865 EmpathyChatPriv *priv = GET_PRIV (chat);
2867 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2869 return priv->account;
2873 empathy_chat_get_id (EmpathyChat *chat)
2875 EmpathyChatPriv *priv = GET_PRIV (chat);
2877 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2883 empathy_chat_get_name (EmpathyChat *chat)
2885 EmpathyChatPriv *priv = GET_PRIV (chat);
2888 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2891 if (!ret && priv->remote_contact) {
2892 ret = empathy_contact_get_name (priv->remote_contact);
2898 return ret ? ret : _("Conversation");
2902 empathy_chat_get_subject (EmpathyChat *chat)
2904 EmpathyChatPriv *priv = GET_PRIV (chat);
2906 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2908 return priv->subject;
2912 empathy_chat_get_remote_contact (EmpathyChat *chat)
2914 EmpathyChatPriv *priv = GET_PRIV (chat);
2916 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2918 return priv->remote_contact;
2922 empathy_chat_get_contact_menu (EmpathyChat *chat)
2924 EmpathyChatPriv *priv = GET_PRIV (chat);
2925 GtkWidget *menu = NULL;
2927 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2929 if (priv->remote_contact) {
2930 menu = empathy_contact_menu_new (priv->remote_contact,
2931 EMPATHY_CONTACT_FEATURE_CALL |
2932 EMPATHY_CONTACT_FEATURE_LOG |
2933 EMPATHY_CONTACT_FEATURE_INFO);
2935 else if (priv->contact_list_view) {
2936 EmpathyContactListView *view;
2938 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
2939 menu = empathy_contact_list_view_get_contact_menu (view);
2946 empathy_chat_clear (EmpathyChat *chat)
2948 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2950 empathy_chat_view_clear (chat->view);
2954 empathy_chat_scroll_down (EmpathyChat *chat)
2956 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2958 empathy_chat_view_scroll_down (chat->view);
2962 empathy_chat_cut (EmpathyChat *chat)
2964 GtkTextBuffer *buffer;
2966 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2968 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2969 if (gtk_text_buffer_get_has_selection (buffer)) {
2970 GtkClipboard *clipboard;
2972 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2974 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2979 empathy_chat_copy (EmpathyChat *chat)
2981 GtkTextBuffer *buffer;
2983 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2985 if (empathy_chat_view_get_has_selection (chat->view)) {
2986 empathy_chat_view_copy_clipboard (chat->view);
2990 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2991 if (gtk_text_buffer_get_has_selection (buffer)) {
2992 GtkClipboard *clipboard;
2994 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2996 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3001 empathy_chat_paste (EmpathyChat *chat)
3003 GtkTextBuffer *buffer;
3004 GtkClipboard *clipboard;
3005 EmpathyChatPriv *priv;
3007 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3009 priv = GET_PRIV (chat);
3011 if (priv->tp_chat == NULL ||
3012 !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
3015 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3016 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3018 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3022 empathy_chat_find (EmpathyChat *chat)
3024 EmpathyChatPriv *priv;
3026 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3028 priv = GET_PRIV (chat);
3030 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3034 empathy_chat_correct_word (EmpathyChat *chat,
3037 const gchar *new_word)
3039 GtkTextBuffer *buffer;
3041 g_return_if_fail (chat != NULL);
3042 g_return_if_fail (new_word != NULL);
3044 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3046 gtk_text_buffer_delete (buffer, start, end);
3047 gtk_text_buffer_insert (buffer, start,
3053 empathy_chat_is_room (EmpathyChat *chat)
3055 EmpathyChatPriv *priv = GET_PRIV (chat);
3057 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3059 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3063 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3065 EmpathyChatPriv *priv = GET_PRIV (self);
3067 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3069 return priv->unread_messages;
3072 /* called when the messages have been read by user */
3074 empathy_chat_messages_read (EmpathyChat *self)
3076 EmpathyChatPriv *priv = GET_PRIV (self);
3078 g_return_if_fail (EMPATHY_IS_CHAT (self));
3080 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3081 * definition. If we are still retrieving the backlogs, do not ACK */
3082 if (priv->retrieving_backlogs)
3085 if (priv->tp_chat != NULL ) {
3086 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3088 priv->unread_messages = 0;