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-2010 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 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-contact-dialogs.h"
51 #include "empathy-contact-list-store.h"
52 #include "empathy-contact-list-view.h"
53 #include "empathy-contact-menu.h"
54 #include "empathy-gtk-marshal.h"
55 #include "empathy-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *remote_contact;
77 gboolean show_contacts;
79 GSettings *gsettings_chat;
80 GSettings *gsettings_ui;
82 TplLogManager *log_manager;
83 TpAccountManager *account_manager;
85 GList *input_history_current;
87 GCompletion *completion;
88 guint composing_stop_timeout_id;
89 guint block_events_timeout_id;
90 TpHandleType handle_type;
92 gboolean has_input_vscroll;
94 /* TRUE if spell checking is enabled, FALSE otherwise.
95 * This is to keep track of the last state of spell checking
97 gboolean spell_checking_enabled;
99 /* These store the signal handler ids for the enclosed text entry. */
100 gulong insert_text_id;
101 gulong delete_range_id;
102 gulong notify_cursor_position_id;
104 /* Source func ID for update_misspelled_words () */
105 guint update_misspelled_words_id;
106 /* Source func ID for save_paned_pos_timeout () */
107 guint save_paned_pos_id;
111 GtkWidget *vbox_left;
112 GtkWidget *scrolled_window_chat;
113 GtkWidget *scrolled_window_input;
114 GtkWidget *scrolled_window_contacts;
115 GtkWidget *hbox_topic;
116 GtkWidget *expander_topic;
117 GtkWidget *label_topic;
118 GtkWidget *contact_list_view;
119 GtkWidget *info_bar_vbox;
120 GtkWidget *search_bar;
122 guint unread_messages;
123 /* TRUE if the pending messages can be displayed. This is to avoid to show
124 * pending messages *before* messages from logs. (#603980) */
125 gboolean can_show_pending;
127 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
128 * be differently handled since it introduces another race condition, which
129 * is really hard to occur, but still possible.
131 * With the current workaround (which has the race above), we need to be
132 * sure to ACK any pending messages only when the retrieval of backlogs is
133 * finished, that's why using retrieving_backlogs flag.
134 * empathy_chat_messages_read () will check this variable and not ACK
135 * anything when TRUE. It will be set TRUE at chat_constructed () and set
136 * back to FALSE when the backlog has been retrieved and the pending
137 * messages actually showed to the user.
139 * Race condition introduced with this workaround:
140 * Scenario: a message is pending, the user is notified and selects the tab.
141 * the tab with a pending message is focused before the messages are properly
142 * shown (since the preparation of the window is slower AND async WRT the
143 * tab showing), which means the user won't see any new messages (rare but
144 * possible), if he/she will change tab focus before the messages are
145 * properly shown, the tab will be set as 'seen' and the user won't be
146 * notified again about the already notified pending messages when the
147 * messages in tab will be properly shown */
148 gboolean retrieving_backlogs;
149 gboolean sms_channel;
153 gchar *text; /* Original message that was specified
154 * upon entry creation. */
155 gchar *modified_text; /* Message that was modified by user.
156 * When no modifications were made, it is NULL */
162 PART_COMMAND_ENTERED,
176 PROP_N_MESSAGES_SENDING,
177 PROP_NB_UNREAD_MESSAGES,
180 static guint signals[LAST_SIGNAL] = { 0 };
182 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
184 static gboolean update_misspelled_words (gpointer data);
187 chat_get_property (GObject *object,
192 EmpathyChat *chat = EMPATHY_CHAT (object);
193 EmpathyChatPriv *priv = GET_PRIV (object);
197 g_value_set_object (value, priv->tp_chat);
200 g_value_set_object (value, priv->account);
203 g_value_take_string (value, empathy_chat_dup_name (chat));
206 g_value_set_string (value, priv->id);
209 g_value_set_string (value, priv->subject);
211 case PROP_REMOTE_CONTACT:
212 g_value_set_object (value, priv->remote_contact);
214 case PROP_SHOW_CONTACTS:
215 g_value_set_boolean (value, priv->show_contacts);
217 case PROP_SMS_CHANNEL:
218 g_value_set_boolean (value, priv->sms_channel);
220 case PROP_N_MESSAGES_SENDING:
221 g_value_set_uint (value,
222 empathy_chat_get_n_messages_sending (chat));
224 case PROP_NB_UNREAD_MESSAGES:
225 g_value_set_uint (value,
226 empathy_chat_get_nb_unread_messages (chat));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
235 chat_set_property (GObject *object,
240 EmpathyChat *chat = EMPATHY_CHAT (object);
244 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
246 case PROP_SHOW_CONTACTS:
247 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
256 account_reconnected (EmpathyChat *chat,
259 EmpathyChatPriv *priv = GET_PRIV (chat);
261 DEBUG ("Account reconnected, request a new Text channel");
263 /* FIXME: Ideally we should ask to handle ourself the channel so we can
264 * report the error if any but this is blocked by
265 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
266 switch (priv->handle_type) {
267 case TP_HANDLE_TYPE_CONTACT:
268 if (priv->sms_channel)
269 empathy_sms_contact_id (
271 TP_USER_ACTION_TIME_NOT_USER_ACTION);
273 empathy_chat_with_contact_id (
275 TP_USER_ACTION_TIME_NOT_USER_ACTION);
277 case TP_HANDLE_TYPE_ROOM:
278 empathy_join_muc (account, priv->id,
279 TP_USER_ACTION_TIME_NOT_USER_ACTION);
281 case TP_HANDLE_TYPE_NONE:
282 case TP_HANDLE_TYPE_LIST:
283 case TP_HANDLE_TYPE_GROUP:
285 g_assert_not_reached ();
289 g_object_unref (chat);
293 chat_new_connection_cb (TpAccount *account,
297 gchar *dbus_error_name,
301 EmpathyChatPriv *priv = GET_PRIV (chat);
303 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
306 if (priv->tp_chat != NULL || account != priv->account ||
307 priv->handle_type == TP_HANDLE_TYPE_NONE ||
308 EMP_STR_EMPTY (priv->id))
313 account_reconnected (chat, account);
317 chat_composing_remove_timeout (EmpathyChat *chat)
319 EmpathyChatPriv *priv;
321 priv = GET_PRIV (chat);
323 if (priv->composing_stop_timeout_id) {
324 g_source_remove (priv->composing_stop_timeout_id);
325 priv->composing_stop_timeout_id = 0;
330 set_chate_state_cb (GObject *source,
331 GAsyncResult *result,
334 GError *error = NULL;
336 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
338 DEBUG ("Failed to set chat state: %s", error->message);
339 g_error_free (error);
344 set_chat_state (EmpathyChat *self,
345 TpChannelChatState state)
347 EmpathyChatPriv *priv = GET_PRIV (self);
349 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
350 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
353 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
354 set_chate_state_cb, self);
358 chat_composing_stop_timeout_cb (EmpathyChat *chat)
360 EmpathyChatPriv *priv;
362 priv = GET_PRIV (chat);
364 priv->composing_stop_timeout_id = 0;
365 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
371 chat_composing_start (EmpathyChat *chat)
373 EmpathyChatPriv *priv;
375 priv = GET_PRIV (chat);
377 if (priv->composing_stop_timeout_id) {
378 /* Just restart the timeout */
379 chat_composing_remove_timeout (chat);
381 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
384 priv->composing_stop_timeout_id = g_timeout_add_seconds (
385 COMPOSING_STOP_TIMEOUT,
386 (GSourceFunc) chat_composing_stop_timeout_cb,
391 chat_composing_stop (EmpathyChat *chat)
393 EmpathyChatPriv *priv;
395 priv = GET_PRIV (chat);
397 chat_composing_remove_timeout (chat);
398 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
402 chat_input_history_entry_cmp (InputHistoryEntry *entry,
405 if (!tp_strdiff (entry->text, text)) {
406 if (entry->modified_text != NULL) {
407 /* Modified entry and single string cannot be equal. */
415 static InputHistoryEntry *
416 chat_input_history_entry_new_with_text (const gchar *text)
418 InputHistoryEntry *entry;
419 entry = g_slice_new0 (InputHistoryEntry);
420 entry->text = g_strdup (text);
426 chat_input_history_entry_free (InputHistoryEntry *entry)
428 g_free (entry->text);
429 g_free (entry->modified_text);
430 g_slice_free (InputHistoryEntry, entry);
434 chat_input_history_entry_revert (InputHistoryEntry *entry)
436 g_free (entry->modified_text);
437 entry->modified_text = NULL;
441 chat_input_history_entry_update_text (InputHistoryEntry *entry,
446 if (!tp_strdiff (text, entry->text)) {
447 g_free (entry->modified_text);
448 entry->modified_text = NULL;
452 old = entry->modified_text;
453 entry->modified_text = g_strdup (text);
458 chat_input_history_entry_get_text (InputHistoryEntry *entry)
464 if (entry->modified_text != NULL) {
465 return entry->modified_text;
471 chat_input_history_remove_item (GList *list,
474 list = g_list_remove_link (list, item);
475 chat_input_history_entry_free (item->data);
476 g_list_free_1 (item);
481 chat_input_history_revert (EmpathyChat *chat)
483 EmpathyChatPriv *priv;
487 InputHistoryEntry *entry;
489 priv = GET_PRIV (chat);
490 list = priv->input_history;
493 DEBUG ("No input history");
497 /* Delete temporary entry */
498 if (priv->input_history_current != NULL) {
500 list = chat_input_history_remove_item (list, item1);
501 if (priv->input_history_current == item1) {
502 /* Removed temporary entry was current entry */
503 priv->input_history = list;
504 priv->input_history_current = NULL;
509 /* There is no entry to revert */
513 /* Restore the current history entry to original value */
514 item1 = priv->input_history_current;
516 chat_input_history_entry_revert (entry);
518 /* Remove restored entry if there is other occurance before this entry */
519 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
520 (GCompareFunc) chat_input_history_entry_cmp);
521 if (item2 != item1) {
522 list = chat_input_history_remove_item (list, item1);
525 /* Remove other occurance of the restored entry */
526 item2 = g_list_find_custom (item1->next,
527 chat_input_history_entry_get_text (entry),
528 (GCompareFunc) chat_input_history_entry_cmp);
530 list = chat_input_history_remove_item (list, item2);
534 priv->input_history_current = NULL;
535 priv->input_history = list;
539 chat_input_history_add (EmpathyChat *chat,
543 EmpathyChatPriv *priv;
546 InputHistoryEntry *entry;
548 priv = GET_PRIV (chat);
550 list = priv->input_history;
552 /* Remove any other occurances of this entry, if not temporary */
554 while ((item = g_list_find_custom (list, str,
555 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
556 list = chat_input_history_remove_item (list, item);
559 /* Trim the list to the last 10 items */
560 while (g_list_length (list) > 10) {
561 item = g_list_last (list);
563 list = chat_input_history_remove_item (list, item);
571 entry = chat_input_history_entry_new_with_text (str);
572 list = g_list_prepend (list, entry);
574 /* Set the list and the current item pointer */
575 priv->input_history = list;
577 priv->input_history_current = list;
580 priv->input_history_current = NULL;
585 chat_input_history_get_next (EmpathyChat *chat)
587 EmpathyChatPriv *priv;
591 priv = GET_PRIV (chat);
593 if (priv->input_history == NULL) {
594 DEBUG ("No input history, next entry is NULL");
597 g_assert (priv->input_history_current != NULL);
599 if ((item = g_list_next (priv->input_history_current)) == NULL)
601 item = priv->input_history_current;
604 msg = chat_input_history_entry_get_text (item->data);
606 DEBUG ("Returning next entry: '%s'", msg);
608 priv->input_history_current = item;
614 chat_input_history_get_prev (EmpathyChat *chat)
616 EmpathyChatPriv *priv;
620 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
622 priv = GET_PRIV (chat);
624 if (priv->input_history == NULL) {
625 DEBUG ("No input history, previous entry is NULL");
629 if (priv->input_history_current == NULL)
633 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
635 item = priv->input_history_current;
638 msg = chat_input_history_entry_get_text (item->data);
640 DEBUG ("Returning previous entry: '%s'", msg);
642 priv->input_history_current = item;
648 chat_input_history_update (EmpathyChat *chat,
649 GtkTextBuffer *buffer)
651 EmpathyChatPriv *priv;
652 GtkTextIter start, end;
654 InputHistoryEntry *entry;
656 priv = GET_PRIV (chat);
658 gtk_text_buffer_get_bounds (buffer, &start, &end);
659 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
661 if (priv->input_history_current == NULL) {
662 /* Add the current text temporarily to the history */
663 chat_input_history_add (chat, text, TRUE);
668 /* Save the changes in the history */
669 entry = priv->input_history_current->data;
670 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
671 chat_input_history_entry_update_text (entry, text);
680 } ChatCommandMsgData;
683 chat_command_msg_cb (GObject *source,
684 GAsyncResult *result,
687 ChatCommandMsgData *data = user_data;
688 GError *error = NULL;
691 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
692 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
694 if (channel == NULL) {
695 DEBUG ("Failed to get channel: %s", error->message);
696 g_error_free (error);
698 empathy_chat_view_append_event (data->chat->view,
699 _("Failed to open private chat"));
703 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
704 TpTextChannel *text = (TpTextChannel *) channel;
707 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
710 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
712 g_object_unref (msg);
715 g_object_unref (channel);
718 g_free (data->message);
719 g_slice_free (ChatCommandMsgData, data);
723 nick_command_supported (EmpathyChat *chat)
725 EmpathyChatPriv * priv = GET_PRIV (chat);
726 TpConnection *connection;
728 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
729 return tp_proxy_has_interface_by_id (connection,
730 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
734 part_command_supported (EmpathyChat *chat)
736 EmpathyChatPriv * priv = GET_PRIV (chat);
738 return tp_proxy_has_interface_by_id (priv->tp_chat,
739 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
743 chat_command_clear (EmpathyChat *chat,
746 empathy_chat_view_clear (chat->view);
750 chat_command_topic (EmpathyChat *chat,
753 EmpathyChatPriv *priv = GET_PRIV (chat);
754 EmpathyTpChatProperty *property;
755 GValue value = {0, };
757 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
758 if (property == NULL) {
759 empathy_chat_view_append_event (chat->view,
760 _("Topic not supported on this conversation"));
764 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
765 empathy_chat_view_append_event (chat->view,
766 _("You are not allowed to change the topic"));
770 g_value_init (&value, G_TYPE_STRING);
771 g_value_set_string (&value, strv[1]);
772 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
773 g_value_unset (&value);
777 empathy_chat_join_muc (EmpathyChat *chat,
780 EmpathyChatPriv *priv = GET_PRIV (chat);
782 empathy_join_muc (priv->account, room,
783 empathy_get_current_action_time ());
787 chat_command_join (EmpathyChat *chat,
792 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
794 /* FIXME: Ideally we should ask to handle ourself the channel so we can
795 * report the error if any but this is blocked by
796 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
797 while (rooms[i] != NULL) {
798 /* ignore empty strings */
799 if (!EMP_STR_EMPTY (rooms[i])) {
800 empathy_chat_join_muc (chat, rooms[i]);
808 chat_command_part (EmpathyChat *chat,
811 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
815 chat_command_msg_internal (EmpathyChat *chat,
816 const gchar *contact_id,
817 const gchar *message)
819 EmpathyChatPriv *priv = GET_PRIV (chat);
820 ChatCommandMsgData *data;
821 TpAccountChannelRequest *req;
824 request = tp_asv_new (
825 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
826 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
827 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
830 req = tp_account_channel_request_new (priv->account, request,
831 empathy_get_current_action_time ());
833 /* FIXME: We should probably search in members alias. But this
834 * is enough for IRC */
835 data = g_slice_new (ChatCommandMsgData);
837 data->message = g_strdup (message);
839 tp_account_channel_request_ensure_and_observe_channel_async (req,
840 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
842 g_object_unref (req);
843 g_hash_table_unref (request);
847 chat_command_query (EmpathyChat *chat,
850 /* If <message> part is not defined,
851 * strv[2] will be the terminal NULL */
852 chat_command_msg_internal (chat, strv[1], strv[2]);
856 chat_command_msg (EmpathyChat *chat,
859 chat_command_msg_internal (chat, strv[1], strv[2]);
863 callback_for_request_rename (TpProxy *proxy,
866 GObject *weak_object)
869 DEBUG ("Call to RequestRename method failed: %s",error->message);
874 chat_command_nick (EmpathyChat *chat,
877 EmpathyChatPriv *priv = GET_PRIV (chat);
880 proxy = TP_PROXY (tp_account_get_connection (priv->account));
882 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
883 strv[1], callback_for_request_rename, NULL, NULL, NULL);
887 chat_command_me (EmpathyChat *chat,
890 EmpathyChatPriv *priv = GET_PRIV (chat);
892 TpTextChannel *channel;
894 channel = (TpTextChannel *) (priv->tp_chat);
896 if (!tp_text_channel_supports_message_type (channel,
897 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
898 /* Action message are not supported, 'simulate' the action */
899 EmpathyContact *self_contact;
902 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
903 /* The TpChat can't be ready if it doesn't have the self contact */
904 g_assert (self_contact != NULL);
906 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
908 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
913 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
917 empathy_tp_chat_send (priv->tp_chat, message);
918 g_object_unref (message);
922 chat_command_say (EmpathyChat *chat,
925 EmpathyChatPriv *priv = GET_PRIV (chat);
928 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
930 empathy_tp_chat_send (priv->tp_chat, message);
931 g_object_unref (message);
935 whois_got_contact_cb (TpConnection *connection,
937 TpContact * const *contacts,
938 const gchar * const *requested_ids,
939 GHashTable *failed_id_errors,
942 GObject *weak_object)
944 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
946 g_return_if_fail (n_contacts <= 1);
948 if (n_contacts == 0) {
950 gpointer key = NULL, value = NULL;
954 /* tp-glib guarantees that the contacts you requested will be
955 * in failed_id_errors regardless of whether the individual
956 * contact was invalid or the whole operation failed.
958 g_hash_table_iter_init (&iter, failed_id_errors);
959 g_hash_table_iter_next (&iter, &key, &value);
963 DEBUG ("Error getting TpContact for '%s': %s %u %s",
964 id, g_quark_to_string (id_error->domain),
965 id_error->code, id_error->message);
968 /* The specific ID failed. */
969 gchar *event = g_strdup_printf (
970 _("“%s” is not a valid contact ID"), id);
971 empathy_chat_view_append_event (chat->view, event);
974 /* Otherwise we're disconnected or something; so the window
975 * will already say ‘Disconnected’, so let's not show anything.
978 EmpathyContact *empathy_contact;
981 g_return_if_fail (contacts[0] != NULL);
982 empathy_contact = empathy_contact_dup_from_tp_contact (
985 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
986 /* If we're alive and this command is running, we'd better be
988 g_return_if_fail (window != NULL);
989 g_return_if_fail (gtk_widget_is_toplevel (window));
990 empathy_contact_information_dialog_show (empathy_contact,
991 GTK_WINDOW (window));
992 g_object_unref (empathy_contact);
997 chat_command_whois (EmpathyChat *chat,
1000 EmpathyChatPriv *priv = GET_PRIV (chat);
1003 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1004 tp_connection_get_contacts_by_id (conn,
1005 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1006 * entered by the user (including spaces, if any). */
1007 1, (const gchar * const *) strv + 1,
1009 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1012 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1014 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1017 const gchar *prefix;
1020 ChatCommandFunc func;
1021 gboolean (*is_supported)(EmpathyChat *chat);
1025 static ChatCommandItem commands[] = {
1026 {"clear", 1, 1, chat_command_clear, NULL,
1027 N_("/clear: clear all messages from the current conversation")},
1029 {"topic", 2, 2, chat_command_topic, NULL,
1030 N_("/topic <topic>: set the topic of the current conversation")},
1032 {"join", 2, 2, chat_command_join, NULL,
1033 N_("/join <chat room ID>: join a new chat room")},
1035 {"j", 2, 2, chat_command_join, NULL,
1036 N_("/j <chat room ID>: join a new chat room")},
1038 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1039 {"part", 1, 3, chat_command_part, part_command_supported,
1040 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1041 "by default the current one")},
1043 {"query", 2, 3, chat_command_query, NULL,
1044 N_("/query <contact ID> [<message>]: open a private chat")},
1046 {"msg", 3, 3, chat_command_msg, NULL,
1047 N_("/msg <contact ID> <message>: open a private chat")},
1049 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1050 N_("/nick <nickname>: change your nickname on the current server")},
1052 {"me", 2, 2, chat_command_me, NULL,
1053 N_("/me <message>: send an ACTION message to the current conversation")},
1055 {"say", 2, 2, chat_command_say, NULL,
1056 N_("/say <message>: send <message> to the current conversation. "
1057 "This is used to send a message starting with a '/'. For example: "
1058 "\"/say /join is used to join a new chat room\"")},
1060 {"whois", 2, 2, chat_command_whois, NULL,
1061 N_("/whois <contact ID>: display information about a contact")},
1063 {"help", 1, 2, chat_command_help, NULL,
1064 N_("/help [<command>]: show all supported commands. "
1065 "If <command> is defined, show its usage.")},
1069 chat_command_show_help (EmpathyChat *chat,
1070 ChatCommandItem *item)
1074 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1075 empathy_chat_view_append_event (chat->view, str);
1080 chat_command_help (EmpathyChat *chat,
1085 /* If <command> part is not defined,
1086 * strv[1] will be the terminal NULL */
1087 if (strv[1] == NULL) {
1088 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1089 if (commands[i].is_supported != NULL) {
1090 if (!commands[i].is_supported (chat)) {
1094 empathy_chat_view_append_event (chat->view,
1095 _(commands[i].help));
1100 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1101 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1102 if (commands[i].is_supported != NULL) {
1103 if (!commands[i].is_supported (chat)) {
1107 chat_command_show_help (chat, &commands[i]);
1112 empathy_chat_view_append_event (chat->view,
1113 _("Unknown command"));
1117 chat_command_parse (const gchar *text, guint max_parts)
1122 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1124 array = g_ptr_array_sized_new (max_parts + 1);
1125 while (max_parts > 1) {
1128 /* Skip white spaces */
1129 while (g_ascii_isspace (*text)) {
1133 /* Search the end of this part, until first space. */
1134 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1140 item = g_strndup (text, end - text);
1141 g_ptr_array_add (array, item);
1142 DEBUG ("\tITEM: \"%s\"", item);
1148 /* Append last part if not empty */
1149 item = g_strstrip (g_strdup (text));
1150 if (!EMP_STR_EMPTY (item)) {
1151 g_ptr_array_add (array, item);
1152 DEBUG ("\tITEM: \"%s\"", item);
1157 /* Make the array NULL-terminated */
1158 g_ptr_array_add (array, NULL);
1160 return (GStrv) g_ptr_array_free (array, FALSE);
1164 has_prefix_case (const gchar *s,
1165 const gchar *prefix)
1167 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1171 chat_send (EmpathyChat *chat,
1174 EmpathyChatPriv *priv;
1178 if (EMP_STR_EMPTY (msg)) {
1182 priv = GET_PRIV (chat);
1184 chat_input_history_add (chat, msg, FALSE);
1186 if (msg[0] == '/') {
1187 gboolean second_slash = FALSE;
1188 const gchar *iter = msg + 1;
1190 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1195 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1198 c = *(msg + 1 + strlen (commands[i].prefix));
1199 if (c != '\0' && !g_ascii_isspace (c)) {
1202 if (commands[i].is_supported != NULL) {
1203 if (!commands[i].is_supported (chat)) {
1208 /* We can't use g_strsplit here because it does
1209 * not deal correctly if we have more than one space
1211 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1213 strv_len = g_strv_length (strv);
1214 if (strv_len < commands[i].min_parts ||
1215 strv_len > commands[i].max_parts) {
1216 chat_command_show_help (chat, &commands[i]);
1221 commands[i].func (chat, strv);
1226 /* Also allow messages with two slashes before the
1227 * first space, so it is possible to send a /unix/path.
1228 * This heuristic is kind of crap. */
1229 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1231 second_slash = TRUE;
1237 if (!second_slash) {
1238 empathy_chat_view_append_event (chat->view,
1239 _("Unknown command; see /help for the available"
1245 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1247 empathy_tp_chat_send (priv->tp_chat, message);
1248 g_object_unref (message);
1252 chat_input_text_view_send (EmpathyChat *chat)
1254 GtkTextBuffer *buffer;
1255 GtkTextIter start, end;
1258 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1260 gtk_text_buffer_get_bounds (buffer, &start, &end);
1261 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1263 /* clear the input field */
1264 gtk_text_buffer_set_text (buffer, "", -1);
1265 /* delete input history modifications */
1266 chat_input_history_revert (chat);
1268 chat_send (chat, msg);
1273 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1274 EmpathyContact *contact,
1275 TpChannelChatState state,
1278 EmpathyChatPriv *priv;
1280 gboolean was_composing;
1282 priv = GET_PRIV (chat);
1284 if (empathy_contact_is_user (contact)) {
1285 /* We don't care about our own chat state */
1289 was_composing = (priv->compositors != NULL);
1291 /* Find the contact in the list. After that l is the list elem or NULL */
1292 for (l = priv->compositors; l; l = l->next) {
1293 if (contact == l->data) {
1299 case TP_CHANNEL_CHAT_STATE_GONE:
1300 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1301 case TP_CHANNEL_CHAT_STATE_PAUSED:
1302 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1303 /* Contact is not composing */
1305 priv->compositors = g_list_remove_link (priv->compositors, l);
1306 g_object_unref (l->data);
1310 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1311 /* Contact is composing */
1313 priv->compositors = g_list_prepend (priv->compositors,
1314 g_object_ref (contact));
1318 g_assert_not_reached ();
1321 DEBUG ("Was composing: %s now composing: %s",
1322 was_composing ? "yes" : "no",
1323 priv->compositors ? "yes" : "no");
1325 if ((was_composing && !priv->compositors) ||
1326 (!was_composing && priv->compositors)) {
1327 /* Composing state changed */
1328 g_signal_emit (chat, signals[COMPOSING], 0,
1329 priv->compositors != NULL);
1334 chat_message_received (EmpathyChat *chat,
1335 EmpathyMessage *message,
1338 EmpathyChatPriv *priv = GET_PRIV (chat);
1339 EmpathyContact *sender;
1341 sender = empathy_message_get_sender (message);
1343 DEBUG ("Appending new message from %s (%d)",
1344 empathy_contact_get_alias (sender),
1345 empathy_contact_get_handle (sender));
1347 empathy_chat_view_append_message (chat->view, message);
1349 /* We received a message so the contact is no longer composing */
1350 chat_state_changed_cb (priv->tp_chat, sender,
1351 TP_CHANNEL_CHAT_STATE_ACTIVE,
1354 if (empathy_message_is_incoming (message)) {
1355 priv->unread_messages++;
1356 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1359 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1363 chat_message_received_cb (EmpathyTpChat *tp_chat,
1364 EmpathyMessage *message,
1367 chat_message_received (chat, message, FALSE);
1371 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1372 EmpathyMessage *message,
1375 EmpathyChatPriv *priv = GET_PRIV (chat);
1377 empathy_chat_view_message_acknowledged (chat->view,
1380 priv->unread_messages--;
1381 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1385 chat_send_error_cb (EmpathyTpChat *tp_chat,
1386 const gchar *message_body,
1387 TpChannelTextSendError error_code,
1388 const gchar *dbus_error,
1391 const gchar *error = NULL;
1394 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1395 /* translators: error used when user doesn't have enough credit on his
1396 * account to send the message. */
1397 error = _("insufficient balance to send message");
1398 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1399 error = _("not capable");
1402 if (error == NULL) {
1403 /* if we didn't find a dbus-error, try the old error */
1404 switch (error_code) {
1405 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1406 error = _("offline");
1408 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1409 error = _("invalid contact");
1411 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1412 error = _("permission denied");
1414 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1415 error = _("too long message");
1417 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1418 error = _("not implemented");
1420 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1422 error = _("unknown");
1427 if (message_body != NULL) {
1428 str = g_strdup_printf (_("Error sending message '%s': %s"),
1429 message_body, error);
1432 str = g_strdup_printf (_("Error sending message: %s"), error);
1435 empathy_chat_view_append_event (chat->view, str);
1440 chat_topic_label_size_allocate_cb (GtkLabel *label,
1441 GtkAllocation *allocation,
1444 EmpathyChatPriv *priv = GET_PRIV (chat);
1446 if (!gtk_label_get_line_wrap (label)) {
1447 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1448 gtk_widget_show (priv->expander_topic);
1450 gtk_widget_hide (priv->expander_topic);
1457 chat_topic_expander_activate_cb (GtkExpander *expander,
1458 GParamSpec *param_spec,
1461 EmpathyChatPriv *priv = GET_PRIV (chat);
1463 if (gtk_expander_get_expanded (expander)) {
1464 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1465 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1467 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1468 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1473 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1478 EmpathyChatPriv *priv = GET_PRIV (chat);
1480 if (!tp_strdiff (name, "subject")) {
1481 g_free (priv->subject);
1482 priv->subject = g_value_dup_string (value);
1483 g_object_notify (G_OBJECT (chat), "subject");
1485 if (EMP_STR_EMPTY (priv->subject)) {
1486 gtk_widget_hide (priv->hbox_topic);
1488 gchar *markup_topic;
1491 markup_topic = empathy_add_link_markup (priv->subject);
1492 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1493 _("Topic:"), markup_topic);
1495 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1496 g_free (markup_text);
1497 g_free (markup_topic);
1499 gtk_widget_show (priv->hbox_topic);
1501 if (priv->block_events_timeout_id == 0) {
1504 if (!EMP_STR_EMPTY (priv->subject)) {
1505 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1507 str = g_strdup (_("No topic defined"));
1509 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1513 else if (!tp_strdiff (name, "name")) {
1514 g_free (priv->name);
1515 priv->name = g_value_dup_string (value);
1516 g_object_notify (G_OBJECT (chat), "name");
1521 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1525 GtkTextIter word_start = *iter;
1526 GtkTextIter word_end = *iter;
1529 if (gtk_text_iter_inside_word (&word_end) &&
1530 !gtk_text_iter_ends_word (&word_end)) {
1531 gtk_text_iter_forward_word_end (&word_end);
1536 if (gtk_text_iter_get_char (&tmp) == '\'') {
1537 gtk_text_iter_forward_char (&tmp);
1539 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1540 gtk_text_iter_forward_word_end (&word_end);
1545 if (gtk_text_iter_inside_word (&word_start) ||
1546 gtk_text_iter_ends_word (&word_start)) {
1547 if (!gtk_text_iter_starts_word (&word_start) ||
1548 gtk_text_iter_equal (&word_start, &word_end)) {
1549 gtk_text_iter_backward_word_start (&word_start);
1553 gtk_text_iter_backward_char (&tmp);
1555 if (gtk_text_iter_get_char (&tmp) == '\'') {
1556 gtk_text_iter_backward_char (&tmp);
1558 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1559 gtk_text_iter_backward_word_start (&word_start);
1564 *start = word_start;
1570 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1571 GtkTextIter *location,
1576 GtkTextIter iter, pos;
1578 /* Remove all misspelled tags in the inserted text.
1579 * This happens when text is inserted within a misspelled word. */
1580 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1581 gtk_text_iter_get_offset (location) - len);
1582 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1585 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1588 GtkTextIter start, end;
1591 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1594 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1596 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1597 gtk_text_iter_equal (&pos, &end) ||
1598 empathy_spell_check (str)) {
1599 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1601 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1606 } while (gtk_text_iter_forward_word_end (&iter) &&
1607 gtk_text_iter_compare (&iter, location) <= 0);
1611 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1616 GtkTextIter word_start, word_end;
1618 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1619 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1620 &word_start, &word_end);
1625 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1628 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1629 chat_composing_stop (chat);
1631 chat_composing_start (chat);
1636 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1641 GtkTextIter prev_pos;
1642 GtkTextIter word_start;
1643 GtkTextIter word_end;
1647 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1649 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1650 gtk_text_buffer_get_insert (buffer));
1651 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1653 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1656 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1657 !gtk_text_iter_equal (&pos, &word_end)) {
1658 str = gtk_text_buffer_get_text (buffer,
1659 &word_start, &word_end, FALSE);
1661 if (!empathy_spell_check (str)) {
1662 gtk_text_buffer_apply_tag_by_name (buffer,
1663 "misspelled", &word_start, &word_end);
1665 gtk_text_buffer_remove_tag_by_name (buffer,
1666 "misspelled", &word_start, &word_end);
1673 gtk_text_buffer_move_mark (buffer, mark, &pos);
1677 empathy_isspace_cb (gunichar c,
1680 return g_unichar_isspace (c);
1684 chat_input_key_press_event_cb (GtkWidget *widget,
1688 EmpathyChatPriv *priv;
1691 GtkWidget *text_view_sw;
1693 priv = GET_PRIV (chat);
1695 /* Catch ctrl+up/down so we can traverse messages we sent */
1696 if ((event->state & GDK_CONTROL_MASK) &&
1697 (event->keyval == GDK_KEY_Up ||
1698 event->keyval == GDK_KEY_Down)) {
1699 GtkTextBuffer *buffer;
1702 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1703 chat_input_history_update (chat, buffer);
1705 if (event->keyval == GDK_KEY_Up) {
1706 str = chat_input_history_get_next (chat);
1708 str = chat_input_history_get_prev (chat);
1711 g_signal_handlers_block_by_func (buffer,
1712 chat_input_text_buffer_changed_cb,
1714 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1715 g_signal_handlers_unblock_by_func (buffer,
1716 chat_input_text_buffer_changed_cb,
1722 /* Catch enter but not ctrl/shift-enter */
1723 if (IS_ENTER (event->keyval) &&
1724 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1727 /* This is to make sure that kinput2 gets the enter. And if
1728 * it's handled there we shouldn't send on it. This is because
1729 * kinput2 uses Enter to commit letters. See:
1730 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1733 view = GTK_TEXT_VIEW (chat->input_text_view);
1734 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1735 gtk_text_view_reset_im_context (view);
1739 chat_input_text_view_send (chat);
1743 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1745 if (IS_ENTER (event->keyval) &&
1746 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1747 /* Newline for shift/control-enter. */
1750 if (!(event->state & GDK_CONTROL_MASK) &&
1751 event->keyval == GDK_KEY_Page_Up) {
1752 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1753 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1756 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1757 event->keyval == GDK_KEY_Page_Down) {
1758 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1759 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1760 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1761 gtk_adjustment_set_value (adj, val);
1764 if (event->keyval == GDK_KEY_Escape) {
1765 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1767 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1768 event->keyval == GDK_KEY_Tab) {
1769 GtkTextBuffer *buffer;
1770 GtkTextIter start, current;
1771 gchar *nick, *completed;
1772 GList *list, *completed_list;
1773 gboolean is_start_of_buffer;
1775 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1776 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1778 /* Get the start of the nick to complete. */
1779 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1780 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1781 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1783 is_start_of_buffer = gtk_text_iter_is_start (&start);
1785 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1786 g_completion_add_items (priv->completion, list);
1788 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1789 completed_list = g_completion_complete (priv->completion,
1798 GString *message = NULL;
1801 gtk_text_buffer_delete (buffer, &start, ¤t);
1803 len = g_list_length (completed_list);
1806 /* If we only have one hit, use that text
1807 * instead of the text in completed since the
1808 * completed text will use the typed string
1809 * which might be cased all wrong.
1812 text = empathy_contact_get_alias (completed_list->data);
1816 /* Print all hits to the scrollback view, so the
1817 * user knows what possibilities he has.
1820 message = g_string_new ("");
1821 for (l = completed_list; l != NULL; l = l->next) {
1822 g_string_append (message, empathy_contact_get_alias (l->data));
1823 g_string_append (message, " - ");
1825 empathy_chat_view_append_event (chat->view, message->str);
1826 g_string_free (message, TRUE);
1829 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1831 if (len == 1 && is_start_of_buffer) {
1832 gchar *complete_char;
1834 complete_char = g_settings_get_string (
1835 priv->gsettings_chat,
1836 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1838 if (complete_char != NULL) {
1839 gtk_text_buffer_insert_at_cursor (buffer,
1841 strlen (complete_char));
1842 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1843 g_free (complete_char);
1850 g_completion_clear_items (priv->completion);
1852 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1862 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1866 gtk_widget_grab_focus (chat->input_text_view);
1872 chat_input_realize_cb (GtkWidget *widget,
1875 DEBUG ("Setting focus to the input text view");
1876 if (gtk_widget_is_sensitive (widget)) {
1877 gtk_widget_grab_focus (widget);
1882 chat_input_has_focus_notify_cb (GtkWidget *widget,
1886 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1890 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1891 EmpathySmiley *smiley,
1894 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1895 GtkTextBuffer *buffer;
1898 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1900 gtk_text_buffer_get_end_iter (buffer, &iter);
1901 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1903 gtk_text_buffer_get_end_iter (buffer, &iter);
1904 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1915 static EmpathyChatSpell *
1916 chat_spell_new (EmpathyChat *chat,
1921 EmpathyChatSpell *chat_spell;
1923 chat_spell = g_slice_new0 (EmpathyChatSpell);
1925 chat_spell->chat = g_object_ref (chat);
1926 chat_spell->word = g_strdup (word);
1927 chat_spell->start = start;
1928 chat_spell->end = end;
1934 chat_spell_free (EmpathyChatSpell *chat_spell)
1936 g_object_unref (chat_spell->chat);
1937 g_free (chat_spell->word);
1938 g_slice_free (EmpathyChatSpell, chat_spell);
1942 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1943 EmpathyChatSpell *chat_spell)
1945 empathy_chat_correct_word (chat_spell->chat,
1946 &(chat_spell->start),
1948 gtk_menu_item_get_label (menu_item));
1953 chat_spelling_build_suggestions_menu (const gchar *code,
1954 EmpathyChatSpell *chat_spell)
1956 GList *suggestions, *l;
1957 GtkWidget *menu, *menu_item;
1959 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1960 if (suggestions == NULL)
1963 menu = gtk_menu_new ();
1964 for (l = suggestions; l; l = l->next) {
1965 menu_item = gtk_menu_item_new_with_label (l->data);
1966 g_signal_connect (G_OBJECT (menu_item), "activate",
1967 G_CALLBACK (chat_spelling_menu_activate_cb),
1969 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1971 empathy_spell_free_suggestions (suggestions);
1973 gtk_widget_show_all (menu);
1979 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1981 GtkWidget *menu, *submenu, *item;
1984 codes = empathy_spell_get_enabled_language_codes ();
1985 g_assert (codes != NULL);
1987 if (g_list_length (codes) > 1) {
1988 menu = gtk_menu_new ();
1990 for (l = codes; l; l = l->next) {
1991 const gchar *code, *name;
1994 name = empathy_spell_get_language_name (code);
1998 item = gtk_image_menu_item_new_with_label (name);
2000 submenu = chat_spelling_build_suggestions_menu (
2002 if (submenu == NULL)
2003 gtk_widget_set_sensitive (item, FALSE);
2005 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2007 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2010 menu = chat_spelling_build_suggestions_menu (codes->data,
2013 menu = gtk_menu_new ();
2014 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2015 gtk_widget_set_sensitive (item, FALSE);
2016 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2019 g_list_free (codes);
2021 gtk_widget_show_all (menu);
2032 static EmpathyChatWord *
2033 chat_word_new (EmpathyChat *chat,
2037 EmpathyChatWord *chat_word;
2039 chat_word = g_slice_new0 (EmpathyChatWord);
2041 chat_word->chat = g_object_ref (chat);
2042 chat_word->word = g_strdup (word);
2043 chat_word->code = g_strdup (code);
2049 chat_word_free (EmpathyChatWord *chat_word)
2051 g_object_unref (chat_word->chat);
2052 g_free (chat_word->word);
2053 g_free (chat_word->code);
2054 g_slice_free (EmpathyChatWord, chat_word);
2058 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2059 EmpathyChatWord *chat_word)
2061 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2063 empathy_spell_add_to_dictionary (chat_word->code,
2065 priv->update_misspelled_words_id =
2066 g_idle_add (update_misspelled_words, chat_word->chat);
2070 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2072 GtkWidget *menu, *item, *lang_item, *image;
2075 const gchar *code, *name;
2076 EmpathyChatWord *chat_word;
2078 codes = empathy_spell_get_enabled_language_codes ();
2079 g_assert (codes != NULL);
2080 if (g_list_length (codes) > 1) {
2081 /* translators: %s is the selected word */
2082 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2084 item = gtk_image_menu_item_new_with_mnemonic (label);
2086 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2087 GTK_ICON_SIZE_MENU);
2088 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2091 menu = gtk_menu_new ();
2093 for (l = codes; l; l = l->next) {
2095 name = empathy_spell_get_language_name (code);
2099 lang_item = gtk_image_menu_item_new_with_label (name);
2101 chat_word= chat_word_new (chat_spell->chat,
2102 chat_spell->word, code);
2103 g_object_set_data_full (G_OBJECT (lang_item),
2104 "chat-word", chat_word,
2105 (GDestroyNotify) chat_word_free);
2107 g_signal_connect (G_OBJECT (lang_item), "activate",
2108 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2110 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2112 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2115 name = empathy_spell_get_language_name (code);
2116 g_assert (name != NULL);
2117 /* translators: first %s is the selected word,
2118 * second %s is the language name of the target dictionary */
2119 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2120 chat_spell->word, name);
2121 item = gtk_image_menu_item_new_with_mnemonic (label);
2123 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2124 GTK_ICON_SIZE_MENU);
2125 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2127 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2129 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2130 (GDestroyNotify) chat_word_free);
2132 g_signal_connect (G_OBJECT (item), "activate",
2133 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2136 g_list_free (codes);
2138 gtk_widget_show_all (item);
2144 chat_text_send_cb (GtkMenuItem *menuitem,
2147 chat_input_text_view_send (chat);
2151 chat_input_populate_popup_cb (GtkTextView *view,
2155 GtkTextBuffer *buffer;
2156 GtkTextTagTable *table;
2159 GtkTextIter iter, start, end;
2162 EmpathyChatSpell *chat_spell;
2163 GtkWidget *spell_menu;
2164 GtkWidget *spell_item;
2165 EmpathySmileyManager *smiley_manager;
2166 GtkWidget *smiley_menu;
2169 buffer = gtk_text_view_get_buffer (view);
2171 /* Add the emoticon menu. */
2172 item = gtk_separator_menu_item_new ();
2173 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2174 gtk_widget_show (item);
2176 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2177 image = gtk_image_new_from_icon_name ("face-smile",
2178 GTK_ICON_SIZE_MENU);
2179 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2180 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2181 gtk_widget_show (item);
2183 smiley_manager = empathy_smiley_manager_dup_singleton ();
2184 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2185 chat_insert_smiley_activate_cb,
2187 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2188 g_object_unref (smiley_manager);
2190 /* Add the Send menu item. */
2191 gtk_text_buffer_get_bounds (buffer, &start, &end);
2192 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2193 if (!EMP_STR_EMPTY (str)) {
2194 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2195 g_signal_connect (G_OBJECT (item), "activate",
2196 G_CALLBACK (chat_text_send_cb), chat);
2197 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2198 gtk_widget_show (item);
2202 /* Add the spell check menu item. */
2203 table = gtk_text_buffer_get_tag_table (buffer);
2204 tag = gtk_text_tag_table_lookup (table, "misspelled");
2205 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2206 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2207 GTK_TEXT_WINDOW_WIDGET,
2210 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2212 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2213 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2215 str = gtk_text_buffer_get_text (buffer,
2216 &start, &end, FALSE);
2218 if (!EMP_STR_EMPTY (str)) {
2219 chat_spell = chat_spell_new (chat, str, start, end);
2220 g_object_set_data_full (G_OBJECT (menu),
2221 "chat-spell", chat_spell,
2222 (GDestroyNotify) chat_spell_free);
2224 item = gtk_separator_menu_item_new ();
2225 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2226 gtk_widget_show (item);
2228 /* Spelling suggestions */
2229 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2230 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2231 GTK_ICON_SIZE_MENU);
2232 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2234 spell_menu = chat_spelling_build_menu (chat_spell);
2235 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2238 spell_item = gtk_separator_menu_item_new ();
2239 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2240 gtk_widget_show (spell_item);
2242 /* Add to dictionary */
2243 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2245 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2246 gtk_widget_show (spell_item);
2248 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2249 gtk_widget_show (item);
2255 chat_log_filter (TplEvent *log,
2258 EmpathyChat *chat = user_data;
2259 EmpathyMessage *message;
2260 EmpathyChatPriv *priv = GET_PRIV (chat);
2261 const GList *pending;
2263 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2264 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2266 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2267 message = empathy_message_from_tpl_log_event (log);
2269 for (; pending; pending = g_list_next (pending)) {
2270 if (empathy_message_equal (message, pending->data)) {
2271 g_object_unref (message);
2276 g_object_unref (message);
2282 show_pending_messages (EmpathyChat *chat) {
2283 EmpathyChatPriv *priv = GET_PRIV (chat);
2284 const GList *messages, *l;
2286 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2288 if (chat->view == NULL || priv->tp_chat == NULL)
2291 if (!priv->can_show_pending)
2294 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2296 for (l = messages; l != NULL ; l = g_list_next (l)) {
2297 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2298 chat_message_received (chat, message, TRUE);
2304 got_filtered_messages_cb (GObject *manager,
2305 GAsyncResult *result,
2310 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2311 EmpathyChatPriv *priv = GET_PRIV (chat);
2312 GError *error = NULL;
2314 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2315 result, &messages, &error)) {
2316 DEBUG ("%s. Aborting.", error->message);
2317 empathy_chat_view_append_event (chat->view,
2318 _("Failed to retrieve recent logs"));
2319 g_error_free (error);
2323 for (l = messages; l; l = g_list_next (l)) {
2324 EmpathyMessage *message;
2325 g_assert (TPL_IS_EVENT (l->data));
2327 message = empathy_message_from_tpl_log_event (l->data);
2328 g_object_unref (l->data);
2330 empathy_chat_view_append_message (chat->view, message);
2331 g_object_unref (message);
2333 g_list_free (messages);
2336 /* in case of TPL error, skip backlog and show pending messages */
2337 priv->can_show_pending = TRUE;
2338 show_pending_messages (chat);
2340 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2341 * about it in EmpathyChatPriv definition */
2342 priv->retrieving_backlogs = FALSE;
2343 empathy_chat_messages_read (chat);
2345 /* Turn back on scrolling */
2346 empathy_chat_view_scroll (chat->view, TRUE);
2350 chat_add_logs (EmpathyChat *chat)
2352 EmpathyChatPriv *priv = GET_PRIV (chat);
2359 /* Turn off scrolling temporarily */
2360 empathy_chat_view_scroll (chat->view, FALSE);
2362 /* Add messages from last conversation */
2363 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2364 target = tpl_entity_new_from_room_id (priv->id);
2366 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2368 priv->retrieving_backlogs = TRUE;
2369 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2372 TPL_EVENT_MASK_TEXT,
2376 got_filtered_messages_cb,
2379 g_object_unref (target);
2383 chat_contacts_completion_func (const gchar *s1,
2387 gchar *tmp, *nick1, *nick2;
2394 return s1 ? -1 : +1;
2397 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2398 nick1 = g_utf8_casefold (tmp, -1);
2401 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2402 nick2 = g_utf8_casefold (tmp, -1);
2405 ret = strncmp (nick1, nick2, n);
2414 build_part_message (guint reason,
2416 EmpathyContact *actor,
2417 const gchar *message)
2419 GString *s = g_string_new ("");
2420 const gchar *actor_name = NULL;
2422 if (actor != NULL) {
2423 actor_name = empathy_contact_get_alias (actor);
2426 /* Having an actor only really makes sense for a few actions... */
2428 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2429 g_string_append_printf (s, _("%s has disconnected"), name);
2431 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2432 if (actor_name != NULL) {
2433 /* translators: reverse the order of these arguments
2434 * if the kicked should come before the kicker in your locale.
2436 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2439 g_string_append_printf (s, _("%s was kicked"), name);
2442 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2443 if (actor_name != NULL) {
2444 /* translators: reverse the order of these arguments
2445 * if the banned should come before the banner in your locale.
2447 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2450 g_string_append_printf (s, _("%s was banned"), name);
2454 g_string_append_printf (s, _("%s has left the room"), name);
2457 if (!EMP_STR_EMPTY (message)) {
2458 /* Note to translators: this string is appended to
2459 * notifications like "foo has left the room", with the message
2460 * given by the user living the room. If this poses a problem,
2461 * please let us know. :-)
2463 g_string_append_printf (s, _(" (%s)"), message);
2466 return g_string_free (s, FALSE);
2470 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2471 EmpathyContact *contact,
2472 EmpathyContact *actor,
2478 EmpathyChatPriv *priv = GET_PRIV (chat);
2479 const gchar *name = empathy_contact_get_alias (contact);
2482 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2484 if (priv->block_events_timeout_id != 0)
2488 str = g_strdup_printf (_("%s has joined the room"),
2491 str = build_part_message (reason, name, actor, message);
2494 empathy_chat_view_append_event (chat->view, str);
2499 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2500 EmpathyContact *old_contact,
2501 EmpathyContact *new_contact,
2506 EmpathyChatPriv *priv = GET_PRIV (chat);
2508 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2510 if (priv->block_events_timeout_id == 0) {
2513 str = g_strdup_printf (_("%s is now known as %s"),
2514 empathy_contact_get_alias (old_contact),
2515 empathy_contact_get_alias (new_contact));
2516 empathy_chat_view_append_event (chat->view, str);
2523 chat_reset_size_request (gpointer widget)
2525 gtk_widget_set_size_request (widget, -1, -1);
2531 chat_update_contacts_visibility (EmpathyChat *chat,
2534 EmpathyChatPriv *priv = GET_PRIV (chat);
2535 GtkAllocation allocation;
2537 if (!priv->scrolled_window_contacts) {
2541 if (priv->remote_contact != NULL) {
2545 if (show && priv->contact_list_view == NULL) {
2546 EmpathyContactListStore *store;
2549 /* We are adding the contact list to the chat, we don't want the
2550 * chat view to become too small. If the chat view is already
2551 * smaller than 250 make sure that size won't change. If the
2552 * chat view is bigger the contact list will take some space on
2553 * it but we make sure the chat view don't become smaller than
2554 * 250. Relax the size request once the resize is done */
2555 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2556 min_width = MIN (allocation.width, 250);
2557 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2558 g_idle_add (chat_reset_size_request, priv->vbox_left);
2560 if (priv->contacts_width > 0) {
2561 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2562 priv->contacts_width);
2565 store = empathy_contact_list_store_new (
2566 EMPATHY_CONTACT_LIST (priv->tp_chat));
2567 empathy_contact_list_store_set_show_groups (
2568 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2570 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2571 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2572 EMPATHY_CONTACT_FEATURE_CHAT |
2573 EMPATHY_CONTACT_FEATURE_CALL |
2574 EMPATHY_CONTACT_FEATURE_LOG |
2575 EMPATHY_CONTACT_FEATURE_INFO));
2576 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2577 priv->contact_list_view);
2578 gtk_widget_show (priv->contact_list_view);
2579 gtk_widget_show (priv->scrolled_window_contacts);
2580 g_object_unref (store);
2582 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2583 gtk_widget_hide (priv->scrolled_window_contacts);
2584 if (priv->contact_list_view != NULL) {
2585 gtk_widget_destroy (priv->contact_list_view);
2586 priv->contact_list_view = NULL;
2592 empathy_chat_set_show_contacts (EmpathyChat *chat,
2595 EmpathyChatPriv *priv = GET_PRIV (chat);
2597 priv->show_contacts = show;
2599 chat_update_contacts_visibility (chat, show);
2601 g_object_notify (G_OBJECT (chat), "show-contacts");
2605 chat_remote_contact_changed_cb (EmpathyChat *chat)
2607 EmpathyChatPriv *priv = GET_PRIV (chat);
2609 if (priv->remote_contact != NULL) {
2610 g_object_unref (priv->remote_contact);
2611 priv->remote_contact = NULL;
2616 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2617 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2618 if (priv->remote_contact != NULL) {
2619 g_object_ref (priv->remote_contact);
2620 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2622 else if (priv->tp_chat != NULL) {
2623 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2626 chat_update_contacts_visibility (chat, priv->show_contacts);
2628 g_object_notify (G_OBJECT (chat), "remote-contact");
2629 g_object_notify (G_OBJECT (chat), "id");
2633 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2639 EmpathyChatPriv *priv;
2641 priv = GET_PRIV (chat);
2643 if (!priv->tp_chat) {
2647 chat_composing_remove_timeout (chat);
2648 g_object_unref (priv->tp_chat);
2649 priv->tp_chat = NULL;
2650 g_object_notify (G_OBJECT (chat), "tp-chat");
2652 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2653 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2655 chat_update_contacts_visibility (chat, FALSE);
2659 update_misspelled_words (gpointer data)
2661 EmpathyChat *chat = EMPATHY_CHAT (data);
2662 EmpathyChatPriv *priv = GET_PRIV (chat);
2663 GtkTextBuffer *buffer;
2667 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2669 gtk_text_buffer_get_end_iter (buffer, &iter);
2670 length = gtk_text_iter_get_offset (&iter);
2671 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2672 NULL, length, chat);
2674 priv->update_misspelled_words_id = 0;
2680 conf_spell_checking_cb (GSettings *gsettings_chat,
2684 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2685 EmpathyChatPriv *priv = GET_PRIV (chat);
2686 gboolean spell_checker;
2687 GtkTextBuffer *buffer;
2689 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2692 spell_checker = g_settings_get_boolean (gsettings_chat,
2693 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2695 if (!empathy_spell_supported ()) {
2696 spell_checker = FALSE;
2699 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2701 if (spell_checker == priv->spell_checking_enabled) {
2702 if (spell_checker) {
2703 /* Possibly changed dictionaries,
2704 * update misspelled words. Need to do so in idle
2705 * so the spell checker is updated. */
2706 priv->update_misspelled_words_id =
2707 g_idle_add (update_misspelled_words, chat);
2713 if (spell_checker) {
2716 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2717 buffer, "notify::cursor-position",
2718 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2720 priv->insert_text_id = tp_g_signal_connect_object (
2721 buffer, "insert-text",
2722 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2723 chat, G_CONNECT_AFTER);
2724 priv->delete_range_id = tp_g_signal_connect_object (
2725 buffer, "delete-range",
2726 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2727 chat, G_CONNECT_AFTER);
2729 gtk_text_buffer_create_tag (buffer, "misspelled",
2730 "underline", PANGO_UNDERLINE_ERROR,
2733 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2734 gtk_text_buffer_get_insert (buffer));
2735 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2738 /* Mark misspelled words in the existing buffer.
2739 * Need to do so in idle so the spell checker is updated. */
2740 priv->update_misspelled_words_id =
2741 g_idle_add (update_misspelled_words, chat);
2743 GtkTextTagTable *table;
2746 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2747 priv->notify_cursor_position_id = 0;
2748 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2749 priv->insert_text_id = 0;
2750 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2751 priv->delete_range_id = 0;
2753 table = gtk_text_buffer_get_tag_table (buffer);
2754 tag = gtk_text_tag_table_lookup (table, "misspelled");
2755 gtk_text_tag_table_remove (table, tag);
2757 gtk_text_buffer_delete_mark_by_name (buffer,
2758 "previous-cursor-position");
2761 priv->spell_checking_enabled = spell_checker;
2765 save_paned_pos_timeout (gpointer data)
2767 EmpathyChat *self = data;
2770 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2772 g_settings_set_int (self->priv->gsettings_ui,
2773 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2780 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2784 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2786 if (chat->priv->save_paned_pos_id != 0)
2787 g_source_remove (chat->priv->save_paned_pos_id);
2789 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2790 save_paned_pos_timeout, chat);
2796 chat_create_ui (EmpathyChat *chat)
2798 EmpathyChatPriv *priv = GET_PRIV (chat);
2802 GtkTextBuffer *buffer;
2804 EmpathyThemeManager *theme_mgr;
2806 filename = empathy_file_lookup ("empathy-chat.ui",
2808 gui = empathy_builder_get_file (filename,
2809 "chat_widget", &priv->widget,
2810 "hpaned", &priv->hpaned,
2811 "vbox_left", &priv->vbox_left,
2812 "scrolled_window_chat", &priv->scrolled_window_chat,
2813 "scrolled_window_input", &priv->scrolled_window_input,
2814 "hbox_topic", &priv->hbox_topic,
2815 "expander_topic", &priv->expander_topic,
2816 "label_topic", &priv->label_topic,
2817 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2818 "info_bar_vbox", &priv->info_bar_vbox,
2821 empathy_builder_connect (gui, chat,
2822 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2823 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2828 /* Add message view. */
2829 theme_mgr = empathy_theme_manager_dup_singleton ();
2830 chat->view = empathy_theme_manager_create_view (theme_mgr);
2831 g_object_unref (theme_mgr);
2832 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2833 and other types, even though it's non-editable and doesn't accept any
2834 drags. This steals drag motion for anything inside the scrollbars,
2835 making drag destinations on chat windows far less useful.
2837 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2838 g_signal_connect (chat->view, "focus_in_event",
2839 G_CALLBACK (chat_text_view_focus_in_event_cb),
2841 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2842 GTK_WIDGET (chat->view));
2843 gtk_widget_show (GTK_WIDGET (chat->view));
2845 /* Add input GtkTextView */
2846 chat->input_text_view = empathy_input_text_view_new ();
2847 g_signal_connect (chat->input_text_view, "notify::has-focus",
2848 G_CALLBACK (chat_input_has_focus_notify_cb),
2850 g_signal_connect (chat->input_text_view, "key-press-event",
2851 G_CALLBACK (chat_input_key_press_event_cb),
2853 g_signal_connect (chat->input_text_view, "realize",
2854 G_CALLBACK (chat_input_realize_cb),
2856 g_signal_connect (chat->input_text_view, "populate-popup",
2857 G_CALLBACK (chat_input_populate_popup_cb),
2859 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2860 tp_g_signal_connect_object (buffer, "changed",
2861 G_CALLBACK (chat_input_text_buffer_changed_cb),
2863 tp_g_signal_connect_object (priv->gsettings_chat,
2864 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2865 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2866 conf_spell_checking_cb (priv->gsettings_chat,
2867 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2868 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2869 chat->input_text_view);
2870 gtk_widget_show (chat->input_text_view);
2872 /* Add the (invisible) search bar */
2873 priv->search_bar = empathy_search_bar_new (chat->view);
2874 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2877 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2879 /* Initialy hide the topic, will be shown if not empty */
2880 gtk_widget_hide (priv->hbox_topic);
2882 g_signal_connect (priv->hpaned, "notify::position",
2883 G_CALLBACK (chat_hpaned_pos_changed_cb),
2886 /* Load the paned position */
2887 paned_pos = g_settings_get_int (priv->gsettings_ui,
2888 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2890 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2892 /* Set widget focus order */
2893 list = g_list_append (NULL, priv->search_bar);
2894 list = g_list_append (list, priv->scrolled_window_input);
2895 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2898 list = g_list_append (NULL, priv->vbox_left);
2899 list = g_list_append (list, priv->scrolled_window_contacts);
2900 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2903 list = g_list_append (NULL, priv->hpaned);
2904 list = g_list_append (list, priv->hbox_topic);
2905 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2908 /* Add the main widget in the chat widget */
2909 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2910 g_object_unref (gui);
2914 chat_size_allocate (GtkWidget *widget,
2915 GtkAllocation *allocation)
2917 GtkBin *bin = GTK_BIN (widget);
2918 GtkAllocation child_allocation;
2921 gtk_widget_set_allocation (widget, allocation);
2923 child = gtk_bin_get_child (bin);
2925 if (child && gtk_widget_get_visible (child))
2927 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2928 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2929 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2930 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2932 gtk_widget_size_allocate (child, &child_allocation);
2937 chat_finalize (GObject *object)
2940 EmpathyChatPriv *priv;
2942 chat = EMPATHY_CHAT (object);
2943 priv = GET_PRIV (chat);
2945 DEBUG ("Finalized: %p", object);
2947 if (priv->update_misspelled_words_id != 0)
2948 g_source_remove (priv->update_misspelled_words_id);
2950 if (priv->save_paned_pos_id != 0)
2951 g_source_remove (priv->save_paned_pos_id);
2953 g_object_unref (priv->gsettings_chat);
2954 g_object_unref (priv->gsettings_ui);
2956 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2957 g_list_free (priv->input_history);
2959 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2960 g_list_free (priv->compositors);
2962 chat_composing_remove_timeout (chat);
2964 g_object_unref (priv->account_manager);
2965 g_object_unref (priv->log_manager);
2967 if (priv->tp_chat) {
2968 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2969 chat_invalidated_cb, chat);
2970 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2971 chat_message_received_cb, chat);
2972 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2973 chat_message_acknowledged_cb, chat);
2974 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2975 chat_send_error_cb, chat);
2976 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2977 chat_state_changed_cb, chat);
2978 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2979 chat_property_changed_cb, chat);
2980 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2981 chat_members_changed_cb, chat);
2982 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2983 chat_remote_contact_changed_cb, chat);
2984 empathy_tp_chat_leave (priv->tp_chat, "");
2985 g_object_unref (priv->tp_chat);
2987 if (priv->account) {
2988 g_object_unref (priv->account);
2990 if (priv->remote_contact) {
2991 g_object_unref (priv->remote_contact);
2994 if (priv->block_events_timeout_id) {
2995 g_source_remove (priv->block_events_timeout_id);
2999 g_free (priv->name);
3000 g_free (priv->subject);
3001 g_completion_free (priv->completion);
3003 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3007 chat_constructed (GObject *object)
3009 EmpathyChat *chat = EMPATHY_CHAT (object);
3010 EmpathyChatPriv *priv = GET_PRIV (chat);
3012 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3013 /* First display logs from the logger and then display pending messages */
3014 chat_add_logs (chat);
3017 /* Just display pending messages for rooms */
3018 priv->can_show_pending = TRUE;
3019 show_pending_messages (chat);
3024 empathy_chat_class_init (EmpathyChatClass *klass)
3026 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
3027 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3029 object_class->finalize = chat_finalize;
3030 object_class->get_property = chat_get_property;
3031 object_class->set_property = chat_set_property;
3032 object_class->constructed = chat_constructed;
3034 widget_class->size_allocate = chat_size_allocate;
3036 g_object_class_install_property (object_class,
3038 g_param_spec_object ("tp-chat",
3040 "The tp chat object",
3041 EMPATHY_TYPE_TP_CHAT,
3044 G_PARAM_STATIC_STRINGS));
3045 g_object_class_install_property (object_class,
3047 g_param_spec_object ("account",
3048 "Account of the chat",
3049 "The account of the chat",
3052 G_PARAM_STATIC_STRINGS));
3053 g_object_class_install_property (object_class,
3055 g_param_spec_string ("id",
3057 "The id of the chat",
3060 G_PARAM_STATIC_STRINGS));
3061 g_object_class_install_property (object_class,
3063 g_param_spec_string ("name",
3065 "The name of the chat",
3068 G_PARAM_STATIC_STRINGS));
3069 g_object_class_install_property (object_class,
3071 g_param_spec_string ("subject",
3073 "The subject or topic of the chat",
3076 G_PARAM_STATIC_STRINGS));
3077 g_object_class_install_property (object_class,
3078 PROP_REMOTE_CONTACT,
3079 g_param_spec_object ("remote-contact",
3080 "The remote contact",
3081 "The remote contact is any",
3082 EMPATHY_TYPE_CONTACT,
3084 G_PARAM_STATIC_STRINGS));
3085 g_object_class_install_property (object_class,
3087 g_param_spec_boolean ("show-contacts",
3088 "Contacts' visibility",
3089 "The visibility of the contacts' list",
3092 G_PARAM_STATIC_STRINGS));
3094 g_object_class_install_property (object_class,
3096 g_param_spec_boolean ("sms-channel",
3098 "TRUE if this channel is for sending SMSes",
3100 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3102 g_object_class_install_property (object_class,
3103 PROP_N_MESSAGES_SENDING,
3104 g_param_spec_uint ("n-messages-sending",
3105 "Num Messages Sending",
3106 "The number of messages being sent",
3108 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3110 g_object_class_install_property (object_class,
3111 PROP_NB_UNREAD_MESSAGES,
3112 g_param_spec_uint ("nb-unread-messages",
3113 "Num Unread Messages",
3114 "The number of unread messages",
3116 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3118 signals[COMPOSING] =
3119 g_signal_new ("composing",
3120 G_OBJECT_CLASS_TYPE (object_class),
3124 g_cclosure_marshal_VOID__BOOLEAN,
3128 signals[NEW_MESSAGE] =
3129 g_signal_new ("new-message",
3130 G_OBJECT_CLASS_TYPE (object_class),
3134 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3136 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3138 signals[PART_COMMAND_ENTERED] =
3139 g_signal_new ("part-command-entered",
3140 G_OBJECT_CLASS_TYPE (object_class),
3144 g_cclosure_marshal_VOID__POINTER,
3148 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3152 chat_block_events_timeout_cb (gpointer data)
3154 EmpathyChatPriv *priv = GET_PRIV (data);
3156 priv->block_events_timeout_id = 0;
3162 account_manager_prepared_cb (GObject *source_object,
3163 GAsyncResult *result,
3166 GList *accounts, *l;
3167 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3168 EmpathyChat *chat = user_data;
3169 GError *error = NULL;
3171 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3172 DEBUG ("Failed to prepare the account manager: %s", error->message);
3173 g_error_free (error);
3177 accounts = tp_account_manager_get_valid_accounts (account_manager);
3179 for (l = accounts; l != NULL; l = l->next) {
3180 TpAccount *account = l->data;
3181 tp_g_signal_connect_object (account, "status-changed",
3182 G_CALLBACK (chat_new_connection_cb),
3186 g_list_free (accounts);
3190 empathy_chat_init (EmpathyChat *chat)
3192 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3193 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3196 priv->log_manager = tpl_log_manager_dup_singleton ();
3197 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3198 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3200 priv->contacts_width = -1;
3201 priv->input_history = NULL;
3202 priv->input_history_current = NULL;
3203 priv->account_manager = tp_account_manager_dup ();
3205 tp_account_manager_prepare_async (priv->account_manager, NULL,
3206 account_manager_prepared_cb, chat);
3208 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3209 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3211 /* Block events for some time to avoid having "has come online" or
3212 * "joined" messages. */
3213 priv->block_events_timeout_id =
3214 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3216 /* Add nick name completion */
3217 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3218 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3220 chat_create_ui (chat);
3224 empathy_chat_new (EmpathyTpChat *tp_chat)
3226 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3230 empathy_chat_get_tp_chat (EmpathyChat *chat)
3232 EmpathyChatPriv *priv = GET_PRIV (chat);
3234 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3236 return priv->tp_chat;
3242 GtkWidget *info_bar;
3252 passwd_remember_button_cb (GtkButton *button,
3255 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3259 passwd_not_now_button_cb (GtkButton *button,
3262 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3266 remember_password_infobar_response_cb (GtkWidget *info_bar,
3270 EmpathyChatPriv *priv = GET_PRIV (data->self);
3272 if (response_id == GTK_RESPONSE_OK) {
3273 DEBUG ("Saving room password");
3274 empathy_keyring_set_room_password_async (priv->account,
3275 empathy_tp_chat_get_id (priv->tp_chat),
3280 gtk_widget_destroy (info_bar);
3281 g_free (data->password);
3282 g_slice_free (PasswordData, data);
3286 chat_prompt_to_save_password (EmpathyChat *self,
3289 GtkWidget *content_area;
3296 /* save the password in case it needs to be saved */
3297 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3299 /* Remove all previous widgets */
3300 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3301 gtk_container_forall (GTK_CONTAINER (content_area),
3302 (GtkCallback) gtk_widget_destroy, NULL);
3303 data->button = NULL;
3306 data->spinner = NULL;
3308 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3309 GTK_MESSAGE_QUESTION);
3311 hbox = gtk_hbox_new (FALSE, 5);
3312 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3315 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3316 GTK_ICON_SIZE_DIALOG);
3317 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3320 label = gtk_label_new (_("Would you like to store this password?"));
3321 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3323 /* Add 'Remember' button */
3324 alig = gtk_alignment_new (0, 0.5, 1, 0);
3326 button = gtk_button_new_with_label (_("Remember"));
3327 gtk_container_add (GTK_CONTAINER (alig), button);
3328 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3330 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3333 /* Add 'Not now' button */
3334 alig = gtk_alignment_new (0, 0.5, 1, 0);
3336 button = gtk_button_new_with_label (_("Not now"));
3337 gtk_container_add (GTK_CONTAINER (alig), button);
3338 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3340 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3344 g_signal_handler_disconnect (data->info_bar, data->response_id);
3345 g_signal_connect (data->info_bar, "response",
3346 G_CALLBACK (remember_password_infobar_response_cb), data);
3348 gtk_widget_show_all (data->info_bar);
3352 provide_password_cb (GObject *tp_chat,
3356 PasswordData *data = user_data;
3357 EmpathyChat *self = data->self;
3358 EmpathyChatPriv *priv = GET_PRIV (self);
3359 GError *error = NULL;
3361 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3363 DEBUG ("error: %s", error->message);
3364 /* FIXME: what should we do if that's another error? Close the channel?
3365 * Display the raw D-Bus error to the user isn't very useful */
3366 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3368 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3369 gtk_widget_set_sensitive (data->entry, TRUE);
3370 gtk_widget_grab_focus (data->entry);
3373 gtk_info_bar_set_message_type (
3374 GTK_INFO_BAR (data->info_bar),
3378 gtk_widget_set_sensitive (data->button, TRUE);
3379 gtk_button_set_label (GTK_BUTTON (data->button),
3383 gtk_label_set_text (GTK_LABEL (data->label),
3384 _("Wrong password; please try again:"));
3387 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3388 gtk_widget_hide (data->spinner);
3390 g_error_free (error);
3394 if (empathy_keyring_is_available ()) {
3395 /* ask whether they want to save the password */
3396 chat_prompt_to_save_password (self, data);
3398 /* Get rid of the password info bar finally */
3399 gtk_widget_destroy (data->info_bar);
3400 g_slice_free (PasswordData, data);
3404 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3405 gtk_widget_grab_focus (self->input_text_view);
3409 password_infobar_response_cb (GtkWidget *info_bar,
3413 EmpathyChatPriv *priv = GET_PRIV (data->self);
3414 const gchar *password;
3416 if (response_id != GTK_RESPONSE_OK) {
3417 gtk_widget_destroy (info_bar);
3418 g_slice_free (PasswordData, data);
3422 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3424 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3425 provide_password_cb, data);
3427 gtk_widget_set_sensitive (data->button, FALSE);
3428 gtk_widget_set_sensitive (data->entry, FALSE);
3430 gtk_spinner_start (GTK_SPINNER (data->spinner));
3431 gtk_widget_show (data->spinner);
3435 password_entry_activate_cb (GtkWidget *entry,
3438 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3442 passwd_join_button_cb (GtkButton *button,
3445 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3449 clear_icon_released_cb (GtkEntry *entry,
3450 GtkEntryIconPosition icon_pos,
3454 gtk_entry_set_text (entry, "");
3458 password_entry_changed_cb (GtkEditable *entry,
3463 str = gtk_entry_get_text (GTK_ENTRY (entry));
3465 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3466 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3470 infobar_chat_invalidated_cb (TpProxy *proxy,
3474 gpointer password_infobar)
3476 /* Destroy the password infobar whenever a channel is invalidated
3477 * so we don't have multiple infobars when the MUC is rejoined */
3478 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3482 display_password_info_bar (EmpathyChat *self)
3484 EmpathyChatPriv *priv = GET_PRIV (self);
3485 GtkWidget *info_bar;
3486 GtkWidget *content_area;
3496 data = g_slice_new0 (PasswordData);
3498 info_bar = gtk_info_bar_new ();
3499 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3500 GTK_MESSAGE_QUESTION);
3502 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3504 hbox = gtk_hbox_new (FALSE, 5);
3505 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3508 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3509 GTK_ICON_SIZE_DIALOG);
3510 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3513 label = gtk_label_new (_("This room is protected by a password:"));
3514 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3516 /* Add password entry */
3517 entry = gtk_entry_new ();
3518 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3519 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3521 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3522 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3523 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3524 GTK_ENTRY_ICON_SECONDARY, FALSE);
3526 g_signal_connect (entry, "icon-release",
3527 G_CALLBACK (clear_icon_released_cb), data);
3528 g_signal_connect (entry, "changed",
3529 G_CALLBACK (password_entry_changed_cb), data);
3531 g_signal_connect (entry, "activate",
3532 G_CALLBACK (password_entry_activate_cb), data);
3534 /* Focus the password entry once it's realized */
3535 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3537 /* Add 'Join' button */
3538 alig = gtk_alignment_new (0, 0.5, 1, 0);
3540 button = gtk_button_new_with_label (_("Join"));
3541 gtk_container_add (GTK_CONTAINER (alig), button);
3542 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3544 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3548 spinner = gtk_spinner_new ();
3549 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3551 /* Save some data for messing around with later */
3553 data->info_bar = info_bar;
3554 data->button = button;
3555 data->label = label;
3556 data->entry = entry;
3557 data->spinner = spinner;
3559 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3561 gtk_widget_show_all (hbox);
3563 tp_g_signal_connect_object (priv->tp_chat,
3564 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3567 data->response_id = g_signal_connect (info_bar, "response",
3568 G_CALLBACK (password_infobar_response_cb), data);
3570 gtk_widget_show_all (info_bar);
3571 /* ... but hide the spinner */
3572 gtk_widget_hide (spinner);
3574 /* prevent the user from typing anything */
3575 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3579 provide_saved_password_cb (GObject *tp_chat,
3583 EmpathyChat *self = user_data;
3584 EmpathyChatPriv *priv = GET_PRIV (self);
3585 GError *error = NULL;
3587 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3589 DEBUG ("error: %s", error->message);
3590 /* FIXME: what should we do if that's another error? Close the channel?
3591 * Display the raw D-Bus error to the user isn't very useful */
3592 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3593 display_password_info_bar (self);
3594 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3596 g_error_free (error);
3601 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3602 gtk_widget_grab_focus (self->input_text_view);
3606 chat_room_got_password_cb (GObject *source,
3607 GAsyncResult *result,
3610 EmpathyChat *self = user_data;
3611 EmpathyChatPriv *priv = GET_PRIV (self);
3612 const gchar *password;
3613 GError *error = NULL;
3615 password = empathy_keyring_get_room_password_finish (priv->account,
3618 if (error != NULL) {
3619 DEBUG ("Couldn't get room password: %s\n", error->message);
3620 g_clear_error (&error);
3622 display_password_info_bar (self);
3623 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3627 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3628 provide_saved_password_cb, self);
3632 chat_password_needed_changed_cb (EmpathyChat *self)
3634 EmpathyChatPriv *priv = GET_PRIV (self);
3636 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3637 empathy_keyring_get_room_password_async (priv->account,
3638 empathy_tp_chat_get_id (priv->tp_chat),
3639 chat_room_got_password_cb, self);
3644 chat_sms_channel_changed_cb (EmpathyChat *self)
3646 EmpathyChatPriv *priv = GET_PRIV (self);
3648 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3649 g_object_notify (G_OBJECT (self), "sms-channel");
3653 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3655 g_object_notify (G_OBJECT (self), "n-messages-sending");
3659 empathy_chat_set_tp_chat (EmpathyChat *chat,
3660 EmpathyTpChat *tp_chat)
3662 EmpathyChatPriv *priv = GET_PRIV (chat);
3663 GPtrArray *properties;
3665 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3666 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3668 if (priv->tp_chat) {
3672 if (priv->account) {
3673 g_object_unref (priv->account);
3676 priv->tp_chat = g_object_ref (tp_chat);
3677 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3679 g_signal_connect (tp_chat, "invalidated",
3680 G_CALLBACK (chat_invalidated_cb),
3682 g_signal_connect (tp_chat, "message-received-empathy",
3683 G_CALLBACK (chat_message_received_cb),
3685 g_signal_connect (tp_chat, "message_acknowledged",
3686 G_CALLBACK (chat_message_acknowledged_cb),
3688 g_signal_connect (tp_chat, "send-error",
3689 G_CALLBACK (chat_send_error_cb),
3691 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3692 G_CALLBACK (chat_state_changed_cb),
3694 g_signal_connect (tp_chat, "property-changed",
3695 G_CALLBACK (chat_property_changed_cb),
3697 g_signal_connect (tp_chat, "members-changed",
3698 G_CALLBACK (chat_members_changed_cb),
3700 g_signal_connect (tp_chat, "member-renamed",
3701 G_CALLBACK (chat_member_renamed_cb),
3703 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3704 G_CALLBACK (chat_remote_contact_changed_cb),
3706 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3707 G_CALLBACK (chat_password_needed_changed_cb),
3709 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3710 G_CALLBACK (chat_sms_channel_changed_cb),
3712 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3713 G_CALLBACK (chat_n_messages_sending_changed_cb),
3716 /* Get initial value of properties */
3717 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3718 if (properties != NULL) {
3721 for (i = 0; i < properties->len; i++) {
3722 EmpathyTpChatProperty *property;
3724 property = g_ptr_array_index (properties, i);
3725 if (property->value == NULL)
3728 chat_property_changed_cb (priv->tp_chat,
3735 chat_sms_channel_changed_cb (chat);
3736 chat_remote_contact_changed_cb (chat);
3738 if (chat->input_text_view) {
3739 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3740 if (priv->block_events_timeout_id == 0) {
3741 empathy_chat_view_append_event (chat->view, _("Connected"));
3745 g_object_notify (G_OBJECT (chat), "tp-chat");
3746 g_object_notify (G_OBJECT (chat), "id");
3747 g_object_notify (G_OBJECT (chat), "account");
3749 /* This is a noop when tp-chat is set at object construction time and causes
3750 * the pending messages to be show when it's set on the object after it has
3752 show_pending_messages (chat);
3754 /* check if a password is needed */
3755 chat_password_needed_changed_cb (chat);
3759 empathy_chat_get_account (EmpathyChat *chat)
3761 EmpathyChatPriv *priv = GET_PRIV (chat);
3763 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3765 return priv->account;
3769 empathy_chat_get_id (EmpathyChat *chat)
3771 EmpathyChatPriv *priv = GET_PRIV (chat);
3773 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3779 empathy_chat_dup_name (EmpathyChat *chat)
3781 EmpathyChatPriv *priv = GET_PRIV (chat);
3784 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3788 if (!ret && priv->remote_contact) {
3789 ret = empathy_contact_get_alias (priv->remote_contact);
3796 ret = _("Conversation");
3798 if (priv->sms_channel)
3799 /* Translators: this string is a something like
3800 * "Escher Cat (SMS)" */
3801 return g_strdup_printf (_("%s (SMS)"), ret);
3803 return g_strdup (ret);
3807 empathy_chat_get_subject (EmpathyChat *chat)
3809 EmpathyChatPriv *priv = GET_PRIV (chat);
3811 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3813 return priv->subject;
3817 empathy_chat_get_remote_contact (EmpathyChat *chat)
3819 EmpathyChatPriv *priv = GET_PRIV (chat);
3821 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3823 return priv->remote_contact;
3827 empathy_chat_get_contact_menu (EmpathyChat *chat)
3829 EmpathyChatPriv *priv = GET_PRIV (chat);
3830 GtkWidget *menu = NULL;
3832 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3834 if (priv->remote_contact) {
3835 menu = empathy_contact_menu_new (priv->remote_contact,
3836 EMPATHY_CONTACT_FEATURE_CALL |
3837 EMPATHY_CONTACT_FEATURE_LOG |
3838 EMPATHY_CONTACT_FEATURE_INFO |
3839 EMPATHY_CONTACT_FEATURE_BLOCK);
3846 empathy_chat_clear (EmpathyChat *chat)
3848 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3850 empathy_chat_view_clear (chat->view);
3854 empathy_chat_scroll_down (EmpathyChat *chat)
3856 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3858 empathy_chat_view_scroll_down (chat->view);
3862 empathy_chat_cut (EmpathyChat *chat)
3864 GtkTextBuffer *buffer;
3866 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3868 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3869 if (gtk_text_buffer_get_has_selection (buffer)) {
3870 GtkClipboard *clipboard;
3872 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3874 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3879 empathy_chat_copy (EmpathyChat *chat)
3881 GtkTextBuffer *buffer;
3883 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3885 if (empathy_chat_view_get_has_selection (chat->view)) {
3886 empathy_chat_view_copy_clipboard (chat->view);
3890 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3891 if (gtk_text_buffer_get_has_selection (buffer)) {
3892 GtkClipboard *clipboard;
3894 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3896 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3901 EmpathyChatPriv *priv = GET_PRIV (chat);
3903 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3910 GtkClipboard *clipboard;
3912 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3913 start = g_utf8_offset_to_pointer (topic, start_offset);
3914 end = g_utf8_offset_to_pointer (topic, end_offset);
3915 selection = g_strndup (start, end - start);
3917 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3918 gtk_clipboard_set_text (clipboard, selection, -1);
3926 empathy_chat_paste (EmpathyChat *chat)
3928 GtkTextBuffer *buffer;
3929 GtkClipboard *clipboard;
3930 EmpathyChatPriv *priv;
3932 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3934 priv = GET_PRIV (chat);
3936 if (gtk_widget_get_visible (priv->search_bar)) {
3937 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3941 if (priv->tp_chat == NULL ||
3942 !gtk_widget_is_sensitive (chat->input_text_view))
3945 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3946 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3948 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3952 empathy_chat_find (EmpathyChat *chat)
3954 EmpathyChatPriv *priv;
3956 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3958 priv = GET_PRIV (chat);
3960 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3964 empathy_chat_correct_word (EmpathyChat *chat,
3967 const gchar *new_word)
3969 GtkTextBuffer *buffer;
3971 g_return_if_fail (chat != NULL);
3972 g_return_if_fail (new_word != NULL);
3974 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3976 gtk_text_buffer_delete (buffer, start, end);
3977 gtk_text_buffer_insert (buffer, start,
3983 empathy_chat_is_room (EmpathyChat *chat)
3985 EmpathyChatPriv *priv = GET_PRIV (chat);
3987 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3989 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3993 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3995 EmpathyChatPriv *priv = GET_PRIV (self);
3997 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3999 return priv->unread_messages;
4002 /* called when the messages have been read by user */
4004 empathy_chat_messages_read (EmpathyChat *self)
4006 EmpathyChatPriv *priv = GET_PRIV (self);
4008 g_return_if_fail (EMPATHY_IS_CHAT (self));
4010 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4011 * definition. If we are still retrieving the backlogs, do not ACK */
4012 if (priv->retrieving_backlogs)
4015 if (priv->tp_chat != NULL) {
4016 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
4020 /* Return TRUE if on of the contacts in this chat is composing */
4022 empathy_chat_is_composing (EmpathyChat *chat)
4024 return chat->priv->compositors != NULL;
4028 empathy_chat_is_sms_channel (EmpathyChat *self)
4030 EmpathyChatPriv *priv = GET_PRIV (self);
4032 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4034 return priv->sms_channel;
4038 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4040 EmpathyChatPriv *priv;
4042 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4044 priv = GET_PRIV (self);
4046 if (priv->tp_chat == NULL) {
4051 g_object_get (priv->tp_chat,
4052 "n-messages-sending", &n_messages,
4060 empathy_chat_dup_text (EmpathyChat *self)
4062 GtkTextBuffer *buffer;
4063 GtkTextIter start, end;
4065 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4067 gtk_text_buffer_get_bounds (buffer, &start, &end);
4068 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4072 empathy_chat_set_text (EmpathyChat *self,
4075 GtkTextBuffer *buffer;
4077 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4079 gtk_text_buffer_set_text (buffer, text, -1);