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 EmpathyChatPriv *priv;
1248 GtkTextIter start, end;
1250 gboolean spell_checker = FALSE;
1252 priv = GET_PRIV (chat);
1254 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1255 chat_composing_stop (chat);
1257 chat_composing_start (chat);
1260 empathy_conf_get_bool (empathy_conf_get (),
1261 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
1264 gtk_text_buffer_get_start_iter (buffer, &start);
1266 if (!spell_checker) {
1267 gtk_text_buffer_get_end_iter (buffer, &end);
1268 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1272 if (!empathy_spell_supported ()) {
1276 /* NOTE: this is really inefficient, we shouldn't have to
1277 reiterate the whole buffer each time and check each work
1280 gboolean correct = FALSE;
1283 if (gtk_text_iter_is_start (&start)) {
1286 if (!gtk_text_iter_forward_word_end (&end)) {
1287 /* no whole word yet */
1291 if (!gtk_text_iter_forward_word_end (&end)) {
1292 /* must be the end of the buffer */
1297 gtk_text_iter_backward_word_start (&start);
1300 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1302 /* spell check string if not a command */
1303 if (str[0] != '/') {
1304 correct = empathy_spell_check (str);
1310 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1312 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1317 /* set start iter to the end iters position */
1323 empathy_isspace_cb (gunichar c,
1326 return g_unichar_isspace (c);
1330 chat_input_key_press_event_cb (GtkWidget *widget,
1334 EmpathyChatPriv *priv;
1337 GtkWidget *text_view_sw;
1339 priv = GET_PRIV (chat);
1341 /* Catch ctrl+up/down so we can traverse messages we sent */
1342 if ((event->state & GDK_CONTROL_MASK) &&
1343 (event->keyval == GDK_Up ||
1344 event->keyval == GDK_Down)) {
1345 GtkTextBuffer *buffer;
1348 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1349 chat_input_history_update (chat, buffer);
1351 if (event->keyval == GDK_Up) {
1352 str = chat_input_history_get_next (chat);
1354 str = chat_input_history_get_prev (chat);
1357 g_signal_handlers_block_by_func (buffer,
1358 chat_input_text_buffer_changed_cb,
1360 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1361 g_signal_handlers_unblock_by_func (buffer,
1362 chat_input_text_buffer_changed_cb,
1368 /* Catch enter but not ctrl/shift-enter */
1369 if (IS_ENTER (event->keyval) &&
1370 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1373 /* This is to make sure that kinput2 gets the enter. And if
1374 * it's handled there we shouldn't send on it. This is because
1375 * kinput2 uses Enter to commit letters. See:
1376 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1379 view = GTK_TEXT_VIEW (chat->input_text_view);
1380 if (gtk_im_context_filter_keypress (view->im_context, event)) {
1381 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
1385 chat_input_text_view_send (chat);
1389 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1391 if (IS_ENTER (event->keyval) &&
1392 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1393 /* Newline for shift/control-enter. */
1396 if (!(event->state & GDK_CONTROL_MASK) &&
1397 event->keyval == GDK_Page_Up) {
1398 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1399 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1402 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1403 event->keyval == GDK_Page_Down) {
1404 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1405 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1406 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1407 gtk_adjustment_set_value (adj, val);
1410 if (event->keyval == GDK_Escape) {
1411 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1413 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1414 event->keyval == GDK_Tab) {
1415 GtkTextBuffer *buffer;
1416 GtkTextIter start, current;
1417 gchar *nick, *completed;
1418 GList *list, *completed_list;
1419 gboolean is_start_of_buffer;
1421 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1422 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1424 /* Get the start of the nick to complete. */
1425 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1426 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1427 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1429 is_start_of_buffer = gtk_text_iter_is_start (&start);
1431 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1432 g_completion_add_items (priv->completion, list);
1434 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1435 completed_list = g_completion_complete (priv->completion,
1444 gchar *complete_char = NULL;
1445 GString *message = NULL;
1448 gtk_text_buffer_delete (buffer, &start, ¤t);
1450 len = g_list_length (completed_list);
1453 /* If we only have one hit, use that text
1454 * instead of the text in completed since the
1455 * completed text will use the typed string
1456 * which might be cased all wrong.
1459 text = empathy_contact_get_name (completed_list->data);
1463 /* Print all hits to the scrollback view, so the
1464 * user knows what possibilities he has.
1467 message = g_string_new ("");
1468 for (l = completed_list; l != NULL; l = l->next) {
1469 g_string_append (message, empathy_contact_get_name (l->data));
1470 g_string_append (message, " - ");
1472 empathy_chat_view_append_event (chat->view, message->str);
1473 g_string_free (message, TRUE);
1476 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1478 if (len == 1 && is_start_of_buffer &&
1479 empathy_conf_get_string (empathy_conf_get (),
1480 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1482 complete_char != NULL) {
1483 gtk_text_buffer_insert_at_cursor (buffer,
1485 strlen (complete_char));
1486 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1487 g_free (complete_char);
1493 g_completion_clear_items (priv->completion);
1495 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1505 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1509 gtk_widget_grab_focus (chat->input_text_view);
1515 chat_input_set_size_request_idle (gpointer sw)
1517 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1523 chat_input_size_request_cb (GtkWidget *widget,
1524 GtkRequisition *requisition,
1527 EmpathyChatPriv *priv = GET_PRIV (chat);
1530 sw = gtk_widget_get_parent (widget);
1531 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1532 g_idle_add (chat_input_set_size_request_idle, sw);
1533 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1536 priv->has_input_vscroll = TRUE;
1539 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1540 gtk_widget_set_size_request (sw, -1, -1);
1541 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1544 priv->has_input_vscroll = FALSE;
1549 chat_input_realize_cb (GtkWidget *widget,
1552 DEBUG ("Setting focus to the input text view");
1553 if (gtk_widget_is_sensitive (widget)) {
1554 gtk_widget_grab_focus (widget);
1559 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1560 EmpathySmiley *smiley,
1563 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1564 GtkTextBuffer *buffer;
1567 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1569 gtk_text_buffer_get_end_iter (buffer, &iter);
1570 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1572 gtk_text_buffer_get_end_iter (buffer, &iter);
1573 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1584 static EmpathyChatSpell *
1585 chat_spell_new (EmpathyChat *chat,
1590 EmpathyChatSpell *chat_spell;
1592 chat_spell = g_slice_new0 (EmpathyChatSpell);
1594 chat_spell->chat = g_object_ref (chat);
1595 chat_spell->word = g_strdup (word);
1596 chat_spell->start = start;
1597 chat_spell->end = end;
1603 chat_spell_free (EmpathyChatSpell *chat_spell)
1605 g_object_unref (chat_spell->chat);
1606 g_free (chat_spell->word);
1607 g_slice_free (EmpathyChatSpell, chat_spell);
1611 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1612 EmpathyChatSpell *chat_spell)
1614 empathy_chat_correct_word (chat_spell->chat,
1615 &(chat_spell->start),
1617 gtk_menu_item_get_label (menu_item));
1621 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1623 GtkWidget *menu, *menu_item;
1624 GList *suggestions, *l;
1626 menu = gtk_menu_new ();
1627 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1628 if (suggestions == NULL) {
1629 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1630 gtk_widget_set_sensitive (menu_item, FALSE);
1631 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1633 for (l = suggestions; l; l = l->next) {
1634 menu_item = gtk_menu_item_new_with_label (l->data);
1635 g_signal_connect (G_OBJECT (menu_item),
1637 G_CALLBACK (chat_spelling_menu_activate_cb),
1639 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1642 empathy_spell_free_suggestions (suggestions);
1644 gtk_widget_show_all (menu);
1650 chat_text_send_cb (GtkMenuItem *menuitem,
1653 chat_input_text_view_send (chat);
1657 chat_input_populate_popup_cb (GtkTextView *view,
1661 EmpathyChatPriv *priv;
1662 GtkTextBuffer *buffer;
1663 GtkTextTagTable *table;
1666 GtkTextIter iter, start, end;
1669 EmpathyChatSpell *chat_spell;
1670 GtkWidget *spell_menu;
1671 EmpathySmileyManager *smiley_manager;
1672 GtkWidget *smiley_menu;
1675 priv = GET_PRIV (chat);
1676 buffer = gtk_text_view_get_buffer (view);
1678 /* Add the emoticon menu. */
1679 item = gtk_separator_menu_item_new ();
1680 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1681 gtk_widget_show (item);
1683 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1684 image = gtk_image_new_from_icon_name ("face-smile",
1685 GTK_ICON_SIZE_MENU);
1686 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1687 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1688 gtk_widget_show (item);
1690 smiley_manager = empathy_smiley_manager_dup_singleton ();
1691 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1692 chat_insert_smiley_activate_cb,
1694 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1695 g_object_unref (smiley_manager);
1697 /* Add the Send menu item. */
1698 gtk_text_buffer_get_bounds (buffer, &start, &end);
1699 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1700 if (!EMP_STR_EMPTY (str)) {
1701 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1702 g_signal_connect (G_OBJECT (item), "activate",
1703 G_CALLBACK (chat_text_send_cb), chat);
1704 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1705 gtk_widget_show (item);
1709 /* Add the spell check menu item. */
1710 table = gtk_text_buffer_get_tag_table (buffer);
1711 tag = gtk_text_tag_table_lookup (table, "misspelled");
1712 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1713 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1714 GTK_TEXT_WINDOW_WIDGET,
1717 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1719 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1720 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1722 str = gtk_text_buffer_get_text (buffer,
1723 &start, &end, FALSE);
1725 if (!EMP_STR_EMPTY (str)) {
1726 chat_spell = chat_spell_new (chat, str, start, end);
1727 g_object_set_data_full (G_OBJECT (menu),
1728 "chat_spell", chat_spell,
1729 (GDestroyNotify) chat_spell_free);
1731 item = gtk_separator_menu_item_new ();
1732 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1733 gtk_widget_show (item);
1735 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1736 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1737 GTK_ICON_SIZE_MENU);
1738 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1740 spell_menu = chat_spelling_build_menu (chat_spell);
1741 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1743 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1744 gtk_widget_show (item);
1751 chat_log_filter (TplLogEntry *log,
1755 chat_log_filter (EmpathyMessage *message,
1757 #endif /* ENABLE_TPL */
1759 EmpathyChat *chat = user_data;
1761 EmpathyMessage *message;
1762 #endif /* ENABLE_TPL */
1763 EmpathyChatPriv *priv = GET_PRIV (chat);
1764 const GList *pending;
1767 g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
1769 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1770 #endif /* ENABLE_TPL */
1771 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1773 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1775 message = empathy_message_from_tpl_log_entry (log);
1776 #endif /* ENABLE_TPL */
1778 for (; pending; pending = g_list_next (pending)) {
1779 if (empathy_message_equal (message, pending->data)) {
1784 g_object_unref (message);
1791 show_pending_messages (EmpathyChat *chat) {
1792 EmpathyChatPriv *priv = GET_PRIV (chat);
1793 const GList *messages, *l;
1795 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1797 if (chat->view == NULL || priv->tp_chat == NULL)
1800 if (!priv->can_show_pending)
1803 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1805 for (l = messages; l != NULL ; l = g_list_next (l)) {
1806 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1807 chat_message_received (chat, message);
1814 got_filtered_messages_cb (GObject *manager,
1815 GAsyncResult *result,
1820 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1821 EmpathyChatPriv *priv = GET_PRIV (chat);
1822 GError *error = NULL;
1824 messages = tpl_log_manager_get_filtered_messages_async_finish (result, &error);
1826 if (error != NULL) {
1827 DEBUG ("%s. Aborting.", error->message);
1828 empathy_chat_view_append_event (chat->view,
1829 _("Failed to retrieve recent logs"));
1830 g_error_free (error);
1834 for (l = messages; l; l = g_list_next (l)) {
1835 EmpathyMessage *message;
1836 g_assert (TPL_IS_LOG_ENTRY (l->data));
1838 message = empathy_message_from_tpl_log_entry (l->data);
1839 g_object_unref (l->data);
1841 empathy_chat_view_append_message (chat->view, message);
1842 g_object_unref (message);
1844 g_list_free (messages);
1847 /* in case of TPL error, skip backlog and show pending messages */
1848 priv->can_show_pending = TRUE;
1849 show_pending_messages (chat);
1851 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
1852 * about it in EmpathyChatPriv definition */
1853 priv->retrieving_backlogs = FALSE;
1854 empathy_chat_messages_read (chat);
1856 /* Turn back on scrolling */
1857 empathy_chat_view_scroll (chat->view, TRUE);
1859 #endif /* ENABLE_TPL */
1863 chat_add_logs (EmpathyChat *chat)
1865 EmpathyChatPriv *priv = GET_PRIV (chat);
1866 gboolean is_chatroom;
1868 GList *messages, *l;
1869 #endif /* ENABLE_TPL */
1875 /* Turn off scrolling temporarily */
1876 empathy_chat_view_scroll (chat->view, FALSE);
1878 /* Add messages from last conversation */
1879 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1882 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1890 for (l = messages; l; l = g_list_next (l)) {
1891 empathy_chat_view_append_message (chat->view, l->data);
1892 g_object_unref (l->data);
1895 g_list_free (messages);
1897 /* Turn back on scrolling */
1898 empathy_chat_view_scroll (chat->view, TRUE);
1900 priv->retrieving_backlogs = TRUE;
1901 tpl_log_manager_get_filtered_messages_async (priv->log_manager,
1908 got_filtered_messages_cb,
1910 #endif /* ENABLE_TPL */
1914 chat_contacts_completion_func (const gchar *s1,
1918 gchar *tmp, *nick1, *nick2;
1925 return s1 ? -1 : +1;
1928 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1929 nick1 = g_utf8_casefold (tmp, -1);
1932 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1933 nick2 = g_utf8_casefold (tmp, -1);
1936 ret = strncmp (nick1, nick2, n);
1945 build_part_message (guint reason,
1947 EmpathyContact *actor,
1948 const gchar *message)
1950 GString *s = g_string_new ("");
1951 const gchar *actor_name = NULL;
1953 if (actor != NULL) {
1954 actor_name = empathy_contact_get_name (actor);
1957 /* Having an actor only really makes sense for a few actions... */
1959 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1960 g_string_append_printf (s, _("%s has disconnected"), name);
1962 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1963 if (actor_name != NULL) {
1964 /* translators: reverse the order of these arguments
1965 * if the kicked should come before the kicker in your locale.
1967 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
1970 g_string_append_printf (s, _("%s was kicked"), name);
1973 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1974 if (actor_name != NULL) {
1975 /* translators: reverse the order of these arguments
1976 * if the banned should come before the banner in your locale.
1978 g_string_append_printf (s, _("%1$s was banned by %2$s"),
1981 g_string_append_printf (s, _("%s was banned"), name);
1985 g_string_append_printf (s, _("%s has left the room"), name);
1988 if (!EMP_STR_EMPTY (message)) {
1989 /* Note to translators: this string is appended to
1990 * notifications like "foo has left the room", with the message
1991 * given by the user living the room. If this poses a problem,
1992 * please let us know. :-)
1994 g_string_append_printf (s, _(" (%s)"), message);
1997 return g_string_free (s, FALSE);
2001 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2002 EmpathyContact *contact,
2003 EmpathyContact *actor,
2009 EmpathyChatPriv *priv = GET_PRIV (chat);
2010 const gchar *name = empathy_contact_get_name (contact);
2013 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2015 if (priv->block_events_timeout_id != 0)
2019 str = g_strdup_printf (_("%s has joined the room"),
2022 str = build_part_message (reason, name, actor, message);
2025 empathy_chat_view_append_event (chat->view, str);
2030 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2031 EmpathyContact *old_contact,
2032 EmpathyContact *new_contact,
2037 EmpathyChatPriv *priv = GET_PRIV (chat);
2039 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2041 if (priv->block_events_timeout_id == 0) {
2044 str = g_strdup_printf (_("%s is now known as %s"),
2045 empathy_contact_get_name (old_contact),
2046 empathy_contact_get_name (new_contact));
2047 empathy_chat_view_append_event (chat->view, str);
2054 chat_reset_size_request (gpointer widget)
2056 gtk_widget_set_size_request (widget, -1, -1);
2062 chat_update_contacts_visibility (EmpathyChat *chat,
2065 EmpathyChatPriv *priv = GET_PRIV (chat);
2066 GtkAllocation allocation;
2068 if (!priv->scrolled_window_contacts) {
2072 if (priv->remote_contact != NULL) {
2076 if (show && priv->contact_list_view == NULL) {
2077 EmpathyContactListStore *store;
2080 /* We are adding the contact list to the chat, we don't want the
2081 * chat view to become too small. If the chat view is already
2082 * smaller than 250 make sure that size won't change. If the
2083 * chat view is bigger the contact list will take some space on
2084 * it but we make sure the chat view don't become smaller than
2085 * 250. Relax the size request once the resize is done */
2086 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2087 min_width = MIN (allocation.width, 250);
2088 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2089 g_idle_add (chat_reset_size_request, priv->vbox_left);
2091 if (priv->contacts_width > 0) {
2092 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2093 priv->contacts_width);
2096 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
2097 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2098 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2099 EMPATHY_CONTACT_FEATURE_CHAT |
2100 EMPATHY_CONTACT_FEATURE_CALL |
2101 EMPATHY_CONTACT_FEATURE_LOG |
2102 EMPATHY_CONTACT_FEATURE_INFO));
2103 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2104 priv->contact_list_view);
2105 gtk_widget_show (priv->contact_list_view);
2106 gtk_widget_show (priv->scrolled_window_contacts);
2107 g_object_unref (store);
2109 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2110 gtk_widget_hide (priv->scrolled_window_contacts);
2111 if (priv->contact_list_view != NULL) {
2112 gtk_widget_destroy (priv->contact_list_view);
2113 priv->contact_list_view = NULL;
2119 empathy_chat_set_show_contacts (EmpathyChat *chat,
2122 EmpathyChatPriv *priv = GET_PRIV (chat);
2124 priv->show_contacts = show;
2126 chat_update_contacts_visibility (chat, show);
2128 g_object_notify (G_OBJECT (chat), "show-contacts");
2132 chat_remote_contact_changed_cb (EmpathyChat *chat)
2134 EmpathyChatPriv *priv = GET_PRIV (chat);
2136 if (priv->remote_contact != NULL) {
2137 g_object_unref (priv->remote_contact);
2138 priv->remote_contact = NULL;
2143 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2144 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2145 if (priv->remote_contact != NULL) {
2146 g_object_ref (priv->remote_contact);
2147 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2149 else if (priv->tp_chat != NULL) {
2152 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2153 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2156 chat_update_contacts_visibility (chat, priv->show_contacts);
2158 g_object_notify (G_OBJECT (chat), "remote-contact");
2159 g_object_notify (G_OBJECT (chat), "id");
2163 chat_destroy_cb (EmpathyTpChat *tp_chat,
2166 EmpathyChatPriv *priv;
2168 priv = GET_PRIV (chat);
2170 if (!priv->tp_chat) {
2174 chat_composing_remove_timeout (chat);
2175 g_object_unref (priv->tp_chat);
2176 priv->tp_chat = NULL;
2177 g_object_notify (G_OBJECT (chat), "tp-chat");
2179 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2180 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2182 chat_update_contacts_visibility (chat, FALSE);
2186 chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
2189 hpaned_pos = gtk_paned_get_position (GTK_PANED(hpaned));
2190 empathy_conf_set_int (empathy_conf_get (),
2191 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2197 chat_create_ui (EmpathyChat *chat)
2199 EmpathyChatPriv *priv = GET_PRIV (chat);
2203 GtkTextBuffer *buffer;
2206 filename = empathy_file_lookup ("empathy-chat.ui",
2208 gui = empathy_builder_get_file (filename,
2209 "chat_widget", &priv->widget,
2210 "hpaned", &priv->hpaned,
2211 "vbox_left", &priv->vbox_left,
2212 "scrolled_window_chat", &priv->scrolled_window_chat,
2213 "scrolled_window_input", &priv->scrolled_window_input,
2214 "hbox_topic", &priv->hbox_topic,
2215 "label_topic", &priv->label_topic,
2216 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2217 "info_bar_vbox", &priv->info_bar_vbox,
2221 /* Add message view. */
2222 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
2223 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2224 and other types, even though it's non-editable and doesn't accept any
2225 drags. This steals drag motion for anything inside the scrollbars,
2226 making drag destinations on chat windows far less useful.
2228 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2229 g_signal_connect (chat->view, "focus_in_event",
2230 G_CALLBACK (chat_text_view_focus_in_event_cb),
2232 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2233 GTK_WIDGET (chat->view));
2234 gtk_widget_show (GTK_WIDGET (chat->view));
2236 /* Add input GtkTextView */
2237 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
2238 "pixels-above-lines", 2,
2239 "pixels-below-lines", 2,
2240 "pixels-inside-wrap", 1,
2243 "wrap-mode", GTK_WRAP_WORD_CHAR,
2245 g_signal_connect (chat->input_text_view, "key-press-event",
2246 G_CALLBACK (chat_input_key_press_event_cb),
2248 g_signal_connect (chat->input_text_view, "size-request",
2249 G_CALLBACK (chat_input_size_request_cb),
2251 g_signal_connect (chat->input_text_view, "realize",
2252 G_CALLBACK (chat_input_realize_cb),
2254 g_signal_connect (chat->input_text_view, "populate-popup",
2255 G_CALLBACK (chat_input_populate_popup_cb),
2257 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2258 g_signal_connect (buffer, "changed",
2259 G_CALLBACK (chat_input_text_buffer_changed_cb),
2261 gtk_text_buffer_create_tag (buffer, "misspelled",
2262 "underline", PANGO_UNDERLINE_ERROR,
2264 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2265 chat->input_text_view);
2266 gtk_widget_show (chat->input_text_view);
2268 /* Add the (invisible) search bar */
2269 priv->search_bar = empathy_search_bar_new (chat->view);
2270 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2273 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2275 /* Initialy hide the topic, will be shown if not empty */
2276 gtk_widget_hide (priv->hbox_topic);
2278 g_signal_connect (priv->hpaned, "notify::position",
2279 G_CALLBACK (chat_hpaned_pos_changed_cb),
2282 /* Load the paned position */
2283 if (empathy_conf_get_int (empathy_conf_get (),
2284 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2287 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2289 /* Set widget focus order */
2290 list = g_list_append (NULL, priv->search_bar);
2291 list = g_list_append (list, priv->scrolled_window_input);
2292 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2295 list = g_list_append (NULL, priv->vbox_left);
2296 list = g_list_append (list, priv->scrolled_window_contacts);
2297 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2300 list = g_list_append (NULL, priv->hpaned);
2301 list = g_list_append (list, priv->hbox_topic);
2302 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2305 /* Add the main widget in the chat widget */
2306 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2307 g_object_unref (gui);
2311 chat_size_request (GtkWidget *widget,
2312 GtkRequisition *requisition)
2314 GtkBin *bin = GTK_BIN (widget);
2317 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2318 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2320 child = gtk_bin_get_child (bin);
2322 if (child && gtk_widget_get_visible (child))
2324 GtkRequisition child_requisition;
2326 gtk_widget_size_request (child, &child_requisition);
2328 requisition->width += child_requisition.width;
2329 requisition->height += child_requisition.height;
2334 chat_size_allocate (GtkWidget *widget,
2335 GtkAllocation *allocation)
2337 GtkBin *bin = GTK_BIN (widget);
2338 GtkAllocation child_allocation;
2341 gtk_widget_set_allocation (widget, allocation);
2343 child = gtk_bin_get_child (bin);
2345 if (child && gtk_widget_get_visible (child))
2347 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2348 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2349 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2350 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2352 gtk_widget_size_allocate (child, &child_allocation);
2357 chat_finalize (GObject *object)
2360 EmpathyChatPriv *priv;
2362 chat = EMPATHY_CHAT (object);
2363 priv = GET_PRIV (chat);
2365 DEBUG ("Finalized: %p", object);
2367 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2368 g_list_free (priv->input_history);
2370 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2371 g_list_free (priv->compositors);
2373 chat_composing_remove_timeout (chat);
2375 g_object_unref (priv->account_manager);
2376 g_object_unref (priv->log_manager);
2378 if (priv->tp_chat) {
2379 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2380 chat_destroy_cb, chat);
2381 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2382 chat_message_received_cb, chat);
2383 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2384 chat_send_error_cb, chat);
2385 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2386 chat_state_changed_cb, chat);
2387 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2388 chat_property_changed_cb, chat);
2389 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2390 chat_members_changed_cb, chat);
2391 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2392 chat_remote_contact_changed_cb, chat);
2393 empathy_tp_chat_leave (priv->tp_chat);
2394 g_object_unref (priv->tp_chat);
2396 if (priv->account) {
2397 g_object_unref (priv->account);
2399 if (priv->remote_contact) {
2400 g_object_unref (priv->remote_contact);
2403 if (priv->block_events_timeout_id) {
2404 g_source_remove (priv->block_events_timeout_id);
2408 g_free (priv->name);
2409 g_free (priv->subject);
2410 g_completion_free (priv->completion);
2412 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2416 chat_constructed (GObject *object)
2418 EmpathyChat *chat = EMPATHY_CHAT (object);
2419 EmpathyChatPriv *priv = GET_PRIV (chat);
2421 if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
2422 chat_add_logs (chat);
2424 /* When async API are involved, pending message are shown at the end of the
2425 * callbacks' chain fired by chat_add_logs */
2426 priv->can_show_pending = TRUE;
2427 show_pending_messages (chat);
2428 #endif /* ENABLE_TPL */
2432 empathy_chat_class_init (EmpathyChatClass *klass)
2434 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2435 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2437 object_class->finalize = chat_finalize;
2438 object_class->get_property = chat_get_property;
2439 object_class->set_property = chat_set_property;
2440 object_class->constructed = chat_constructed;
2442 widget_class->size_request = chat_size_request;
2443 widget_class->size_allocate = chat_size_allocate;
2445 g_object_class_install_property (object_class,
2447 g_param_spec_object ("tp-chat",
2449 "The tp chat object",
2450 EMPATHY_TYPE_TP_CHAT,
2453 G_PARAM_STATIC_STRINGS));
2454 g_object_class_install_property (object_class,
2456 g_param_spec_object ("account",
2457 "Account of the chat",
2458 "The account of the chat",
2461 G_PARAM_STATIC_STRINGS));
2462 g_object_class_install_property (object_class,
2464 g_param_spec_string ("id",
2466 "The id of the chat",
2469 G_PARAM_STATIC_STRINGS));
2470 g_object_class_install_property (object_class,
2472 g_param_spec_string ("name",
2474 "The name of the chat",
2477 G_PARAM_STATIC_STRINGS));
2478 g_object_class_install_property (object_class,
2480 g_param_spec_string ("subject",
2482 "The subject or topic of the chat",
2485 G_PARAM_STATIC_STRINGS));
2486 g_object_class_install_property (object_class,
2487 PROP_REMOTE_CONTACT,
2488 g_param_spec_object ("remote-contact",
2489 "The remote contact",
2490 "The remote contact is any",
2491 EMPATHY_TYPE_CONTACT,
2493 G_PARAM_STATIC_STRINGS));
2494 g_object_class_install_property (object_class,
2496 g_param_spec_boolean ("show-contacts",
2497 "Contacts' visibility",
2498 "The visibility of the contacts' list",
2501 G_PARAM_STATIC_STRINGS));
2503 signals[COMPOSING] =
2504 g_signal_new ("composing",
2505 G_OBJECT_CLASS_TYPE (object_class),
2509 g_cclosure_marshal_VOID__BOOLEAN,
2513 signals[NEW_MESSAGE] =
2514 g_signal_new ("new-message",
2515 G_OBJECT_CLASS_TYPE (object_class),
2519 g_cclosure_marshal_VOID__OBJECT,
2521 1, EMPATHY_TYPE_MESSAGE);
2523 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2527 chat_block_events_timeout_cb (gpointer data)
2529 EmpathyChatPriv *priv = GET_PRIV (data);
2531 priv->block_events_timeout_id = 0;
2537 account_manager_prepared_cb (GObject *source_object,
2538 GAsyncResult *result,
2541 GList *accounts, *l;
2542 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2543 EmpathyChat *chat = user_data;
2544 GError *error = NULL;
2546 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2547 DEBUG ("Failed to prepare the account manager: %s", error->message);
2548 g_error_free (error);
2552 accounts = tp_account_manager_get_valid_accounts (account_manager);
2554 for (l = accounts; l != NULL; l = l->next) {
2555 TpAccount *account = l->data;
2556 empathy_signal_connect_weak (account, "status-changed",
2557 G_CALLBACK (chat_new_connection_cb),
2561 g_list_free (accounts);
2565 empathy_chat_init (EmpathyChat *chat)
2567 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2568 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2572 priv->log_manager = empathy_log_manager_dup_singleton ();
2574 priv->log_manager = tpl_log_manager_dup_singleton ();
2575 #endif /* ENABLE_TPL */
2576 priv->contacts_width = -1;
2577 priv->input_history = NULL;
2578 priv->input_history_current = NULL;
2579 priv->account_manager = tp_account_manager_dup ();
2581 tp_account_manager_prepare_async (priv->account_manager, NULL,
2582 account_manager_prepared_cb, chat);
2584 empathy_conf_get_bool (empathy_conf_get (),
2585 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
2586 &priv->show_contacts);
2588 /* Block events for some time to avoid having "has come online" or
2589 * "joined" messages. */
2590 priv->block_events_timeout_id =
2591 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2593 /* Add nick name completion */
2594 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
2595 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2597 chat_create_ui (chat);
2601 empathy_chat_new (EmpathyTpChat *tp_chat)
2603 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2607 empathy_chat_get_tp_chat (EmpathyChat *chat)
2609 EmpathyChatPriv *priv = GET_PRIV (chat);
2611 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2613 return priv->tp_chat;
2616 static void display_password_info_bar (EmpathyChat *self,
2620 provide_password_cb (GObject *tp_chat,
2624 EmpathyChat *self = EMPATHY_CHAT (user_data);
2625 EmpathyChatPriv *priv = GET_PRIV (self);
2626 GError *error = NULL;
2628 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
2630 DEBUG ("error: %s", error->message);
2631 /* FIXME: what should we do if that's another error? Close the channel?
2632 * Display the raw D-Bus error to the user isn't very useful */
2633 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
2634 display_password_info_bar (self, TRUE);
2635 g_error_free (error);
2640 gtk_widget_set_sensitive (priv->hpaned, TRUE);
2641 gtk_widget_grab_focus (self->input_text_view);
2645 password_infobar_response_cb (GtkWidget *info_bar,
2649 EmpathyChatPriv *priv = GET_PRIV (self);
2651 const gchar *password;
2653 if (response_id != GTK_RESPONSE_OK)
2656 entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
2657 g_assert (entry != NULL);
2659 password = gtk_entry_get_text (GTK_ENTRY (entry));
2661 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
2662 provide_password_cb, self);
2665 gtk_widget_destroy (info_bar);
2669 password_entry_activate_cb (GtkWidget *entry,
2670 GtkWidget *info_bar)
2672 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2676 passwd_join_button_cb (GtkButton *button,
2677 GtkWidget *info_bar)
2679 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2683 display_password_info_bar (EmpathyChat *self,
2686 EmpathyChatPriv *priv = GET_PRIV (self);
2687 GtkWidget *info_bar;
2688 GtkWidget *content_area;
2695 GtkMessageType type;
2696 const gchar *msg, *button_label;
2699 /* Previous password was wrong */
2700 type = GTK_MESSAGE_ERROR;
2701 msg = _("Wrong password; please try again:");
2702 button_label = _("Retry");
2705 /* First time we're trying to join */
2706 type = GTK_MESSAGE_QUESTION;
2707 msg = _("This room is protected by a password:");
2708 button_label = _("Join");
2711 info_bar = gtk_info_bar_new ();
2712 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
2714 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2716 hbox = gtk_hbox_new (FALSE, 3);
2717 gtk_container_add (GTK_CONTAINER (content_area), hbox);
2720 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
2721 GTK_ICON_SIZE_DIALOG);
2722 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2725 label = gtk_label_new (msg);
2726 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
2728 /* Add password entry */
2729 entry = gtk_entry_new ();
2730 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
2731 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
2733 g_signal_connect (entry, "activate",
2734 G_CALLBACK (password_entry_activate_cb), info_bar);
2736 /* Focus the password entry once it's realized */
2737 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
2739 /* Add 'Join' button */
2740 alig = gtk_alignment_new (0, 0.5, 0, 0);
2742 button = gtk_button_new_with_label (button_label);
2743 gtk_container_add (GTK_CONTAINER (alig), button);
2744 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
2746 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
2749 g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
2751 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
2753 gtk_widget_show_all (hbox);
2755 g_signal_connect (info_bar, "response",
2756 G_CALLBACK (password_infobar_response_cb), self);
2758 gtk_widget_show_all (info_bar);
2762 chat_password_needed_changed_cb (EmpathyChat *self)
2764 EmpathyChatPriv *priv = GET_PRIV (self);
2766 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
2767 display_password_info_bar (self, FALSE);
2768 gtk_widget_set_sensitive (priv->hpaned, FALSE);
2773 empathy_chat_set_tp_chat (EmpathyChat *chat,
2774 EmpathyTpChat *tp_chat)
2776 EmpathyChatPriv *priv = GET_PRIV (chat);
2777 TpConnection *connection;
2778 GPtrArray *properties;
2780 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2781 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
2782 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
2784 if (priv->tp_chat) {
2788 if (priv->account) {
2789 g_object_unref (priv->account);
2792 priv->tp_chat = g_object_ref (tp_chat);
2793 connection = empathy_tp_chat_get_connection (priv->tp_chat);
2794 priv->account = g_object_ref (empathy_get_account_for_connection (connection));
2796 g_signal_connect (tp_chat, "destroy",
2797 G_CALLBACK (chat_destroy_cb),
2799 g_signal_connect (tp_chat, "message-received",
2800 G_CALLBACK (chat_message_received_cb),
2802 g_signal_connect (tp_chat, "send-error",
2803 G_CALLBACK (chat_send_error_cb),
2805 g_signal_connect (tp_chat, "chat-state-changed",
2806 G_CALLBACK (chat_state_changed_cb),
2808 g_signal_connect (tp_chat, "property-changed",
2809 G_CALLBACK (chat_property_changed_cb),
2811 g_signal_connect (tp_chat, "members-changed",
2812 G_CALLBACK (chat_members_changed_cb),
2814 g_signal_connect (tp_chat, "member-renamed",
2815 G_CALLBACK (chat_member_renamed_cb),
2817 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
2818 G_CALLBACK (chat_remote_contact_changed_cb),
2820 g_signal_connect_swapped (tp_chat, "notify::password-needed",
2821 G_CALLBACK (chat_password_needed_changed_cb),
2824 /* Get initial value of properties */
2825 properties = empathy_tp_chat_get_properties (priv->tp_chat);
2826 if (properties != NULL) {
2829 for (i = 0; i < properties->len; i++) {
2830 EmpathyTpChatProperty *property;
2832 property = g_ptr_array_index (properties, i);
2833 if (property->value == NULL)
2836 chat_property_changed_cb (priv->tp_chat,
2843 chat_remote_contact_changed_cb (chat);
2845 if (chat->input_text_view) {
2846 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2847 if (priv->block_events_timeout_id == 0) {
2848 empathy_chat_view_append_event (chat->view, _("Connected"));
2852 g_object_notify (G_OBJECT (chat), "tp-chat");
2853 g_object_notify (G_OBJECT (chat), "id");
2854 g_object_notify (G_OBJECT (chat), "account");
2856 /* This is a noop when tp-chat is set at object construction time and causes
2857 * the pending messages to be show when it's set on the object after it has
2859 show_pending_messages (chat);
2861 /* check if a password is needed */
2862 chat_password_needed_changed_cb (chat);
2866 empathy_chat_get_account (EmpathyChat *chat)
2868 EmpathyChatPriv *priv = GET_PRIV (chat);
2870 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2872 return priv->account;
2876 empathy_chat_get_id (EmpathyChat *chat)
2878 EmpathyChatPriv *priv = GET_PRIV (chat);
2880 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2886 empathy_chat_get_name (EmpathyChat *chat)
2888 EmpathyChatPriv *priv = GET_PRIV (chat);
2891 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2894 if (!ret && priv->remote_contact) {
2895 ret = empathy_contact_get_name (priv->remote_contact);
2901 return ret ? ret : _("Conversation");
2905 empathy_chat_get_subject (EmpathyChat *chat)
2907 EmpathyChatPriv *priv = GET_PRIV (chat);
2909 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2911 return priv->subject;
2915 empathy_chat_get_remote_contact (EmpathyChat *chat)
2917 EmpathyChatPriv *priv = GET_PRIV (chat);
2919 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2921 return priv->remote_contact;
2925 empathy_chat_get_contact_menu (EmpathyChat *chat)
2927 EmpathyChatPriv *priv = GET_PRIV (chat);
2928 GtkWidget *menu = NULL;
2930 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2932 if (priv->remote_contact) {
2933 menu = empathy_contact_menu_new (priv->remote_contact,
2934 EMPATHY_CONTACT_FEATURE_CALL |
2935 EMPATHY_CONTACT_FEATURE_LOG |
2936 EMPATHY_CONTACT_FEATURE_INFO);
2938 else if (priv->contact_list_view) {
2939 EmpathyContactListView *view;
2941 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
2942 menu = empathy_contact_list_view_get_contact_menu (view);
2949 empathy_chat_clear (EmpathyChat *chat)
2951 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2953 empathy_chat_view_clear (chat->view);
2957 empathy_chat_scroll_down (EmpathyChat *chat)
2959 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2961 empathy_chat_view_scroll_down (chat->view);
2965 empathy_chat_cut (EmpathyChat *chat)
2967 GtkTextBuffer *buffer;
2969 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2971 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2972 if (gtk_text_buffer_get_has_selection (buffer)) {
2973 GtkClipboard *clipboard;
2975 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2977 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2982 empathy_chat_copy (EmpathyChat *chat)
2984 GtkTextBuffer *buffer;
2986 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2988 if (empathy_chat_view_get_has_selection (chat->view)) {
2989 empathy_chat_view_copy_clipboard (chat->view);
2993 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2994 if (gtk_text_buffer_get_has_selection (buffer)) {
2995 GtkClipboard *clipboard;
2997 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2999 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3004 empathy_chat_paste (EmpathyChat *chat)
3006 GtkTextBuffer *buffer;
3007 GtkClipboard *clipboard;
3008 EmpathyChatPriv *priv;
3010 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3012 priv = GET_PRIV (chat);
3014 if (priv->tp_chat == NULL ||
3015 !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
3018 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3019 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3021 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3025 empathy_chat_find (EmpathyChat *chat)
3027 EmpathyChatPriv *priv;
3029 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3031 priv = GET_PRIV (chat);
3033 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3037 empathy_chat_correct_word (EmpathyChat *chat,
3040 const gchar *new_word)
3042 GtkTextBuffer *buffer;
3044 g_return_if_fail (chat != NULL);
3045 g_return_if_fail (new_word != NULL);
3047 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3049 gtk_text_buffer_delete (buffer, start, end);
3050 gtk_text_buffer_insert (buffer, start,
3056 empathy_chat_is_room (EmpathyChat *chat)
3058 EmpathyChatPriv *priv = GET_PRIV (chat);
3060 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3062 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3066 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3068 EmpathyChatPriv *priv = GET_PRIV (self);
3070 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3072 return priv->unread_messages;
3075 /* called when the messages have been read by user */
3077 empathy_chat_messages_read (EmpathyChat *self)
3079 EmpathyChatPriv *priv = GET_PRIV (self);
3081 g_return_if_fail (EMPATHY_IS_CHAT (self));
3083 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3084 * definition. If we are still retrieving the backlogs, do not ACK */
3085 if (priv->retrieving_backlogs)
3088 if (priv->tp_chat != NULL ) {
3089 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3091 priv->unread_messages = 0;